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