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