This commit was generated by cvs2svn to compensate for changes in r3921,
[freeside.git] / sql-ledger / bin / mozilla / pos.pl
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (c) 2003
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 # Contributors: Steve Doerr <sdoerr907@everestkc.net>
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #=====================================================================
24 #
25 # POS
26 #
27 #=====================================================================
28
29
30 1;
31 # end
32
33
34 sub add {
35
36   $form->{title} = $locale->text('Add POS Invoice');
37
38   $form->{callback} = "$form->{script}?action=$form->{nextsub}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}" unless $form->{callback};
39   
40   &invoice_links;
41
42   $form->{type} =  "pos_invoice";
43   $form->{format} = "txt";
44   $form->{media} = ($myconfig{printer}) ? $myconfig{printer} : "screen";
45   $form->{rowcount} = 0;
46
47   $form->{readonly} = ($myconfig{acs} =~ /POS--Sale/) ? 1 : 0;
48
49   $ENV{REMOTE_ADDR} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
50   $form->{till} = $4;
51
52   map { $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n" } @{ $form->{all_partsgroup} };
53
54   &display_form;
55
56 }
57
58
59 sub openinvoices {
60
61   $ENV{REMOTE_ADDR} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
62   $form->{till} = $4;
63   
64   $form->{sort} = 'transdate';
65
66   map { $form->{$_} = 'Y' } qw(open l_invnumber l_transdate l_name l_amount l_curr l_till l_subtotal);
67
68   if ($myconfig{role} ne 'user') {
69     $form->{l_employee} = 'Y';
70   }
71
72   $form->{title} = $locale->text('Open');
73   &ar_transactions;
74   
75 }
76
77
78 sub edit {
79
80   $form->{title} = $locale->text('Edit POS Invoice');
81
82   $form->{callback} = "$form->{script}?action=$form->{nextsub}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}" unless $form->{callback};
83   
84   &invoice_links;
85   &prepare_invoice;
86
87   $form->{type} =  "pos_invoice";
88   $form->{format} = "txt";
89   $form->{media} = ($myconfig{printer}) ? $myconfig{printer} : "screen";
90
91   $form->{readonly} = ($myconfig{acs} =~ /POS--Sale/) ? 1 : 0;
92
93   map { $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n" } @{ $form->{all_partsgroup} };
94   
95   &display_form;
96
97 }
98
99
100 sub form_header {
101
102   # set option selected
103   foreach $item (qw(AR currency)) {
104     $form->{"select$item"} =~ s/ selected//;
105     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
106   }
107
108   foreach $item (qw(customer department employee)) {
109     $form->{"select$item"} = $form->unescape($form->{"select$item"});
110     $form->{"select$item"} =~ s/ selected//;
111     $form->{"select$item"} =~ s/(<option value="\Q$form->{$item}\E")/$1 selected/;
112   }
113     
114   $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
115
116   if ($form->{oldtotalpaid} > $form->{oldinvtotal}) {
117     $adj = $form->{oldtotalpaid} - $form->{oldinvtotal};
118   }
119   $form->{creditremaining} = $form->{creditremaining} - $adj + $form->{oldchange};
120   
121   $exchangerate = "";
122   if ($form->{currency} ne $form->{defaultcurrency}) {
123     if ($form->{forex}) {
124       $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>|;
125     } else {
126       $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
127     }
128   }
129   $exchangerate .= qq|
130 <input type=hidden name=forex value=$form->{forex}>
131 |;
132
133   if ($form->{selectcustomer}) {
134     $customer = qq|<select name=customer>$form->{selectcustomer}</select>
135                    <input type=hidden name="selectcustomer" value="|.
136                    $form->escape($form->{selectcustomer},1).qq|">|;
137   } else {
138     $customer = qq|<input name=customer value="$form->{customer}" size=35>|;
139   }
140   
141   $department = qq|
142               <tr>
143                 <th align="right" nowrap>|.$locale->text('Department').qq|</th>
144                 <td colspan=3><select name=department>$form->{selectdepartment}</select>
145                 <input type=hidden name=selectdepartment value="|.
146                 $form->escape($form->{selectdepartment},1).qq|">
147                 </td>
148               </tr>
149 | if $form->{selectdepartment};
150         
151   $n = ($form->{creditremaining} < 0) ? "0" : "1";
152
153   if ($form->{selectlanguage}) {
154     if ($form->{language_code} ne $form->{oldlanguage_code}) {
155       # rebuild partsgroup
156       $form->get_partsgroup(\%myconfig, { language_code => $form->{language_code} });
157       $form->{partsgroup} = "";
158       map { $form->{partsgroup} .= "$_->{partsgroup}--$_->{translation}\n" } @{ $form->{all_partsgroup} };
159       $form->{oldlanguage_code} = $form->{language_code};
160     }
161
162       
163     $form->{"selectlanguage"} = $form->unescape($form->{"selectlanguage"});
164     $form->{"selectlanguage"} =~ s/ selected//;
165     $form->{"selectlanguage"} =~ s/(<option value="\Q$form->{language_code}\E")/$1 selected/; 
166     $lang = qq|
167               <tr>
168                 <th align=right>|.$locale->text('Language').qq|</th>
169                 <td colspan=3><select name=language_code>$form->{selectlanguage}</select></td>
170               </tr>
171     <input type=hidden name=oldlanguage_code value=$form->{oldlanguage_code}>
172     <input type=hidden name=selectlanguage value="|.
173     $form->escape($form->{selectlanguage},1).qq|">|;
174   }
175  
176
177   $form->header;
178
179  
180   print qq|
181 <body>
182
183 <form method=post action=$form->{script}>
184
185 <input type=hidden name=id value=$form->{id}>
186
187 <input type=hidden name=till value=$form->{till}>
188
189 <input type=hidden name=type value=$form->{type}>
190 <input type=hidden name=media value=$form->{media}>
191 <input type=hidden name=format value=$form->{format}>
192
193 <input type=hidden name=printed value="$form->{printed}">
194
195 <input type=hidden name=title value="$form->{title}">
196 <input type=hidden name=vc value="customer">
197
198 <input type=hidden name=discount value=$form->{discount}>
199 <input type=hidden name=creditlimit value=$form->{creditlimit}>
200 <input type=hidden name=creditremaining value=$form->{creditremaining}>
201
202 <input type=hidden name=closedto value=$form->{closedto}>
203 <input type=hidden name=locked value=$form->{locked}>
204
205
206 <table width=100%>
207   <tr class=listtop>
208     <th class=listtop>$form->{title}</font></th>
209   </tr>
210   <tr height="5"></tr>
211   <tr>
212     <td>
213       <table width=100%>
214         <tr valign=top>
215           <td>
216             <table>
217               <tr>
218                 <th align=right nowrap>|.$locale->text('Customer').qq|</th>
219                 <td>$customer</td>
220                 <input type=hidden name=customer_id value=$form->{customer_id}>
221                 <input type=hidden name=oldcustomer value="$form->{oldcustomer}"> 
222               </tr>
223               <tr>
224                 <td></td>
225                 <td colspan=3>
226                   <table>
227                     <tr>
228                       <th nowrap>|.$locale->text('Credit Limit').qq|</th>
229                       <td>$form->{creditlimit}</td>
230                       <th nowrap>|.$locale->text('Remaining').qq|</th>
231                       <td class="plus$n">|.$form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0").qq|</font></td>
232                     </tr>
233                   </table>
234                 </td>
235               </tr>
236               $discount
237               <tr>
238                 <th align=right nowrap>|.$locale->text('Record in').qq|</th>
239                 <td><select name=AR>$form->{selectAR}</select></td>
240                 <input type=hidden name=selectAR value="$form->{selectAR}">
241               </tr>
242               $department
243             </table>
244           </td>
245           <td>
246             <table>
247               <tr>
248                 <th align=right nowrap>|.$locale->text('Salesperson').qq|</th>
249                 <td colspan=3><select name=employee>$form->{selectemployee}</select></td>
250                 <input type=hidden name=selectemployee value="|.
251                 $form->escape($form->{selectemployee},1).qq|">
252               </tr>
253               <tr>
254                 <th align=right nowrap>|.$locale->text('Currency').qq|</th>
255                 <td><select name=currency>$form->{selectcurrency}</select></td>
256                 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
257                 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
258                 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
259                 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
260                 $exchangerate
261               </tr>
262               $lang
263             </table>
264           </td>
265         <input type=hidden name=invnumber value=$form->{invnumber}>
266         <input type=hidden name=transdate value=$form->{transdate}>
267         <input type=hidden name=duedate value=$form->{duedate}>
268         </tr>
269       </table>
270     </td>
271   </tr>
272   <tr>
273     <td>
274     </td>
275   </tr>
276
277
278 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
279 |;
280
281   foreach $item (split / /, $form->{taxaccounts}) {
282     print qq|
283 <input type=hidden name="${item}_rate" value="$form->{"${item}_rate"}">
284 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
285 <input type=hidden name="${item}_taxnumber" value="$form->{"${item}_taxnumber"}">
286 |;
287   }
288
289 }
290
291
292
293 sub form_footer {
294
295   $form->{invtotal} = $form->{invsubtotal};
296   
297   foreach $item (split / /, $form->{taxaccounts}) {
298     if ($form->{"${item}_base"}) {
299       $form->{"${item}_total"} = $form->round_amount($form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
300       $form->{invtotal} += $form->{"${item}_total"};
301       $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2, 0);
302       
303       $tax .= qq|
304             <tr>
305               <th align=right>$form->{"${item}_description"}</th>
306               <td align=right>$form->{"${item}_total"}</td>
307             </tr>
308 |;
309     }
310   }
311
312
313
314   $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
315
316   $subtotal = qq|
317               <tr>
318                 <th align=right>|.$locale->text('Subtotal').qq|</th>
319                 <td align=right>$form->{invsubtotal}</td>
320               </tr>
321 |;
322
323
324   $totalpaid = 0;
325   
326   $form->{paidaccounts} = 1;
327   $i = 1;
328   
329   $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
330   $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
331   
332   # format amounts
333   $totalpaid += $form->{"paid_$i"};
334   $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
335   $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
336   
337   $form->{change} = 0;
338   if ($totalpaid > $form->{invtotal}) {
339     $form->{change} = $totalpaid - $form->{invtotal};
340   }
341   $form->{oldchange} = $form->{change};
342   $form->{change} = $form->format_amount(\%myconfig, $form->{change}, 2, 0);
343   $form->{totalpaid} = $form->format_amount(\%myconfig, $totalpaid, 2);
344
345  
346   $form->{oldinvtotal} = $form->{invtotal};
347   $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
348   
349  
350   print qq|
351
352 <input type=hidden name="exchangerate_$i" value=$form->{"exchangerate"}>
353 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
354
355   <tr>
356     <td>
357       <table width=100%>
358         <tr valign=bottom>
359           <td>
360             <table>
361               <tr>
362                 <td></td>
363                 <th>|.$locale->text('Paid').qq|</th>
364                 <th>|.$locale->text('Source').qq|</th>
365                 <th>|.$locale->text('Memo').qq|</th>
366                 <th>|.$locale->text('Account').qq|</th>
367               </tr>
368               <tr>
369                 <td></td>
370                 <td><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>
371                 <td><input name="source_$i" size=10 value="$form->{"source_$i"}"></td>
372                 <td><input name="memo_$i" size=10 value="$form->{"memo_$i"}"></td>
373                 <td><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>
374               </tr>
375               <tr>
376                 <th align=right>|.$locale->text('Change').qq|</th>
377                 <th>$form->{change}</th>
378               </tr>
379             </table>
380           </td>
381           <td align=right>
382             <table>
383               $subtotal
384               $tax
385               <tr>
386                 <th align=right>|.$locale->text('Total').qq|</th>
387                 <td align=right>$form->{invtotal}</td>
388               </tr>
389               $taxincluded
390             </table>
391           </td>
392         </tr>
393       </table>
394     </td>
395   </tr>
396  
397 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
398 <input type=hidden name=selectAR_paid value="$form->{selectAR_paid}">
399 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
400 <input type=hidden name=oldtotalpaid value=$totalpaid>
401
402 <input type=hidden name=change value=$form->{change}>
403 <input type=hidden name=oldchange value=$form->{oldchange}>
404
405 <input type=hidden name=datepaid value=$form->{transdate}>
406 <input type=hidden name=invtotal value=$form->{invtotal}>
407
408 <tr>
409   <td>
410 |;
411
412   &print_options;
413
414   print qq|
415     </td>
416   </tr>
417   <tr>
418     <td><hr size=3 noshade></td>
419   </tr>
420 </table>
421 |;
422
423   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
424   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
425  
426   if (! $form->{readonly}) {
427     if ($transdate > $closedto) {
428       print qq|
429       <input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
430       <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
431       <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
432
433       if ($form->{id}) {
434         print qq|
435       <input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">|;
436       }
437     
438
439       print "<p>\n";
440     
441       if ($form->{partsgroup}) {
442         print qq|
443 <input type=hidden name=nextsub value=lookup_partsgroup>
444 <input type=hidden name=partsgroup value="$form->{partsgroup}">|;
445
446         foreach $item (split /\n/, $form->{partsgroup}) {
447           $item =~ s/\r//;
448           ($partsgroup, $translation) = split /--/, $item;
449           $item = ($translation) ? $translation : $partsgroup;
450           print qq| <input class=submit type=submit name=action value=".$item">\n|;
451         }
452       }
453     }
454   }
455   
456   print qq|
457
458 <input type=hidden name=rowcount value=$form->{rowcount}>
459
460 <input name=callback type=hidden value="$form->{callback}">
461
462 <input type=hidden name=path value=$form->{path}>
463 <input type=hidden name=login value=$form->{login}>
464 <input type=hidden name=sessionid value=$form->{sessionid}>
465
466 </form>
467
468 </body>
469 </html>
470 |;
471
472 }
473
474
475 sub post {
476
477   $form->isblank("customer", $locale->text('Customer missing!'));
478
479   # if oldcustomer ne customer redo form
480   $customer = $form->{customer};
481   $customer =~ s/--.*//g;
482   $customer .= "--$form->{customer_id}";
483   if ($customer ne $form->{oldcustomer}) {
484     &update;
485     exit;
486   }
487   
488   &validate_items;
489
490   $form->isblank("exchangerate", $locale->text('Exchange rate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
491   
492   $paid = $form->parse_amount(\%myconfig, $form->{"paid_1"});
493   $total = $form->parse_amount(\%myconfig, $form->{invtotal});
494   
495   $form->{"paid_1"} = $form->{invtotal} if $paid > $total;
496   
497   ($form->{AR}) = split /--/, $form->{AR};
498
499   $form->{invnumber} = $form->update_defaults(\%myconfig, "sinumber") unless $form->{invnumber};
500   
501   $form->redirect($locale->text('Posted!')) if (IS->post_invoice(\%myconfig, \%$form));
502   $form->error($locale->text('Cannot post transaction!'));
503   
504 }
505
506
507 sub display_row {
508   my $numrows = shift;
509
510   @column_index = qw(partnumber description partsgroup qty unit sellprice discount linetotal);
511     
512   $form->{invsubtotal} = 0;
513
514   map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
515   
516   $column_data{partnumber} = qq|<th class=listheading nowrap>|.$locale->text('Number').qq|</th>|;
517   $column_data{description} = qq|<th class=listheading nowrap>|.$locale->text('Description').qq|</th>|;
518   $column_data{qty} = qq|<th class=listheading nowrap>|.$locale->text('Qty').qq|</th>|;
519   $column_data{unit} = qq|<th class=listheading nowrap>|.$locale->text('Unit').qq|</th>|;
520   $column_data{sellprice} = qq|<th class=listheading nowrap>|.$locale->text('Price').qq|</th>|;
521   $column_data{linetotal} = qq|<th class=listheading nowrap>|.$locale->text('Extended').qq|</th>|;
522   $column_data{discount} = qq|<th class=listheading nowrap>%</th>|;
523   
524   print qq|
525   <tr>
526     <td>
527       <table width=100%>
528         <tr class=listheading>|;
529
530   map { print "\n$column_data{$_}" } @column_index;
531
532   print qq|
533         </tr>
534 |;
535
536   $exchangerate = $form->parse_amount(\%myconfig, $form->{exchangerate});
537   $exchangerate = ($exchangerate) ? $exchangerate : 1;
538   
539   for $i (1 .. $numrows) {
540     # undo formatting
541     map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(qty discount sellprice);
542
543     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
544     $dec = length $dec;
545     $decimalplaces = ($dec > 2) ? $dec : 2;
546
547     if (($form->{"qty_$i"} != $form->{"oldqty_$i"}) || ($form->{currency} ne $form->{oldcurrency})) {
548 # check for a pricematrix
549       foreach $item (split / /, $form->{"pricematrix_$i"}) {
550         ($q, $p) = split /:/, $item;
551         if ($p && $form->{"qty_$i"} > $q) {
552           $form->{"sellprice_$i"} = $form->round_amount($p / $exchangerate, $decimalplaces);
553         }
554       }
555     }
556     
557     if ($i < $numrows) {
558       if ($form->{"discount_$i"} != $form->{discount} * 100) {
559         $form->{"discount_$i"} = $form->{discount} * 100;
560       }
561     }
562     
563     $discount = $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"}/100, $decimalplaces);
564     $linetotal = $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
565     $linetotal = $form->round_amount($linetotal * $form->{"qty_$i"}, 2);
566
567     map { $form->{"${_}_$i"} = $form->quote($form->{"${_}_$i"}) } qw(partnumber sku description partsgroup unit);
568     
569     $column_data{partnumber} = qq|<td><input name="partnumber_$i" size=20 value="$form->{"partnumber_$i"}"></td>|;
570
571     if (($rows = $form->numtextrows($form->{"description_$i"}, 25, 6)) > 1) {
572       $column_data{description} = qq|<td><textarea name="description_$i" rows=$rows cols=25 wrap=soft>$form->{"description_$i"}</textarea></td>|;
573     } else {
574       $column_data{description} = qq|<td><input name="description_$i" size=30 value="$form->{"description_$i"}"></td>|;
575     }
576
577     $column_data{partsgroup} = qq|<input type=hidden name="partsgroup_$i" value="$form->{"partsgroup_$i"}">|;
578
579     $column_data{qty} = qq|<td align=right><input name="qty_$i" size=5 value=|.$form->format_amount(\%myconfig, $form->{"qty_$i"}).qq|></td>|;
580     $column_data{unit} = qq|<td><input type=hidden name="unit_$i" value="$form->{"unit_$i"}">$form->{"unit_$i"}</td>|;
581     $column_data{sellprice} = qq|<td align=right><input name="sellprice_$i" size=9 value=|.$form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces).qq|></td>|;
582     $column_data{linetotal} = qq|<td align=right>|.$form->format_amount(\%myconfig, $linetotal, 2).qq|</td>|;
583     
584
585     $discount = $form->format_amount(\%myconfig, $form->{"discount_$i"});
586     $column_data{discount} = qq|<td align=right>$discount</td>
587     <input type=hidden name="discount_$i" value=$discount>|;
588     
589     print qq|
590         <tr valign=top>|;
591
592     map { print "\n$column_data{$_}" } @column_index;
593   
594     print qq|
595         </tr>
596
597 <input type=hidden name="id_$i" value=$form->{"id_$i"}>
598 <input type=hidden name="inventory_accno_$i" value=$form->{"inventory_accno_$i"}>
599 <input type=hidden name="income_accno_$i" value=$form->{"income_accno_$i"}>
600 <input type=hidden name="expense_accno_$i" value=$form->{"expense_accno_$i"}>
601 <input type=hidden name="listprice_$i" value="$form->{"listprice_$i"}">
602 <input type=hidden name="assembly_$i" value="$form->{"assembly_$i"}">
603 <input type=hidden name="taxaccounts_$i" value="$form->{"taxaccounts_$i"}">
604 <input type=hidden name="pricematrix_$i" value="$form->{"pricematrix_$i"}">
605 <input type=hidden name="oldqty_$i" value="$form->{"qty_$i"}">
606 <input type=hidden name="sku_$i" value="$form->{"sku_$i"}">
607
608 |;
609
610     map { $form->{"${_}_base"} += $linetotal } (split / /, $form->{"taxaccounts_$i"});
611   
612     $form->{invsubtotal} += $linetotal;
613   }
614
615   print qq|
616       </table>
617     </td>
618   </tr>
619
620 <input type=hidden name=oldcurrency value=$form->{currency}>
621
622 |;
623
624 }
625
626
627 sub print {
628   
629   $paid = $form->parse_amount(\%myconfig, $form->{"paid_1"});
630   $total = $form->parse_amount(\%myconfig, $form->{invtotal});
631
632   $form->{change} = 0;
633   if ($paid > $total) {
634     $form->{paid} = $total - $paid;
635     $form->{"paid_1"} = $form->format_amount(\%myconfig, $paid, 2, 0);
636     $form->{change} = $form->format_amount(\%myconfig, $paid - $total, 2, 0);
637   }
638
639   $old_form = new Form;
640   map { $old_form->{$_} = $form->{$_} } keys %$form;
641   
642   map { $form->{$_} =~ s/--.*//g } qw(employee department);
643   $form->{invdate} = $form->{transdate};
644   $form->{invtime} = scalar localtime;
645
646   &print_form($old_form);
647
648 }
649
650
651 sub print_form {
652   my $old_form = shift;
653   
654   # if oldcustomer ne customer redo form
655   $customer = $form->{customer};
656   $customer =~ s/--.*//g;
657   $customer .= "--$form->{customer_id}";
658   if ($customer ne $form->{oldcustomer}) {
659     &update;
660     exit;
661   }
662  
663  
664   &validate_items;
665
666   &{ "$form->{vc}_details" };
667
668   @a = ();
669   map { push @a, ("partnumber_$_", "description_$_") } (1 .. $form->{rowcount});
670   map { push @a, "${_}_description" } split / /, $form->{taxaccounts};
671   $form->format_string(@a);
672
673   # format payment dates
674   map { $form->{"datepaid_$_"} = $locale->date(\%myconfig, $form->{"datepaid_$_"}) } (1 .. $form->{paidaccounts});
675   
676   IS->invoice_details(\%myconfig, \%$form);
677
678   map { $form->{$_} = $myconfig{$_} } (qw(company address tel fax businessnumber));
679   $form->{username} = $myconfig{name};
680   map { $form->{$_} =~ s/\\n/ /g } qw(company address);
681
682   $form->{templates} = "$myconfig{templates}";
683   $form->{IN} = "$form->{type}.$form->{format}";
684
685   if ($form->{media} !~ /screen/) {
686     $form->{OUT} = "| $printer{$form->{media}}";
687   }
688
689   $form->{discount} = $form->format_amount(\%myconfig, $form->{discount} * 100);
690   
691   $form->{rowcount}--;
692   $form->{pre} = "<body bgcolor=#ffffff>\n<pre>";
693   delete $form->{stylesheet};
694   
695   $form->parse_template(\%myconfig, $userspath);
696
697   if ($form->{printed} !~ /$form->{formname}/) {
698     $form->{printed} .= " $form->{formname}";
699     $form->{printed} =~ s/^ //;
700     
701     $form->update_status(\%myconfig);
702   }
703   $old_form->{printed} = $form->{printed};
704   
705   # if we got back here restore the previous form
706   if ($form->{media} !~ /screen/) {
707     # restore and display form
708     map { $form->{$_} = $old_form->{$_} } keys %$old_form;
709     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate);
710
711     $form->{rowcount}--;
712     for $i (1 .. $form->{paidaccounts}) {
713       map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
714     }
715
716     delete $form->{pre};
717
718     &display_form;
719     exit;
720   }
721
722 }
723
724
725 sub lookup_partsgroup {
726
727   $form->{action} =~ s/\r//;
728   $form->{action} = substr($form->{action}, 1);
729
730   if ($form->{language_code}) {
731     # get english
732     foreach $item (split /\n/, $form->{partsgroup}) {
733       if ($item =~ /$form->{action}/) {
734         ($partsgroup, $translation) = split /--/, $item;
735         $form->{action} = $partsgroup;
736         last;
737       }
738     }
739   }
740   
741   $form->{"partsgroup_$form->{rowcount}"} = $form->{action};
742  
743   &update;
744
745 }
746
747
748
749 sub print_options {
750
751   $form->{PD}{$form->{type}} = "checked";
752   
753   print qq|
754 <input type=hidden name=format value=$form->{format}>
755 <input type=hidden name=formname value=$form->{type}>
756
757 <table width=100%>
758   <tr>
759 |;
760
761  
762   $media = qq|
763     <td><input class=radio type=radio name=media value="screen"></td>
764     <td>|.$locale->text('Screen').qq|</td>|;
765
766   if (%printer) {
767     map { $media .= qq|
768     <td><input class=radio type=radio name=media value="$_"></td>
769     <td nowrap>$_</td>
770 | } keys %printer;
771   }
772
773   $media =~ s/(value="\Q$form->{media}\E")/$1 checked/;
774
775   print qq|
776   $media
777   
778   <td width=99%>&nbsp;</td>|;
779   
780   if ($form->{printed} =~ /$form->{type}/) {
781     print qq|
782     <th>\||.$locale->text('Printed').qq|\|</th>|;
783   }
784   
785   print qq|
786   </tr>
787 </table>
788 |;
789
790 }
791
792
793 sub receipts {
794
795   $form->{title} = $locale->text('Receipts');
796
797   $form->{db} = 'ar';
798   RP->paymentaccounts(\%myconfig, \%$form);
799   
800   map { $paymentaccounts .= "$_->{accno} " } @{ $form->{PR} };
801
802   # accounting years
803   $form->{selectaccountingyear} = "<option>\n";
804   map { $form->{selectaccountingyear} .= qq|<option>$_\n| } @{ $form->{all_years} };
805   $form->{selectaccountingmonth} = "<option>\n";
806   map { $form->{selectaccountingmonth} .= qq|<option value=$_>|.$locale->text($form->{all_month}{$_}).qq|\n| } sort keys %{ $form->{all_month} };
807
808   $selectfrom = qq|
809         <tr>
810         <th align=right>|.$locale->text('Period').qq|</th>
811         <td colspan=3>
812         <select name=month>$form->{selectaccountingmonth}</select>
813         <select name=year>$form->{selectaccountingyear}</select>
814         <input name=interval class=radio type=radio value=0 checked>|.$locale->text('Current').qq|
815         <input name=interval class=radio type=radio value=1>|.$locale->text('Month').qq|
816         <input name=interval class=radio type=radio value=3>|.$locale->text('Quarter').qq|
817         <input name=interval class=radio type=radio value=12>|.$locale->text('Year').qq|
818         </td>
819       </tr>
820 |;
821
822   $form->header;
823   
824   print qq|
825 <body>
826
827 <form method=post action=$form->{script}>
828
829 <input type=hidden name=title value="$form->{title}">
830 <input type=hidden name=paymentaccounts value="$paymentaccounts">
831
832 <input type=hidden name=till value=1>
833 <input type=hidden name=subtotal value=1>
834
835 <table width=100%>
836   <tr>
837     <th class=listtop>$form->{title}</th>
838   </tr>
839   <tr height="5"></tr>
840   <tr>
841     <td>
842       <table>
843       
844         <input type=hidden name=nextsub value=list_payments>
845         
846         <tr>
847           <th align=right>|.$locale->text('From').qq|</th>
848           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
849           <th align=right>|.$locale->text('To').qq|</th>
850           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
851         </tr>
852         $selectfrom
853           <input type=hidden name=sort value=transdate>
854           <input type=hidden name=db value=$form->{db}>
855       </table>
856     </td>
857   </tr>
858   <tr>
859     <td><hr size=3 noshade></td>
860   </tr>
861 </table>
862
863 <br>
864 <input type=hidden name=path value=$form->{path}>
865 <input type=hidden name=login value=$form->{login}>
866 <input type=hidden name=sessionid value=$form->{sessionid}>
867
868 <input type=submit class=submit name=action value="|.$locale->text('Continue').qq|">
869
870 </form>
871
872 </body>
873 </html>
874 |;
875
876 }
877
878