summaryrefslogtreecommitdiff
path: root/bin/mozilla/ir.pl
blob: 35f9975bab8483b20402a221b39106b3602e2414 (plain)
  1. #=====================================================================
  2. # LedgerSMB Small Medium Business Accounting
  3. # http://www.ledgersmb.org/
  4. #
  5. # Copyright (C) 2006
  6. # This work contains copyrighted information from a number of sources all used
  7. # with permission.
  8. #
  9. # This file contains source code included with or based on SQL-Ledger which
  10. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  11. # under the GNU General Public License version 2 or, at your option, any later
  12. # version. For a full list including contact information of contributors,
  13. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  14. #
  15. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  16. # Copyright (c) 2001
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. #
  22. # This program is free software; you can redistribute it and/or modify
  23. # it under the terms of the GNU General Public License as published by
  24. # the Free Software Foundation; either version 2 of the License, or
  25. # (at your option) any later version.
  26. #
  27. # This program is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU General Public License for more details.
  31. # You should have received a copy of the GNU General Public License
  32. # along with this program; if not, write to the Free Software
  33. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34. #======================================================================
  35. #
  36. # Inventory received module
  37. #
  38. #======================================================================
  39. use LedgerSMB::IR;
  40. use LedgerSMB::PE;
  41. 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. <th align=left>|.$locale->text('Import Text').qq|</th>
  367. </tr>
  368. <tr valign=top>
  369. <td>$notes</td>
  370. <td>$intnotes</td>
  371. <td><textarea name=import_text rows=$rows cols=25></textarea>
  372. </tr>
  373. </table>
  374. </td>
  375. <td align=right>
  376. $taxincluded
  377. <br>
  378. <table>
  379. $subtotal
  380. $tax
  381. <tr>
  382. <th align=right>|.$locale->text('Total').qq|</th>
  383. <td align=right>$form->{invtotal}</td>
  384. </tr>
  385. </table>
  386. </td>
  387. </tr>
  388. </table>
  389. </td>
  390. </tr>
  391. <tr>
  392. <td>
  393. <table width=100%>
  394. <tr>
  395. <th colspan=6 class=listheading>|.$locale->text('Payments').qq|</th>
  396. </tr>
  397. |;
  398. if ($form->{currency} eq $form->{defaultcurrency}) {
  399. @column_index = qw(datepaid source memo paid AP_paid);
  400. } else {
  401. @column_index = qw(datepaid source memo paid exchangerate AP_paid);
  402. }
  403. $column_data{datepaid} = "<th>".$locale->text('Date')."</th>";
  404. $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
  405. $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
  406. $column_data{AP_paid} = "<th>".$locale->text('Account')."</th>";
  407. $column_data{source} = "<th>".$locale->text('Source')."</th>";
  408. $column_data{memo} = "<th>".$locale->text('Memo')."</th>";
  409. print qq|
  410. <tr>
  411. |;
  412. for (@column_index) { print "$column_data{$_}\n" }
  413. print qq|
  414. </tr>
  415. |;
  416. $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
  417. for $i (1 .. $form->{paidaccounts}) {
  418. $form->hide_form("cleared_$i");
  419. print qq|
  420. <tr>
  421. |;
  422. $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
  423. $form->{"selectAP_paid_$i"} =~ s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
  424. # format amounts
  425. $totalpaid += $form->{"paid_$i"};
  426. $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
  427. $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
  428. $exchangerate = qq|&nbsp;|;
  429. if ($form->{currency} ne $form->{defaultcurrency}) {
  430. if ($form->{"forex_$i"}) {
  431. $exchangerate = qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
  432. } else {
  433. $exchangerate = qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
  434. }
  435. }
  436. $exchangerate .= qq|
  437. <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
  438. |;
  439. $column_data{"paid_$i"} = qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  440. $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
  441. $column_data{"AP_paid_$i"} = qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
  442. $column_data{"datepaid_$i"} = qq|<td align=center><input name="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
  443. $column_data{"source_$i"} = qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
  444. $column_data{"memo_$i"} = qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
  445. for (@column_index) { print qq|$column_data{"${_}_$i"}\n| }
  446. print qq|
  447. </tr>
  448. |;
  449. }
  450. $form->{oldtotalpaid} = $totalpaid;
  451. $form->hide_form(qw(paidaccounts selectAP_paid oldinvtotal oldtotalpaid));
  452. print qq|
  453. </table>
  454. </td>
  455. </tr>
  456. <tr>
  457. <td><hr size=3 noshade></td>
  458. </tr>
  459. </table>
  460. <br>
  461. |;
  462. $transdate = $form->datetonum(\%myconfig, $form->{transdate});
  463. $closedto = $form->datetonum(\%myconfig, $form->{closedto});
  464. # type=submit $locale->text('Update')
  465. # type=submit $locale->text('Post')
  466. # type=submit $locale->text('Post as new')
  467. # type=submit $locale->text('Schedule')
  468. # type=submit $locale->text('Purchase Order')
  469. # type=submit $locale->text('Delete')
  470. if (! $form->{readonly}) {
  471. %button = ('Update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
  472. 'Post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  473. 'Post as new' => { ndx => 5, key => 'N', value => $locale->text('Post as new') },
  474. 'Purchase Order' => { ndx => 6, key => 'L', value => $locale->text('Purchase Order') },
  475. 'Schedule' => { ndx => 7, key => 'H', value => $locale->text('Schedule') },
  476. 'Delete' => { ndx => 8, key => 'D', value => $locale->text('Delete') },
  477. );
  478. if ($form->{id}) {
  479. if ($form->{locked}) {
  480. for ("Post", "Delete") { delete $button{$_} }
  481. }
  482. } else {
  483. if ($transdate > $closedto) {
  484. for ('Update', 'Post', 'Schedule') { $a{$_} = 1 }
  485. }
  486. for (keys %button) { delete $button{$_} if ! $a{$_} }
  487. }
  488. for (sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button) { $form->print_button(\%button, $_) }
  489. }
  490. if ($form->{menubar}) {
  491. require "$form->{path}/menu.pl";
  492. &menubar;
  493. }
  494. $form->hide_form(qw(rowcount callback path login sessionid));
  495. print qq|
  496. </form>
  497. </body>
  498. </html>
  499. |;
  500. }
  501. sub import_text {
  502. my @o_list;
  503. my @i_lines = split(/(\n|\r|\r\n)/, $form->{import_text});
  504. foreach $i (@i_lines){
  505. chomp($i);
  506. if ($i != ""){ # Strip out blank lines
  507. push @o_list, $i;
  508. }
  509. }
  510. my $c = 1;
  511. my $linenumber = 0;
  512. foreach $l (@o_list){
  513. if ($c % 2){
  514. $linenumber += 1;
  515. $form->{"partnumber_$linenumber"} = $l;
  516. }
  517. else {
  518. $form->{"qty_$linenumber"} = $l;
  519. }
  520. $c += 1;
  521. $form->{rowcount}=$linenumber;
  522. IR->retrieve_item(\%myconfig, \%$form);
  523. $rows = scalar @{ $form->{item_list} };
  524. $rows = 0 unless $rows;
  525. $i = $form->{rowcount};
  526. if ($rows = 0){
  527. $form->{"id_$i"} = 0;
  528. $form->{"unit_$i"} = $locale->text('ea');
  529. &new_item;
  530. } elsif ($rows > 1){
  531. &select_item;
  532. exit;
  533. } else {
  534. map { $form->{item_list}[$i]{$_} = $form->quote($form->{item_list}[$i]{$_}) } qw(partnumber description unit);
  535. map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
  536. $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
  537. ($dec) = ($s =~ /\.(\d+)/);
  538. $dec = length $dec;
  539. $decimalplaces = ($dec > 2) ? $dec : 2;
  540. $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
  541. map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
  542. map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
  543. map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
  544. $form->{creditremaining} -= $amount;
  545. $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
  546. $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
  547. }
  548. $form->{item_list} = [];
  549. }
  550. ++$form->{rowcount}
  551. }
  552. sub update {
  553. if($form->{import_text}){
  554. &import_text;
  555. }
  556. $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
  557. if ($newname = &check_name(vendor)) {
  558. &rebuild_vc(vendor, AP, $form->{transdate}, 1);
  559. }
  560. if ($form->{transdate} ne $form->{oldtransdate}) {
  561. $form->{duedate} = ($form->{terms}) ? $form->current_date(\%myconfig, $form->{transdate}, $form->{terms} * 1) : $form->{duedate};
  562. $form->{oldtransdate} = $form->{transdate};
  563. &rebuild_vc(vendor, AP, $form->{transdate}, 1) if ! $newname;
  564. if ($form->{currency} ne $form->{defaultcurrency}) {
  565. delete $form->{exchangerate};
  566. $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, 'sell')));
  567. $form->{oldcurrency} = $form->{currency};
  568. }
  569. }
  570. if ($form->{currency} ne $form->{oldcurrency}) {
  571. delete $form->{exchangerate};
  572. $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, 'sell')));
  573. }
  574. $j = 1;
  575. for $i (1 .. $form->{paidaccounts}) {
  576. if ($form->{"paid_$i"}) {
  577. for (qw(datepaid source memo cleared)) { $form->{"${_}_$j"} = $form->{"${_}_$i"} }
  578. for (qw(paid exchangerate)) { $form->{"${_}_$j"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
  579. $form->{"exchangerate_$j"} = $exchangerate if ($form->{"forex_$j"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$j"}, 'sell')));
  580. if ($j++ != $i) {
  581. for (qw(datepaid source memo cleared paid exchangerate forex)) { delete $form->{"${_}_$i"} }
  582. }
  583. } else {
  584. for (qw(datepaid source memo cleared paid exchangerate forex)) { delete $form->{"${_}_$i"} }
  585. }
  586. $form->{paidaccounts} = $j;
  587. }
  588. $i = $form->{rowcount};
  589. $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
  590. for (qw(partsgroup projectnumber)) {
  591. $form->{"select$_"} = $form->unescape($form->{"select$_"}) if $form->{"select$_"};
  592. }
  593. if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
  594. $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
  595. &check_form;
  596. } else {
  597. IR->retrieve_item(\%myconfig, \%$form);
  598. my $rows = scalar @{ $form->{item_list} };
  599. if ($form->{language_code} && $rows == 0) {
  600. $language_code = $form->{language_code};
  601. $form->{language_code} = "";
  602. IR->retrieve_item(\%myconfig, \%$form);
  603. $form->{language_code} = $language_code;
  604. $rows = scalar @{ $form->{item_list} };
  605. }
  606. if ($rows) {
  607. if ($rows > 1) {
  608. &select_item;
  609. exit;
  610. } else {
  611. $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
  612. $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
  613. for (qw(partnumber description unit)) { $form->{item_list}[$i]{$_} = $form->quote($form->{item_list}[$i]{$_}) }
  614. for (keys %{ $form->{item_list}[0] }) { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
  615. $form->{"discount_$i"} = $form->{discount} * 100;
  616. if ($sellprice) {
  617. $form->{"sellprice_$i"} = $sellprice;
  618. ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  619. $dec = length $dec;
  620. $decimalplaces = ($dec > 2) ? $dec : 2;
  621. } else {
  622. ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  623. $dec = length $dec;
  624. $decimalplaces = ($dec > 2) ? $dec : 2;
  625. $form->{"sellprice_$i"} /= $exchangerate;
  626. }
  627. $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
  628. for (split / /, $form->{taxaccounts}) { $form->{"${_}_base"} = 0 }
  629. for (split / /, $form->{"taxaccounts_$i"}) { $form->{"${_}_base"} += $amount }
  630. if (!$form->{taxincluded}) {
  631. for (split / /, $form->{"taxaccounts_$i"}) { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) }
  632. }
  633. $form->{creditremaining} -= $amount;
  634. $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
  635. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  636. for (qw(qty discount)) { $form->{"{_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}) }
  637. }
  638. &display_form;
  639. } else {
  640. # ok, so this is a new part
  641. # ask if it is a part or service item
  642. if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
  643. $form->{"discount_$i"} = "";
  644. &display_form;
  645. } else {
  646. $form->{"id_$i"} = 0;
  647. $form->{"unit_$i"} = $locale->text('ea');
  648. &new_item;
  649. }
  650. }
  651. }
  652. }
  653. sub post {
  654. $form->isblank("transdate", $locale->text('Invoice Date missing!'));
  655. $form->isblank("vendor", $locale->text('Vendor missing!'));
  656. # if the vendor changed get new values
  657. if (&check_name(vendor)) {
  658. &update;
  659. exit;
  660. }
  661. &validate_items;
  662. $closedto = $form->datetonum(\%myconfig, $form->{closedto});
  663. $transdate = $form->datetonum(\%myconfig, $form->{transdate});
  664. $form->error($locale->text('Cannot post invoice for a closed period!')) if ($transdate <= $closedto);
  665. $form->isblank("exchangerate", $locale->text('Exchange rate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
  666. for $i (1 .. $form->{paidaccounts}) {
  667. if ($form->{"paid_$i"}) {
  668. $datepaid = $form->datetonum(\%myconfig, $form->{"datepaid_$i"});
  669. $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
  670. $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
  671. if ($form->{currency} ne $form->{defaultcurrency}) {
  672. $form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
  673. $form->isblank("exchangerate_$i", $locale->text('Exchange rate for payment missing!'));
  674. }
  675. }
  676. }
  677. if (! $form->{repost}) {
  678. if ($form->{id}) {
  679. &repost;
  680. exit;
  681. }
  682. }
  683. ($form->{AP}) = split /--/, $form->{AP};
  684. ($form->{AP_paid}) = split /--/, $form->{AP_paid};
  685. if (IR->post_invoice(\%myconfig, \%$form)) {
  686. $form->redirect($locale->text('Invoice')." $form->{invnumber} ".$locale->text('posted!'));
  687. } else {
  688. $form->error($locale->text('Cannot post invoice!'));
  689. }
  690. }
  691. sub delete {
  692. $form->header;
  693. print qq|
  694. <body>
  695. <form method=post action=$form->{script}>
  696. |;
  697. $form->{action} = "yes";
  698. $form->hide_form;
  699. print qq|
  700. <h2 class=confirm>|.$locale->text('Confirm!').qq|</h2>
  701. <h4>|.$locale->text('Are you sure you want to delete Invoice Number').qq| $form->{invnumber}</h4>
  702. <p>
  703. <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
  704. </form>
  705. |;
  706. }
  707. sub yes {
  708. if (IR->delete_invoice(\%myconfig, \%$form)) {
  709. $form->redirect($locale->text('Invoice deleted!'));
  710. } else {
  711. $form->error($locale->text('Cannot delete invoice!'));
  712. }
  713. }