summaryrefslogtreecommitdiff
path: root/bin/ir.pl
blob: e2a9a7f6fadf17e347b28ba39cf28c0cc833235e (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. $form->{invtotal} = $form->{invsubtotal};
  385. if ( ( $rows = $form->numtextrows( $form->{notes}, 35, 8 ) ) < 2 ) {
  386. $rows = 2;
  387. }
  388. if ( ( $introws = $form->numtextrows( $form->{intnotes}, 35, 8 ) ) < 2 ) {
  389. $introws = 2;
  390. }
  391. $rows = ( $rows > $introws ) ? $rows : $introws;
  392. $notes =
  393. qq|<textarea name=notes rows=$rows cols=35 wrap=soft>$form->{notes}</textarea>|;
  394. $intnotes =
  395. qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
  396. $form->{taxincluded} = ( $form->{taxincluded} ) ? "checked" : "";
  397. $taxincluded = "";
  398. if ( $form->{taxaccounts} ) {
  399. $taxincluded = qq|
  400. <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
  401. . $locale->text('Tax Included') . qq|</b>
  402. |;
  403. }
  404. if ( !$form->{taxincluded} ) {
  405. my @taxset = Tax::init_taxes( $form, $form->{taxaccounts} );
  406. foreach $taxobj (@taxset) {
  407. $item = $taxobj->account;
  408. $form->{invtotal} += $form->round_amount(
  409. $form->{"${item}_rate"} * $form->{"${item}_base"}, 2);
  410. $form->{"${item}_total"} =
  411. $form->format_amount( \%myconfig,
  412. $form->{"${item}_rate"} * $form->{"${item}_base"}, 2 );
  413. if ( $form->{"${item}_base"} ) {
  414. $form->{"${item}_total"} =
  415. $form->format_amount( \%myconfig,
  416. $form->round_amount( $taxobj->value, 2 ), 2 );
  417. $tax .= qq|
  418. <tr>
  419. <th align=right>$form->{"${item}_description"}</th>
  420. <td align=right>$form->{"${item}_total"}</td>
  421. </tr>
  422. |;
  423. }
  424. }
  425. $form->{invsubtotal} =
  426. $form->format_amount( \%myconfig, $form->{invsubtotal}, 2, 0 );
  427. $subtotal = qq|
  428. <tr>
  429. <th align=right>| . $locale->text('Subtotal') . qq|</th>
  430. <td align=right>$form->{invsubtotal}</td>
  431. </tr>
  432. |;
  433. }
  434. $form->{oldinvtotal} = $form->{invtotal};
  435. $form->{invtotal} =
  436. $form->format_amount( \%myconfig, $form->{invtotal}, 2, 0 );
  437. print qq|
  438. <tr>
  439. <td>
  440. <table width=100%>
  441. <tr valign=bottom>
  442. <td>
  443. <table>
  444. <tr>
  445. <th align=left>| . $locale->text('Notes') . qq|</th>
  446. <th align=left>| . $locale->text('Internal Notes') . qq|</th>
  447. <th align=left>| . $locale->text('Import Text') . qq|</th>
  448. </tr>
  449. <tr valign=top>
  450. <td>$notes</td>
  451. <td>$intnotes</td>
  452. <td><textarea name=import_text rows=$rows cols=25></textarea>
  453. </tr>
  454. </table>
  455. </td>
  456. <td align=right>
  457. $taxincluded
  458. <br>
  459. <table>
  460. $subtotal
  461. $tax
  462. <tr>
  463. <th align=right>| . $locale->text('Total') . qq|</th>
  464. <td align=right>$form->{invtotal}</td>
  465. </tr>
  466. </table>
  467. </td>
  468. </tr>
  469. </table>
  470. </td>
  471. </tr>
  472. <tr>
  473. <td>
  474. <table width=100%>
  475. <tr>
  476. <th colspan=6 class=listheading>| . $locale->text('Payments') . qq|</th>
  477. </tr>
  478. |;
  479. if ( $form->{currency} eq $form->{defaultcurrency} ) {
  480. @column_index = qw(datepaid source memo paid AP_paid);
  481. }
  482. else {
  483. @column_index = qw(datepaid source memo paid exchangerate AP_paid);
  484. }
  485. $column_data{datepaid} = "<th>" . $locale->text('Date') . "</th>";
  486. $column_data{paid} = "<th>" . $locale->text('Amount') . "</th>";
  487. $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
  488. $column_data{AP_paid} = "<th>" . $locale->text('Account') . "</th>";
  489. $column_data{source} = "<th>" . $locale->text('Source') . "</th>";
  490. $column_data{memo} = "<th>" . $locale->text('Memo') . "</th>";
  491. print qq|
  492. <tr>
  493. |;
  494. for (@column_index) { print "$column_data{$_}\n" }
  495. print qq|
  496. </tr>
  497. |;
  498. $form->{paidaccounts}++ if ( $form->{"paid_$form->{paidaccounts}"} );
  499. for $i ( 1 .. $form->{paidaccounts} ) {
  500. $form->hide_form("cleared_$i");
  501. print qq|
  502. <tr>
  503. |;
  504. $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
  505. $form->{"selectAP_paid_$i"} =~
  506. s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
  507. # format amounts
  508. $totalpaid += $form->{"paid_$i"};
  509. $form->{"paid_$i"} =
  510. $form->format_amount( \%myconfig, $form->{"paid_$i"}, 2 );
  511. $form->{"exchangerate_$i"} =
  512. $form->format_amount( \%myconfig, $form->{"exchangerate_$i"} );
  513. $exchangerate = qq|&nbsp;|;
  514. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  515. if ( $form->{"forex_$i"} ) {
  516. $exchangerate =
  517. qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
  518. }
  519. else {
  520. $exchangerate =
  521. qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
  522. }
  523. }
  524. $exchangerate .= qq|
  525. <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
  526. |;
  527. $column_data{"paid_$i"} =
  528. qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  529. $column_data{"exchangerate_$i"} =
  530. qq|<td align=center>$exchangerate</td>|;
  531. $column_data{"AP_paid_$i"} =
  532. qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
  533. $column_data{"datepaid_$i"} =
  534. qq|<td align=center><input name="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
  535. $column_data{"source_$i"} =
  536. qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
  537. $column_data{"memo_$i"} =
  538. qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
  539. for (@column_index) { print qq|$column_data{"${_}_$i"}\n| }
  540. print qq|
  541. </tr>
  542. |;
  543. }
  544. $form->{oldtotalpaid} = $totalpaid;
  545. $form->hide_form(qw(paidaccounts selectAP_paid oldinvtotal oldtotalpaid));
  546. print qq|
  547. </table>
  548. </td>
  549. </tr>
  550. <tr>
  551. <td><hr size=3 noshade></td>
  552. </tr>
  553. </table>
  554. <br>
  555. |;
  556. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  557. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  558. # type=submit $locale->text('Update')
  559. # type=submit $locale->text('Post')
  560. # type=submit $locale->text('Post as new')
  561. # type=submit $locale->text('Schedule')
  562. # type=submit $locale->text('Purchase Order')
  563. # type=submit $locale->text('Delete')
  564. if ( !$form->{readonly} ) {
  565. %button = (
  566. 'update' =>
  567. { ndx => 1, key => 'U', value => $locale->text('Update') },
  568. 'post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  569. 'post_as_new' =>
  570. { ndx => 5, key => 'N', value => $locale->text('Post as new') },
  571. 'purchase_order' => {
  572. ndx => 6,
  573. key => 'L',
  574. value => $locale->text('Purchase Order')
  575. },
  576. 'schedule' =>
  577. { ndx => 7, key => 'H', value => $locale->text('Schedule') },
  578. 'delete' =>
  579. { ndx => 8, key => 'D', value => $locale->text('Delete') },
  580. );
  581. if ( $form->{id} ) {
  582. if ( $form->{locked} ) {
  583. for ( "post", "delete" ) { delete $button{$_} }
  584. }
  585. }
  586. else {
  587. if ( $transdate > $closedto ) {
  588. for ( 'update', 'post', 'schedule' ) { $allowed{$_} = 1 }
  589. for ( keys %button ) { delete $button{$_} if !$allowed{$_} }
  590. }
  591. elsif ($closedto) {
  592. %buttons = ();
  593. }
  594. }
  595. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
  596. {
  597. $form->print_button( \%button, $_ );
  598. }
  599. }
  600. if ( $form->{lynx} ) {
  601. require "bin/menu.pl";
  602. &menubar;
  603. }
  604. $form->hide_form(qw(rowcount callback path login sessionid));
  605. print qq|
  606. </form>
  607. </body>
  608. </html>
  609. |;
  610. }
  611. sub import_text {
  612. my @o_list;
  613. my @i_lines = split( /(\n|\r|\r\n)/, $form->{import_text} );
  614. foreach $i (@i_lines) {
  615. chomp($i);
  616. if ( $i ne "" ) { # Strip out blank lines
  617. push @o_list, $i;
  618. }
  619. }
  620. my $c = 1;
  621. my $linenumber = 0;
  622. foreach $l (@o_list) {
  623. if ( $c % 2 ) {
  624. $linenumber += 1;
  625. $form->{"partnumber_$linenumber"} = $l;
  626. }
  627. else {
  628. $form->{"qty_$linenumber"} = $l;
  629. }
  630. $c += 1;
  631. $form->{rowcount} = $linenumber;
  632. IR->retrieve_item( \%myconfig, \%$form );
  633. $rows = scalar @{ $form->{item_list} };
  634. $rows = 0 unless $rows;
  635. $i = $form->{rowcount};
  636. if ( $rows = 0 ) {
  637. $form->{"id_$i"} = 0;
  638. $form->{"unit_$i"} = $locale->text('ea');
  639. &new_item;
  640. }
  641. elsif ( $rows > 1 ) {
  642. &select_item;
  643. exit;
  644. }
  645. else {
  646. map {
  647. $form->{item_list}[$i]{$_} =
  648. $form->quote( $form->{item_list}[$i]{$_} )
  649. } qw(partnumber description unit);
  650. map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} }
  651. keys %{ $form->{item_list}[0] };
  652. $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
  653. ($dec) = ( $s =~ /\.(\d+)/ );
  654. $dec = length $dec;
  655. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  656. $amount =
  657. $form->{"sellprice_$i"} * $form->{"qty_$i"} *
  658. ( 1 - $form->{"discount_$i"} / 100 );
  659. map { $form->{"${_}_base"} = 0 }
  660. ( split / /, $form->{taxaccounts} );
  661. map { $form->{"${_}_base"} += $amount }
  662. ( split / /, $form->{"taxaccounts_$i"} );
  663. if ( !$form->{taxincluded} ) {
  664. my @taxes = Tax::init_taxes( $form, $form->{taxaccounts} );
  665. $amount +=
  666. ( Tax::calculate_taxes( \@taxes, $form, $amount, 0 ) );
  667. }
  668. $form->{creditremaining} -= $amount;
  669. $form->{"sellprice_$i"} =
  670. $form->format_amount( \%myconfig, $form->{"sellprice_$i"},
  671. $decimalplaces );
  672. $form->{"qty_$i"} =
  673. $form->format_amount( \%myconfig, $form->{"qty_$i"} );
  674. }
  675. $form->{item_list} = [];
  676. }
  677. ++$form->{rowcount};
  678. }
  679. sub update {
  680. if ( $form->{import_text} ) {
  681. &import_text;
  682. }
  683. $form->{exchangerate} =
  684. $form->parse_amount( \%myconfig, $form->{exchangerate} );
  685. if ( $newname = &check_name(vendor) ) {
  686. &rebuild_vc( vendor, AP, $form->{transdate}, 1 );
  687. }
  688. if ( $form->{transdate} ne $form->{oldtransdate} ) {
  689. $form->{duedate} =
  690. ( $form->{terms} )
  691. ? $form->current_date( \%myconfig, $form->{transdate},
  692. $form->{terms} * 1 )
  693. : $form->{duedate};
  694. $form->{oldtransdate} = $form->{transdate};
  695. &rebuild_vc( vendor, AP, $form->{transdate}, 1 ) if !$newname;
  696. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  697. delete $form->{exchangerate};
  698. $form->{exchangerate} = $exchangerate
  699. if (
  700. $form->{forex} = (
  701. $exchangerate = $form->check_exchangerate(
  702. \%myconfig, $form->{currency},
  703. $form->{transdate}, 'sell'
  704. )
  705. )
  706. );
  707. $form->{oldcurrency} = $form->{currency};
  708. }
  709. }
  710. if ( $form->{currency} ne $form->{oldcurrency} ) {
  711. delete $form->{exchangerate};
  712. $form->{exchangerate} = $exchangerate
  713. if (
  714. $form->{forex} = (
  715. $exchangerate = $form->check_exchangerate(
  716. \%myconfig, $form->{currency},
  717. $form->{transdate}, 'sell'
  718. )
  719. )
  720. );
  721. }
  722. $j = 1;
  723. for $i ( 1 .. $form->{paidaccounts} ) {
  724. if ( $form->{"paid_$i"} ) {
  725. for (qw(datepaid source memo cleared)) {
  726. $form->{"${_}_$j"} = $form->{"${_}_$i"};
  727. }
  728. for (qw(paid exchangerate)) {
  729. $form->{"${_}_$j"} =
  730. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  731. }
  732. $form->{"exchangerate_$j"} = $exchangerate
  733. if (
  734. $form->{"forex_$j"} = (
  735. $exchangerate = $form->check_exchangerate(
  736. \%myconfig, $form->{currency},
  737. $form->{"datepaid_$j"}, 'sell'
  738. )
  739. )
  740. );
  741. if ( $j++ != $i ) {
  742. for (qw(datepaid source memo cleared paid exchangerate forex)) {
  743. delete $form->{"${_}_$i"};
  744. }
  745. }
  746. }
  747. else {
  748. for (qw(datepaid source memo cleared paid exchangerate forex)) {
  749. delete $form->{"${_}_$i"};
  750. }
  751. }
  752. $form->{paidaccounts} = $j;
  753. }
  754. $i = $form->{rowcount};
  755. $exchangerate = ( $form->{exchangerate} ) ? $form->{exchangerate} : 1;
  756. for (qw(partsgroup projectnumber)) {
  757. $form->{"select$_"} = $form->unescape( $form->{"select$_"} )
  758. if $form->{"select$_"};
  759. }
  760. if ( ( $form->{"partnumber_$i"} eq "" )
  761. && ( $form->{"description_$i"} eq "" )
  762. && ( $form->{"partsgroup_$i"} eq "" ) )
  763. {
  764. $form->{creditremaining} +=
  765. ( $form->{oldinvtotal} - $form->{oldtotalpaid} );
  766. &check_form;
  767. }
  768. else {
  769. IR->retrieve_item( \%myconfig, \%$form );
  770. my $rows = scalar @{ $form->{item_list} };
  771. if ( $form->{language_code} && $rows == 0 ) {
  772. $language_code = $form->{language_code};
  773. $form->{language_code} = "";
  774. IR->retrieve_item( \%myconfig, \%$form );
  775. $form->{language_code} = $language_code;
  776. $rows = scalar @{ $form->{item_list} };
  777. }
  778. if ($rows) {
  779. if ( $rows > 1 ) {
  780. &select_item;
  781. exit;
  782. }
  783. else {
  784. $form->{"qty_$i"} =
  785. ( $form->{"qty_$i"} * 1 ) ? $form->{"qty_$i"} : 1;
  786. $sellprice =
  787. $form->parse_amount( \%myconfig, $form->{"sellprice_$i"} );
  788. for (qw(partnumber description unit)) {
  789. $form->{item_list}[$i]{$_} =
  790. $form->quote( $form->{item_list}[$i]{$_} );
  791. }
  792. for ( keys %{ $form->{item_list}[0] } ) {
  793. $form->{"${_}_$i"} = $form->{item_list}[0]{$_};
  794. }
  795. $form->{"discount_$i"} = $form->{discount} * 100;
  796. if ($sellprice) {
  797. $form->{"sellprice_$i"} = $sellprice;
  798. ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  799. $dec = length $dec;
  800. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  801. }
  802. else {
  803. ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  804. $dec = length $dec;
  805. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  806. $form->{"sellprice_$i"} /= $exchangerate;
  807. }
  808. $amount =
  809. $form->{"sellprice_$i"} * $form->{"qty_$i"} *
  810. ( 1 - $form->{"discount_$i"} / 100 );
  811. for ( split / /, $form->{taxaccounts} ) {
  812. $form->{"${_}_base"} = 0;
  813. }
  814. for ( split / /, $form->{"taxaccounts_$i"} ) {
  815. $form->{"${_}_base"} += $amount;
  816. }
  817. if ( !$form->{taxincluded} ) {
  818. my @taxes = Tax::init_taxes(
  819. $form,
  820. $form->{"taxaccounts_$i"},
  821. $form->{"taxaccounts"}
  822. );
  823. $amount +=
  824. ( Tax::calculate_taxes( \@taxes, $form, $amount, 0 ) );
  825. }
  826. $form->{creditremaining} -= $amount;
  827. $form->{"sellprice_$i"} =
  828. $form->format_amount( \%myconfig, $form->{"sellprice_$i"},
  829. $decimalplaces );
  830. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  831. for (qw(qty discount)) {
  832. $form->{"{_}_$i"} =
  833. $form->format_amount( \%myconfig, $form->{"${_}_$i"} );
  834. }
  835. }
  836. &display_form;
  837. }
  838. else {
  839. # ok, so this is a new part
  840. # ask if it is a part or service item
  841. if ( $form->{"partsgroup_$i"}
  842. && ( $form->{"partsnumber_$i"} eq "" )
  843. && ( $form->{"description_$i"} eq "" ) )
  844. {
  845. $form->{"discount_$i"} = "";
  846. &display_form;
  847. }
  848. else {
  849. $form->{"id_$i"} = 0;
  850. $form->{"unit_$i"} = $locale->text('ea');
  851. &new_item;
  852. }
  853. }
  854. }
  855. }
  856. sub post {
  857. $form->isblank( "transdate", $locale->text('Invoice Date missing!') );
  858. $form->isblank( "vendor", $locale->text('Vendor missing!') );
  859. # if the vendor changed get new values
  860. if ( &check_name(vendor) ) {
  861. &update;
  862. exit;
  863. }
  864. &validate_items;
  865. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  866. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  867. $form->error( $locale->text('Cannot post invoice for a closed period!') )
  868. if ( $transdate <= $closedto );
  869. $form->isblank( "exchangerate", $locale->text('Exchange rate missing!') )
  870. if ( $form->{currency} ne $form->{defaultcurrency} );
  871. for $i ( 1 .. $form->{paidaccounts} ) {
  872. if ( $form->{"paid_$i"} ) {
  873. $datepaid = $form->datetonum( \%myconfig, $form->{"datepaid_$i"} );
  874. $form->isblank( "datepaid_$i",
  875. $locale->text('Payment date missing!') );
  876. $form->error(
  877. $locale->text('Cannot post payment for a closed period!') )
  878. if ( $datepaid <= $closedto );
  879. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  880. $form->{"exchangerate_$i"} = $form->{exchangerate}
  881. if ( $transdate == $datepaid );
  882. $form->isblank( "exchangerate_$i",
  883. $locale->text('Exchange rate for payment missing!') );
  884. }
  885. }
  886. }
  887. if ( !$form->{repost} ) {
  888. if ( $form->{id} ) {
  889. &repost;
  890. exit;
  891. }
  892. }
  893. ( $form->{AP} ) = split /--/, $form->{AP};
  894. ( $form->{AP_paid} ) = split /--/, $form->{AP_paid};
  895. if ( IR->post_invoice( \%myconfig, \%$form ) ) {
  896. $form->redirect(
  897. $locale->text( 'Invoice [_1] posted!', $form->{invnumber} ) );
  898. }
  899. else {
  900. $form->error( $locale->text('Cannot post invoice!') );
  901. }
  902. }
  903. sub delete {
  904. $form->header;
  905. print qq|
  906. <body>
  907. <form method=post action=$form->{script}>
  908. |;
  909. $form->{action} = "yes";
  910. $form->hide_form;
  911. print qq|
  912. <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
  913. <h4>|
  914. . $locale->text( 'Are you sure you want to delete Invoice Number [_1]?',
  915. $form->{invnumber} )
  916. . qq|</h4>
  917. <p>
  918. <button name="action" class="submit" type="submit" value="yes">|
  919. . $locale->text('Yes')
  920. . qq|</button>
  921. </form>
  922. |;
  923. }
  924. sub yes {
  925. if ( IR->delete_invoice( \%myconfig, \%$form ) ) {
  926. $form->redirect( $locale->text('Invoice deleted!') );
  927. }
  928. else {
  929. $form->error( $locale->text('Cannot delete invoice!') );
  930. }
  931. }