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