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