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