summaryrefslogtreecommitdiff
path: root/bin/pos.pl
blob: 00cf8202c0e827bffcc2e56633717bbc0ae2b1fe (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. $form->{dontdisplayend} = 1;
  97. &display_form;
  98. $form->{dontdisplayrows} = 1;
  99. $form->{dontdisplayend} = 0;
  100. &openinvoices;
  101. }
  102. sub openinvoices {
  103. undef %column_data;
  104. undef %column_heading;
  105. $ENV{REMOTE_ADDR} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
  106. $form->{till} = $4;
  107. $form->{sort} = 'transdate';
  108. for (qw(open l_invnumber l_transdate l_name l_amount l_curr l_till l_subtotal)) { $form->{$_} = 'Y'; }
  109. if ($myconfig{role} ne 'user') {
  110. $form->{l_employee} = 'Y';
  111. }
  112. $form->{title} = $locale->text('Open');
  113. transactions;
  114. }
  115. sub edit {
  116. $form->{title} = $locale->text('Edit POS Invoice');
  117. $form->{callback} = "$form->{script}?action=$form->{nextsub}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  118. &invoice_links;
  119. &prepare_invoice;
  120. $form->{type} = "pos_invoice";
  121. $form->{format} = "txt";
  122. $form->{media} = ($myconfig{printer}) ? $myconfig{printer} : "screen";
  123. $form->{readonly} = ($myconfig{acs} =~ /POS--Sale/) ? 1 : 0;
  124. $form->{partsgroup} = "";
  125. for (@{ $form->{all_partsgroup} }) { $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n"; }
  126. &display_form;
  127. }
  128. sub form_header {
  129. if (!$form->{'check_id'}){
  130. &check_alert;
  131. }
  132. # set option selected
  133. for (qw(AR currency)) {
  134. $form->{"select$_"} =~ s/ selected//;
  135. $form->{"select$_"} =~ s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
  136. }
  137. for (qw(customer department employee)) {
  138. $form->{"select$_"} = $form->unescape($form->{"select$_"});
  139. $form->{"select$_"} =~ s/ selected//;
  140. $form->{"select$_"} =~ s/(<option value="\Q$form->{$_}\E")/$1 selected/;
  141. }
  142. $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
  143. $exchangerate = qq|<tr>|;
  144. $exchangerate .= qq|
  145. <th align=right nowrap>|.$locale->text('Currency').qq|</th>
  146. <td><select name=currency>$form->{selectcurrency}</select></td> | if $form->{defaultcurrency};
  147. $exchangerate .= qq|
  148. <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
  149. <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
  150. |;
  151. if ($form->{defaultcurrency} && $form->{currency} ne $form->{defaultcurrency}) {
  152. if ($form->{forex}) {
  153. $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>|;
  154. } else {
  155. $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
  156. }
  157. }
  158. $exchangerate .= qq|
  159. <input type=hidden name=forex value=$form->{forex}>
  160. </tr>
  161. |;
  162. if ($form->{selectcustomer}) {
  163. $customer = qq|<select name=customer>$form->{selectcustomer}</select>
  164. <input type=hidden name="selectcustomer" value="|.
  165. $form->escape($form->{selectcustomer},1).qq|">|;
  166. } else {
  167. $customer = qq|<input name=customer value="$form->{customer}" size=35>|;
  168. }
  169. $department = qq|
  170. <tr>
  171. <th align="right" nowrap>|.$locale->text('Department').qq|</th>
  172. <td colspan=3><select name=department>$form->{selectdepartment}</select>
  173. <input type=hidden name=selectdepartment value="|.
  174. $form->escape($form->{selectdepartment},1).qq|">
  175. </td>
  176. </tr>
  177. | if $form->{selectdepartment};
  178. $employee = qq|
  179. <tr>
  180. <th align=right nowrap>|.$locale->text('Salesperson').qq|</th>
  181. <td colspan=3><select name=employee>$form->{selectemployee}</select></td>
  182. <input type=hidden name=selectemployee value="|.
  183. $form->escape($form->{selectemployee},1).qq|">
  184. </tr>
  185. | if $form->{selectemployee};
  186. if ($form->{change} != $form->{oldchange}) {
  187. $form->{creditremaining} -= $form->{oldchange};
  188. }
  189. $n = ($form->{creditremaining} < 0) ? "0" : "1";
  190. if ($form->{business}) {
  191. $business = qq|
  192. <tr>
  193. <th align=right nowrap>|.$locale->text('Business').qq|</th>
  194. <td>$form->{business}</td>
  195. <td width=10></td>
  196. <th align=right nowrap>|.$locale->text('Trade Discount').qq|</th>
  197. <td>|.$form->format_amount(\%myconfig, $form->{tradediscount} * 100).qq| %</td>
  198. </tr>
  199. |;
  200. }
  201. if ($form->{selectlanguage}) {
  202. if ($form->{language_code} ne $form->{oldlanguage_code}) {
  203. # rebuild partsgroup
  204. $form->get_partsgroup(\%myconfig, { language_code => $form->{language_code}, searchitems => 'nolabor'});
  205. $form->{partsgroup} = "";
  206. for (@{ $form->{all_partsgroup} }) { $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n"; }
  207. $form->{oldlanguage_code} = $form->{language_code};
  208. }
  209. $form->{"selectlanguage"} = $form->unescape($form->{"selectlanguage"});
  210. $form->{"selectlanguage"} =~ s/ selected//;
  211. $form->{"selectlanguage"} =~ s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
  212. $lang = qq|
  213. <tr>
  214. <th align=right>|.$locale->text('Language').qq|</th>
  215. <td colspan=3><select name=language_code>$form->{selectlanguage}</select></td>
  216. </tr>
  217. <input type=hidden name=oldlanguage_code value=$form->{oldlanguage_code}>
  218. <input type=hidden name=selectlanguage value="|.
  219. $form->escape($form->{selectlanguage},1).qq|">|;
  220. }
  221. $i = $form->{rowcount} + 1;
  222. $focus = "partnumber_$i";
  223. $form->header();
  224. print qq|
  225. <body onLoad="document.forms[0].${focus}.focus()" />
  226. <form method=post action="$form->{script}">
  227. |;
  228. $form->hide_form(qw(id till type format printed title discount creditlimit creditremaining tradediscount business closedto locked oldtransdate customer_id oldcustomer check_id));
  229. print qq|
  230. <input type=hidden name=vc value="customer">
  231. <table width=100%>
  232. <tr class=listtop>
  233. <th class=listtop>$form->{title}</font></th>
  234. </tr>|;
  235. if ($form->{'check_id'}){
  236. print qq|
  237. <tr class=listtop>
  238. <th class=listtop style="background-color: red">|.$locale->text('Check ID').qq|
  239. </th>
  240. </tr>|;
  241. }
  242. print qq|
  243. <tr height="5"></tr>
  244. <tr>
  245. <td>
  246. <table width=100%>
  247. <tr valign=top>
  248. <td>
  249. <table>
  250. <tr>
  251. <th align=right nowrap>|.$locale->text('Customer').qq|</th>
  252. <td colspan=3>$customer</td>
  253. </tr>
  254. <tr>
  255. <td></td>
  256. <td colspan=3>
  257. <table>
  258. <tr>
  259. <th align=right nowrap>|.$locale->text('Credit Limit').qq|</th>
  260. <td>$form->{creditlimit}</td>
  261. <td width=10></td>
  262. <th align=right nowrap>|.$locale->text('Remaining').qq|</th>
  263. <td class="plus$n">|.$form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0").qq|</font></td>
  264. </tr>
  265. $business
  266. </table>
  267. </td>
  268. </tr>
  269. <tr>
  270. <th align=right nowrap>|.$locale->text('Record in').qq|</th>
  271. <td colspan=3><select name=AR>$form->{selectAR}</select></td>
  272. <input type=hidden name=selectAR value="$form->{selectAR}">
  273. </tr>
  274. $department
  275. </table>
  276. </td>
  277. <td>
  278. <table>
  279. $employee
  280. $exchangerate
  281. $lang
  282. </table>
  283. </td>
  284. </tr>
  285. </table>
  286. </td>
  287. </tr>
  288. <tr>
  289. <td>
  290. </td>
  291. </tr>
  292. |;
  293. $form->hide_form(qw(taxaccounts duedate invnumber transdate selectcurrency defaultcurrency));
  294. for (split / /, $form->{taxaccounts}) {
  295. $form->hide_form("${_}_rate", "${_}_description", "${_}_taxnumber");
  296. }
  297. }
  298. sub form_footer {
  299. $form->{invtotal} = $form->{invsubtotal};
  300. $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
  301. $taxincluded = "";
  302. if ($form->{taxaccounts}) {
  303. $taxincluded = qq|
  304. <tr height="5"></tr>
  305. <tr>
  306. <td align=right>
  307. <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td><th align=left>|.$locale->text('Tax Included').qq|</th>
  308. </tr>
  309. |;
  310. }
  311. if (!$form->{taxincluded}) {
  312. my @taxes = Tax::init_taxes($form, $form->{taxaccounts});
  313. $form->{invtotal} += Tax::calculate_taxes(\@taxes, $form,
  314. $form->{invsubtotal}, 0);
  315. foreach my $item (@taxes) {
  316. my $taccno = $item->account;
  317. $form->{"${taccno}_total"} = $form->format_amount(\%myconfig,
  318. $item->value, 2, 0);
  319. $tax .= qq|
  320. <tr>
  321. <th align=right>$form->{"${taccno}_description"}</th>
  322. <td align=right>$form->{"${taccno}_total"}</td>
  323. </tr>
  324. | if $item->value;
  325. }
  326. $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
  327. $subtotal = qq|
  328. <tr>
  329. <th align=right>|.$locale->text('Subtotal').qq|</th>
  330. <td align=right>$form->{invsubtotal}</td>
  331. </tr>
  332. |;
  333. }
  334. @column_index = qw(paid memo source cctrack AR_paid);
  335. $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
  336. $column_data{source} = "<th>".$locale->text('Source')."</th>";
  337. $column_data{memo} = "<th>".$locale->text('Memo')."</th>";
  338. $column_data{cctrack} = "<th>".$locale->text('Credit Card Track')."</th>";
  339. $column_data{AR_paid} = "<th>&nbsp;</th>";
  340. print qq|
  341. <tr>
  342. <td>
  343. <table width=100%>
  344. <tr valign=top>
  345. <td>
  346. <table>
  347. <tr>
  348. |;
  349. for (@column_index) { print "$column_data{$_}\n"; }
  350. print qq|
  351. </tr>
  352. |;
  353. $totalpaid = 0;
  354. $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
  355. my $memoselect = qq|<SELECT name="MEMONAME">|;
  356. for (sort keys %pos_sources){
  357. $memoselect .= qq|<option value="$_">$pos_sources{$_}</option>|;
  358. }
  359. $memoselect .= qq|</SELECT>|;
  360. for $i (1 .. $form->{paidaccounts}) {
  361. $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
  362. $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
  363. # format amounts
  364. $totalpaid += $form->{"paid_$i"};
  365. $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
  366. $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
  367. if ($form->{"paid__$i"}){
  368. $column_data{paid} = qq|<td><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  369. } else {
  370. $column_data{paid} = qq|<td><input accesskey='n' name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
  371. }
  372. $column_data{source} = qq|<td><input name="source_$i" size=10 value="$form->{"source_$i"}"></td>|;
  373. $column_data{memo} = qq|<td>$memoselect</td>|;
  374. $column_data{memo} =~ s/MEMONAME/memo_$i/;
  375. if (!$form->{"memo_$i"}){
  376. $form->{"memo_$i"} = $pos_source_default;
  377. }
  378. my $memval = $form->{"memo_$i"};
  379. $column_data{memo} =~ s/(option value="$memval")/$1 SELECTED/;
  380. $column_data{cctrack} = qq|<td><input type="text" name="cctrack_$i"
  381. value="|.$form->{"cctrack_$i"}.qq|" size="3"></td>|;
  382. if ($pos_config{"coa_prefix"}){
  383. if (!$form->{"AR_paid_$i"}){
  384. $form->{"AR_paid_$i"} =
  385. $pos_config{"coa_prefix"}.'.'.$pos_config{"till"};
  386. }
  387. $column_data{AR_paid} = qq|<input type=hidden name="AR_paid_$i"
  388. value='$form->{"AR_paid_$i"}'>|;
  389. } else {
  390. $column_data{AR_paid} = qq|<td><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
  391. }
  392. print qq|
  393. <tr>
  394. |;
  395. for (@column_index) { print "$column_data{$_}\n"; }
  396. print qq|
  397. </tr>
  398. |;
  399. $form->hide_form("cleared_$i", "exchangerate_$i", "forex_$i");
  400. }
  401. $form->{change} = 0;
  402. if ($totalpaid > $form->{invtotal}) {
  403. $form->{change} = $totalpaid - $form->{invtotal};
  404. }
  405. $form->{oldchange} = $form->{change};
  406. $form->{change} = $form->format_amount(\%myconfig, $form->{change}, 2, 0);
  407. $form->{totalpaid} = $form->format_amount(\%myconfig, $totalpaid, 2);
  408. $form->{oldinvtotal} = $form->{invtotal};
  409. $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
  410. print qq|
  411. <tr>
  412. <th align=right>|.$locale->text('Change').qq|</th>
  413. <th>$form->{change}</th>
  414. </tr>
  415. </table>
  416. </td>
  417. <td align=right>
  418. <table>
  419. $subtotal
  420. $tax
  421. <tr>
  422. <th align=right>|.$locale->text('Total').qq|</th>
  423. <td align=right>$form->{invtotal}</td>
  424. </tr>
  425. $taxincluded
  426. </table>
  427. </td>
  428. </tr>
  429. </table>
  430. </td>
  431. </tr>
  432. <input type=hidden name=oldtotalpaid value=$totalpaid>
  433. <input type=hidden name=datepaid value=$form->{transdate}>
  434. <tr>
  435. <td>
  436. |;
  437. $form->hide_form(qw(paidaccounts selectAR_paid oldinvtotal change oldchange invtotal));
  438. &print_options;
  439. print qq|
  440. </td>
  441. </tr>
  442. <tr>
  443. <td><hr size=3 noshade></td>
  444. </tr>
  445. </table>
  446. |;
  447. $transdate = $form->datetonum(\%myconfig, $form->{transdate});
  448. $closedto = $form->datetonum(\%myconfig, $form->{closedto});
  449. # type=submit $locale->text('Update')
  450. # type=submit $locale->text('Print')
  451. # type=submit $locale->text('Post')
  452. # type=submit $locale->text('Print and Post')
  453. # type=submit $locale->text('Delete')
  454. if (! $form->{readonly}) {
  455. %button = ('Update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
  456. 'Print' => { ndx => 2, key => 'P', value => $locale->text('Print') },
  457. 'Post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  458. 'Print and Post' => { ndx => 4, key => 'R', value => $locale->text('Print and Post') },
  459. 'Delete' => { ndx => 5, key => 'D', value => $locale->text('Delete') },
  460. );
  461. if ($transdate > $closedto) {
  462. if (! $form->{id}) {
  463. delete $button{'Delete'};
  464. }
  465. delete $button{'Print and Post'} unless ${LedgerSMB::Sysconfig::latex};
  466. } else {
  467. for ('Print', 'Post', 'Print and Post', 'Delete') { delete $button{$_} }
  468. }
  469. for (sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button) { $form->print_button(\%button, $_) }
  470. print qq|<p>
  471. <input type=text size=1 value="B" accesskey="B" title="[Alt-B]">\n|;
  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. }
  686. $form->{discount} = $form->format_amount(\%myconfig, $form->{discount} * 100);
  687. $form->{rowcount}--;
  688. $form->{pre} = "<body bgcolor=#ffffff>\n<pre>";
  689. delete $form->{stylesheet};
  690. $form->{cd_open} = $pos_config{rp_cash_drawer_open};
  691. $form->parse_template(\%myconfig, ${LedgerSMB::Sysconfig::userspath});
  692. if ($form->{printed} !~ /$form->{formname}/) {
  693. $form->{printed} .= " $form->{formname}";
  694. $form->{printed} =~ s/^ //;
  695. $form->update_status(\%myconfig);
  696. }
  697. $old_form->{printed} = $form->{printed};
  698. # if we got back here restore the previous form
  699. if ($form->{media} ne 'screen') {
  700. # restore and display form
  701. for (keys %$old_form) { $form->{$_} = $old_form->{$_}; }
  702. $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
  703. for $i (1 .. $form->{paidaccounts}) {
  704. for (qw(paid exchangerate)) { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}); }
  705. }
  706. delete $form->{pre};
  707. if (! $form->{printandpost}) {
  708. $form->{rowcount}--;
  709. #&display_form;
  710. }
  711. }
  712. }
  713. sub print_and_post {
  714. $form->error($locale->text('Select a Printer!')) if ($form->{media} eq 'screen');
  715. $form->{printandpost} = 1;
  716. &print;
  717. &post;
  718. }
  719. sub lookup_partsgroup {
  720. $form->{action} =~ s/\r//;
  721. $form->{action} = substr($form->{action}, 1);
  722. if ($form->{language_code}) {
  723. # get english
  724. foreach $item (split /\n/, $form->{partsgroup}) {
  725. if ($item =~ /$form->{action}/) {
  726. ($partsgroup, $translation) = split /--/, $item;
  727. $form->{action} = $partsgroup;
  728. last;
  729. }
  730. }
  731. }
  732. $form->{"partsgroup_$form->{rowcount}"} = $form->{action};
  733. &update;
  734. }
  735. sub print_options {
  736. $form->{PD}{$form->{type}} = "checked";
  737. print qq|
  738. <input type=hidden name=format value=$form->{format}>
  739. <input type=hidden name=formname value=$form->{type}>
  740. <table width=100%>
  741. <tr>
  742. |;
  743. $media = qq|
  744. <td><input class=radio type=radio name=media value="screen"></td>
  745. <td>|.$locale->text('Screen').qq|</td>|;
  746. if (%{LedgerSMB::Sysconfig::printer}) {
  747. for (keys %{LedgerSMB::Sysconfig::printer}) {
  748. $media .= qq|
  749. <td><input class=radio type=radio name=media value="$_"></td>
  750. <td nowrap>$_</td>
  751. |;
  752. }
  753. }
  754. $media =~ s/(value="\Q$form->{media}\E")/$1 checked/;
  755. print qq|
  756. $media
  757. <td width=99%>&nbsp;</td>|;
  758. if ($form->{printed} =~ /$form->{type}/) {
  759. print qq|
  760. <th>\||.$locale->text('Printed').qq|\|</th>|;
  761. }
  762. print qq|
  763. </tr>
  764. </table>
  765. |;
  766. }
  767. sub receipts {
  768. $form->{title} = $locale->text('Receipts');
  769. $form->{db} = 'ar';
  770. RP->paymentaccounts(\%myconfig, \%$form);
  771. $paymentaccounts = "";
  772. for (@{ $form->{PR} } ) { $paymentaccounts .= "$_->{accno} "; }
  773. if (@{ $form->{all_years} }) {
  774. # accounting years
  775. $form->{selectaccountingyear} = "<option>\n";
  776. for (@{ $form->{all_years} }) { $form->{selectaccountingyear} .= qq|<option>$_\n|; }
  777. $form->{selectaccountingmonth} = "<option>\n";
  778. for (sort keys %{ $form->{all_month} }) { $form->{selectaccountingmonth} .= qq|<option value=$_>|.$locale->text($form->{all_month}{$_}).qq|\n|; }
  779. $selectfrom = qq|
  780. <tr>
  781. <th align=right>|.$locale->text('Period').qq|</th>
  782. <td colspan=3>
  783. <select name=month>$form->{selectaccountingmonth}</select>
  784. <select name=year>$form->{selectaccountingyear}</select>
  785. <input name=interval class=radio type=radio value=0 checked>&nbsp;|.$locale->text('Current').qq|
  786. <input name=interval class=radio type=radio value=1>&nbsp;|.$locale->text('Month').qq|
  787. <input name=interval class=radio type=radio value=3>&nbsp;|.$locale->text('Quarter').qq|
  788. <input name=interval class=radio type=radio value=12>&nbsp;|.$locale->text('Year').qq|
  789. </td>
  790. </tr>
  791. |;
  792. }
  793. $form->header;
  794. print qq|
  795. <body>
  796. <form method=post action=$form->{script}>
  797. <input type=hidden name=title value="$form->{title}">
  798. <input type=hidden name=paymentaccounts value="$paymentaccounts">
  799. <input type=hidden name=till value=1>
  800. <input type=hidden name=subtotal value=1>
  801. <table width=100%>
  802. <tr>
  803. <th class=listtop>$form->{title}</th>
  804. </tr>
  805. <tr height="5"></tr>
  806. <tr>
  807. <td>
  808. <table>
  809. <input type=hidden name=nextsub value=list_payments>
  810. <tr>
  811. <th align=right>|.$locale->text('From').qq|</th>
  812. <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
  813. <th align=right>|.$locale->text('To').qq|</th>
  814. <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
  815. </tr>
  816. $selectfrom
  817. <input type=hidden name=sort value=transdate>
  818. <input type=hidden name=db value=$form->{db}>
  819. </table>
  820. </td>
  821. </tr>
  822. <tr>
  823. <td><hr size=3 noshade></td>
  824. </tr>
  825. </table>
  826. <br>
  827. <input type="hidden" name="path" value="$form->{path}">
  828. <input type="hidden" name="login" value="$form->{login}">
  829. <input type="hidden" name="sessionid" value="$form->{sessionid}">
  830. <button type="submit" class="submit" name="action" value="continue">|.$locale->text('Continue').qq|</button>
  831. |;
  832. if ($form->{lynx}) {
  833. require "bin/menu.pl";
  834. &menubar;
  835. }
  836. print qq|
  837. </form>
  838. </body>
  839. </html>
  840. |;
  841. }