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