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