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