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