summaryrefslogtreecommitdiff
path: root/bin/pos.pl
blob: dd7fb46f19aca9cbcc9b8fe89e429fa125e325b8 (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) 2003
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. # Contributors: Steve Doerr <sdoerr907@everestkc.net>
  22. #
  23. #
  24. # This program is free software; you can redistribute it and/or modify
  25. # it under the terms of the GNU General Public License as published by
  26. # the Free Software Foundation; either version 2 of the License, or
  27. # (at your option) any later version.
  28. #
  29. # This program is distributed in the hope that it will be useful,
  30. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. # GNU General Public License for more details.
  33. # You should have received a copy of the GNU General Public License
  34. # along with this program; if not, write to the Free Software
  35. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36. #=====================================================================
  37. #
  38. # POS
  39. #
  40. #=====================================================================
  41. use Error qw(:try);
  42. use LedgerSMB::Template;
  43. use LedgerSMB::Tax;
  44. 1;
  45. # end
  46. sub check_alert {
  47. my $rc = $form->{'rowcount'};
  48. if ( !$form->{"partnumber_$rc"} ) {
  49. --$rc; # Ensures that alert shows up when item is selected from a list;
  50. }
  51. for ( 1 .. $rc ) {
  52. $form->{'check_id'} = ( $form->{'check_id'} || $form->{"check_id_$_"} );
  53. }
  54. }
  55. sub send_to_pd {
  56. socket( SOCK, 2, 1, getprotobynumber( $pos_config{'pd_proto'} ) );
  57. connect( SOCK, $pos_config{'pd_dest'} );
  58. my $rn = $numrows - 1;
  59. my $ds_string = sprintf(
  60. '%s%s @ $%-7.2f%s%s%s',
  61. $pd_control{'new_line'},
  62. $form->{"qty_$rn"},
  63. $form->{"sellprice_$rn"},
  64. $pd_control{'new_line'},
  65. "Subtotal: \$" . sprintf( '%-7.2f', $form->{'invtotal'} )
  66. );
  67. print SOCK $ds_string;
  68. close SOCK;
  69. }
  70. sub on_update {
  71. &send_to_pd;
  72. &check_alert;
  73. }
  74. sub open_drawer {
  75. open( PRINTER, "|-", ${LedgerSMB::Sysconfig::printer}{Printer} );
  76. print PRINTER $pos_config{'rp_cash_open'};
  77. close PRINTER;
  78. sleep 1;
  79. }
  80. sub open {
  81. &open_drawer;
  82. &update;
  83. }
  84. sub add {
  85. $form->{nextsub} = 'add';
  86. $form->{title} = $locale->text('Add POS Invoice');
  87. $form->{callback} =
  88. "$form->{script}?action=$form->{nextsub}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  89. &invoice_links;
  90. $form->{type} = "pos_invoice";
  91. $form->{format} = "txt";
  92. $form->{media} = ( $myconfig{printer} ) ? $myconfig{printer} : "screen";
  93. $form->{rowcount} = 0;
  94. $form->{readonly} = ( $myconfig{acs} =~ /POS--Sale/ ) ? 1 : 0;
  95. $ENV{REMOTE_ADDR} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
  96. $form->{till} = $4;
  97. $form->{partsgroup} = "";
  98. for ( @{ $form->{all_partsgroup} } ) {
  99. $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n";
  100. }
  101. $form->{dontdisplayend} = 1;
  102. &display_form;
  103. $form->{dontdisplayrows} = 1;
  104. $form->{dontdisplayend} = 0;
  105. &openinvoices;
  106. }
  107. sub openinvoices {
  108. undef %column_data;
  109. undef %column_heading;
  110. $form->{customer} = "";
  111. $form->{customer_id} = 0;
  112. delete $form->{l_subtotal};
  113. delete $form->{subtotal};
  114. $ENV{REMOTE_ADDR} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
  115. $form->{till} = $4;
  116. $form->{sort} = 'transdate';
  117. for (qw(open l_invnumber l_transdate l_name l_amount l_curr l_till)) {
  118. $form->{$_} = 'Y';
  119. }
  120. if ( $myconfig{role} ne 'user' ) {
  121. $form->{l_employee} = 'Y';
  122. }
  123. $form->{title} = $locale->text('Open');
  124. &transactions;
  125. }
  126. sub edit {
  127. $form->{title} = $locale->text('Edit POS Invoice');
  128. $form->{callback} =
  129. "$form->{script}?action=$form->{nextsub}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  130. &invoice_links;
  131. &prepare_invoice;
  132. $form->{type} = "pos_invoice";
  133. $form->{format} = "txt";
  134. $form->{media} = ( $myconfig{printer} ) ? $myconfig{printer} : "screen";
  135. $form->{readonly} = ( $myconfig{acs} =~ /POS--Sale/ ) ? 1 : 0;
  136. $form->{partsgroup} = "";
  137. for ( @{ $form->{all_partsgroup} } ) {
  138. $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n";
  139. }
  140. &display_form;
  141. }
  142. sub form_header {
  143. if ( !$form->{'check_id'} ) {
  144. &check_alert;
  145. }
  146. # set option selected
  147. for (qw(AR currency)) {
  148. $form->{"select$_"} =~ s/ selected//;
  149. $form->{"select$_"} =~
  150. s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
  151. }
  152. for (qw(customer department employee)) {
  153. $form->{"select$_"} = $form->unescape( $form->{"select$_"} );
  154. $form->{"select$_"} =~ s/ selected//;
  155. $form->{"select$_"} =~ s/(<option value="\Q$form->{$_}\E")/$1 selected/;
  156. }
  157. $form->{exchangerate} =
  158. $form->format_amount( \%myconfig, $form->{exchangerate} );
  159. $exchangerate = qq|<tr>|;
  160. $exchangerate .= qq|
  161. <th align=right nowrap>| . $locale->text('Currency') . qq|</th>
  162. <td><select name=currency>$form->{selectcurrency}</select></td> |
  163. if $form->{defaultcurrency};
  164. $exchangerate .= qq|
  165. <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
  166. <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
  167. |;
  168. if ( $form->{defaultcurrency}
  169. && $form->{currency} ne $form->{defaultcurrency} )
  170. {
  171. if ( $form->{forex} ) {
  172. $exchangerate .=
  173. qq|<th align=right>|
  174. . $locale->text('Exchange Rate')
  175. . qq|</th><td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>|;
  176. }
  177. else {
  178. $exchangerate .=
  179. qq|<th align=right>|
  180. . $locale->text('Exchange Rate')
  181. . qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
  182. }
  183. }
  184. $exchangerate .= qq|
  185. <input type=hidden name=forex value=$form->{forex}>
  186. </tr>
  187. |;
  188. if ( $form->{selectcustomer} ) {
  189. $customer = qq|<select name=customer>$form->{selectcustomer}</select>
  190. <input type=hidden name="selectcustomer" value="|
  191. . $form->escape( $form->{selectcustomer}, 1 ) . qq|">|;
  192. }
  193. else {
  194. $customer = qq|<input name=customer value="$form->{customer}" size=35>|;
  195. }
  196. $department = qq|
  197. <tr>
  198. <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
  199. <td colspan=3><select name=department>$form->{selectdepartment}</select>
  200. <input type=hidden name=selectdepartment value="|
  201. . $form->escape( $form->{selectdepartment}, 1 ) . qq|">
  202. </td>
  203. </tr>
  204. | if $form->{selectdepartment};
  205. $employee = qq|
  206. <tr>
  207. <th align=right nowrap>| . $locale->text('Salesperson') . qq|</th>
  208. <td colspan=3><select name=employee>$form->{selectemployee}</select></td>
  209. <input type=hidden name=selectemployee value="|
  210. . $form->escape( $form->{selectemployee}, 1 ) . qq|">
  211. </tr>
  212. | if $form->{selectemployee};
  213. if ( $form->{change} != $form->{oldchange} ) {
  214. $form->{creditremaining} -= $form->{oldchange};
  215. }
  216. $n = ( $form->{creditremaining} < 0 ) ? "0" : "1";
  217. if ( $form->{business} ) {
  218. $business = qq|
  219. <tr>
  220. <th align=right nowrap>| . $locale->text('Business') . qq|</th>
  221. <td>$form->{business}</td>
  222. <td width=10></td>
  223. <th align=right nowrap>| . $locale->text('Trade Discount') . qq|</th>
  224. <td>|
  225. . $form->format_amount( \%myconfig, $form->{tradediscount} * 100 )
  226. . qq| %</td>
  227. </tr>
  228. |;
  229. }
  230. if ( $form->{selectlanguage} ) {
  231. if ( $form->{language_code} ne $form->{oldlanguage_code} ) {
  232. # rebuild partsgroup
  233. $form->get_partsgroup(\%myconfig, { all => 1 });
  234. $form->{partsgroup} = "";
  235. for ( @{ $form->{all_partsgroup} } ) {
  236. $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n";
  237. }
  238. $form->{oldlanguage_code} = $form->{language_code};
  239. }
  240. $form->{"selectlanguage"} =
  241. $form->unescape( $form->{"selectlanguage"} );
  242. $form->{"selectlanguage"} =~ s/ selected//;
  243. $form->{"selectlanguage"} =~
  244. s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
  245. $lang = qq|
  246. <tr>
  247. <th align=right>| . $locale->text('Language') . qq|</th>
  248. <td colspan=3><select name=language_code>$form->{selectlanguage}</select></td>
  249. </tr>
  250. <input type=hidden name=oldlanguage_code value=$form->{oldlanguage_code}>
  251. <input type=hidden name=selectlanguage value="|
  252. . $form->escape( $form->{selectlanguage}, 1 ) . qq|">|;
  253. }
  254. $i = $form->{rowcount} + 1;
  255. $focus = "partnumber_$i";
  256. $form->header();
  257. print qq|
  258. <body onLoad="document.forms[0].${focus}.focus()" />
  259. <form method=post action="$form->{script}">
  260. |;
  261. $form->hide_form(
  262. qw(id till type format printed title discount creditlimit creditremaining tradediscount business closedto locked oldtransdate customer_id oldcustomer check_id)
  263. );
  264. print qq|
  265. <input type=hidden name=vc value="customer">
  266. <table width=100%>
  267. <tr class=listtop>
  268. <th class=listtop>$form->{title}</font></th>
  269. </tr>|;
  270. if ( $form->{'check_id'} ) {
  271. print qq|
  272. <tr class=listtop>
  273. <th class=listtop style="background-color: red">|
  274. . $locale->text('Check ID') . qq|
  275. </th>
  276. </tr>|;
  277. }
  278. print qq|
  279. <tr height="5"></tr>
  280. <tr>
  281. <td>
  282. <table width=100%>
  283. <tr valign=top>
  284. <td>
  285. <table>
  286. <tr>
  287. <th align=right nowrap>| . $locale->text('Customer') . qq|</th>
  288. <td colspan=3>$customer</td>
  289. </tr>
  290. <tr>
  291. <td></td>
  292. <td colspan=3>
  293. <table>
  294. <tr>
  295. <th align=right nowrap>| . $locale->text('Credit Limit') . qq|</th>
  296. <td>$form->{creditlimit}</td>
  297. <td width=10></td>
  298. <th align=right nowrap>| . $locale->text('Remaining') . qq|</th>
  299. <td class="plus$n">|
  300. . $form->format_amount( \%myconfig, $form->{creditremaining}, 0, "0" )
  301. . qq|</font></td>
  302. </tr>
  303. $business
  304. </table>
  305. </td>
  306. </tr>
  307. <tr>
  308. <th align=right nowrap>| . $locale->text('Record in') . qq|</th>
  309. <td colspan=3><select name=AR>$form->{selectAR}</select></td>
  310. <input type=hidden name=selectAR value="$form->{selectAR}">
  311. </tr>
  312. $department
  313. </table>
  314. </td>
  315. <td>
  316. <table>
  317. $employee
  318. $exchangerate
  319. $lang
  320. </table>
  321. </td>
  322. </tr>
  323. </table>
  324. </td>
  325. </tr>
  326. <tr>
  327. <td>
  328. </td>
  329. </tr>
  330. |;
  331. $form->hide_form(
  332. qw(taxaccounts duedate invnumber transdate selectcurrency defaultcurrency)
  333. );
  334. for ( split / /, $form->{taxaccounts} ) {
  335. $form->hide_form( "${_}_rate", "${_}_description", "${_}_taxnumber" );
  336. }
  337. }
  338. sub form_footer {
  339. $form->{invtotal} = $form->{invsubtotal};
  340. $form->{taxincluded} = ( $form->{taxincluded} ) ? "checked" : "";
  341. $taxincluded = "";
  342. if ( $form->{taxaccounts} ) {
  343. $taxincluded = qq|
  344. <tr height="5"></tr>
  345. <tr>
  346. <td align=right>
  347. <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td><th align=left>|
  348. . $locale->text('Tax Included')
  349. . qq|</th>
  350. </tr>
  351. |;
  352. }
  353. if ( !$form->{taxincluded} ) {
  354. my @taxset = Tax::init_taxes( $form, $form->{taxaccounts} );
  355. foreach $taxobj (@taxset) {
  356. $item = $taxobj->account;
  357. $form->{invtotal} += $form->round_amount(
  358. $form->{"${item}_rate"} * $form->{"${item}_base"}, 2);
  359. $form->{"${item}_total"} =
  360. $form->format_amount( \%myconfig,
  361. $form->{"${item}_rate"} * $form->{"${item}_base"}, 2 );
  362. if ( $form->{"${item}_base"} ) {
  363. $form->{"${item}_total"} =
  364. $form->format_amount( \%myconfig,
  365. $form->round_amount( $taxobj->value, 2 ), 2 );
  366. $tax .= qq|
  367. <tr>
  368. <th align=right>$form->{"${item}_description"}</th>
  369. <td align=right>$form->{"${item}_total"}</td>
  370. </tr>
  371. |;
  372. }
  373. }
  374. $form->{invsubtotal} =
  375. $form->format_amount( \%myconfig, $form->{invsubtotal}, 2, 0 );
  376. $subtotal = qq|
  377. <tr>
  378. <th align=right>| . $locale->text('Subtotal') . qq|</th>
  379. <td align=right>$form->{invsubtotal}</td>
  380. </tr>
  381. |;
  382. }
  383. @column_index = qw(paid memo source cctrack AR_paid);
  384. $column_data{paid} = "<th>" . $locale->text('Amount') . "</th>";
  385. $column_data{source} = "<th>" . $locale->text('Source') . "</th>";
  386. $column_data{memo} = "<th>" . $locale->text('Memo') . "</th>";
  387. $column_data{cctrack} =
  388. "<th>" . $locale->text('Credit Card Track') . "</th>";
  389. $column_data{AR_paid} = "<th>&nbsp;</th>";
  390. print qq|
  391. <tr>
  392. <td>
  393. <table width=100%>
  394. <tr valign=top>
  395. <td>
  396. <table>
  397. <tr>
  398. |;
  399. for (@column_index) { print "$column_data{$_}\n"; }
  400. print qq|
  401. </tr>
  402. |;
  403. $totalpaid = 0;
  404. $form->{paidaccounts}++ if ( $form->{"paid_$form->{paidaccounts}"} );
  405. my $memoselect = qq|<SELECT name="MEMONAME">|;
  406. for ( sort keys %pos_sources ) {
  407. $memoselect .= qq|<option value="$_">$pos_sources{$_}</option>|;
  408. }
  409. $memoselect .= qq|</SELECT>|;
  410. for $i ( 1 .. $form->{paidaccounts} ) {
  411. $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
  412. $form->{"selectAR_paid_$i"} =~
  413. s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
  414. # format amounts
  415. $totalpaid += $form->{"paid_$i"};
  416. $form->{"paid_$i"} =
  417. $form->format_amount( \%myconfig, $form->{"paid_$i"}, 2 );
  418. $form->{"exchangerate_$i"} =
  419. $form->format_amount( \%myconfig, $form->{"exchangerate_$i"} );
  420. if ( $form->{"paid__$i"} ) {
  421. $column_data{paid} =
  422. qq|<td><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  423. }
  424. else {
  425. $column_data{paid} =
  426. qq|<td><input accesskey='n' name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  427. }
  428. $column_data{source} =
  429. qq|<td><input name="source_$i" size=10 value="$form->{"source_$i"}"></td>|;
  430. $column_data{memo} = qq|<td>$memoselect</td>|;
  431. $column_data{memo} =~ s/MEMONAME/memo_$i/;
  432. if ( !$form->{"memo_$i"} ) {
  433. $form->{"memo_$i"} = $pos_source_default;
  434. }
  435. my $memval = $form->{"memo_$i"};
  436. $column_data{memo} =~ s/(option value="$memval")/$1 SELECTED/;
  437. $column_data{cctrack} = qq|<td><input type="text" name="cctrack_$i"
  438. value="| . $form->{"cctrack_$i"} . qq|" size="3"></td>|;
  439. if ( $pos_config{"coa_prefix"} ) {
  440. if ( !$form->{"AR_paid_$i"} ) {
  441. $form->{"AR_paid_$i"} =
  442. $pos_config{"coa_prefix"} . '.' . $pos_config{"till"};
  443. }
  444. $column_data{AR_paid} = qq|<input type=hidden name="AR_paid_$i"
  445. value='$form->{"AR_paid_$i"}'>|;
  446. }
  447. else {
  448. $column_data{AR_paid} =
  449. qq|<td><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
  450. }
  451. print qq|
  452. <tr>
  453. |;
  454. for (@column_index) { print "$column_data{$_}\n"; }
  455. print qq|
  456. </tr>
  457. |;
  458. $form->hide_form( "cleared_$i", "exchangerate_$i", "forex_$i" );
  459. }
  460. $form->{change} = 0;
  461. if ( $totalpaid > $form->{invtotal} ) {
  462. $form->{change} = $totalpaid - $form->{invtotal};
  463. }
  464. $form->{oldchange} = $form->{change};
  465. $form->{change} = $form->format_amount( \%myconfig, $form->{change}, 2, 0 );
  466. $form->{totalpaid} = $form->format_amount( \%myconfig, $totalpaid, 2 );
  467. $form->{oldinvtotal} = $form->{invtotal};
  468. $form->{invtotal} =
  469. $form->format_amount( \%myconfig, $form->{invtotal}, 2, 0 );
  470. print qq|
  471. <tr>
  472. <th align=right>| . $locale->text('Change') . qq|</th>
  473. <th>$form->{change}</th>
  474. </tr>
  475. </table>
  476. </td>
  477. <td align=right>
  478. <table>
  479. $subtotal
  480. $tax
  481. <tr>
  482. <th align=right>| . $locale->text('Total') . qq|</th>
  483. <td align=right>$form->{invtotal}</td>
  484. </tr>
  485. $taxincluded
  486. </table>
  487. </td>
  488. </tr>
  489. </table>
  490. </td>
  491. </tr>
  492. <input type=hidden name=oldtotalpaid value=$totalpaid>
  493. <input type=hidden name=datepaid value=$form->{transdate}>
  494. <tr>
  495. <td>
  496. |;
  497. $form->hide_form(
  498. qw(paidaccounts selectAR_paid oldinvtotal change oldchange invtotal));
  499. &print_options;
  500. print qq|
  501. </td>
  502. </tr>
  503. <tr>
  504. <td><hr size=3 noshade></td>
  505. </tr>
  506. </table>
  507. |;
  508. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  509. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  510. # type=submit $locale->text('Update')
  511. # type=submit $locale->text('Print')
  512. # type=submit $locale->text('Post')
  513. # type=submit $locale->text('Print and Post')
  514. # type=submit $locale->text('Delete')
  515. if ( !$form->{readonly} ) {
  516. %button = (
  517. 'update' =>
  518. { ndx => 1, key => 'U', value => $locale->text('Update') },
  519. 'print' =>
  520. { ndx => 2, key => 'P', value => $locale->text('Print') },
  521. 'post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  522. 'print_and_post' => {
  523. ndx => 4,
  524. key => 'R',
  525. value => $locale->text('Print and Post')
  526. },
  527. 'delete' =>
  528. { ndx => 5, key => 'D', value => $locale->text('Delete') },
  529. );
  530. if ( $transdate > $closedto ) {
  531. if ( !$form->{id} ) {
  532. delete $button{'delete'};
  533. }
  534. delete $button{'print_and_post'}
  535. unless ${LedgerSMB::Sysconfig::latex};
  536. }
  537. else {
  538. for ( 'print', 'post', 'print_and_post', 'delete' ) {
  539. delete $button{$_};
  540. }
  541. }
  542. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
  543. {
  544. $form->print_button( \%button, $_ );
  545. }
  546. if ( $form->{partsgroup} ) {
  547. $form->{partsgroup} =~ s/\r//g;
  548. $form->{partsgroup} = $form->quote( $form->{partsgroup} );
  549. $spc = ( $form->{path} =~ /lynx/ ) ? "." : " ";
  550. print qq|
  551. <input type=hidden name=nextsub value=lookup_partsgroup>
  552. <input type=hidden name=partsgroup value="$form->{partsgroup}">|;
  553. foreach $item ( split /\n/, $form->{partsgroup} ) {
  554. ( $partsgroup, $translation ) = split /--/, $item;
  555. $item = ($translation) ? $translation : $partsgroup;
  556. print
  557. qq| <button class="submit" type="submit" name="action" value="$spc$item">$spc$item</button>\n|
  558. if $item;
  559. }
  560. }
  561. }
  562. if ( $form->{lynx} ) {
  563. require "bin/menu.pl";
  564. &menubar;
  565. }
  566. $form->hide_form(qw(rowcount callback path login sessionid));
  567. print qq|</form>|;
  568. if ( !$form->{dontdisplayend} ) {
  569. print qq|
  570. </body>
  571. </html>
  572. |;
  573. }
  574. }
  575. sub post {
  576. $form->{media} = 'Printer';
  577. $form->isblank( "customer", $locale->text('Customer missing!') );
  578. # if oldcustomer ne customer redo form
  579. $customer = $form->{customer};
  580. $customer =~ s/--.*//g;
  581. $customer .= "--$form->{customer_id}";
  582. if ( $customer ne $form->{oldcustomer} ) {
  583. &update;
  584. exit;
  585. }
  586. &validate_items;
  587. &print;
  588. $form->isblank( "exchangerate", $locale->text('Exchange rate missing!') )
  589. if ( $form->{currency} ne $form->{defaultcurrency} );
  590. $paid = 0;
  591. for ( 1 .. $form->{paidaccounts} ) {
  592. $paid += $form->parse_amount( \%myconfig, $form->{"paid_$_"} );
  593. }
  594. delete $form->{datepaid} unless $paid;
  595. $total = $form->parse_amount( \%myconfig, $form->{invtotal} );
  596. # deduct change from first payment
  597. #$form->{"paid_1"} = $form->{invtotal} if $paid > $total;
  598. $form->{paid} = $paid;
  599. if ( $paid > $total ) {
  600. ++$form->{paidaccounts};
  601. my $pa = $form->{paidaccounts};
  602. $form->{"paid_$pa"} = $total - $paid;
  603. $form->{"source_$pa"} = 'cash';
  604. $form->{"exchangerate_$pa"} = 0;
  605. $form->{"AR_paid_$pa"} = $form->{AR_paid_1};
  606. }
  607. ( $form->{AR} ) = split /--/, $form->{AR};
  608. if ( IS->post_invoice( \%myconfig, \%$form ) ) {
  609. $form->redirect( $locale->text('Posted!') );
  610. }
  611. else {
  612. $form->error( $locale->text('Cannot post transaction!') );
  613. }
  614. }
  615. sub display_row {
  616. my $numrows = shift;
  617. @column_index =
  618. qw(partnumber description partsgroup qty unit sellprice discount linetotal);
  619. $form->{invsubtotal} = 0;
  620. for ( split / /, $form->{taxaccounts} ) { $form->{"${_}_base"} = 0; }
  621. $column_data{partnumber} =
  622. qq|<th class=listheading nowrap>| . $locale->text('Number') . qq|</th>|;
  623. $column_data{description} =
  624. qq|<th class=listheading nowrap>|
  625. . $locale->text('Description')
  626. . qq|</th>|;
  627. $column_data{qty} =
  628. qq|<th class=listheading nowrap>| . $locale->text('Qty') . qq|</th>|;
  629. $column_data{unit} =
  630. qq|<th class=listheading nowrap>| . $locale->text('Unit') . qq|</th>|;
  631. $column_data{sellprice} =
  632. qq|<th class=listheading nowrap>| . $locale->text('Price') . qq|</th>|;
  633. $column_data{linetotal} =
  634. qq|<th class=listheading nowrap>| . $locale->text('Extended') . qq|</th>|;
  635. $column_data{discount} = qq|<th class=listheading nowrap>%</th>|;
  636. print qq|
  637. <tr>
  638. <td>
  639. <table width=100%>
  640. <tr class=listheading>|;
  641. for (@column_index) { print "\n$column_data{$_}"; }
  642. print qq|
  643. </tr>
  644. |;
  645. $exchangerate = $form->parse_amount( \%myconfig, $form->{exchangerate} );
  646. $exchangerate = ($exchangerate) ? $exchangerate : 1;
  647. for $i ( 1 .. $numrows ) {
  648. # undo formatting
  649. for (qw(qty discount sellprice)) {
  650. $form->{"${_}_$i"} =
  651. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  652. }
  653. ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  654. $dec = length $dec;
  655. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  656. if ( ( $form->{"qty_$i"} != $form->{"oldqty_$i"} )
  657. || ( $form->{currency} ne $form->{oldcurrency} ) )
  658. {
  659. # check for a pricematrix
  660. @a = split / /, $form->{"pricematrix_$i"};
  661. if ( scalar @a ) {
  662. foreach $item (@a) {
  663. ( $q, $p ) = split /:/, $item;
  664. if ( ( $p * 1 ) && ( $form->{"qty_$i"} >= ( $q * 1 ) ) ) {
  665. ($dec) = ( $p =~ /\.(\d+)/ );
  666. $dec = length $dec;
  667. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  668. $form->{"sellprice_$i"} =
  669. $form->round_amount( $p / $exchangerate,
  670. $decimalplaces );
  671. }
  672. }
  673. }
  674. }
  675. if ( $i < $numrows ) {
  676. $column_data{discount} =
  677. qq|<td align=right><input name="discount_$i" size=3 value=|
  678. . $form->format_amount( \%myconfig, $form->{"discount_$i"} )
  679. . qq|></td>|;
  680. }
  681. else {
  682. $column_data{discount} = qq|<td></td>|;
  683. }
  684. $discount =
  685. $form->round_amount(
  686. $form->{"sellprice_$i"} * $form->{"discount_$i"} / 100,
  687. $decimalplaces );
  688. $linetotal = $form->round_amount( $form->{"sellprice_$i"} - $discount,
  689. $decimalplaces );
  690. $linetotal = $form->round_amount( $linetotal * $form->{"qty_$i"}, 2 );
  691. for (qw(partnumber sku description partsgroup unit)) {
  692. $form->{"${_}_$i"} = $form->quote( $form->{"${_}_$i"} );
  693. }
  694. $column_data{partnumber} =
  695. qq|<td><input name="partnumber_$i" size=20 value="$form->{"partnumber_$i"}" accesskey="$i" title="[Alt-$i]"></td>|;
  696. if (
  697. ( $rows = $form->numtextrows( $form->{"description_$i"}, 40, 6 ) ) >
  698. 1 )
  699. {
  700. $column_data{description} =
  701. qq|<td><textarea name="description_$i" rows=$rows cols=46 wrap=soft>$form->{"description_$i"}</textarea></td>|;
  702. }
  703. else {
  704. $column_data{description} =
  705. qq|<td><input name="description_$i" size=48 value="$form->{"description_$i"}"></td>|;
  706. }
  707. $column_data{qty} =
  708. qq|<td align=right><input name="qty_$i" size=5 value=|
  709. . $form->format_amount( \%myconfig, $form->{"qty_$i"} )
  710. . qq|></td>|;
  711. $column_data{unit} = qq|<td>$form->{"unit_$i"}</td>|;
  712. $column_data{sellprice} =
  713. qq|<td align=right><input name="sellprice_$i" size=9 value=|
  714. . $form->format_amount( \%myconfig, $form->{"sellprice_$i"},
  715. $decimalplaces )
  716. . qq|></td>|;
  717. $column_data{linetotal} =
  718. qq|<td align=right>|
  719. . $form->format_amount( \%myconfig, $linetotal, 2 )
  720. . qq|</td>|;
  721. print qq|
  722. <tr valign=top>|;
  723. for (@column_index) { print "\n$column_data{$_}"; }
  724. print qq|
  725. </tr>
  726. |;
  727. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  728. for (
  729. qw(id listprice lastcost taxaccounts pricematrix oldqty sku partsgroup unit inventory_accno_id income_accno_id expense_accno_id)
  730. )
  731. {
  732. $form->hide_form("${_}_$i");
  733. }
  734. for ( split / /, $form->{"taxaccounts_$i"} ) {
  735. $form->{"${_}_base"} += $linetotal;
  736. }
  737. $form->{invsubtotal} += $linetotal;
  738. }
  739. print qq|
  740. </table>
  741. </td>
  742. </tr>
  743. <input type=hidden name=oldcurrency value=$form->{currency}>
  744. |;
  745. }
  746. sub print {
  747. if ( !$form->{invnumber} ) {
  748. $form->{invnumber} = $form->update_defaults( \%myconfig, 'sinumber' );
  749. }
  750. $rc = $form->{'rowcount'};
  751. $pc = $form->{'paidaccounts'};
  752. if ( $form->{"partnumber_$rc"}
  753. || $form->{"description_$rc"}
  754. || $form->{"paid_$pc"} )
  755. {
  756. &update;
  757. exit;
  758. }
  759. for $i ( 1 .. $rc - 1 ) {
  760. if ( $form->{"qty_$i"} != $form->{"oldqty_$i"} ) {
  761. &update;
  762. exit;
  763. }
  764. }
  765. if ( !$form->{invnumber} ) {
  766. $form->{invnumber} = $form->update_defaults( \%myconfig, "sinumber" );
  767. if ( $form->{media} eq 'screen' ) {
  768. &update;
  769. exit;
  770. }
  771. }
  772. $old_form = new Form;
  773. for ( keys %$form ) { $old_form->{$_} = $form->{$_}; }
  774. for (qw(employee department)) { $form->{$_} =~ s/--.*//g; }
  775. $form->{invdate} = $form->{transdate};
  776. my @lt = localtime();
  777. $form->{dateprinted} = $lt[2] . ":" . $lt[1] . ":" . $lt[0];
  778. &print_form($old_form);
  779. }
  780. sub print_form {
  781. my $old_form = shift;
  782. &open_drawer;
  783. # if oldcustomer ne customer redo form
  784. $customer = $form->{customer};
  785. $customer =~ s/--.*//g;
  786. $customer .= "--$form->{customer_id}";
  787. if ( $customer ne $form->{oldcustomer} ) {
  788. &update;
  789. exit;
  790. }
  791. &validate_items;
  792. &{"$form->{vc}_details"};
  793. @a = ();
  794. for ( 1 .. $form->{rowcount} ) {
  795. push @a, ( "partnumber_$_", "description_$_" );
  796. }
  797. for ( split / /, $form->{taxaccounts} ) { push @a, "${_}_description"; }
  798. $form->format_string(@a);
  799. # format payment dates
  800. for ( 1 .. $form->{paidaccounts} ) {
  801. $form->{"datepaid_$_"} =
  802. $locale->date( \%myconfig, $form->{"datepaid_$_"} );
  803. }
  804. IS->invoice_details( \%myconfig, \%$form );
  805. if ( $form->parse_amount( \%myconfig, $form->{total} ) <= 0 ) {
  806. $form->{total} = 0;
  807. }
  808. else {
  809. $form->{change} = 0;
  810. }
  811. for (qw(company address tel fax businessnumber)) {
  812. $form->{$_} = $myconfig{$_};
  813. }
  814. $form->{username} = $myconfig{name};
  815. $form->{address} =~ s/\\n/\n/g;
  816. push @a, qw(company address tel fax businessnumber username);
  817. $form->format_string(@a);
  818. $form->{templates} = "$myconfig{templates}";
  819. $form->{IN} = "$form->{type}.$form->{format}";
  820. if ( $form->{format} =~ /(postscript|pdf)/ ) {
  821. $form->{IN} =~ s/$&$/tex/;
  822. }
  823. if ( $form->{media} ne 'screen' ) {
  824. $form->{OUT} = ${LedgerSMB::Sysconfig::printer}{ $form->{media} };
  825. $form->{printmode} = '|-';
  826. }
  827. $form->{discount} =
  828. $form->format_amount( \%myconfig, $form->{discount} * 100 );
  829. $form->{rowcount}--;
  830. $form->{pre} = "<body bgcolor=#ffffff>\n<pre>";
  831. delete $form->{stylesheet};
  832. $form->{cd_open} = $pos_config{rp_cash_drawer_open};
  833. my $template = LedgerSMB::Template->new(user => \%myconfig,
  834. template => $form->{'formname'}, format => uc $form->{format} );
  835. try {
  836. $template->render($form);
  837. $template->output(%{$form});
  838. }
  839. catch Error::Simple with {
  840. my $E = shift;
  841. $form->error( $E->stacktrace );
  842. };
  843. if ( $form->{printed} !~ /$form->{formname}/ ) {
  844. $form->{printed} .= " $form->{formname}";
  845. $form->{printed} =~ s/^ //;
  846. $form->update_status( \%myconfig );
  847. }
  848. $old_form->{printed} = $form->{printed};
  849. # if we got back here restore the previous form
  850. if ( $form->{media} ne 'screen' ) {
  851. # restore and display form
  852. for ( keys %$old_form ) { $form->{$_} = $old_form->{$_}; }
  853. $form->{exchangerate} =
  854. $form->parse_amount( \%myconfig, $form->{exchangerate} );
  855. for $i ( 1 .. $form->{paidaccounts} ) {
  856. for (qw(paid exchangerate)) {
  857. $form->{"${_}_$i"} =
  858. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  859. }
  860. }
  861. delete $form->{pre};
  862. if ( !$form->{printandpost} ) {
  863. $form->{rowcount}--;
  864. #&display_form;
  865. }
  866. }
  867. }
  868. sub print_and_post {
  869. $form->error( $locale->text('Select a Printer!') )
  870. if ( $form->{media} eq 'screen' );
  871. $form->{printandpost} = 1;
  872. &print;
  873. &post;
  874. }
  875. sub lookup_partsgroup {
  876. $form->{my_partsgroup} =~ s/\r//;
  877. $form->{action} = $form->{my_partsgroup};
  878. if ( $form->{language_code} ) {
  879. # get english
  880. foreach $item ( split /\n/, $form->{partsgroup} ) {
  881. if ( $item =~ /$form->{action}/ ) {
  882. ( $partsgroup, $translation ) = split /--/, $item;
  883. $form->{action} = $partsgroup;
  884. last;
  885. }
  886. }
  887. }
  888. $form->{"partsgroup_$form->{rowcount}"} = $form->{action};
  889. &update;
  890. }
  891. sub print_options {
  892. $form->{PD}{ $form->{type} } = "checked";
  893. print qq|
  894. <input type=hidden name=format value=$form->{format}>
  895. <input type=hidden name=formname value=$form->{type}>
  896. <table width=100%>
  897. <tr>
  898. |;
  899. $media = qq|
  900. <td><input class=radio type=radio name=media value="screen"></td>
  901. <td>| . $locale->text('Screen') . qq|</td>|;
  902. if ( %{LedgerSMB::Sysconfig::printer} ) {
  903. for ( keys %{LedgerSMB::Sysconfig::printer} ) {
  904. $media .= qq|
  905. <td><input class=radio type=radio name=media value="$_"></td>
  906. <td nowrap>$_</td>
  907. |;
  908. }
  909. }
  910. $media =~ s/(value="\Q$form->{media}\E")/$1 checked/;
  911. print qq|
  912. $media
  913. <td width=99%>&nbsp;</td>|;
  914. if ( $form->{printed} =~ /$form->{type}/ ) {
  915. print qq|
  916. <th>\|| . $locale->text('Printed') . qq|\|</th>|;
  917. }
  918. print qq|
  919. </tr>
  920. </table>
  921. |;
  922. }
  923. sub receipts {
  924. $form->{title} = $locale->text('Receipts');
  925. $form->{db} = 'ar';
  926. RP->paymentaccounts( \%myconfig, \%$form );
  927. $paymentaccounts = "";
  928. for ( @{ $form->{PR} } ) { $paymentaccounts .= "$_->{accno} "; }
  929. if ( @{ $form->{all_years} } ) {
  930. # accounting years
  931. $form->{selectaccountingyear} = "<option>\n";
  932. for ( @{ $form->{all_years} } ) {
  933. $form->{selectaccountingyear} .= qq|<option>$_\n|;
  934. }
  935. $form->{selectaccountingmonth} = "<option>\n";
  936. for ( sort keys %{ $form->{all_month} } ) {
  937. $form->{selectaccountingmonth} .=
  938. qq|<option value=$_>|
  939. . $locale->text( $form->{all_month}{$_} ) . qq|\n|;
  940. }
  941. $selectfrom = qq|
  942. <tr>
  943. <th align=right>| . $locale->text('Period') . qq|</th>
  944. <td colspan=3>
  945. <select name=month>$form->{selectaccountingmonth}</select>
  946. <select name=year>$form->{selectaccountingyear}</select>
  947. <input name=interval class=radio type=radio value=0 checked>&nbsp;|
  948. . $locale->text('Current') . qq|
  949. <input name=interval class=radio type=radio value=1>&nbsp;|
  950. . $locale->text('Month') . qq|
  951. <input name=interval class=radio type=radio value=3>&nbsp;|
  952. . $locale->text('Quarter') . qq|
  953. <input name=interval class=radio type=radio value=12>&nbsp;|
  954. . $locale->text('Year') . qq|
  955. </td>
  956. </tr>
  957. |;
  958. }
  959. $form->header;
  960. print qq|
  961. <body>
  962. <form method=post action=$form->{script}>
  963. <input type=hidden name=title value="$form->{title}">
  964. <input type=hidden name=paymentaccounts value="$paymentaccounts">
  965. <input type=hidden name=till value=1>
  966. <input type=hidden name=subtotal value=1>
  967. <table width=100%>
  968. <tr>
  969. <th class=listtop>$form->{title}</th>
  970. </tr>
  971. <tr height="5"></tr>
  972. <tr>
  973. <td>
  974. <table>
  975. <input type=hidden name=nextsub value=list_payments>
  976. <tr>
  977. <th align=right>| . $locale->text('From') . qq|</th>
  978. <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
  979. <th align=right>| . $locale->text('To') . qq|</th>
  980. <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
  981. </tr>
  982. $selectfrom
  983. <input type=hidden name=sort value=transdate>
  984. <input type=hidden name=db value=$form->{db}>
  985. </table>
  986. </td>
  987. </tr>
  988. <tr>
  989. <td><hr size=3 noshade></td>
  990. </tr>
  991. </table>
  992. <br>
  993. <input type="hidden" name="path" value="$form->{path}">
  994. <input type="hidden" name="login" value="$form->{login}">
  995. <input type="hidden" name="sessionid" value="$form->{sessionid}">
  996. <button type="submit" class="submit" name="action" value="continue">|
  997. . $locale->text('Continue')
  998. . qq|</button>
  999. |;
  1000. if ( $form->{lynx} ) {
  1001. require "bin/menu.pl";
  1002. &menubar;
  1003. }
  1004. print qq|
  1005. </form>
  1006. </body>
  1007. </html>
  1008. |;
  1009. }