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