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