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