summaryrefslogtreecommitdiff
path: root/bin/io.pl
blob: 16e6f1c3f7874933ce1431f1418d288bc5e3aead (plain)
  1. ######################################################################
  2. # LedgerSMB Small Medium Business Accounting
  3. # http://www.ledgersmb.org/
  4. #
  5. # Copyright (C) 2006
  6. # This work contains copyrighted information from a number of sources all used
  7. # with permission.
  8. #
  9. # This file contains source code included with or based on SQL-Ledger which
  10. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  11. # under the GNU General Public License version 2 or, at your option, any later
  12. # version. For a full list including contact information of contributors,
  13. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  14. #
  15. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  16. # Copyright (c) 2002
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. #
  22. # This program is free software; you can redistribute it and/or modify
  23. # it under the terms of the GNU General Public License as published by
  24. # the Free Software Foundation; either version 2 of the License, or
  25. # (at your option) any later version.
  26. #
  27. # This program is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU General Public License for more details.
  31. # You should have received a copy of the GNU General Public License
  32. # along with this program; if not, write to the Free Software
  33. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34. #######################################################################
  35. #
  36. # common routines used in is, ir, oe
  37. #
  38. #######################################################################
  39. use Error qw(:try);
  40. use LedgerSMB::Tax;
  41. use LedgerSMB::Template;
  42. use LedgerSMB::Sysconfig;
  43. # any custom scripts for this one
  44. if (-f "bin/custom/io.pl") {
  45. eval { require "bin/custom/io.pl"; };
  46. }
  47. if (-f "bin/custom/$form->{login}_io.pl") {
  48. eval { require "bin/custom/$form->{login}_io.pl"; };
  49. }
  50. 1;
  51. # end of main
  52. # this is for our long dates
  53. # $locale->text('January')
  54. # $locale->text('February')
  55. # $locale->text('March')
  56. # $locale->text('April')
  57. # $locale->text('May ')
  58. # $locale->text('June')
  59. # $locale->text('July')
  60. # $locale->text('August')
  61. # $locale->text('September')
  62. # $locale->text('October')
  63. # $locale->text('November')
  64. # $locale->text('December')
  65. # this is for our short month
  66. # $locale->text('Jan')
  67. # $locale->text('Feb')
  68. # $locale->text('Mar')
  69. # $locale->text('Apr')
  70. # $locale->text('May')
  71. # $locale->text('Jun')
  72. # $locale->text('Jul')
  73. # $locale->text('Aug')
  74. # $locale->text('Sep')
  75. # $locale->text('Oct')
  76. # $locale->text('Nov')
  77. # $locale->text('Dec')
  78. sub display_row {
  79. my $numrows = shift;
  80. @column_index = qw(runningnumber partnumber description qty);
  81. if ($form->{type} eq "sales_order") {
  82. push @column_index, "ship";
  83. $column_data{ship} = qq|<th class=listheading align=center width="auto">|.$locale->text('Ship').qq|</th>|;
  84. }
  85. if ($form->{type} eq "purchase_order") {
  86. push @column_index, "ship";
  87. $column_data{ship} = qq|<th class=listheading align=center width="auto">|.$locale->text('Recd').qq|</th>|;
  88. }
  89. for (qw(projectnumber partsgroup)) {
  90. $form->{"select$_"} = $form->unescape($form->{"select$_"}) if $form->{"select$_"};
  91. }
  92. if ($form->{language_code} ne $form->{oldlanguage_code}) {
  93. # rebuild partsgroup
  94. $l{language_code} = $form->{language_code};
  95. $l{searchitems} = 'nolabor' if $form->{vc} eq 'customer';
  96. $form->get_partsgroup(\%myconfig, \%l);
  97. if (@ { $form->{all_partsgroup} }) {
  98. $form->{selectpartsgroup} = "<option>\n";
  99. foreach $ref (@ { $form->{all_partsgroup} }) {
  100. if ($ref->{translation}) {
  101. $form->{selectpartsgroup} .= qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{translation}\n|;
  102. } else {
  103. $form->{selectpartsgroup} .= qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{partsgroup}\n|;
  104. }
  105. }
  106. }
  107. $form->{oldlanguage_code} = $form->{language_code};
  108. }
  109. push @column_index, @{LedgerSMB::Sysconfig::io_lineitem_columns};
  110. my $colspan = $#column_index + 1;
  111. $form->{invsubtotal} = 0;
  112. for (split / /, $form->{taxaccounts}) { $form->{"${_}_base"} = 0 }
  113. $column_data{runningnumber} = qq|<th class=listheading nowrap>|.$locale->text('Item').qq|</th>|;
  114. $column_data{partnumber} = qq|<th class=listheading nowrap>|.$locale->text('Number').qq|</th>|;
  115. $column_data{description} = qq|<th class=listheading nowrap>|.$locale->text('Description').qq|</th>|;
  116. $column_data{qty} = qq|<th class=listheading nowrap>|.$locale->text('Qty').qq|</th>|;
  117. $column_data{unit} = qq|<th class=listheading nowrap>|.$locale->text('Unit').qq|</th>|;
  118. $column_data{sellprice} = qq|<th class=listheading nowrap>|.$locale->text('Price').qq|</th>|;
  119. $column_data{discount} = qq|<th class=listheading>%</th>|;
  120. $column_data{linetotal} = qq|<th class=listheading nowrap>|.$locale->text('Extended').qq|</th>|;
  121. $column_data{bin} = qq|<th class=listheading nowrap>|.$locale->text('Bin').qq|</th>|;
  122. $column_data{onhand} = qq|<th class=listheading nowrap>|.$locale->text('OH').qq|</th>|;
  123. print qq|
  124. <tr>
  125. <td>
  126. <table width=100%>
  127. <tr class=listheading>|;
  128. for (@column_index) { print "\n$column_data{$_}" }
  129. print qq|
  130. </tr>
  131. |;
  132. $deliverydate = $locale->text('Delivery Date');
  133. $serialnumber = $locale->text('Serial No.');
  134. $projectnumber = $locale->text('Project');
  135. $group = $locale->text('Group');
  136. $sku = $locale->text('SKU');
  137. $delvar = 'deliverydate';
  138. if ($form->{type} =~ /_(order|quotation)$/) {
  139. $reqdate = $locale->text('Required by');
  140. $delvar = 'reqdate';
  141. }
  142. $exchangerate = $form->parse_amount(\%myconfig, $form->{exchangerate});
  143. $exchangerate = ($exchangerate) ? $exchangerate : 1;
  144. $spc = substr($myconfig{numberformat},-3,1);
  145. for $i (1 .. $numrows) {
  146. if ($spc eq '.') {
  147. ($null, $dec) = split /\./, $form->{"sellprice_$i"};
  148. } else {
  149. ($null, $dec) = split /,/, $form->{"sellprice_$i"};
  150. }
  151. $dec = length $dec;
  152. $decimalplaces = ($dec > 2) ? $dec : 2;
  153. # undo formatting
  154. for (qw(qty oldqty ship discount sellprice)) { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
  155. if ($form->{"qty_$i"} != $form->{"oldqty_$i"}) {
  156. # check pricematrix
  157. @a = split / /, $form->{"pricematrix_$i"};
  158. if (scalar @a > 2) {
  159. foreach $item (@a) {
  160. ($q, $p) = split /:/, $item;
  161. if (($p * 1) && ($form->{"qty_$i"} >= ($q * 1))) {
  162. ($dec) = ($p =~ /\.(\d+)/);
  163. $dec = length $dec;
  164. $decimalplaces = ($dec > 2) ? $dec : 2;
  165. $form->{"sellprice_$i"} = $form->round_amount($p / $exchangerate, $decimalplaces);
  166. }
  167. }
  168. }
  169. }
  170. $discount = $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"}/100, $decimalplaces);
  171. $linetotal = $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
  172. $linetotal = $form->round_amount($linetotal * $form->{"qty_$i"}, 2);
  173. if (($rows = $form->numtextrows($form->{"description_$i"}, 46, 6)) > 1) {
  174. $form->{"description_$i"} = $form->quote($form->{"description_$i"});
  175. $column_data{description} = qq|<td><textarea name="description_$i" rows=$rows cols=46 wrap=soft>$form->{"description_$i"}</textarea></td>|;
  176. } else {
  177. $form->{"description_$i"} = $form->quote($form->{"description_$i"});
  178. $column_data{description} = qq|<td><input name="description_$i" size=48 value="$form->{"description_$i"}"></td>|;
  179. }
  180. for (qw(partnumber sku unit)) { $form->{"${_}_$i"} = $form->quote($form->{"${_}_$i"}) }
  181. $skunumber = qq|
  182. <p><b>$sku</b> $form->{"sku_$i"}| if ($form->{vc} eq 'vendor' && $form->{"sku_$i"});
  183. if ($form->{selectpartsgroup}) {
  184. if ($i < $numrows) {
  185. $partsgroup = qq|
  186. <b>$group</b>
  187. <input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">|;
  188. ($form->{"partsgroup_$i"}) = split /--/, $form->{"partsgroup_$i"};
  189. $partsgroup .= $form->{"partsgroup_$i"};
  190. $partsgroup = "" unless $form->{"partsgroup_$i"};
  191. }
  192. }
  193. $delivery = qq|
  194. <td colspan=2 nowrap>
  195. <b>${$delvar}</b>
  196. <input name="${delvar}_$i" size=11 title="$myconfig{dateformat}" value="$form->{"${delvar}_$i"}"></td>
  197. |;
  198. $column_data{runningnumber} = qq|<td><input name="runningnumber_$i" size=3 value=$i></td>|;
  199. $column_data{partnumber} = qq|<td><input name="partnumber_$i" size=15 value="$form->{"partnumber_$i"}" accesskey="$i" title="[Alt-$i]">$skunumber</td>|;
  200. $column_data{qty} = qq|<td align=right><input name="qty_$i" title="$form->{"onhand_$i"}" size=5 value=|.$form->format_amount(\%myconfig, $form->{"qty_$i"}).qq|></td>|;
  201. $column_data{ship} = qq|<td align=right><input name="ship_$i" size=5 value=|.$form->format_amount(\%myconfig, $form->{"ship_$i"}).qq|></td>|;
  202. $column_data{unit} = qq|<td><input name="unit_$i" size=5 value="$form->{"unit_$i"}"></td>|;
  203. $column_data{sellprice} = qq|<td align=right><input name="sellprice_$i" size=9 value=|.$form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces).qq|></td>|;
  204. $column_data{discount} = qq|<td align=right><input name="discount_$i" size=3 value=|.$form->format_amount(\%myconfig, $form->{"discount_$i"}).qq|></td>|;
  205. $column_data{linetotal} = qq|<td align=right>|.$form->format_amount(\%myconfig, $linetotal, 2).qq|</td>|;
  206. $column_data{bin} = qq|<td>$form->{"bin_$i"}</td>|;
  207. $column_data{onhand} = qq|<td>$form->{"onhand_$i"}</td>|;
  208. print qq|
  209. <tr valign=top>|;
  210. for (@column_index) {
  211. print "\n$column_data{$_}";
  212. }
  213. print qq|
  214. </tr>
  215. <input type=hidden name="oldqty_$i" value="$form->{"qty_$i"}">
  216. |;
  217. for (qw(orderitems_id id bin weight listprice lastcost taxaccounts pricematrix sku onhand assembly inventory_accno_id income_accno_id expense_accno_id)) {
  218. $form->hide_form("${_}_$i");
  219. }
  220. $form->{selectprojectnumber} =~ s/ selected//;
  221. $form->{selectprojectnumber} =~ s/(<option value="\Q$form->{"projectnumber_$i"}\E")/$1 selected/;
  222. $project = qq|
  223. <b>$projectnumber</b>
  224. <select name="projectnumber_$i">$form->{selectprojectnumber}</select>
  225. | if $form->{selectprojectnumber};
  226. if (($rows = $form->numtextrows($form->{"notes_$i"}, 46, 6)) > 1) {
  227. $form->{"notes_$i"} = $form->quote($form->{"notes_$i"});
  228. $notes = qq|<td><textarea name="notes_$i" rows=$rows cols=46 wrap=soft>$form->{"notes_$i"}</textarea></td>|;
  229. } else {
  230. $form->{"notes_$i"} = $form->quote($form->{"notes_$i"});
  231. $notes = qq|<td><input name="notes_$i" size=48 value="$form->{"notes_$i"}"></td>|;
  232. }
  233. $serial = qq|
  234. <td colspan=6 nowrap><b>$serialnumber</b> <input name="serialnumber_$i" value="$form->{"serialnumber_$i"}"></td>| if $form->{type} !~ /_quotation/;
  235. if ($i == $numrows) {
  236. $partsgroup = "";
  237. if ($form->{selectpartsgroup}) {
  238. $partsgroup = qq|
  239. <b>$group</b>
  240. <select name="partsgroup_$i">$form->{selectpartsgroup}</select>
  241. |;
  242. }
  243. $serial = "";
  244. $project = "";
  245. $delivery = "";
  246. $notes = "";
  247. }
  248. # print second and third row
  249. print qq|
  250. <tr valign=top>
  251. $delivery
  252. $notes
  253. $serial
  254. </tr>
  255. <tr valign=top>
  256. <td colspan=$colspan>
  257. $project
  258. $partsgroup
  259. </td>
  260. </tr>
  261. <tr>
  262. <td colspan=$colspan><hr size=1 noshade></td>
  263. </tr>
  264. |;
  265. $skunumber = "";
  266. for (split / /, $form->{"taxaccounts_$i"}) {
  267. $form->{"${_}_base"} += $linetotal;
  268. }
  269. $form->{invsubtotal} += $linetotal;
  270. }
  271. print qq|
  272. </table>
  273. </td>
  274. </tr>
  275. |;
  276. $form->hide_form(qw(audittrail));
  277. print qq|
  278. <input type=hidden name=oldcurrency value=$form->{currency}>
  279. <input type=hidden name=selectpartsgroup value="|.$form->escape($form->{selectpartsgroup},1).qq|">
  280. <input type=hidden name=selectprojectnumber value="|.$form->escape($form->{selectprojectnumber},1).qq|">
  281. |;
  282. }
  283. sub select_item {
  284. if ($form->{vc} eq "vendor") {
  285. @column_index = qw(ndx partnumber sku description partsgroup onhand sellprice);
  286. } else {
  287. @column_index = qw(ndx partnumber description partsgroup onhand sellprice);
  288. }
  289. $column_data{ndx} = qq|<th>&nbsp;</th>|;
  290. $column_data{partnumber} = qq|<th class=listheading>|.$locale->text('Number').qq|</th>|;
  291. $column_data{sku} = qq|<th class=listheading>|.$locale->text('SKU').qq|</th>|;
  292. $column_data{description} = qq|<th class=listheading>|.$locale->text('Description').qq|</th>|;
  293. $column_data{partsgroup} = qq|<th class=listheading>|.$locale->text('Group').qq|</th>|;
  294. $column_data{sellprice} = qq|<th class=listheading>|.$locale->text('Price').qq|</th>|;
  295. $column_data{onhand} = qq|<th class=listheading>|.$locale->text('Qty').qq|</th>|;
  296. $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
  297. # list items with radio button on a form
  298. $form->header;
  299. $title = $locale->text('Select items');
  300. print qq|
  301. <body>
  302. <form method=post action="$form->{script}">
  303. <table width=100%>
  304. <tr>
  305. <th class=listtop>$title</th>
  306. </tr>
  307. <tr height="5"></tr>
  308. <tr>
  309. <td>$option</td>
  310. </tr>
  311. <tr>
  312. <td>
  313. <table width=100%>
  314. <tr class=listheading>|;
  315. for (@column_index) { print "\n$column_data{$_}" }
  316. print qq|
  317. </tr>
  318. |;
  319. my $i = 0;
  320. foreach $ref (@{ $form->{item_list} }) {
  321. $i++;
  322. for (qw(sku partnumber description unit notes partsgroup)) {
  323. $ref->{$_} = $form->quote($ref->{$_});
  324. }
  325. $column_data{ndx} = qq|<td><input name="ndx_$i" class=checkbox type=checkbox value=$i></td>|;
  326. for (qw(partnumber sku description partsgroup)) { $column_data{$_} = qq|<td>$ref->{$_}&nbsp;</td>| }
  327. $column_data{sellprice} = qq|<td align=right>|.$form->format_amount(\%myconfig, $ref->{sellprice} / $exchangerate, 2, "&nbsp;").qq|</td>|;
  328. $column_data{onhand} = qq|<td align=right>|.$form->format_amount(\%myconfig, $ref->{onhand}, '', "&nbsp;").qq|</td>|;
  329. $j++; $j %= 2;
  330. print qq|
  331. <tr class=listrow$j>|;
  332. for (@column_index) {
  333. print "\n$column_data{$_}";
  334. }
  335. print qq|
  336. </tr>
  337. |;
  338. for (qw(partnumber sku description partsgroup partsgroup_id bin weight sellprice listprice lastcost onhand unit assembly taxaccounts inventory_accno_id income_accno_id expense_accno_id pricematrix id notes)) {
  339. print qq|<input type=hidden name="new_${_}_$i" value="$ref->{$_}">\n|;
  340. }
  341. }
  342. print qq|
  343. </table>
  344. </td>
  345. </tr>
  346. <tr>
  347. <td><hr size=3 noshade></td>
  348. </tr>
  349. </table>
  350. <input name=lastndx type=hidden value=$i>
  351. |;
  352. # delete variables
  353. for (qw(nextsub item_list)) { delete $form->{$_} }
  354. $form->{action} = "item_selected";
  355. $form->hide_form;
  356. print qq|
  357. <input type="hidden" name="nextsub" value="item_selected">
  358. <br>
  359. <button class="submit" type="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  360. </form>
  361. </body>
  362. </html>
  363. |;
  364. }
  365. sub item_selected {
  366. $i = $form->{rowcount} - 1;
  367. $i = $form->{assembly_rows} - 1 if ($form->{item} eq 'assembly');
  368. $qty = ($form->{"qty_$form->{rowcount}"}) ? $form->{"qty_$form->{rowcount}"} : 1;
  369. for $j (1 .. $form->{lastndx}) {
  370. if ($form->{"ndx_$j"}) {
  371. $i++;
  372. $form->{"qty_$i"} = $qty;
  373. $form->{"discount_$i"} = $form->{discount} * 100;
  374. $form->{"reqdate_$i"} = $form->{reqdate} if $form->{type} !~ /_quotation/;
  375. for (qw(id partnumber sku description listprice lastcost bin unit weight assembly taxaccounts pricematrix onhand notes inventory_accno_id income_accno_id expense_accno_id)) {
  376. $form->{"${_}_$i"} = $form->{"new_${_}_$j"};
  377. }
  378. $form->{"sellprice_$i"} = $form->{"new_sellprice_$j"} unless $form->{"sellprice_$i"};
  379. $form->{"partsgroup_$i"} = qq|$form->{"new_partsgroup_$j"}--$form->{"new_partsgroup_id_$j"}|;
  380. ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  381. $dec = length $dec;
  382. $decimalplaces1 = ($dec > 2) ? $dec : 2;
  383. ($dec) = ($form->{"lastcost_$i"} =~ /\.(\d+)/);
  384. $dec = length $dec;
  385. $decimalplaces2 = ($dec > 2) ? $dec : 2;
  386. # if there is an exchange rate adjust sellprice
  387. if (($form->{exchangerate} * 1)) {
  388. for (qw(sellprice listprice lastcost)) { $form->{"${_}_$i"} /= $form->{exchangerate} }
  389. # don't format list and cost
  390. $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"}, $decimalplaces1);
  391. }
  392. # this is for the assembly
  393. if ($form->{item} eq 'assembly') {
  394. $form->{"adj_$i"} = 1;
  395. for (qw(sellprice listprice weight)) { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
  396. $form->{sellprice} += ($form->{"sellprice_$i"} * $form->{"qty_$i"});
  397. $form->{weight} += ($form->{"weight_$i"} * $form->{"qty_$i"});
  398. }
  399. $amount = $form->{"sellprice_$i"} * (1 - $form->{"discount_$i"} / 100) * $form->{"qty_$i"};
  400. for (split / /, $form->{"taxaccounts_$i"}) { $form->{"${_}_base"} += $amount }
  401. if (!$form->{taxincluded}) {
  402. my @taxlist= Tax::init_taxes($form, $form->{"taxaccounts_$i"});
  403. $amount += Tax::calculate_taxes(\@taxlist, $form, $amount, 0);
  404. }
  405. $form->{creditremaining} -= $amount;
  406. $form->{"runningnumber_$i"} = $i;
  407. # format amounts
  408. if ($form->{item} ne 'assembly') {
  409. for (qw(sellprice listprice)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces1) }
  410. $form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces2);
  411. }
  412. $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
  413. }
  414. }
  415. $form->{rowcount} = $i;
  416. $form->{assembly_rows} = $i if ($form->{item} eq 'assembly');
  417. $form->{focus} = "description_$i";
  418. # delete all the new_ variables
  419. for $i (1 .. $form->{lastndx}) {
  420. for (qw(id partnumber sku description sellprice listprice lastcost bin unit weight assembly taxaccounts pricematrix onhand notes inventory_accno_id income_accno_id expense_accno_id)) {
  421. delete $form->{"new_${_}_$i"};
  422. }
  423. }
  424. for (qw(ndx lastndx nextsub)) { delete $form->{$_} }
  425. &display_form;
  426. }
  427. sub new_item {
  428. if ($form->{language_code} && $form->{"description_$form->{rowcount}"}) {
  429. $form->error($locale->text('Translation not on file!'));
  430. }
  431. # change callback
  432. $form->{old_callback} = $form->escape($form->{callback},1);
  433. $form->{callback} = $form->escape("$form->{script}?action=display_form",1);
  434. # delete action
  435. delete $form->{action};
  436. # save all other form variables in a previousform variable
  437. if (!$form->{previousform}) {
  438. foreach $key (keys %$form) {
  439. # escape ampersands
  440. $form->{$key} =~ s/&/%26/g;
  441. $form->{previousform} .= qq|$key=$form->{$key}&|;
  442. }
  443. chop $form->{previousform};
  444. $form->{previousform} = $form->escape($form->{previousform}, 1);
  445. }
  446. $i = $form->{rowcount};
  447. for (qw(partnumber description sellprice unit)) { $form->{"${_}_$i"} = $form->quote($form->{"${_}_$i"}) }
  448. $form->header;
  449. print qq|
  450. <body>
  451. <h4 class=error>|.$locale->text('Item not on file!').qq|</h4>|;
  452. if ($myconfig{acs} !~ /(Goods \& Services--Add Part|Goods \& Services--Add Service)/) {
  453. print qq|
  454. <h4>|.$locale->text('What type of item is this?').qq|</h4>
  455. <form method=post action=ic.pl>
  456. <p>
  457. <input class=radio type=radio name=item value=part checked>&nbsp;|.$locale->text('Part')
  458. .qq|<br>
  459. <input class=radio type=radio name=item value=service>&nbsp;|.$locale->text('Service')
  460. .qq|
  461. <input type=hidden name=partnumber value="$form->{"partnumber_$i"}">
  462. <input type=hidden name=description value="$form->{"description_$i"}">
  463. <input type=hidden name=nextsub value=add>
  464. <input type=hidden name=action value=add>
  465. |;
  466. $form->hide_form(qw(previousform rowcount path login sessionid));
  467. print qq|
  468. <p>
  469. <button class="submit" type="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  470. </form>
  471. |;
  472. }
  473. print qq|
  474. </body>
  475. </html>
  476. |;
  477. }
  478. sub display_form {
  479. # if we have a display_form
  480. if ($form->{display_form}) {
  481. &{ "$form->{display_form}" };
  482. exit;
  483. }
  484. &form_header;
  485. $numrows = ++$form->{rowcount};
  486. $subroutine = "display_row";
  487. if ($form->{item} eq 'part') {
  488. # create makemodel rows
  489. &makemodel_row(++$form->{makemodel_rows});
  490. &vendor_row(++$form->{vendor_rows});
  491. $numrows = ++$form->{customer_rows};
  492. $subroutine = "customer_row";
  493. }
  494. if ($form->{item} eq 'assembly') {
  495. # create makemodel rows
  496. &makemodel_row(++$form->{makemodel_rows});
  497. $numrows = ++$form->{customer_rows};
  498. $subroutine = "customer_row";
  499. }
  500. if ($form->{item} eq 'service') {
  501. &vendor_row(++$form->{vendor_rows});
  502. $numrows = ++$form->{customer_rows};
  503. $subroutine = "customer_row";
  504. }
  505. if ($form->{item} eq 'labor') {
  506. $numrows = 0;
  507. }
  508. # create rows
  509. &{ $subroutine }($numrows) if $numrows;
  510. &form_footer;
  511. }
  512. sub check_form {
  513. my @a = ();
  514. my $count = 0;
  515. my $i;
  516. my $j;
  517. my @flds = qw(id runningnumber partnumber description partsgroup qty ship unit sellprice discount oldqty orderitems_id bin weight listprice lastcost taxaccounts pricematrix sku onhand assembly inventory_accno_id income_accno_id expense_accno_id notes reqdate deliverydate serialnumber projectnumber);
  518. # remove any makes or model rows
  519. if ($form->{item} eq 'part') {
  520. for (qw(listprice sellprice lastcost avgcost weight rop markup)) { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
  521. &calc_markup;
  522. @flds = qw(make model);
  523. $count = 0;
  524. @a = ();
  525. for $i (1 .. $form->{makemodel_rows}) {
  526. if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
  527. push @a, {};
  528. $j = $#a;
  529. for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
  530. $count++;
  531. }
  532. }
  533. $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
  534. $form->{makemodel_rows} = $count;
  535. &check_vendor;
  536. &check_customer;
  537. }
  538. if ($form->{item} eq 'service') {
  539. for (qw(sellprice listprice lastcost avgcost markup)) { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
  540. &calc_markup;
  541. &check_vendor;
  542. &check_customer;
  543. }
  544. if ($form->{item} eq 'assembly') {
  545. if (!$form->{project_id}) {
  546. $form->{sellprice} = 0;
  547. $form->{listprice} = 0;
  548. $form->{lastcost} = 0;
  549. $form->{weight} = 0;
  550. }
  551. for (qw(rop stock markup)) { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
  552. @flds = qw(id qty unit bom adj partnumber description sellprice listprice lastcost weight assembly runningnumber partsgroup);
  553. $count = 0;
  554. @a = ();
  555. for $i (1 .. ($form->{assembly_rows} - 1)) {
  556. if ($form->{"qty_$i"}) {
  557. push @a, {};
  558. my $j = $#a;
  559. $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
  560. for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
  561. if (! $form->{project_id}) {
  562. for (qw(sellprice listprice weight lastcost)) { $form->{$_} += ($form->{"${_}_$i"} * $form->{"qty_$i"}) }
  563. }
  564. $count++;
  565. }
  566. }
  567. if ($form->{markup} && $form->{markup} != $form->{oldmarkup}) {
  568. $form->{sellprice} = 0;
  569. &calc_markup;
  570. }
  571. for (qw(sellprice lastcost listprice)) { $form->{$_} = $form->round_amount($form->{$_}, 2) }
  572. $form->redo_rows(\@flds, \@a, $count, $form->{assembly_rows});
  573. $form->{assembly_rows} = $count;
  574. $count = 0;
  575. @flds = qw(make model);
  576. @a = ();
  577. for $i (1 .. ($form->{makemodel_rows})) {
  578. if (($form->{"make_$i"} ne "") || ($form->{"model_$i"} ne "")) {
  579. push @a, {};
  580. my $j = $#a;
  581. for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
  582. $count++;
  583. }
  584. }
  585. $form->redo_rows(\@flds, \@a, $count, $form->{makemodel_rows});
  586. $form->{makemodel_rows} = $count;
  587. &check_customer;
  588. }
  589. if ($form->{type}) {
  590. # this section applies to invoices and orders
  591. # remove any empty numbers
  592. $count = 0;
  593. @a = ();
  594. if ($form->{rowcount}) {
  595. for $i (1 .. $form->{rowcount} - 1) {
  596. if ($form->{"partnumber_$i"}) {
  597. push @a, {};
  598. my $j = $#a;
  599. for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
  600. $count++;
  601. }
  602. }
  603. $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
  604. $form->{rowcount} = $count;
  605. $form->{creditremaining} -= &invoicetotal;
  606. }
  607. }
  608. &display_form;
  609. }
  610. sub calc_markup {
  611. if ($form->{markup}) {
  612. if ($form->{markup} != $form->{oldmarkup}) {
  613. if ($form->{lastcost}) {
  614. $form->{sellprice} = $form->{lastcost} * (1 + $form->{markup}/100);
  615. $form->{sellprice} = $form->round_amount($form->{sellprice}, 2);
  616. } else {
  617. $form->{lastcost} = $form->{sellprice} / (1 + $form->{markup}/100);
  618. $form->{lastcost} = $form->round_amount($form->{lastcost}, 2);
  619. }
  620. }
  621. } else {
  622. if ($form->{lastcost}) {
  623. $form->{markup} = $form->round_amount(((1 - $form->{sellprice} / $form->{lastcost}) * 100), 1);
  624. }
  625. $form->{markup} = "" if $form->{markup} == 0;
  626. }
  627. }
  628. sub invoicetotal {
  629. $form->{oldinvtotal} = 0;
  630. # add all parts and deduct paid
  631. for (split / /, $form->{taxaccounts}) { $form->{"${_}_base"} = 0 }
  632. my ($amount, $sellprice, $discount, $qty);
  633. for $i (1 .. $form->{rowcount}) {
  634. $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
  635. $discount = $form->parse_amount(\%myconfig, $form->{"discount_$i"});
  636. $qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
  637. $amount = $sellprice * (1 - $discount / 100) * $qty;
  638. for (split / /, $form->{"taxaccounts_$i"}) { $form->{"${_}_base"} += $amount }
  639. $form->{oldinvtotal} += $amount;
  640. }
  641. if (!$form->{taxincluded}) {
  642. my @taxlist= Tax::init_taxes($form, $form->{taxaccounts});
  643. $form->{oldinvtotal} += Tax::calculate_taxes(\@taxlist, $form,
  644. $amount, 0);
  645. }
  646. $form->{oldtotalpaid} = 0;
  647. for $i (1 .. $form->{paidaccounts}) {
  648. $form->{oldtotalpaid} += $form->{"paid_$i"};
  649. }
  650. # return total
  651. ($form->{oldinvtotal} - $form->{oldtotalpaid});
  652. }
  653. sub validate_items {
  654. # check if items are valid
  655. if ($form->{rowcount} == 1) {
  656. &update;
  657. exit;
  658. }
  659. for $i (1 .. $form->{rowcount} - 1) {
  660. $form->isblank("partnumber_$i", $locale->text('Number missing in Row [_1]', $i));
  661. }
  662. }
  663. sub purchase_order {
  664. $form->{title} = $locale->text('Add Purchase Order');
  665. $form->{vc} = 'vendor';
  666. $form->{type} = 'purchase_order';
  667. $buysell = 'sell';
  668. &create_form;
  669. }
  670. sub sales_order {
  671. $form->{title} = $locale->text('Add Sales Order');
  672. $form->{vc} = 'customer';
  673. $form->{type} = 'sales_order';
  674. $buysell = 'buy';
  675. &create_form;
  676. }
  677. sub rfq {
  678. $form->{title} = $locale->text('Add Request for Quotation');
  679. $form->{vc} = 'vendor';
  680. $form->{type} = 'request_quotation';
  681. $buysell = 'sell';
  682. &create_form;
  683. }
  684. sub quotation {
  685. $form->{title} = $locale->text('Add Quotation');
  686. $form->{vc} = 'customer';
  687. $form->{type} = 'sales_quotation';
  688. $buysell = 'buy';
  689. &create_form;
  690. }
  691. sub create_form {
  692. for (qw(id printed emailed queued)) { delete $form->{$_} }
  693. $form->{script} = 'oe.pl';
  694. $form->{shipto} = 1;
  695. $form->{rowcount}-- if $form->{rowcount};
  696. $form->{rowcount} = 0 if ! $form->{"$form->{vc}_id"};
  697. do "bin/$form->{script}";
  698. for ("$form->{vc}", "currency") { $form->{"select$_"} = "" }
  699. for (qw(currency employee department intnotes notes language_code taxincluded)) { $temp{$_} = $form->{$_} }
  700. &order_links;
  701. for (keys %temp) { $form->{$_} = $temp{$_} if $temp{$_} }
  702. $form->{exchangerate} = "";
  703. $form->{forex} = "";
  704. if ($form->{currency} ne $form->{defaultcurrency}) {
  705. $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell)));
  706. }
  707. &prepare_order;
  708. &display_form;
  709. }
  710. sub e_mail {
  711. $bcc = qq|<input type=hidden name=bcc value="$form->{bcc}">|;
  712. if ($myconfig{role} =~ /(admin|manager)/) {
  713. $bcc = qq|
  714. <th align=right nowrap=true>|.$locale->text('Bcc').qq|</th>
  715. <td><input name=bcc size=30 value="$form->{bcc}"></td>
  716. |;
  717. }
  718. if ($form->{formname} =~ /(pick|packing|bin)_list/) {
  719. $form->{email} = $form->{shiptoemail} if $form->{shiptoemail};
  720. }
  721. $name = $form->{$form->{vc}};
  722. $name =~ s/--.*//g;
  723. $title = $locale->text('E-mail')." $name";
  724. $form->header;
  725. print qq|
  726. <body>
  727. <form method=post action="$form->{script}">
  728. <table width=100%>
  729. <tr class=listtop>
  730. <th class=listtop>$title</th>
  731. </tr>
  732. <tr height="5"></tr>
  733. <tr>
  734. <td>
  735. <table width=100%>
  736. <tr>
  737. <th align=right nowrap>|.$locale->text('E-mail').qq|</th>
  738. <td><input name=email size=30 value="$form->{email}"></td>
  739. <th align=right nowrap>|.$locale->text('Cc').qq|</th>
  740. <td><input name=cc size=30 value="$form->{cc}"></td>
  741. </tr>
  742. <tr>
  743. <th align=right nowrap>|.$locale->text('Subject').qq|</th>
  744. <td><input name=subject size=30 value="$form->{subject}"></td>
  745. $bcc
  746. </tr>
  747. </table>
  748. </td>
  749. </tr>
  750. <tr>
  751. <td>
  752. <table width=100%>
  753. <tr>
  754. <th align=left nowrap>|.$locale->text('Message').qq|</th>
  755. </tr>
  756. <tr>
  757. <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
  758. </tr>
  759. </table>
  760. </td>
  761. </tr>
  762. <tr>
  763. <td>
  764. |;
  765. $form->{oldmedia} = $form->{media};
  766. $form->{media} = "email";
  767. $form->{format} = "pdf";
  768. &print_options;
  769. for (qw(email cc bcc subject message formname sendmode format language_code action nextsub)) { delete $form->{$_} }
  770. $form->hide_form;
  771. print qq|
  772. </td>
  773. </tr>
  774. <tr>
  775. <td><hr size=3 noshade></td>
  776. </tr>
  777. </table>
  778. <input type="hidden" name="nextsub" value="send_email">
  779. <br>
  780. <button name="action" class="submit" type="submit" value="continue">|.$locale->text('Continue').qq|</button>
  781. </form>
  782. </body>
  783. </html>
  784. |;
  785. }
  786. sub send_email {
  787. $old_form = new Form;
  788. for (keys %$form) { $old_form->{$_} = $form->{$_} }
  789. $old_form->{media} = $old_form->{oldmedia};
  790. &print_form($old_form);
  791. }
  792. sub print_options {
  793. $form->{sendmode} = "attachment";
  794. $form->{copies} = 1 unless $form->{copies};
  795. $form->{SM}{$form->{sendmode}} = "selected";
  796. if ($form->{selectlanguage}) {
  797. $form->{"selectlanguage"} = $form->unescape($form->{"selectlanguage"});
  798. $form->{"selectlanguage"} =~ s/ selected//;
  799. $form->{"selectlanguage"} =~ s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
  800. $lang = qq|<select name=language_code>$form->{selectlanguage}</select>
  801. <input type=hidden name=oldlanguage_code value=$form->{oldlanguage_code}>
  802. <input type=hidden name=selectlanguage value="|.$form->escape($form->{selectlanguage},1).qq|">|;
  803. }
  804. $form->{selectformname} = $form->unescape($form->{selectformname});
  805. $form->{selectformname} =~ s/ selected//;
  806. $form->{selectformname} =~ s/(<option value="\Q$form->{formname}\E")/$1 selected/;
  807. $type = qq|<select name=formname>$form->{selectformname}</select>
  808. <input type=hidden name=selectformname value="|.$form->escape($form->{selectformname},1).qq|">|;
  809. if ($form->{media} eq 'email') {
  810. $media = qq|<select name=sendmode>
  811. <option value=attachment $form->{SM}{attachment}>|.$locale->text('Attachment').qq|
  812. <option value=inline $form->{SM}{inline}>|.$locale->text('In-line').qq|</select>|;
  813. } else {
  814. $media = qq|<select name=media>
  815. <option value="screen">|.$locale->text('Screen');
  816. if (%{LedgerSMB::Sysconfig::printer} && ${LedgerSMB::Sysconfig::latex}) {
  817. for (sort keys %{LedgerSMB::Sysconfig::printer}) { $media .= qq|
  818. <option value="$_">$_| }
  819. }
  820. if (${LedgerSMB::Sysconfig::latex}) {
  821. $media .= qq|
  822. <option value="queue">|.$locale->text('Queue');
  823. }
  824. $media .= qq|</select>|;
  825. # set option selected
  826. $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
  827. }
  828. $form->{selectformat} = qq|<option value="html">html\n|;
  829. # <option value="txt">|.$locale->text('Text');
  830. if (${LedgerSMB::Sysconfig::latex}) {
  831. $form->{selectformat} .= qq|
  832. <option value="postscript">|.$locale->text('Postscript').qq|
  833. <option value="pdf">|.$locale->text('PDF');
  834. }
  835. $format = qq|<select name=format>$form->{selectformat}</select>|;
  836. $format =~ s/(<option value="\Q$form->{format}\E")/$1 selected/;
  837. $format .= qq|
  838. <input type=hidden name=selectformat value="|.$form->escape($form->{selectformat},1).qq|">|;
  839. print qq|
  840. <table width=100%>
  841. <tr>
  842. <td>$type</td>
  843. <td>$lang</td>
  844. <td>$format</td>
  845. <td>$media</td>
  846. |;
  847. if (%{LedgerSMB::Sysconfig::printer} && ${LedgerSMB::Sysconfig::latex} && $form->{media} ne 'email') {
  848. print qq|
  849. <td nowrap>|.$locale->text('Copies').qq|
  850. <input name=copies size=2 value=$form->{copies}></td>
  851. |;
  852. }
  853. # $locale->text('Printed')
  854. # $locale->text('E-mailed')
  855. # $locale->text('Queued')
  856. # $locale->text('Scheduled')
  857. %status = ( printed => 'Printed',
  858. emailed => 'E-mailed',
  859. queued => 'Queued',
  860. recurring => 'Scheduled' );
  861. print qq|<td align=right width=90%>|;
  862. for (qw(printed emailed queued recurring)) {
  863. if ($form->{$_} =~ /$form->{formname}/) {
  864. print $locale->text($status{$_}).qq|<br>|;
  865. }
  866. }
  867. print qq|
  868. </td>
  869. </tr>
  870. |;
  871. $form->{groupprojectnumber} = "checked" if $form->{groupprojectnumber};
  872. $form->{grouppartsgroup} = "checked" if $form->{grouppartsgroup};
  873. for (qw(runningnumber partnumber description bin)) { $sortby{$_} = "checked" if $form->{sortby} eq $_ }
  874. print qq|
  875. <tr>
  876. <td colspan=6>|.$locale->text('Group by').qq| ->
  877. <input name=groupprojectnumber type=checkbox class=checkbox $form->{groupprojectnumber}>
  878. |.$locale->text('Project').qq|
  879. <input name=grouppartsgroup type=checkbox class=checkbox $form->{grouppartsgroup}>
  880. |.$locale->text('Group').qq|
  881. </td>
  882. </tr>
  883. <tr>
  884. <td colspan=6>|.$locale->text('Sort by').qq| ->
  885. <input name=sortby type=radio class=radio value=runningnumber $sortby{runningnumber}>
  886. |.$locale->text('Item').qq|
  887. <input name=sortby type=radio class=radio value=partnumber $sortby{partnumber}>
  888. |.$locale->text('Number').qq|
  889. <input name=sortby type=radio class=radio value=description $sortby{description}>
  890. |.$locale->text('Description').qq|
  891. <input name=sortby type=radio class=radio value=bin $sortby{bin}>
  892. |.$locale->text('Bin').qq|
  893. </td>
  894. </tr>
  895. </table>
  896. |;
  897. }
  898. sub print {
  899. # if this goes to the printer pass through
  900. if ($form->{media} !~ /(screen|email)/) {
  901. $form->error($locale->text('Select txt, postscript or PDF!')) if ($form->{format} !~ /(txt|postscript|pdf)/);
  902. $old_form = new Form;
  903. for (keys %$form) { $old_form->{$_} = $form->{$_} }
  904. }
  905. &print_form($old_form);
  906. }
  907. sub print_form {
  908. my ($old_form) = @_;
  909. $inv = "inv";
  910. $due = "due";
  911. $numberfld = "sinumber";
  912. $display_form = ($form->{display_form}) ? $form->{display_form} : "display_form";
  913. if ($form->{formname} eq "invoice") {
  914. $form->{label} = $locale->text('Invoice');
  915. }
  916. if ($form->{formname} eq 'sales_order') {
  917. $inv = "ord";
  918. $due = "req";
  919. $form->{label} = $locale->text('Sales Order');
  920. $numberfld = "sonumber";
  921. $order = 1;
  922. }
  923. if ($form->{formname} eq 'work_order') {
  924. $inv = "ord";
  925. $due = "req";
  926. $form->{label} = $locale->text('Work Order');
  927. $numberfld = "sonumber";
  928. $order = 1;
  929. }
  930. if ($form->{formname} eq 'packing_list') {
  931. # we use the same packing list as from an invoice
  932. $form->{label} = $locale->text('Packing List');
  933. if ($form->{type} ne 'invoice') {
  934. $inv = "ord";
  935. $due = "req";
  936. $numberfld = "sonumber";
  937. $order = 1;
  938. $filled = 0;
  939. for ($i = 1; $i < $form->{rowcount}; $i++) {
  940. if ($form->{"ship_$i"}) {
  941. $filled = 1;
  942. last;
  943. }
  944. }
  945. if (!$filled) {
  946. for (1 .. $form->{rowcount}) { $form->{"ship_$_"} = $form->{"qty_$_"} }
  947. }
  948. }
  949. }
  950. if ($form->{formname} eq 'pick_list') {
  951. $form->{label} = $locale->text('Pick List');
  952. if ($form->{type} ne 'invoice') {
  953. $inv = "ord";
  954. $due = "req";
  955. $order = 1;
  956. $numberfld = "sonumber";
  957. }
  958. }
  959. if ($form->{formname} eq 'purchase_order') {
  960. $inv = "ord";
  961. $due = "req";
  962. $form->{label} = $locale->text('Purchase Order');
  963. $numberfld = "ponumber";
  964. $order = 1;
  965. }
  966. if ($form->{formname} eq 'bin_list') {
  967. $inv = "ord";
  968. $due = "req";
  969. $form->{label} = $locale->text('Bin List');
  970. $numberfld = "ponumber";
  971. $order = 1;
  972. }
  973. if ($form->{formname} eq 'sales_quotation') {
  974. $inv = "quo";
  975. $due = "req";
  976. $form->{label} = $locale->text('Quotation');
  977. $numberfld = "sqnumber";
  978. $order = 1;
  979. }
  980. if ($form->{formname} eq 'request_quotation') {
  981. $inv = "quo";
  982. $due = "req";
  983. $form->{label} = $locale->text('Quotation');
  984. $numberfld = "rfqnumber";
  985. $order = 1;
  986. }
  987. &validate_items;
  988. $form->{"${inv}date"} = $form->{transdate};
  989. $form->isblank("email", $locale->text('E-mail address missing!')) if ($form->{media} eq 'email');
  990. $form->isblank("${inv}date", $locale->text($form->{label} .' Date missing!'));
  991. # get next number
  992. if (! $form->{"${inv}number"}) {
  993. $form->{"${inv}number"} = $form->update_defaults(\%myconfig, $numberfld);
  994. if ($form->{media} eq 'screen') {
  995. &update;
  996. exit;
  997. }
  998. }
  999. # $locale->text('Invoice Number missing!')
  1000. # $locale->text('Invoice Date missing!')
  1001. # $locale->text('Packing List Number missing!')
  1002. # $locale->text('Packing List Date missing!')
  1003. # $locale->text('Order Number missing!')
  1004. # $locale->text('Order Date missing!')
  1005. # $locale->text('Quotation Number missing!')
  1006. # $locale->text('Quotation Date missing!')
  1007. &{ "$form->{vc}_details" };
  1008. @a = ();
  1009. foreach $i (1 .. $form->{rowcount}) {
  1010. push @a, ("partnumber_$i", "description_$i", "projectnumber_$i", "partsgroup_$i", "serialnumber_$i", "bin_$i", "unit_$i", "notes_$i");
  1011. }
  1012. for (split / /, $form->{taxaccounts}) { push @a, "${_}_description" }
  1013. $ARAP = ($form->{vc} eq 'customer') ? "AR" : "AP";
  1014. push @a, $ARAP;
  1015. # format payment dates
  1016. for $i (1 .. $form->{paidaccounts} - 1) {
  1017. if (exists $form->{longformat}) {
  1018. $form->{"datepaid_$i"} = $locale->date(\%myconfig, $form->{"datepaid_$i"}, $form->{longformat});
  1019. }
  1020. push @a, "${ARAP}_paid_$i", "source_$i", "memo_$i";
  1021. }
  1022. $form->format_string(@a);
  1023. ($form->{employee}) = split /--/, $form->{employee};
  1024. ($form->{warehouse}, $form->{warehouse_id}) = split /--/, $form->{warehouse};
  1025. # this is a label for the subtotals
  1026. $form->{groupsubtotaldescription} = $locale->text('Subtotal') if not exists $form->{groupsubtotaldescription};
  1027. delete $form->{groupsubtotaldescription} if $form->{deletegroupsubtotal};
  1028. $duedate = $form->{"${due}date"};
  1029. # create the form variables
  1030. if ($order) {
  1031. OE->order_details(\%myconfig, \%$form);
  1032. } else {
  1033. IS->invoice_details(\%myconfig, \%$form);
  1034. }
  1035. if (exists $form->{longformat}) {
  1036. $form->{"${due}date"} = $duedate;
  1037. for ("${inv}date", "${due}date", "shippingdate", "transdate") { $form->{$_} = $locale->date(\%myconfig, $form->{$_}, $form->{longformat}) }
  1038. }
  1039. @a = qw(name address1 address2 city state zipcode country contact phone fax email);
  1040. $shipto = 1;
  1041. # if there is no shipto fill it in from billto
  1042. foreach $item (@a) {
  1043. if ($form->{"shipto$item"}) {
  1044. $shipto = 0;
  1045. last;
  1046. }
  1047. }
  1048. if ($shipto) {
  1049. if ($form->{formname} eq 'purchase_order' || $form->{formname} eq 'request_quotation') {
  1050. $form->{shiptoname} = $myconfig{company};
  1051. $form->{shiptoaddress1} = $myconfig{address};
  1052. $form->{shiptoaddress1} =~ s/\\n/\n/g;
  1053. } else {
  1054. if ($form->{formname} !~ /bin_list/) {
  1055. for (@a) { $form->{"shipto$_"} = $form->{$_} }
  1056. }
  1057. }
  1058. }
  1059. # some of the stuff could have umlauts so we translate them
  1060. push @a, qw(contact shiptoname shiptoaddress1 shiptoaddress2 shiptocity shiptostate shiptozipcode shiptocountry shiptocontact shiptoemail shippingpoint shipvia notes intnotes employee warehouse);
  1061. push @a, ("${inv}number", "${inv}date", "${due}date");
  1062. for (qw(company address tel fax businessnumber)) { $form->{$_} = $myconfig{$_} }
  1063. $form->{address} =~ s/\\n/\n/g;
  1064. for (qw(name email)) { $form->{"user$_"} = $myconfig{$_} }
  1065. push @a, qw(company address tel fax businessnumber username useremail);
  1066. for (qw(notes intnotes)) { $form->{$_} =~ s/^\s+//g }
  1067. # before we format replace <%var%>
  1068. for (qw(notes intnotes message)) { $form->{$_} =~ s/<%(.*?)%>/$form->{$1}/g }
  1069. $form->format_string(@a);
  1070. $form->{templates} = "$myconfig{templates}";
  1071. $form->{IN} = "$form->{formname}.$form->{format}";
  1072. if ($form->{format} =~ /(postscript|pdf)/) {
  1073. $form->{IN} =~ s/$&$/tex/;
  1074. }
  1075. $form->{pre} = "<body bgcolor=#ffffff>\n<pre>" if $form->{format} eq 'txt';
  1076. if ($form->{media} !~ /(screen|queue|email)/) {
  1077. $form->{OUT} = "${LedgerSMB::SysConfig::printer}{$form->{media}}";
  1078. $form->{printmode} = '|-';
  1079. $form->{OUT} =~ s/<%(fax)%>/<%$form->{vc}$1%>/;
  1080. $form->{OUT} =~ s/<%(.*?)%>/$form->{$1}/g;
  1081. if ($form->{printed} !~ /$form->{formname}/) {
  1082. $form->{printed} .= " $form->{formname}";
  1083. $form->{printed} =~ s/^ //;
  1084. $form->update_status(\%myconfig);
  1085. }
  1086. $old_form->{printed} = $form->{printed} if defined %$old_form;
  1087. %audittrail = ( tablename => ($order) ? 'oe' : lc $ARAP,
  1088. reference => $form->{"${inv}number"},
  1089. formname => $form->{formname},
  1090. action => 'printed',
  1091. id => $form->{id} );
  1092. $old_form->{audittrail} .= $form->audittrail("", \%myconfig, \%audittrail) if defined %$old_form;
  1093. }
  1094. if ($form->{media} eq 'email') {
  1095. $form->{subject} = qq|$form->{label} $form->{"${inv}number"}| unless $form->{subject};
  1096. $form->{plainpaper} = 1;
  1097. $form->{OUT} = "${LedgerSMB::Sysconfig::sendmail}";
  1098. $form->{printmode} = '|-';
  1099. if ($form->{emailed} !~ /$form->{formname}/) {
  1100. $form->{emailed} .= " $form->{formname}";
  1101. $form->{emailed} =~ s/^ //;
  1102. # save status
  1103. $form->update_status(\%myconfig);
  1104. }
  1105. $now = scalar localtime;
  1106. $cc = $locale->text('Cc: [_1]', $form->{cc}).qq|\n| if $form->{cc};
  1107. $bcc = $locale->text('Bcc: [_1]', $form->{bcc}).qq|\n| if $form->{bcc};
  1108. if (defined %$old_form) {
  1109. $old_form->{intnotes} = qq|$old_form->{intnotes}\n\n| if $old_form->{intnotes};
  1110. $old_form->{intnotes} .= qq|[email]\n|
  1111. .$locale->text('Date: [_1]', $now).qq|\n|
  1112. .$locale->text('To: [_1]', $form->{email}).qq|\n${cc}${bcc}|
  1113. .$locale->text('Subject: [_1]', $form->{subject}).qq|\n|;
  1114. $old_form->{intnotes} .= qq|\n|.$locale->text('Message').qq|: |;
  1115. $old_form->{intnotes} .= ($form->{message}) ? $form->{message} : $locale->text('sent');
  1116. $old_form->{message} = $form->{message};
  1117. $old_form->{emailed} = $form->{emailed};
  1118. $old_form->{format} = "postscript" if $myconfig{printer};
  1119. $old_form->{media} = $myconfig{printer};
  1120. $old_form->save_intnotes(\%myconfig, ($order) ? 'oe' : lc $ARAP);
  1121. }
  1122. %audittrail = ( tablename => ($order) ? 'oe' : lc $ARAP,
  1123. reference => $form->{"${inv}number"},
  1124. formname => $form->{formname},
  1125. action => 'emailed',
  1126. id => $form->{id} );
  1127. $old_form->{audittrail} .= $form->audittrail("", \%myconfig, \%audittrail) if defined %$old_form;
  1128. }
  1129. if ($form->{media} eq 'queue') {
  1130. %queued = split / /, $form->{queued};
  1131. if ($filename = $queued{$form->{formname}}) {
  1132. $form->{queued} =~ s/$form->{formname} $filename//;
  1133. unlink "${LedgerSMB::Sysconfig::spool}/$filename";
  1134. $filename =~ s/\..*$//g;
  1135. } else {
  1136. $filename = time;
  1137. $filename .= $$;
  1138. }
  1139. $filename .= ($form->{format} eq 'postscript') ? '.ps' : '.pdf';
  1140. $form->{OUT} = "${LedgerSMB::Sysconfig::spool}/$filename";
  1141. $form->{printmode} = '>';
  1142. $form->{queued} .= " $form->{formname} $filename";
  1143. $form->{queued} =~ s/^ //;
  1144. # save status
  1145. $form->update_status(\%myconfig);
  1146. $old_form->{queued} = $form->{queued};
  1147. %audittrail = ( tablename => ($order) ? 'oe' : lc $ARAP,
  1148. reference => $form->{"${inv}number"},
  1149. formname => $form->{formname},
  1150. action => 'queued',
  1151. id => $form->{id} );
  1152. $old_form->{audittrail} .= $form->audittrail("", \%myconfig, \%audittrail);
  1153. }
  1154. $form->format_string("email", "cc", "bcc");
  1155. $form->{fileid} = $form->{"${inv}number"};
  1156. $form->{fileid} =~ s/(\s|\W)+//g;
  1157. if (($form->{'media'} eq 'screen') and ($form->{'format'} eq 'html')) {
  1158. my $template = LedgerSMB::Template->new(\%myconfig, $form->{'formname'}, 'HTML');
  1159. try {
  1160. $template->render($form);
  1161. $form->header;
  1162. print $template->{'output'};
  1163. exit;
  1164. } catch Error::Simple with {
  1165. my $E = shift;
  1166. $form->error($E->stacktrace);
  1167. };
  1168. }
  1169. $form->parse_template(\%myconfig, ${LedgerSMB::Sysconfig::userspath});
  1170. # if we got back here restore the previous form
  1171. if (defined %$old_form) {
  1172. $old_form->{"${inv}number"} = $form->{"${inv}number"};
  1173. # restore and display form
  1174. for (keys %$old_form) { $form->{$_} = $old_form->{$_} }
  1175. delete $form->{pre};
  1176. $form->{rowcount}--;
  1177. for (qw(exchangerate creditlimit creditremaining)) { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
  1178. for $i (1 .. $form->{paidaccounts}) {
  1179. for (qw(paid exchangerate)) { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
  1180. }
  1181. &{ "$display_form" };
  1182. }
  1183. }
  1184. sub customer_details {
  1185. IS->customer_details(\%myconfig, \%$form);
  1186. }
  1187. sub vendor_details {
  1188. IR->vendor_details(\%myconfig, \%$form);
  1189. }
  1190. sub ship_to {
  1191. $title = $form->{title};
  1192. $form->{title} = $locale->text('Ship to');
  1193. for (qw(exchangerate creditlimit creditremaining)) { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
  1194. for (1 .. $form->{paidaccounts}) { $form->{"paid_$_"} = $form->parse_amount(\%myconfig, $form->{"paid_$_"}) }
  1195. # get details for name
  1196. &{ "$form->{vc}_details" };
  1197. $number = ($form->{vc} eq 'customer') ? $locale->text('Customer Number') : $locale->text('Vendor Number');
  1198. $nextsub = ($form->{display_form}) ? $form->{display_form} : "display_form";
  1199. $form->{rowcount}--;
  1200. $form->header;
  1201. print qq|
  1202. <body>
  1203. <form method=post action=$form->{script}>
  1204. <table width=100%>
  1205. <tr>
  1206. <td>
  1207. <table>
  1208. <tr class=listheading>
  1209. <th class=listheading colspan=2 width=50%>|.$locale->text('Billing Address').qq|</th>
  1210. <th class=listheading width=50%>|.$locale->text('Shipping Address').qq|</th>
  1211. </tr>
  1212. <tr height="5"></tr>
  1213. <tr>
  1214. <th align=right nowrap>$number</th>
  1215. <td>$form->{"$form->{vc}number"}</td>
  1216. </tr>
  1217. <tr>
  1218. <th align=right nowrap>|.$locale->text('Company Name').qq|</th>
  1219. <td>$form->{name}</td>
  1220. <td><input name=shiptoname size=35 maxlength=64 value="$form->{shiptoname}"></td>
  1221. </tr>
  1222. <tr>
  1223. <th align=right nowrap>|.$locale->text('Address').qq|</th>
  1224. <td>$form->{address1}</td>
  1225. <td><input name=shiptoaddress1 size=35 maxlength=32 value="$form->{shiptoaddress1}"></td>
  1226. </tr>
  1227. <tr>
  1228. <th></th>
  1229. <td>$form->{address2}</td>
  1230. <td><input name=shiptoaddress2 size=35 maxlength=32 value="$form->{shiptoaddress2}"></td>
  1231. </tr>
  1232. <tr>
  1233. <th align=right nowrap>|.$locale->text('City').qq|</th>
  1234. <td>$form->{city}</td>
  1235. <td><input name=shiptocity size=35 maxlength=32 value="$form->{shiptocity}"></td>
  1236. </tr>
  1237. <tr>
  1238. <th align=right nowrap>|.$locale->text('State/Province').qq|</th>
  1239. <td>$form->{state}</td>
  1240. <td><input name=shiptostate size=35 maxlength=32 value="$form->{shiptostate}"></td>
  1241. </tr>
  1242. <tr>
  1243. <th align=right nowrap>|.$locale->text('Zip/Postal Code').qq|</th>
  1244. <td>$form->{zipcode}</td>
  1245. <td><input name=shiptozipcode size=10 maxlength=10 value="$form->{shiptozipcode}"></td>
  1246. </tr>
  1247. <tr>
  1248. <th align=right nowrap>|.$locale->text('Country').qq|</th>
  1249. <td>$form->{country}</td>
  1250. <td><input name=shiptocountry size=35 maxlength=32 value="$form->{shiptocountry}"></td>
  1251. </tr>
  1252. <tr>
  1253. <th align=right nowrap>|.$locale->text('Contact').qq|</th>
  1254. <td>$form->{contact}</td>
  1255. <td><input name=shiptocontact size=35 maxlength=64 value="$form->{shiptocontact}"></td>
  1256. </tr>
  1257. <tr>
  1258. <th align=right nowrap>|.$locale->text('Phone').qq|</th>
  1259. <td>$form->{"$form->{vc}phone"}</td>
  1260. <td><input name=shiptophone size=20 value="$form->{shiptophone}"></td>
  1261. </tr>
  1262. <tr>
  1263. <th align=right nowrap>|.$locale->text('Fax').qq|</th>
  1264. <td>$form->{"$form->{vc}fax"}</td>
  1265. <td><input name=shiptofax size=20 value="$form->{shiptofax}"></td>
  1266. </tr>
  1267. <tr>
  1268. <th align=right nowrap>|.$locale->text('E-mail').qq|</th>
  1269. <td>$form->{email}</td>
  1270. <td><input name=shiptoemail size=35 value="$form->{shiptoemail}"></td>
  1271. </tr>
  1272. </table>
  1273. </td>
  1274. </tr>
  1275. </table>
  1276. <input type=hidden name=nextsub value=$nextsub>
  1277. |;
  1278. # delete shipto
  1279. for (qw(action nextsub)) { delete $form->{$_} }
  1280. for (qw(name address1 address2 city state zipcode country contact phone fax email)) { delete $form->{"shipto$_"} }
  1281. $form->{title} = $title;
  1282. $form->hide_form;
  1283. print qq|
  1284. <hr size=3 noshade>
  1285. <br>
  1286. <button class="submit" type="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  1287. </form>
  1288. </body>
  1289. </html>
  1290. |;
  1291. }