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