summaryrefslogtreecommitdiff
path: root/bin/ir.pl
blob: ba3c352428b23d664a528f490f4b893e4af2df98 (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. use LedgerSMB::Tax;
  42. require "bin/io.pl";
  43. require "bin/arap.pl";
  44. 1;
  45. # end of main
  46. sub add {
  47. $form->{title} = $locale->text('Add Vendor Invoice');
  48. $form->{callback} =
  49. "$form->{script}?action=add&type=$form->{type}&login=$form->{login}&path=$form->{path}&sessionid=$form->{sessionid}"
  50. unless $form->{callback};
  51. &invoice_links;
  52. &prepare_invoice;
  53. &display_form;
  54. }
  55. sub edit {
  56. $form->{title} = $locale->text('Edit Vendor Invoice');
  57. &invoice_links;
  58. &prepare_invoice;
  59. &display_form;
  60. }
  61. sub invoice_links {
  62. $form->{vc} = "vendor";
  63. $form->{type} = "invoice";
  64. # create links
  65. $form->create_links( "AP", \%myconfig, "vendor", 1 );
  66. # currencies
  67. @curr = split /:/, $form->{currencies};
  68. $form->{defaultcurrency} = $curr[0];
  69. chomp $form->{defaultcurrency};
  70. for (@curr) { $form->{selectcurrency} .= "<option>$_\n" }
  71. if ( @{ $form->{all_vendor} } ) {
  72. unless ( $form->{vendor_id} ) {
  73. $form->{vendor_id} = $form->{all_vendor}->[0]->{id};
  74. }
  75. }
  76. AA->get_name( \%myconfig, \%$form );
  77. delete $form->{notes};
  78. IR->retrieve_invoice( \%myconfig, \%$form );
  79. $form->{oldlanguage_code} = $form->{language_code};
  80. $form->get_partsgroup( \%myconfig,
  81. { language_code => $form->{language_code} } );
  82. if ( @{ $form->{all_partsgroup} } ) {
  83. $form->{selectpartsgroup} = "<option>\n";
  84. foreach $ref ( @{ $form->{all_partsgroup} } ) {
  85. if ( $ref->{translation} ) {
  86. $form->{selectpartsgroup} .=
  87. qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{translation}\n|;
  88. }
  89. else {
  90. $form->{selectpartsgroup} .=
  91. qq|<option value="$ref->{partsgroup}--$ref->{id}">$ref->{partsgroup}\n|;
  92. }
  93. }
  94. }
  95. if ( @{ $form->{all_project} } ) {
  96. $form->{selectprojectnumber} = "<option>\n";
  97. for ( @{ $form->{all_project} } ) {
  98. $form->{selectprojectnumber} .=
  99. qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n|;
  100. }
  101. }
  102. $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
  103. $form->{oldtransdate} = $form->{transdate};
  104. # vendors
  105. $form->{selectvendor} = "";
  106. if ( @{ $form->{all_vendor} } ) {
  107. $form->{vendor} = "$form->{vendor}--$form->{vendor_id}";
  108. for ( @{ $form->{all_vendor} } ) {
  109. $form->{selectvendor} .=
  110. qq|<option value="$_->{name}--$_->{id}">$_->{name}\n|;
  111. }
  112. }
  113. # departments
  114. if ( @{ $form->{all_department} } ) {
  115. $form->{selectdepartment} = "<option>\n";
  116. $form->{department} = "$form->{department}--$form->{department_id}"
  117. if $form->{department_id};
  118. for ( @{ $form->{all_department} } ) {
  119. $form->{selectdepartment} .=
  120. qq|<option value="$_->{description}--$_->{id}">$_->{description}\n|;
  121. }
  122. }
  123. if ( @{ $form->{all_language} } ) {
  124. $form->{selectlanguage} = "<option>\n";
  125. for ( @{ $form->{all_language} } ) {
  126. $form->{selectlanguage} .=
  127. qq|<option value="$_->{code}">$_->{description}\n|;
  128. }
  129. }
  130. # forex
  131. $form->{forex} = $form->{exchangerate};
  132. $exchangerate = ( $form->{exchangerate} ) ? $form->{exchangerate} : 1;
  133. foreach $key ( keys %{ $form->{AP_links} } ) {
  134. $form->{"select$key"} = "";
  135. foreach $ref ( @{ $form->{AP_links}{$key} } ) {
  136. $form->{"select$key"} .=
  137. "<option>$ref->{accno}--$ref->{description}\n";
  138. }
  139. if ( $key eq "AP_paid" ) {
  140. for $i ( 1 .. scalar @{ $form->{acc_trans}{$key} } ) {
  141. $form->{"AP_paid_$i"} =
  142. "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
  143. # reverse paid
  144. $form->{"paid_$i"} =
  145. $form->{acc_trans}{$key}->[ $i - 1 ]->{amount};
  146. $form->{"datepaid_$i"} =
  147. $form->{acc_trans}{$key}->[ $i - 1 ]->{transdate};
  148. $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
  149. $form->{acc_trans}{$key}->[ $i - 1 ]->{exchangerate};
  150. $form->{"source_$i"} =
  151. $form->{acc_trans}{$key}->[ $i - 1 ]->{source};
  152. $form->{"memo_$i"} =
  153. $form->{acc_trans}{$key}->[ $i - 1 ]->{memo};
  154. $form->{"cleared_$i"} =
  155. $form->{acc_trans}{$key}->[ $i - 1 ]->{cleared};
  156. $form->{paidaccounts} = $i;
  157. }
  158. }
  159. else {
  160. $form->{$key} =
  161. "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}"
  162. if $form->{acc_trans}{$key}->[0]->{accno};
  163. }
  164. }
  165. $form->{paidaccounts} = 1 unless ( exists $form->{paidaccounts} );
  166. $form->{AP} = $form->{AP_1} unless $form->{id};
  167. $form->{locked} =
  168. ( $form->{revtrans} )
  169. ? '1'
  170. : ( $form->datetonum( \%myconfig, $form->{transdate} ) <=
  171. $form->datetonum( \%myconfig, $form->{closedto} ) );
  172. if ( !$form->{readonly} ) {
  173. $form->{readonly} = 1 if $myconfig{acs} =~ /AP--Vendor Invoice/;
  174. }
  175. }
  176. sub prepare_invoice {
  177. $form->{type} = "invoice";
  178. $form->{currency} =~ s/ //g;
  179. $form->{oldcurrency} = $form->{currency};
  180. if ( $form->{id} ) {
  181. for (qw(invnumber ordnumber ponumber quonumber)) {
  182. $form->{$_} = $form->quote( $form->{$_} );
  183. }
  184. foreach $ref ( @{ $form->{invoice_details} } ) {
  185. $i++;
  186. for ( keys %$ref ) { $form->{"${_}_$i"} = $ref->{$_} }
  187. $form->{"projectnumber_$i"} =
  188. qq|$ref->{projectnumber}--$ref->{project_id}|
  189. if $ref->{project_id};
  190. $form->{"partsgroup_$i"} =
  191. qq|$ref->{partsgroup}--$ref->{partsgroup_id}|
  192. if $ref->{partsgroup_id};
  193. $form->{"discount_$i"} =
  194. $form->format_amount( \%myconfig, $form->{"discount_$i"} * 100 );
  195. ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  196. $dec = length $dec;
  197. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  198. $form->{"sellprice_$i"} =
  199. $form->format_amount( \%myconfig, $form->{"sellprice_$i"},
  200. $decimalplaces );
  201. $form->{"qty_$i"} =
  202. $form->format_amount( \%myconfig, $form->{"qty_$i"} );
  203. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  204. for (qw(partnumber sku description unit)) {
  205. $form->{"${_}_$i"} = $form->quote( $form->{"${_}_$i"} );
  206. }
  207. $form->{rowcount} = $i;
  208. }
  209. }
  210. }
  211. sub form_header {
  212. # set option selected
  213. for (qw(AP currency)) {
  214. $form->{"select$_"} =~ s/ selected//;
  215. $form->{"select$_"} =~
  216. s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
  217. }
  218. for (qw(vendor department)) {
  219. $form->{"select$_"} = $form->unescape( $form->{"select$_"} );
  220. $form->{"select$_"} =~ s/ selected//;
  221. $form->{"select$_"} =~ s/(<option value="\Q$form->{$_}\E")/$1 selected/;
  222. }
  223. if ( $form->{selectlanguage} ) {
  224. $form->{"selectlanguage"} =
  225. $form->unescape( $form->{"selectlanguage"} );
  226. $form->{"selectlanguage"} =~ s/ selected//;
  227. $form->{"selectlanguage"} =~
  228. s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
  229. $lang = qq|
  230. <tr>
  231. <th align="right" nowrap>| . $locale->text('Language') . qq|</th>
  232. <td><select name="language_code">$form->{selectlanguage}</select></td>
  233. <input type="hidden" name="oldlanguage_code" value="$form->{oldlanguage_code}">
  234. <input type="hidden" name="selectlanguage" value="|
  235. . $form->escape( $form->{selectlanguage}, 1 ) . qq|">
  236. </tr>
  237. |;
  238. }
  239. $form->{exchangerate} =
  240. $form->format_amount( \%myconfig, $form->{exchangerate} );
  241. $exchangerate = qq|<tr>|;
  242. $exchangerate .= qq|
  243. <th align="right" nowrap>| . $locale->text('Currency') . qq|</th>
  244. <td><select name="currency">$form->{selectcurrency}</select></td> |
  245. if $form->{defaultcurrency};
  246. $exchangerate .= qq|
  247. <input type="hidden" name="selectcurrency" value="$form->{selectcurrency}">
  248. <input type="hidden" name="defaultcurrency" value="$form->{defaultcurrency}">
  249. |;
  250. if ( $form->{defaultcurrency}
  251. && $form->{currency} ne $form->{defaultcurrency} )
  252. {
  253. if ( $form->{forex} ) {
  254. $exchangerate .= qq|
  255. <th align="right" nowrap>|
  256. . $locale->text('Exchange Rate')
  257. . qq|</th>
  258. <td>$form->{exchangerate}<input type="hidden" name="exchangerate" value="$form->{exchangerate}"></td>
  259. |;
  260. }
  261. else {
  262. $exchangerate .= qq|
  263. <th align="right" nowrap>|
  264. . $locale->text('Exchange Rate')
  265. . qq|</th>
  266. <td><input name="exchangerate" size="10" value="$form->{exchangerate}"></td>
  267. |;
  268. }
  269. }
  270. $exchangerate .= qq|
  271. <input type="hidden" name="forex" value="$form->{forex}">
  272. </tr>
  273. |;
  274. if ( $form->{selectvendor} ) {
  275. $vendor = qq|<select name="vendor">$form->{selectvendor}</select>
  276. <input type="hidden" name="selectvendor" value="|
  277. . $form->escape( $form->{selectvendor}, 1 ) . qq|">|;
  278. }
  279. else {
  280. $vendor = qq|<input name="vendor" value="$form->{vendor}" size="35">|;
  281. }
  282. $department = qq|
  283. <tr>
  284. <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
  285. <td colspan="3"><select name="department">$form->{selectdepartment}</select>
  286. <input type="hidden" name="selectdepartment" value="|
  287. . $form->escape( $form->{selectdepartment}, 1 ) . qq|">
  288. </td>
  289. </tr>
  290. | if $form->{selectdepartment};
  291. $n = ( $form->{creditremaining} < 0 ) ? "0" : "1";
  292. $i = $form->{rowcount} + 1;
  293. $focus = "partnumber_$i";
  294. $form->header;
  295. print qq|
  296. <body onLoad="document.forms[0].${focus}.focus()">
  297. <form method="post" action="$form->{script}">
  298. |;
  299. $form->{vc} = "vendor";
  300. $form->hide_form(
  301. qw(id title vc type terms creditlimit creditremaining closedto locked shipped oldtransdate recurring)
  302. );
  303. print qq|
  304. <table width="100%">
  305. <tr class="listtop">
  306. <th>$form->{title}</th>
  307. </tr>
  308. <tr height="5"></tr>
  309. <tr>
  310. <td>
  311. <table width="100%">
  312. <tr valign="top">
  313. <td>
  314. <table>
  315. <tr>
  316. <th align="right" nowrap>| . $locale->text('Vendor') . qq|</th>
  317. <td colspan="3">$vendor</td>
  318. <input type="hidden" name="vendor_id" value="$form->{vendor_id}">
  319. <input type="hidden" name="oldvendor" value="$form->{oldvendor}">
  320. </tr>
  321. <tr>
  322. <td></td>
  323. <td colspan="3">
  324. <table>
  325. <tr>
  326. <th nowrap>| . $locale->text('Credit Limit') . qq|</th>
  327. <td>|
  328. . $form->format_amount( \%myconfig, $form->{creditlimit}, 0, "0" )
  329. . qq|</td>
  330. <td width="20%"></td>
  331. <th nowrap>| . $locale->text('Remaining') . qq|</th>
  332. <td class="plus$n" nowrap>|
  333. . $form->format_amount( \%myconfig, $form->{creditremaining}, 0, "0" )
  334. . qq|</td>
  335. </tr>
  336. </table>
  337. </td>
  338. <tr>
  339. <th align="right">| . $locale->text('Record in') . qq|</th>
  340. <td colspan="3"><select name="AP">$form->{selectAP}</select></td>
  341. <input type="hidden" name="selectAP" value="$form->{selectAP}">
  342. </tr>
  343. $department
  344. $exchangerate
  345. </table>
  346. </td>
  347. <td align="right">
  348. <table>
  349. <tr>
  350. <th align="right" nowrap>| . $locale->text('Invoice Number') . qq|</th>
  351. <td><input name="invnumber" size="20" value="$form->{invnumber}"></td>
  352. </tr>
  353. <tr>
  354. <th align="right" nowrap>| . $locale->text('Order Number') . qq|</th>
  355. <td><input name="ordnumber" size="20" value="$form->{ordnumber}"></td>
  356. <input type="hidden" name="quonumber" value="$form->{quonumber}">
  357. </tr>
  358. <tr>
  359. <th align="right" nowrap>| . $locale->text('Invoice Date') . qq|</th>
  360. <td><input name="transdate" size="11" title="$myconfig{dateformat}" value="$form->{transdate}"></td>
  361. </tr>
  362. <tr>
  363. <th align="right" nowrap>| . $locale->text('Due Date') . qq|</th>
  364. <td><input name="duedate" size="11" title="$myconfig{dateformat}" value="$form->{duedate}"></td>
  365. </tr>
  366. <tr>
  367. <th align="right" nowrap>| . $locale->text('PO Number') . qq|</th>
  368. <td><input name="ponumber" size="20" value="$form->{ponumber}"></td>
  369. </tr>
  370. $lang
  371. </table>
  372. </td>
  373. </tr>
  374. </table>
  375. </td>
  376. </tr>
  377. |;
  378. $form->hide_form(qw(selectcurrency defaultcurrency taxaccounts));
  379. for ( split / /, $form->{taxaccounts} ) {
  380. $form->hide_form( "${_}_rate", "${_}_description" );
  381. }
  382. }
  383. sub form_footer {
  384. _calc_taxes();
  385. $form->{invtotal} = $form->{invsubtotal};
  386. if ( ( $rows = $form->numtextrows( $form->{notes}, 35, 8 ) ) < 2 ) {
  387. $rows = 2;
  388. }
  389. if ( ( $introws = $form->numtextrows( $form->{intnotes}, 35, 8 ) ) < 2 ) {
  390. $introws = 2;
  391. }
  392. $rows = ( $rows > $introws ) ? $rows : $introws;
  393. $notes =
  394. qq|<textarea name="notes" rows="$rows" cols="35" wrap="soft">$form->{notes}</textarea>|;
  395. $intnotes =
  396. qq|<textarea name="intnotes" rows="$rows" cols="35" wrap="soft">$form->{intnotes}</textarea>|;
  397. $tax = "";
  398. $form->{taxincluded} = ( $form->{taxincluded} ) ? "checked" : "";
  399. $taxincluded = "";
  400. if ( $form->{taxaccounts} ) {
  401. $taxincluded = qq|
  402. <input name="taxincluded" class="checkbox" type="checkbox" value="1" $form->{taxincluded}> <b>|
  403. . $locale->text('Tax Included') . qq|</b>
  404. |;
  405. }
  406. if ( !$form->{taxincluded} ) {
  407. foreach $item (keys %{$form->{taxes}}) {
  408. my $taccno = $item;
  409. $form->{invtotal} += $form->round_amount($form->{taxes}{$item}, 2);
  410. $form->{"${taccno}_total"} =
  411. $form->format_amount(\%myconfig,
  412. $form->round_amount($form->{taxes}{$item}, 2),
  413. 2);
  414. $tax .= qq|
  415. <tr>
  416. <th align="right">$form->{"${item}_description"}</th>
  417. <td align="right">$form->{"${item}_total"}</td>
  418. </tr>
  419. |;
  420. }
  421. $form->{invsubtotal} =
  422. $form->format_amount( \%myconfig, $form->{invsubtotal}, 2, 0 );
  423. $subtotal = qq|
  424. <tr>
  425. <th align="right">| . $locale->text('Subtotal') . qq|</th>
  426. <td align="right">$form->{invsubtotal}</td>
  427. </tr>
  428. |;
  429. }
  430. $form->{oldinvtotal} = $form->{invtotal};
  431. $form->{invtotal} =
  432. $form->format_amount( \%myconfig, $form->{invtotal}, 2, 0 );
  433. print qq|
  434. <tr>
  435. <td>
  436. <table width="100%">
  437. <tr valign="bottom">
  438. <td>
  439. <table>
  440. <tr>
  441. <th align="left">| . $locale->text('Notes') . qq|</th>
  442. <th align="left">| . $locale->text('Internal Notes') . qq|</th>
  443. <th align="left">| . $locale->text('Import Text') . qq|</th>
  444. </tr>
  445. <tr valign="top">
  446. <td>$notes</td>
  447. <td>$intnotes</td>
  448. <td><textarea name="import_text" rows="$rows" cols="25"></textarea>
  449. </tr>
  450. </table>
  451. </td>
  452. <td align="right">
  453. $taxincluded
  454. <br>
  455. <table>
  456. $subtotal
  457. $tax
  458. <tr>
  459. <th align="right">| . $locale->text('Total') . qq|</th>
  460. <td align="right">$form->{invtotal}</td>
  461. </tr>
  462. </table>
  463. </td>
  464. </tr>
  465. </table>
  466. </td>
  467. </tr>
  468. <tr>
  469. <td>
  470. <table width="100%">
  471. <tr>
  472. <th colspan="6" class="listheading">| . $locale->text('Payments') . qq|</th>
  473. </tr>
  474. |;
  475. if ( $form->{currency} eq $form->{defaultcurrency} ) {
  476. @column_index = qw(datepaid source memo paid AP_paid);
  477. }
  478. else {
  479. @column_index = qw(datepaid source memo paid exchangerate AP_paid);
  480. }
  481. $column_data{datepaid} = "<th>" . $locale->text('Date') . "</th>";
  482. $column_data{paid} = "<th>" . $locale->text('Amount') . "</th>";
  483. $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
  484. $column_data{AP_paid} = "<th>" . $locale->text('Account') . "</th>";
  485. $column_data{source} = "<th>" . $locale->text('Source') . "</th>";
  486. $column_data{memo} = "<th>" . $locale->text('Memo') . "</th>";
  487. print qq|
  488. <tr>
  489. |;
  490. for (@column_index) { print "$column_data{$_}\n" }
  491. print qq|
  492. </tr>
  493. |;
  494. $form->{paidaccounts}++ if ( $form->{"paid_$form->{paidaccounts}"} );
  495. for $i ( 1 .. $form->{paidaccounts} ) {
  496. $form->hide_form("cleared_$i");
  497. print qq|
  498. <tr>
  499. |;
  500. $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
  501. $form->{"selectAP_paid_$i"} =~
  502. s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
  503. # format amounts
  504. $totalpaid += $form->{"paid_$i"};
  505. $form->{"paid_$i"} =
  506. $form->format_amount( \%myconfig, $form->{"paid_$i"}, 2 );
  507. $form->{"exchangerate_$i"} =
  508. $form->format_amount( \%myconfig, $form->{"exchangerate_$i"} );
  509. $exchangerate = qq|&nbsp;|;
  510. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  511. if ( $form->{"forex_$i"} ) {
  512. $exchangerate =
  513. qq|<input type="hidden" name="exchangerate_$i" value="$form->{"exchangerate_$i"}">$form->{"exchangerate_$i"}|;
  514. }
  515. else {
  516. $exchangerate =
  517. qq|<input name="exchangerate_$i" size="10" value="$form->{"exchangerate_$i"}">|;
  518. }
  519. }
  520. $exchangerate .= qq|
  521. <input type="hidden" name="forex_$i" value="$form->{"forex_$i"}">
  522. |;
  523. $column_data{"paid_$i"} =
  524. qq|<td align="center"><input name="paid_$i" size="11" value="$form->{"paid_$i"}"></td>|;
  525. $column_data{"exchangerate_$i"} =
  526. qq|<td align="center">$exchangerate</td>|;
  527. $column_data{"AP_paid_$i"} =
  528. qq|<td align="center"><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
  529. $column_data{"datepaid_$i"} =
  530. qq|<td align="center"><input name="datepaid_$i" size="11" title="$myconfig{dateformat}" value="$form->{"datepaid_$i"}"></td>|;
  531. $column_data{"source_$i"} =
  532. qq|<td align="center"><input name="source_$i" size="11" value="$form->{"source_$i"}"></td>|;
  533. $column_data{"memo_$i"} =
  534. qq|<td align="center"><input name="memo_$i" size="11" value="$form->{"memo_$i"}"></td>|;
  535. for (@column_index) { print qq|$column_data{"${_}_$i"}\n| }
  536. print qq|
  537. </tr>
  538. |;
  539. }
  540. $form->{oldtotalpaid} = $totalpaid;
  541. $form->hide_form(qw(paidaccounts selectAP_paid oldinvtotal oldtotalpaid));
  542. print qq|
  543. </table>
  544. </td>
  545. </tr>
  546. <tr>
  547. <td><hr size="3" noshade></td>
  548. </tr>
  549. </table>
  550. <br>
  551. |;
  552. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  553. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  554. # type=submit $locale->text('Update')
  555. # type=submit $locale->text('Post')
  556. # type=submit $locale->text('Post as new')
  557. # type=submit $locale->text('Schedule')
  558. # type=submit $locale->text('Purchase Order')
  559. # type=submit $locale->text('Delete')
  560. if ( !$form->{readonly} ) {
  561. %button = (
  562. 'update' =>
  563. { ndx => 1, key => 'U', value => $locale->text('Update') },
  564. 'post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  565. 'post_as_new' =>
  566. { ndx => 5, key => 'N', value => $locale->text('Post as new') },
  567. 'purchase_order' => {
  568. ndx => 6,
  569. key => 'L',
  570. value => $locale->text('Purchase Order')
  571. },
  572. 'schedule' =>
  573. { ndx => 7, key => 'H', value => $locale->text('Schedule') },
  574. 'delete' =>
  575. { ndx => 8, key => 'D', value => $locale->text('Delete') },
  576. );
  577. if ( $form->{id} ) {
  578. if ( $form->{locked} ) {
  579. for ( "post", "delete" ) { delete $button{$_} }
  580. }
  581. }
  582. else {
  583. if ( $transdate > $closedto ) {
  584. for ( 'update', 'post', 'schedule' ) { $allowed{$_} = 1 }
  585. for ( keys %button ) { delete $button{$_} if !$allowed{$_} }
  586. }
  587. elsif ($closedto) {
  588. %buttons = ();
  589. }
  590. }
  591. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
  592. {
  593. $form->print_button( \%button, $_ );
  594. }
  595. }
  596. if ( $form->{lynx} ) {
  597. require "bin/menu.pl";
  598. &menubar;
  599. }
  600. $form->hide_form(qw(rowcount callback path login sessionid));
  601. print qq|
  602. </form>
  603. </body>
  604. </html>
  605. |;
  606. }
  607. sub import_text {
  608. my @o_list;
  609. my @i_lines = split( /(\n|\r|\r\n)/, $form->{import_text} );
  610. foreach $i (@i_lines) {
  611. chomp($i);
  612. if ( $i ne "" ) { # Strip out blank lines
  613. push @o_list, $i;
  614. }
  615. }
  616. my $c = 1;
  617. my $linenumber = 0;
  618. foreach $l (@o_list) {
  619. if ( $c % 2 ) {
  620. $linenumber += 1;
  621. $form->{"partnumber_$linenumber"} = $l;
  622. }
  623. else {
  624. $form->{"qty_$linenumber"} = $l;
  625. }
  626. $c += 1;
  627. $form->{rowcount} = $linenumber;
  628. IR->retrieve_item( \%myconfig, \%$form );
  629. $rows = scalar @{ $form->{item_list} };
  630. $rows = 0 unless $rows;
  631. $i = $form->{rowcount};
  632. if ( $rows = 0 ) {
  633. $form->{"id_$i"} = 0;
  634. $form->{"unit_$i"} = $locale->text('ea');
  635. &new_item;
  636. }
  637. elsif ( $rows > 1 ) {
  638. &select_item;
  639. exit;
  640. }
  641. else {
  642. map {
  643. $form->{item_list}[$i]{$_} =
  644. $form->quote( $form->{item_list}[$i]{$_} )
  645. } qw(partnumber description unit);
  646. map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
  647. keys %{ $form->{item_list}[0] };
  648. $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
  649. ($dec) = ( $s =~ /\.(\d+)/ );
  650. $dec = length $dec;
  651. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  652. $amount =
  653. $form->{"sellprice_$i"} * $form->{"qty_$i"} *
  654. ( 1 - $form->{"discount_$i"} / 100 );
  655. map { $form->{"${_}_base"} = 0 }
  656. ( split / /, $form->{taxaccounts} );
  657. map { $form->{"${_}_base"} += $amount }
  658. ( split / /, $form->{"taxaccounts_$i"} );
  659. if ( !$form->{taxincluded} ) {
  660. my @taxes = Tax::init_taxes( $form, $form->{taxaccounts} );
  661. $amount +=
  662. ( Tax::calculate_taxes( \@taxes, $form, $amount, 0 ) );
  663. }
  664. $form->{creditremaining} -= $amount;
  665. $form->{"sellprice_$i"} =
  666. $form->format_amount( \%myconfig, $form->{"sellprice_$i"},
  667. $decimalplaces );
  668. $form->{"qty_$i"} =
  669. $form->format_amount( \%myconfig, $form->{"qty_$i"} );
  670. }
  671. $form->{item_list} = [];
  672. }
  673. ++$form->{rowcount};
  674. }
  675. sub update {
  676. if ( $form->{import_text} ) {
  677. &import_text;
  678. }
  679. $form->{exchangerate} =
  680. $form->parse_amount( \%myconfig, $form->{exchangerate} );
  681. if ( $newname = &check_name(vendor) ) {
  682. &rebuild_vc( vendor, AP, $form->{transdate}, 1 );
  683. }
  684. if ( $form->{transdate} ne $form->{oldtransdate} ) {
  685. $form->{duedate} =
  686. ( $form->{terms} )
  687. ? $form->current_date( \%myconfig, $form->{transdate},
  688. $form->{terms} * 1 )
  689. : $form->{duedate};
  690. $form->{oldtransdate} = $form->{transdate};
  691. &rebuild_vc( vendor, AP, $form->{transdate}, 1 ) if !$newname;
  692. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  693. delete $form->{exchangerate};
  694. $form->{exchangerate} = $exchangerate
  695. if (
  696. $form->{forex} = (
  697. $exchangerate = $form->check_exchangerate(
  698. \%myconfig, $form->{currency},
  699. $form->{transdate}, 'sell'
  700. )
  701. )
  702. );
  703. $form->{oldcurrency} = $form->{currency};
  704. }
  705. }
  706. if ( $form->{currency} ne $form->{oldcurrency} ) {
  707. delete $form->{exchangerate};
  708. $form->{exchangerate} = $exchangerate
  709. if (
  710. $form->{forex} = (
  711. $exchangerate = $form->check_exchangerate(
  712. \%myconfig, $form->{currency},
  713. $form->{transdate}, 'sell'
  714. )
  715. )
  716. );
  717. }
  718. $j = 1;
  719. for $i ( 1 .. $form->{paidaccounts} ) {
  720. if ( $form->{"paid_$i"} ) {
  721. for (qw(datepaid source memo cleared)) {
  722. $form->{"${_}_$j"} = $form->{"${_}_$i"};
  723. }
  724. for (qw(paid exchangerate)) {
  725. $form->{"${_}_$j"} =
  726. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  727. }
  728. $form->{"exchangerate_$j"} = $exchangerate
  729. if (
  730. $form->{"forex_$j"} = (
  731. $exchangerate = $form->check_exchangerate(
  732. \%myconfig, $form->{currency},
  733. $form->{"datepaid_$j"}, 'sell'
  734. )
  735. )
  736. );
  737. if ( $j++ != $i ) {
  738. for (qw(datepaid source memo cleared paid exchangerate forex)) {
  739. delete $form->{"${_}_$i"};
  740. }
  741. }
  742. }
  743. else {
  744. for (qw(datepaid source memo cleared paid exchangerate forex)) {
  745. delete $form->{"${_}_$i"};
  746. }
  747. }
  748. $form->{paidaccounts} = $j;
  749. }
  750. $i = $form->{rowcount};
  751. $exchangerate = ( $form->{exchangerate} ) ? $form->{exchangerate} : 1;
  752. for (qw(partsgroup projectnumber)) {
  753. $form->{"select$_"} = $form->unescape( $form->{"select$_"} )
  754. if $form->{"select$_"};
  755. }
  756. if ( ( $form->{"partnumber_$i"} eq "" )
  757. && ( $form->{"description_$i"} eq "" )
  758. && ( $form->{"partsgroup_$i"} eq "" ) )
  759. {
  760. $form->{creditremaining} +=
  761. ( $form->{oldinvtotal} - $form->{oldtotalpaid} );
  762. &check_form;
  763. }
  764. else {
  765. IR->retrieve_item( \%myconfig, \%$form );
  766. my $rows = scalar @{ $form->{item_list} };
  767. if ( $form->{language_code} && $rows == 0 ) {
  768. $language_code = $form->{language_code};
  769. $form->{language_code} = "";
  770. IR->retrieve_item( \%myconfig, \%$form );
  771. $form->{language_code} = $language_code;
  772. $rows = scalar @{ $form->{item_list} };
  773. }
  774. if ($rows) {
  775. if ( $rows > 1 ) {
  776. &select_item;
  777. exit;
  778. }
  779. else {
  780. $form->{"qty_$i"} =
  781. ( $form->{"qty_$i"} * 1 ) ? $form->{"qty_$i"} : 1;
  782. $sellprice =
  783. $form->parse_amount( \%myconfig, $form->{"sellprice_$i"} );
  784. for (qw(partnumber description unit)) {
  785. $form->{item_list}[$i]{$_} =
  786. $form->quote( $form->{item_list}[$i]{$_} );
  787. }
  788. for ( keys %{ $form->{item_list}[0] } ) {
  789. $form->{"${_}_$i"} = $form->{item_list}[0]{$_};
  790. }
  791. if (! defined $form->{"discount_$i"}){
  792. $form->{"discount_$i"} = $form->{discount} * 100;
  793. }
  794. if ($sellprice) {
  795. $form->{"sellprice_$i"} = $sellprice;
  796. ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  797. $dec = length $dec;
  798. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  799. }
  800. else {
  801. ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  802. $dec = length $dec;
  803. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  804. $form->{"sellprice_$i"} /= $exchangerate;
  805. }
  806. $amount =
  807. $form->{"sellprice_$i"} * $form->{"qty_$i"} *
  808. ( 1 - $form->{"discount_$i"} / 100 );
  809. for ( split / /, $form->{taxaccounts} ) {
  810. $form->{"${_}_base"} = 0;
  811. }
  812. for ( split / /, $form->{"taxaccounts_$i"} ) {
  813. $form->{"${_}_base"} += $amount;
  814. }
  815. if ( !$form->{taxincluded} ) {
  816. my @taxes = Tax::init_taxes(
  817. $form,
  818. $form->{"taxaccounts_$i"},
  819. $form->{"taxaccounts"}
  820. );
  821. $amount +=
  822. ( Tax::calculate_taxes( \@taxes, $form, $amount, 0 ) );
  823. }
  824. $form->{creditremaining} -= $amount;
  825. $form->{"sellprice_$i"} =
  826. $form->format_amount( \%myconfig, $form->{"sellprice_$i"},
  827. $decimalplaces );
  828. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  829. for (qw(qty discount)) {
  830. $form->{"{_}_$i"} =
  831. $form->format_amount( \%myconfig, $form->{"${_}_$i"} );
  832. }
  833. }
  834. &display_form;
  835. }
  836. else {
  837. # ok, so this is a new part
  838. # ask if it is a part or service item
  839. if ( $form->{"partsgroup_$i"}
  840. && ( $form->{"partsnumber_$i"} eq "" )
  841. && ( $form->{"description_$i"} eq "" ) )
  842. {
  843. $form->{"discount_$i"} = "";
  844. &display_form;
  845. }
  846. else {
  847. $form->{"id_$i"} = 0;
  848. $form->{"unit_$i"} = $locale->text('ea');
  849. &new_item;
  850. }
  851. }
  852. }
  853. }
  854. sub post {
  855. $form->isblank( "transdate", $locale->text('Invoice Date missing!') );
  856. $form->isblank( "vendor", $locale->text('Vendor missing!') );
  857. # if the vendor changed get new values
  858. if ( &check_name(vendor) ) {
  859. &update;
  860. exit;
  861. }
  862. &validate_items;
  863. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  864. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  865. $form->error( $locale->text('Cannot post invoice for a closed period!') )
  866. if ( $transdate <= $closedto );
  867. $form->isblank( "exchangerate", $locale->text('Exchange rate missing!') )
  868. if ( $form->{currency} ne $form->{defaultcurrency} );
  869. for $i ( 1 .. $form->{paidaccounts} ) {
  870. if ( $form->{"paid_$i"} ) {
  871. $datepaid = $form->datetonum( \%myconfig, $form->{"datepaid_$i"} );
  872. $form->isblank( "datepaid_$i",
  873. $locale->text('Payment date missing!') );
  874. $form->error(
  875. $locale->text('Cannot post payment for a closed period!') )
  876. if ( $datepaid <= $closedto );
  877. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  878. $form->{"exchangerate_$i"} = $form->{exchangerate}
  879. if ( $transdate == $datepaid );
  880. $form->isblank( "exchangerate_$i",
  881. $locale->text('Exchange rate for payment missing!') );
  882. }
  883. }
  884. }
  885. if ( !$form->{repost} ) {
  886. if ( $form->{id} ) {
  887. &repost;
  888. exit;
  889. }
  890. }
  891. ( $form->{AP} ) = split /--/, $form->{AP};
  892. ( $form->{AP_paid} ) = split /--/, $form->{AP_paid};
  893. if ( IR->post_invoice( \%myconfig, \%$form ) ) {
  894. $form->redirect(
  895. $locale->text( 'Invoice [_1] posted!', $form->{invnumber} ) );
  896. }
  897. else {
  898. $form->error( $locale->text('Cannot post invoice!') );
  899. }
  900. }
  901. sub delete {
  902. $form->header;
  903. print qq|
  904. <body>
  905. <form method="post" action="$form->{script}">
  906. |;
  907. $form->{action} = "yes";
  908. $form->hide_form;
  909. print qq|
  910. <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
  911. <h4>|
  912. . $locale->text( 'Are you sure you want to delete Invoice Number [_1]?',
  913. $form->{invnumber} )
  914. . qq|</h4>
  915. <p>
  916. <button name="action" class="submit" type="submit" value="yes">|
  917. . $locale->text('Yes')
  918. . qq|</button>
  919. </form>
  920. |;
  921. }
  922. sub yes {
  923. if ( IR->delete_invoice( \%myconfig, \%$form ) ) {
  924. $form->redirect( $locale->text('Invoice deleted!') );
  925. }
  926. else {
  927. $form->error( $locale->text('Cannot delete invoice!') );
  928. }
  929. }