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