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