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