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