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