summaryrefslogtreecommitdiff
path: root/bin/pos.pl
blob: fb191a851c3f5a703f3337241b3d40982ac6ba0a (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, "|-", $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} = "$form->{script}?action=$form->{nextsub}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  86. &invoice_links;
  87. $form->{type} = "pos_invoice";
  88. $form->{format} = "txt";
  89. $form->{media} = ($myconfig{printer}) ? $myconfig{printer} : "screen";
  90. $form->{rowcount} = 0;
  91. $form->{readonly} = ($myconfig{acs} =~ /POS--Sale/) ? 1 : 0;
  92. $ENV{REMOTE_ADDR} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
  93. $form->{till} = $4;
  94. $form->{partsgroup} = "";
  95. for (@{ $form->{all_partsgroup} }) { $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n"; }
  96. &display_form;
  97. $form->{dontdisplayrows} = 1;
  98. &openinvoices;
  99. }
  100. sub openinvoices {
  101. undef %column_data;
  102. undef %column_heading;
  103. $ENV{REMOTE_ADDR} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
  104. $form->{till} = $4;
  105. $form->{sort} = 'transdate';
  106. for (qw(open l_invnumber l_transdate l_name l_amount l_curr l_till l_subtotal)) { $form->{$_} = 'Y'; }
  107. if ($myconfig{role} ne 'user') {
  108. $form->{l_employee} = 'Y';
  109. }
  110. $form->{title} = $locale->text('Open');
  111. transactions;
  112. }
  113. sub edit {
  114. $form->{title} = $locale->text('Edit POS Invoice');
  115. $form->{callback} = "$form->{script}?action=$form->{nextsub}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  116. &invoice_links;
  117. &prepare_invoice;
  118. $form->{type} = "pos_invoice";
  119. $form->{format} = "txt";
  120. $form->{media} = ($myconfig{printer}) ? $myconfig{printer} : "screen";
  121. $form->{readonly} = ($myconfig{acs} =~ /POS--Sale/) ? 1 : 0;
  122. $form->{partsgroup} = "";
  123. for (@{ $form->{all_partsgroup} }) { $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n"; }
  124. &display_form;
  125. }
  126. sub form_header {
  127. if (!$form->{'check_id'}){
  128. &check_alert;
  129. }
  130. # set option selected
  131. for (qw(AR currency)) {
  132. $form->{"select$_"} =~ s/ selected//;
  133. $form->{"select$_"} =~ s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
  134. }
  135. for (qw(customer department employee)) {
  136. $form->{"select$_"} = $form->unescape($form->{"select$_"});
  137. $form->{"select$_"} =~ s/ selected//;
  138. $form->{"select$_"} =~ s/(<option value="\Q$form->{$_}\E")/$1 selected/;
  139. }
  140. $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
  141. $exchangerate = qq|<tr>|;
  142. $exchangerate .= qq|
  143. <th align=right nowrap>|.$locale->text('Currency').qq|</th>
  144. <td><select name=currency>$form->{selectcurrency}</select></td> | if $form->{defaultcurrency};
  145. $exchangerate .= qq|
  146. <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
  147. <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
  148. |;
  149. if ($form->{defaultcurrency} && $form->{currency} ne $form->{defaultcurrency}) {
  150. if ($form->{forex}) {
  151. $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>|;
  152. } else {
  153. $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
  154. }
  155. }
  156. $exchangerate .= qq|
  157. <input type=hidden name=forex value=$form->{forex}>
  158. </tr>
  159. |;
  160. if ($form->{selectcustomer}) {
  161. $customer = qq|<select name=customer>$form->{selectcustomer}</select>
  162. <input type=hidden name="selectcustomer" value="|.
  163. $form->escape($form->{selectcustomer},1).qq|">|;
  164. } else {
  165. $customer = qq|<input name=customer value="$form->{customer}" size=35>|;
  166. }
  167. $department = qq|
  168. <tr>
  169. <th align="right" nowrap>|.$locale->text('Department').qq|</th>
  170. <td colspan=3><select name=department>$form->{selectdepartment}</select>
  171. <input type=hidden name=selectdepartment value="|.
  172. $form->escape($form->{selectdepartment},1).qq|">
  173. </td>
  174. </tr>
  175. | if $form->{selectdepartment};
  176. $employee = qq|
  177. <tr>
  178. <th align=right nowrap>|.$locale->text('Salesperson').qq|</th>
  179. <td colspan=3><select name=employee>$form->{selectemployee}</select></td>
  180. <input type=hidden name=selectemployee value="|.
  181. $form->escape($form->{selectemployee},1).qq|">
  182. </tr>
  183. | if $form->{selectemployee};
  184. if ($form->{change} != $form->{oldchange}) {
  185. $form->{creditremaining} -= $form->{oldchange};
  186. }
  187. $n = ($form->{creditremaining} < 0) ? "0" : "1";
  188. if ($form->{business}) {
  189. $business = qq|
  190. <tr>
  191. <th align=right nowrap>|.$locale->text('Business').qq|</th>
  192. <td>$form->{business}</td>
  193. <td width=10></td>
  194. <th align=right nowrap>|.$locale->text('Trade Discount').qq|</th>
  195. <td>|.$form->format_amount(\%myconfig, $form->{tradediscount} * 100).qq| %</td>
  196. </tr>
  197. |;
  198. }
  199. if ($form->{selectlanguage}) {
  200. if ($form->{language_code} ne $form->{oldlanguage_code}) {
  201. # rebuild partsgroup
  202. $form->get_partsgroup(\%myconfig, { language_code => $form->{language_code}, searchitems => 'nolabor'});
  203. $form->{partsgroup} = "";
  204. for (@{ $form->{all_partsgroup} }) { $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n"; }
  205. $form->{oldlanguage_code} = $form->{language_code};
  206. }
  207. $form->{"selectlanguage"} = $form->unescape($form->{"selectlanguage"});
  208. $form->{"selectlanguage"} =~ s/ selected//;
  209. $form->{"selectlanguage"} =~ s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
  210. $lang = qq|
  211. <tr>
  212. <th align=right>|.$locale->text('Language').qq|</th>
  213. <td colspan=3><select name=language_code>$form->{selectlanguage}</select></td>
  214. </tr>
  215. <input type=hidden name=oldlanguage_code value=$form->{oldlanguage_code}>
  216. <input type=hidden name=selectlanguage value="|.
  217. $form->escape($form->{selectlanguage},1).qq|">|;
  218. }
  219. $i = $form->{rowcount} + 1;
  220. $focus = "partnumber_$i";
  221. $form->header;
  222. print qq|
  223. <body onLoad="document.forms[0].${focus}.focus()" />
  224. <form method=post action="$form->{script}">
  225. |;
  226. $form->hide_form(qw(id till type format printed title discount creditlimit creditremaining tradediscount business closedto locked oldtransdate customer_id oldcustomer check_id));
  227. print qq|
  228. <input type=hidden name=vc value="customer">
  229. <table width=100%>
  230. <tr class=listtop>
  231. <th class=listtop>$form->{title}</font></th>
  232. </tr>|;
  233. if ($form->{'check_id'}){
  234. print qq|
  235. <tr class=listtop>
  236. <th class=listtop style="background-color: red">|.$locale->text('Check ID').qq|
  237. </th>
  238. </tr>|;
  239. }
  240. print qq|
  241. <tr height="5"></tr>
  242. <tr>
  243. <td>
  244. <table width=100%>
  245. <tr valign=top>
  246. <td>
  247. <table>
  248. <tr>
  249. <th align=right nowrap>|.$locale->text('Customer').qq|</th>
  250. <td colspan=3>$customer</td>
  251. </tr>
  252. <tr>
  253. <td></td>
  254. <td colspan=3>
  255. <table>
  256. <tr>
  257. <th align=right nowrap>|.$locale->text('Credit Limit').qq|</th>
  258. <td>$form->{creditlimit}</td>
  259. <td width=10></td>
  260. <th align=right nowrap>|.$locale->text('Remaining').qq|</th>
  261. <td class="plus$n">|.$form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0").qq|</font></td>
  262. </tr>
  263. $business
  264. </table>
  265. </td>
  266. </tr>
  267. <tr>
  268. <th align=right nowrap>|.$locale->text('Record in').qq|</th>
  269. <td colspan=3><select name=AR>$form->{selectAR}</select></td>
  270. <input type=hidden name=selectAR value="$form->{selectAR}">
  271. </tr>
  272. $department
  273. </table>
  274. </td>
  275. <td>
  276. <table>
  277. $employee
  278. $exchangerate
  279. $lang
  280. </table>
  281. </td>
  282. </tr>
  283. </table>
  284. </td>
  285. </tr>
  286. <tr>
  287. <td>
  288. </td>
  289. </tr>
  290. |;
  291. $form->hide_form(qw(taxaccounts duedate invnumber transdate selectcurrency defaultcurrency));
  292. for (split / /, $form->{taxaccounts}) {
  293. $form->hide_form("${_}_rate", "${_}_description", "${_}_taxnumber");
  294. }
  295. }
  296. sub form_footer {
  297. $form->{invtotal} = $form->{invsubtotal};
  298. $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
  299. $taxincluded = "";
  300. if ($form->{taxaccounts}) {
  301. $taxincluded = qq|
  302. <tr height="5"></tr>
  303. <tr>
  304. <td align=right>
  305. <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td><th align=left>|.$locale->text('Tax Included').qq|</th>
  306. </tr>
  307. |;
  308. }
  309. if (!$form->{taxincluded}) {
  310. my @taxes = Tax::init_taxes($form, $form->{taxaccounts});
  311. $form->{invtotal} += Tax::calculate_taxes(\@taxes, $form,
  312. $form->{invsubtotal}, 0);
  313. foreach my $item (@taxes) {
  314. my $taccno = $item->account;
  315. $form->{"${taccno}_total"} = $form->format_amount(\%myconfig,
  316. $item->value, 2, 0);
  317. $tax .= qq|
  318. <tr>
  319. <th align=right>$form->{"${taccno}_description"}</th>
  320. <td align=right>$form->{"${taccno}_total"}</td>
  321. </tr>
  322. | if $item->value;
  323. }
  324. $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
  325. $subtotal = qq|
  326. <tr>
  327. <th align=right>|.$locale->text('Subtotal').qq|</th>
  328. <td align=right>$form->{invsubtotal}</td>
  329. </tr>
  330. |;
  331. }
  332. @column_index = qw(paid source memo AR_paid);
  333. $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
  334. $column_data{source} = "<th>".$locale->text('Source')."</th>";
  335. $column_data{memo} = "<th>".$locale->text('Memo')."</th>";
  336. $column_data{AR_paid} = "<th>&nbsp;</th>";
  337. print qq|
  338. <tr>
  339. <td>
  340. <table width=100%>
  341. <tr valign=top>
  342. <td>
  343. <table>
  344. <tr>
  345. |;
  346. for (@column_index) { print "$column_data{$_}\n"; }
  347. print qq|
  348. </tr>
  349. |;
  350. $totalpaid = 0;
  351. $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
  352. for $i (1 .. $form->{paidaccounts}) {
  353. $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
  354. $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
  355. # format amounts
  356. $totalpaid += $form->{"paid_$i"};
  357. $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
  358. $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
  359. if ($form->{"paid__$i"}){
  360. $column_data{paid} = qq|<td><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  361. } else {
  362. $column_data{paid} = qq|<td><input accesskey='n' name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  363. }
  364. $column_data{source} = qq|<td><input name="source_$i" size=10 value="$form->{"source_$i"}"></td>|;
  365. $column_data{memo} = qq|<td><input name="memo_$i" size=10 value="$form->{"memo_$i"}"></td>|;
  366. if ($pos_config{"coa_prefix"}){
  367. if (!$form->{"AR_paid_$i"}){
  368. $form->{"AR_paid_$i"} =
  369. $pos_config{"coa_prefix"}.'.'.$pos_config{"till"};
  370. }
  371. $column_data{AR_paid} = qq|<input type=hidden name="AR_paid_$i"
  372. value='$form->{"AR_paid_$i"}'>|;
  373. } else {
  374. $column_data{AR_paid} = qq|<td><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
  375. }
  376. print qq|
  377. <tr>
  378. |;
  379. for (@column_index) { print "$column_data{$_}\n"; }
  380. print qq|
  381. </tr>
  382. |;
  383. $form->hide_form("cleared_$i", "exchangerate_$i", "forex_$i");
  384. }
  385. $form->{change} = 0;
  386. if ($totalpaid > $form->{invtotal}) {
  387. $form->{change} = $totalpaid - $form->{invtotal};
  388. }
  389. $form->{oldchange} = $form->{change};
  390. $form->{change} = $form->format_amount(\%myconfig, $form->{change}, 2, 0);
  391. $form->{totalpaid} = $form->format_amount(\%myconfig, $totalpaid, 2);
  392. $form->{oldinvtotal} = $form->{invtotal};
  393. $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
  394. print qq|
  395. <tr>
  396. <th align=right>|.$locale->text('Change').qq|</th>
  397. <th>$form->{change}</th>
  398. </tr>
  399. </table>
  400. </td>
  401. <td align=right>
  402. <table>
  403. $subtotal
  404. $tax
  405. <tr>
  406. <th align=right>|.$locale->text('Total').qq|</th>
  407. <td align=right>$form->{invtotal}</td>
  408. </tr>
  409. $taxincluded
  410. </table>
  411. </td>
  412. </tr>
  413. </table>
  414. </td>
  415. </tr>
  416. <input type=hidden name=oldtotalpaid value=$totalpaid>
  417. <input type=hidden name=datepaid value=$form->{transdate}>
  418. <tr>
  419. <td>
  420. |;
  421. $form->hide_form(qw(paidaccounts selectAR_paid oldinvtotal change oldchange invtotal));
  422. &print_options;
  423. print qq|
  424. </td>
  425. </tr>
  426. <tr>
  427. <td><hr size=3 noshade></td>
  428. </tr>
  429. </table>
  430. |;
  431. $transdate = $form->datetonum(\%myconfig, $form->{transdate});
  432. $closedto = $form->datetonum(\%myconfig, $form->{closedto});
  433. # type=submit $locale->text('Update')
  434. # type=submit $locale->text('Print')
  435. # type=submit $locale->text('Post')
  436. # type=submit $locale->text('Print and Post')
  437. # type=submit $locale->text('Delete')
  438. if (! $form->{readonly}) {
  439. %button = ('Update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
  440. 'Print' => { ndx => 2, key => 'P', value => $locale->text('Print') },
  441. 'Post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  442. 'Print and Post' => { ndx => 4, key => 'R', value => $locale->text('Print and Post') },
  443. 'Delete' => { ndx => 5, key => 'D', value => $locale->text('Delete') },
  444. );
  445. if ($transdate > $closedto) {
  446. if (! $form->{id}) {
  447. delete $button{'Delete'};
  448. }
  449. delete $button{'Print and Post'} unless ${LedgerSMB::Sysconfig::latex};
  450. } else {
  451. for ('Print', 'Post', 'Print and Post', 'Delete') { delete $button{$_} }
  452. }
  453. for (sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button) { $form->print_button(\%button, $_) }
  454. print qq|<p>
  455. <input type=text size=1 value="B" accesskey="B" title="[Alt-B]">\n|;
  456. if ($form->{partsgroup}) {
  457. $form->{partsgroup} =~ s/\r//g;
  458. $form->{partsgroup} = $form->quote($form->{partsgroup});
  459. $spc = ($form->{path} =~ /lynx/) ? "." : " ";
  460. print qq|
  461. <input type=hidden name=nextsub value=lookup_partsgroup>
  462. <input type=hidden name=partsgroup value="$form->{partsgroup}">|;
  463. foreach $item (split /\n/, $form->{partsgroup}) {
  464. ($partsgroup, $translation) = split /--/, $item;
  465. $item = ($translation) ? $translation : $partsgroup;
  466. print qq| <button class="submit" type="submit" name="action" value="$spc$item">$spc$item</button>\n| if $item;
  467. }
  468. }
  469. }
  470. if ($form->{lynx}) {
  471. require "bin/menu.pl";
  472. &menubar;
  473. }
  474. $form->hide_form(qw(rowcount callback path login sessionid));
  475. print qq|
  476. </form>
  477. </body>
  478. </html>
  479. |;
  480. }
  481. sub post {
  482. $form->{media} = 'Printer';
  483. $form->isblank("customer", $locale->text('Customer missing!'));
  484. # if oldcustomer ne customer redo form
  485. $customer = $form->{customer};
  486. $customer =~ s/--.*//g;
  487. $customer .= "--$form->{customer_id}";
  488. if ($customer ne $form->{oldcustomer}) {
  489. &update;
  490. exit;
  491. }
  492. &validate_items;
  493. &print;
  494. $form->isblank("exchangerate", $locale->text('Exchange rate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
  495. $paid = 0;
  496. for (1 .. $form->{paidaccounts}) { $paid += $form->parse_amount(\%myconfig, $form->{"paid_$_"}); }
  497. delete $form->{datepaid} unless $paid;
  498. $total = $form->parse_amount(\%myconfig, $form->{invtotal});
  499. # deduct change from first payment
  500. #$form->{"paid_1"} = $form->{invtotal} if $paid > $total;
  501. $form->{paid} = $paid;
  502. if ($paid > $total){
  503. ++$form->{paidaccounts};
  504. my $pa = $form->{paidaccounts};
  505. $form->{"paid_$pa"} = $total - $paid;
  506. $form->{"source_$pa"} = 'cash';
  507. $form->{"exchangerate_$pa"} = 0;
  508. $form->{"AR_paid_$pa"} = $form->{AR_paid_1}
  509. }
  510. ($form->{AR}) = split /--/, $form->{AR};
  511. if (IS->post_invoice(\%myconfig, \%$form)) {
  512. $form->redirect($locale->text('Posted!'));
  513. } else {
  514. $form->error($locale->text('Cannot post transaction!'));
  515. }
  516. }
  517. sub display_row {
  518. my $numrows = shift;
  519. @column_index = qw(partnumber description partsgroup qty unit sellprice discount linetotal);
  520. $form->{invsubtotal} = 0;
  521. for (split / /, $form->{taxaccounts}) { $form->{"${_}_base"} = 0; }
  522. $column_data{partnumber} = qq|<th class=listheading nowrap>|.$locale->text('Number').qq|</th>|;
  523. $column_data{description} = qq|<th class=listheading nowrap>|.$locale->text('Description').qq|</th>|;
  524. $column_data{qty} = qq|<th class=listheading nowrap>|.$locale->text('Qty').qq|</th>|;
  525. $column_data{unit} = qq|<th class=listheading nowrap>|.$locale->text('Unit').qq|</th>|;
  526. $column_data{sellprice} = qq|<th class=listheading nowrap>|.$locale->text('Price').qq|</th>|;
  527. $column_data{linetotal} = qq|<th class=listheading nowrap>|.$locale->text('Extended').qq|</th>|;
  528. $column_data{discount} = qq|<th class=listheading nowrap>%</th>|;
  529. print qq|
  530. <tr>
  531. <td>
  532. <table width=100%>
  533. <tr class=listheading>|;
  534. for (@column_index) { print "\n$column_data{$_}"; };
  535. print qq|
  536. </tr>
  537. |;
  538. $exchangerate = $form->parse_amount(\%myconfig, $form->{exchangerate});
  539. $exchangerate = ($exchangerate) ? $exchangerate : 1;
  540. for $i (1 .. $numrows) {
  541. # undo formatting
  542. for (qw(qty discount sellprice)) { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}); }
  543. ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
  544. $dec = length $dec;
  545. $decimalplaces = ($dec > 2) ? $dec : 2;
  546. if (($form->{"qty_$i"} != $form->{"oldqty_$i"}) || ($form->{currency} ne $form->{oldcurrency})) {
  547. # check for a pricematrix
  548. @a = split / /, $form->{"pricematrix_$i"};
  549. if (scalar @a) {
  550. foreach $item (@a) {
  551. ($q, $p) = split /:/, $item;
  552. if (($p * 1) && ($form->{"qty_$i"} >= ($q * 1))) {
  553. ($dec) = ($p =~ /\.(\d+)/);
  554. $dec = length $dec;
  555. $decimalplaces = ($dec > 2) ? $dec : 2;
  556. $form->{"sellprice_$i"} = $form->round_amount($p / $exchangerate, $decimalplaces);
  557. }
  558. }
  559. }
  560. }
  561. if ($i < $numrows) {
  562. $column_data{discount} = qq|<td align=right><input name="discount_$i" size=3 value=|.$form->format_amount(\%myconfig, $form->{"discount_$i"}).qq|></td>|;
  563. } else {
  564. $column_data{discount} = qq|<td></td>|;
  565. }
  566. $discount = $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"}/100, $decimalplaces);
  567. $linetotal = $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
  568. $linetotal = $form->round_amount($linetotal * $form->{"qty_$i"}, 2);
  569. for (qw(partnumber sku description partsgroup unit)) { $form->{"${_}_$i"} = $form->quote($form->{"${_}_$i"}); }
  570. $column_data{partnumber} = qq|<td><input name="partnumber_$i" size=20 value="$form->{"partnumber_$i"}" accesskey="$i" title="[Alt-$i]"></td>|;
  571. if (($rows = $form->numtextrows($form->{"description_$i"}, 40, 6)) > 1) {
  572. $column_data{description} = qq|<td><textarea name="description_$i" rows=$rows cols=46 wrap=soft>$form->{"description_$i"}</textarea></td>|;
  573. } else {
  574. $column_data{description} = qq|<td><input name="description_$i" size=48 value="$form->{"description_$i"}"></td>|;
  575. }
  576. $column_data{qty} = qq|<td align=right><input name="qty_$i" size=5 value=|.$form->format_amount(\%myconfig, $form->{"qty_$i"}).qq|></td>|;
  577. $column_data{unit} = qq|<td>$form->{"unit_$i"}</td>|;
  578. $column_data{sellprice} = qq|<td align=right><input name="sellprice_$i" size=9 value=|.$form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces).qq|></td>|;
  579. $column_data{linetotal} = qq|<td align=right>|.$form->format_amount(\%myconfig, $linetotal, 2).qq|</td>|;
  580. print qq|
  581. <tr valign=top>|;
  582. for (@column_index) { print "\n$column_data{$_}"; }
  583. print qq|
  584. </tr>
  585. |;
  586. $form->{"oldqty_$i"} = $form->{"qty_$i"};
  587. for (qw(id listprice lastcost taxaccounts pricematrix oldqty sku partsgroup unit inventory_accno_id income_accno_id expense_accno_id)) { $form->hide_form("${_}_$i") }
  588. for (split / /, $form->{"taxaccounts_$i"}) { $form->{"${_}_base"} += $linetotal; }
  589. $form->{invsubtotal} += $linetotal;
  590. }
  591. print qq|
  592. </table>
  593. </td>
  594. </tr>
  595. <input type=hidden name=oldcurrency value=$form->{currency}>
  596. |;
  597. }
  598. sub print {
  599. if (!$form->{invnumber}){
  600. $form->{invnumber} = $form->update_defaults(\%myconfig, 'sinumber');
  601. }
  602. $rc = $form->{'rowcount'};
  603. $pc = $form->{'paidaccounts'};
  604. if ($form->{"partnumber_$rc"} || $form->{"description_$rc"} ||
  605. $form->{"paid_$pc"}){
  606. &update;
  607. exit;
  608. }
  609. for $i (1 .. $rc - 1){
  610. if ($form->{"qty_$i"} != $form->{"oldqty_$i"}){
  611. &update;
  612. exit;
  613. }
  614. }
  615. if (!$form->{invnumber}) {
  616. $form->{invnumber} = $form->update_defaults(\%myconfig, "sinumber");
  617. if ($form->{media} eq 'screen') {
  618. &update;
  619. exit;
  620. }
  621. }
  622. $old_form = new Form;
  623. for (keys %$form) { $old_form->{$_} = $form->{$_}; }
  624. for (qw(employee department)) { $form->{$_} =~ s/--.*//g; }
  625. $form->{invdate} = $form->{transdate};
  626. my @lt = localtime();
  627. $form->{dateprinted} = $lt[2].":".$lt[1].":".$lt[0];
  628. &print_form($old_form);
  629. }
  630. sub print_form {
  631. my $old_form = shift;
  632. &open_drawer;
  633. # if oldcustomer ne customer redo form
  634. $customer = $form->{customer};
  635. $customer =~ s/--.*//g;
  636. $customer .= "--$form->{customer_id}";
  637. if ($customer ne $form->{oldcustomer}) {
  638. &update;
  639. exit;
  640. }
  641. &validate_items;
  642. &{ "$form->{vc}_details" };
  643. @a = ();
  644. for (1 .. $form->{rowcount}) { push @a, ("partnumber_$_", "description_$_"); }
  645. for (split / /, $form->{taxaccounts}) { push @a, "${_}_description"; }
  646. $form->format_string(@a);
  647. # format payment dates
  648. for (1 .. $form->{paidaccounts}) { $form->{"datepaid_$_"} = $locale->date(\%myconfig, $form->{"datepaid_$_"}); }
  649. IS->invoice_details(\%myconfig, \%$form);
  650. if ($form->parse_amount(\%myconfig, $form->{total}) <= 0) {
  651. $form->{total} = 0;
  652. } else {
  653. $form->{change} = 0;
  654. }
  655. for (qw(company address tel fax businessnumber)) { $form->{$_} = $myconfig{$_}; }
  656. $form->{username} = $myconfig{name};
  657. $form->{address} =~ s/\\n/\n/g;
  658. push @a, qw(company address tel fax businessnumber username);
  659. $form->format_string(@a);
  660. $form->{templates} = "$myconfig{templates}";
  661. $form->{IN} = "$form->{type}.$form->{format}";
  662. if ($form->{format} =~ /(postscript|pdf)/) {
  663. $form->{IN} =~ s/$&$/tex/;
  664. }
  665. if ($form->{media} ne 'screen') {
  666. $form->{OUT} = "| $printer{$form->{media}}";
  667. }
  668. $form->{discount} = $form->format_amount(\%myconfig, $form->{discount} * 100);
  669. $form->{rowcount}--;
  670. $form->{pre} = "<body bgcolor=#ffffff>\n<pre>";
  671. delete $form->{stylesheet};
  672. $form->{cd_open} = $pos_config{rp_cash_drawer_open};
  673. $form->parse_template(\%myconfig, ${LedgerSMB::Sysconfig::userspath});
  674. if ($form->{printed} !~ /$form->{formname}/) {
  675. $form->{printed} .= " $form->{formname}";
  676. $form->{printed} =~ s/^ //;
  677. $form->update_status(\%myconfig);
  678. }
  679. $old_form->{printed} = $form->{printed};
  680. # if we got back here restore the previous form
  681. if ($form->{media} ne 'screen') {
  682. # restore and display form
  683. for (keys %$old_form) { $form->{$_} = $old_form->{$_}; }
  684. $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
  685. for $i (1 .. $form->{paidaccounts}) {
  686. for (qw(paid exchangerate)) { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}); }
  687. }
  688. delete $form->{pre};
  689. if (! $form->{printandpost}) {
  690. $form->{rowcount}--;
  691. #&display_form;
  692. }
  693. }
  694. }
  695. sub print_and_post {
  696. $form->error($locale->text('Select a Printer!')) if ($form->{media} eq 'screen');
  697. $form->{printandpost} = 1;
  698. &print;
  699. &post;
  700. }
  701. sub lookup_partsgroup {
  702. $form->{action} =~ s/\r//;
  703. $form->{action} = substr($form->{action}, 1);
  704. if ($form->{language_code}) {
  705. # get english
  706. foreach $item (split /\n/, $form->{partsgroup}) {
  707. if ($item =~ /$form->{action}/) {
  708. ($partsgroup, $translation) = split /--/, $item;
  709. $form->{action} = $partsgroup;
  710. last;
  711. }
  712. }
  713. }
  714. $form->{"partsgroup_$form->{rowcount}"} = $form->{action};
  715. &update;
  716. }
  717. sub print_options {
  718. $form->{PD}{$form->{type}} = "checked";
  719. print qq|
  720. <input type=hidden name=format value=$form->{format}>
  721. <input type=hidden name=formname value=$form->{type}>
  722. <table width=100%>
  723. <tr>
  724. |;
  725. $media = qq|
  726. <td><input class=radio type=radio name=media value="screen"></td>
  727. <td>|.$locale->text('Screen').qq|</td>|;
  728. if (%printer) {
  729. for (keys %printer) {
  730. $media .= qq|
  731. <td><input class=radio type=radio name=media value="$_"></td>
  732. <td nowrap>$_</td>
  733. |;
  734. }
  735. }
  736. $media =~ s/(value="\Q$form->{media}\E")/$1 checked/;
  737. print qq|
  738. $media
  739. <td width=99%>&nbsp;</td>|;
  740. if ($form->{printed} =~ /$form->{type}/) {
  741. print qq|
  742. <th>\||.$locale->text('Printed').qq|\|</th>|;
  743. }
  744. print qq|
  745. </tr>
  746. </table>
  747. |;
  748. }
  749. sub receipts {
  750. $form->{title} = $locale->text('Receipts');
  751. $form->{db} = 'ar';
  752. RP->paymentaccounts(\%myconfig, \%$form);
  753. $paymentaccounts = "";
  754. for (@{ $form->{PR} } ) { $paymentaccounts .= "$_->{accno} "; }
  755. if (@{ $form->{all_years} }) {
  756. # accounting years
  757. $form->{selectaccountingyear} = "<option>\n";
  758. for (@{ $form->{all_years} }) { $form->{selectaccountingyear} .= qq|<option>$_\n|; }
  759. $form->{selectaccountingmonth} = "<option>\n";
  760. for (sort keys %{ $form->{all_month} }) { $form->{selectaccountingmonth} .= qq|<option value=$_>|.$locale->text($form->{all_month}{$_}).qq|\n|; }
  761. $selectfrom = qq|
  762. <tr>
  763. <th align=right>|.$locale->text('Period').qq|</th>
  764. <td colspan=3>
  765. <select name=month>$form->{selectaccountingmonth}</select>
  766. <select name=year>$form->{selectaccountingyear}</select>
  767. <input name=interval class=radio type=radio value=0 checked>&nbsp;|.$locale->text('Current').qq|
  768. <input name=interval class=radio type=radio value=1>&nbsp;|.$locale->text('Month').qq|
  769. <input name=interval class=radio type=radio value=3>&nbsp;|.$locale->text('Quarter').qq|
  770. <input name=interval class=radio type=radio value=12>&nbsp;|.$locale->text('Year').qq|
  771. </td>
  772. </tr>
  773. |;
  774. }
  775. $form->header;
  776. print qq|
  777. <body>
  778. <form method=post action=$form->{script}>
  779. <input type=hidden name=title value="$form->{title}">
  780. <input type=hidden name=paymentaccounts value="$paymentaccounts">
  781. <input type=hidden name=till value=1>
  782. <input type=hidden name=subtotal value=1>
  783. <table width=100%>
  784. <tr>
  785. <th class=listtop>$form->{title}</th>
  786. </tr>
  787. <tr height="5"></tr>
  788. <tr>
  789. <td>
  790. <table>
  791. <input type=hidden name=nextsub value=list_payments>
  792. <tr>
  793. <th align=right>|.$locale->text('From').qq|</th>
  794. <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
  795. <th align=right>|.$locale->text('To').qq|</th>
  796. <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
  797. </tr>
  798. $selectfrom
  799. <input type=hidden name=sort value=transdate>
  800. <input type=hidden name=db value=$form->{db}>
  801. </table>
  802. </td>
  803. </tr>
  804. <tr>
  805. <td><hr size=3 noshade></td>
  806. </tr>
  807. </table>
  808. <br>
  809. <input type="hidden" name="path" value="$form->{path}">
  810. <input type="hidden" name="login" value="$form->{login}">
  811. <input type="hidden" name="sessionid" value="$form->{sessionid}">
  812. <button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  813. |;
  814. if ($form->{lynx}) {
  815. require "bin/menu.pl";
  816. &menubar;
  817. }
  818. print qq|
  819. </form>
  820. </body>
  821. </html>
  822. |;
  823. }