summaryrefslogtreecommitdiff
path: root/bin/pos.pl
blob: 58d98ac1ef4fb6ac30c18f735b01082a38f69501 (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. $form->{invtotal} = $form->{invsubtotal};
  338. $form->{taxincluded} = ( $form->{taxincluded} ) ? "checked" : "";
  339. $taxincluded = "";
  340. if ( $form->{taxaccounts} ) {
  341. $taxincluded = qq|
  342. <tr height="5"></tr>
  343. <tr>
  344. <td align=right>
  345. <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td><th align=left>|
  346. . $locale->text('Tax Included')
  347. . qq|</th>
  348. </tr>
  349. |;
  350. }
  351. if ( !$form->{taxincluded} ) {
  352. my @taxes = Tax::init_taxes( $form, $form->{taxaccounts} );
  353. $form->{invtotal} +=
  354. Tax::calculate_taxes( \@taxes, $form, $form->{invsubtotal}, 0 );
  355. foreach my $item (@taxes) {
  356. my $taccno = $item->account;
  357. $form->{"${taccno}_total"} =
  358. $form->format_amount( \%myconfig, $item->value, 2, 0 );
  359. $tax .= qq|
  360. <tr>
  361. <th align=right>$form->{"${taccno}_description"}</th>
  362. <td align=right>$form->{"${taccno}_total"}</td>
  363. </tr>
  364. | if $item->value;
  365. }
  366. $form->{invsubtotal} =
  367. $form->format_amount( \%myconfig, $form->{invsubtotal}, 2, 0 );
  368. $subtotal = qq|
  369. <tr>
  370. <th align=right>| . $locale->text('Subtotal') . qq|</th>
  371. <td align=right>$form->{invsubtotal}</td>
  372. </tr>
  373. |;
  374. }
  375. on_update() if ($form->{action} eq 'update');
  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{"coa_prefix"} ) {
  433. if ( !$form->{"AR_paid_$i"} ) {
  434. $form->{"AR_paid_$i"} =
  435. $pos_config{"coa_prefix"} . '.' . $pos_config{"till"};
  436. }
  437. $column_data{AR_paid} = qq|<input type=hidden name="AR_paid_$i"
  438. value='$form->{"AR_paid_$i"}'>|;
  439. }
  440. else {
  441. $column_data{AR_paid} =
  442. qq|<td><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
  443. }
  444. print qq|
  445. <tr>
  446. |;
  447. for (@column_index) { print "$column_data{$_}\n"; }
  448. print qq|
  449. </tr>
  450. |;
  451. $form->hide_form( "cleared_$i", "exchangerate_$i", "forex_$i" );
  452. }
  453. $form->{change} = 0;
  454. if ( $totalpaid > $form->{invtotal} ) {
  455. $form->{change} = $totalpaid - $form->{invtotal};
  456. }
  457. $form->{oldchange} = $form->{change};
  458. $form->{change} = $form->format_amount( \%myconfig, $form->{change}, 2, 0 );
  459. $form->{totalpaid} = $form->format_amount( \%myconfig, $totalpaid, 2 );
  460. $form->{oldinvtotal} = $form->{invtotal};
  461. $form->{invtotal} =
  462. $form->format_amount( \%myconfig, $form->{invtotal}, 2, 0 );
  463. print qq|
  464. <tr>
  465. <th align=right>| . $locale->text('Change') . qq|</th>
  466. <th>$form->{change}</th>
  467. </tr>
  468. </table>
  469. </td>
  470. <td align=right>
  471. <table>
  472. $subtotal
  473. $tax
  474. <tr>
  475. <th align=right>| . $locale->text('Total') . qq|</th>
  476. <td align=right>$form->{invtotal}</td>
  477. </tr>
  478. $taxincluded
  479. </table>
  480. </td>
  481. </tr>
  482. </table>
  483. </td>
  484. </tr>
  485. <input type=hidden name=oldtotalpaid value=$totalpaid>
  486. <input type=hidden name=datepaid value=$form->{transdate}>
  487. <tr>
  488. <td>
  489. |;
  490. $form->hide_form(
  491. qw(paidaccounts selectAR_paid oldinvtotal change oldchange invtotal));
  492. &print_options;
  493. print qq|
  494. </td>
  495. </tr>
  496. <tr>
  497. <td><hr size=3 noshade></td>
  498. </tr>
  499. </table>
  500. |;
  501. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  502. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  503. # type=submit $locale->text('Update')
  504. # type=submit $locale->text('Print')
  505. # type=submit $locale->text('Post')
  506. # type=submit $locale->text('Print and Post')
  507. # type=submit $locale->text('Delete')
  508. if ( !$form->{readonly} ) {
  509. %button = (
  510. 'update' =>
  511. { ndx => 1, key => 'U', value => $locale->text('Update') },
  512. 'print' =>
  513. { ndx => 2, key => 'P', value => $locale->text('Print') },
  514. 'post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  515. 'print_and_post' => {
  516. ndx => 4,
  517. key => 'R',
  518. value => $locale->text('Print and Post')
  519. },
  520. 'delete' =>
  521. { ndx => 5, key => 'D', value => $locale->text('Delete') },
  522. );
  523. if ( $transdate > $closedto ) {
  524. if ( !$form->{id} ) {
  525. delete $button{'delete'};
  526. }
  527. delete $button{'print_and_post'}
  528. unless ${LedgerSMB::Sysconfig::latex};
  529. }
  530. else {
  531. for ( 'print', 'post', 'print_and_post', 'delete' ) {
  532. delete $button{$_};
  533. }
  534. }
  535. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
  536. {
  537. $form->print_button( \%button, $_ );
  538. }
  539. if ( $form->{partsgroup} ) {
  540. $form->{partsgroup} =~ s/\r//g;
  541. $form->{partsgroup} = $form->quote( $form->{partsgroup} );
  542. $spc = ( $form->{path} =~ /lynx/ ) ? "." : " ";
  543. print qq|
  544. <input type=hidden name=nextsub value=lookup_partsgroup>
  545. <input type=hidden name=partsgroup value="$form->{partsgroup}">|;
  546. foreach $item ( split /\n/, $form->{partsgroup} ) {
  547. ( $partsgroup, $translation ) = split /--/, $item;
  548. $item = ($translation) ? $translation : $partsgroup;
  549. print
  550. qq| <button class="submit" type="submit" name="action" value="$spc$item">$spc$item</button>\n|
  551. if $item;
  552. }
  553. }
  554. }
  555. if ( $form->{lynx} ) {
  556. require "bin/menu.pl";
  557. &menubar;
  558. }
  559. $form->hide_form(qw(rowcount callback path login sessionid));
  560. print qq|</form>|;
  561. if ( !$form->{dontdisplayend} ) {
  562. print qq|
  563. </body>
  564. </html>
  565. |;
  566. }
  567. }
  568. sub post {
  569. $form->{media} = 'Printer';
  570. $form->isblank( "customer", $locale->text('Customer missing!') );
  571. # if oldcustomer ne customer redo form
  572. $customer = $form->{customer};
  573. $customer =~ s/--.*//g;
  574. $customer .= "--$form->{customer_id}";
  575. if ( $customer ne $form->{oldcustomer} ) {
  576. &update;
  577. exit;
  578. }
  579. &validate_items;
  580. &print;
  581. $form->isblank( "exchangerate", $locale->text('Exchange rate missing!') )
  582. if ( $form->{currency} ne $form->{defaultcurrency} );
  583. $paid = 0;
  584. for ( 1 .. $form->{paidaccounts} ) {
  585. $paid += $form->parse_amount( \%myconfig, $form->{"paid_$_"} );
  586. }
  587. delete $form->{datepaid} unless $paid;
  588. $total = $form->parse_amount( \%myconfig, $form->{invtotal} );
  589. # deduct change from first payment
  590. #$form->{"paid_1"} = $form->{invtotal} if $paid > $total;
  591. $form->{paid} = $paid;
  592. if ( $paid > $total ) {
  593. ++$form->{paidaccounts};
  594. my $pa = $form->{paidaccounts};
  595. $form->{"paid_$pa"} = $total - $paid;
  596. $form->{"source_$pa"} = 'cash';
  597. $form->{"exchangerate_$pa"} = 0;
  598. $form->{"AR_paid_$pa"} = $form->{AR_paid_1};
  599. }
  600. ( $form->{AR} ) = split /--/, $form->{AR};
  601. if ( IS->post_invoice( \%myconfig, \%$form ) ) {
  602. $form->redirect( $locale->text('Posted!') );
  603. }
  604. else {
  605. $form->error( $locale->text('Cannot post transaction!') );
  606. }
  607. }
  608. sub display_row {
  609. my $numrows = shift;
  610. @column_index =
  611. qw(partnumber description partsgroup qty unit sellprice discount linetotal);
  612. $form->{invsubtotal} = 0;
  613. for ( split / /, $form->{taxaccounts} ) { $form->{"${_}_base"} = 0; }
  614. $column_data{partnumber} =
  615. qq|<th class=listheading nowrap>| . $locale->text('Number') . qq|</th>|;
  616. $column_data{description} =
  617. qq|<th class=listheading nowrap>|
  618. . $locale->text('Description')
  619. . qq|</th>|;
  620. $column_data{qty} =
  621. qq|<th class=listheading nowrap>| . $locale->text('Qty') . qq|</th>|;
  622. $column_data{unit} =
  623. qq|<th class=listheading nowrap>| . $locale->text('Unit') . qq|</th>|;
  624. $column_data{sellprice} =
  625. qq|<th class=listheading nowrap>| . $locale->text('Price') . qq|</th>|;
  626. $column_data{linetotal} =
  627. qq|<th class=listheading nowrap>| . $locale->text('Extended') . qq|</th>|;
  628. $column_data{discount} = qq|<th class=listheading nowrap>%</th>|;
  629. print qq|
  630. <tr>
  631. <td>
  632. <table width=100%>
  633. <tr class=listheading>|;
  634. for (@column_index) { print "\n$column_data{$_}"; }
  635. print qq|
  636. </tr>
  637. |;
  638. $exchangerate = $form->parse_amount( \%myconfig, $form->{exchangerate} );
  639. $exchangerate = ($exchangerate) ? $exchangerate : 1;
  640. for $i ( 1 .. $numrows ) {
  641. # undo formatting
  642. for (qw(qty discount sellprice)) {
  643. $form->{"${_}_$i"} =
  644. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  645. }
  646. ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  647. $dec = length $dec;
  648. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  649. if ( ( $form->{"qty_$i"} != $form->{"oldqty_$i"} )
  650. || ( $form->{currency} ne $form->{oldcurrency} ) )
  651. {
  652. # check for a pricematrix
  653. @a = split / /, $form->{"pricematrix_$i"};
  654. if ( scalar @a ) {
  655. foreach $item (@a) {
  656. ( $q, $p ) = split /:/, $item;
  657. if ( ( $p * 1 ) && ( $form->{"qty_$i"} >= ( $q * 1 ) ) ) {
  658. ($dec) = ( $p =~ /\.(\d+)/ );
  659. $dec = length $dec;
  660. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  661. $form->{"sellprice_$i"} =
  662. $form->round_amount( $p / $exchangerate,
  663. $decimalplaces );
  664. }
  665. }
  666. }
  667. }
  668. if ( $i < $numrows ) {
  669. $column_data{discount} =
  670. qq|<td align=right><input name="discount_$i" size=3 value=|
  671. . $form->format_amount( \%myconfig, $form->{"discount_$i"} )
  672. . qq|></td>|;
  673. }
  674. else {
  675. $column_data{discount} = qq|<td></td>|;
  676. }
  677. $discount =
  678. $form->round_amount(
  679. $form->{"sellprice_$i"} * $form->{"discount_$i"} / 100,
  680. $decimalplaces );
  681. $linetotal = $form->round_amount( $form->{"sellprice_$i"} - $discount,
  682. $decimalplaces );
  683. $linetotal = $form->round_amount( $linetotal * $form->{"qty_$i"}, 2 );
  684. for (qw(partnumber sku description partsgroup unit)) {
  685. $form->{"${_}_$i"} = $form->quote( $form->{"${_}_$i"} );
  686. }
  687. $column_data{partnumber} =
  688. qq|<td><input name="partnumber_$i" size=20 value="$form->{"partnumber_$i"}" accesskey="$i" title="[Alt-$i]"></td>|;
  689. if (
  690. ( $rows = $form->numtextrows( $form->{"description_$i"}, 40, 6 ) ) >
  691. 1 )
  692. {
  693. $column_data{description} =
  694. qq|<td><textarea name="description_$i" rows=$rows cols=46 wrap=soft>$form->{"description_$i"}</textarea></td>|;
  695. }
  696. else {
  697. $column_data{description} =
  698. qq|<td><input name="description_$i" size=48 value="$form->{"description_$i"}"></td>|;
  699. }
  700. $column_data{qty} =
  701. qq|<td align=right><input name="qty_$i" size=5 value=|
  702. . $form->format_amount( \%myconfig, $form->{"qty_$i"} )
  703. . qq|></td>|;
  704. $column_data{unit} = qq|<td>$form->{"unit_$i"}</td>|;
  705. $column_data{sellprice} =
  706. qq|<td align=right><input name="sellprice_$i" size=9 value=|
  707. . $form->format_amount( \%myconfig, $form->{"sellprice_$i"},
  708. $decimalplaces )
  709. . qq|></td>|;
  710. $column_data{linetotal} =
  711. qq|<td align=right>|
  712. . $form->format_amount( \%myconfig, $linetotal, 2 )
  713. . qq|</td>|;
  714. print qq|
  715. <tr valign=top>|;
  716. for (@column_index) { print "\n$column_data{$_}"; }
  717. print qq|
  718. </tr>
  719. |;
  720. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  721. for (
  722. qw(id listprice lastcost taxaccounts pricematrix oldqty sku partsgroup unit inventory_accno_id income_accno_id expense_accno_id)
  723. )
  724. {
  725. $form->hide_form("${_}_$i");
  726. }
  727. for ( split / /, $form->{"taxaccounts_$i"} ) {
  728. $form->{"${_}_base"} += $linetotal;
  729. }
  730. $form->{invsubtotal} += $linetotal;
  731. }
  732. print qq|
  733. </table>
  734. </td>
  735. </tr>
  736. <input type=hidden name=oldcurrency value=$form->{currency}>
  737. |;
  738. }
  739. sub print {
  740. if ( !$form->{invnumber} ) {
  741. $form->{invnumber} = $form->update_defaults( \%myconfig, 'sinumber' );
  742. }
  743. $rc = $form->{'rowcount'};
  744. $pc = $form->{'paidaccounts'};
  745. if ( $form->{"partnumber_$rc"}
  746. || $form->{"description_$rc"}
  747. || $form->{"paid_$pc"} )
  748. {
  749. &update;
  750. exit;
  751. }
  752. for $i ( 1 .. $rc - 1 ) {
  753. if ( $form->{"qty_$i"} != $form->{"oldqty_$i"} ) {
  754. &update;
  755. exit;
  756. }
  757. }
  758. if ( !$form->{invnumber} ) {
  759. $form->{invnumber} = $form->update_defaults( \%myconfig, "sinumber" );
  760. if ( $form->{media} eq 'screen' ) {
  761. &update;
  762. exit;
  763. }
  764. }
  765. $old_form = new Form;
  766. for ( keys %$form ) { $old_form->{$_} = $form->{$_}; }
  767. for (qw(employee department)) { $form->{$_} =~ s/--.*//g; }
  768. $form->{invdate} = $form->{transdate};
  769. my @lt = localtime();
  770. $form->{dateprinted} = $lt[2] . ":" . $lt[1] . ":" . $lt[0];
  771. &print_form($old_form);
  772. }
  773. sub print_form {
  774. my $old_form = shift;
  775. &open_drawer;
  776. # if oldcustomer ne customer redo form
  777. $customer = $form->{customer};
  778. $customer =~ s/--.*//g;
  779. $customer .= "--$form->{customer_id}";
  780. if ( $customer ne $form->{oldcustomer} ) {
  781. &update;
  782. exit;
  783. }
  784. &validate_items;
  785. &{"$form->{vc}_details"};
  786. @a = ();
  787. for ( 1 .. $form->{rowcount} ) {
  788. push @a, ( "partnumber_$_", "description_$_" );
  789. }
  790. for ( split / /, $form->{taxaccounts} ) { push @a, "${_}_description"; }
  791. $form->format_string(@a);
  792. # format payment dates
  793. for ( 1 .. $form->{paidaccounts} ) {
  794. $form->{"datepaid_$_"} =
  795. $locale->date( \%myconfig, $form->{"datepaid_$_"} );
  796. }
  797. IS->invoice_details( \%myconfig, \%$form );
  798. if ( $form->parse_amount( \%myconfig, $form->{total} ) <= 0 ) {
  799. $form->{total} = 0;
  800. }
  801. else {
  802. $form->{change} = 0;
  803. }
  804. for (qw(company address tel fax businessnumber)) {
  805. $form->{$_} = $myconfig{$_};
  806. }
  807. $form->{username} = $myconfig{name};
  808. $form->{address} =~ s/\\n/\n/g;
  809. push @a, qw(company address tel fax businessnumber username);
  810. $form->format_string(@a);
  811. $form->{templates} = "$myconfig{templates}";
  812. $form->{IN} = "$form->{type}.$form->{format}";
  813. if ( $form->{format} =~ /(postscript|pdf)/ ) {
  814. $form->{IN} =~ s/$&$/tex/;
  815. }
  816. if ( $form->{media} ne 'screen' ) {
  817. $form->{OUT} = ${LedgerSMB::Sysconfig::printer}{ $form->{media} };
  818. $form->{printmode} = '|-';
  819. }
  820. $form->{discount} =
  821. $form->format_amount( \%myconfig, $form->{discount} * 100 );
  822. $form->{rowcount}--;
  823. $form->{pre} = "<body bgcolor=#ffffff>\n<pre>";
  824. delete $form->{stylesheet};
  825. $form->{cd_open} = $pos_config{rp_cash_drawer_open};
  826. $form->parse_template( \%myconfig, ${LedgerSMB::Sysconfig::userspath} );
  827. if ( $form->{printed} !~ /$form->{formname}/ ) {
  828. $form->{printed} .= " $form->{formname}";
  829. $form->{printed} =~ s/^ //;
  830. $form->update_status( \%myconfig );
  831. }
  832. $old_form->{printed} = $form->{printed};
  833. # if we got back here restore the previous form
  834. if ( $form->{media} ne 'screen' ) {
  835. # restore and display form
  836. for ( keys %$old_form ) { $form->{$_} = $old_form->{$_}; }
  837. $form->{exchangerate} =
  838. $form->parse_amount( \%myconfig, $form->{exchangerate} );
  839. for $i ( 1 .. $form->{paidaccounts} ) {
  840. for (qw(paid exchangerate)) {
  841. $form->{"${_}_$i"} =
  842. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  843. }
  844. }
  845. delete $form->{pre};
  846. if ( !$form->{printandpost} ) {
  847. $form->{rowcount}--;
  848. #&display_form;
  849. }
  850. }
  851. }
  852. sub print_and_post {
  853. $form->error( $locale->text('Select a Printer!') )
  854. if ( $form->{media} eq 'screen' );
  855. $form->{printandpost} = 1;
  856. &print;
  857. &post;
  858. }
  859. sub lookup_partsgroup {
  860. $form->{my_partsgroup} =~ s/\r//;
  861. $form->{action} = $form->{my_partsgroup};
  862. if ( $form->{language_code} ) {
  863. # get english
  864. foreach $item ( split /\n/, $form->{partsgroup} ) {
  865. if ( $item =~ /$form->{action}/ ) {
  866. ( $partsgroup, $translation ) = split /--/, $item;
  867. $form->{action} = $partsgroup;
  868. last;
  869. }
  870. }
  871. }
  872. $form->{"partsgroup_$form->{rowcount}"} = $form->{action};
  873. &update;
  874. }
  875. sub print_options {
  876. $form->{PD}{ $form->{type} } = "checked";
  877. print qq|
  878. <input type=hidden name=format value=$form->{format}>
  879. <input type=hidden name=formname value=$form->{type}>
  880. <table width=100%>
  881. <tr>
  882. |;
  883. $media = qq|
  884. <td><input class=radio type=radio name=media value="screen"></td>
  885. <td>| . $locale->text('Screen') . qq|</td>|;
  886. if ( %{LedgerSMB::Sysconfig::printer} ) {
  887. for ( keys %{LedgerSMB::Sysconfig::printer} ) {
  888. $media .= qq|
  889. <td><input class=radio type=radio name=media value="$_"></td>
  890. <td nowrap>$_</td>
  891. |;
  892. }
  893. }
  894. $media =~ s/(value="\Q$form->{media}\E")/$1 checked/;
  895. print qq|
  896. $media
  897. <td width=99%>&nbsp;</td>|;
  898. if ( $form->{printed} =~ /$form->{type}/ ) {
  899. print qq|
  900. <th>\|| . $locale->text('Printed') . qq|\|</th>|;
  901. }
  902. print qq|
  903. </tr>
  904. </table>
  905. |;
  906. }
  907. sub receipts {
  908. $form->{title} = $locale->text('Receipts');
  909. $form->{db} = 'ar';
  910. RP->paymentaccounts( \%myconfig, \%$form );
  911. $paymentaccounts = "";
  912. for ( @{ $form->{PR} } ) { $paymentaccounts .= "$_->{accno} "; }
  913. if ( @{ $form->{all_years} } ) {
  914. # accounting years
  915. $form->{selectaccountingyear} = "<option>\n";
  916. for ( @{ $form->{all_years} } ) {
  917. $form->{selectaccountingyear} .= qq|<option>$_\n|;
  918. }
  919. $form->{selectaccountingmonth} = "<option>\n";
  920. for ( sort keys %{ $form->{all_month} } ) {
  921. $form->{selectaccountingmonth} .=
  922. qq|<option value=$_>|
  923. . $locale->text( $form->{all_month}{$_} ) . qq|\n|;
  924. }
  925. $selectfrom = qq|
  926. <tr>
  927. <th align=right>| . $locale->text('Period') . qq|</th>
  928. <td colspan=3>
  929. <select name=month>$form->{selectaccountingmonth}</select>
  930. <select name=year>$form->{selectaccountingyear}</select>
  931. <input name=interval class=radio type=radio value=0 checked>&nbsp;|
  932. . $locale->text('Current') . qq|
  933. <input name=interval class=radio type=radio value=1>&nbsp;|
  934. . $locale->text('Month') . qq|
  935. <input name=interval class=radio type=radio value=3>&nbsp;|
  936. . $locale->text('Quarter') . qq|
  937. <input name=interval class=radio type=radio value=12>&nbsp;|
  938. . $locale->text('Year') . qq|
  939. </td>
  940. </tr>
  941. |;
  942. }
  943. $form->header;
  944. print qq|
  945. <body>
  946. <form method=post action=$form->{script}>
  947. <input type=hidden name=title value="$form->{title}">
  948. <input type=hidden name=paymentaccounts value="$paymentaccounts">
  949. <input type=hidden name=till value=1>
  950. <input type=hidden name=subtotal value=1>
  951. <table width=100%>
  952. <tr>
  953. <th class=listtop>$form->{title}</th>
  954. </tr>
  955. <tr height="5"></tr>
  956. <tr>
  957. <td>
  958. <table>
  959. <input type=hidden name=nextsub value=list_payments>
  960. <tr>
  961. <th align=right>| . $locale->text('From') . qq|</th>
  962. <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
  963. <th align=right>| . $locale->text('To') . qq|</th>
  964. <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
  965. </tr>
  966. $selectfrom
  967. <input type=hidden name=sort value=transdate>
  968. <input type=hidden name=db value=$form->{db}>
  969. </table>
  970. </td>
  971. </tr>
  972. <tr>
  973. <td><hr size=3 noshade></td>
  974. </tr>
  975. </table>
  976. <br>
  977. <input type="hidden" name="path" value="$form->{path}">
  978. <input type="hidden" name="login" value="$form->{login}">
  979. <input type="hidden" name="sessionid" value="$form->{sessionid}">
  980. <button type="submit" class="submit" name="action" value="continue">|
  981. . $locale->text('Continue')
  982. . qq|</button>
  983. |;
  984. if ( $form->{lynx} ) {
  985. require "bin/menu.pl";
  986. &menubar;
  987. }
  988. print qq|
  989. </form>
  990. </body>
  991. </html>
  992. |;
  993. }