summaryrefslogtreecommitdiff
path: root/bin/is.pl
blob: ac2235d629df70e887a601744cecd2fb0dcd3446 (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,
  86. { language_code => $form->{language_code}, searchitems => 'nolabor' } );
  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. $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. my @taxes = Tax::init_taxes( $form, $form->{taxaccounts} );
  458. foreach $item (@taxes) {
  459. my $taccno = $item->account;
  460. $form->{invtotal} += $form->round_amount(
  461. $form->{"${taccno}_rate"} * $form->{"${taccno}_base"}, 2);
  462. $form->{"${taccno}_total"} =
  463. $form->format_amount( \%myconfig,
  464. $form->{"${taccno}_rate"} * $form->{"${taccno}_base"}, 2 );
  465. $tax .= qq|
  466. <tr>
  467. <th align=right>$form->{"${taccno}_description"}</th>
  468. <td align=right>$form->{"${taccno}_total"}</td>
  469. </tr>
  470. | if $form->{"${taccno}_base"};
  471. }
  472. $form->{invsubtotal} =
  473. $form->format_amount( \%myconfig, $form->{invsubtotal}, 2, 0 );
  474. $subtotal = qq|
  475. <tr>
  476. <th align=right>| . $locale->text('Subtotal') . qq|</th>
  477. <td align=right>$form->{invsubtotal}</td>
  478. </tr>
  479. |;
  480. }
  481. $form->{oldinvtotal} = $form->{invtotal};
  482. $form->{invtotal} =
  483. $form->format_amount( \%myconfig, $form->{invtotal}, 2, 0 );
  484. print qq|
  485. <tr>
  486. <td>
  487. <table width=100%>
  488. <tr valign=bottom>
  489. <td>
  490. <table>
  491. <tr>
  492. <th align=left>| . $locale->text('Notes') . qq|</th>
  493. <th align=left>| . $locale->text('Internal Notes') . qq|</th>
  494. </tr>
  495. <tr valign=top>
  496. <td>$notes</td>
  497. <td>$intnotes</td>
  498. </tr>
  499. </table>
  500. </td>
  501. <td align=right>
  502. <table>
  503. $subtotal
  504. $tax
  505. <tr>
  506. <th align=right>| . $locale->text('Total') . qq|</th>
  507. <td align=right>$form->{invtotal}</td>
  508. </tr>
  509. $taxincluded
  510. </table>
  511. </td>
  512. </tr>
  513. </table>
  514. </td>
  515. </tr>
  516. <tr>
  517. <td>
  518. <table width=100%>
  519. <tr class=listheading>
  520. <th colspan=6 class=listheading>| . $locale->text('Payments') . qq|</th>
  521. </tr>
  522. |;
  523. if ( $form->{currency} eq $form->{defaultcurrency} ) {
  524. @column_index = qw(datepaid source memo paid AR_paid);
  525. }
  526. else {
  527. @column_index = qw(datepaid source memo paid exchangerate AR_paid);
  528. }
  529. $column_data{datepaid} = "<th>" . $locale->text('Date') . "</th>";
  530. $column_data{paid} = "<th>" . $locale->text('Amount') . "</th>";
  531. $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
  532. $column_data{AR_paid} = "<th>" . $locale->text('Account') . "</th>";
  533. $column_data{source} = "<th>" . $locale->text('Source') . "</th>";
  534. $column_data{memo} = "<th>" . $locale->text('Memo') . "</th>";
  535. print "
  536. <tr>
  537. ";
  538. for (@column_index) { print "$column_data{$_}\n" }
  539. print "
  540. </tr>
  541. ";
  542. $form->{paidaccounts}++ if ( $form->{"paid_$form->{paidaccounts}"} );
  543. for $i ( 1 .. $form->{paidaccounts} ) {
  544. $form->hide_form("cleared_$i");
  545. print "
  546. <tr>\n";
  547. $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
  548. $form->{"selectAR_paid_$i"} =~
  549. s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
  550. # format amounts
  551. $totalpaid += $form->{"paid_$i"};
  552. $form->{"paid_$i"} =
  553. $form->format_amount( \%myconfig, $form->{"paid_$i"}, 2 );
  554. $form->{"exchangerate_$i"} =
  555. $form->format_amount( \%myconfig, $form->{"exchangerate_$i"} );
  556. $exchangerate = qq|&nbsp;|;
  557. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  558. if ( $form->{"forex_$i"} ) {
  559. $exchangerate =
  560. qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
  561. }
  562. else {
  563. $exchangerate =
  564. qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
  565. }
  566. }
  567. $exchangerate .= qq|
  568. <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
  569. |;
  570. $column_data{paid} =
  571. qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  572. $column_data{exchangerate} = qq|<td align=center>$exchangerate</td>|;
  573. $column_data{AR_paid} =
  574. qq|<td align=center><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
  575. $column_data{datepaid} =
  576. qq|<td align=center><input name="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
  577. $column_data{source} =
  578. qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
  579. $column_data{memo} =
  580. qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
  581. for (@column_index) { print qq|$column_data{$_}\n| }
  582. print "
  583. </tr>\n";
  584. }
  585. $form->{oldtotalpaid} = $totalpaid;
  586. $form->hide_form(qw(paidaccounts selectAR_paid oldinvtotal oldtotalpaid));
  587. print qq|
  588. </table>
  589. </td>
  590. </tr>
  591. <tr>
  592. <td><hr size=3 noshade></td>
  593. </tr>
  594. <tr>
  595. <td>
  596. |;
  597. &print_options;
  598. print qq|
  599. </td>
  600. </tr>
  601. </table>
  602. <br>
  603. |;
  604. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  605. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  606. # type=submit $locale->text('Update')
  607. # type=submit $locale->text('Print')
  608. # type=submit $locale->text('Post')
  609. # type=submit $locale->text('Print and Post')
  610. # type=submit $locale->text('Schedule')
  611. # type=submit $locale->text('Ship to')
  612. # type=submit $locale->text('Post as new')
  613. # type=submit $locale->text('Print and Post as new')
  614. # type=submit $locale->text('E-mail')
  615. # type=submit $locale->text('Delete')
  616. # type=submit $locale->text('Sales Order')
  617. if ( !$form->{readonly} ) {
  618. %button = (
  619. 'update' =>
  620. { ndx => 1, key => 'U', value => $locale->text('Update') },
  621. 'print' =>
  622. { ndx => 2, key => 'P', value => $locale->text('Print') },
  623. 'post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  624. 'ship_to' =>
  625. { ndx => 4, key => 'T', value => $locale->text('Ship to') },
  626. 'e_mail' =>
  627. { ndx => 5, key => 'E', value => $locale->text('E-mail') },
  628. 'print_and_post' => {
  629. ndx => 6,
  630. key => 'R',
  631. value => $locale->text('Print and Post')
  632. },
  633. 'post_as_new' =>
  634. { ndx => 7, key => 'N', value => $locale->text('Post as new') },
  635. 'print_and_post_as_new' => {
  636. ndx => 8,
  637. key => 'W',
  638. value => $locale->text('Print and Post as new')
  639. },
  640. 'sales_order' =>
  641. { ndx => 9, key => 'L', value => $locale->text('Sales Order') },
  642. 'schedule' =>
  643. { ndx => 10, key => 'H', value => $locale->text('Schedule') },
  644. 'delete' =>
  645. { ndx => 11, key => 'D', value => $locale->text('Delete') },
  646. );
  647. if ( $form->{id} ) {
  648. if ( $form->{locked} || $transdate <= $closedto ) {
  649. for ( "post", "print_and_post", "delete" ) {
  650. delete $button{$_};
  651. }
  652. }
  653. if ( !${LedgerSMB::Sysconfig::latex} ) {
  654. for ( "print_and_post", "print_and_post_as_new" ) {
  655. delete $button{$_};
  656. }
  657. }
  658. }
  659. else {
  660. if ( $transdate > $closedto ) {
  661. for ( "update", "ship_to", "print", "e_mail", "post",
  662. "schedule" )
  663. {
  664. $allowed{$_} = 1;
  665. }
  666. $a{'print_and_post'} = 1 if ${LedgerSMB::Sysconfig::latex};
  667. for ( keys %button ) { delete $button{$_} if !$allowed{$_} }
  668. }
  669. elsif ($closedto) {
  670. %button = ();
  671. }
  672. }
  673. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
  674. {
  675. $form->print_button( \%button, $_ );
  676. }
  677. }
  678. if ( $form->{lynx} ) {
  679. require "bin/menu.pl";
  680. &menubar;
  681. }
  682. $form->hide_form(qw(rowcount callback path login sessionid));
  683. print qq|
  684. </form>
  685. </body>
  686. </html>
  687. |;
  688. }
  689. sub update {
  690. $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. $form->{"discount_$i"} = $form->{discount} * 100;
  810. if ($sellprice) {
  811. $form->{"sellprice_$i"} = $sellprice;
  812. ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  813. $dec = length $dec;
  814. $decimalplaces1 = ( $dec > 2 ) ? $dec : 2;
  815. }
  816. else {
  817. ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  818. $dec = length $dec;
  819. $decimalplaces1 = ( $dec > 2 ) ? $dec : 2;
  820. $form->{"sellprice_$i"} /= $exchangerate;
  821. }
  822. ($dec) = ( $form->{"lastcost_$i"} =~ /\.(\d+)/ );
  823. $dec = length $dec;
  824. $decimalplaces2 = ( $dec > 2 ) ? $dec : 2;
  825. # if there is an exchange rate adjust sellprice
  826. for (qw(listprice lastcost)) {
  827. $form->{"${_}_$i"} /= $exchangerate;
  828. }
  829. $amount =
  830. $form->{"sellprice_$i"} * $form->{"qty_$i"} *
  831. ( 1 - $form->{"discount_$i"} / 100 );
  832. for ( split / /, $form->{taxaccounts} ) {
  833. $form->{"${_}_base"} = 0;
  834. }
  835. for ( split / /, $form->{"taxaccounts_$i"} ) {
  836. $form->{"${_}_base"} += $amount;
  837. }
  838. if ( !$form->{taxincluded} ) {
  839. my @taxes =
  840. Tax::init_taxes( $form, $form->{"taxaccounts_$i"},
  841. $form->{taxaccounts} );
  842. $amount +=
  843. Tax::calculate_taxes( \@taxes, $form, $amount, 0 );
  844. }
  845. $form->{creditremaining} -= $amount;
  846. for (qw(sellprice listprice)) {
  847. $form->{"${_}_$i"} =
  848. $form->format_amount( \%myconfig, $form->{"${_}_$i"},
  849. $decimalplaces1 );
  850. }
  851. $form->{"lastcost_$i"} =
  852. $form->format_amount( \%myconfig, $form->{"lastcost_$i"},
  853. $decimalplaces2 );
  854. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  855. for (qw(qty discount)) {
  856. $form->{"{_}_$i"} =
  857. $form->format_amount( \%myconfig, $form->{"${_}_$i"} );
  858. }
  859. }
  860. &display_form;
  861. }
  862. else {
  863. # ok, so this is a new part
  864. # ask if it is a part or service item
  865. if ( $form->{"partsgroup_$i"}
  866. && ( $form->{"partsnumber_$i"} eq "" )
  867. && ( $form->{"description_$i"} eq "" ) )
  868. {
  869. $form->{rowcount}--;
  870. &display_form;
  871. }
  872. else {
  873. $form->{"id_$i"} = 0;
  874. $form->{"unit_$i"} = $locale->text('ea');
  875. &new_item;
  876. }
  877. }
  878. }
  879. }
  880. sub post {
  881. $form->isblank( "transdate", $locale->text('Invoice Date missing!') );
  882. $form->isblank( "customer", $locale->text('Customer missing!') );
  883. # if oldcustomer ne customer redo form
  884. if ( &check_name(customer) ) {
  885. &update;
  886. exit;
  887. }
  888. &validate_items;
  889. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  890. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  891. $form->error( $locale->text('Cannot post invoice for a closed period!') )
  892. if ( $transdate <= $closedto );
  893. $form->isblank( "exchangerate", $locale->text('Exchange rate missing!') )
  894. if ( $form->{currency} ne $form->{defaultcurrency} );
  895. for $i ( 1 .. $form->{paidaccounts} ) {
  896. if ( $form->{"paid_$i"} ) {
  897. $datepaid = $form->datetonum( \%myconfig, $form->{"datepaid_$i"} );
  898. $form->isblank( "datepaid_$i",
  899. $locale->text('Payment date missing!') );
  900. $form->error(
  901. $locale->text('Cannot post payment for a closed period!') )
  902. if ( $datepaid <= $closedto );
  903. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  904. $form->{"exchangerate_$i"} = $form->{exchangerate}
  905. if ( $transdate == $datepaid );
  906. $form->isblank( "exchangerate_$i",
  907. $locale->text('Exchange rate for payment missing!') );
  908. }
  909. }
  910. }
  911. $form->{label} = $locale->text('Invoice');
  912. if ( !$form->{repost} ) {
  913. if ( $form->{id} ) {
  914. &repost;
  915. exit;
  916. }
  917. }
  918. ( $form->{AR} ) = split /--/, $form->{AR};
  919. ( $form->{AR_paid} ) = split /--/, $form->{AR_paid};
  920. if ( IS->post_invoice( \%myconfig, \%$form ) ) {
  921. $form->redirect(
  922. $locale->text( 'Invoice [_1] posted!', $form->{invnumber} ) );
  923. }
  924. else {
  925. $form->error( $locale->text('Cannot post invoice!') );
  926. }
  927. }
  928. sub print_and_post {
  929. $form->error( $locale->text('Select postscript or PDF!') )
  930. if $form->{format} !~ /(postscript|pdf)/;
  931. $form->error( $locale->text('Select a Printer!') )
  932. if $form->{media} eq 'screen';
  933. if ( !$form->{repost} ) {
  934. if ( $form->{id} ) {
  935. $form->{print_and_post} = 1;
  936. &repost;
  937. exit;
  938. }
  939. }
  940. $old_form = new Form;
  941. $form->{display_form} = "post";
  942. for ( keys %$form ) { $old_form->{$_} = $form->{$_} }
  943. $old_form->{rowcount}++;
  944. &print_form($old_form);
  945. }
  946. sub delete {
  947. $form->header;
  948. print qq|
  949. <body>
  950. <form method=post action=$form->{script}>
  951. |;
  952. $form->{action} = "yes";
  953. $form->hide_form;
  954. print qq|
  955. <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
  956. <h4>|
  957. . $locale->text( 'Are you sure you want to delete Invoice Number [_1]?',
  958. $form->{invnumber} )
  959. . qq|
  960. </h4>
  961. <p>
  962. <button name="action" class="submit" type="submit" value="yes">|
  963. . $locale->text('Yes')
  964. . qq|</button>
  965. </form>
  966. |;
  967. }
  968. sub yes {
  969. if (
  970. IS->delete_invoice(
  971. \%myconfig, \%$form, ${LedgerSMB::Sysconfig::spool}
  972. )
  973. )
  974. {
  975. $form->redirect( $locale->text('Invoice deleted!') );
  976. }
  977. else {
  978. $form->error( $locale->text('Cannot delete invoice!') );
  979. }
  980. }