This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / sql-ledger / bin / mozilla / rp.pl
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (c) 2001
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 require "$form->{path}/arap.pl";
31
32 use SL::PE;
33 use SL::RP;
34
35 1;
36 # end of main
37
38 # this is for our long dates
39 # $locale->text('January')
40 # $locale->text('February')
41 # $locale->text('March')
42 # $locale->text('April')
43 # $locale->text('May ')
44 # $locale->text('June')
45 # $locale->text('July')
46 # $locale->text('August')
47 # $locale->text('September')
48 # $locale->text('October')
49 # $locale->text('November')
50 # $locale->text('December')
51
52 # this is for our short month
53 # $locale->text('Jan')
54 # $locale->text('Feb')
55 # $locale->text('Mar')
56 # $locale->text('Apr')
57 # $locale->text('May')
58 # $locale->text('Jun')
59 # $locale->text('Jul')
60 # $locale->text('Aug')
61 # $locale->text('Sep')
62 # $locale->text('Oct')
63 # $locale->text('Nov')
64 # $locale->text('Dec')
65
66 # $locale->text('Balance Sheet')
67 # $locale->text('Income Statement')
68 # $locale->text('Trial Balance')
69 # $locale->text('AR Aging')
70 # $locale->text('AP Aging')
71 # $locale->text('Tax collected')
72 # $locale->text('Tax paid')
73 # $locale->text('Receipts')
74 # $locale->text('Payments')
75 # $locale->text('Project Transactions')
76 # $locale->text('Non-taxable Sales')
77 # $locale->text('Non-taxable Purchases')
78
79
80 sub report {
81
82   %title = ( balance_sheet      => 'Balance Sheet',
83              income_statement   => 'Income Statement',
84              trial_balance      => 'Trial Balance',
85              ar_aging           => 'AR Aging',
86              ap_aging           => 'AP Aging',
87              tax_collected      => 'Tax collected',
88              tax_paid           => 'Tax paid',
89              nontaxable_sales   => 'Non-taxable Sales',
90              nontaxable_purchases => 'Non-taxable Purchases',
91              receipts           => 'Receipts',
92              payments           => 'Payments',
93              projects           => 'Project Transactions',
94            );
95   
96   $form->{title} = $locale->text($title{$form->{report}});
97
98   $gifi = qq|
99 <tr>
100   <th align=right>|.$locale->text('Accounts').qq|</th>
101   <td><input name=accounttype class=radio type=radio value=standard checked> |.$locale->text('Standard').qq|
102    
103       <input name=accounttype class=radio type=radio value=gifi> |.$locale->text('GIFI').qq|
104   </td>
105 </tr>
106 |;
107   
108   # get departments
109   $form->all_departments(\%myconfig);
110   if (@{ $form->{all_departments} }) {
111     $form->{selectdepartment} = "<option>\n";
112
113     map { $form->{selectdepartment} .= qq|<option value="$_->{description}--$_->{id}">$_->{description}\n| } (@{ $form->{all_departments} });
114   }
115  
116   $department = qq|
117         <tr>
118           <th align=right nowrap>|.$locale->text('Department').qq|</th>
119           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
120         </tr>
121 | if $form->{selectdepartment};
122
123   # accounting years
124   $form->{selectaccountingyear} = "<option>\n";
125   map { $form->{selectaccountingyear} .= qq|<option>$_\n| } @{ $form->{all_years} };
126
127   $form->{selectaccountingmonth} = "<option>\n";
128   map { $form->{selectaccountingmonth} .= qq|<option value=$_>|.$locale->text($form->{all_month}{$_}).qq|\n| } sort keys %{ $form->{all_month} };
129
130   $selectfrom = qq|
131         <tr>
132           <th align=right>|.$locale->text('Period').qq|</th>
133           <td colspan=3>
134           <select name=month>$form->{selectaccountingmonth}</select>
135           <select name=year>$form->{selectaccountingyear}</select>
136           <input name=interval class=radio type=radio value=0 checked>|.$locale->text('Current').qq|
137           <input name=interval class=radio type=radio value=1>|.$locale->text('Month').qq|
138           <input name=interval class=radio type=radio value=3>|.$locale->text('Quarter').qq|
139           <input name=interval class=radio type=radio value=12>|.$locale->text('Year').qq|
140           </td>
141         </tr>
142 |;
143
144   $selectto = qq|
145         <tr>
146           <th align=right></th>
147           <td>
148           <select name=month>$form->{selectaccountingmonth}</select>
149           <select name=year>$form->{selectaccountingyear}</select>
150           </td>
151         </tr>
152 |;
153
154
155   $summary = qq|
156         <tr>
157           <th></th>
158           <td><input name=summary type=radio class=radio value=1 checked> |.$locale->text('Summary').qq|
159           <input name=summary type=radio class=radio value=0> |.$locale->text('Detail').qq|
160           </td>
161         </tr>
162 |;
163
164   # get projects
165   $form->all_projects(\%myconfig);
166   if (@{ $form->{all_projects} }) {
167     $form->{selectproject} = "<option>\n";
168     map { $form->{selectproject} .= qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n| } @{ $form->{all_projects} };
169
170     $project = qq|
171         <tr>
172           <th align=right nowrap>|.$locale->text('Project').qq|</th>
173           <td colspan=3><select name=projectnumber>$form->{selectproject}</select></td>
174         </tr>|;
175
176   }
177  
178   $form->header;
179  
180   print qq|
181 <body>
182
183 <form method=post action=$form->{script}>
184
185 <input type=hidden name=title value="$form->{title}">
186
187 <table width=100%>
188   <tr>
189     <th class=listtop>$form->{title}</th>
190   </tr>
191   <tr height="5"></tr>
192   <tr>
193     <td>
194       <table>
195       $department
196 |;
197
198   if ($form->{report} eq "projects") {
199     print qq|
200         $project
201         <input type=hidden name=nextsub value=generate_projects>
202         <tr>
203           <th align=right>|.$locale->text('From').qq|</th>
204           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
205           <th align=right>|.$locale->text('To').qq|</th>
206           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
207         </tr>
208         $selectfrom
209       </table>
210     </td>
211   </tr>
212   <tr>
213     <td>
214       <table>
215         <tr>
216           <th align=right nowrap>|.$locale->text('Include in Report').qq|</th>
217           <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Heading').qq|
218           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Subtotal').qq|</td>
219         </tr>
220 |;
221   }
222
223   if ($form->{report} eq "income_statement") {
224     print qq|
225         $project
226         <input type=hidden name=nextsub value=generate_income_statement>
227         <tr>
228           <th align=right>|.$locale->text('From').qq|</th>
229           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
230           <th align=right>|.$locale->text('To').qq|</th>
231           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
232         </tr>
233         <tr>
234           <th align=right>|.$locale->text('Period').qq|</th>
235           <td colspan=3>
236           <select name=frommonth>$form->{selectaccountingmonth}</select>
237           <select name=fromyear>$form->{selectaccountingyear}</select>
238           <input name=interval class=radio type=radio value=0 checked>|.$locale->text('Current').qq|
239           <input name=interval class=radio type=radio value=1>|.$locale->text('Month').qq|
240           <input name=interval class=radio type=radio value=3>|.$locale->text('Quarter').qq|
241           <input name=interval class=radio type=radio value=12>|.$locale->text('Year').qq|
242           </td>
243         </tr>
244         <tr>
245           <th align=right>|.$locale->text('Compare to').qq|</th>
246         </tr>
247         <tr>
248           <th align=right>|.$locale->text('From').qq|</th>
249           <td><input name=comparefromdate size=11 title="$myconfig{dateformat}"></td>
250           <th align=right>|.$locale->text('To').qq|</th>
251           <td><input name=comparetodate size=11 title="$myconfig{dateformat}"></td>
252         </tr>
253         <tr>
254           <th align=right>|.$locale->text('Period').qq|</th>
255           <td>
256           <select name=comparemonth>$form->{selectaccountingmonth}</select>
257           <select name=compareyear>$form->{selectaccountingyear}</select>
258           </td>
259         </tr>
260         <tr>
261           <th align=right>|.$locale->text('Decimalplaces').qq|</th>
262           <td><input name=decimalplaces size=3 value=2></td>
263         </tr>
264       </table>
265     </td>
266   </tr>
267   <tr>
268     <td>
269       <table>
270         <tr>
271           <th align=right>|.$locale->text('Method').qq|</th>
272           <td colspan=3><input name=method class=radio type=radio value=accrual checked>|.$locale->text('Accrual').qq|
273           &nbsp;<input name=method class=radio type=radio value=cash>|.$locale->text('Cash').qq|</td>
274         </tr>
275
276         <tr>
277           <th align=right nowrap>|.$locale->text('Include in Report').qq|</th>
278           <td colspan=3><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Heading').qq|
279           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Subtotal').qq|
280           <input name=l_accno class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Account Number').qq|</td>
281         </tr>
282 |;
283   }
284
285
286   if ($form->{report} eq "balance_sheet") {
287     print qq|
288         <input type=hidden name=nextsub value=generate_balance_sheet>
289         <tr>
290           <th align=right>|.$locale->text('as at').qq|</th>
291           <td><input name=asofdate size=11 title="$myconfig{dateformat}" value=$form->{asofdate}></td>
292           <td>
293           <select name=asofmonth>$form->{selectaccountingmonth}</select>
294           <select name=asofyear>$form->{selectaccountingyear}</select>
295           </td>
296         </tr>
297
298           <th align=right nowrap>|.$locale->text('Compare to').qq|</th>
299           <td><input name=compareasofdate size=11 title="$myconfig{dateformat}"></td>
300           <td>
301           <select name=compareasofmonth>$form->{selectaccountingmonth}</select>
302           <select name=compareasofyear>$form->{selectaccountingyear}</select>
303           </td>
304
305         </tr>
306         <tr>
307           <th align=right>|.$locale->text('Decimalplaces').qq|</th>
308           <td><input name=decimalplaces size=3 value=2></td>
309         </tr>
310       </table>
311     </td>
312   </tr>
313   <tr>
314     <td>
315       <table>
316         <tr>
317           <th align=right>|.$locale->text('Method').qq|</th>
318           <td colspan=3><input name=method class=radio type=radio value=accrual checked>|.$locale->text('Accrual').qq|
319           &nbsp;<input name=method class=radio type=radio value=cash>|.$locale->text('Cash').qq|</td>
320         </tr>
321
322         <tr>
323           <th align=right nowrap>|.$locale->text('Include in Report').qq|</th>
324           <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Heading').qq|
325           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Subtotal').qq|
326           <input name=l_accno class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Account Number').qq|</td>
327         </tr>
328 |;
329   }
330
331
332   if ($form->{report} eq "trial_balance") {
333     print qq|
334         <input type=hidden name=nextsub value=generate_trial_balance>
335         <tr>
336           <th align=right>|.$locale->text('From').qq|</th>
337           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
338           <th align=right>|.$locale->text('To').qq|</th>
339           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
340         </tr>
341         $selectfrom
342       </table>
343     </td>
344   </tr>
345   <tr>
346     <td>
347       <table>
348         <tr>
349           <th align=right nowrap>|.$locale->text('Include in Report').qq|</th>
350           <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Heading').qq|
351           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Subtotal').qq|
352           <input name=all_accounts class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('All Accounts').qq|</td>
353         </tr>
354 |;
355   }
356
357   
358   if ($form->{report} =~ /^tax_/) {
359     $gifi = "";
360
361     $form->{db} = ($form->{report} =~ /_collected/) ? "ar" : "ap";
362     
363     RP->get_taxaccounts(\%myconfig, \%$form);
364
365     print qq|
366         <input type=hidden name=nextsub value=generate_tax_report>
367         <tr>
368           <th align=right>|.$locale->text('From').qq|</th>
369           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
370           <th align=right>|.$locale->text('To').qq|</th>
371           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
372         </tr>
373         $selectfrom
374         $summary
375         <tr>
376           <th align=right>|.$locale->text('Report for').qq|</th>
377           <td colspan=3>
378 |;
379
380   $checked = "checked";
381   foreach $ref (@{ $form->{taxaccounts} }) {
382     
383     print qq|<input name=accno class=radio type=radio value=$ref->{accno} $checked>&nbsp;$ref->{description}
384
385     <input name="$ref->{accno}_description" type=hidden value="$ref->{description}">
386     <input name="$ref->{accno}_rate" type=hidden value="$ref->{rate}">|;
387
388     $checked = "";
389
390   }
391
392   print qq|
393   <input type=hidden name=db value=$form->{db}>
394   <input type=hidden name=sort value=transdate>
395
396           </td>
397         </tr>
398 |;
399
400
401   if (@{ $form->{gifi_taxaccounts} }) {
402     print qq|
403         <tr>
404           <th align=right>|.$locale->text('GIFI').qq|</th>
405           <td colspan=3>
406 |;
407
408     foreach $ref (@{ $form->{gifi_taxaccounts} }) {
409       
410       print qq|<input name=accno class=radio type=radio value="gifi_$ref->{accno}">&nbsp;$ref->{description}
411
412       <input name="gifi_$ref->{accno}_description" type=hidden value="$ref->{description}">
413       <input name="gifi_$ref->{accno}_rate" type=hidden value="$ref->{rate}">|;
414
415     }
416
417     print qq|
418           </td>
419         </tr>
420 |;
421   }
422
423
424 print qq|
425         <tr>
426           <th align=right>|.$locale->text('Method').qq|</th>
427           <td colspan=3><input name=method class=radio type=radio value=accrual checked>|.$locale->text('Accrual').qq|
428           &nbsp;<input name=method class=radio type=radio value=cash>|.$locale->text('Cash').qq|</td>
429         </tr>
430       </table>
431     </td>
432   </tr>
433   <tr>
434     <td>
435       <table>
436         <tr>
437           <th align=right>|.$locale->text('Include in Report').qq|</th>
438           <td>
439             <table>
440               <tr>
441                 <td><input name="l_id" class=checkbox type=checkbox value=Y></td>
442                 <td>|.$locale->text('ID').qq|</td>
443                 <td><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
444                 <td>|.$locale->text('Invoice').qq|</td>
445                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
446                 <td>|.$locale->text('Date').qq|</td>
447               </tr>
448               <tr>
449                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
450 |;
451                 
452   if ($form->{db} eq 'ar') {
453     print qq|<td>|.$locale->text('Customer').qq|</td>|;
454   }
455   if ($form->{db} eq 'ap') {
456     print qq|<td>|.$locale->text('Vendor').qq|</td>|;
457   }
458   
459   print qq|
460                 <td><input name="l_description" class=checkbox type=checkbox value=Y checked></td>
461                 <td>|.$locale->text('Description').qq|</td>
462                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y checked></td>
463                 <td>|.$locale->text('Amount').qq|</td>
464                 
465                 <td><input name="l_tax" class=checkbox type=checkbox value=Y checked></td>
466                 <td>|.$locale->text('Tax').qq|</td>
467                 
468                 <td><input name="l_total" class=checkbox type=checkbox value=Y checked></td>
469                 <td>|.$locale->text('Total').qq|</td>
470               </tr>
471               <tr>
472               </tr>
473               <tr>
474                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
475                 <td>|.$locale->text('Subtotal').qq|</td>
476               </tr>
477             </table>
478           </td>
479         </tr>
480 |;
481
482   }
483
484   
485   if ($form->{report} =~ /^nontaxable_/) {
486     $gifi = "";
487
488     $form->{db} = ($form->{report} =~ /_sales/) ? "ar" : "ap";
489     
490     print qq|
491         <input type=hidden name=nextsub value=generate_tax_report>
492
493         <input type=hidden name=db value=$form->{db}>
494         <input type=hidden name=sort value=transdate>
495         <input type=hidden name=report value=$form->{report}>
496
497         <tr>
498           <th align=right>|.$locale->text('From').qq|</th>
499           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
500           <th align=right>|.$locale->text('To').qq|</th>
501           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
502         </tr>
503         $selectfrom
504         $summary
505         <tr>
506           <th align=right>|.$locale->text('Method').qq|</th>
507           <td colspan=3><input name=method class=radio type=radio value=accrual checked>|.$locale->text('Accrual').qq|
508           &nbsp;<input name=method class=radio type=radio value=cash>|.$locale->text('Cash').qq|</td>
509         </tr>
510         <tr>
511           <th align=right>|.$locale->text('Include in Report').qq|</th>
512           <td colspan=3>
513             <table>
514               <tr>
515                 <td><input name="l_id" class=checkbox type=checkbox value=Y></td>
516                 <td>|.$locale->text('ID').qq|</td>
517                 <td><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
518                 <td>|.$locale->text('Invoice').qq|</td>
519                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
520                 <td>|.$locale->text('Date').qq|</td>
521               </tr>
522               <tr>
523                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
524 |;
525                 
526   if ($form->{db} eq 'ar') {
527     print qq|<td>|.$locale->text('Customer').qq|</td>|;
528   }
529   if ($form->{db} eq 'ap') {
530     print qq|<td>|.$locale->text('Vendor').qq|</td>|;
531   }
532   
533   print qq|
534                 <td><input name="l_description" class=checkbox type=checkbox value=Y checked></td>
535                 <td>|.$locale->text('Description').qq|</td>
536                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y checked></td>
537                 <td>|.$locale->text('Amount').qq|</td>
538               </tr>
539               <tr>
540                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
541                 <td>|.$locale->text('Subtotal').qq|</td>
542               </tr>
543             </table>
544           </td>
545         </tr>
546 |;
547
548   }
549
550
551   if (($form->{report} eq "ar_aging") || ($form->{report} eq "ap_aging")) {
552     $gifi = "";
553
554     if ($form->{report} eq 'ar_aging') {
555       $label = $locale->text('Customer');
556       $form->{vc} = 'customer';
557     } else {
558       $label = $locale->text('Vendor');
559       $form->{vc} = 'vendor';
560     }
561       
562     $nextsub = "generate_$form->{report}";
563     
564     # setup vc selection
565     $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
566
567     map { $vc .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| } @{ $form->{"all_$form->{vc}"} };
568     
569     $vc = ($vc) ? qq|<select name=$form->{vc}><option>\n$vc</select>| : qq|<input name=$form->{vc} size=35>|;
570     
571     $postscript = "postscript" if $myconfig{printer};
572
573     print qq|
574         <tr>
575           <th align=right>|.$locale->text($label).qq|</th>
576           <td>$vc</td>
577         </tr>
578         <tr>
579           <th align=right>|.$locale->text('To').qq|</th>
580           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
581         </tr>
582         $selectto
583         <input type=hidden name=type value=statement>
584         <input type=hidden name=format value=$postscript>
585         <input type=hidden name=media value="$myconfig{printer}">
586
587         <input type=hidden name=nextsub value=$nextsub>
588         <input type=hidden name=action value=$nextsub>
589         $summary
590 |;
591   }
592
593 # above action can be removed if there is more than one input field
594
595
596   if ($form->{report} =~ /(receipts|payments)$/) {
597     $gifi = "";
598
599     $form->{db} = ($form->{report} =~ /payments$/) ? "ap" : "ar";
600
601     RP->paymentaccounts(\%myconfig, \%$form);
602
603     $selection = "<option>\n";
604     foreach $ref (@{ $form->{PR} }) {
605       $paymentaccounts .= "$ref->{accno} ";
606       $selection .= "<option>$ref->{accno}--$ref->{description}\n";
607     }
608     
609     chop $paymentaccounts;
610
611     print qq|
612         <input type=hidden name=nextsub value=list_payments>
613         <tr>
614           <th align=right nowrap>|.$locale->text('Account').qq|</th>
615           <td colspan=3><select name=account>$selection</select>
616             <input type=hidden name=paymentaccounts value="$paymentaccounts">
617           </td>
618         </tr>
619         <tr>
620           <th align=right nowrap>|.$locale->text('Description').qq|</th>
621           <td colspan=3><input name=description size=35></td>
622         </tr>
623         <tr>
624           <th align=right nowrap>|.$locale->text('Source').qq|</th>
625           <td colspan=3><input name=source></td>
626         </tr>
627         <tr>
628           <th align=right nowrap>|.$locale->text('Memo').qq|</th>
629           <td colspan=3><input name=memo size=30></td>
630         </tr>
631         <tr>
632           <th align=right>|.$locale->text('From').qq|</th>
633           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
634           <th align=right>|.$locale->text('To').qq|</th>
635           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
636         </tr>
637         $selectfrom
638         <tr>
639           <td align=right><input type=checkbox style=checkbox name=fx_transaction value=1 checked></td>
640           <td colspan=3>|.$locale->text('Include Exchange Rate Difference').qq|</td>
641         </tr>
642         <tr>
643           <td align=right><input name=l_subtotal class=checkbox type=checkbox value=Y></td>
644           <td align=left colspan=3>|.$locale->text('Subtotal').qq|</th>
645         </tr>
646           
647           <input type=hidden name=db value=$form->{db}>
648           <input type=hidden name=sort value=transdate>
649 |;
650
651   }
652
653
654   print qq|
655
656 $gifi
657
658       </table>
659     </td>
660   </tr>
661   <tr>
662     <td><hr size=3 noshade></td>
663   </tr>
664 </table>
665
666 <br>
667 <input type=hidden name=path value=$form->{path}>
668 <input type=hidden name=login value=$form->{login}>
669 <input type=hidden name=sessionid value=$form->{sessionid}>
670
671 <input type=submit class=submit name=action value="|.$locale->text('Continue').qq|">
672
673 </form>
674
675 </body>
676 </html>
677 |;
678
679 }
680
681
682 sub continue { &{$form->{nextsub}} };
683
684
685 sub generate_income_statement {
686
687   $form->{padding} = "&nbsp;&nbsp;";
688   $form->{bold} = "<b>";
689   $form->{endbold} = "</b>";
690   $form->{br} = "<br>";
691   
692   RP->income_statement(\%myconfig, \%$form);
693
694   ($form->{department}) = split /--/, $form->{department};
695   ($form->{projectnumber}) = split /--/, $form->{projectnumber};
696   
697   $form->{period} = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
698   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
699
700   # if there are any dates construct a where
701   if ($form->{fromdate} || $form->{todate}) {
702     
703     unless ($form->{todate}) {
704       $form->{todate} = $form->current_date(\%myconfig);
705     }
706
707     $longtodate = $locale->date(\%myconfig, $form->{todate}, 1);
708     $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
709     
710     $longfromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
711     $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
712     
713     $form->{this_period} = "$shortfromdate<br>\n$shorttodate";
714     $form->{period} = $locale->text('for Period').qq|<br>\n$longfromdate |.$locale->text('To').qq| $longtodate|;
715   }
716
717   if ($form->{comparefromdate} || $form->{comparetodate}) {
718     $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
719     $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
720     
721     $longcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 1);
722     $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
723     
724     $form->{last_period} = "$shortcomparefromdate<br>\n$shortcomparetodate";
725     $form->{period} .= "<br>\n$longcomparefromdate ".$locale->text('To').qq| $longcomparetodate|;
726   }
727
728   # setup variables for the form
729   @a = qw(company address businessnumber);
730   map { $form->{$_} = $myconfig{$_} } @a;
731   $form->{address} =~ s/\\n/<br>/g;
732
733   $form->{templates} = $myconfig{templates};
734
735   $form->{IN} = "income_statement.html";
736   
737   $form->parse_template;
738
739 }
740
741
742 sub generate_balance_sheet {
743
744   $form->{padding} = "&nbsp;&nbsp;";
745   $form->{bold} = "<b>";
746   $form->{endbold} = "</b>";
747   $form->{br} = "<br>";
748   
749   RP->balance_sheet(\%myconfig, \%$form);
750
751   $form->{asofdate} = $form->current_date(\%myconfig) unless $form->{asofdate};
752   $form->{period} = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
753   
754   ($form->{department}) = split /--/, $form->{department};
755   
756   # define Current Earnings account
757   $padding = ($form->{l_heading}) ? $form->{padding} : "";
758   push(@{$form->{equity_account}}, $padding.$locale->text('Current Earnings'));
759
760   $form->{this_period} = $locale->date(\%myconfig, $form->{asofdate}, 0);
761   $form->{last_period} = $locale->date(\%myconfig, $form->{compareasofdate}, 0);
762
763   $form->{IN} = "balance_sheet.html";
764
765   # setup company variables for the form
766   map { $form->{$_} = $myconfig{$_} } (qw(company address businessnumber nativecurr));
767   $form->{address} =~ s/\\n/<br>/g;
768
769   $form->{templates} = $myconfig{templates};
770           
771   $form->parse_template;
772
773 }
774
775
776 sub generate_projects {
777
778   $form->{nextsub} = "generate_projects";
779   $form->{title} = $locale->text('Project Transactions');
780   RP->trial_balance(\%myconfig, \%$form);
781   
782   &list_accounts;
783
784 }
785
786
787 # Antonio Gallardo
788 #
789 # D.S. Feb 16, 2001
790 # included links to display transactions for period entered
791 # added headers and subtotals
792 #
793 sub generate_trial_balance {
794   
795   # get for each account initial balance, debits and credits
796   RP->trial_balance(\%myconfig, \%$form);
797
798   $form->{nextsub} = "generate_trial_balance";
799   $form->{title} = $locale->text('Trial Balance');
800   &list_accounts;
801
802 }
803
804
805 sub list_accounts {
806
807   $title = $form->escape($form->{title});
808   
809   if ($form->{department}) {
810     ($department) = split /--/, $form->{department};
811     $options = $locale->text('Department')." : $department<br>";
812     $department = $form->escape($form->{department});
813   }
814   if ($form->{projectnumber}) {
815     ($projectnumber) = split /--/, $form->{projectnumber};
816     $options .= $locale->text('Project Number')." : $projectnumber<br>";
817     $projectnumber = $form->escape($form->{projectnumber});
818   }
819
820   # if there are any dates
821   if ($form->{fromdate} || $form->{todate}) {
822     if ($form->{fromdate}) {
823       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
824     }
825     if ($form->{todate}) {
826       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
827     }
828     
829     $form->{period} = "$fromdate - $todate";
830   } else {
831     $form->{period} = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
832
833   }
834   $options .= $form->{period};
835
836   @column_index = qw(accno description begbalance debit credit endbalance);
837
838   $column_header{accno} = qq|<th class=listheading>|.$locale->text('Account').qq|</th>|;
839   $column_header{description} = qq|<th class=listheading>|.$locale->text('Description').qq|</th>|;
840   $column_header{debit} = qq|<th class=listheading>|.$locale->text('Debit').qq|</th>|;
841   $column_header{credit} = qq|<th class=listheading>|.$locale->text('Credit').qq|</th>|;
842   $column_header{begbalance} = qq|<th class=listheading>|.$locale->text('Balance').qq|</th>|;
843   $column_header{endbalance} = qq|<th class=listheading>|.$locale->text('Balance').qq|</th>|;
844
845
846   if ($form->{accounttype} eq 'gifi') {
847     $column_header{accno} = qq|<th class=listheading>|.$locale->text('GIFI').qq|</th>|;
848   }
849   
850
851   $form->header;
852
853   print qq|
854 <body>
855
856 <table width=100%>
857   <tr>
858     <th class=listtop>$form->{title}</th>
859   </tr>
860   <tr height="5"></tr>
861   <tr>
862     <td>$options</td>
863   </tr>
864   <tr>
865     <td>
866       <table width=100%>
867         <tr>|;
868
869   map { print "$column_header{$_}\n" } @column_index;
870
871   print qq|
872         </tr>
873 |;
874
875
876   
877   # sort the whole thing by account numbers and display
878   foreach $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} }) {
879
880     $description = $form->escape($ref->{description});
881     
882     $href = qq|ca.pl?path=$form->{path}&action=list_transactions&accounttype=$form->{accounttype}&login=$form->{login}&sessionid=$form->{sessionid}&fromdate=$form->{fromdate}&todate=$form->{todate}&sort=transdate&l_heading=$form->{l_heading}&l_subtotal=$form->{l_subtotal}&department=$department&projectnumber=$projectnumber&project_id=$form->{project_id}&title=$title&nextsub=$form->{nextsub}|;
883     
884     if ($form->{accounttype} eq 'gifi') {
885       $href .= "&gifi_accno=$ref->{accno}&gifi_description=$description";
886       $na = $locale->text('N/A');
887       map { $ref->{$_} = $na } qw(accno description) unless $ref->{accno};
888     } else {
889       $href .= "&accno=$ref->{accno}&description=$description";
890     }
891
892     $ml = ($ref->{category} =~ /(A|E)/) ? -1 : 1;
893     
894     $debit = $form->format_amount(\%myconfig, $ref->{debit}, 2, "&nbsp;");
895     $credit = $form->format_amount(\%myconfig, $ref->{credit}, 2, "&nbsp;");
896     $begbalance = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2, "&nbsp;");
897     $endbalance = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2, "&nbsp;");
898
899  
900     if ($ref->{charttype} eq "H" && $subtotal && $form->{l_subtotal}) {
901
902       if ($subtotal) {
903
904         map { $column_data{$_} = "<th>&nbsp;</th>" } qw(accno begbalance endbalance);
905
906         $subtotalbegbalance = $form->format_amount(\%myconfig, $subtotalbegbalance, 2, "&nbsp;");
907         $subtotalendbalance = $form->format_amount(\%myconfig, $subtotalendbalance, 2, "&nbsp;");
908         $subtotaldebit = $form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;");
909         $subtotalcredit = $form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;");
910         
911         $column_data{description} = "<th class=listsubtotal>$subtotaldescription</th>";
912         $column_data{begbalance} = "<th align=right class=listsubtotal>$subtotalbegbalance</th>";
913         $column_data{endbalance} = "<th align=right class=listsubtotal>$subtotalendbalance</th>";
914         $column_data{debit} = "<th align=right class=listsubtotal>$subtotaldebit</th>";
915         $column_data{credit} = "<th align=right class=listsubtotal>$subtotalcredit</th>";
916         
917         print qq|
918           <tr class=listsubtotal>
919 |;
920         map { print "$column_data{$_}\n" } @column_index;
921         
922         print qq|
923           </tr>
924 |;
925       }
926     }
927  
928     if ($ref->{charttype} eq "H") {
929       $subtotal = 1;
930       $subtotaldescription = $ref->{description};
931       $subtotaldebit = $ref->{debit};
932       $subtotalcredit = $ref->{credit};
933       $subtotalbegbalance = 0;
934       $subtotalendbalance = 0;
935
936       if ($form->{l_heading}) {
937         if (! $form->{all_accounts}) {
938           if (($subtotaldebit + $subtotalcredit) == 0) {
939             $subtotal = 0;
940             next;
941           }
942         }
943       } else {
944         $subtotal = 0;
945         if ($form->{all_accounts} || ($form->{l_subtotal} && (($subtotaldebit + $subtotalcredit) != 0))) {
946           $subtotal = 1;
947         }
948         next;
949       }
950       
951       map { $column_data{$_} = "<th>&nbsp;</th>" } qw(accno debit credit begbalance endbalance);
952       $column_data{description} = "<th class=listheading>$ref->{description}</th>";
953     }
954
955     if ($ref->{charttype} eq "A") {
956       $column_data{accno} = "<td><a href=$href>$ref->{accno}</a></td>";
957       $column_data{description} = "<td>$ref->{description}</td>";
958       $column_data{debit} = "<td align=right>$debit</td>";
959       $column_data{credit} = "<td align=right>$credit</td>";
960       $column_data{begbalance} = "<td align=right>$begbalance</td>";
961       $column_data{endbalance} = "<td align=right>$endbalance</td>";
962     
963       $totaldebit += $ref->{debit};
964       $totalcredit += $ref->{credit};
965
966       $cml = ($ref->{category} eq 'C') ? -1 : 1;
967       $subtotalbegbalance += $ref->{balance} * $ml * $cml;
968       $subtotalendbalance += ($ref->{balance} + $ref->{amount}) * $ml * $cml;
969
970     }
971  
972    
973     if ($ref->{charttype} eq "H") {
974       print qq|
975       <tr class=listheading>
976 |;
977     }
978     if ($ref->{charttype} eq "A") {
979       $i++; $i %= 2;
980       print qq|
981       <tr class=listrow$i>
982 |;
983     }
984     
985     map { print "$column_data{$_}\n" } @column_index;
986     
987     print qq|
988       </tr>
989 |;
990   }
991
992
993   # print last subtotal
994   if ($subtotal && $form->{l_subtotal}) {
995     map { $column_data{$_} = "<th>&nbsp;</th>" } qw(accno begbalance endbalance);
996     $subtotalbegbalance = $form->format_amount(\%myconfig, $subtotalbegbalance, 2, "&nbsp;");
997     $subtotalendbalance = $form->format_amount(\%myconfig, $subtotalendbalance, 2, "&nbsp;");
998     $subtotaldebit = $form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;");
999     $subtotalcredit = $form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;");
1000     $column_data{description} = "<th class=listsubtotal>$subtotaldescription</th>";
1001     $column_data{begbalance} = "<th align=right class=listsubtotal>$subtotalbegbalance</th>";
1002     $column_data{endbalance} = "<th align=right class=listsubtotal>$subtotalendbalance</th>";
1003     $column_data{debit} = "<th align=right class=listsubtotal>$subtotaldebit</th>";
1004     $column_data{credit} = "<th align=right class=listsubtotal>$subtotalcredit</th>";
1005     
1006     print qq|
1007     <tr class=listsubtotal>
1008 |;
1009     map { print "$column_data{$_}\n" } @column_index;
1010     
1011     print qq|
1012     </tr>
1013 |;
1014   }
1015   
1016   $totaldebit = $form->format_amount(\%myconfig, $totaldebit, 2, "&nbsp;");
1017   $totalcredit = $form->format_amount(\%myconfig, $totalcredit, 2, "&nbsp;");
1018
1019   map { $column_data{$_} = "<th>&nbsp;</th>" } qw(accno description begbalance endbalance);
1020  
1021   $column_data{debit} = qq|<th align=right class=listtotal>$totaldebit</th>|;
1022   $column_data{credit} = qq|<th align=right class=listtotal>$totalcredit</th>|;
1023   
1024   print qq|
1025         <tr class=listtotal>
1026 |;
1027
1028   map { print "$column_data{$_}\n" } @column_index;
1029
1030   print qq|
1031         </tr>
1032       </table>
1033     </td>
1034   </tr>
1035   <tr>
1036     <td><hr size=3 noshade></td>
1037   </tr>
1038 </table>
1039
1040 </body>
1041 </html>
1042 |;
1043
1044 }
1045
1046
1047 sub generate_ar_aging {
1048
1049   # split customer
1050   ($form->{customer}) = split(/--/, $form->{customer});
1051   $customer = $form->escape($form->{customer},1);
1052   $title = $form->escape($form->{title},1);
1053   $media = $form->escape($form->{media},1);
1054
1055   $form->{ct} = "customer";
1056   $form->{arap} = "ar";
1057
1058   RP->aging(\%myconfig, \%$form);
1059
1060   $form->{callback} = qq|$form->{script}?path=$form->{path}&action=generate_ar_aging&login=$form->{login}&sessionid=$form->{sessionid}&todate=$form->{todate}&customer=$customer&title=$title&type=$form->{type}&format=$form->{format}&media=$media&summary=$form->{summary}|;
1061   
1062   &aging;
1063   
1064 }
1065
1066
1067 sub generate_ap_aging {
1068   
1069   # split vendor
1070   ($form->{vendor}) = split(/--/, $form->{vendor});
1071   $vendor = $form->escape($form->{vendor},1);
1072   $title = $form->escape($form->{title},1);
1073   $media = $form->escape($form->{media},1);
1074
1075   $form->{ct} = "vendor";
1076   $form->{arap} = "ap";
1077   
1078   RP->aging(\%myconfig, \%$form);
1079
1080   $form->{callback} = qq|$form->{script}?path=$form->{path}&action=generate_ap_aging&login=$form->{login}&sessionid=$form->{sessionid}&todate=$form->{todate}&vendor=$vendor&title=$title&type=$form->{type}&format=$form->{format}&media=$media&summary=$form->{summary}|;
1081
1082   &aging;
1083   
1084 }
1085
1086
1087 sub aging {
1088
1089   $form->header;
1090   
1091   $column_header{statement} = qq|<th class=listheading width=1%>&nbsp;</th>|;
1092   $column_header{ct} = qq|<th class=listheading width=60%>|.$locale->text(ucfirst $form->{ct}).qq|</th>|;
1093   $column_header{language} = qq|<th class=listheading>|.$locale->text('Language').qq|</th>|;
1094   $column_header{invnumber} = qq|<th class=listheading>|.$locale->text('Invoice').qq|</th>|;
1095   $column_header{ordnumber} = qq|<th class=listheading>|.$locale->text('Order').qq|</th>|;
1096   $column_header{transdate} = qq|<th class=listheading nowrap>|.$locale->text('Date').qq|</th>|;
1097   $column_header{duedate} = qq|<th class=listheading nowrap>|.$locale->text('Due Date').qq|</th>|;
1098   $column_header{c0} = qq|<th class=listheading width=10%>|.$locale->text('Current').qq|</th>|;
1099   $column_header{c30} = qq|<th class=listheading width=10%>30</th>|;
1100   $column_header{c60} = qq|<th class=listheading width=10%>60</th>|;
1101   $column_header{c90} = qq|<th class=listheading width=10%>90</th>|;
1102   
1103   @column_index = qw(statement ct);
1104
1105   if (@{ $form->{all_language} } && $form->{arap} eq 'ar') {
1106     push @column_index, "language";
1107     $form->{selectlanguage} = qq|<option>\n|;
1108
1109     map { $form->{selectlanguage} .= qq|<option value="$_->{code}">$_->{description}\n| } @{ $form->{all_language} };
1110   }
1111   
1112   if ($form->{summary}) {
1113     push @column_index, qw(c0 c30 c60 c90);
1114   } else {
1115     push @column_index, qw(invnumber ordnumber transdate duedate c0 c30 c60 c90);
1116   }
1117
1118   if ($form->{department}) {
1119       $option .= "\n<br>" if $option;
1120       ($department) = split /--/, $form->{department};
1121       $option .= $locale->text('Department')." : $department";
1122       $department = $form->escape($form->{department},1);
1123       $form->{callback} .= "&department=$department";
1124   }
1125     
1126   if ($form->{arap} eq 'ar') {
1127     if ($form->{customer}) {
1128       $option .= "\n<br>" if $option;
1129       $option .= $form->{customer};
1130     }
1131   }
1132   if ($form->{arap} eq 'ap') {
1133     shift @column_index;
1134     if ($form->{vendor}) {
1135       $option .= "\n<br>" if $option;
1136       $option .= $form->{vendor};
1137     }
1138   }
1139
1140   $todate = $locale->date(\%myconfig, $form->{todate}, 1);
1141   $option .= "\n<br>" if $option;
1142   $option .= $locale->text('for Period')." ".$locale->text('To')." $todate";
1143
1144   print qq|
1145 <body>
1146
1147 <form method=post action=$form->{script}>
1148
1149 <table width=100%>
1150   <tr>
1151     <th class=listtop>$form->{title}</th>
1152   </tr>
1153   <tr height="5"></tr>
1154   <tr>
1155     <td>$option</td>
1156   </tr>
1157   <tr>
1158     <td>
1159       <table width=100%>
1160         <tr class=listheading>
1161 |;
1162
1163   map { print "$column_header{$_}\n" } @column_index;
1164   
1165   print qq|
1166         </tr>
1167 |;
1168
1169
1170   $ctid = 0;
1171   $i = 0;
1172   $k = 0;
1173   $l = $#{ $form->{AG} };
1174
1175   foreach $ref (@{ $form->{AG} }) {
1176
1177     $k++;
1178     
1179     if ($ctid != $ref->{ctid}) {
1180
1181       $i++;
1182
1183       $column_data{ct} = qq|<td>$ref->{name}</td>|;
1184       
1185       if ($form->{selectlanguage}) {
1186         $form->{"selectlanguage_$i"} = $form->{selectlanguage};
1187         $form->{"selectlanguage_$i"} =~ s/(<option value="\Q$ref->{language_code}\E")/$1 selected/;
1188         $column_data{language} = qq|<td><select name="language_code_$i">$form->{"selectlanguage_$i"}</select></td>|;
1189       }
1190       
1191       $column_data{statement} = qq|<td><input name="statement_$i" type=checkbox class=checkbox value=1 $ref->{checked}>
1192       <input type=hidden name="$form->{ct}_id_$i" value=$ref->{ctid}>
1193       </td>|;
1194
1195     }
1196             
1197     $ctid = $ref->{ctid};
1198
1199     $c0subtotal += $ref->{c0};
1200     $c30subtotal += $ref->{c30};
1201     $c60subtotal += $ref->{c60};
1202     $c90subtotal += $ref->{c90};
1203
1204     $c0total += $ref->{c0};
1205     $c30total += $ref->{c30};
1206     $c60total += $ref->{c60};
1207     $c90total += $ref->{c90};
1208
1209     $ref->{c0} = $form->format_amount(\%myconfig, $ref->{c0}, 2, "&nbsp;");
1210     $ref->{c30} = $form->format_amount(\%myconfig, $ref->{c30}, 2, "&nbsp;");
1211     $ref->{c60} = $form->format_amount(\%myconfig, $ref->{c60}, 2, "&nbsp;");
1212     $ref->{c90} = $form->format_amount(\%myconfig, $ref->{c90}, 2, "&nbsp;");
1213
1214     $href = qq|$ref->{module}.pl?path=$form->{path}&action=edit&id=$ref->{id}&login=$form->{login}&sessionid=$form->{sessionid}&callback=|.$form->escape($form->{callback});
1215     
1216     $column_data{invnumber} = qq|<td><a href=$href>$ref->{invnumber}</a></td>|;
1217     map { $column_data{$_} = qq|<td>$ref->{$_}</td>| } qw(ordnumber transdate duedate);
1218     map { $column_data{$_} = qq|<td align=right>$ref->{$_}</td>| } qw(c0 c30 c60 c90);
1219     
1220     if (!$form->{summary}) {
1221
1222       $j++; $j %= 2;
1223       print qq|
1224         <tr class=listrow$j>
1225 |;
1226
1227       map { print "$column_data{$_}\n" } @column_index;
1228
1229       print qq|
1230         </tr>
1231 |;
1232
1233       map { $column_data{$_} = qq|<td>&nbsp;</td>| } qw(ct statement language);
1234       
1235     }
1236    
1237     # print subtotal
1238     $nextid = ($k <= $l) ? $form->{AG}->[$k]->{ctid} : 0;
1239     if ($ctid != $nextid) {
1240
1241       $c0subtotal = $form->format_amount(\%myconfig, $c0subtotal, 2, "&nbsp");
1242       $c30subtotal = $form->format_amount(\%myconfig, $c30subtotal, 2, "&nbsp");
1243       $c60subtotal = $form->format_amount(\%myconfig, $c60subtotal, 2, "&nbsp");
1244       $c90subtotal = $form->format_amount(\%myconfig, $c90subtotal, 2, "&nbsp");
1245       
1246       if ($form->{summary}) {
1247         $column_data{c0} = qq|<td align=right>$c0subtotal</th>|;
1248         $column_data{c30} = qq|<td align=right>$c30subtotal</th>|;
1249         $column_data{c60} = qq|<td align=right>$c60subtotal</th>|;
1250         $column_data{c90} = qq|<td align=right>$c90subtotal</th>|;
1251
1252         $j++; $j %= 2;
1253         print qq|
1254         <tr class=listrow$j>
1255 |;
1256
1257         map { print "$column_data{$_}\n" } @column_index;
1258
1259         print qq|
1260         </tr>
1261 |;
1262
1263       } else {
1264
1265         map { $column_data{$_} = qq|<th>&nbsp;</th>| } @column_index;
1266
1267         $column_data{c0} = qq|<th class=listsubtotal align=right>$c0subtotal</th>|;
1268         $column_data{c30} = qq|<th class=listsubtotal align=right>$c30subtotal</th>|;
1269         $column_data{c60} = qq|<th class=listsubtotal align=right>$c60subtotal</th>|;
1270         $column_data{c90} = qq|<th class=listsubtotal align=right>$c90subtotal</th>|;
1271
1272         # print subtotals
1273         print qq|
1274         <tr class=listsubtotal>
1275 |;
1276         map { print "$column_data{$_}\n" } @column_index;
1277
1278         print qq|
1279         </tr>
1280 |;
1281
1282       }
1283       
1284       $c0subtotal = 0;
1285       $c30subtotal = 0;
1286       $c60subtotal = 0;
1287       $c90subtotal = 0;
1288       
1289     }
1290   }
1291   
1292   print qq|
1293         </tr>
1294         <tr class=listtotal>
1295 |;
1296
1297   map { $column_data{$_} = qq|<th>&nbsp;</th>| } @column_index;
1298
1299   $c0total = $form->format_amount(\%myconfig, $c0total, 2, "&nbsp;");
1300   $c30total = $form->format_amount(\%myconfig, $c30total, 2, "&nbsp;");
1301   $c60total = $form->format_amount(\%myconfig, $c60total, 2, "&nbsp;");
1302   $c90total = $form->format_amount(\%myconfig, $c90total, 2, "&nbsp;");
1303   
1304   $column_data{c0} = qq|<th align=right class=listtotal>$c0total</th>|;
1305   $column_data{c30} = qq|<th align=right class=listtotal>$c30total</th>|;
1306   $column_data{c60} = qq|<th align=right class=listtotal>$c60total</th>|;
1307   $column_data{c90} = qq|<th align=right class=listtotal>$c90total</th>|;
1308
1309   map { print "$column_data{$_}\n" } @column_index;
1310   
1311   print qq|
1312           <input type=hidden name=rowcount value=$i>
1313         </tr>
1314       </table>
1315     </td>
1316   </tr>
1317   <tr>
1318     <td>
1319 |;
1320
1321   &print_options if ($form->{arap} eq 'ar');
1322
1323   print qq|
1324     </td>
1325   </tr>
1326   <tr>
1327     <td><hr size=3 noshade></td>
1328   </tr>
1329 </table>
1330 |;
1331
1332   if ($form->{arap} eq 'ar') {
1333     print qq|
1334 <input type=hidden name=todate value=$form->{todate}>
1335
1336 <input type=hidden name=title value="$form->{title}">
1337 <input type=hidden name=summary value=$form->{summary}>
1338
1339 <input type=hidden name=callback value=$form->{callback}>
1340
1341 <input type=hidden name=arap value=$form->{arap}>
1342 <input type=hidden name=ct value=$form->{ct}>
1343 <input type=hidden name=$form->{ct} value="$form->{$form->{ct}}">
1344
1345 <input type=hidden name=department value="$form->{department}">
1346
1347 <input type=hidden name=path value=$form->{path}>
1348 <input type=hidden name=login value=$form->{login}>
1349 <input type=hidden name=sessionid value=$form->{sessionid}>
1350   
1351 <br>
1352 <input class=submit type=submit name=action value="|.$locale->text('Select all').qq|">
1353 <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
1354 <input class=submit type=submit name=action value="|.$locale->text('E-mail').qq|">
1355 |;
1356   }
1357
1358   if ($form->{menubar}) {
1359     require "$form->{path}/menu.pl";
1360     &menubar;
1361   }
1362
1363   print qq|
1364 </form>
1365
1366 </body>
1367 </html>
1368 |;
1369
1370 }
1371
1372
1373 sub select_all {
1374
1375   RP->aging(\%myconfig, \%$form);
1376
1377   map { $_->{checked} = "checked" } @{ $form->{AG} };
1378
1379   &aging;
1380
1381 }
1382
1383
1384 sub print_options {
1385
1386   $form->{sendmode} = "attachment";
1387   $form->{copies} = 1 unless $form->{copies};
1388   
1389   $form->{PD}{$form->{type}} = "selected";
1390   $form->{DF}{$form->{format}} = "selected";
1391   $form->{SM}{$form->{sendmode}} = "selected";
1392   
1393   $format = qq|
1394             <option value=html $form->{PD}{format}>html|;
1395             
1396   $type = qq|
1397             <option value=statement $form->{PD}{statement}>|.$locale->text('Statement');
1398
1399   
1400   if ($form->{media} eq 'email') {
1401     $media = qq|
1402             <td><select name=sendmode>
1403             <option value=attachment $form->{SM}{attachment}>|.$locale->text('Attachment').qq|
1404             <option value=inline $form->{SM}{inline}>|.$locale->text('In-line');
1405   } else {
1406     $media = qq|
1407             <td><select name=media>
1408             <option value=screen>|.$locale->text('Screen');
1409     if (%printer && $latex) {
1410       map { $media .= qq|
1411             <option value="$_">$_| } keys %printer;
1412     }
1413   }
1414
1415   $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
1416   $media .= qq|</select></td>|;
1417
1418   if ($latex) {
1419     $format .= qq|
1420             <option value=postscript $form->{DF}{postscript}>|.$locale->text('Postscript').qq|
1421             <option value=pdf $form->{DF}{pdf}>|.$locale->text('PDF');
1422   }
1423
1424   print qq|
1425 <table>
1426   <tr>
1427     <td><select name=type>$type</select></td>
1428     <td><select name=format>$format</select></td>
1429     $media
1430 |;
1431
1432   if (%printer && $latex && $form->{media} ne 'email') {
1433     print qq|
1434       <td>|.$locale->text('Copies').qq|
1435       <input name=copies size=2 value=$form->{copies}></td>
1436 |;
1437   }
1438   
1439   print qq|
1440   </tr>
1441 </table>
1442 |;
1443
1444 }
1445
1446
1447 sub e_mail {
1448
1449   # get name and email addresses
1450   for $i (1 .. $form->{rowcount}) {
1451     if ($form->{"statement_$i"}) {
1452       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1453       RP->get_customer(\%myconfig, \%$form);
1454       $selected = 1;
1455       last;
1456     }
1457   }
1458
1459   $form->error($locale->text('Nothing selected!')) unless $selected;
1460
1461   if ($myconfig{role} =~ /(admin|manager)/) {
1462     $bcc = qq|
1463           <th align=right nowrap=true>|.$locale->text('Bcc').qq|</th>
1464           <td><input name=bcc size=30 value="$form->{bcc}"></td>
1465 |;
1466   }
1467
1468   $title = $locale->text('E-mail Statement to')." $form->{$form->{ct}}";
1469
1470   $form->{media} = "email";
1471   
1472   $form->header;
1473
1474   print qq|
1475 <body>
1476
1477 <form method=post action=$form->{script}>
1478
1479 <table width=100%>
1480   <tr class=listtop>
1481     <th>$title</th>
1482   </tr>
1483   <tr height="5"></tr>
1484   <tr>
1485     <td>
1486       <table width=100%>
1487         <tr>
1488           <th align=right nowrap>|.$locale->text('E-mail').qq|</th>
1489           <td><input name=email size=30 value="$form->{email}"></td>
1490           <th align=right nowrap>|.$locale->text('Cc').qq|</th>
1491           <td><input name=cc size=30 value="$form->{cc}"></td>
1492         </tr>
1493         <tr>
1494           <th align=right nowrap>|.$locale->text('Subject').qq|</th>
1495           <td><input name=subject size=30 value="$form->{subject}"></td>
1496           $bcc
1497         </tr>
1498       </table>
1499     </td>
1500   </tr>
1501   <tr>
1502     <td>
1503       <table width=100%>
1504         <tr>
1505           <th align=left nowrap>|.$locale->text('Message').qq|</th>
1506         </tr>
1507         <tr>
1508           <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
1509         </tr>
1510       </table>
1511     </td>
1512   </tr>
1513   <tr>
1514     <td>
1515 |;
1516
1517   &print_options;
1518
1519   map { delete $form->{$_} } qw(action email cc bcc subject message type sendmode format header);
1520
1521   $form->hide_form();
1522
1523   print qq|
1524     </td>
1525   </tr>
1526   <tr>
1527     <td><hr size=3 noshade></td>
1528   </tr>
1529 </table>
1530
1531 <input type=hidden name=nextsub value=send_email>
1532
1533 <br>
1534 <input name=action class=submit type=submit value="|.$locale->text('Continue').qq|">
1535 </form>
1536
1537 </body>
1538 </html>
1539 |;
1540
1541 }
1542
1543
1544 sub send_email {
1545
1546   $form->{OUT} = "$sendmail";
1547
1548   $form->{subject} = $locale->text('Statement').qq| - $form->{todate}| unless $form->{subject};
1549   $form->isblank("email", $locale->text('E-mail address missing!'));
1550   
1551   RP->aging(\%myconfig, \%$form);
1552   
1553   $form->{"statement_1"} = 1;
1554
1555   &print_form;
1556   
1557   $form->redirect($locale->text('Statement sent to')." $form->{$form->{ct}}");
1558
1559 }
1560
1561
1562
1563 sub print {
1564
1565   if ($form->{media} !~ /(screen|email)/) {
1566     $form->error($locale->text('Select postscript or PDF!')) if ($form->{format} !~ /(postscript|pdf)/);
1567   }
1568   
1569   for $i (1 .. $form->{rowcount}) {
1570     if ($form->{"statement_$i"}) {
1571       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1572       $language_code = $form->{"language_code_$i"};
1573       $selected = 1;
1574       last;
1575     }
1576   }
1577
1578   $form->error($locale->text('Nothing selected!')) unless $selected;
1579      
1580  
1581   if ($form->{media} !~ /(screen|email)/) {
1582     $form->{OUT} = "| $printer{$form->{media}}";
1583     $form->{"$form->{ct}_id"} = "";
1584     $SIG{INT} = 'IGNORE';
1585   } else {
1586     $form->{"statement_1"} = 1;
1587     $form->{"language_code_1"} = $language_code;
1588   }
1589
1590   RP->aging(\%myconfig, \%$form);
1591  
1592   &print_form;
1593
1594   $form->redirect($locale->text('Statements sent to printer!')) if ($form->{media} !~ /(screen|email)/);
1595
1596 }
1597
1598
1599 sub print_form {
1600   
1601   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1602
1603   $form->{templates} = "$myconfig{templates}";
1604
1605   # setup variables for the form
1606   @a = qw(company address businessnumber tel fax);
1607   map { $form->{$_} = $myconfig{$_} } @a;
1608   $form->format_string(@a);
1609   
1610   $form->{IN} = "$form->{type}.html";
1611
1612   if ($form->{format} eq 'postscript') {
1613     $form->{IN} =~ s/html$/tex/;
1614   }
1615   if ($form->{format} eq 'pdf') {
1616     $form->{IN} =~ s/html$/tex/;
1617   }
1618
1619   @a = qw(name address1 address2 city state zipcode country contact);
1620   push @a, "$form->{ct}phone", "$form->{ct}fax";
1621   push @a, 'email' if ! $form->{media} eq 'email';
1622
1623   $i = 0;
1624   while (@{ $form->{AG} }) {
1625
1626     $ref = shift @{ $form->{AG} };
1627     
1628     if ($ctid != $ref->{ctid}) {
1629       
1630       $ctid = $ref->{ctid};
1631       $i++;
1632
1633       if ($form->{"statement_$i"}) {
1634         
1635         map { $form->{$_} = $ref->{$_} } @a;
1636         $form->format_string(@a);
1637
1638         $form->{$form->{ct}} = $form->{name};
1639         $form->{"$form->{ct}_id"} = $ref->{ctid};
1640         $form->{language_code} = $form->{"language_code_$i"};
1641         
1642         map { $form->{$_} = () } qw(invnumber ordnumber notes invdate duedate);
1643         $form->{total} = 0;
1644         foreach $item (qw(c0 c30 c60 c90)) {
1645           $form->{$item} = ();
1646           $form->{"${item}total"} = 0;
1647         }
1648
1649         &statement_details($ref);
1650
1651         while ($ref) {
1652
1653           if (scalar (@{ $form->{AG} }) > 0) {
1654             # one or more left to go
1655             if ($ctid == $form->{AG}->[0]->{ctid}) {
1656               $ref = shift @{ $form->{AG} };
1657               &statement_details($ref);
1658               # any more?
1659               $ref = scalar (@{ $form->{AG} });
1660             } else {
1661               $ref = 0;
1662             }
1663           } else {
1664             # set initial ref to 0
1665             $ref = 0;
1666           }
1667
1668         }
1669         
1670         map { $form->{"${_}total"} = $form->format_amount(\%myconfig, $form->{"${_}total"}, 2) } (c0, c30, c60, c90, "");
1671
1672         $form->parse_template(\%myconfig, $userspath);
1673         
1674       }
1675     }
1676   }
1677
1678 }
1679
1680
1681 sub statement_details {
1682   my ($ref) = @_;
1683
1684   $ref->{invdate} = $ref->{transdate};
1685   my @a = qw(invnumber ordnumber notes invdate duedate);
1686   map { $form->{"${_}_1"} = $ref->{$_} } @a;
1687   $form->format_string(qw(invnumber_1 ordnumber_1 notes_1));
1688   map { push @{ $form->{$_} }, $form->{"${_}_1"} } @a;
1689   
1690   foreach $item (qw(c0 c30 c60 c90)) {
1691     eval { $ref->{$item} = $form->round_amount($ref->{$item} / $ref->{exchangerate}, 2) };
1692     $form->{"${item}total"} += $ref->{$item};
1693     $form->{total} += $ref->{$item};
1694     push @{ $form->{$item} }, $form->format_amount(\%myconfig, $ref->{$item}, 2);
1695   }
1696
1697 }
1698  
1699
1700 sub generate_tax_report {
1701
1702   RP->tax_report(\%myconfig, \%$form);
1703
1704   $descvar = "$form->{accno}_description";
1705   $description = $form->escape($form->{$descvar});
1706   $ratevar = "$form->{accno}_rate";
1707   $taxrate = $form->{"$form->{accno}_rate"};
1708   
1709   if ($form->{accno} =~ /^gifi_/) {
1710     $descvar = "gifi_$form->{accno}_description";
1711     $description = $form->escape($form->{$descvar});
1712     $ratevar = "gifi_$form->{accno}_rate";
1713     $taxrate = $form->{"gifi_$form->{accno}_rate"};
1714   }
1715   
1716   $department = $form->escape($form->{department});
1717   
1718   # construct href
1719   $href = "$form->{script}?path=$form->{path}&direction=$form->{direction}&oldsort=$form->{oldsort}&action=generate_tax_report&login=$form->{login}&sessionid=$form->{sessionid}&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&summary=$form->{summary}&accno=$form->{accno}&$descvar=$description&department=$department&$ratevar=$taxrate&report=$form->{report}";
1720
1721   # construct callback
1722   $description = $form->escape($form->{$descvar},1);
1723   $department = $form->escape($form->{department},1);
1724
1725   $form->sort_order();
1726
1727   $callback = "$form->{script}?path=$form->{path}&direction=$form->{direction}&oldsort=$form->{oldsort}&action=generate_tax_report&login=$form->{login}&sessionid=$form->{sessionid}&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&summary=$form->{summary}&accno=$form->{accno}&$descvar=$description&department=$department&$ratevar=$taxrate&report=$form->{report}";
1728
1729   $form->{title} = $locale->text('GIFI')." - " if ($form->{accno} =~ /^gifi_/);
1730
1731   $title = $form->escape($form->{title});
1732   $href .= "&title=$title";
1733   $title = $form->escape($form->{title},1);
1734   $callback .= "&title=$title";
1735   
1736   $form->{title} = qq|$form->{title} $form->{"$form->{accno}_description"} |;
1737
1738   @columns = $form->sort_columns(qw(id transdate invnumber name description netamount tax total));
1739
1740   $form->{"l_description"} = "" if $form->{summary};
1741   
1742   foreach $item (@columns) {
1743     if ($form->{"l_$item"} eq "Y") {
1744       push @column_index, $item;
1745
1746       # add column to href and callback
1747       $callback .= "&l_$item=Y";
1748       $href .= "&l_$item=Y";
1749     }
1750   }
1751
1752
1753   if ($form->{l_subtotal} eq 'Y') {
1754     $callback .= "&l_subtotal=Y";
1755     $href .= "&l_subtotal=Y";
1756   }
1757   
1758   
1759   if ($form->{department}) {
1760     ($department) = split /--/, $form->{department};
1761     $option = $locale->text('Department')." : $department";
1762   }
1763   
1764   # if there are any dates
1765   if ($form->{fromdate} || $form->{todate}) {
1766     if ($form->{fromdate}) {
1767       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
1768     }
1769     if ($form->{todate}) {
1770       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
1771     }
1772     
1773     $form->{period} = "$fromdate - $todate";
1774   } else {
1775     $form->{period} = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1776   }
1777
1778
1779   if ($form->{db} eq 'ar') {
1780     $name = $locale->text('Customer');
1781     $invoice = 'is.pl';
1782     $arap = 'ar.pl';
1783   }
1784   if ($form->{db} eq 'ap') {
1785     $name = $locale->text('Vendor');
1786     $invoice = 'ir.pl';
1787     $arap = 'ap.pl';
1788   }
1789
1790   $option .= "<br>" if $option;
1791   $option .= "$form->{period}";
1792   
1793  
1794   $column_header{id} = qq|<th><a class=listheading href=$href&sort=id>|.$locale->text('ID').qq|</th>|;
1795   $column_header{invnumber} = qq|<th><a class=listheading href=$href&sort=invnumber>|.$locale->text('Invoice').qq|</th>|;
1796   $column_header{transdate} = qq|<th><a class=listheading href=$href&sort=transdate>|.$locale->text('Date').qq|</th>|;
1797   $column_header{netamount} = qq|<th class=listheading>|.$locale->text('Amount').qq|</th>|;
1798   $column_header{tax} = qq|<th class=listheading>|.$locale->text('Tax').qq|</th>|;
1799   $column_header{total} = qq|<th class=listheading>|.$locale->text('Total').qq|</th>|;
1800   
1801   $column_header{name} = qq|<th><a class=listheading href=$href&sort=name>$name</th>|;
1802   
1803   $column_header{description} = qq|<th><a class=listheading href=$href&sort=description>|.$locale->text('Description').qq|</th>|;
1804
1805   
1806   $form->header;
1807
1808   print qq|
1809 <body>
1810
1811 <table width=100%>
1812   <tr>
1813     <th class=listtop colspan=$colspan>$form->{title}</th>
1814   </tr>
1815   <tr height="5"></tr>
1816   <tr>
1817     <td>$option</td>
1818   </tr>
1819   <tr>
1820     <td>
1821       <table width=100%>
1822         <tr class=listheading>
1823 |;
1824
1825   map { print "$column_header{$_}\n" } @column_index;
1826   
1827   print qq|
1828         </tr>
1829 |;
1830
1831   # add sort and escape callback
1832   $callback = $form->escape($callback . "&sort=$form->{sort}");
1833     
1834   if (@{ $form->{TR} }) {
1835     $sameitem = $form->{TR}->[0]->{$form->{sort}};
1836   }
1837
1838   foreach $ref (@{ $form->{TR} }) {
1839
1840     $module = ($ref->{invoice}) ? $invoice : $arap;
1841     $module = 'ps.pl' if $ref->{till};
1842     
1843     if ($form->{l_subtotal} eq 'Y') {
1844       if ($sameitem ne $ref->{$form->{sort}}) {
1845         &tax_subtotal;
1846         $sameitem = $ref->{$form->{sort}};
1847       }
1848     }
1849
1850     $totalnetamount += $ref->{netamount};
1851     $totaltax += $ref->{tax};
1852     $ref->{total} = $ref->{netamount} + $ref->{tax};
1853
1854     $subtotalnetamount += $ref->{netamount};
1855     $subtotaltax += $ref->{tax};
1856     
1857     map { $ref->{$_} = $form->format_amount(\%myconfig, $ref->{$_}, 2, "&nbsp;"); } qw(netamount tax total);
1858     
1859     $column_data{id} = qq|<td>$ref->{id}</td>|;
1860     $column_data{invnumber} = qq|<td><a href=$module?path=$form->{path}&action=edit&id=$ref->{id}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback>$ref->{invnumber}</a></td>|;
1861
1862     map { $column_data{$_} = qq|<td>$ref->{$_}</td>| } qw(id transdate name partnumber description);
1863     
1864     map { $column_data{$_} = qq|<td align=right>$ref->{$_}</td>| } qw(netamount tax total);
1865
1866     $i++; $i %= 2;
1867     print qq|
1868         <tr class=listrow$i>
1869 |;
1870
1871     map { print "$column_data{$_}\n" } @column_index;
1872
1873     print qq|
1874         </tr>
1875 |;
1876  
1877   }
1878  
1879   if ($form->{l_subtotal} eq 'Y') {
1880     &tax_subtotal;
1881   }
1882
1883   
1884   map { $column_data{$_} = qq|<th>&nbsp;</th>| } @column_index;
1885   
1886   print qq|
1887         </tr>
1888         <tr class=listtotal>
1889 |;
1890
1891   $total = $form->format_amount(\%myconfig, $totalnetamount + $totaltax, 2, "&nbsp;");
1892   $totalnetamount = $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;");
1893   $totaltax = $form->format_amount(\%myconfig, $totaltax, 2, "&nbsp;");
1894   
1895   $column_data{netamount} = qq|<th class=listtotal align=right>$totalnetamount</th>|;
1896   $column_data{tax} = qq|<th class=listtotal align=right>$totaltax</th>|;
1897   $column_data{total} = qq|<th class=listtotal align=right>$total</th>|;
1898  
1899   map { print "$column_data{$_}\n" } @column_index;
1900  
1901     
1902   print qq|
1903         </tr>
1904       </table>
1905     </td>
1906   </tr>
1907   <tr>
1908     <td><hr size=3 noshade></td>
1909   </tr>
1910 </table>
1911
1912 </body>
1913 </html>
1914 |;
1915
1916 }
1917
1918
1919 sub tax_subtotal {
1920
1921   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
1922
1923   $subtotal = $form->format_amount(\%myconfig, $subtotalnetamount + $subtotaltax, 2, "&nbsp;");
1924   $subtotalnetamount = $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;");
1925   $subtotaltax = $form->format_amount(\%myconfig, $subtotaltax, 2, "&nbsp;");
1926   
1927   $column_data{netamount} = "<th class=listsubtotal align=right>$subtotalnetamount</th>";
1928   $column_data{tax} = "<th class=listsubtotal align=right>$subtotaltax</th>";
1929   $column_data{total} = "<th class=listsubtotal align=right>$subtotal</th>";
1930
1931   $subtotalnetamount = 0;
1932   $subtotaltax = 0;
1933   
1934   print qq|
1935         <tr class=listsubtotal>
1936 |;
1937   map { print "\n$column_data{$_}" } @column_index;
1938
1939   print qq|
1940         </tr>
1941 |;
1942   
1943 }
1944
1945
1946
1947 sub list_payments {
1948
1949
1950   if ($form->{account}) {
1951     ($form->{paymentaccounts}) = split /--/, $form->{account};
1952   }
1953   if ($form->{department}) {
1954     ($department, $form->{department_id}) = split /--/, $form->{department};
1955     $option = $locale->text('Department')." : $department";
1956   }
1957
1958   RP->payments(\%myconfig, \%$form);
1959   
1960   @columns = $form->sort_columns(qw(transdate name paid source memo));
1961
1962   if ($form->{till}) {
1963     @columns = $form->sort_columns(qw(transdate name paid curr source till));
1964     if ($myconfig{role} ne 'user') {
1965       @columns = $form->sort_columns(qw(transdate name paid curr source till employee));
1966     }
1967   }
1968   
1969   # construct href
1970   $account = $form->escape($form->{account});
1971   $title = $form->escape($form->{title});
1972   $department = $form->escape($form->{department});
1973   $form->{paymentaccounts} =~ s/ /%20/g;
1974   $source = $form->escape($form->{source});
1975   $memo = $form->escape($form->{memo});
1976   
1977   $href = "$form->{script}?path=$form->{path}&direction=$form->{direction}&oldsort=$form->{oldsort}&action=list_payments&till=$form->{till}&login=$form->{login}&sessionid=$form->{sessionid}&fromdate=$form->{fromdate}&todate=$form->{todate}&fx_transaction=$form->{fx_transaction}&db=$form->{db}&l_subtotal=$form->{l_cubtotal}&prepayment=$form->{prepayment}&title=$title&account=$account&department=$department&paymentaccounts=$form->{paymentaccounts}&source=$source&memo=$memo";
1978
1979   # construct callback
1980   $account = $form->escape($form->{account},1);
1981   $title = $form->escape($form->{title},1);
1982   $department = $form->escape($form->{department},1);
1983   $source = $form->escape($form->{source},1);
1984   $memo = $form->escape($form->{memo},1);
1985   
1986   $form->sort_order();
1987
1988   $form->{callback} = "$form->{script}?path=$form->{path}&direction=$form->{direction}&oldsort=$form->{oldsort}&action=list_payments&till=$form->{till}&login=$form->{login}&sessionid=$form->{sessionid}&fromdate=$form->{fromdate}&todate=$form->{todate}&fx_transaction=$form->{fx_transaction}&db=$form->{db}&l_subtotal=$form->{l_subtotal}&prepayment=$form->{prepayment}&title=$title&account=$account&department=$department&paymentaccounts=$form->{paymentaccounts}&source=$source&memo=$memo&sort=$form->{sort}";
1989   $callback = $form->escape($form->{callback});
1990
1991   
1992   $column_header{name} = "<th><a class=listheading href=$href&sort=name>".$locale->text('Description')."</a></th>";
1993   $column_header{transdate} = "<th><a class=listheading href=$href&sort=transdate>".$locale->text('Date')."</a></th>";
1994   $column_header{paid} = "<th class=listheading>".$locale->text('Amount')."</a></th>";
1995   $column_header{curr} = "<th class=listheading>".$locale->text('Curr')."</a></th>";
1996   $column_header{source} = "<th><a class=listheading href=$href&sort=source>".$locale->text('Source')."</a></th>";
1997   $column_header{memo} = "<th><a class=listheading href=$href&sort=memo>".$locale->text('Memo')."</a></th>";
1998
1999   $column_header{employee} = "<th><a class=listheading href=$href&sort=employee>".$locale->text('Salesperson')."</a></th>";
2000   $column_header{till} = "<th><a class=listheading href=$href&sort=till>".$locale->text('Till')."</a></th>";
2001   
2002
2003   if ($form->{fromdate}) {
2004     $option .= "\n<br>" if ($option);
2005     $option .= $locale->text('From')."&nbsp;".$locale->date(\%myconfig, $form->{fromdate}, 1);
2006   }
2007   if ($form->{todate}) {
2008     $option .= "\n<br>" if ($option);
2009     $option .= $locale->text('To')."&nbsp;".$locale->date(\%myconfig, $form->{todate}, 1);
2010   }
2011
2012   @column_index = @columns;
2013   $colspan = $#column_index + 1;
2014
2015   $form->header;
2016
2017   print qq|
2018 <body>
2019
2020 <table width=100%>
2021   <tr>
2022     <th class=listtop>$form->{title}</th>
2023   </tr>
2024   <tr height="5"></tr>
2025   <tr>
2026     <td>$option</td>
2027   </tr>
2028   <tr>
2029     <td>
2030       <table width=100%>
2031         <tr class=listheading>
2032 |;
2033
2034   map { print "\n$column_header{$_}" } @column_index;
2035
2036   print qq|
2037         </tr>
2038 |;
2039
2040   
2041   foreach $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
2042
2043     next unless @{ $form->{$ref->{id}} };
2044
2045     print qq|
2046         <tr>
2047           <th colspan=$colspan align=left>$ref->{accno}--$ref->{description}</th>
2048         </tr>
2049 |;
2050
2051     if (@{ $form->{$ref->{id}} }) {
2052       $sameitem = $form->{$ref->{id}}[0]->{$form->{sort}};
2053     }
2054     
2055     foreach $payment (@{ $form->{$ref->{id}} }) {
2056
2057       if ($form->{l_subtotal}) {
2058         if ($payment->{$form->{sort}} ne $sameitem) {
2059           # print subtotal
2060           &payment_subtotal;
2061         }
2062       }
2063       
2064       next if ($form->{till} && ! $payment->{till});
2065       
2066       $column_data{name} = "<td>$payment->{name}&nbsp;</td>";
2067       $column_data{transdate} = "<td>$payment->{transdate}&nbsp;</td>";
2068       $column_data{paid} = "<td align=right>".$form->format_amount(\%myconfig, $payment->{paid}, 2, "&nbsp;")."</td>";
2069       $column_data{curr} = "<td>$payment->{curr}</td>";
2070       $column_data{source} = "<td>$payment->{source}&nbsp;</td>";
2071       $column_data{memo} = "<td>$payment->{memo}&nbsp;</td>";
2072       $column_data{employee} = "<td>$payment->{employee}&nbsp;</td>";
2073       $column_data{till} = "<td>$payment->{till}&nbsp;</td>";
2074
2075       $subtotalpaid += $payment->{paid};
2076       $accounttotalpaid += $payment->{paid};
2077       $totalpaid += $payment->{paid};
2078        
2079       $i++; $i %= 2;
2080       print qq|
2081         <tr class=listrow$i>
2082 |;
2083
2084       map { print "\n$column_data{$_}" } @column_index;
2085
2086       print qq|
2087         </tr>
2088 |;
2089
2090       $sameitem = $payment->{$form->{sort}};
2091       
2092     }
2093
2094     &payment_subtotal if $form->{l_subtotal};
2095     
2096     # print account totals
2097     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
2098
2099     $column_data{paid} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $accounttotalpaid, 2, "&nbsp;")."</th>";
2100      
2101     print qq|
2102         <tr class=listtotal>
2103 |;
2104
2105     map { print "\n$column_data{$_}" } @column_index;
2106
2107     print qq|
2108         </tr>
2109 |;
2110
2111     $accounttotalpaid = 0;
2112      
2113   }
2114
2115
2116   # print total
2117   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
2118
2119   $column_data{paid} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;")."</th>";
2120      
2121   print qq|
2122         <tr class=listtotal>
2123 |;
2124
2125   map { print "\n$column_data{$_}" } @column_index;
2126
2127   print qq|
2128         </tr>
2129
2130       </table>
2131     </td>
2132   </tr>
2133   <tr>
2134     <td><hr size=3 noshade></td>
2135   </tr>
2136 </table>
2137
2138 </body>
2139 </html>
2140 |;
2141
2142 }
2143
2144
2145 sub payment_subtotal {
2146
2147   if ($subtotalpaid != 0) {
2148     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
2149
2150     $column_data{paid} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalpaid, 2, "&nbsp;")."</th>";
2151
2152     print qq|
2153   <tr class=listsubtotal>
2154 |;
2155
2156     map { print "\n$column_data{$_}" } @column_index;
2157
2158     print qq|
2159   </tr>
2160 |;
2161   }
2162
2163   $subtotalpaid = 0;
2164
2165 }
2166
2167