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