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