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