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