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