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