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