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