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