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