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