summaryrefslogtreecommitdiff
path: root/bin/mozilla/is.pl
blob: 44314941bcefbaa1971ad0a60dc4971ef00ef750 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. #
  5. # See COPYRIGHT file for copyright information
  6. #======================================================================
  7. #
  8. # This file has NOT undergone whitespace cleanup.
  9. #
  10. #======================================================================
  11. #
  12. # Inventory invoicing module
  13. #
  14. #======================================================================
  15. use LedgerSMB::IS;
  16. use LedgerSMB::PE;
  17. require "$form->{path}/arap.pl";
  18. require "$form->{path}/io.pl";
  19. 1;
  20. # end of main
  21. sub add {
  22. $form->{title} = $locale->text('Add Sales Invoice');
  23. $form->{callback} = "$form->{script}?action=add&type=$form->{type}&login=$form->{login}&path=$form->{path}&sessionid=$form->{sessionid}" unless $form->{callback};
  24. &invoice_links;
  25. &prepare_invoice;
  26. &display_form;
  27. }
  28. sub edit {
  29. $form->{title} = $locale->text('Edit Sales Invoice');
  30. &invoice_links;
  31. &prepare_invoice;
  32. &display_form;
  33. }
  34. sub invoice_links {
  35. $form->{vc} = "customer";
  36. $form->{type} = "invoice";
  37. # create links
  38. $form->create_links("AR", \%myconfig, "customer", 1);
  39. # currencies
  40. @curr = split /:/, $form->{currencies};
  41. $form->{defaultcurrency} = $curr[0];
  42. chomp $form->{defaultcurrency};
  43. for (@curr) { $form->{selectcurrency} .= "<option>$_\n" }
  44. if (@{ $form->{all_customer} }) {
  45. unless ($form->{customer_id}) {
  46. $form->{customer_id} = $form->{all_customer}->[0]->{id};
  47. }
  48. }
  49. AA->get_name(\%myconfig, \%$form);
  50. delete $form->{notes};
  51. IS->retrieve_invoice(\%myconfig, \%$form);
  52. $form->{oldlanguage_code} = $form->{language_code};
  53. $form->get_partsgroup(\%myconfig, { language_code => $form->{language_code}, searchitems => 'nolabor' });
  54. if (@{ $form->{all_partsgroup} }) {
  55. $form->{selectpartsgroup} = "<option>\n";
  56. foreach $ref (@ { $form->{all_partsgroup} }) {
  57. if ($ref->{translation}) {
  58. $form->{selectpartsgroup} .= qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{translation}\n|;
  59. } else {
  60. $form->{selectpartsgroup} .= qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{partsgroup}\n|;
  61. }
  62. }
  63. }
  64. if (@{ $form->{all_project} }) {
  65. $form->{selectprojectnumber} = "<option>\n";
  66. for (@{ $form->{all_project} }) { $form->{selectprojectnumber} .= qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n| }
  67. }
  68. $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
  69. $form->{oldtransdate} = $form->{transdate};
  70. $form->{selectcustomer} = "";
  71. if (@{ $form->{all_customer} }) {
  72. $form->{customer} = "$form->{customer}--$form->{customer_id}";
  73. for (@{ $form->{all_customer} }) { $form->{selectcustomer} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| }
  74. }
  75. # departments
  76. if (@{ $form->{all_department} }) {
  77. $form->{selectdepartment} = "<option>\n";
  78. $form->{department} = "$form->{department}--$form->{department_id}" if $form->{department_id};
  79. for (@{ $form->{all_department} }) { $form->{selectdepartment} .= qq|<option value="$_->{description}--$_->{id}">$_->{description}\n| }
  80. }
  81. $form->{employee} = "$form->{employee}--$form->{employee_id}";
  82. # sales staff
  83. if (@{ $form->{all_employee} }) {
  84. $form->{selectemployee} = "";
  85. for (@{ $form->{all_employee} }) { $form->{selectemployee} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| }
  86. }
  87. if (@{ $form->{all_language} }) {
  88. $form->{selectlanguage} = "<option>\n";
  89. for (@{ $form->{all_language} }) { $form->{selectlanguage} .= qq|<option value="$_->{code}">$_->{description}\n| }
  90. }
  91. # forex
  92. $form->{forex} = $form->{exchangerate};
  93. $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
  94. foreach $key (keys %{ $form->{AR_links} }) {
  95. $form->{"select$key"} = "";
  96. foreach $ref (@{ $form->{AR_links}{$key} }) {
  97. $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
  98. }
  99. if ($key eq "AR_paid") {
  100. for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
  101. $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
  102. # reverse paid
  103. $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount} * -1;
  104. $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
  105. $form->{"forex_$i"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
  106. $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
  107. $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i-1]->{memo};
  108. $form->{"cleared_$i"} = $form->{acc_trans}{$key}->[$i-1]->{cleared};
  109. $form->{paidaccounts} = $i;
  110. }
  111. } else {
  112. $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}" if $form->{acc_trans}{$key}->[0]->{accno};
  113. }
  114. }
  115. for (qw(AR_links acc_trans)) { delete $form->{$_} }
  116. $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
  117. $form->{AR} = $form->{AR_1} unless $form->{id};
  118. $form->{locked} = ($form->{revtrans}) ? '1' : ($form->datetonum(\%myconfig, $form->{transdate}) <= $form->datetonum(\%myconfig, $form->{closedto}));
  119. if (! $form->{readonly}) {
  120. $form->{readonly} = 1 if $myconfig{acs} =~ /AR--Sales Invoice/;
  121. }
  122. }
  123. sub prepare_invoice {
  124. $form->{type} = "invoice";
  125. $form->{formname} = "invoice";
  126. $form->{sortby} ||= "runningnumber";
  127. $form->{format} = "postscript" if $myconfig{printer};
  128. $form->{media} = $myconfig{printer};
  129. $form->{selectformname} = qq|<option value="invoice">|.$locale->text('Invoice').qq|
  130. <option value="pick_list">|.$locale->text('Pick List').qq|
  131. <option value="packing_list">|.$locale->text('Packing List');
  132. $i = 0;
  133. $form->{currency} =~ s/ //g;
  134. $form->{oldcurrency} = $form->{currency};
  135. if ($form->{id}) {
  136. for (qw(invnumber ordnumber ponumber quonumber shippingpoint shipvia notes intnotes)) { $form->{$_} = $form->quote($form->{$_}) }
  137. foreach $ref (@{ $form->{invoice_details} } ) {
  138. $i++;
  139. for (keys %$ref) { $form->{"${_}_$i"} = $ref->{$_} }
  140. $form->{"projectnumber_$i"} = qq|$ref->{projectnumber}--$ref->{project_id}| if $ref->{project_id};
  141. $form->{"partsgroup_$i"} = qq|$ref->{partsgroup}--$ref->{partsgroup_id}| if $ref->{partsgroup_id};
  142. $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
  143. ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  144. $dec = length $dec;
  145. $decimalplaces = ($dec > 2) ? $dec : 2;
  146. $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
  147. $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
  148. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  149. for (qw(partnumber sku description unit)) { $form->{"${_}_$i"} = $form->quote($form->{"${_}_$i"}) }
  150. $form->{rowcount} = $i;
  151. }
  152. }
  153. }
  154. sub form_header {
  155. # set option selected
  156. for (qw(AR currency)) {
  157. $form->{"select$_"} =~ s/ selected//;
  158. $form->{"select$_"} =~ s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
  159. }
  160. for (qw(customer department employee)) {
  161. $form->{"select$_"} = $form->unescape($form->{"select$_"});
  162. $form->{"select$_"} =~ s/ selected//;
  163. $form->{"select$_"} =~ s/(<option value="\Q$form->{$_}\E")/$1 selected/;
  164. }
  165. $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
  166. $exchangerate = qq|<tr>|;
  167. $exchangerate .= qq|
  168. <th align=right nowrap>|.$locale->text('Currency').qq|</th>
  169. <td><select name=currency>$form->{selectcurrency}</select></td>
  170. | if $form->{defaultcurrency};
  171. $exchangerate .= qq|
  172. <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
  173. <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
  174. |;
  175. if ($form->{defaultcurrency} && $form->{currency} ne $form->{defaultcurrency}) {
  176. if ($form->{forex}) {
  177. $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>|;
  178. } else {
  179. $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
  180. }
  181. }
  182. $exchangerate .= qq|
  183. <input type=hidden name=forex value=$form->{forex}>
  184. </tr>
  185. |;
  186. if ($form->{selectcustomer}) {
  187. $customer = qq|<select name=customer>$form->{selectcustomer}</select>
  188. <input type=hidden name="selectcustomer" value="|.
  189. $form->escape($form->{selectcustomer},1).qq|">|;
  190. } else {
  191. $customer = qq|<input name=customer value="$form->{customer}" size=35>|;
  192. }
  193. $department = qq|
  194. <tr>
  195. <th align="right" nowrap>|.$locale->text('Department').qq|</th>
  196. <td colspan=3><select name=department>$form->{selectdepartment}</select>
  197. <input type=hidden name=selectdepartment value="|.
  198. $form->escape($form->{selectdepartment},1).qq|">
  199. </td>
  200. </tr>
  201. | if $form->{selectdepartment};
  202. $n = ($form->{creditremaining} < 0) ? "0" : "1";
  203. if ($form->{business}) {
  204. $business = qq|
  205. <tr>
  206. <th align=right nowrap>|.$locale->text('Business').qq|</th>
  207. <td>$form->{business}</td>
  208. <td width=10></td>
  209. <th align=right nowrap>|.$locale->text('Trade Discount').qq|</th>
  210. <td>|.$form->format_amount(\%myconfig, $form->{tradediscount} * 100).qq| %</td>
  211. </tr>
  212. |;
  213. }
  214. $employee = qq|
  215. <input type=hidden name=employee value="$form->{employee}">
  216. |;
  217. $employee = qq|
  218. <tr>
  219. <th align=right nowrap>|.$locale->text('Salesperson').qq|</th>
  220. <td><select name=employee>$form->{selectemployee}</select></td>
  221. <input type=hidden name=selectemployee value="|.
  222. $form->escape($form->{selectemployee},1).qq|">
  223. </tr>
  224. | if $form->{selectemployee};
  225. $i = $form->{rowcount} + 1;
  226. $focus = "partnumber_$i";
  227. $form->header;
  228. print qq|
  229. <body onLoad="document.forms[0].${focus}.focus()" />
  230. <form method=post action="$form->{script}">
  231. |;
  232. $form->hide_form(qw(id type media format printed emailed queued title vc terms discount creditlimit creditremaining tradediscount business closedto locked shipped oldtransdate recurring));
  233. print qq|
  234. <table width=100%>
  235. <tr class=listtop>
  236. <th class=listtop>$form->{title}</th>
  237. </tr>
  238. <tr height="5"></tr>
  239. <tr>
  240. <td>
  241. <table width=100%>
  242. <tr valign=top>
  243. <td>
  244. <table>
  245. <tr>
  246. <th align=right nowrap>|.$locale->text('Customer').qq|</th>
  247. <td colspan=3>$customer</td>
  248. <input type=hidden name=customer_id value=$form->{customer_id}>
  249. <input type=hidden name=oldcustomer value="$form->{oldcustomer}">
  250. </tr>
  251. <tr>
  252. <td></td>
  253. <td colspan=3>
  254. <table>
  255. <tr>
  256. <th align=right nowrap>|.$locale->text('Credit Limit').qq|</th>
  257. <td>|.$form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0").qq|</td>
  258. <td width=10></td>
  259. <th align=right nowrap>|.$locale->text('Remaining').qq|</th>
  260. <td class="plus$n" nowrap>|.$form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0").qq|</td>
  261. </tr>
  262. $business
  263. </table>
  264. </td>
  265. </tr>
  266. <tr>
  267. <th align=right nowrap>|.$locale->text('Record in').qq|</th>
  268. <td colspan=3><select name=AR>$form->{selectAR}</select></td>
  269. <input type=hidden name=selectAR value="$form->{selectAR}">
  270. </tr>
  271. $department
  272. $exchangerate
  273. <tr>
  274. <th align=right nowrap>|.$locale->text('Shipping Point').qq|</th>
  275. <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
  276. </tr>
  277. <tr>
  278. <th align=right nowrap>|.$locale->text('Ship via').qq|</th>
  279. <td colspan=3><input name=shipvia size=35 value="$form->{shipvia}"></td>
  280. </tr>
  281. </table>
  282. </td>
  283. <td align=right>
  284. <table>
  285. $employee
  286. <tr>
  287. <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
  288. <td><input name=invnumber size=20 value="$form->{invnumber}"></td>
  289. </tr>
  290. <tr>
  291. <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
  292. <td><input name=ordnumber size=20 value="$form->{ordnumber}"></td>
  293. <input type=hidden name=quonumber value="$form->{quonumber}">
  294. </tr>
  295. <tr>
  296. <th align=right>|.$locale->text('Invoice Date').qq|</th>
  297. <td><input name=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}></td>
  298. </tr>
  299. <tr>
  300. <th align=right>|.$locale->text('Due Date').qq|</th>
  301. <td><input name=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>
  302. </tr>
  303. <tr>
  304. <th align=right nowrap>|.$locale->text('PO Number').qq|</th>
  305. <td><input name=ponumber size=20 value="$form->{ponumber}"></td>
  306. </tr>
  307. </table>
  308. </td>
  309. </tr>
  310. </table>
  311. </td>
  312. </tr>
  313. <tr>
  314. <td>
  315. </td>
  316. </tr>
  317. |;
  318. $form->hide_form(qw(shiptoname shiptoaddress1 shiptoaddress2 shiptocity shiptostate shiptozipcode shiptocountry shiptocontact shiptophone shiptofax shiptoemail message email subject cc bcc taxaccounts));
  319. foreach $item (split / /, $form->{taxaccounts}) {
  320. $form->hide_form("${item}_rate", "${item}_description", "${item}_taxnumber");
  321. }
  322. }
  323. sub form_footer {
  324. $form->{invtotal} = $form->{invsubtotal};
  325. if (($rows = $form->numtextrows($form->{notes}, 35, 8)) < 2) {
  326. $rows = 2;
  327. }
  328. if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
  329. $introws = 2;
  330. }
  331. $rows = ($rows > $introws) ? $rows : $introws;
  332. $notes = qq|<textarea name=notes rows=$rows cols=35 wrap=soft>$form->{notes}</textarea>|;
  333. $intnotes = qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
  334. $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
  335. $taxincluded = "";
  336. if ($form->{taxaccounts}) {
  337. $taxincluded = qq|
  338. <tr height="5"></tr>
  339. <tr>
  340. <td align=right>
  341. <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td><th align=left>|.$locale->text('Tax Included').qq|</th>
  342. </tr>
  343. |;
  344. }
  345. if (!$form->{taxincluded}) {
  346. for (split / /, $form->{taxaccounts}) {
  347. if ($form->{"${_}_base"}) {
  348. $form->{"${_}_total"} = $form->round_amount($form->{"${_}_base"} * $form->{"${_}_rate"}, 2);
  349. $form->{invtotal} += $form->{"${_}_total"};
  350. $form->{"${_}_total"} = $form->format_amount(\%myconfig, $form->{"${_}_total"}, 2);
  351. $tax .= qq|
  352. <tr>
  353. <th align=right>$form->{"${_}_description"}</th>
  354. <td align=right>$form->{"${_}_total"}</td>
  355. </tr>
  356. |;
  357. }
  358. }
  359. $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
  360. $subtotal = qq|
  361. <tr>
  362. <th align=right>|.$locale->text('Subtotal').qq|</th>
  363. <td align=right>$form->{invsubtotal}</td>
  364. </tr>
  365. |;
  366. }
  367. $form->{oldinvtotal} = $form->{invtotal};
  368. $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
  369. print qq|
  370. <tr>
  371. <td>
  372. <table width=100%>
  373. <tr valign=bottom>
  374. <td>
  375. <table>
  376. <tr>
  377. <th align=left>|.$locale->text('Notes').qq|</th>
  378. <th align=left>|.$locale->text('Internal Notes').qq|</th>
  379. </tr>
  380. <tr valign=top>
  381. <td>$notes</td>
  382. <td>$intnotes</td>
  383. </tr>
  384. </table>
  385. </td>
  386. <td align=right>
  387. <table>
  388. $subtotal
  389. $tax
  390. <tr>
  391. <th align=right>|.$locale->text('Total').qq|</th>
  392. <td align=right>$form->{invtotal}</td>
  393. </tr>
  394. $taxincluded
  395. </table>
  396. </td>
  397. </tr>
  398. </table>
  399. </td>
  400. </tr>
  401. <tr>
  402. <td>
  403. <table width=100%>
  404. <tr class=listheading>
  405. <th colspan=6 class=listheading>|.$locale->text('Payments')
  406. .qq|</th>
  407. </tr>
  408. |;
  409. if ($form->{currency} eq $form->{defaultcurrency}) {
  410. @column_index = qw(datepaid source memo paid AR_paid);
  411. } else {
  412. @column_index = qw(datepaid source memo paid exchangerate AR_paid);
  413. }
  414. $column_data{datepaid} = "<th>".$locale->text('Date')."</th>";
  415. $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
  416. $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
  417. $column_data{AR_paid} = "<th>".$locale->text('Account')."</th>";
  418. $column_data{source} = "<th>".$locale->text('Source')."</th>";
  419. $column_data{memo} = "<th>".$locale->text('Memo')."</th>";
  420. print "
  421. <tr>
  422. ";
  423. for (@column_index) { print "$column_data{$_}\n" }
  424. print "
  425. </tr>
  426. ";
  427. $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
  428. for $i (1 .. $form->{paidaccounts}) {
  429. $form->hide_form("cleared_$i");
  430. print "
  431. <tr>\n";
  432. $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
  433. $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
  434. # format amounts
  435. $totalpaid += $form->{"paid_$i"};
  436. $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
  437. $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
  438. $exchangerate = qq|&nbsp;|;
  439. if ($form->{currency} ne $form->{defaultcurrency}) {
  440. if ($form->{"forex_$i"}) {
  441. $exchangerate = qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
  442. } else {
  443. $exchangerate = qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
  444. }
  445. }
  446. $exchangerate .= qq|
  447. <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
  448. |;
  449. $column_data{paid} = qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  450. $column_data{exchangerate} = qq|<td align=center>$exchangerate</td>|;
  451. $column_data{AR_paid} = qq|<td align=center><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
  452. $column_data{datepaid} = qq|<td align=center><input name="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
  453. $column_data{source} = qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
  454. $column_data{memo} = qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
  455. for (@column_index) { print qq|$column_data{$_}\n| }
  456. print "
  457. </tr>\n";
  458. }
  459. $form->{oldtotalpaid} = $totalpaid;
  460. $form->hide_form(qw(paidaccounts selectAR_paid oldinvtotal oldtotalpaid));
  461. print qq|
  462. </table>
  463. </td>
  464. </tr>
  465. <tr>
  466. <td><hr size=3 noshade></td>
  467. </tr>
  468. <tr>
  469. <td>
  470. |;
  471. &print_options;
  472. print qq|
  473. </td>
  474. </tr>
  475. </table>
  476. <br>
  477. |;
  478. $transdate = $form->datetonum(\%myconfig, $form->{transdate});
  479. $closedto = $form->datetonum(\%myconfig, $form->{closedto});
  480. # type=submit $locale->text('Update')
  481. # type=submit $locale->text('Print')
  482. # type=submit $locale->text('Post')
  483. # type=submit $locale->text('Print and Post')
  484. # type=submit $locale->text('Schedule')
  485. # type=submit $locale->text('Ship to')
  486. # type=submit $locale->text('Post as new')
  487. # type=submit $locale->text('Print and Post as new')
  488. # type=submit $locale->text('E-mail')
  489. # type=submit $locale->text('Delete')
  490. # type=submit $locale->text('Sales Order')
  491. if (! $form->{readonly}) {
  492. %button = ('Update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
  493. 'Print' => { ndx => 2, key => 'P', value => $locale->text('Print') },
  494. 'Post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  495. 'Ship to' => { ndx => 4, key => 'T', value => $locale->text('Ship to') },
  496. 'E-mail' => { ndx => 5, key => 'E', value => $locale->text('E-mail') },
  497. 'Print and Post' => { ndx => 6, key => 'R', value => $locale->text('Print and Post') },
  498. 'Post as new' => { ndx => 7, key => 'N', value => $locale->text('Post as new') },
  499. 'Print and Post as new' => { ndx => 8, key => 'W', value => $locale->text('Print and Post as new') },
  500. 'Sales Order' => { ndx => 9, key => 'L', value => $locale->text('Sales Order') },
  501. 'Schedule' => { ndx => 10, key => 'H', value => $locale->text('Schedule') },
  502. 'Delete' => { ndx => 11, key => 'D', value => $locale->text('Delete') },
  503. );
  504. if ($form->{id}) {
  505. if ($form->{locked} || $transdate <= $closedto) {
  506. for ("Post", "Print and Post", "Delete") { delete $button{$_} }
  507. }
  508. if (!$latex) {
  509. for ("Print and Post", "Print and Post as new") { delete $button{$_} }
  510. }
  511. } else {
  512. if ($transdate > $closedto) {
  513. for ("Update", "Ship to", "Print", "E-mail", "Post", "Schedule") { $a{$_} = 1 }
  514. $a{'Print and Post'} = 1 if $latex;
  515. }
  516. for (keys %button) { delete $button{$_} if ! $a{$_} }
  517. }
  518. for (sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button) { $form->print_button(\%button, $_) }
  519. }
  520. if ($form->{menubar}) {
  521. require "$form->{path}/menu.pl";
  522. &menubar;
  523. }
  524. $form->hide_form(qw(rowcount callback path login sessionid));
  525. print qq|
  526. </form>
  527. </body>
  528. </html>
  529. |;
  530. }
  531. sub update {
  532. $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
  533. if ($newname = &check_name(customer)) {
  534. &rebuild_vc(customer, AR, $form->{transdate}, 1);
  535. }
  536. if ($form->{transdate} ne $form->{oldtransdate}) {
  537. $form->{duedate} = ($form->{terms}) ? $form->current_date(\%myconfig, $form->{transdate}, $form->{terms} * 1) : $form->{duedate};
  538. $form->{oldtransdate} = $form->{transdate};
  539. &rebuild_vc(customer, AR, $form->{transdate}, 1) if ! $newname;
  540. if ($form->{currency} ne $form->{defaultcurrency}) {
  541. delete $form->{exchangerate};
  542. $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, 'buy')));
  543. }
  544. $form->{selectemployee} = "";
  545. if (@{ $form->{all_employee} }) {
  546. for (@{ $form->{all_employee} }) { $form->{selectemployee} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| }
  547. }
  548. }
  549. if ($form->{currency} ne $form->{oldcurrency}) {
  550. delete $form->{exchangerate};
  551. $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, 'buy')));
  552. }
  553. $j = 1;
  554. for $i (1 .. $form->{paidaccounts}) {
  555. if ($form->{"paid_$i"}) {
  556. for (qw(datepaid source memo cleared)) { $form->{"${_}_$j"} = $form->{"${_}_$i"} }
  557. for (qw(paid exchangerate)) { $form->{"${_}_$j"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
  558. $form->{"exchangerate_$j"} = $exchangerate if ($form->{"forex_$j"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$j"}, 'buy')));
  559. if ($j++ != $i) {
  560. for (qw(datepaid source memo cleared paid exchangerate forex)) { delete $form->{"${_}_$i"} }
  561. }
  562. } else {
  563. for (qw(datepaid source memo cleared paid exchangerate forex)) { delete $form->{"${_}_$i"} }
  564. }
  565. }
  566. $form->{paidaccounts} = $j;
  567. $i = $form->{rowcount};
  568. $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
  569. for (qw(partsgroup projectnumber)) {
  570. $form->{"select$_"} = $form->unescape($form->{"select$_"}) if $form->{"select$_"};
  571. }
  572. # if last row empty, check the form otherwise retrieve new item
  573. if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
  574. $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
  575. &check_form;
  576. } else {
  577. IS->retrieve_item(\%myconfig, \%$form);
  578. $rows = scalar @{ $form->{item_list} };
  579. if ($form->{language_code} && $rows == 0) {
  580. $language_code = $form->{language_code};
  581. $form->{language_code} = "";
  582. IS->retrieve_item(\%myconfig, \%$form);
  583. $form->{language_code} = $language_code;
  584. $rows = scalar @{ $form->{item_list} };
  585. }
  586. if ($rows) {
  587. if ($rows > 1) {
  588. &select_item;
  589. exit;
  590. } else {
  591. $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
  592. $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
  593. for (qw(partnumber description unit)) { $form->{item_list}[$i]{$_} = $form->quote($form->{item_list}[$i]{$_}) }
  594. for (keys %{ $form->{item_list}[0] }) { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
  595. $form->{"discount_$i"} = $form->{discount} * 100;
  596. if ($sellprice) {
  597. $form->{"sellprice_$i"} = $sellprice;
  598. ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  599. $dec = length $dec;
  600. $decimalplaces1 = ($dec > 2) ? $dec : 2;
  601. } else {
  602. ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  603. $dec = length $dec;
  604. $decimalplaces1 = ($dec > 2) ? $dec : 2;
  605. $form->{"sellprice_$i"} /= $exchangerate;
  606. }
  607. ($dec) = ($form->{"lastcost_$i"} =~ /\.(\d+)/);
  608. $dec = length $dec;
  609. $decimalplaces2 = ($dec > 2) ? $dec : 2;
  610. # if there is an exchange rate adjust sellprice
  611. for (qw(listprice lastcost)) { $form->{"${_}_$i"} /= $exchangerate }
  612. $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
  613. for (split / /, $form->{taxaccounts}) { $form->{"${_}_base"} = 0 }
  614. for (split / /, $form->{"taxaccounts_$i"}) { $form->{"${_}_base"} += $amount }
  615. if (!$form->{taxincluded}) {
  616. for (split / /, $form->{"taxaccounts_$i"}) { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
  617. }
  618. $form->{creditremaining} -= $amount;
  619. for (qw(sellprice listprice)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces1) }
  620. $form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces2);
  621. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  622. for (qw(qty discount)) { $form->{"{_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}) }
  623. }
  624. &display_form;
  625. } else {
  626. # ok, so this is a new part
  627. # ask if it is a part or service item
  628. if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
  629. $form->{rowcount}--;
  630. &display_form;
  631. } else {
  632. $form->{"id_$i"} = 0;
  633. $form->{"unit_$i"} = $locale->text('ea');
  634. &new_item;
  635. }
  636. }
  637. }
  638. }
  639. sub post {
  640. $form->isblank("transdate", $locale->text('Invoice Date missing!'));
  641. $form->isblank("customer", $locale->text('Customer missing!'));
  642. # if oldcustomer ne customer redo form
  643. if (&check_name(customer)) {
  644. &update;
  645. exit;
  646. }
  647. &validate_items;
  648. $closedto = $form->datetonum(\%myconfig, $form->{closedto});
  649. $transdate = $form->datetonum(\%myconfig, $form->{transdate});
  650. $form->error($locale->text('Cannot post invoice for a closed period!')) if ($transdate <= $closedto);
  651. $form->isblank("exchangerate", $locale->text('Exchange rate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
  652. for $i (1 .. $form->{paidaccounts}) {
  653. if ($form->{"paid_$i"}) {
  654. $datepaid = $form->datetonum(\%myconfig, $form->{"datepaid_$i"});
  655. $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
  656. $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
  657. if ($form->{currency} ne $form->{defaultcurrency}) {
  658. $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
  659. $form->isblank("exchangerate_$i", $locale->text('Exchange rate for payment missing!'));
  660. }
  661. }
  662. }
  663. $form->{label} = $locale->text('Invoice');
  664. if (! $form->{repost}) {
  665. if ($form->{id}) {
  666. &repost;
  667. exit;
  668. }
  669. }
  670. ($form->{AR}) = split /--/, $form->{AR};
  671. ($form->{AR_paid}) = split /--/, $form->{AR_paid};
  672. if (IS->post_invoice(\%myconfig, \%$form)) {
  673. $form->redirect($locale->text('Invoice')." $form->{invnumber} ".$locale->text('posted!'));
  674. } else {
  675. $form->error($locale->text('Cannot post invoice!'));
  676. }
  677. }
  678. sub print_and_post {
  679. $form->error($locale->text('Select postscript or PDF!')) if $form->{format} !~ /(postscript|pdf)/;
  680. $form->error($locale->text('Select a Printer!')) if $form->{media} eq 'screen';
  681. if (! $form->{repost}) {
  682. if ($form->{id}) {
  683. $form->{print_and_post} = 1;
  684. &repost;
  685. exit;
  686. }
  687. }
  688. $old_form = new Form;
  689. $form->{display_form} = "post";
  690. for (keys %$form) { $old_form->{$_} = $form->{$_} }
  691. $old_form->{rowcount}++;
  692. &print_form($old_form);
  693. }
  694. sub delete {
  695. $form->header;
  696. print qq|
  697. <body>
  698. <form method=post action=$form->{script}>
  699. |;
  700. $form->{action} = "yes";
  701. $form->hide_form;
  702. print qq|
  703. <h2 class=confirm>|.$locale->text('Confirm!').qq|</h2>
  704. <h4>|.$locale->text('Are you sure you want to delete Invoice Number').qq| $form->{invnumber}
  705. </h4>
  706. <p>
  707. <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
  708. </form>
  709. |;
  710. }
  711. sub yes {
  712. if (IS->delete_invoice(\%myconfig, \%$form, $spool)) {
  713. $form->redirect($locale->text('Invoice deleted!'));
  714. } else {
  715. $form->error($locale->text('Cannot delete invoice!'));
  716. }
  717. }