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