This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / sql-ledger / bin / mozilla / arapprn.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 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #======================================================================
23
24 #
25 # printing routines for ar, ap
26 #
27
28 # any custom scripts for this one
29 if (-f "$form->{path}/custom_arapprn.pl") {
30     eval { require "$form->{path}/custom_arapprn.pl"; };
31 }
32 if (-f "$form->{path}/$form->{login}_arapprn.pl") {
33     eval { require "$form->{path}/$form->{login}_arapprn.pl"; };
34 }
35
36
37 1;
38 # end of main
39
40
41 sub print {
42   
43   if ($form->{AP}) {
44     $form->{vc} = "vendor";
45     $form->{ARAP} = "AP";
46     $invfld = "vinumber";
47   }
48   if ($form->{AR}) {
49     $form->{vc} = "customer";
50     $form->{ARAP} = "AR";
51     $invfld = "sinumber";
52   }
53   
54   if ($form->{media} !~ /screen/) {
55     $form->error($locale->text('Select postscript or PDF!')) if $form->{format} !~ /(postscript|pdf)/;
56   }
57
58   if ($form->{media} eq 'screen' && $form->{formname} =~ /(check|receipt)/) {
59     $form->error($locale->text('Select postscript or PDF!')) if $form->{format} !~ /(postscript|pdf)/;
60   }
61   
62   if (! $form->{invnumber}) {
63     $form->{invnumber} = $form->update_defaults(\%myconfig, $invfld);
64     if ($form->{media} eq 'screen') {
65       if ($form->{media} eq 'screen') {
66         &update;
67         exit;
68       }
69     }
70   }
71
72   if ($form->{formname} =~ /(check|receipt)/) {
73     if ($form->{media} ne 'screen') {
74       map { delete $form->{$_} } qw(action header);
75       $form->{invtotal} = $form->{oldinvtotal};
76       
77       foreach $key (keys %$form) {
78         $form->{$key} =~ s/&/%26/g;
79         $form->{previousform} .= qq|$key=$form->{$key}&|;
80       }
81       chop $form->{previousform};
82       $form->{previousform} = $form->escape($form->{previousform}, 1);
83     }
84
85     if ($form->{paidaccounts} > 1) {
86       if ($form->{"paid_$form->{paidaccounts}"}) {
87         &update;
88         exit;
89       } elsif ($form->{paidaccounts} > 2) {
90         # select payment
91         &select_payment;
92         exit;
93       }
94     } else {
95       $form->error($locale->text('Nothing to print!'));
96     }
97     
98   }
99
100   &{ "print_$form->{formname}" }(1);
101
102 }
103
104
105 sub print_check {
106   my ($i) = @_;
107   
108   $display_form = ($form->{display_form}) ? $form->{display_form} : "display_form";
109
110   if ($form->{"paid_$i"}) {
111     @a = ();
112     
113     if (exists $form->{longformat}) {
114       $form->{"datepaid_$i"} = $locale->date(\%myconfig, $form->{"datepaid_$i"}, $form->{longformat});
115     }
116
117     push @a, "source_$i", "memo_$i";
118     $form->format_string(@a);
119   }
120
121   $form->{amount} = $form->{"paid_$i"};
122   map { $form->{$_} = $form->{"${_}_$i"} } qw(datepaid source memo);
123
124   
125   &{ "$form->{vc}_details" };
126   @a = qw(name address1 address2 city state zipcode country);
127  
128   foreach $item (qw(invnumber ordnumber)) {
129     $temp{$item} = $form->{$item};
130     delete $form->{$item};
131     push(@{ $form->{$item} }, $temp{$item});
132   }
133   push(@{ $form->{invdate} }, $form->{transdate});
134   push(@{ $form->{due} }, $form->format_amount(\%myconfig, $form->{oldinvtotal}, 2));
135   push(@{ $form->{paid} }, $form->{"paid_$i"});
136
137   use SL::CP;
138   if ($form->{language_code}) {
139     $c = new CP $form->{language_code};
140   } else {
141     $c = new CP $myconfig->{countrycode};
142   } 
143   $c->init;
144   ($whole, $form->{decimal}) = split /\./, $form->{amount};
145   $form->{decimal} .= "00";
146   $form->{decimal} = substr($form->{decimal}, 0, 2);
147   $form->{text_amount} = $c->num2text($whole); 
148   
149   ($form->{employee}) = split /--/, $form->{employee};
150
151   $form->{notes} =~ s/^\s+//g;
152   push @a, "notes";
153
154   map { $form->{$_} = $myconfig{$_} } (qw(company address tel fax businessnumber));
155   push @a, qw(company address tel fax businessnumber);
156   
157   $form->format_string(@a);
158
159   $form->{templates} = "$myconfig{templates}";
160   $form->{IN} = ($form->{formname} eq 'transaction') ? lc $form->{ARAP} . "_$form->{formname}.html" : "$form->{formname}.html";
161
162   if ($form->{format} =~ /(postscript|pdf)/) {
163     $form->{IN} =~ s/html$/tex/;
164   }
165
166   if ($form->{media} !~ /(screen|queue)/) {
167     $form->{OUT} = "| $printer{$form->{media}}";
168     
169     $reference = $form->{invnumber};
170     
171     if ($form->{formname} =~ /(check|receipt)/) {
172       $form->{rowcount} = 1;
173       $form->{"id_1"} = $form->{id};
174       $form->{"checked_1"} = 1;
175       $form->{account} = $form->{"$form->{ARAP}_paid_$i"};
176       $reference = $form->{"source_$i"};
177     }
178       
179     if ($form->{printed} !~ /$form->{formname}/) {
180
181       $form->{printed} .= " $form->{formname}";
182       $form->{printed} =~ s/^ //;
183
184       $form->update_status(\%myconfig);
185     }
186
187     %audittrail = ( tablename   => lc $form->{ARAP},
188                     reference   => $reference,
189                     formname    => $form->{formname},
190                     action      => 'printed',
191                     id          => $form->{id} );
192     
193     %status = ();
194     map { $status{$_} = $form->{$_} } qw(printed queued audittrail);
195     
196     $status{audittrail} .= $form->audittrail("", \%myconfig, \%audittrail);
197
198   }
199
200   if ($form->{media} eq 'queue') {
201     %queued = split / /, $form->{queued};
202     
203     $reference = $form->{invnumber};
204  
205     if ($form->{formname} =~ /(check|receipt)/) {
206       $form->{rowcount} = 1;
207       $form->{"id_1"} = $form->{id};
208       $form->{"checked_1"} = 1;
209       $form->{account} = $form->{"$form->{ARAP}_paid_$i"};
210       $reference = $form->{"source_$i"};
211     }
212  
213     if ($filename = $queued{$form->{formname}}) {
214       $form->{queued} =~ s/$form->{formname} $filename//;
215       unlink "$spool/$filename";
216       $filename =~ s/\..*$//g;
217     } else {
218       $filename = time;
219       $filename .= $$;
220     }
221
222     $filename .= ($form->{format} eq 'postscript') ? '.ps' : '.pdf';
223     $form->{OUT} = ">$spool/$filename";
224
225     $form->{queued} .= " $form->{formname} $filename";
226     $form->{queued} =~ s/^ //;
227
228     # save status
229     $form->update_status(\%myconfig);
230
231     %audittrail = ( tablename   => lc $form->{ARAP},
232                     reference   => $reference,
233                     formname    => $form->{formname},
234                     action      => 'queued',
235                     id          => $form->{id} );
236
237     %status = ();
238     map { $status{$_} = $form->{$_} } qw(printed queued audittrail);
239
240     $status{audittrail} .= $form->audittrail("", \%myconfig, \%audittrail);
241
242   }
243
244   $form->{fileid} = $invnumber;
245   $form->{fileid} =~ s/(\s|\W)+//g;
246
247   $form->parse_template(\%myconfig, $userspath);
248
249   if ($form->{previousform}) {
250   
251     $previousform = $form->unescape($form->{previousform});
252
253     map { delete $form->{$_} } keys %$form;
254
255     foreach $item (split /&/, $previousform) {
256       ($key, $value) = split /=/, $item, 2;
257       $value =~ s/%26/&/g;
258       $form->{$key} = $value;
259     }
260
261     map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
262
263     map { $form->{"amount_$_"} = $form->parse_amount(\%myconfig, $form->{"amount_$_"}) } (1 .. $form->{rowcount});
264     map { $form->{"tax_$_"} = $form->parse_amount(\%myconfig, $form->{"tax_$_"}) } split / /, $form->{taxaccounts};
265
266     for $i (1 .. $form->{paidaccounts}) {
267       map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
268     }
269
270     map { $form->{$_} = $status{$_} } qw(printed queued audittrail);
271
272     &{ "$display_form" };
273     
274   }
275
276 }
277
278
279 sub print_receipt { &print_check; }
280
281
282 sub print_transaction {
283  
284   $display_form = ($form->{display_form}) ? $form->{display_form} : "display_form";
285  
286   if ($form->{media} !~ /screen/) {
287     $old_form = new Form;
288     map { $old_form->{$_} = $form->{$_} } keys %$form;
289   }
290  
291   &{ "$form->{vc}_details" };
292   @a = qw(name address1 address2 city state zipcode country);
293   
294   
295   $form->{invtotal} = 0;
296   foreach $i (1 .. $form->{rowcount} - 1) {
297     ($form->{tempaccno}, $form->{tempaccount}) = split /--/, $form->{"$form->{ARAP}_amount_$i"};
298     ($form->{tempprojectnumber}) = split /--/, $form->{"projectnumber_$i"};
299     
300     $form->format_string(qw(tempaccno tempaccount tempprojectnumber));
301     
302     push(@{ $form->{accno} }, $form->{tempaccno});
303     push(@{ $form->{account} }, $form->{tempaccount});
304     push(@{ $form->{projectnumber} }, $form->{tempprojectnumber});
305
306     push(@{ $form->{amount} }, $form->{"amount_$i"});
307
308     $form->{subtotal} += $form->parse_amount(\%myconfig, $form->{"amount_$i"});
309     
310   }
311
312   foreach $accno (split / /, $form->{taxaccounts}) {
313     if ($form->{"tax_$accno"}) {
314       $tax += $form->parse_amount(\%myconfig, $form->{"tax_$accno"});
315       push(@{ $form->{tax} }, $form->{"tax_$accno"});
316       push(@{ $form->{taxdescription} }, $form->{"${accno}_description"});
317       push(@{ $form->{taxrate} }, $form->{"${accno}_rate"} * 100);
318       push(@{ $form->{taxnumber} }, $form->{"${accno}_taxnumber"});
319     }
320   }
321     
322  
323   push @a, $form->{ARAP};
324   $form->format_string(@a);
325   
326   $form->{paid} = 0;
327   for $i (1 .. $form->{paidaccounts} - 1) {
328
329     if ($form->{"paid_$i"}) {
330     @a = ();
331     $form->{paid} += $form->parse_amount(\%myconfig, $form->{"paid_$i"});
332     
333     if (exists $form->{longformat}) {
334       $form->{"datepaid_$i"} = $locale->date(\%myconfig, $form->{"datepaid_$i"}, $form->{longformat});
335     }
336
337     push @a, "$form->{ARAP}_paid_$i", "source_$i", "memo_$i";
338     $form->format_string(@a);
339     
340     ($accno, $account) = split /--/, $form->{"$form->{ARAP}_paid_$i"};
341     
342     push(@{ $form->{payment} }, $form->{"paid_$i"});
343     push(@{ $form->{paymentdate} }, $form->{"datepaid_$i"});
344     push(@{ $form->{paymentaccount} }, $account);
345     push(@{ $form->{paymentsource} }, $form->{"source_$i"});
346     push(@{ $form->{paymentmemo} }, $form->{"memo_$i"});
347     }
348     
349   }
350
351
352   $form->{invtotal} = $form->{subtotal} + $tax;
353   $form->{total} = $form->{invtotal} - $form->{paid};
354   
355   use SL::CP;
356   if ($form->{language_code}) {
357     $c = new CP $form->{language_code};
358   } else {
359     $c = new CP $myconfig->{countrycode};
360   } 
361   $c->init;
362   ($whole, $form->{decimal}) = split /\./, $form->{invtotal};
363   $form->{decimal} .= "00";
364   $form->{decimal} = substr($form->{decimal}, 0, 2);
365   $form->{text_amount} = $c->num2text($whole); 
366   
367   map { $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, 2) } qw(invtotal subtotal paid total);
368   
369   ($form->{employee}) = split /--/, $form->{employee};
370
371   if (exists $form->{longformat}) {
372     map { $form->{$_} = $locale->date(\%myconfig, $form->{$_}, $form->{longformat}) } ("duedate", "transdate");
373   }
374
375   $form->{notes} =~ s/^\s+//g;
376
377   push @a, ("invnumber", "transdate", "duedate", "notes");
378
379   map { $form->{$_} = $myconfig{$_} } (qw(company address tel fax businessnumber));
380   push @a, qw(company address tel fax businessnumber);
381   
382   $form->format_string(@a);
383
384   $form->{invdate} = $form->{transdate};
385
386   $form->{templates} = "$myconfig{templates}";
387   $form->{IN} = ($form->{formname} eq 'transaction') ? lc $form->{ARAP} . "_$form->{formname}.html" : "$form->{formname}.html";
388
389   if ($form->{format} =~ /(postscript|pdf)/) {
390     $form->{IN} =~ s/html$/tex/;
391   }
392
393   if ($form->{media} !~ /(screen|queue)/) {
394     $form->{OUT} = "| $printer{$form->{media}}";
395     
396     if ($form->{printed} !~ /$form->{formname}/) {
397
398       $form->{printed} .= " $form->{formname}";
399       $form->{printed} =~ s/^ //;
400
401       $form->update_status(\%myconfig);
402     }
403
404     $old_form->{printed} = $form->{printed};
405     
406     %audittrail = ( tablename   => lc $form->{ARAP},
407                     reference   => $form->{"invnumber"},
408                     formname    => $form->{formname},
409                     action      => 'printed',
410                     id          => $form->{id} );
411     
412     $old_form->{audittrail} .= $form->audittrail("", \%myconfig, \%audittrail);
413
414   }
415
416   if ($form->{media} eq 'queue') {
417     %queued = split / /, $form->{queued};
418
419     if ($filename = $queued{$form->{formname}}) {
420       $form->{queued} =~ s/$form->{formname} $filename//;
421       unlink "$spool/$filename";
422       $filename =~ s/\..*$//g;
423     } else {
424       $filename = time;
425       $filename .= $$;
426     }
427
428     $filename .= ($form->{format} eq 'postscript') ? '.ps' : '.pdf';
429     $form->{OUT} = ">$spool/$filename";
430
431     $form->{queued} .= " $form->{formname} $filename";
432     $form->{queued} =~ s/^ //;
433
434     # save status
435     $form->update_status(\%myconfig);
436
437     $old_form->{queued} = $form->{queued};
438
439     %audittrail = ( tablename   => lc $form->{ARAP},
440                     reference   => $form->{invnumber},
441                     formname    => $form->{formname},
442                     action      => 'queued',
443                     id          => $form->{id} );
444     $old_form->{audittrail} .= $form->audittrail("", \%myconfig, \%audittrail);
445
446   }
447
448   $form->{fileid} = $form->{invnumber};
449   $form->{fileid} =~ s/(\s|\W)+//g;
450
451   $form->parse_template(\%myconfig, $userspath);
452
453   if ($old_form) {
454     $old_form->{invnumber} = $form->{invnumber};
455     $old_form->{invtotal} = $form->{invtotal};
456
457     map { delete $form->{$_} } keys %$form;
458     map { $form->{$_} = $old_form->{$_} } keys %$old_form;
459
460     if (! $form->{printandpost}) {
461       map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
462
463       map { $form->{"amount_$_"} = $form->parse_amount(\%myconfig, $form->{"amount_$_"}) } (1 .. $form->{rowcount});
464       map { $form->{"tax_$_"} = $form->parse_amount(\%myconfig, $form->{"tax_$_"}) } split / /, $form->{taxaccounts};
465
466       for $i (1 .. $form->{paidaccounts}) {
467         map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
468       }
469     }
470     
471     &{ "$display_form" };
472
473   }
474 }
475
476
477 sub vendor_details { IR->vendor_details(\%myconfig, \%$form) };
478 sub customer_details { IS->customer_details(\%myconfig, \%$form) };
479
480
481 sub select_payment {
482
483   @column_index = ("ndx", "datepaid", "source", "memo", "paid", "$form->{ARAP}_paid");
484
485   # list payments with radio button on a form
486   $form->header;
487
488   $title = $locale->text('Select payment');
489
490   $column_data{ndx} = qq|<th width=1%>&nbsp;</th>|;
491   $column_data{datepaid} = qq|<th>|.$locale->text('Date').qq|</th>|;
492   $column_data{source} = qq|<th>|.$locale->text('Source').qq|</th>|;
493   $column_data{memo} = qq|<th>|.$locale->text('Memo').qq|</th>|;
494   $column_data{paid} = qq|<th>|.$locale->text('Amount').qq|</th>|;
495   $column_data{"$form->{ARAP}_paid"} = qq|<th>|.$locale->text('Account').qq|</th>|;
496
497   print qq|
498 <body>
499
500 <form method=post action=$form->{script}>
501
502 <table width=100%>
503   <tr>
504     <th class=listtop>$title</th>
505   </tr>
506   <tr space=5></tr>
507   <tr>
508     <td>
509       <table width=100%>
510         <tr class=listheading>|;
511
512   map { print "\n$column_data{$_}" } @column_index;
513   
514   print qq|
515         </tr>
516 |;
517
518   foreach $i (1 .. $form->{paidaccounts} - 1) {
519    $checked = ($i == 1) ? "checked" : "";
520
521    map { $column_data{$_} = qq|<td>$form->{"${_}_$i"}</td>| } @column_index;
522    $column_data{ndx} = qq|<td><input name=ndx class=radio type=radio value=$i $checked></td>|;
523    $column_data{paid} = qq|<td align=right>$form->{"paid_$i"}</td>|;
524     
525     $j++; $j %= 2;
526     print qq|
527         <tr class=listrow$j>|;
528
529     map { print "\n$column_data{$_}" } @column_index;
530
531     print qq|
532         </tr>
533 |;
534
535   }
536   
537   print qq|
538       </table>
539     </td>
540   </tr>
541   <tr>
542     <td><hr size=3 noshade></td>
543   </tr>
544 </table>
545 |;
546
547   $form->{nextsub} = "payment_selected";
548
549   $form->hide_form();
550   
551   print qq|
552
553 <br>
554 <input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">
555 </form>
556
557 </body>
558 </html>
559 |;
560   
561 }
562
563 sub payment_selected {
564
565   &{ "print_$form->{formname}" }($form->{ndx});
566
567 }
568
569
570 sub print_options {
571
572   $form->{PD}{$form->{formname}} = "selected";
573   $form->{DF}{$form->{format}} = "selected";
574
575   if ($form->{selectlanguage}) {
576     $form->{"selectlanguage"} = $form->unescape($form->{"selectlanguage"});
577     $form->{"selectlanguage"} =~ s/ selected//;
578     $form->{"selectlanguage"} =~ s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
579     $lang = qq|<select name=language_code>$form->{selectlanguage}</select>
580     <input type=hidden name=selectlanguage value="|.
581     $form->escape($form->{selectlanguage},1).qq|">|;
582   }
583   
584   $type = qq|<select name=formname>
585           <option value=transaction $form->{PD}{transaction}>|.$locale->text('Transaction');
586
587   if ($form->{AR}) {
588     $type .= qq|
589           <option value=receipt $form->{PD}{receipt}>|.$locale->text('Receipt').qq|</select>|;
590   }
591
592   if ($form->{AP}) {
593     $type .= qq|
594           <option value=check $form->{PD}{check}>|.$locale->text('Check').qq|</select>|;
595   }
596           
597   $media = qq|<select name=media>
598           <option value=screen>|.$locale->text('Screen');
599
600   if (%printer && $latex) {
601     map { $media .= qq| 
602           <option value="$_">$_| } sort keys %printer;
603   }
604
605   $format = qq|<select name=format>
606             <option value=html $form->{DF}{html}>html|;
607             
608   if ($latex) {
609 # disable for now
610 #    $media .= qq|
611 #          <option value="queue">|.$locale->text('Queue');
612     $format .= qq|
613             <option value=postscript $form->{DF}{postscript}>|.$locale->text('Postscript').qq|
614             <option value=pdf $form->{DF}{pdf}>|.$locale->text('PDF');
615   }
616
617   $format .= qq|</select>|;
618   $media .= qq|</select>|;
619   $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
620
621   print qq|
622   <table width=100%>
623     <tr>
624       <td>
625   $type
626   $lang
627   $format
628   $media
629      </td>
630      <td align=right>
631   |;
632
633   if ($form->{printed} =~ /$form->{formname}/) {
634     print $locale->text('Printed').qq|<br>|;
635   }
636
637   if ($form->{queued} =~ /$form->{formname}/) {
638     print $locale->text('Queued');
639   }
640
641   print qq|
642       </td>
643     </tr>
644   </table>
645 |;
646
647 }
648
649
650 sub print_and_post {
651
652   $form->error($locale->text('Select postscript or PDF!')) if $form->{format} !~ /(postscript|pdf)/;
653   $form->error($locale->text('Select Printer or Queue!')) if $form->{media} eq 'screen';
654
655   $form->{printandpost} = 1;
656   $form->{display_form} = "post";
657   &print;
658
659 }
660