summaryrefslogtreecommitdiff
path: root/bin/pos.pl
blob: 90a12e58c6d592676e9e3d668eb16fbb5f485d71 (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{"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. my $template = LedgerSMB::Template->new(user => \%myconfig,
  827. template => $form->{'formname'}, format => uc $form->{format} );
  828. try {
  829. $template->render($form);
  830. $template->output(%{$form});
  831. }
  832. catch Error::Simple with {
  833. my $E = shift;
  834. $form->error( $E->stacktrace );
  835. };
  836. if ( $form->{printed} !~ /$form->{formname}/ ) {
  837. $form->{printed} .= " $form->{formname}";
  838. $form->{printed} =~ s/^ //;
  839. $form->update_status( \%myconfig );
  840. }
  841. $old_form->{printed} = $form->{printed};
  842. # if we got back here restore the previous form
  843. if ( $form->{media} ne 'screen' ) {
  844. # restore and display form
  845. for ( keys %$old_form ) { $form->{$_} = $old_form->{$_}; }
  846. $form->{exchangerate} =
  847. $form->parse_amount( \%myconfig, $form->{exchangerate} );
  848. for $i ( 1 .. $form->{paidaccounts} ) {
  849. for (qw(paid exchangerate)) {
  850. $form->{"${_}_$i"} =
  851. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  852. }
  853. }
  854. delete $form->{pre};
  855. if ( !$form->{printandpost} ) {
  856. $form->{rowcount}--;
  857. #&display_form;
  858. }
  859. }
  860. }
  861. sub print_and_post {
  862. $form->error( $locale->text('Select a Printer!') )
  863. if ( $form->{media} eq 'screen' );
  864. $form->{printandpost} = 1;
  865. &print;
  866. &post;
  867. }
  868. sub lookup_partsgroup {
  869. $form->{my_partsgroup} =~ s/\r//;
  870. $form->{action} = $form->{my_partsgroup};
  871. if ( $form->{language_code} ) {
  872. # get english
  873. foreach $item ( split /\n/, $form->{partsgroup} ) {
  874. if ( $item =~ /$form->{action}/ ) {
  875. ( $partsgroup, $translation ) = split /--/, $item;
  876. $form->{action} = $partsgroup;
  877. last;
  878. }
  879. }
  880. }
  881. $form->{"partsgroup_$form->{rowcount}"} = $form->{action};
  882. &update;
  883. }
  884. sub print_options {
  885. $form->{PD}{ $form->{type} } = "checked";
  886. print qq|
  887. <input type=hidden name=format value=$form->{format}>
  888. <input type=hidden name=formname value=$form->{type}>
  889. <table width=100%>
  890. <tr>
  891. |;
  892. $media = qq|
  893. <td><input class=radio type=radio name=media value="screen"></td>
  894. <td>| . $locale->text('Screen') . qq|</td>|;
  895. if ( %{LedgerSMB::Sysconfig::printer} ) {
  896. for ( keys %{LedgerSMB::Sysconfig::printer} ) {
  897. $media .= qq|
  898. <td><input class=radio type=radio name=media value="$_"></td>
  899. <td nowrap>$_</td>
  900. |;
  901. }
  902. }
  903. $media =~ s/(value="\Q$form->{media}\E")/$1 checked/;
  904. print qq|
  905. $media
  906. <td width=99%>&nbsp;</td>|;
  907. if ( $form->{printed} =~ /$form->{type}/ ) {
  908. print qq|
  909. <th>\|| . $locale->text('Printed') . qq|\|</th>|;
  910. }
  911. print qq|
  912. </tr>
  913. </table>
  914. |;
  915. }
  916. sub receipts {
  917. $form->{title} = $locale->text('Receipts');
  918. $form->{db} = 'ar';
  919. RP->paymentaccounts( \%myconfig, \%$form );
  920. $paymentaccounts = "";
  921. for ( @{ $form->{PR} } ) { $paymentaccounts .= "$_->{accno} "; }
  922. if ( @{ $form->{all_years} } ) {
  923. # accounting years
  924. $form->{selectaccountingyear} = "<option>\n";
  925. for ( @{ $form->{all_years} } ) {
  926. $form->{selectaccountingyear} .= qq|<option>$_\n|;
  927. }
  928. $form->{selectaccountingmonth} = "<option>\n";
  929. for ( sort keys %{ $form->{all_month} } ) {
  930. $form->{selectaccountingmonth} .=
  931. qq|<option value=$_>|
  932. . $locale->text( $form->{all_month}{$_} ) . qq|\n|;
  933. }
  934. $selectfrom = qq|
  935. <tr>
  936. <th align=right>| . $locale->text('Period') . qq|</th>
  937. <td colspan=3>
  938. <select name=month>$form->{selectaccountingmonth}</select>
  939. <select name=year>$form->{selectaccountingyear}</select>
  940. <input name=interval class=radio type=radio value=0 checked>&nbsp;|
  941. . $locale->text('Current') . qq|
  942. <input name=interval class=radio type=radio value=1>&nbsp;|
  943. . $locale->text('Month') . qq|
  944. <input name=interval class=radio type=radio value=3>&nbsp;|
  945. . $locale->text('Quarter') . qq|
  946. <input name=interval class=radio type=radio value=12>&nbsp;|
  947. . $locale->text('Year') . qq|
  948. </td>
  949. </tr>
  950. |;
  951. }
  952. $form->header;
  953. print qq|
  954. <body>
  955. <form method=post action=$form->{script}>
  956. <input type=hidden name=title value="$form->{title}">
  957. <input type=hidden name=paymentaccounts value="$paymentaccounts">
  958. <input type=hidden name=till value=1>
  959. <input type=hidden name=subtotal value=1>
  960. <table width=100%>
  961. <tr>
  962. <th class=listtop>$form->{title}</th>
  963. </tr>
  964. <tr height="5"></tr>
  965. <tr>
  966. <td>
  967. <table>
  968. <input type=hidden name=nextsub value=list_payments>
  969. <tr>
  970. <th align=right>| . $locale->text('From') . qq|</th>
  971. <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
  972. <th align=right>| . $locale->text('To') . qq|</th>
  973. <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
  974. </tr>
  975. $selectfrom
  976. <input type=hidden name=sort value=transdate>
  977. <input type=hidden name=db value=$form->{db}>
  978. </table>
  979. </td>
  980. </tr>
  981. <tr>
  982. <td><hr size=3 noshade></td>
  983. </tr>
  984. </table>
  985. <br>
  986. <input type="hidden" name="path" value="$form->{path}">
  987. <input type="hidden" name="login" value="$form->{login}">
  988. <input type="hidden" name="sessionid" value="$form->{sessionid}">
  989. <button type="submit" class="submit" name="action" value="continue">|
  990. . $locale->text('Continue')
  991. . qq|</button>
  992. |;
  993. if ( $form->{lynx} ) {
  994. require "bin/menu.pl";
  995. &menubar;
  996. }
  997. print qq|
  998. </form>
  999. </body>
  1000. </html>
  1001. |;
  1002. }