summaryrefslogtreecommitdiff
path: root/bin/pos.pl
blob: c60ed7ae41cf6fcb797be7b960c6ac9ca9968a1d (plain)
  1. #=====================================================================
  2. # LedgerSMB Small Medium Business Accounting
  3. # http://www.ledgersmb.org/
  4. #
  5. # Copyright (C) 2006
  6. # This work contains copyrighted information from a number of sources all used
  7. # with permission.
  8. #
  9. # This file contains source code included with or based on SQL-Ledger which
  10. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  11. # under the GNU General Public License version 2 or, at your option, any later
  12. # version. For a full list including contact information of contributors,
  13. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  14. #
  15. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  16. # Copyright (c) 2003
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. # Contributors: Steve Doerr <sdoerr907@everestkc.net>
  22. #
  23. #
  24. # This program is free software; you can redistribute it and/or modify
  25. # it under the terms of the GNU General Public License as published by
  26. # the Free Software Foundation; either version 2 of the License, or
  27. # (at your option) any later version.
  28. #
  29. # This program is distributed in the hope that it will be useful,
  30. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. # GNU General Public License for more details.
  33. # You should have received a copy of the GNU General Public License
  34. # along with this program; if not, write to the Free Software
  35. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36. #=====================================================================
  37. #
  38. # POS
  39. #
  40. #=====================================================================
  41. use LedgerSMB::Tax;
  42. 1;
  43. # end
  44. sub check_alert {
  45. my $rc = $form->{'rowcount'};
  46. if ( !$form->{"partnumber_$rc"} ) {
  47. --$rc; # Ensures that alert shows up when item is selected from a list;
  48. }
  49. for ( 1 .. $rc ) {
  50. $form->{'check_id'} = ( $form->{'check_id'} || $form->{"check_id_$_"} );
  51. }
  52. }
  53. sub send_to_pd {
  54. socket( SOCK, 2, 1, getprotobynumber( $pos_config{'pd_proto'} ) );
  55. connect( SOCK, $pos_config{'pd_dest'} );
  56. my $rn = $numrows - 1;
  57. my $ds_string = sprintf(
  58. '%s%s @ $%-7.2f%s%s%s',
  59. $pd_control{'new_line'},
  60. $form->{"qty_$rn"},
  61. $form->{"sellprice_$rn"},
  62. $pd_control{'new_line'},
  63. "Subtotal: \$" . sprintf( '%-7.2f', $form->{'invtotal'} )
  64. );
  65. print SOCK $ds_string;
  66. close SOCK;
  67. }
  68. sub on_update {
  69. &send_to_pd;
  70. &check_alert;
  71. }
  72. sub open_drawer {
  73. open( PRINTER, "|-", ${LedgerSMB::Sysconfig::printer}{Printer} );
  74. print PRINTER $pos_config{'rp_cash_open'};
  75. close PRINTER;
  76. sleep 1;
  77. }
  78. sub open {
  79. &open_drawer;
  80. &update;
  81. }
  82. sub add {
  83. $form->{nextsub} = 'add';
  84. $form->{title} = $locale->text('Add POS Invoice');
  85. $form->{callback} =
  86. "$form->{script}?action=$form->{nextsub}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  87. &invoice_links;
  88. $form->{type} = "pos_invoice";
  89. $form->{format} = "txt";
  90. $form->{media} = ( $myconfig{printer} ) ? $myconfig{printer} : "screen";
  91. $form->{rowcount} = 0;
  92. $form->{readonly} = ( $myconfig{acs} =~ /POS--Sale/ ) ? 1 : 0;
  93. $ENV{REMOTE_ADDR} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
  94. $form->{till} = $4;
  95. $form->{partsgroup} = "";
  96. for ( @{ $form->{all_partsgroup} } ) {
  97. $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n";
  98. }
  99. $form->{dontdisplayend} = 1;
  100. &display_form;
  101. $form->{dontdisplayrows} = 1;
  102. $form->{dontdisplayend} = 0;
  103. &openinvoices;
  104. }
  105. sub openinvoices {
  106. undef %column_data;
  107. undef %column_heading;
  108. $form->{customer} = "";
  109. $form->{customer_id} = 0;
  110. delete $form->{l_subtotal};
  111. delete $form->{subtotal};
  112. $ENV{REMOTE_ADDR} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
  113. $form->{till} = $4;
  114. $form->{sort} = 'transdate';
  115. for (qw(open l_invnumber l_transdate l_name l_amount l_curr l_till)) {
  116. $form->{$_} = 'Y';
  117. }
  118. if ( $myconfig{role} ne 'user' ) {
  119. $form->{l_employee} = 'Y';
  120. }
  121. $form->{title} = $locale->text('Open');
  122. &transactions;
  123. }
  124. sub edit {
  125. $form->{title} = $locale->text('Edit POS Invoice');
  126. $form->{callback} =
  127. "$form->{script}?action=$form->{nextsub}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  128. &invoice_links;
  129. &prepare_invoice;
  130. $form->{type} = "pos_invoice";
  131. $form->{format} = "txt";
  132. $form->{media} = ( $myconfig{printer} ) ? $myconfig{printer} : "screen";
  133. $form->{readonly} = ( $myconfig{acs} =~ /POS--Sale/ ) ? 1 : 0;
  134. $form->{partsgroup} = "";
  135. for ( @{ $form->{all_partsgroup} } ) {
  136. $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n";
  137. }
  138. &display_form;
  139. }
  140. sub form_header {
  141. if ( !$form->{'check_id'} ) {
  142. &check_alert;
  143. }
  144. # set option selected
  145. for (qw(AR currency)) {
  146. $form->{"select$_"} =~ s/ selected//;
  147. $form->{"select$_"} =~
  148. s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
  149. }
  150. for (qw(customer department employee)) {
  151. $form->{"select$_"} = $form->unescape( $form->{"select$_"} );
  152. $form->{"select$_"} =~ s/ selected//;
  153. $form->{"select$_"} =~ s/(<option value="\Q$form->{$_}\E")/$1 selected/;
  154. }
  155. $form->{exchangerate} =
  156. $form->format_amount( \%myconfig, $form->{exchangerate} );
  157. $exchangerate = qq|<tr>|;
  158. $exchangerate .= qq|
  159. <th align=right nowrap>| . $locale->text('Currency') . qq|</th>
  160. <td><select name=currency>$form->{selectcurrency}</select></td> |
  161. if $form->{defaultcurrency};
  162. $exchangerate .= qq|
  163. <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
  164. <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
  165. |;
  166. if ( $form->{defaultcurrency}
  167. && $form->{currency} ne $form->{defaultcurrency} )
  168. {
  169. if ( $form->{forex} ) {
  170. $exchangerate .=
  171. qq|<th align=right>|
  172. . $locale->text('Exchange Rate')
  173. . qq|</th><td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>|;
  174. }
  175. else {
  176. $exchangerate .=
  177. qq|<th align=right>|
  178. . $locale->text('Exchange Rate')
  179. . qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
  180. }
  181. }
  182. $exchangerate .= qq|
  183. <input type=hidden name=forex value=$form->{forex}>
  184. </tr>
  185. |;
  186. if ( $form->{selectcustomer} ) {
  187. $customer = qq|<select name=customer>$form->{selectcustomer}</select>
  188. <input type=hidden name="selectcustomer" value="|
  189. . $form->escape( $form->{selectcustomer}, 1 ) . qq|">|;
  190. }
  191. else {
  192. $customer = qq|<input name=customer value="$form->{customer}" size=35>|;
  193. }
  194. $department = qq|
  195. <tr>
  196. <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
  197. <td colspan=3><select name=department>$form->{selectdepartment}</select>
  198. <input type=hidden name=selectdepartment value="|
  199. . $form->escape( $form->{selectdepartment}, 1 ) . qq|">
  200. </td>
  201. </tr>
  202. | if $form->{selectdepartment};
  203. $employee = qq|
  204. <tr>
  205. <th align=right nowrap>| . $locale->text('Salesperson') . qq|</th>
  206. <td colspan=3><select name=employee>$form->{selectemployee}</select></td>
  207. <input type=hidden name=selectemployee value="|
  208. . $form->escape( $form->{selectemployee}, 1 ) . qq|">
  209. </tr>
  210. | if $form->{selectemployee};
  211. if ( $form->{change} != $form->{oldchange} ) {
  212. $form->{creditremaining} -= $form->{oldchange};
  213. }
  214. $n = ( $form->{creditremaining} < 0 ) ? "0" : "1";
  215. if ( $form->{business} ) {
  216. $business = qq|
  217. <tr>
  218. <th align=right nowrap>| . $locale->text('Business') . qq|</th>
  219. <td>$form->{business}</td>
  220. <td width=10></td>
  221. <th align=right nowrap>| . $locale->text('Trade Discount') . qq|</th>
  222. <td>|
  223. . $form->format_amount( \%myconfig, $form->{tradediscount} * 100 )
  224. . qq| %</td>
  225. </tr>
  226. |;
  227. }
  228. if ( $form->{selectlanguage} ) {
  229. if ( $form->{language_code} ne $form->{oldlanguage_code} ) {
  230. # rebuild partsgroup
  231. $form->get_partsgroup(\%myconfig, { all => 1});
  232. $form->{partsgroup} = "";
  233. for ( @{ $form->{all_partsgroup} } ) {
  234. $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n";
  235. }
  236. $form->{oldlanguage_code} = $form->{language_code};
  237. }
  238. $form->{"selectlanguage"} =
  239. $form->unescape( $form->{"selectlanguage"} );
  240. $form->{"selectlanguage"} =~ s/ selected//;
  241. $form->{"selectlanguage"} =~
  242. s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
  243. $lang = qq|
  244. <tr>
  245. <th align=right>| . $locale->text('Language') . qq|</th>
  246. <td colspan=3><select name=language_code>$form->{selectlanguage}</select></td>
  247. </tr>
  248. <input type=hidden name=oldlanguage_code value=$form->{oldlanguage_code}>
  249. <input type=hidden name=selectlanguage value="|
  250. . $form->escape( $form->{selectlanguage}, 1 ) . qq|">|;
  251. }
  252. $i = $form->{rowcount} + 1;
  253. $focus = "partnumber_$i";
  254. $form->header();
  255. print qq|
  256. <body onLoad="document.forms[0].${focus}.focus()" />
  257. <form method=post action="$form->{script}">
  258. |;
  259. $form->hide_form(
  260. qw(id till type format printed title discount creditlimit creditremaining tradediscount business closedto locked oldtransdate customer_id oldcustomer check_id)
  261. );
  262. print qq|
  263. <input type=hidden name=vc value="customer">
  264. <table width=100%>
  265. <tr class=listtop>
  266. <th class=listtop>$form->{title}</font></th>
  267. </tr>|;
  268. if ( $form->{'check_id'} ) {
  269. print qq|
  270. <tr class=listtop>
  271. <th class=listtop style="background-color: red">|
  272. . $locale->text('Check ID') . qq|
  273. </th>
  274. </tr>|;
  275. }
  276. print qq|
  277. <tr height="5"></tr>
  278. <tr>
  279. <td>
  280. <table width=100%>
  281. <tr valign=top>
  282. <td>
  283. <table>
  284. <tr>
  285. <th align=right nowrap>| . $locale->text('Customer') . qq|</th>
  286. <td colspan=3>$customer</td>
  287. </tr>
  288. <tr>
  289. <td></td>
  290. <td colspan=3>
  291. <table>
  292. <tr>
  293. <th align=right nowrap>| . $locale->text('Credit Limit') . qq|</th>
  294. <td>$form->{creditlimit}</td>
  295. <td width=10></td>
  296. <th align=right nowrap>| . $locale->text('Remaining') . qq|</th>
  297. <td class="plus$n">|
  298. . $form->format_amount( \%myconfig, $form->{creditremaining}, 0, "0" )
  299. . qq|</font></td>
  300. </tr>
  301. $business
  302. </table>
  303. </td>
  304. </tr>
  305. <tr>
  306. <th align=right nowrap>| . $locale->text('Record in') . qq|</th>
  307. <td colspan=3><select name=AR>$form->{selectAR}</select></td>
  308. <input type=hidden name=selectAR value="$form->{selectAR}">
  309. </tr>
  310. $department
  311. </table>
  312. </td>
  313. <td>
  314. <table>
  315. $employee
  316. $exchangerate
  317. $lang
  318. </table>
  319. </td>
  320. </tr>
  321. </table>
  322. </td>
  323. </tr>
  324. <tr>
  325. <td>
  326. </td>
  327. </tr>
  328. |;
  329. $form->hide_form(
  330. qw(taxaccounts duedate invnumber transdate selectcurrency defaultcurrency)
  331. );
  332. for ( split / /, $form->{taxaccounts} ) {
  333. $form->hide_form( "${_}_rate", "${_}_description", "${_}_taxnumber" );
  334. }
  335. }
  336. sub form_footer {
  337. $form->{invtotal} = $form->{invsubtotal};
  338. $form->{taxincluded} = ( $form->{taxincluded} ) ? "checked" : "";
  339. $taxincluded = "";
  340. if ( $form->{taxaccounts} ) {
  341. $taxincluded = qq|
  342. <tr height="5"></tr>
  343. <tr>
  344. <td align=right>
  345. <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td><th align=left>|
  346. . $locale->text('Tax Included')
  347. . qq|</th>
  348. </tr>
  349. |;
  350. }
  351. if ( !$form->{taxincluded} ) {
  352. my @taxes = Tax::init_taxes( $form, $form->{taxaccounts} );
  353. $form->{invtotal} +=
  354. Tax::calculate_taxes( \@taxes, $form, $form->{invsubtotal}, 0 );
  355. foreach my $item (@taxes) {
  356. my $taccno = $item->account;
  357. $form->{"${taccno}_total"} =
  358. $form->format_amount( \%myconfig, $item->value, 2, 0 );
  359. $tax .= qq|
  360. <tr>
  361. <th align=right>$form->{"${taccno}_description"}</th>
  362. <td align=right>$form->{"${taccno}_total"}</td>
  363. </tr>
  364. | if $item->value;
  365. }
  366. $form->{invsubtotal} =
  367. $form->format_amount( \%myconfig, $form->{invsubtotal}, 2, 0 );
  368. $subtotal = qq|
  369. <tr>
  370. <th align=right>| . $locale->text('Subtotal') . qq|</th>
  371. <td align=right>$form->{invsubtotal}</td>
  372. </tr>
  373. |;
  374. }
  375. @column_index = qw(paid memo source cctrack AR_paid);
  376. $column_data{paid} = "<th>" . $locale->text('Amount') . "</th>";
  377. $column_data{source} = "<th>" . $locale->text('Source') . "</th>";
  378. $column_data{memo} = "<th>" . $locale->text('Memo') . "</th>";
  379. $column_data{cctrack} =
  380. "<th>" . $locale->text('Credit Card Track') . "</th>";
  381. $column_data{AR_paid} = "<th>&nbsp;</th>";
  382. print qq|
  383. <tr>
  384. <td>
  385. <table width=100%>
  386. <tr valign=top>
  387. <td>
  388. <table>
  389. <tr>
  390. |;
  391. for (@column_index) { print "$column_data{$_}\n"; }
  392. print qq|
  393. </tr>
  394. |;
  395. $totalpaid = 0;
  396. $form->{paidaccounts}++ if ( $form->{"paid_$form->{paidaccounts}"} );
  397. my $memoselect = qq|<SELECT name="MEMONAME">|;
  398. for ( sort keys %pos_sources ) {
  399. $memoselect .= qq|<option value="$_">$pos_sources{$_}</option>|;
  400. }
  401. $memoselect .= qq|</SELECT>|;
  402. for $i ( 1 .. $form->{paidaccounts} ) {
  403. $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
  404. $form->{"selectAR_paid_$i"} =~
  405. s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
  406. # format amounts
  407. $totalpaid += $form->{"paid_$i"};
  408. $form->{"paid_$i"} =
  409. $form->format_amount( \%myconfig, $form->{"paid_$i"}, 2 );
  410. $form->{"exchangerate_$i"} =
  411. $form->format_amount( \%myconfig, $form->{"exchangerate_$i"} );
  412. if ( $form->{"paid__$i"} ) {
  413. $column_data{paid} =
  414. qq|<td><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  415. }
  416. else {
  417. $column_data{paid} =
  418. qq|<td><input accesskey='n' name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  419. }
  420. $column_data{source} =
  421. qq|<td><input name="source_$i" size=10 value="$form->{"source_$i"}"></td>|;
  422. $column_data{memo} = qq|<td>$memoselect</td>|;
  423. $column_data{memo} =~ s/MEMONAME/memo_$i/;
  424. if ( !$form->{"memo_$i"} ) {
  425. $form->{"memo_$i"} = $pos_source_default;
  426. }
  427. my $memval = $form->{"memo_$i"};
  428. $column_data{memo} =~ s/(option value="$memval")/$1 SELECTED/;
  429. $column_data{cctrack} = qq|<td><input type="text" name="cctrack_$i"
  430. value="| . $form->{"cctrack_$i"} . qq|" size="3"></td>|;
  431. if ( $pos_config{"coa_prefix"} ) {
  432. if ( !$form->{"AR_paid_$i"} ) {
  433. $form->{"AR_paid_$i"} =
  434. $pos_config{"coa_prefix"} . '.' . $pos_config{"till"};
  435. }
  436. $column_data{AR_paid} = qq|<input type=hidden name="AR_paid_$i"
  437. value='$form->{"AR_paid_$i"}'>|;
  438. }
  439. else {
  440. $column_data{AR_paid} =
  441. qq|<td><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
  442. }
  443. print qq|
  444. <tr>
  445. |;
  446. for (@column_index) { print "$column_data{$_}\n"; }
  447. print qq|
  448. </tr>
  449. |;
  450. $form->hide_form( "cleared_$i", "exchangerate_$i", "forex_$i" );
  451. }
  452. $form->{change} = 0;
  453. if ( $totalpaid > $form->{invtotal} ) {
  454. $form->{change} = $totalpaid - $form->{invtotal};
  455. }
  456. $form->{oldchange} = $form->{change};
  457. $form->{change} = $form->format_amount( \%myconfig, $form->{change}, 2, 0 );
  458. $form->{totalpaid} = $form->format_amount( \%myconfig, $totalpaid, 2 );
  459. $form->{oldinvtotal} = $form->{invtotal};
  460. $form->{invtotal} =
  461. $form->format_amount( \%myconfig, $form->{invtotal}, 2, 0 );
  462. print qq|
  463. <tr>
  464. <th align=right>| . $locale->text('Change') . qq|</th>
  465. <th>$form->{change}</th>
  466. </tr>
  467. </table>
  468. </td>
  469. <td align=right>
  470. <table>
  471. $subtotal
  472. $tax
  473. <tr>
  474. <th align=right>| . $locale->text('Total') . qq|</th>
  475. <td align=right>$form->{invtotal}</td>
  476. </tr>
  477. $taxincluded
  478. </table>
  479. </td>
  480. </tr>
  481. </table>
  482. </td>
  483. </tr>
  484. <input type=hidden name=oldtotalpaid value=$totalpaid>
  485. <input type=hidden name=datepaid value=$form->{transdate}>
  486. <tr>
  487. <td>
  488. |;
  489. $form->hide_form(
  490. qw(paidaccounts selectAR_paid oldinvtotal change oldchange invtotal));
  491. &print_options;
  492. print qq|
  493. </td>
  494. </tr>
  495. <tr>
  496. <td><hr size=3 noshade></td>
  497. </tr>
  498. </table>
  499. |;
  500. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  501. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  502. # type=submit $locale->text('Update')
  503. # type=submit $locale->text('Print')
  504. # type=submit $locale->text('Post')
  505. # type=submit $locale->text('Print and Post')
  506. # type=submit $locale->text('Delete')
  507. if ( !$form->{readonly} ) {
  508. %button = (
  509. 'update' =>
  510. { ndx => 1, key => 'U', value => $locale->text('Update') },
  511. 'print' =>
  512. { ndx => 2, key => 'P', value => $locale->text('Print') },
  513. 'post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  514. 'print_and_post' => {
  515. ndx => 4,
  516. key => 'R',
  517. value => $locale->text('Print and Post')
  518. },
  519. 'delete' =>
  520. { ndx => 5, key => 'D', value => $locale->text('Delete') },
  521. );
  522. if ( $transdate > $closedto ) {
  523. if ( !$form->{id} ) {
  524. delete $button{'delete'};
  525. }
  526. delete $button{'print_and_post'}
  527. unless ${LedgerSMB::Sysconfig::latex};
  528. }
  529. else {
  530. for ( 'print', 'post', 'print_and_post', 'delete' ) {
  531. delete $button{$_};
  532. }
  533. }
  534. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
  535. {
  536. $form->print_button( \%button, $_ );
  537. }
  538. if ( $form->{partsgroup} ) {
  539. $form->{partsgroup} =~ s/\r//g;
  540. $form->{partsgroup} = $form->quote( $form->{partsgroup} );
  541. $spc = ( $form->{path} =~ /lynx/ ) ? "." : " ";
  542. print qq|
  543. <input type=hidden name=nextsub value=lookup_partsgroup>
  544. <input type=hidden name=partsgroup value="$form->{partsgroup}">|;
  545. foreach $item ( split /\n/, $form->{partsgroup} ) {
  546. ( $partsgroup, $translation ) = split /--/, $item;
  547. $item = ($translation) ? $translation : $partsgroup;
  548. print
  549. qq| <button class="submit" type="submit" name="action" value="$spc$item">$spc$item</button>\n|
  550. if $item;
  551. }
  552. }
  553. }
  554. if ( $form->{lynx} ) {
  555. require "bin/menu.pl";
  556. &menubar;
  557. }
  558. $form->hide_form(qw(rowcount callback path login sessionid));
  559. print qq|</form>|;
  560. if ( !$form->{dontdisplayend} ) {
  561. print qq|
  562. </body>
  563. </html>
  564. |;
  565. }
  566. }
  567. sub post {
  568. $form->{media} = 'Printer';
  569. $form->isblank( "customer", $locale->text('Customer missing!') );
  570. # if oldcustomer ne customer redo form
  571. $customer = $form->{customer};
  572. $customer =~ s/--.*//g;
  573. $customer .= "--$form->{customer_id}";
  574. if ( $customer ne $form->{oldcustomer} ) {
  575. &update;
  576. exit;
  577. }
  578. &validate_items;
  579. &print;
  580. $form->isblank( "exchangerate", $locale->text('Exchange rate missing!') )
  581. if ( $form->{currency} ne $form->{defaultcurrency} );
  582. $paid = 0;
  583. for ( 1 .. $form->{paidaccounts} ) {
  584. $paid += $form->parse_amount( \%myconfig, $form->{"paid_$_"} );
  585. }
  586. delete $form->{datepaid} unless $paid;
  587. $total = $form->parse_amount( \%myconfig, $form->{invtotal} );
  588. # deduct change from first payment
  589. #$form->{"paid_1"} = $form->{invtotal} if $paid > $total;
  590. $form->{paid} = $paid;
  591. if ( $paid > $total ) {
  592. ++$form->{paidaccounts};
  593. my $pa = $form->{paidaccounts};
  594. $form->{"paid_$pa"} = $total - $paid;
  595. $form->{"source_$pa"} = 'cash';
  596. $form->{"exchangerate_$pa"} = 0;
  597. $form->{"AR_paid_$pa"} = $form->{AR_paid_1};
  598. }
  599. ( $form->{AR} ) = split /--/, $form->{AR};
  600. if ( IS->post_invoice( \%myconfig, \%$form ) ) {
  601. $form->redirect( $locale->text('Posted!') );
  602. }
  603. else {
  604. $form->error( $locale->text('Cannot post transaction!') );
  605. }
  606. }
  607. sub display_row {
  608. my $numrows = shift;
  609. @column_index =
  610. qw(partnumber description partsgroup qty unit sellprice discount linetotal);
  611. $form->{invsubtotal} = 0;
  612. for ( split / /, $form->{taxaccounts} ) { $form->{"${_}_base"} = 0; }
  613. $column_data{partnumber} =
  614. qq|<th class=listheading nowrap>| . $locale->text('Number') . qq|</th>|;
  615. $column_data{description} =
  616. qq|<th class=listheading nowrap>|
  617. . $locale->text('Description')
  618. . qq|</th>|;
  619. $column_data{qty} =
  620. qq|<th class=listheading nowrap>| . $locale->text('Qty') . qq|</th>|;
  621. $column_data{unit} =
  622. qq|<th class=listheading nowrap>| . $locale->text('Unit') . qq|</th>|;
  623. $column_data{sellprice} =
  624. qq|<th class=listheading nowrap>| . $locale->text('Price') . qq|</th>|;
  625. $column_data{linetotal} =
  626. qq|<th class=listheading nowrap>| . $locale->text('Extended') . qq|</th>|;
  627. $column_data{discount} = qq|<th class=listheading nowrap>%</th>|;
  628. print qq|
  629. <tr>
  630. <td>
  631. <table width=100%>
  632. <tr class=listheading>|;
  633. for (@column_index) { print "\n$column_data{$_}"; }
  634. print qq|
  635. </tr>
  636. |;
  637. $exchangerate = $form->parse_amount( \%myconfig, $form->{exchangerate} );
  638. $exchangerate = ($exchangerate) ? $exchangerate : 1;
  639. for $i ( 1 .. $numrows ) {
  640. # undo formatting
  641. for (qw(qty discount sellprice)) {
  642. $form->{"${_}_$i"} =
  643. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  644. }
  645. ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
  646. $dec = length $dec;
  647. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  648. if ( ( $form->{"qty_$i"} != $form->{"oldqty_$i"} )
  649. || ( $form->{currency} ne $form->{oldcurrency} ) )
  650. {
  651. # check for a pricematrix
  652. @a = split / /, $form->{"pricematrix_$i"};
  653. if ( scalar @a ) {
  654. foreach $item (@a) {
  655. ( $q, $p ) = split /:/, $item;
  656. if ( ( $p * 1 ) && ( $form->{"qty_$i"} >= ( $q * 1 ) ) ) {
  657. ($dec) = ( $p =~ /\.(\d+)/ );
  658. $dec = length $dec;
  659. $decimalplaces = ( $dec > 2 ) ? $dec : 2;
  660. $form->{"sellprice_$i"} =
  661. $form->round_amount( $p / $exchangerate,
  662. $decimalplaces );
  663. }
  664. }
  665. }
  666. }
  667. if ( $i < $numrows ) {
  668. $column_data{discount} =
  669. qq|<td align=right><input name="discount_$i" size=3 value=|
  670. . $form->format_amount( \%myconfig, $form->{"discount_$i"} )
  671. . qq|></td>|;
  672. }
  673. else {
  674. $column_data{discount} = qq|<td></td>|;
  675. }
  676. $discount =
  677. $form->round_amount(
  678. $form->{"sellprice_$i"} * $form->{"discount_$i"} / 100,
  679. $decimalplaces );
  680. $linetotal = $form->round_amount( $form->{"sellprice_$i"} - $discount,
  681. $decimalplaces );
  682. $linetotal = $form->round_amount( $linetotal * $form->{"qty_$i"}, 2 );
  683. for (qw(partnumber sku description partsgroup unit)) {
  684. $form->{"${_}_$i"} = $form->quote( $form->{"${_}_$i"} );
  685. }
  686. $column_data{partnumber} =
  687. qq|<td><input name="partnumber_$i" size=20 value="$form->{"partnumber_$i"}" accesskey="$i" title="[Alt-$i]"></td>|;
  688. if (
  689. ( $rows = $form->numtextrows( $form->{"description_$i"}, 40, 6 ) ) >
  690. 1 )
  691. {
  692. $column_data{description} =
  693. qq|<td><textarea name="description_$i" rows=$rows cols=46 wrap=soft>$form->{"description_$i"}</textarea></td>|;
  694. }
  695. else {
  696. $column_data{description} =
  697. qq|<td><input name="description_$i" size=48 value="$form->{"description_$i"}"></td>|;
  698. }
  699. $column_data{qty} =
  700. qq|<td align=right><input name="qty_$i" size=5 value=|
  701. . $form->format_amount( \%myconfig, $form->{"qty_$i"} )
  702. . qq|></td>|;
  703. $column_data{unit} = qq|<td>$form->{"unit_$i"}</td>|;
  704. $column_data{sellprice} =
  705. qq|<td align=right><input name="sellprice_$i" size=9 value=|
  706. . $form->format_amount( \%myconfig, $form->{"sellprice_$i"},
  707. $decimalplaces )
  708. . qq|></td>|;
  709. $column_data{linetotal} =
  710. qq|<td align=right>|
  711. . $form->format_amount( \%myconfig, $linetotal, 2 )
  712. . qq|</td>|;
  713. print qq|
  714. <tr valign=top>|;
  715. for (@column_index) { print "\n$column_data{$_}"; }
  716. print qq|
  717. </tr>
  718. |;
  719. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  720. for (
  721. qw(id listprice lastcost taxaccounts pricematrix oldqty sku partsgroup unit inventory_accno_id income_accno_id expense_accno_id)
  722. )
  723. {
  724. $form->hide_form("${_}_$i");
  725. }
  726. for ( split / /, $form->{"taxaccounts_$i"} ) {
  727. $form->{"${_}_base"} += $linetotal;
  728. }
  729. $form->{invsubtotal} += $linetotal;
  730. }
  731. print qq|
  732. </table>
  733. </td>
  734. </tr>
  735. <input type=hidden name=oldcurrency value=$form->{currency}>
  736. |;
  737. }
  738. sub print {
  739. if ( !$form->{invnumber} ) {
  740. $form->{invnumber} = $form->update_defaults( \%myconfig, 'sinumber' );
  741. }
  742. $rc = $form->{'rowcount'};
  743. $pc = $form->{'paidaccounts'};
  744. if ( $form->{"partnumber_$rc"}
  745. || $form->{"description_$rc"}
  746. || $form->{"paid_$pc"} )
  747. {
  748. &update;
  749. exit;
  750. }
  751. for $i ( 1 .. $rc - 1 ) {
  752. if ( $form->{"qty_$i"} != $form->{"oldqty_$i"} ) {
  753. &update;
  754. exit;
  755. }
  756. }
  757. if ( !$form->{invnumber} ) {
  758. $form->{invnumber} = $form->update_defaults( \%myconfig, "sinumber" );
  759. if ( $form->{media} eq 'screen' ) {
  760. &update;
  761. exit;
  762. }
  763. }
  764. $old_form = new Form;
  765. for ( keys %$form ) { $old_form->{$_} = $form->{$_}; }
  766. for (qw(employee department)) { $form->{$_} =~ s/--.*//g; }
  767. $form->{invdate} = $form->{transdate};
  768. my @lt = localtime();
  769. $form->{dateprinted} = $lt[2] . ":" . $lt[1] . ":" . $lt[0];
  770. &print_form($old_form);
  771. }
  772. sub print_form {
  773. my $old_form = shift;
  774. &open_drawer;
  775. # if oldcustomer ne customer redo form
  776. $customer = $form->{customer};
  777. $customer =~ s/--.*//g;
  778. $customer .= "--$form->{customer_id}";
  779. if ( $customer ne $form->{oldcustomer} ) {
  780. &update;
  781. exit;
  782. }
  783. &validate_items;
  784. &{"$form->{vc}_details"};
  785. @a = ();
  786. for ( 1 .. $form->{rowcount} ) {
  787. push @a, ( "partnumber_$_", "description_$_" );
  788. }
  789. for ( split / /, $form->{taxaccounts} ) { push @a, "${_}_description"; }
  790. $form->format_string(@a);
  791. # format payment dates
  792. for ( 1 .. $form->{paidaccounts} ) {
  793. $form->{"datepaid_$_"} =
  794. $locale->date( \%myconfig, $form->{"datepaid_$_"} );
  795. }
  796. IS->invoice_details( \%myconfig, \%$form );
  797. if ( $form->parse_amount( \%myconfig, $form->{total} ) <= 0 ) {
  798. $form->{total} = 0;
  799. }
  800. else {
  801. $form->{change} = 0;
  802. }
  803. for (qw(company address tel fax businessnumber)) {
  804. $form->{$_} = $myconfig{$_};
  805. }
  806. $form->{username} = $myconfig{name};
  807. $form->{address} =~ s/\\n/\n/g;
  808. push @a, qw(company address tel fax businessnumber username);
  809. $form->format_string(@a);
  810. $form->{templates} = "$myconfig{templates}";
  811. $form->{IN} = "$form->{type}.$form->{format}";
  812. if ( $form->{format} =~ /(postscript|pdf)/ ) {
  813. $form->{IN} =~ s/$&$/tex/;
  814. }
  815. if ( $form->{media} ne 'screen' ) {
  816. $form->{OUT} = ${LedgerSMB::Sysconfig::printer}{ $form->{media} };
  817. $form->{printmode} = '|-';
  818. }
  819. $form->{discount} =
  820. $form->format_amount( \%myconfig, $form->{discount} * 100 );
  821. $form->{rowcount}--;
  822. $form->{pre} = "<body bgcolor=#ffffff>\n<pre>";
  823. delete $form->{stylesheet};
  824. $form->{cd_open} = $pos_config{rp_cash_drawer_open};
  825. $form->parse_template( \%myconfig, ${LedgerSMB::Sysconfig::userspath} );
  826. if ( $form->{printed} !~ /$form->{formname}/ ) {
  827. $form->{printed} .= " $form->{formname}";
  828. $form->{printed} =~ s/^ //;
  829. $form->update_status( \%myconfig );
  830. }
  831. $old_form->{printed} = $form->{printed};
  832. # if we got back here restore the previous form
  833. if ( $form->{media} ne 'screen' ) {
  834. # restore and display form
  835. for ( keys %$old_form ) { $form->{$_} = $old_form->{$_}; }
  836. $form->{exchangerate} =
  837. $form->parse_amount( \%myconfig, $form->{exchangerate} );
  838. for $i ( 1 .. $form->{paidaccounts} ) {
  839. for (qw(paid exchangerate)) {
  840. $form->{"${_}_$i"} =
  841. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  842. }
  843. }
  844. delete $form->{pre};
  845. if ( !$form->{printandpost} ) {
  846. $form->{rowcount}--;
  847. #&display_form;
  848. }
  849. }
  850. }
  851. sub print_and_post {
  852. $form->error( $locale->text('Select a Printer!') )
  853. if ( $form->{media} eq 'screen' );
  854. $form->{printandpost} = 1;
  855. &print;
  856. &post;
  857. }
  858. sub lookup_partsgroup {
  859. $form->{my_partsgroup} =~ s/\r//;
  860. $form->{action} = $form->{my_partsgroup};
  861. if ( $form->{language_code} ) {
  862. # get english
  863. foreach $item ( split /\n/, $form->{partsgroup} ) {
  864. if ( $item =~ /$form->{action}/ ) {
  865. ( $partsgroup, $translation ) = split /--/, $item;
  866. $form->{action} = $partsgroup;
  867. last;
  868. }
  869. }
  870. }
  871. $form->{"partsgroup_$form->{rowcount}"} = $form->{action};
  872. &update;
  873. }
  874. sub print_options {
  875. $form->{PD}{ $form->{type} } = "checked";
  876. print qq|
  877. <input type=hidden name=format value=$form->{format}>
  878. <input type=hidden name=formname value=$form->{type}>
  879. <table width=100%>
  880. <tr>
  881. |;
  882. $media = qq|
  883. <td><input class=radio type=radio name=media value="screen"></td>
  884. <td>| . $locale->text('Screen') . qq|</td>|;
  885. if ( %{LedgerSMB::Sysconfig::printer} ) {
  886. for ( keys %{LedgerSMB::Sysconfig::printer} ) {
  887. $media .= qq|
  888. <td><input class=radio type=radio name=media value="$_"></td>
  889. <td nowrap>$_</td>
  890. |;
  891. }
  892. }
  893. $media =~ s/(value="\Q$form->{media}\E")/$1 checked/;
  894. print qq|
  895. $media
  896. <td width=99%>&nbsp;</td>|;
  897. if ( $form->{printed} =~ /$form->{type}/ ) {
  898. print qq|
  899. <th>\|| . $locale->text('Printed') . qq|\|</th>|;
  900. }
  901. print qq|
  902. </tr>
  903. </table>
  904. |;
  905. }
  906. sub receipts {
  907. $form->{title} = $locale->text('Receipts');
  908. $form->{db} = 'ar';
  909. RP->paymentaccounts( \%myconfig, \%$form );
  910. $paymentaccounts = "";
  911. for ( @{ $form->{PR} } ) { $paymentaccounts .= "$_->{accno} "; }
  912. if ( @{ $form->{all_years} } ) {
  913. # accounting years
  914. $form->{selectaccountingyear} = "<option>\n";
  915. for ( @{ $form->{all_years} } ) {
  916. $form->{selectaccountingyear} .= qq|<option>$_\n|;
  917. }
  918. $form->{selectaccountingmonth} = "<option>\n";
  919. for ( sort keys %{ $form->{all_month} } ) {
  920. $form->{selectaccountingmonth} .=
  921. qq|<option value=$_>|
  922. . $locale->text( $form->{all_month}{$_} ) . qq|\n|;
  923. }
  924. $selectfrom = qq|
  925. <tr>
  926. <th align=right>| . $locale->text('Period') . qq|</th>
  927. <td colspan=3>
  928. <select name=month>$form->{selectaccountingmonth}</select>
  929. <select name=year>$form->{selectaccountingyear}</select>
  930. <input name=interval class=radio type=radio value=0 checked>&nbsp;|
  931. . $locale->text('Current') . qq|
  932. <input name=interval class=radio type=radio value=1>&nbsp;|
  933. . $locale->text('Month') . qq|
  934. <input name=interval class=radio type=radio value=3>&nbsp;|
  935. . $locale->text('Quarter') . qq|
  936. <input name=interval class=radio type=radio value=12>&nbsp;|
  937. . $locale->text('Year') . qq|
  938. </td>
  939. </tr>
  940. |;
  941. }
  942. $form->header;
  943. print qq|
  944. <body>
  945. <form method=post action=$form->{script}>
  946. <input type=hidden name=title value="$form->{title}">
  947. <input type=hidden name=paymentaccounts value="$paymentaccounts">
  948. <input type=hidden name=till value=1>
  949. <input type=hidden name=subtotal value=1>
  950. <table width=100%>
  951. <tr>
  952. <th class=listtop>$form->{title}</th>
  953. </tr>
  954. <tr height="5"></tr>
  955. <tr>
  956. <td>
  957. <table>
  958. <input type=hidden name=nextsub value=list_payments>
  959. <tr>
  960. <th align=right>| . $locale->text('From') . qq|</th>
  961. <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
  962. <th align=right>| . $locale->text('To') . qq|</th>
  963. <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
  964. </tr>
  965. $selectfrom
  966. <input type=hidden name=sort value=transdate>
  967. <input type=hidden name=db value=$form->{db}>
  968. </table>
  969. </td>
  970. </tr>
  971. <tr>
  972. <td><hr size=3 noshade></td>
  973. </tr>
  974. </table>
  975. <br>
  976. <input type="hidden" name="path" value="$form->{path}">
  977. <input type="hidden" name="login" value="$form->{login}">
  978. <input type="hidden" name="sessionid" value="$form->{sessionid}">
  979. <button type="submit" class="submit" name="action" value="continue">|
  980. . $locale->text('Continue')
  981. . qq|</button>
  982. |;
  983. if ( $form->{lynx} ) {
  984. require "bin/menu.pl";
  985. &menubar;
  986. }
  987. print qq|
  988. </form>
  989. </body>
  990. </html>
  991. |;
  992. }