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