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