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