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