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