This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / old / sql-ledger / bin / mozilla / rp.pl
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (c) 1998-2002
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #  Contributors: Antonio Gallardo <agssa@ibw.com.ni>
10 #                Benjamin Lee <benjaminlee@consultant.com>
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #======================================================================
25 #
26 # module for preparing Income Statement and Balance Sheet
27
28 #======================================================================
29
30
31 use SL::RP;
32
33 1;
34 # end of main
35
36 # this is for our long dates
37 # $locale->text('January')
38 # $locale->text('February')
39 # $locale->text('March')
40 # $locale->text('April')
41 # $locale->text('May ')
42 # $locale->text('June')
43 # $locale->text('July')
44 # $locale->text('August')
45 # $locale->text('September')
46 # $locale->text('October')
47 # $locale->text('November')
48 # $locale->text('December')
49
50 # this is for our short month
51 # $locale->text('Jan')
52 # $locale->text('Feb')
53 # $locale->text('Mar')
54 # $locale->text('Apr')
55 # $locale->text('May')
56 # $locale->text('Jun')
57 # $locale->text('Jul')
58 # $locale->text('Aug')
59 # $locale->text('Sep')
60 # $locale->text('Oct')
61 # $locale->text('Nov')
62 # $locale->text('Dec')
63
64 # $locale->text('Balance Sheet')
65 # $locale->text('Income Statement')
66 # $locale->text('Trial Balance')
67 # $locale->text('AR Aging')
68 # $locale->text('AP Aging')
69 # $locale->text('Tax collected')
70 # $locale->text('Tax paid')
71 # $locale->text('Receipts')
72 # $locale->text('Payments')
73
74
75 sub report {
76
77   %title = ( 'balance_sheet'    => 'Balance Sheet',
78              'income_statement' => 'Income Statement',
79              'trial_balance'    => 'Trial Balance',
80              'ar_aging'         => 'AR Aging',
81              'ap_aging'         => 'AP Aging',
82              'tax_collected'    => 'Tax collected',
83              'tax_paid'         => 'Tax paid',
84              'receipts'         => 'Receipts',
85              'payments'         => 'Payments',
86            );
87   
88   $form->{title} = $locale->text($title{$form->{report}});
89   
90   $form->header;
91   
92   $gifi = qq|
93 <tr>
94   <th align=right>|.$locale->text('Accounts').qq|</th>
95   <td><input name=accounttype class=radio type=radio value=standard checked> |.$locale->text('Standard').qq|
96    
97       <input name=accounttype class=radio type=radio value=gifi> |.$locale->text('GIFI').qq|
98   </td>
99 </tr>
100 |;
101
102   print qq|
103 <body>
104
105 <form method=post action=$form->{script}>
106
107 <input type=hidden name=title value="$form->{title}">
108
109 <table width=100%>
110   <tr>
111     <th class=listtop>$form->{title}</th>
112   </tr>
113   <tr height="5"></tr>
114   <tr>
115     <td>
116       <table>
117 |;
118
119   if ($form->{report} eq "income_statement") {
120     print qq|
121         <input type=hidden name=nextsub value=generate_income_statement>
122         <tr>
123           <th align=right>|.$locale->text('From').qq|</th>
124           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
125           <th align=right>|.$locale->text('to').qq|</th>
126           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
127         </tr>
128         <tr>
129           <th colspan=4>|.$locale->text('Compare to').qq|</th>
130         </tr>
131         <tr>
132           <th align=right>|.$locale->text('From').qq|</th>
133           <td><input name=comparefromdate size=11 title="$myconfig{dateformat}"></td>
134           <th align=right>|.$locale->text('to').qq|</th>
135           <td><input name=comparetodate size=11 title="$myconfig{dateformat}"></td>
136         </tr>
137         <tr>
138           <th align=right>|.$locale->text('Decimalplaces').qq|</th>
139           <td><input name=decimalplaces size=3 maxsize=1></td>
140         </tr>
141       </table>
142     </td>
143   </tr>
144   <tr>
145     <td>
146       <table>
147         <tr>
148           <th align=right nowrap>|.$locale->text('Include in Report').qq|</th>
149           <td colspan=3><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Heading').qq|</td>
150           <td><input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Subtotal').qq|</td>
151         </tr>
152 |;
153   }
154
155
156   if ($form->{report} eq "balance_sheet") {
157     print qq|
158         <input type=hidden name=nextsub value=generate_balance_sheet>
159         <tr>
160           <th align=right>|.$locale->text('as at').qq|</th>
161           <td><input name=asofdate size=11 title="$myconfig{dateformat}" value=$form->{asofdate}></td>
162           <th align=right nowrap>|.$locale->text('Compare to').qq|</th>
163           <td><input name=compareasofdate size=11 title="$myconfig{dateformat}"></td>
164         </tr>
165         <tr>
166           <th align=right>|.$locale->text('Decimalplaces').qq|</th>
167           <td><input name=decimalplaces size=3 maxsize=1></td>
168         </tr>
169       </table>
170     </td>
171   </tr>
172   <tr>
173     <td>
174       <table>
175         <tr>
176           <th align=right nowrap>|.$locale->text('Include in Report').qq|</th>
177           <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Heading').qq|</td>
178           <td><input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Subtotal').qq|</td>
179         </tr>
180 |;
181   }
182
183
184   if ($form->{report} eq "trial_balance") {
185     print qq|
186         <input type=hidden name=nextsub value=generate_trial_balance>
187         <tr>
188           <th align=right>|.$locale->text('From').qq|</th>
189           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
190           <th align=right>|.$locale->text('to').qq|</th>
191           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
192         </tr>
193       </table>
194     </td>
195   </tr>
196   <tr>
197     <td>
198       <table>
199         <tr>
200           <th align=right nowrap>|.$locale->text('Include in Report').qq|</th>
201           <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Heading').qq|
202           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Subtotal').qq|</td>
203         </tr>
204 |;
205   }
206
207   
208   if (($form->{report} eq "tax_paid") || ($form->{report} eq "tax_collected")) {
209     $gifi = "";
210
211     $form->{db} = ($form->{report} eq "tax_collected") ? "ar" : "ap";
212     
213     RP->get_taxaccounts(\%myconfig, \%$form);
214
215     print qq|
216         <input type=hidden name=nextsub value=generate_tax_report>
217         <tr>
218           <th align=right>|.$locale->text('From').qq|</th>
219           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
220           <th align=right>|.$locale->text('to').qq|</th>
221           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
222         </tr>
223         <tr>
224           <th align=right>|.$locale->text('Cash based').qq|</th>
225           <td><input name=cashbased class=checkbox type=checkbox value=Y></td>
226         </tr>
227         <tr>
228           <th align=right>|.$locale->text('Report for').qq|</th>
229           <td>
230 |;
231
232   $checked = "checked";
233   foreach $item (@{ $form->{taxaccounts} }) {
234     ($accno, $description) = split /--/, $item;
235     
236     print qq|<input name=accno class=radio type=radio value=$accno $checked>&nbsp;$description
237
238     <input name="${accno}_description" type=hidden value="$description">|;
239
240     $checked = "";
241   }
242
243   print qq|
244   <input type=hidden name=db value=$form->{db}>
245   <input type=hidden name=sort value=transdate>
246
247           </td>
248         </tr>
249 |;
250
251
252   if (@{ $form->{gifi_taxaccounts} }) {
253     print qq|
254         <tr>
255           <th align=right>|.$locale->text('GIFI').qq|</th>
256           <td>
257 |;
258
259     foreach $item (@{ $form->{gifi_taxaccounts} }) {
260       ($accno, $description) = split /--/, $item;
261       
262       print qq|<input name=accno class=radio type=radio value="gifi_$accno" $checked>&nbsp;$description
263
264       <input name="gifi_${accno}_description" type=hidden value="$description">|;
265
266     }
267
268     print qq|
269           </td>
270         </tr>
271 |;
272   }
273
274
275 print qq|
276       </table>
277     </td>
278   </tr>
279   <tr>
280     <td>
281       <table>
282         <tr>
283           <th align=right>|.$locale->text('Include in Report').qq|</th>
284           <td>
285             <table>
286               <tr>
287                 <td><input name="l_id" class=checkbox type=checkbox value=Y></td>
288                 <td>|.$locale->text('ID').qq|</td>
289                 <td><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
290                 <td>|.$locale->text('Invoice').qq|</td>
291                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
292                 <td>|.$locale->text('Date').qq|</td>
293               </tr>
294               <tr>
295                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
296                 <td>|;
297                 
298   if ($form->{db} eq 'ar') {
299     print $locale->text('Customer');
300   }
301   if ($form->{db} eq 'ap') {
302     print $locale->text('Vendor');
303   }
304   
305   print qq|</td>
306                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y checked></td>
307                 <td>|.$locale->text('Amount').qq|</td>
308                 <td><input name="l_tax" class=checkbox type=checkbox value=Y checked></td>
309                 <td>|.$locale->text('Tax').qq|</td>
310                 <td><input name="l_amount" class=checkbox type=checkbox value=Y></td>
311                 <td>|.$locale->text('Total').qq|</td>
312               </tr>
313               <tr>
314                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
315                 <td>|.$locale->text('Subtotal').qq|</td>
316               </tr>
317             </table>
318           </td>
319         </tr>
320 |;
321
322   }
323
324
325   if (($form->{report} eq "ar_aging") || ($form->{report} eq "ap_aging")) {
326     $gifi = "";
327
328     if ($form->{report} eq 'ar_aging') {
329       $label = $locale->text('Customer');
330       $form->{vc} = 'customer';
331     } else {
332       $label = $locale->text('Vendor');
333       $form->{vc} = 'vendor';
334     }
335       
336     $nextsub = "generate_$form->{report}";
337     
338     # setup vc selection
339     $form->all_vc(\%myconfig, $form->{vc});
340
341     map { $vc .= "<option>$_->{name}--$_->{id}\n" } @{ $form->{"all_$form->{vc}"} };
342     
343     $vc = ($vc) ? qq|<select name=$form->{vc}><option>\n$vc</select>| : qq|<input name=$form->{vc} size=35>|;
344     
345     print qq|
346         <tr>
347           <th align=right>|.$locale->text($label).qq|</th>
348           <td>$vc</td>
349         </tr>
350         <tr>
351           <th align=right>|.$locale->text('to').qq|</th>
352           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
353         </tr>
354         <input type=hidden name=type value=statement>
355         <input type=hidden name=format value=html>
356         <input type=hidden name=media value=screen>
357
358         <input type=hidden name=nextsub value=$nextsub>
359         <input type=hidden name=action value=$nextsub>
360 |;
361   }
362
363 # above action can be removed if there is more than one input field
364
365
366   if (($form->{report} eq "receipts") || ($form->{report} eq "payments")) {
367     $gifi = "";
368
369     $form->{db} = ($form->{report} eq "receipts") ? "ar" : "ap";
370
371     RP->paymentaccounts(\%myconfig, \%$form);
372
373     $selection = "<option>\n";
374     foreach $ref (@{ $form->{PR} }) {
375       $paymentaccounts .= "$ref->{accno} ";
376       $selection .= "<option>$ref->{accno}--$ref->{description}\n";
377     }
378     
379     chop $paymentaccounts;
380
381     print qq|
382         <input type=hidden name=nextsub value=list_payments>
383         <tr>
384           <th align=right nowrap>|.$locale->text('Account').qq|</th>
385           <td colspan=3><select name=account>$selection</select>
386             <input type=hidden name=paymentaccounts value="$paymentaccounts">
387           </td>
388         </tr>
389         <tr>
390           <th align=right>|.$locale->text('From').qq|</th>
391           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
392           <th align=right>|.$locale->text('to').qq|</th>
393           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
394         </tr>
395           <input type=hidden name=db value=$form->{db}>
396           <input type=hidden name=sort value=transdate>
397 |;
398
399   }
400
401
402   print qq|
403
404 $gifi
405
406       </table>
407     </td>
408   </tr>
409   <tr>
410     <td><hr size=3 noshade></td>
411   </tr>
412 </table>
413
414 <br>
415 <input type=hidden name=path value=$form->{path}>
416 <input type=hidden name=login value=$form->{login}>
417 <input type=hidden name=password value=$form->{password}>
418
419 <input type=submit class=submit name=action value="|.$locale->text('Continue').qq|">
420
421 </form>
422
423 </body>
424 </html>
425 |;
426
427 }
428
429
430 sub continue { &{$form->{nextsub}} };
431
432
433 sub generate_income_statement {
434
435   $form->{padding} = "&nbsp;&nbsp;";
436   $form->{bold} = "<b>";
437   $form->{endbold} = "</b>";
438   $form->{br} = "<br>";
439
440   RP->income_statement(\%myconfig, \%$form);
441
442
443   $form->{period} = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
444   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
445
446   # if there are any dates construct a where
447   if ($form->{fromdate} || $form->{todate}) {
448     
449     unless ($form->{todate}) {
450       $form->{todate} = $form->current_date(\%myconfig);
451     }
452
453     $longtodate = $locale->date(\%myconfig, $form->{todate}, 1);
454     $shorttodate = $locale->date(\%myconfig, $form->{todate});
455     
456     $longfromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
457     $shortfromdate = $locale->date(\%myconfig, $form->{fromdate});
458     
459     $form->{this_period} = "$shortfromdate<br>\n$shorttodate";
460     $form->{period} = $locale->text('for Period').qq|<br>\n$longfromdate |.$locale->text('to').qq| $longtodate|;
461   }
462
463   if ($form->{comparefromdate} || $form->{comparetodate}) {
464     $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
465     $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate});
466     
467     $longcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 1);
468     $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate});
469     
470     $form->{last_period} = "$shortcomparefromdate<br>\n$shortcomparetodate";
471     $form->{period} .= "<br>\n$longcomparefromdate ".$locale->text('to').qq| $longcomparetodate|;
472   }
473
474  
475   # setup variables for the form
476   @a = qw(company address businessnumber);
477   map { $form->{$_} = $myconfig{$_} } @a;
478   $form->{address} =~ s/\\n/<br>/g;
479
480   $form->{templates} = $myconfig{templates};
481
482   $form->{IN} = "income_statement.html";
483   
484   $form->parse_template;
485
486 }
487
488
489 sub generate_balance_sheet {
490
491   $form->{padding} = "&nbsp;&nbsp;";
492   $form->{bold} = "<b>";
493   $form->{endbold} = "</b>";
494   $form->{br} = "<br>";
495
496   RP->balance_sheet(\%myconfig, \%$form);
497
498   $form->{asofdate} = $form->current_date(\%myconfig) unless $form->{asofdate};
499   $form->{period} = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
500   
501   # define Retained Earnings account
502   $padding = ($form->{l_heading}) ? $form->{padding} : "";
503   push(@{$form->{equity_account}}, $padding.$locale->text('Retained Earnings'));
504
505   $form->{this_period} = $locale->date(\%myconfig, $form->{asofdate});
506   $form->{last_period} = $locale->date(\%myconfig, $form->{compareasofdate});
507
508   $form->{IN} = "balance_sheet.html";
509
510   # setup company variables for the form
511   map { $form->{$_} = $myconfig{$_} } (qw(company address businessnumber nativecurr));
512   $form->{address} =~ s/\\n/<br>/g;
513
514   $form->{templates} = $myconfig{templates};
515           
516   $form->parse_template;
517
518 }
519
520
521 # Antonio Gallardo
522 #
523 # D.S. Feb 16, 2001
524 # included links to display transactions for period entered
525 # added headers and subtotals
526 #
527 sub generate_trial_balance {
528   
529   # get for each account initial balance, debits and credits
530   RP->trial_balance_details(\%myconfig, \%$form);
531
532   $form->{nextsub} = "generate_trial_balance";
533   $form->{title} = $locale->text('Trial Balance');
534   &list_accounts;
535
536 }
537
538
539 sub list_accounts {
540
541   $title = $form->escape($form->{title});
542   
543   if ($form->{projectnumber}) {
544     $options .= $locale->text('Project Number')." : $form->{projectnumber}<br>";
545     $projectnumber = $form->escape($form->{projectnumber});
546   }
547  
548   # if there are any dates
549   if ($form->{fromdate} || $form->{todate}) {
550     if ($form->{fromdate}) {
551       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
552     }
553     if ($form->{todate}) {
554       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
555     }
556     
557     $form->{period} = "$fromdate - $todate";
558   } else {
559     $form->{period} = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
560   }
561   
562   $options .= $form->{period};
563
564   @column_index = qw(accno description begbalance debit credit endbalance);
565
566   $column_header{accno} = qq|<th class=listheading>|.$locale->text('Account').qq|</th>|;
567   $column_header{description} = qq|<th class=listheading>|.$locale->text('Description').qq|</th>|;
568   $column_header{debit} = qq|<th class=listheading>|.$locale->text('Debit').qq|</th>|;
569   $column_header{credit} = qq|<th class=listheading>|.$locale->text('Credit').qq|</th>|;
570   $column_header{begbalance} = qq|<th class=listheading>|.$locale->text('Balance').qq|</th>|;
571   $column_header{endbalance} = qq|<th class=listheading>|.$locale->text('Balance').qq|</th>|;
572
573
574   if ($form->{accounttype} eq 'gifi') {
575     $column_header{accno} = qq|<th class=listheading>|.$locale->text('GIFI').qq|</th>|;
576   }
577   
578
579   $form->header;
580
581   print qq|
582 <body>
583
584 <table width=100%>
585   <tr>
586     <th class=listtop>$form->{title}</th>
587   </tr>
588   <tr height="5"></tr>
589   <tr>
590     <td>$options</td>
591   </tr>
592   <tr>
593     <td>
594       <table width=100%>
595         <tr>|;
596
597   map { print "$column_header{$_}\n" } @column_index;
598
599   print qq|
600         </tr>
601 |;
602
603
604   
605   # sort the whole thing by account numbers and display
606   foreach $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} }) {
607
608     $description = $form->escape($ref->{description});
609     
610     $href = qq|ca.pl?path=$form->{path}&action=list_transactions&accounttype=$form->{accounttype}&login=$form->{login}&password=$form->{password}&fromdate=$form->{fromdate}&todate=$form->{todate}&sort=transdate&l_heading=$form->{l_heading}&l_subtotal=$form->{l_subtotal}&projectnumber=$projectnumber&project_id=$form->{project_id}&title=$title&nextsub=$form->{nextsub}|;
611     
612     if ($form->{accounttype} eq 'gifi') {
613       $href .= "&gifi_accno=$ref->{accno}&gifi_description=$description";
614       $na = $locale->text('N/A');
615       map { $ref->{$_} = $na } qw(accno description) unless $ref->{accno};
616     } else {
617       $href .= "&accno=$ref->{accno}&description=$description";
618     }
619
620     $ml = ($ref->{category} =~ /(A|E)/) ? -1 : 1;
621     
622     $debit = $form->format_amount(\%myconfig, $ref->{debit}, 2, "&nbsp;");
623     $credit = $form->format_amount(\%myconfig, $ref->{credit}, 2, "&nbsp;");
624     $begbalance = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2, "&nbsp;");
625     $endbalance = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2, "&nbsp;");
626
627     next if ($ref->{debit} == 0 && $ref->{credit} == 0);
628     
629     if ($ref->{charttype} eq "H" && $subtotal && $form->{l_subtotal}) {
630       map { $column_data{$_} = "<th>&nbsp;</th>" } qw(accno begbalance endbalance);
631
632       $subtotalbegbalance = $form->format_amount(\%myconfig, $subtotalbegbalance, 2, "&nbsp;");
633       $subtotalendbalance = $form->format_amount(\%myconfig, $subtotalendbalance, 2, "&nbsp;");
634       $subtotaldebit = $form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;");
635       $subtotalcredit = $form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;");
636       $column_data{description} = "<th>$subtotaldescription</th>";
637       $column_data{begbalance} = "<th align=right>$subtotalbegbalance</th>";
638       $column_data{endbalance} = "<th align=right>$subtotalendbalance</th>";
639       $column_data{debit} = "<th align=right>$subtotaldebit</th>";
640       $column_data{credit} = "<th align=right>$subtotalcredit</th>";
641       
642       print qq|
643         <tr class=listsubtotal>
644 |;
645       map { print "$column_data{$_}\n" } @column_index;
646       
647       print qq|
648         </tr>
649 |;
650     }
651  
652     if ($ref->{charttype} eq "H") {
653       $subtotal = 1;
654       $subtotaldescription = $ref->{description};
655       $subtotaldebit = $ref->{debit};
656       $subtotalcredit = $ref->{credit};
657       $subtotalbegbalance = 0;
658       $subtotalendbalance = 0;
659
660       next unless $form->{l_heading};
661       
662       map { $column_data{$_} = "<th>&nbsp;</th>" } qw(accno debit credit begbalance endbalance);
663       $column_data{description} = "<th class=listheading>$ref->{description}</th>";
664     }
665
666     if ($ref->{charttype} eq "A") {
667       $column_data{accno} = "<td><a href=$href>$ref->{accno}</a></td>";
668       $column_data{description} = "<td>$ref->{description}</td>";
669       $column_data{debit} = "<td align=right>$debit</td>";
670       $column_data{credit} = "<td align=right>$credit</td>";
671       $column_data{begbalance} = "<td align=right>$begbalance</td>";
672       $column_data{endbalance} = "<td align=right>$endbalance</td>";
673     
674       $totaldebit += $ref->{debit};
675       $totalcredit += $ref->{credit};
676
677       $subtotalbegbalance += $ref->{balance} * $ml;
678       $subtotalendbalance += ($ref->{balance} + $ref->{amount}) * $ml;
679
680     }
681     
682     if ($ref->{charttype} eq "H") {
683       print qq|
684       <tr class=listheading>
685 |;
686     }
687     if ($ref->{charttype} eq "A") {
688       $i++; $i %= 2;
689       print qq|
690       <tr class=listrow$i>
691 |;
692     }
693     
694     map { print "$column_data{$_}\n" } @column_index;
695     
696     print qq|
697       </tr>
698 |;
699   }
700
701
702   # print last subtotal
703   if ($subtotal && $form->{l_subtotal}) {
704     map { $column_data{$_} = "<th>&nbsp;</th>" } qw(accno begbalance endbalance);
705     $subtotalbegbalance = $form->format_amount(\%myconfig, $subtotalbegbalance, 2, "&nbsp;");
706     $subtotalendbalance = $form->format_amount(\%myconfig, $subtotalendbalance, 2, "&nbsp;");
707     $subtotaldebit = $form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;");
708     $subtotalcredit = $form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;");
709     $column_data{description} = "<th>$subdescription</th>";
710     $column_data{begbalance} = "<th align=right>$subtotalbegbalance</th>";
711     $column_data{endbalance} = "<th align=right>$subtotalendbalance</th>";
712     $column_data{debit} = "<th align=right>$subtotaldebit</th>";
713     $column_data{credit} = "<th align=right>$subtotalcredit</th>";
714     
715     print qq|
716       <tr class=listsubtotal>
717 |;
718     map { print "$column_data{$_}\n" } @column_index;
719     
720     print qq|
721       </tr>
722 |;
723   }
724   
725   $totaldebit = $form->format_amount(\%myconfig, $totaldebit, 2, "&nbsp;");
726   $totalcredit = $form->format_amount(\%myconfig, $totalcredit, 2, "&nbsp;");
727
728   map { $column_data{$_} = "<th>&nbsp;</th>" } qw(accno description begbalance endbalance);
729  
730   $column_data{debit} = qq|<th align=right class=listtotal>$totaldebit</th>|;
731   $column_data{credit} = qq|<th align=right class=listtotal>$totalcredit</th>|;
732   
733   print qq|
734         <tr class=listtotal>
735 |;
736
737   map { print "$column_data{$_}\n" } @column_index;
738
739   print qq|
740         </tr>
741       </table>
742     </td>
743   </tr>
744   <tr>
745     <td><hr size=3 noshade></td>
746   </tr>
747 </table>
748
749 </body>
750 </html>
751 |;
752
753 }
754
755
756
757 sub generate_ar_aging {
758
759   # split customer
760   ($form->{customer}) = split(/--/, $form->{customer});
761
762   $form->{ct} = "customer";
763   $form->{arap} = "ar";
764
765   $form->{callback} = qq|$form->{script}?path=$form->{path}&action=generate_ar_aging&login=$form->{login}&password=$form->{password}&todate=$form->{todate}&customer=|.$form->escape($form->{customer});
766
767   RP->aging(\%myconfig, \%$form);
768   &aging;
769   
770 }
771
772
773 sub generate_ap_aging {
774   
775   # split vendor
776   ($form->{vendor}) = split(/--/, $form->{vendor});
777
778   $form->{ct} = "vendor";
779   $form->{arap} = "ap";
780   
781   $form->{callback} = qq|$form->{script}?path=$form->{path}&action=generate_ap_aging&login=$form->{login}&password=$form->{password}&todate=$form->{todate}&vendor=|.$form->escape($form->{vendor});
782
783   RP->aging(\%myconfig, \%$form);
784   &aging;
785   
786 }
787
788
789 sub aging {
790
791
792   $form->header;
793
794   $column_header{statement} = qq|<th class=listheading>&nbsp;</th>|;
795   $column_header{ct} = qq|<th class=listheading>|.$locale->text(ucfirst $form->{ct}).qq|</th>|;
796   $column_header{invnumber} = qq|<th class=listheading>|.$locale->text('Invoice').qq|</th>|;
797   $column_header{transdate} = qq|<th class=listheading>|.$locale->text('Date').qq|</th>|;
798   $column_header{duedate} = qq|<th class=listheading>|.$locale->text('Due').qq|</th>|;
799   $column_header{c0} = qq|<th class=listheading>|.$locale->text('Current').qq|</th>|;
800   $column_header{c30} = qq|<th class=listheading>30</th>|;
801   $column_header{c60} = qq|<th class=listheading>60</th>|;
802   $column_header{c90} = qq|<th class=listheading>90</th>|;
803   
804   @column_index = (qw(statement ct invnumber transdate duedate c0 c30 c60 c90));
805
806   
807   if ($form->{arap} eq 'ar') {
808     if ($form->{customer}) {
809       $option .= "\n<br>" if $option;
810       $option .= $form->{customer};
811     }
812   }
813   if ($form->{arap} eq 'ap') {
814     shift @column_index;
815     if ($form->{vendor}) {
816       $option .= "\n<br>" if $option;
817       $option .= $form->{vendor};
818     }
819   }
820   
821   $todate = $locale->date(\%myconfig, $form->{todate}, 1);
822   $option .= "\n<br>" if $option;
823   $option .= $locale->text('for Period')." ".$locale->text('to')." $todate";
824
825   print qq|
826 <body>
827
828 <form method=post action=$form->{script}>
829
830 <table width=100%>
831   <tr>
832     <th class=listtop>$form->{title}</th>
833   </tr>
834   <tr height="5"></tr>
835   <tr>
836     <td>$option</td>
837   </tr>
838   <tr>
839     <td>
840       <table width=100%>
841         <tr class=listheading>
842 |;
843
844   map { print "$column_header{$_}\n" } @column_index;
845   
846   print qq|
847         </tr>
848 |;
849
850
851   $ctid = 0;
852   $subtotal = 0;
853   $i = 0;
854
855   foreach $ref (@{ $form->{AG} }) {
856
857     if ($ctid != $ref->{ctid}) {
858
859       $i++;
860
861       if ($subtotal) {
862         $c0subtotal = $form->format_amount(\%myconfig, $c0subtotal, 2, "&nbsp");
863         $c30subtotal = $form->format_amount(\%myconfig, $c30subtotal, 2, "&nbsp");
864         $c60subtotal = $form->format_amount(\%myconfig, $c60subtotal, 2, "&nbsp");
865         $c90subtotal = $form->format_amount(\%myconfig, $c90subtotal, 2, "&nbsp");
866       }
867       
868       $column_data{ct} = qq|<th>&nbsp;</th>|;
869       $column_data{invnumber} = qq|<th>&nbsp;</th>|;
870       $column_data{transdate} = qq|<th>&nbsp;</th>|;
871       $column_data{duedate} = qq|<th>&nbsp;</th>|;
872       $column_data{c0} = qq|<th class=listsubtotal align=right>$c0subtotal</th>|;
873       $column_data{c30} = qq|<th class=listsubtotal align=right>$c30subtotal</th>|;
874       $column_data{c60} = qq|<th class=listsubtotal align=right>$c60subtotal</th>|;
875       $column_data{c90} = qq|<th class=listsubtotal align=right>$c90subtotal</th>|;
876      
877       if ($subtotal) {
878         # print subtotals
879         print qq|
880         <tr class=listsubtotal>
881 |;
882
883         map { print "$column_data{$_}\n" } @column_index;
884
885         $column_data{statement} = qq|<td>&nbsp;</td>|;
886
887         print qq|
888         </tr>
889 |;
890       }
891    
892       $subtotal = 1;
893
894       $c0subtotal = 0;
895       $c30subtotal = 0;
896       $c60subtotal = 0;
897       $c90subtotal = 0;
898
899       $column_data{ct} = qq|<td>$ref->{name}</td>|;
900       $column_data{statement} = qq|<td><input name="statement_$i" type=checkbox class=checkbox value=1 $ref->{checked}>
901       <input type=hidden name="$form->{ct}_id_$i" value=$ref->{ctid}>
902       </td>|;
903     }
904
905     $c0subtotal += $ref->{c0};
906     $c30subtotal += $ref->{c30};
907     $c60subtotal += $ref->{c60};
908     $c90subtotal += $ref->{c90};
909
910     $c0total += $ref->{c0};
911     $c30total += $ref->{c30};
912     $c60total += $ref->{c60};
913     $c90total += $ref->{c90};
914
915     $ref->{c0} = $form->format_amount(\%myconfig, $ref->{c0}, 2, "&nbsp;");
916     $ref->{c30} = $form->format_amount(\%myconfig, $ref->{c30}, 2, "&nbsp;");
917     $ref->{c60} = $form->format_amount(\%myconfig, $ref->{c60}, 2, "&nbsp;");
918     $ref->{c90} = $form->format_amount(\%myconfig, $ref->{c90}, 2, "&nbsp;");
919  
920     $href = qq|$ref->{module}.pl?path=$form->{path}&action=edit&id=$ref->{id}&login=$form->{login}&password=$form->{password}&callback=|.$form->escape($form->{callback});
921     
922     $column_data{invnumber} = qq|<td><a href=$href>$ref->{invnumber}</a></td>|;
923     $column_data{transdate} = qq|<td>$ref->{transdate}</td>|;
924     $column_data{duedate} = qq|<td>$ref->{duedate}&nbsp;</td>|;
925     $column_data{c0} = qq|<td align=right>$ref->{c0}</td>|;
926     $column_data{c30} = qq|<td align=right>$ref->{c30}</td>|;
927     $column_data{c60} = qq|<td align=right>$ref->{c60}</td>|;
928     $column_data{c90} = qq|<td align=right>$ref->{c90}</td>|;
929     
930     $j++; $j %= 2;
931     print qq|
932         <tr class=listrow$j>
933 |;
934
935     map { print "$column_data{$_}\n" } @column_index;
936
937     print qq|
938         </tr>
939 |;
940  
941     $column_data{ct} = qq|<td>&nbsp;</td>|;
942     $column_data{statement} = qq|<td>&nbsp;</td>|;
943
944     $ctid = $ref->{ctid};
945
946   }
947   
948   # print subtotals
949   $c0subtotal = $form->format_amount(\%myconfig, $c0subtotal, 2, "&nbsp;");
950   $c30subtotal = $form->format_amount(\%myconfig, $c30subtotal, 2, "&nbsp;");
951   $c60subtotal = $form->format_amount(\%myconfig, $c60subtotal, 2, "&nbsp;");
952   $c90subtotal = $form->format_amount(\%myconfig, $c90subtotal, 2, "&nbsp;");
953   
954   print qq|
955         <tr class=listsubtotal>
956 |;
957
958   map { $column_data{$_} = qq|<th>&nbsp;</th>| } @column_index;
959
960   $column_data{c0} = qq|<th class=listsubtotal align=right>$c0subtotal</th>|;
961   $column_data{c30} = qq|<th class=listsubtotal align=right>$c30subtotal</th>|;
962   $column_data{c60} = qq|<th class=listsubtotal align=right>$c60subtotal</th>|;
963   $column_data{c90} = qq|<th class=listsubtotal align=right>$c90subtotal</th>|;
964
965   map { print "$column_data{$_}\n" } @column_index;
966  
967   
968   print qq|
969         </tr>
970         <tr class=listtotal>
971 |;
972
973   $c0total = $form->format_amount(\%myconfig, $c0total, 2, "&nbsp;");
974   $c30total = $form->format_amount(\%myconfig, $c30total, 2, "&nbsp;");
975   $c60total = $form->format_amount(\%myconfig, $c60total, 2, "&nbsp;");
976   $c90total = $form->format_amount(\%myconfig, $c90total, 2, "&nbsp;");
977   
978   $column_data{c0} = qq|<th class=listtotal align=right>$c0total</th>|;
979   $column_data{c30} = qq|<th class=listtotal align=right>$c30total</th>|;
980   $column_data{c60} = qq|<th class=listtotal align=right>$c60total</th>|;
981   $column_data{c90} = qq|<th class=listtotal align=right>$c90total</th>|;
982
983  
984   map { print "$column_data{$_}\n" } @column_index;
985
986   
987   print qq|
988           <input type=hidden name=rowcount value=$i>
989         </tr>
990       </table>
991     </td>
992   </tr>
993   <tr>
994     <td>
995 |;
996
997   &print_options if ($form->{arap} eq 'ar');
998
999   print qq|
1000     </td>
1001   </tr>
1002   <tr>
1003     <td><hr size=3 noshade></td>
1004   </tr>
1005 </table>
1006 |;
1007
1008   if ($form->{arap} eq 'ar') {
1009     print qq|
1010 <input type=hidden name=todate value=$form->{todate}>
1011
1012 <input type=hidden name=title value="$form->{title}">
1013
1014 <input type=hidden name=arap value=$form->{arap}>
1015 <input type=hidden name=ct value=$form->{ct}>
1016 <input type=hidden name=$form->{ct} value="$form->{$form->{ct}}">
1017
1018 <input type=hidden name=path value=$form->{path}>
1019 <input type=hidden name=login value=$form->{login}>
1020 <input type=hidden name=password value=$form->{password}>
1021   
1022 <br>
1023 <input class=submit type=submit name=action value="|.$locale->text('Select all').qq|">
1024 <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
1025 <input class=submit type=submit name=action value="|.$locale->text('E-mail').qq|">
1026 |;
1027   }
1028
1029   print qq|
1030 </form>
1031
1032 </body>
1033 </html>
1034 |;
1035
1036 }
1037
1038
1039 sub select_all {
1040
1041   RP->aging(\%myconfig, \%$form);
1042
1043   map { $_->{checked} = "checked" } @{ $form->{AG} };
1044
1045   &aging;
1046
1047 }
1048
1049
1050 sub print_options {
1051
1052   $form->{sendmode} = "attachment";
1053   $form->{copies} = 2 unless $form->{copies};
1054   
1055   $form->{PD}{$form->{type}} = "checked";
1056   $form->{DF}{$form->{format}} = "checked";
1057   $form->{OP}{$form->{media}} = "checked";
1058   $form->{SM}{$form->{sendmode}} = "checked";
1059   
1060
1061   if ($form->{media} eq 'email') {
1062     $email = qq|
1063         <td><input class=radio type=radio name=sendmode value=attachment $form->{SM}{attachment}> |.$locale->text('Attachment')
1064         .qq| <input class=radio type=radio name=sendmode value=inline $form->{SM}{inline}> |.$locale->text('In-line').qq|</td>
1065 |;
1066   } else {
1067     $screen = qq|
1068         <td><input class=radio type=radio name=media value=screen $form->{OP}{screen}></td>
1069         <td>|.$locale->text('Screen').qq|</td>
1070 |;
1071   }
1072
1073   print qq|
1074 <table width=100%>
1075   <tr valign=top>
1076     <td><input class=radio type=radio name=type value=statement $form->{PD}{statement}></td><td>|.$locale->text('Statement').qq|</td>
1077     <td><input class=radio type=radio name=format value=html $form->{DF}{html}></td>
1078     <td>html</td>
1079 |;
1080
1081   if ($latex) {
1082       print qq|
1083     <td><input class=radio type=radio name=format value=postscript $form->{DF}{postscript}></td>
1084     <td>|.$locale->text('Postscript').qq|</td>
1085     <td><input class=radio type=radio name=format value=pdf $form->{DF}{pdf}></td>
1086     <td>|.$locale->text('PDF').qq|</td>
1087 |;
1088   }
1089
1090   print qq|
1091     $screen
1092 |;
1093
1094   if ($screen) {
1095     if ($myconfig{printer} && $latex) {
1096       print qq|
1097     <td><input class=radio type=radio name=media value=printer $form->{OP}{printer}></td>
1098     <td>|.$locale->text('Printer')
1099     .qq| (|.$locale->text('Copies')
1100     .qq| <input name=copies size=2 value=$form->{copies}>)</td>
1101 |;
1102     }
1103   }
1104
1105   print qq|
1106     $email
1107   </tr>
1108 </table>
1109 |;
1110
1111 }
1112
1113
1114 sub e_mail {
1115
1116   # get name and email addresses
1117   for $i (1 .. $form->{rowcount}) {
1118     if ($form->{"statement_$i"}) {
1119       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1120       RP->get_customer(\%myconfig, \%$form);
1121       $selected = 1;
1122       last;
1123     }
1124   }
1125
1126   $form->error($locale->text('Nothing selected!')) unless $selected;
1127
1128   if ($myconfig{admin}) {
1129     $bcc = qq|
1130           <th align=right nowrap=true>|.$locale->text('Bcc').qq|</th>
1131           <td><input name=bcc size=30 value="$form->{bcc}"></td>
1132 |;
1133   }
1134
1135   $title = $locale->text('E-mail Statement to')." $form->{$form->{ct}}";
1136
1137   $form->{media} = "email";
1138   
1139   $form->header;
1140
1141   print qq|
1142 <body>
1143
1144 <form method=post action=$form->{script}>
1145
1146 <table width=100%>
1147   <tr class=listtop>
1148     <th class=listtop>$title</th>
1149   </tr>
1150   <tr height="5"></tr>
1151   <tr>
1152     <td>
1153       <table width=100%>
1154         <tr>
1155           <th align=right nowrap>|.$locale->text('E-mail').qq|</th>
1156           <td><input name=email size=30 value="$form->{email}"></td>
1157           <th align=right nowrap>|.$locale->text('Cc').qq|</th>
1158           <td><input name=cc size=30 value="$form->{cc}"></td>
1159         </tr>
1160         <tr>
1161           <th align=right nowrap>|.$locale->text('Subject').qq|</th>
1162           <td><input name=subject size=30 value="$form->{subject}"></td>
1163           $bcc
1164         </tr>
1165       </table>
1166     </td>
1167   </tr>
1168   <tr>
1169     <td>
1170       <table width=100%>
1171         <tr>
1172           <th align=left nowrap>|.$locale->text('Message').qq|</th>
1173         </tr>
1174         <tr>
1175           <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
1176         </tr>
1177       </table>
1178     </td>
1179   </tr>
1180   <tr>
1181     <td>
1182 |;
1183
1184   &print_options;
1185
1186   map { delete $form->{$_} } qw(action email cc bcc subject message type sendmode format);
1187
1188   # save all other variables
1189   foreach $key (keys %$form) {
1190     $form->{$key} =~ s/"/&quot;/g;
1191     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1192   }
1193
1194   print qq|
1195     </td>
1196   </tr>
1197   <tr>
1198     <td><hr size=3 noshade></td>
1199   </tr>
1200 </table>
1201
1202 <input type=hidden name=nextsub value=send_email>
1203
1204 <br>
1205 <input name=action class=submit type=submit value="|.$locale->text('Continue').qq|">
1206 </form>
1207
1208 </body>
1209 </html>
1210 |;
1211
1212 }
1213
1214
1215 sub send_email {
1216
1217   $form->{OUT} = "$sendmail";
1218
1219   $form->{subject} = $locale->text('Statement').qq| - $form->{todate}| unless $form->{subject};
1220   
1221   RP->aging(\%myconfig, \%$form);
1222   
1223   $form->{"statement_1"} = 1;
1224
1225   &print_form;
1226   
1227   $form->redirect($locale->text('Statement sent to')." $form->{$form->{ct}}");
1228
1229 }
1230
1231
1232
1233 sub print {
1234   
1235   if ($form->{media} eq 'printer') {
1236     $form->error($locale->text('Select postscript or PDF!')) if ($form->{format} !~ /(postscript|pdf)/);
1237   }
1238   
1239   for $i (1 .. $form->{rowcount}) {
1240     if ($form->{"statement_$i"}) {
1241       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1242       $selected = 1;
1243       last;
1244     }
1245   }
1246
1247   $form->error($locale->text('Nothing selected!')) unless $selected;
1248      
1249  
1250   if ($form->{media} eq 'printer') {
1251     $form->{OUT} = "| $myconfig{printer}";
1252     $form->{"$form->{ct}_id"} = "";
1253   } else {
1254     $form->{"statement_1"} = 1;
1255   }
1256   
1257   RP->aging(\%myconfig, \%$form);
1258  
1259   &print_form;
1260
1261   $form->redirect($locale->text('Statements sent to printer!')) if ($form->{media} eq 'printer');
1262
1263 }
1264
1265
1266 sub print_form {
1267   
1268   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1269
1270   $form->{templates} = "$myconfig{templates}";
1271  
1272   # setup variables for the form
1273   @a = qw(company address businessnumber tel fax);
1274   map { $form->{$_} = $myconfig{$_} } @a;
1275   $form->format_string(@a);
1276   
1277   $form->{IN} = "$form->{type}.html";
1278
1279   if ($form->{format} eq 'postscript') {
1280     $form->{postscript} = 1;
1281     $form->{IN} =~ s/html$/tex/;
1282   }
1283   if ($form->{format} eq 'pdf') {
1284     $form->{pdf} = 1;
1285     $form->{IN} =~ s/html$/tex/;
1286   }
1287
1288   $i = 0;
1289   while (@{ $form->{AG} }) {
1290
1291     $ref = shift @{ $form->{AG} };
1292     
1293     if ($ctid != $ref->{ctid}) {
1294       
1295       $ctid = $ref->{ctid};
1296       $i++;
1297
1298       if ($form->{"statement_$i"}) {
1299         
1300         @a = (name, addr1, addr2, addr3, addr4, contact, "$form->{ct}phone", "$form->{ct}fax");
1301         map { $form->{$_} = $ref->{$_} } @a;
1302         $form->format_string(@a);
1303
1304         $form->{$form->{ct}} = $form->{name};
1305         $form->{"$form->{ct}_id"} = $ref->{ctid};
1306         
1307         map { $form->{$_} = () } qw(invnumber invdate duedate);
1308         $form->{total} = 0;
1309         foreach $item (qw(c0 c30 c60 c90)) {
1310           $form->{$item} = ();
1311           $form->{"${item}total"} = 0;
1312         }
1313
1314         &statement_details($ref);
1315         
1316         while ($ref) {
1317
1318           if (scalar (@{ $form->{AG} }) > 0) {
1319             # one or more left to go
1320             if ($ctid == $form->{AG}->[0]->{ctid}) {
1321               $ref = shift @{ $form->{AG} };
1322               &statement_details($ref);
1323               # any more?
1324               $ref = scalar (@{ $form->{AG} });
1325             } else {
1326               $ref = 0;
1327             }
1328           } else {
1329             # set initial ref to 0
1330             $ref = 0;
1331           }
1332
1333         }
1334         
1335         map { $form->{"${_}total"} = $form->format_amount(\%myconfig, $form->{"${_}total"}, 2) } (c0, c30, c60, c90, "");
1336
1337         $form->parse_template(\%myconfig, $userspath);
1338         
1339       }
1340     }
1341   }
1342
1343 }
1344
1345
1346 sub statement_details {
1347   my ($ref) = @_;
1348   
1349   push @{ $form->{invnumber} }, $ref->{invnumber};
1350   push @{ $form->{invdate} }, $ref->{transdate};
1351   push @{ $form->{duedate} }, $ref->{duedate};
1352   
1353   foreach $item (qw(c0 c30 c60 c90)) {
1354     $ref->{$item} = $form->round_amount($ref->{$item} / $ref->{exchangerate}, 2);
1355     $form->{"${item}total"} += $ref->{$item};
1356     $form->{total} += $ref->{$item};
1357     push @{ $form->{$item} }, $form->format_amount(\%myconfig, $ref->{$item}, 2);
1358   }
1359
1360 }
1361  
1362
1363 sub generate_tax_report {
1364
1365   RP->tax_report(\%myconfig, \%$form);
1366
1367   $descvar = "$form->{accno}_description";
1368   $description = $form->escape($form->{$descvar});
1369   
1370   # construct href
1371   $href = "$form->{script}?path=$form->{path}&action=generate_tax_report&login=$form->{login}&password=$form->{password}&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&accno=$form->{accno}&$descvar=$description";
1372
1373   # construct callback
1374   $callback = $href;
1375
1376  
1377   @columns = $form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1378
1379   foreach $item (@columns) {
1380     if ($form->{"l_$item"} eq "Y") {
1381       push @column_index, $item;
1382
1383       # add column to href and callback
1384       $callback .= "&l_$item=Y";
1385       $href .= "&l_$item=Y";
1386     }
1387   }
1388
1389   if ($form->{l_subtotal} eq 'Y') {
1390     $callback .= "&l_subtotal=Y";
1391     $href .= "&l_subtotal=Y";
1392   }
1393   
1394   
1395   # if there are any dates
1396   if ($form->{fromdate} || $form->{todate}) {
1397     if ($form->{fromdate}) {
1398       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
1399     }
1400     if ($form->{todate}) {
1401       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
1402     }
1403     
1404     $form->{period} = "$fromdate - $todate";
1405   } else {
1406     $form->{period} = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1407   }
1408
1409   $form->{title} = $locale->text('GIFI')." - " if ($form->{accno} =~ /^gifi_/);
1410   $form->{title} = qq|$form->{"$form->{accno}_description"} |;
1411   if ($form->{db} eq 'ar') {
1412     $form->{title} .= $locale->text('collected on sales');
1413     $name = $locale->text('Customer');
1414     $invoice = 'is.pl';
1415     $arap = 'ar.pl';
1416   }
1417   if ($form->{db} eq 'ap') {
1418     $form->{title} .= $locale->text('paid on purchases');
1419     $name = $locale->text('Vendor');
1420     $invoice = 'ir.pl';
1421     $arap = 'ap.pl';
1422   }
1423
1424  
1425   $column_header{id} = qq|<th><a class=listheading href=$href&sort=id>|.$locale->text('ID').qq|</th>|;
1426   $column_header{invnumber} = qq|<th><a class=listheading href=$href&sort=invnumber>|.$locale->text('Invoice').qq|</th>|;
1427   $column_header{transdate} = qq|<th><a class=listheading href=$href&sort=transdate>|.$locale->text('Date').qq|</th>|;
1428   $column_header{netamount} = qq|<th class=listheading>|.$locale->text('Amount').qq|</th>|;
1429   $column_header{tax} = qq|<th class=listheading>|.$locale->text('Tax').qq|</th>|;
1430   $column_header{amount} = qq|<th class=listheading>|.$locale->text('Total').qq|</th>|;
1431   
1432   $column_header{name} = qq|<th><a class=listheading href=$href&sort=name>$name</th>|;
1433
1434   
1435   $form->header;
1436
1437   print qq|
1438 <body>
1439
1440 <table width=100%>
1441   <tr>
1442     <th class=listtop colspan=$colspan>$form->{title}</th>
1443   </tr>
1444   <tr height="5"></tr>
1445   <tr>
1446     <td>$form->{period}</td>
1447   </tr>
1448   <tr>
1449     <td>
1450       <table width=100%>
1451         <tr class=listheading>
1452 |;
1453
1454   map { print "$column_header{$_}\n" } @column_index;
1455   
1456   print qq|
1457         </tr>
1458 |;
1459
1460   # add sort and escape callback
1461   $callback = $form->escape($callback . "&sort=$form->{sort}");
1462     
1463   if (@{ $form->{TR} }) {
1464     $sameitem = $form->{TR}->[0]->{$form->{sort}};
1465   }
1466
1467   foreach $ref (@{ $form->{TR} }) {
1468
1469     $module = ($ref->{invoice}) ? $invoice : $arap;
1470     
1471     if ($form->{l_subtotal} eq 'Y') {
1472       if ($sameitem ne $ref->{$form->{sort}}) {
1473         &tax_subtotal;
1474         $sameitem = $ref->{$form->{sort}};
1475       }
1476     }
1477
1478     $totalnetamount += $ref->{netamount};
1479     $totaltax += $ref->{tax};
1480     $ref->{amount} = $ref->{netamount} + $ref->{tax};
1481
1482     $subtotalnetamount += $ref->{netamount};
1483     $subtotaltax += $ref->{tax};
1484     
1485     map { $ref->{$_} = $form->format_amount(\%myconfig, $ref->{$_}, 2, "&nbsp;"); } qw(netamount tax amount);
1486     
1487     $column_data{id} = qq|<td>$ref->{id}</td>|;
1488     $column_data{invnumber} = qq|<td><a href=$module?path=$form->{path}&action=edit&id=$ref->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{invnumber}</a></td>|;
1489     $column_data{transdate} = qq|<td>$ref->{transdate}</td>|;
1490     $column_data{name} = qq|<td>$ref->{name}&nbsp;</td>|;
1491     
1492     map { $column_data{$_} = qq|<td align=right>$ref->{$_}</td>| } qw(netamount tax amount);
1493
1494     $i++; $i %= 2;
1495     print qq|
1496         <tr class=listrow$i>
1497 |;
1498
1499     map { print "$column_data{$_}\n" } @column_index;
1500
1501     print qq|
1502         </tr>
1503 |;
1504  
1505   }
1506  
1507   if ($form->{l_subtotal} eq 'Y') {
1508     &tax_subtotal;
1509   }
1510
1511   
1512   map { $column_data{$_} = qq|<th>&nbsp;</th>| } @column_index;
1513   
1514   print qq|
1515         </tr>
1516         <tr class=listtotal>
1517 |;
1518
1519   $total = $form->format_amount(\%myconfig, $totalnetamount + $totaltax, 2, "&nbsp;");
1520   $totalnetamount = $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;");
1521   $totaltax = $form->format_amount(\%myconfig, $totaltax, 2, "&nbsp;");
1522   
1523   $column_data{netamount} = qq|<th class=listtotal align=right>$totalnetamount</th>|;
1524   $column_data{tax} = qq|<th class=listtotal align=right>$totaltax</th>|;
1525   $column_data{amount} = qq|<th class=listtotal align=right>$total</th>|;
1526  
1527   map { print "$column_data{$_}\n" } @column_index;
1528  
1529     
1530   print qq|
1531         </tr>
1532       </table>
1533     </td>
1534   </tr>
1535   <tr>
1536     <td><hr size=3 noshade></td>
1537   </tr>
1538 </table>
1539
1540 </body>
1541 </html>
1542 |;
1543
1544 }
1545
1546
1547 sub tax_subtotal {
1548
1549   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1550
1551   $subtotalnetamount = $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;");
1552   $subtotaltax = $form->format_amount(\%myconfig, $subtotaltax, 2, "&nbsp;");
1553   $subtotal = $form->format_amount(\%myconfig, $subtotalnetamount + $subtotaltax, 2, "&nbsp;");
1554   
1555   $column_data{netamount} = "<th class=listsubtotal align=right>$subtotalnetamount</th>";
1556   $column_data{tax} = "<th class=listsubtotal align=right>$subtotaltax</th>";
1557   $column_data{amount} = "<th class=listsubtotal align=right>$subtotal</th>";
1558
1559   $subtotalnetamount = 0;
1560   $subtotaltax = 0;
1561   
1562   print qq|
1563         <tr class=listsubtotal>
1564 |;
1565   map { print "\n$column_data{$_}" } @column_index;
1566
1567   print qq|
1568         </tr>
1569 |;
1570   
1571 }
1572
1573
1574
1575 sub list_payments {
1576
1577   if ($form->{account}) {
1578     ($form->{paymentaccounts}) = split /--/, $form->{account};
1579   }
1580
1581   RP->payments(\%myconfig, \%$form);
1582   
1583   @columns = $form->sort_columns(qw(transdate invnumber name paid source));
1584
1585   # construct href
1586   $href = "$form->{script}?path=$form->{path}&action=list_payments&login=$form->{login}&password=$form->{password}&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&title=".$form->escape($form->{title})."&account=".$form->escape($form->{account});
1587
1588   $form->{paymentaccounts} =~ s/ /%20/g;
1589   $href .= "&paymentaccounts=$form->{paymentaccounts}";
1590
1591
1592   # construct callback
1593   $form->{callback} = "$href&sort=$form->{sort}";
1594   $callback = $form->escape($form->{callback});
1595
1596   
1597   $column_header{name} = "<th><a class=listheading href=$href&sort=name>".$locale->text('Description')."</a></th>";
1598   $column_header{invnumber} = "<th><a class=listheading href=$href&sort=invnumber>".$locale->text('Invoice')."</a></th>";
1599   $column_header{transdate} = "<th><a class=listheading href=$href&sort=transdate>".$locale->text('Date')."</a></th>";
1600   $column_header{paid} = "<th class=listheading>".$locale->text('Amount')."</a></th>";
1601   $column_header{source} = "<th><a class=listheading href=$href&sort=source>".$locale->text('Source')."</a></th>";
1602   
1603
1604   if ($form->{fromdate}) {
1605     $option .= "\n<br>" if ($option);
1606     $option .= $locale->text('From')."&nbsp;".$locale->date(\%myconfig, $form->{fromdate}, 1);
1607   }
1608   if ($form->{todate}) {
1609     $option .= "\n<br>" if ($option);
1610     $option .= $locale->text('to')."&nbsp;".$locale->date(\%myconfig, $form->{todate}, 1);
1611   }
1612
1613   @column_index = @columns;
1614   $colspan = $#column_index + 1;
1615
1616   $form->header;
1617
1618   print qq|
1619 <body>
1620
1621 <table width=100%>
1622   <tr>
1623     <th class=listtop>$form->{title}</th>
1624   </tr>
1625   <tr height="5"></tr>
1626   <tr>
1627     <td>$option</td>
1628   </tr>
1629   <tr>
1630     <td>
1631       <table width=100%>
1632         <tr class=listheading>
1633 |;
1634
1635   map { print "\n$column_header{$_}" } @column_index;
1636
1637   print qq|
1638         </tr>
1639 |;
1640
1641   
1642   foreach $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
1643
1644     print qq|
1645         <tr>
1646           <th colspan=$colspan align=left>$ref->{accno}--$ref->{description}</th>
1647         </tr>
1648 |;
1649   
1650     foreach $payment (@{ $form->{$ref->{id}} }) {
1651       
1652       $module = $payment->{module};
1653       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
1654       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
1655       
1656       $href = qq|${module}.pl?path=$form->{path}&action=edit&id=$payment->{id}&login=$form->{login}&password=$form->{password}&callback=$callback|;
1657     
1658       
1659       $column_data{name} = "<td>$payment->{name}&nbsp;</td>";
1660       $column_data{invnumber} = qq|<td><a href=$href>$payment->{invnumber}</a></td>|;
1661       $column_data{transdate} = "<td>$payment->{transdate}&nbsp;</td>";
1662       $column_data{paid} = "<td align=right>".$form->format_amount(\%myconfig, $payment->{paid}, 2, "&nbsp;")."</td>";
1663       $column_data{source} = "<td>$payment->{source}&nbsp;</td>";
1664
1665       $subtotalpaid += $payment->{paid};
1666       $totalpaid += $payment->{paid};
1667        
1668       $i++; $i %= 2;
1669       print qq|
1670         <tr class=listrow$i>
1671 |;
1672
1673       map { print "\n$column_data{$_}" } @column_index;
1674
1675       print qq|
1676         </tr>
1677 |;
1678
1679     }
1680
1681     # print subtotals
1682     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1683
1684     $column_data{paid} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalpaid, 2, "&nbsp;")."</th>";
1685      
1686     print qq|
1687         <tr class=listsubtotal>
1688 |;
1689
1690     map { print "\n$column_data{$_}" } @column_index;
1691
1692     print qq|
1693         </tr>
1694 |;
1695
1696     $subtotalpaid = 0;
1697      
1698   }
1699
1700
1701   # print total
1702   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1703
1704   $column_data{paid} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;")."</th>";
1705      
1706   print qq|
1707         <tr class=listtotal>
1708 |;
1709
1710   map { print "\n$column_data{$_}" } @column_index;
1711
1712   print qq|
1713         </tr>
1714
1715       </table>
1716     </td>
1717   </tr>
1718   <tr>
1719     <td><hr size=3 noshade></td>
1720   </tr>
1721 </table>
1722
1723 </body>
1724 </html>
1725 |;
1726
1727 }
1728
1729
1730