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