summaryrefslogtreecommitdiff
path: root/bin/ir.pl
blob: a43cd2f5fc1f9f33c8277e9f12501f6d6edd0fcd (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') { $a{$_} = 1 }
  490. }
  491. for (keys %button) { delete $button{$_} if ! $a{$_} }
  492. }
  493. for (sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button) { $form->print_button(\%button, $_) }
  494. }
  495. if ($form->{lynx}) {
  496. require "bin/menu.pl";
  497. &menubar;
  498. }
  499. $form->hide_form(qw(rowcount callback path login sessionid));
  500. print qq|
  501. </form>
  502. </body>
  503. </html>
  504. |;
  505. }
  506. sub import_text {
  507. my @o_list;
  508. my @i_lines = split(/(\n|\r|\r\n)/, $form->{import_text});
  509. foreach $i (@i_lines){
  510. chomp($i);
  511. if ($i != ""){ # Strip out blank lines
  512. push @o_list, $i;
  513. }
  514. }
  515. my $c = 1;
  516. my $linenumber = 0;
  517. foreach $l (@o_list){
  518. if ($c % 2){
  519. $linenumber += 1;
  520. $form->{"partnumber_$linenumber"} = $l;
  521. }
  522. else {
  523. $form->{"qty_$linenumber"} = $l;
  524. }
  525. $c += 1;
  526. $form->{rowcount}=$linenumber;
  527. IR->retrieve_item(\%myconfig, \%$form);
  528. $rows = scalar @{ $form->{item_list} };
  529. $rows = 0 unless $rows;
  530. $i = $form->{rowcount};
  531. if ($rows = 0){
  532. $form->{"id_$i"} = 0;
  533. $form->{"unit_$i"} = $locale->text('ea');
  534. &new_item;
  535. } elsif ($rows > 1){
  536. &select_item;
  537. exit;
  538. } else {
  539. map { $form->{item_list}[$i]{$_} = $form->quote($form->{item_list}[$i]{$_}) } qw(partnumber description unit);
  540. map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
  541. $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
  542. ($dec) = ($s =~ /\.(\d+)/);
  543. $dec = length $dec;
  544. $decimalplaces = ($dec > 2) ? $dec : 2;
  545. $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
  546. map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
  547. map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
  548. if (!$form->{taxincluded}) {
  549. my @taxes = Tax::init_taxes($form, $form->{taxaccounts});
  550. $amount += (Tax::calculate_taxes(\@taxes, $form, $amount, 0));
  551. }
  552. $form->{creditremaining} -= $amount;
  553. $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
  554. $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
  555. }
  556. $form->{item_list} = [];
  557. }
  558. ++$form->{rowcount}
  559. }
  560. sub update {
  561. if($form->{import_text}){
  562. &import_text;
  563. }
  564. $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
  565. if ($newname = &check_name(vendor)) {
  566. &rebuild_vc(vendor, AP, $form->{transdate}, 1);
  567. }
  568. if ($form->{transdate} ne $form->{oldtransdate}) {
  569. $form->{duedate} = ($form->{terms}) ? $form->current_date(\%myconfig, $form->{transdate}, $form->{terms} * 1) : $form->{duedate};
  570. $form->{oldtransdate} = $form->{transdate};
  571. &rebuild_vc(vendor, AP, $form->{transdate}, 1) if ! $newname;
  572. if ($form->{currency} ne $form->{defaultcurrency}) {
  573. delete $form->{exchangerate};
  574. $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, 'sell')));
  575. $form->{oldcurrency} = $form->{currency};
  576. }
  577. }
  578. if ($form->{currency} ne $form->{oldcurrency}) {
  579. delete $form->{exchangerate};
  580. $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, 'sell')));
  581. }
  582. $j = 1;
  583. for $i (1 .. $form->{paidaccounts}) {
  584. if ($form->{"paid_$i"}) {
  585. for (qw(datepaid source memo cleared)) { $form->{"${_}_$j"} = $form->{"${_}_$i"} }
  586. for (qw(paid exchangerate)) { $form->{"${_}_$j"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
  587. $form->{"exchangerate_$j"} = $exchangerate if ($form->{"forex_$j"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$j"}, 'sell')));
  588. if ($j++ != $i) {
  589. for (qw(datepaid source memo cleared paid exchangerate forex)) { delete $form->{"${_}_$i"} }
  590. }
  591. } else {
  592. for (qw(datepaid source memo cleared paid exchangerate forex)) { delete $form->{"${_}_$i"} }
  593. }
  594. $form->{paidaccounts} = $j;
  595. }
  596. $i = $form->{rowcount};
  597. $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
  598. for (qw(partsgroup projectnumber)) {
  599. $form->{"select$_"} = $form->unescape($form->{"select$_"}) if $form->{"select$_"};
  600. }
  601. if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
  602. $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
  603. &check_form;
  604. } else {
  605. IR->retrieve_item(\%myconfig, \%$form);
  606. my $rows = scalar @{ $form->{item_list} };
  607. if ($form->{language_code} && $rows == 0) {
  608. $language_code = $form->{language_code};
  609. $form->{language_code} = "";
  610. IR->retrieve_item(\%myconfig, \%$form);
  611. $form->{language_code} = $language_code;
  612. $rows = scalar @{ $form->{item_list} };
  613. }
  614. if ($rows) {
  615. if ($rows > 1) {
  616. &select_item;
  617. exit;
  618. } else {
  619. $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
  620. $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
  621. for (qw(partnumber description unit)) { $form->{item_list}[$i]{$_} = $form->quote($form->{item_list}[$i]{$_}) }
  622. for (keys %{ $form->{item_list}[0] }) { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
  623. $form->{"discount_$i"} = $form->{discount} * 100;
  624. if ($sellprice) {
  625. $form->{"sellprice_$i"} = $sellprice;
  626. ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  627. $dec = length $dec;
  628. $decimalplaces = ($dec > 2) ? $dec : 2;
  629. } else {
  630. ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  631. $dec = length $dec;
  632. $decimalplaces = ($dec > 2) ? $dec : 2;
  633. $form->{"sellprice_$i"} /= $exchangerate;
  634. }
  635. $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
  636. for (split / /, $form->{taxaccounts}) { $form->{"${_}_base"} = 0 }
  637. for (split / /, $form->{"taxaccounts_$i"}) { $form->{"${_}_base"} += $amount }
  638. if (!$form->{taxincluded}) {
  639. my @taxes = Tax::init_taxes($form, $form->{"taxaccounts_$i"});
  640. $amount += (Tax::calculate_taxes(\@taxes, $form, $amount, 0));
  641. }
  642. $form->{creditremaining} -= $amount;
  643. $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
  644. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  645. for (qw(qty discount)) { $form->{"{_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}) }
  646. }
  647. &display_form;
  648. } else {
  649. # ok, so this is a new part
  650. # ask if it is a part or service item
  651. if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
  652. $form->{"discount_$i"} = "";
  653. &display_form;
  654. } else {
  655. $form->{"id_$i"} = 0;
  656. $form->{"unit_$i"} = $locale->text('ea');
  657. &new_item;
  658. }
  659. }
  660. }
  661. }
  662. sub post {
  663. $form->isblank("transdate", $locale->text('Invoice Date missing!'));
  664. $form->isblank("vendor", $locale->text('Vendor missing!'));
  665. # if the vendor changed get new values
  666. if (&check_name(vendor)) {
  667. &update;
  668. exit;
  669. }
  670. &validate_items;
  671. $closedto = $form->datetonum(\%myconfig, $form->{closedto});
  672. $transdate = $form->datetonum(\%myconfig, $form->{transdate});
  673. $form->error($locale->text('Cannot post invoice for a closed period!')) if ($transdate <= $closedto);
  674. $form->isblank("exchangerate", $locale->text('Exchange rate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
  675. for $i (1 .. $form->{paidaccounts}) {
  676. if ($form->{"paid_$i"}) {
  677. $datepaid = $form->datetonum(\%myconfig, $form->{"datepaid_$i"});
  678. $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
  679. $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
  680. if ($form->{currency} ne $form->{defaultcurrency}) {
  681. $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
  682. $form->isblank("exchangerate_$i", $locale->text('Exchange rate for payment missing!'));
  683. }
  684. }
  685. }
  686. if (! $form->{repost}) {
  687. if ($form->{id}) {
  688. &repost;
  689. exit;
  690. }
  691. }
  692. ($form->{AP}) = split /--/, $form->{AP};
  693. ($form->{AP_paid}) = split /--/, $form->{AP_paid};
  694. if (IR->post_invoice(\%myconfig, \%$form)) {
  695. $form->redirect($locale->text('Invoice')." $form->{invnumber} ".$locale->text('posted!'));
  696. } else {
  697. $form->error($locale->text('Cannot post invoice!'));
  698. }
  699. }
  700. sub delete {
  701. $form->header;
  702. print qq|
  703. <body>
  704. <form method=post action=$form->{script}>
  705. |;
  706. $form->{action} = "yes";
  707. $form->hide_form;
  708. print qq|
  709. <h2 class=confirm>|.$locale->text('Confirm!').qq|</h2>
  710. <h4>|.$locale->text('Are you sure you want to delete Invoice Number').qq| $form->{invnumber}</h4>
  711. <p>
  712. <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
  713. </form>
  714. |;
  715. }
  716. sub yes {
  717. if (IR->delete_invoice(\%myconfig, \%$form)) {
  718. $form->redirect($locale->text('Invoice deleted!'));
  719. } else {
  720. $form->error($locale->text('Cannot delete invoice!'));
  721. }
  722. }