Umstellung der Benutzerverwaltung von Dateien im Verzeichnis "users" auf die Verwendu...
[kivitendo-erp.git] / bin / mozilla / rp.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors: Antonio Gallardo <agssa@ibw.com.ni>
16 #                Benjamin Lee <benjaminlee@consultant.com>
17 #                Philip Reetz <p.reetz@linet-services.de>
18 #                Udo Spallek
19 #
20 # This program is free software; you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation; either version 2 of the License, or
23 # (at your option) any later version.
24 #
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 # GNU General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to the Free Software
31 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #======================================================================
33 #
34 # module for preparing Income Statement and Balance Sheet
35 #
36 #======================================================================
37
38 use POSIX qw(strftime);
39
40 use SL::PE;
41 use SL::RP;
42 use SL::Iconv;
43 use SL::ReportGenerator;
44
45 require "bin/mozilla/arap.pl";
46 require "bin/mozilla/common.pl";
47 require "bin/mozilla/reportgenerator.pl";
48
49 1;
50
51 # end of main
52
53 # this is for our long dates
54 # $locale->text('January')
55 # $locale->text('February')
56 # $locale->text('March')
57 # $locale->text('April')
58 # $locale->text('May ')
59 # $locale->text('June')
60 # $locale->text('July')
61 # $locale->text('August')
62 # $locale->text('September')
63 # $locale->text('October')
64 # $locale->text('November')
65 # $locale->text('December')
66
67 # this is for our short month
68 # $locale->text('Jan')
69 # $locale->text('Feb')
70 # $locale->text('Mar')
71 # $locale->text('Apr')
72 # $locale->text('May')
73 # $locale->text('Jun')
74 # $locale->text('Jul')
75 # $locale->text('Aug')
76 # $locale->text('Sep')
77 # $locale->text('Oct')
78 # $locale->text('Nov')
79 # $locale->text('Dec')
80
81 # $locale->text('Balance Sheet')
82 # $locale->text('Income Statement')
83 # $locale->text('Trial Balance')
84 # $locale->text('AR Aging')
85 # $locale->text('AP Aging')
86 # $locale->text('Tax collected')
87 # $locale->text('Tax paid')
88 # $locale->text('Receipts')
89 # $locale->text('Payments')
90 # $locale->text('Project Transactions')
91 # $locale->text('Non-taxable Sales')
92 # $locale->text('Non-taxable Purchases')
93
94 my $rp_access_map = {
95   'projects'         => 'report',
96   'ar_aging'         => 'general_ledger',
97   'ap_aging'         => 'general_ledger',
98   'receipts'         => 'cash',
99   'payments'         => 'cash',
100   'trial_balance'    => 'report',
101   'income_statement' => 'report',
102   'bwa'              => 'report',
103   'balance_sheet'    => 'report',
104 };
105
106 sub check_rp_access {
107   my $right   = $rp_access_map->{$form->{report}};
108   $right    ||= 'DOES_NOT_EXIST';
109
110   $auth->assert($right);
111 }
112
113 sub report {
114   $lxdebug->enter_sub();
115
116   check_rp_access();
117
118   %title = ('balance_sheet'        => 'Balance Sheet',
119             'income_statement'     => 'Income Statement',
120             'trial_balance'        => 'Trial Balance',
121             'ar_aging'             => 'AR Aging',
122             'ap_aging'             => 'Offene Verbindlichkeiten',
123             'tax_collected'        => 'Tax collected',
124             'tax_paid'             => 'Tax paid',
125             'nontaxable_sales'     => 'Non-taxable Sales',
126             'nontaxable_purchases' => 'Non-taxable Purchases',
127             'receipts'             => 'Receipts',
128             'payments'             => 'Payments',
129             'projects'             => 'Project Transactions',
130             'bwa'                  => 'Betriebswirtschaftliche Auswertung',);
131
132   $form->{title} = $locale->text($title{ $form->{report} });
133
134   $accrual = ($eur) ? ""        : "checked";
135   $cash    = ($eur) ? "checked" : "";
136
137   $year = (localtime)[5] + 1900;
138
139   # get departments
140   $form->all_departments(\%myconfig);
141   if (@{ $form->{all_departments} }) {
142     $form->{selectdepartment} = "<option>\n";
143
144     map {
145       $form->{selectdepartment} .=
146         "<option>$_->{description}--$_->{id}\n"
147     } (@{ $form->{all_departments} });
148   }
149
150   $department = qq|
151         <tr>
152           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
153           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
154         </tr>
155 | if $form->{selectdepartment};
156
157   $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
158                                    "all" => 1 });
159
160   my %project_labels = ();
161   my @project_values = ("");
162   foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
163     push(@project_values, $item->{"id"});
164     $project_labels{$item->{"id"}} = $item->{"projectnumber"};
165   }
166
167   my $projectnumber =
168     NTI($cgi->popup_menu('-name' => "project_id",
169                          '-values' => \@project_values,
170                          '-labels' => \%project_labels));
171
172   # use JavaScript Calendar or not
173   $form->{jsscript} = 1;
174   $jsscript = "";
175   if ($form->{report} eq "balance_sheet") {
176     $name_1    = "asofdate";
177     $id_1      = "asofdate";
178     $value_1   = "$form->{asofdate}";
179     $trigger_1 = "trigger1";
180     $name_2    = "compareasofdate";
181     $id_2      = "compareasofdate";
182     $value_2   = "$form->{compareasofdate}";
183     $trigger_2 = "trigger2";
184   } elsif ($form->{report} =~ /(receipts|payments)$/) {
185     $name_1    = "fromdate";
186     $id_1      = "fromdate";
187     $value_1   = "$form->{fromdate}";
188     $trigger_1 = "trigger1";
189     $name_2    = "todate";
190     $id_2      = "todate";
191     $value_2   = "";
192     $trigger_2 = "trigger2";
193   } elsif (($form->{report} eq "ar_aging") || ($form->{report} eq "ap_aging")) {
194     $name_1    = "";
195     $id_1      = "";
196     $value_1   = "";
197     $trigger_1 = "";
198     $name_2    = "todate";
199     $id_2      = "todate";
200     $value_2   = "";
201     $trigger_2 = "trigger2";
202
203   } else {
204     $name_1    = "fromdate";
205     $id_1      = "fromdate";
206     $value_1   = "$form->{fromdate}";
207     $trigger_1 = "trigger1";
208     $name_2    = "todate";
209     $id_2      = "todate";
210     $value_2   = "";
211     $trigger_2 = "trigger2";
212   }
213
214   # with JavaScript Calendar
215   if ($form->{jsscript}) {
216     if ($name_1 eq "") {
217
218       $button1 = qq|
219          <input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
220       $button1_2 = qq|
221         <input type=button name=$name_2 id="$trigger_2" value=|
222         . $locale->text('button') . qq|>|;
223
224       #write Trigger
225       $jsscript =
226         Form->write_trigger(\%myconfig, "1", "$name_2", "BR", "$trigger_2");
227     } else {
228       $button1 = qq|
229          <input name=$name_1 id=$id_1 size=11 title="$myconfig{dateformat}" value="$value_1" onBlur=\"check_right_date_format(this)\">|;
230       $button1_2 = qq|
231         <input type=button name=$name_1 id="$trigger_1" value=|
232         . $locale->text('button') . qq|>|;
233       $button2 = qq|
234          <input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
235       $button2_2 = qq|
236          <input type=button name=$name_2 id="$trigger_2" value=|
237         . $locale->text('button') . qq|>
238        |;
239
240       #write Trigger
241       $jsscript =
242         Form->write_trigger(\%myconfig, "2", "$name_1", "BR", "$trigger_1",
243                             "$name_2", "BL", "$trigger_2");
244     }
245   } else {
246
247     # without JavaScript Calendar
248     if ($name_1 eq "") {
249       $button1 =
250         qq|<input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
251     } else {
252       $button1 =
253         qq|<input name=$name_1 id=$id_1 size=11 title="$myconfig{dateformat}" value=$value_1 onBlur=\"check_right_date_format(this)\">|;
254       $button2 =
255         qq|<input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
256     }
257   }
258   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
259   $form->header;
260   $onload = qq|focus()|;
261   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
262   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
263   print qq|
264 <body onLoad="$onload">
265
266 <form method=post action=$form->{script}>
267
268 <input type=hidden name=title value="$form->{title}">
269
270 <table width=100%>
271   <tr>
272     <th class=listtop>$form->{title}</th>
273   </tr>
274   <tr height="5"></tr>
275   <tr>
276     <td>
277       <table>
278       $department
279 |;
280
281   if ($form->{report} eq "projects") {
282     print qq|
283         <tr>
284           <th align=right nowrap>| . $locale->text('Project') . qq|</th>
285           <td colspan=5><input name=projectnumber size=25</td>
286         </tr>
287         <input type=hidden name=nextsub value=generate_projects>
288         <tr>
289           <th align=right>| . $locale->text('From') . qq|</th>
290           <td>$button1</td>
291           <td>$button1_2</td>
292           <th align=right>| . $locale->text('Bis') . qq|</th>
293           <td>$button2</td>
294           <td>$button2_2</td>
295         </tr>
296       </table>
297     </td>
298   </tr>
299   <tr>
300     <td>
301       <table>
302         <tr>
303           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
304           <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|
305       . $locale->text('Heading') . qq|
306           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
307       . $locale->text('Subtotal') . qq|</td>
308         </tr>
309
310 $jsscript
311 |;
312   }
313
314   if ($form->{report} eq "income_statement") {
315     print qq|
316         <tr>
317           <th align=right nowrap>| . $locale->text('Project') . qq|</th>
318           <td colspan=3>$projectnumber</td>
319         </tr>
320         <input type=hidden name=nextsub value=generate_income_statement>
321 </table>
322 <table>
323         <tr>
324           <th align=left><input name=reporttype class=radio type=radio value="custom" checked> |
325       . $locale->text('Customized Report') . qq|</th>
326         </tr>
327         <tr>
328           <th colspan=1>| . $locale->text('Year') . qq|</th>
329           <td><input name=year size=11 title="|
330       . $locale->text('YYYY') . qq|" value="$year"></td>
331         </tr>
332 |;
333
334     print qq|
335         <tr>
336                 <td align=right>
337 <b> | . $locale->text('Yearly') . qq|</b> </td>
338                 <th align=left>| . $locale->text('Quarterly') . qq|</th>
339                 <th align=left colspan=3>| . $locale->text('Monthly') . qq|</th>
340         </tr>
341         <tr>
342                 <td align=right>&nbsp; <input name=duetyp class=radio type=radio value="13"
343 "checked"></td>
344                 <td><input name=duetyp class=radio type=radio value="A" $checked >&nbsp;1. |
345       . $locale->text('Quarter') . qq|</td>
346 |;
347     $checked = "";
348     print qq|
349                 <td><input name=duetyp class=radio type=radio value="1" $checked >&nbsp;|
350       . $locale->text('January') . qq|</td>
351 |;
352     $checked = "";
353     print qq|
354                 <td><input name=duetyp class=radio type=radio value="5" $checked >&nbsp;|
355       . $locale->text('May') . qq|</td>
356                 <td><input name=duetyp class=radio type=radio value="9" $checked >&nbsp;|
357       . $locale->text('September') . qq|</td>
358
359         </tr>
360         <tr>
361                 <td align= right>&nbsp;</td>
362                 <td><input name=duetyp class=radio type=radio value="B" $checked>&nbsp;2. |
363       . $locale->text('Quarter') . qq|</td>
364                 <td><input name=duetyp class=radio type=radio value="2" $checked >&nbsp;|
365       . $locale->text('February') . qq|</td>
366                 <td><input name=duetyp class=radio type=radio value="6" $checked >&nbsp;|
367       . $locale->text('June') . qq|</td>
368                 <td><input name=duetyp class=radio type=radio value="10" $checked >&nbsp;|
369       . $locale->text('October') . qq|</td>
370         </tr>
371         <tr>
372                 <td> &nbsp;</td>
373                 <td><input name=duetyp class=radio type=radio value="C" $checked>&nbsp;3. |
374       . $locale->text('Quarter') . qq|</td>
375                 <td><input name=duetyp class=radio type=radio value="3" $checked >&nbsp;|
376       . $locale->text('March') . qq|</td>
377                 <td><input name=duetyp class=radio type=radio value="7" $checked >&nbsp;|
378       . $locale->text('July') . qq|</td>
379                 <td><input name=duetyp class=radio type=radio value="11" $checked >&nbsp;|
380       . $locale->text('November') . qq|</td>
381
382         </tr>
383         <tr>
384                 <td> &nbsp;</td>
385                 <td><input name=duetyp class=radio type=radio value="D" $checked>&nbsp;4. |
386       . $locale->text('Quarter') . qq|&nbsp;</td>
387                 <td><input name=duetyp class=radio type=radio value="4" $checked >&nbsp;|
388       . $locale->text('April') . qq|</td>
389                 <td><input name=duetyp class=radio type=radio value="8" $checked >&nbsp;|
390       . $locale->text('August') . qq|</td>
391                 <td><input name=duetyp class=radio type=radio value="12" $checked >&nbsp;|
392       . $locale->text('December') . qq|</td>
393
394         </tr>
395         <tr>
396                 <td colspan=5><hr size=3 noshade></td>
397         </tr>
398         <tr>
399           <th align=left><input name=reporttype class=radio type=radio value="free" $checked> |
400       . $locale->text('Free report period') . qq|</th>
401           <td align=left colspan=4>| . $locale->text('From') . qq|&nbsp;
402               $button1
403               $button1_2&nbsp;
404               | . $locale->text('Bis') . qq|
405               $button2
406               $button2_2&nbsp;
407           </td>
408         </tr>
409         <tr>
410                 <td colspan=5><hr size=3 noshade></td>
411         </tr>
412         <tr>
413           <th align=leftt>| . $locale->text('Method') . qq|</th>
414           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
415       . $locale->text('Accrual') . qq|
416           &nbsp;<input name=method class=radio type=radio value=cash $cash>|
417       . $locale->text('EUR') . qq|</td>
418         </tr>
419
420 $jsscript
421 |;
422   }
423
424   if ($form->{report} eq "bwa") {
425     print qq|
426         <tr>
427           <th align=right nowrap>| . $locale->text('Project') . qq|</th>
428           <td colspan=3>$projectnumber</td>
429         </tr>
430         <input type=hidden name=nextsub value=generate_bwa>
431 </table>
432 <table>
433         <tr>
434           <th align=left><input name=reporttype class=radio type=radio value="custom" checked> |
435       . $locale->text('Customized Report') . qq|</th>
436         </tr>
437         <tr>
438           <th colspan=1>| . $locale->text('Year') . qq|</th>
439           <td><input name=year size=11 title="|
440       . $locale->text('YYYY') . qq|" value="$year"></td>
441         </tr>
442 |;
443
444     print qq|
445         <tr>
446                 <td align=right>
447 <b> | . $locale->text('Yearly') . qq|</b> </td>
448                 <th align=left>| . $locale->text('Quarterly') . qq|</th>
449                 <th align=left colspan=3>| . $locale->text('Monthly') . qq|</th>
450         </tr>
451         <tr>
452                 <td align=right>&nbsp; <input name=duetyp class=radio type=radio value="13"
453 $checked></td>
454                 <td><input name=duetyp class=radio type=radio value="A" $checked >&nbsp;1. |
455       . $locale->text('Quarter') . qq|</td>
456 |;
457     $checked = "checked";
458     print qq|
459                 <td><input name=duetyp class=radio type=radio value="1" $checked >&nbsp;|
460       . $locale->text('January') . qq|</td>
461 |;
462     $checked = "";
463     print qq|
464                 <td><input name=duetyp class=radio type=radio value="5" $checked >&nbsp;|
465       . $locale->text('May') . qq|</td>
466                 <td><input name=duetyp class=radio type=radio value="9" $checked >&nbsp;|
467       . $locale->text('September') . qq|</td>
468
469         </tr>
470         <tr>
471                 <td align= right>&nbsp;</td>
472                 <td><input name=duetyp class=radio type=radio value="B" $checked>&nbsp;2. |
473       . $locale->text('Quarter') . qq|</td>
474                 <td><input name=duetyp class=radio type=radio value="2" $checked >&nbsp;|
475       . $locale->text('February') . qq|</td>
476                 <td><input name=duetyp class=radio type=radio value="6" $checked >&nbsp;|
477       . $locale->text('June') . qq|</td>
478                 <td><input name=duetyp class=radio type=radio value="10" $checked >&nbsp;|
479       . $locale->text('October') . qq|</td>
480         </tr>
481         <tr>
482                 <td> &nbsp;</td>
483                 <td><input name=duetyp class=radio type=radio value="C" $checked>&nbsp;3. |
484       . $locale->text('Quarter') . qq|</td>
485                 <td><input name=duetyp class=radio type=radio value="3" $checked >&nbsp;|
486       . $locale->text('March') . qq|</td>
487                 <td><input name=duetyp class=radio type=radio value="7" $checked >&nbsp;|
488       . $locale->text('July') . qq|</td>
489                 <td><input name=duetyp class=radio type=radio value="11" $checked >&nbsp;|
490       . $locale->text('November') . qq|</td>
491
492         </tr>
493         <tr>
494                 <td> &nbsp;</td>
495                 <td><input name=duetyp class=radio type=radio value="D" $checked>&nbsp;4. |
496       . $locale->text('Quarter') . qq|&nbsp;</td>
497                 <td><input name=duetyp class=radio type=radio value="4" $checked >&nbsp;|
498       . $locale->text('April') . qq|</td>
499                 <td><input name=duetyp class=radio type=radio value="8" $checked >&nbsp;|
500       . $locale->text('August') . qq|</td>
501                 <td><input name=duetyp class=radio type=radio value="12" $checked >&nbsp;|
502       . $locale->text('December') . qq|</td>
503
504         </tr>
505         <tr>
506                 <td colspan=5><hr size=3 noshade></td>
507         </tr>
508         <tr>
509           <th align=left><input name=reporttype class=radio type=radio value="free" $checked> |
510       . $locale->text('Free report period') . qq|</th>
511           <td align=left colspan=4>| . $locale->text('From') . qq|&nbsp;
512               $button1
513               $button1_2&nbsp;
514               | . $locale->text('Bis') . qq|&nbsp;
515               $button2
516               $button2_2
517           </td>
518         </tr>
519         <tr>
520                 <td colspan=5><hr size=3 noshade></td>
521         </tr>
522         <tr>
523           <th align=leftt>| . $locale->text('Method') . qq|</th>
524           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
525       . $locale->text('Accrual') . qq|
526           &nbsp;<input name=method class=radio type=radio value=cash $cash>|
527       . $locale->text('EUR') . qq|</td>
528         </tr>
529         <tr>
530          <th align=right colspan=4>|
531       . $locale->text('Decimalplaces')
532       . qq|</th>
533              <td><input name=decimalplaces size=3 value="2"></td>
534          </tr>
535                                     
536 $jsscript
537 |;
538   }
539
540   if ($form->{report} eq "balance_sheet") {
541     print qq|
542         <input type=hidden name=nextsub value=generate_balance_sheet>
543         <tr>
544           <th align=right>| . $locale->text('as at') . qq|</th>
545           <td>
546             $button1
547             $button1_2
548           </td>
549           <th align=right nowrap>| . $locale->text('Compare to') . qq|</th>
550           <td>
551           $button2
552           $button2_2
553           </td>
554         </tr>
555         <tr>
556           <th align=right>| . $locale->text('Decimalplaces') . qq|</th>
557           <td><input name=decimalplaces size=3 value="2"></td>
558         </tr>
559       </table>
560     </td>
561   </tr>
562   <tr>
563     <td>
564       <table>
565         <tr>
566           <th align=right>| . $locale->text('Method') . qq|</th>
567           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
568       . $locale->text('Accrual') . qq|
569           &nbsp;<input name=method class=radio type=radio value=cash $cash>|
570       . $locale->text('EUR') . qq|</td>
571         </tr>
572
573         <tr>
574           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
575           <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|
576       . $locale->text('Heading') . qq|
577           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
578       . $locale->text('Subtotal') . qq|
579           <input name=l_accno class=checkbox type=checkbox value=Y>&nbsp;|
580       . $locale->text('Account Number') . qq|</td>
581         </tr>
582
583 $jsscript
584 |;
585   }
586
587   if ($form->{report} eq "trial_balance") {
588     print qq|
589         <input type=hidden name=nextsub value=generate_trial_balance>
590         <input type=hidden name=eur value=$eur>
591        <tr>
592           <th align=right>| . $locale->text('From') . qq|</th>
593           <td>
594             $button1
595             $button1_2
596           </td>
597           <th align=right>| . $locale->text('Bis') . qq|</th>
598           <td>
599             $button2
600             $button2_2
601           </td>
602         </tr>
603       </table>
604     </td>
605   </tr>
606   <tr>
607     <td>
608       <table>
609         <tr>
610           <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
611           <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;|
612       . $locale->text('Heading') . qq|
613           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
614       . $locale->text('Subtotal') . qq|
615           <input name=all_accounts class=checkbox type=checkbox value=Y>&nbsp;|
616       . $locale->text('All Accounts') . qq|</td>
617         </tr>
618
619 $jsscript
620 |;
621   }
622
623   if ($form->{report} =~ /^tax_/) {
624     $form->{db} = ($form->{report} =~ /_collected/) ? "ar" : "ap";
625
626     RP->get_taxaccounts(\%myconfig, \%$form);
627
628     print qq|
629         <input type=hidden name=nextsub value=generate_tax_report>
630         <tr>
631           <th align=right>| . $locale->text('From') . qq|</th>
632           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
633           <th align=right>| . $locale->text('Bis') . qq|</th>
634           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
635         </tr>
636         <tr>
637           <th align=right>| . $locale->text('Report for') . qq|</th>
638           <td colspan=3>
639 |;
640
641     $checked = "checked";
642     foreach $ref (@{ $form->{taxaccounts} }) {
643
644       print
645         qq|<input name=accno class=radio type=radio value=$ref->{accno} $checked>&nbsp;$ref->{description}
646
647     <input name="$ref->{accno}_description" type=hidden value="$ref->{description}">
648     <input name="$ref->{accno}_rate" type=hidden value="$ref->{rate}">|;
649
650       $checked = "";
651
652     }
653
654     print qq|
655   <input type=hidden name=db value=$form->{db}>
656   <input type=hidden name=sort value=transdate>
657
658           </td>
659         </tr>
660         <tr>
661           <th align=right>| . $locale->text('Method') . qq|</th>
662           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
663       . $locale->text('Accrual') . qq|
664           &nbsp;<input name=method class=radio type=radio value=cash $cash>|
665       . $locale->text('EUR') . qq|</td>
666         </tr>
667       </table>
668     </td>
669   </tr>
670   <tr>
671     <td>
672       <table>
673         <tr>
674           <th align=right>| . $locale->text('Include in Report') . qq|</th>
675           <td>
676             <table>
677               <tr>
678                 <td><input name="l_id" class=checkbox type=checkbox value=Y></td>
679                 <td>| . $locale->text('ID') . qq|</td>
680                 <td><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
681                 <td>| . $locale->text('Invoice') . qq|</td>
682                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
683                 <td>| . $locale->text('Date') . qq|</td>
684               </tr>
685               <tr>
686                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
687                 <td>|;
688
689     if ($form->{db} eq 'ar') {
690       print $locale->text('Customer');
691     }
692     if ($form->{db} eq 'ap') {
693       print $locale->text('Vendor');
694     }
695
696     print qq|</td>
697                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y checked></td>
698                 <td>| . $locale->text('Amount') . qq|</td>
699                 <td><input name="l_tax" class=checkbox type=checkbox value=Y checked></td>
700                 <td>| . $locale->text('Tax') . qq|</td>
701                 <td><input name="l_amount" class=checkbox type=checkbox value=Y></td>
702                 <td>| . $locale->text('Total') . qq|</td>
703               </tr>
704               <tr>
705                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
706                 <td>| . $locale->text('Subtotal') . qq|</td>
707               </tr>
708             </table>
709           </td>
710         </tr>
711 |;
712
713   }
714
715   if ($form->{report} =~ /^nontaxable_/) {
716     $form->{db} = ($form->{report} =~ /_sales/) ? "ar" : "ap";
717
718     print qq|
719         <input type=hidden name=nextsub value=generate_tax_report>
720
721         <input type=hidden name=db value=$form->{db}>
722         <input type=hidden name=sort value=transdate>
723         <input type=hidden name=report value=$form->{report}>
724
725         <tr>
726           <th align=right>| . $locale->text('From') . qq|</th>
727           <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
728           <th align=right>| . $locale->text('Bis') . qq|</th>
729           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
730         </tr>
731         <tr>
732           <th align=right>| . $locale->text('Method') . qq|</th>
733           <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
734       . $locale->text('Accrual') . qq|
735           &nbsp;<input name=method class=radio type=radio value=cash $cash>|
736       . $locale->text('EUR') . qq|</td>
737         </tr>
738         <tr>
739           <th align=right>| . $locale->text('Include in Report') . qq|</th>
740           <td colspan=3>
741             <table>
742               <tr>
743                 <td><input name="l_id" class=checkbox type=checkbox value=Y></td>
744                 <td>| . $locale->text('ID') . qq|</td>
745                 <td><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
746                 <td>| . $locale->text('Invoice') . qq|</td>
747                 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
748                 <td>| . $locale->text('Date') . qq|</td>
749               </tr>
750               <tr>
751                 <td><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
752                 <td>|;
753
754     if ($form->{db} eq 'ar') {
755       print $locale->text('Customer');
756     }
757     if ($form->{db} eq 'ap') {
758       print $locale->text('Vendor');
759     }
760
761     print qq|</td>
762                 <td><input name="l_netamount" class=checkbox type=checkbox value=Y checked></td>
763                 <td>| . $locale->text('Amount') . qq|</td>
764                 <td><input name="l_amount" class=checkbox type=checkbox value=Y></td>
765                 <td>| . $locale->text('Total') . qq|</td>
766               </tr>
767               <tr>
768                 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
769                 <td>| . $locale->text('Subtotal') . qq|</td>
770               </tr>
771             </table>
772           </td>
773         </tr>
774 |;
775
776   }
777
778   if (($form->{report} eq "ar_aging") || ($form->{report} eq "ap_aging")) {
779     if ($form->{report} eq 'ar_aging') {
780       $label = $locale->text('Customer');
781       $form->{vc} = 'customer';
782     } else {
783       $label = $locale->text('Vendor');
784       $form->{vc} = 'vendor';
785     }
786
787     $nextsub = "generate_$form->{report}";
788
789     # setup vc selection
790     $form->all_vc(\%myconfig, $form->{vc},
791                   ($form->{vc} eq 'customer') ? "AR" : "AP");
792
793     map { $vc .= "<option>$_->{name}--$_->{id}\n" }
794       @{ $form->{"all_$form->{vc}"} };
795
796     $vc =
797       ($vc)
798       ? qq|<select name=$form->{vc}><option>\n$vc</select>|
799       : qq|<input name=$form->{vc} size=35>|;
800
801     print qq|
802         <tr>
803           <th align=right>| . $locale->text($label) . qq|</th>
804           <td>$vc</td>
805         </tr>
806         <tr>
807           <th align=right>| . $locale->text('Bis') . qq|</th>
808           <td>
809             $button1
810             $button1_2
811           </td>
812         </tr>
813         <input type=hidden name=type value=statement>
814         <input type=hidden name=format value=html>
815         <input type=hidden name=media value=screen>
816
817         <input type=hidden name=nextsub value=$nextsub>
818         <input type=hidden name=action value=$nextsub>
819
820 $jsscript
821 |;
822   }
823
824   # above action can be removed if there is more than one input field
825
826   if ($form->{report} =~ /(receipts|payments)$/) {
827     $form->{db} = ($form->{report} =~ /payments$/) ? "ap" : "ar";
828
829     RP->paymentaccounts(\%myconfig, \%$form);
830
831     $selection = "<option>\n";
832     foreach $ref (@{ $form->{PR} }) {
833       $paymentaccounts .= "$ref->{accno} ";
834       $selection       .= "<option>$ref->{accno}--$ref->{description}\n";
835     }
836
837     chop $paymentaccounts;
838
839     print qq|
840         <input type=hidden name=nextsub value=list_payments>
841         <tr>
842           <th align=right nowrap>| . $locale->text('Account') . qq|</th>
843           <td colspan=3><select name=account>$selection</select>
844             <input type=hidden name=paymentaccounts value="$paymentaccounts">
845           </td>
846         </tr>
847         <tr>
848           <th align=right>| . $locale->text('Reference') . qq|</th>
849           <td colspan=3><input name=reference></td>
850         </tr>
851         <tr>
852           <th align=right nowrap>| . $locale->text('Source') . qq|</th>
853           <td colspan=3><input name=source></td>
854         </tr>
855         <tr>
856           <th align=right nowrap>| . $locale->text('Memo') . qq|</th>
857           <td colspan=3><input name=memo size=30></td>
858         </tr>
859         <tr>
860           <th align=right>| . $locale->text('From') . qq|</th>
861           <td>
862             $button1
863             $button1_2
864           </td>
865           <th align=right>| . $locale->text('Bis') . qq|</th>
866           <td>
867             $button2
868             $button2_2
869           </td>
870         </tr>
871         <tr>
872           <td align=right><input type=checkbox style=checkbox name=fx_transaction value=1 checked></td>
873           <th align=left colspan=3>|
874       . $locale->text('Include Exchangerate Difference') . qq|</td>
875         </tr>
876
877 $jsscript
878
879           <input type=hidden name=db value=$form->{db}>
880           <input type=hidden name=sort value=transdate>
881 |;
882
883   }
884
885   print qq|
886
887       </table>
888     </td>
889   </tr>
890   <tr>
891     <td><hr size=3 noshade></td>
892   </tr>
893 </table>
894
895 <br>
896 <input type=submit class=submit name=action value="|
897     . $locale->text('Continue') . qq|">
898
899 </form>
900
901 </body>
902 </html>
903 |;
904
905   $lxdebug->leave_sub();
906 }
907
908 sub continue { call_sub($form->{"nextsub"}); }
909
910 sub get_project {
911   $lxdebug->enter_sub();
912
913   $auth->assert('report');
914
915   my $nextsub = shift;
916
917   $form->{project_id} = $form->{project_id_1};
918   if ($form->{projectnumber} && !$form->{project_id}) {
919     $form->{rowcount} = 1;
920
921     # call this instead of update
922     $form->{update}          = $nextsub;
923     $form->{projectnumber_1} = $form->{projectnumber};
924
925     delete $form->{sort};
926     &check_project;
927
928     # if there is one only, assign id
929     $form->{project_id} = $form->{project_id_1};
930   }
931
932   $lxdebug->leave_sub();
933 }
934
935 sub generate_income_statement {
936   $lxdebug->enter_sub();
937
938   $auth->assert('report');
939
940   $form->{padding} = "&nbsp;&nbsp;";
941   $form->{bold}    = "<b>";
942   $form->{endbold} = "</b>";
943   $form->{br}      = "<br>";
944
945   if ($form->{reporttype} eq "custom") {
946
947     #forgotten the year --> thisyear
948     if ($form->{year} !~ m/^\d\d\d\d$/) {
949       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
950         /(\d\d\d\d)/;
951       $form->{year} = $1;
952     }
953
954     #yearly report
955     if ($form->{duetyp} eq "13") {
956       $form->{fromdate} = "1.1.$form->{year}";
957       $form->{todate}   = "31.12.$form->{year}";
958     }
959
960     #Quater reports
961     if ($form->{duetyp} eq "A") {
962       $form->{fromdate} = "1.1.$form->{year}";
963       $form->{todate}   = "31.3.$form->{year}";
964     }
965     if ($form->{duetyp} eq "B") {
966       $form->{fromdate} = "1.4.$form->{year}";
967       $form->{todate}   = "30.6.$form->{year}";
968     }
969     if ($form->{duetyp} eq "C") {
970       $form->{fromdate} = "1.7.$form->{year}";
971       $form->{todate}   = "30.9.$form->{year}";
972     }
973     if ($form->{duetyp} eq "D") {
974       $form->{fromdate} = "1.10.$form->{year}";
975       $form->{todate}   = "31.12.$form->{year}";
976     }
977
978     #Monthly reports
979   SWITCH: {
980       $form->{duetyp} eq "1" && do {
981         $form->{fromdate} = "1.1.$form->{year}";
982         $form->{todate}   = "31.1.$form->{year}";
983         last SWITCH;
984       };
985       $form->{duetyp} eq "2" && do {
986         $form->{fromdate} = "1.2.$form->{year}";
987
988         #this works from 1901 to 2099, 1900 and 2100 fail.
989         $leap = ($form->{year} % 4 == 0) ? "29" : "28";
990         $form->{todate} = "$leap.2.$form->{year}";
991         last SWITCH;
992       };
993       $form->{duetyp} eq "3" && do {
994         $form->{fromdate} = "1.3.$form->{year}";
995         $form->{todate}   = "31.3.$form->{year}";
996         last SWITCH;
997       };
998       $form->{duetyp} eq "4" && do {
999         $form->{fromdate} = "1.4.$form->{year}";
1000         $form->{todate}   = "30.4.$form->{year}";
1001         last SWITCH;
1002       };
1003       $form->{duetyp} eq "5" && do {
1004         $form->{fromdate} = "1.5.$form->{year}";
1005         $form->{todate}   = "31.5.$form->{year}";
1006         last SWITCH;
1007       };
1008       $form->{duetyp} eq "6" && do {
1009         $form->{fromdate} = "1.6.$form->{year}";
1010         $form->{todate}   = "30.6.$form->{year}";
1011         last SWITCH;
1012       };
1013       $form->{duetyp} eq "7" && do {
1014         $form->{fromdate} = "1.7.$form->{year}";
1015         $form->{todate}   = "31.7.$form->{year}";
1016         last SWITCH;
1017       };
1018       $form->{duetyp} eq "8" && do {
1019         $form->{fromdate} = "1.8.$form->{year}";
1020         $form->{todate}   = "31.8.$form->{year}";
1021         last SWITCH;
1022       };
1023       $form->{duetyp} eq "9" && do {
1024         $form->{fromdate} = "1.9.$form->{year}";
1025         $form->{todate}   = "30.9.$form->{year}";
1026         last SWITCH;
1027       };
1028       $form->{duetyp} eq "10" && do {
1029         $form->{fromdate} = "1.10.$form->{year}";
1030         $form->{todate}   = "31.10.$form->{year}";
1031         last SWITCH;
1032       };
1033       $form->{duetyp} eq "11" && do {
1034         $form->{fromdate} = "1.11.$form->{year}";
1035         $form->{todate}   = "30.11.$form->{year}";
1036         last SWITCH;
1037       };
1038       $form->{duetyp} eq "12" && do {
1039         $form->{fromdate} = "1.12.$form->{year}";
1040         $form->{todate}   = "31.12.$form->{year}";
1041         last SWITCH;
1042       };
1043     }
1044   }
1045
1046   RP->income_statement(\%myconfig, \%$form);
1047
1048   ($form->{department}) = split /--/, $form->{department};
1049
1050   $form->{period} =
1051     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1052   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1053
1054   # if there are any dates construct a where
1055   if ($form->{fromdate} || $form->{todate}) {
1056
1057     unless ($form->{todate}) {
1058       $form->{todate} = $form->current_date(\%myconfig);
1059     }
1060
1061     $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1);
1062     $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
1063
1064     $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1);
1065     $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
1066
1067     $form->{this_period} = "$shortfromdate\n$shorttodate";
1068     $form->{period}      =
1069         $locale->text('for Period')
1070       . qq|\n$longfromdate |
1071       . $locale->text('Bis')
1072       . qq| $longtodate|;
1073   }
1074
1075   if ($form->{comparefromdate} || $form->{comparetodate}) {
1076     $longcomparefromdate =
1077       $locale->date(\%myconfig, $form->{comparefromdate}, 1);
1078     $shortcomparefromdate =
1079       $locale->date(\%myconfig, $form->{comparefromdate}, 0);
1080
1081     $longcomparetodate  = $locale->date(\%myconfig, $form->{comparetodate}, 1);
1082     $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
1083
1084     $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
1085     $form->{period} .=
1086         "\n$longcomparefromdate "
1087       . $locale->text('Bis')
1088       . qq| $longcomparetodate|;
1089   }
1090
1091   # setup variables for the form
1092   @a = qw(company address businessnumber);
1093   map { $form->{$_} = $myconfig{$_} } @a;
1094
1095   $form->{templates} = $myconfig{templates};
1096
1097   $form->{IN} = "income_statement.html";
1098
1099   $form->parse_template;
1100
1101   $lxdebug->leave_sub();
1102 }
1103
1104 sub generate_balance_sheet {
1105   $lxdebug->enter_sub();
1106
1107   $auth->assert('report');
1108
1109   $form->{padding} = "&nbsp;&nbsp;";
1110   $form->{bold}    = "<b>";
1111   $form->{endbold} = "</b>";
1112   $form->{br}      = "<br>";
1113
1114   RP->balance_sheet(\%myconfig, \%$form);
1115
1116   $form->{asofdate} = $form->current_date(\%myconfig) unless $form->{asofdate};
1117   $form->{period} =
1118     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1119
1120   ($form->{department}) = split /--/, $form->{department};
1121
1122   # define Current Earnings account
1123   $padding = ($form->{l_heading}) ? $form->{padding} : "";
1124   push(@{ $form->{equity_account} },
1125        $padding . $locale->text('Current Earnings'));
1126
1127   $form->{this_period} = $locale->date(\%myconfig, $form->{asofdate}, 0);
1128   $form->{last_period} =
1129     $locale->date(\%myconfig, $form->{compareasofdate}, 0);
1130
1131   $form->{IN} = "balance_sheet.html";
1132
1133   # setup company variables for the form
1134   map { $form->{$_} = $myconfig{$_}; } (qw(company address businessnumber nativecurr));
1135
1136   $form->{templates} = $myconfig{templates};
1137
1138   $form->parse_template;
1139
1140   $lxdebug->leave_sub();
1141 }
1142
1143 sub generate_projects {
1144   $lxdebug->enter_sub();
1145
1146   $auth->assert('report');
1147
1148   &get_project(generate_projects);
1149   $form->{projectnumber} = $form->{projectnumber_1};
1150
1151   $form->{nextsub} = "generate_projects";
1152   $form->{title}   = $locale->text('Project Transactions');
1153   RP->trial_balance(\%myconfig, \%$form);
1154
1155   list_accounts('generate_projects');
1156
1157   $lxdebug->leave_sub();
1158 }
1159
1160 # Antonio Gallardo
1161 #
1162 # D.S. Feb 16, 2001
1163 # included links to display transactions for period entered
1164 # added headers and subtotals
1165 #
1166 sub generate_trial_balance {
1167   $lxdebug->enter_sub();
1168
1169   $auth->assert('report');
1170
1171   # get for each account initial balance, debits and credits
1172   RP->trial_balance(\%myconfig, \%$form);
1173
1174   $form->{nextsub} = "generate_trial_balance";
1175   $form->{title}   = $locale->text('Trial Balance');
1176   list_accounts('generate_trial_balance');
1177
1178   $lxdebug->leave_sub();
1179 }
1180
1181 sub create_list_accounts_subtotal_row {
1182   $lxdebug->enter_sub();
1183
1184   my ($subtotals, $columns, $fields, $class) = @_;
1185
1186   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
1187
1188   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
1189
1190   $lxdebug->leave_sub();
1191
1192   return $row;
1193 }
1194
1195 sub list_accounts {
1196   $lxdebug->enter_sub();
1197
1198   my ($action) = @_;
1199
1200   my @options;
1201   if ($form->{department}) {
1202     my ($department) = split /--/, $form->{department};
1203     push @options, $locale->text('Department') . " : $department";
1204   }
1205   if ($form->{projectnumber}) {
1206     push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
1207   }
1208
1209   # if there are any dates
1210   if ($form->{fromdate} || $form->{todate}) {
1211     my ($fromdate, $todate);
1212
1213     if ($form->{fromdate}) {
1214       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
1215     }
1216     if ($form->{todate}) {
1217       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
1218     }
1219
1220     push @options, "$fromdate - $todate";
1221
1222   } else {
1223     push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1224   }
1225
1226   my @columns     = qw(accno description begbalance debit credit endbalance);
1227   my %column_defs = (
1228     'accno'       => { 'text' => $locale->text('Account'), },
1229     'description' => { 'text' => $locale->text('Description'), },
1230     'debit'       => { 'text' => $locale->text('Debit'), },
1231     'credit'      => { 'text' => $locale->text('Credit'), },
1232     'begbalance'  => { 'text' => $locale->text('Balance'), },
1233     'endbalance'  => { 'text' => $locale->text('Balance'), },
1234   );
1235   my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
1236
1237   my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
1238
1239   $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
1240
1241   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1242
1243   $report->set_options('top_info_text'         => join("\n", @options),
1244                        'output_format'         => 'HTML',
1245                        'title'                 => $form->{title},
1246                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
1247                        'std_column_visibility' => 1,
1248     );
1249   $report->set_options_from_form();
1250
1251   $report->set_columns(%column_defs);
1252   $report->set_column_order(@columns);
1253
1254   $report->set_export_options($action, @hidden_variables);
1255
1256   $report->set_sort_indicator('accno', 1);
1257
1258   my @totals_columns = qw(credit debit begbalance endbalance);
1259   my %subtotals      = map { $_ => 0 } @totals_columns;
1260   my %totals         = map { $_ => 0 } @totals_columns;
1261   my $found_heading  = 0;
1262   my @tb             = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} };
1263
1264   # sort the whole thing by account numbers and display
1265   foreach my $idx (0 .. scalar(@tb) - 1) {
1266     my $ref  = $tb[$idx];
1267     my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
1268
1269     my $ml   = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
1270
1271     my $row  = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
1272
1273     if ($ref->{charttype} eq 'H') {
1274       next unless ($form->{l_heading});
1275
1276       %subtotals                   = map { $_ => 0 } @totals_columns;
1277       $found_heading               = 1;
1278       $row->{description}->{class} = 'listheading';
1279       $row->{description}->{data}  = $ref->{description};
1280
1281       $report->add_data($row);
1282
1283       next;
1284     }
1285
1286     foreach (qw(debit credit)) {
1287       $subtotals{$_} += $ref->{$_};
1288       $totals{$_}    += $ref->{$_};
1289     }
1290
1291     $subtotals{begbalance} += $ref->{balance} * $ml;
1292     $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
1293
1294     map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
1295     map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
1296
1297     $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
1298     $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
1299
1300     $report->add_data($row);
1301
1302     if ($form->{l_heading} && $found_heading &&
1303         (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
1304       $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
1305     }
1306   }
1307
1308   $report->add_separator();
1309
1310   $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
1311
1312   $report->generate_with_headers();
1313
1314   $lxdebug->leave_sub();
1315 }
1316
1317 sub generate_ar_aging {
1318   $lxdebug->enter_sub();
1319
1320   $auth->assert('general_ledger');
1321
1322   # split customer
1323   ($form->{customer}) = split(/--/, $form->{customer});
1324
1325   $form->{ct}   = "customer";
1326   $form->{arap} = "ar";
1327
1328   $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
1329
1330   RP->aging(\%myconfig, \%$form);
1331   aging();
1332
1333   $lxdebug->leave_sub();
1334 }
1335
1336 sub generate_ap_aging {
1337   $lxdebug->enter_sub();
1338
1339   $auth->assert('general_ledger');
1340
1341   # split vendor
1342   ($form->{vendor}) = split(/--/, $form->{vendor});
1343
1344   $form->{ct}   = "vendor";
1345   $form->{arap} = "ap";
1346
1347   $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
1348
1349   RP->aging(\%myconfig, \%$form);
1350   aging();
1351
1352   $lxdebug->leave_sub();
1353 }
1354
1355 sub create_aging_subtotal_row {
1356   $lxdebug->enter_sub();
1357
1358   my ($subtotals, $columns, $periods, $class) = @_;
1359
1360   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
1361
1362   foreach (@{ $periods }) {
1363     $row->{"c$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
1364     $subtotals->{$_}      = 0;
1365   }
1366
1367   $lxdebug->leave_sub();
1368
1369   return $row;
1370 }
1371
1372 sub aging {
1373   $lxdebug->enter_sub();
1374
1375   $auth->assert('general_ledger');
1376
1377   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1378
1379   my @columns = qw(statement ct invnumber transdate duedate c0 c30 c60 c90);
1380
1381   my %column_defs = (
1382     'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
1383     'ct'        => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1384     'invnumber' => { 'text' => $locale->text('Invoice'), },
1385     'transdate' => { 'text' => $locale->text('Date'), },
1386     'duedate'   => { 'text' => $locale->text('Due'), },
1387     'c0'        => { 'text' => $locale->text('Current'), },
1388     'c30'       => { 'text' => '30', },
1389     'c60'       => { 'text' => '60', },
1390     'c90'       => { 'text' => '90', },
1391   );
1392
1393   my %column_alignment = ('statement' => 'center',
1394                           map { $_ => 'right' } qw(c0 c30 c60 c90));
1395
1396   $report->set_options('std_column_visibility' => 1);
1397   $report->set_columns(%column_defs);
1398   $report->set_column_order(@columns);
1399
1400   my @hidden_variables = qw(todate customer vendor arap title ct);
1401   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
1402
1403   my @options;
1404
1405   if ($form->{department}) {
1406     my ($department) = split /--/, $form->{department};
1407     push @options, $locale->text('Department') . " : $department";
1408     $form->{callback} .= "&department=" . E($department);
1409   }
1410
1411   if (($form->{arap} eq 'ar') && $form->{customer}) {
1412     push @options, $form->{customer};
1413   }
1414
1415   if (($form->{arap} eq 'ap') && $form->{vendor}) {
1416     push @options, $form->{vendor};
1417   }
1418
1419   push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1420
1421   my $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1422
1423   $report->set_options('top_info_text'        => join("\n", @options),
1424                        'output_format'        => 'HTML',
1425                        'title'                => $form->{title},
1426                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
1427     );
1428
1429   my $previous_ctid = 0;
1430   my $row_idx       = 0;
1431   my @periods       = qw(0 30 60 90);
1432   my %subtotals     = map { $_ => 0 } @periods;
1433   my %totals        = map { $_ => 0 } @periods;
1434
1435   foreach $ref (@{ $form->{AG} }) {
1436     if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1437       $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1438     }
1439
1440     foreach my $key (@periods) {
1441       $subtotals{$key}  += $ref->{"c${key}"};
1442       $totals{$key}     += $ref->{"c${key}"};
1443       $ref->{"c${key}"}  = $ref->{"c${key}"} != 0 ? $form->format_amount(\%myconfig, $ref->{"c${key}"}, 2) : '';
1444     }
1445
1446     my $row = { };
1447
1448     foreach my $column (@columns) {
1449       $row->{$column} = {
1450         'data'   => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1451         'align'  => $column_alignment{$column},
1452         'valign' => $column eq 'statement' ? 'center' : '',
1453       };
1454     }
1455
1456     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1457
1458     if ($previous_ctid != $ref->{ctid}) {
1459       $row->{statement}->{raw_data} =
1460           $cgi->hidden('-name' => "customer_id_${row_idx}", '-value' => $ref->{ctid})
1461         . $cgi->checkbox('-name' => "statement_${row_idx}", '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1462       $row->{ct}->{data} = $ref->{name};
1463
1464       $row_idx++;
1465     }
1466
1467     $previous_ctid = $ref->{ctid};
1468
1469     $report->add_data($row);
1470   }
1471
1472   $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1473
1474   $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1475
1476   if ($form->{arap} eq 'ar') {
1477     $raw_top_info_text    = $form->parse_html_template('rp/aging_ar_top');
1478     $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1479                                                                                'PRINT_OPTIONS' => print_options(inline => 1), });
1480     $report->set_options('raw_top_info_text'    => $raw_top_info_text,
1481                          'raw_bottom_info_text' => $raw_bottom_info_text);
1482   }
1483
1484   $report->set_options_from_form();
1485
1486   $report->generate_with_headers();
1487
1488   $lxdebug->leave_sub();
1489 }
1490
1491 sub select_all {
1492   $lxdebug->enter_sub();
1493
1494   RP->aging(\%myconfig, \%$form);
1495
1496   map { $_->{checked} = "checked" } @{ $form->{AG} };
1497
1498   &aging;
1499
1500   $lxdebug->leave_sub();
1501 }
1502
1503 sub e_mail {
1504   $lxdebug->enter_sub();
1505
1506   $auth->assert('general_ledger');
1507
1508   # get name and email addresses
1509   for $i (1 .. $form->{rowcount}) {
1510     if ($form->{"statement_$i"}) {
1511       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1512       RP->get_customer(\%myconfig, \%$form);
1513       $selected = 1;
1514       last;
1515     }
1516   }
1517
1518   $form->error($locale->text('Nothing selected!')) unless $selected;
1519
1520   if ($myconfig{role} eq 'admin') {
1521     $bcc = qq|
1522           <th align=right nowrap=true>| . $locale->text('Bcc') . qq|</th>
1523           <td><input name=bcc size=30 value="$form->{bcc}"></td>
1524 |;
1525   }
1526
1527   $title = $locale->text('E-mail Statement to') . " $form->{$form->{ct}}";
1528
1529   $form->{media} = "email";
1530
1531   $form->header;
1532
1533   print qq|
1534 <body>
1535
1536 <form method=post action=$form->{script}>
1537
1538 <table width=100%>
1539   <tr class=listtop>
1540     <th>$title</th>
1541   </tr>
1542   <tr height="5"></tr>
1543   <tr>
1544     <td>
1545       <table width=100%>
1546         <tr>
1547           <th align=right nowrap>| . $locale->text('E-mail') . qq|</th>
1548           <td><input name=email size=30 value="$form->{email}"></td>
1549           <th align=right nowrap>| . $locale->text('Cc') . qq|</th>
1550           <td><input name=cc size=30 value="$form->{cc}"></td>
1551         </tr>
1552         <tr>
1553           <th align=right nowrap>| . $locale->text('Subject') . qq|</th>
1554           <td><input name=subject size=30 value="$form->{subject}"></td>
1555           $bcc
1556         </tr>
1557       </table>
1558     </td>
1559   </tr>
1560   <tr>
1561     <td>
1562       <table width=100%>
1563         <tr>
1564           <th align=left nowrap>| . $locale->text('Message') . qq|</th>
1565         </tr>
1566         <tr>
1567           <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
1568         </tr>
1569       </table>
1570     </td>
1571   </tr>
1572   <tr>
1573     <td>
1574 |;
1575
1576   &print_options;
1577
1578   map { delete $form->{$_} }
1579     qw(action email cc bcc subject message type sendmode format header);
1580
1581   # save all other variables
1582   foreach $key (keys %$form) {
1583     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1584     $form->{$key} =~ s/\"/&quot;/g;
1585     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1586   }
1587
1588   print qq|
1589     </td>
1590   </tr>
1591   <tr>
1592     <td><hr size=3 noshade></td>
1593   </tr>
1594 </table>
1595
1596 <input type=hidden name=nextsub value=send_email>
1597
1598 <br>
1599 <input name=action class=submit type=submit value="|
1600     . $locale->text('Continue') . qq|">
1601 </form>
1602
1603 </body>
1604 </html>
1605 |;
1606
1607   $lxdebug->leave_sub();
1608 }
1609
1610 sub send_email {
1611   $lxdebug->enter_sub();
1612
1613   $auth->assert('general_ledger');
1614
1615   $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1616     unless $form->{subject};
1617
1618   RP->aging(\%myconfig, \%$form);
1619
1620   $form->{"statement_1"} = 1;
1621
1622   $form->{media} = 'email';
1623   print_form();
1624
1625   $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1626
1627   $lxdebug->leave_sub();
1628 }
1629
1630 sub print {
1631   $lxdebug->enter_sub();
1632
1633   $auth->assert('general_ledger');
1634
1635   if ($form->{media} eq 'printer') {
1636     $form->error($locale->text('Select postscript or PDF!'))
1637       if ($form->{format} !~ /(postscript|pdf)/);
1638   }
1639
1640   for $i (1 .. $form->{rowcount}) {
1641     if ($form->{"statement_$i"}) {
1642       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1643       $selected = 1;
1644       last;
1645     }
1646   }
1647
1648   $form->error($locale->text('Nothing selected!')) unless $selected;
1649
1650   if ($form->{media} eq 'printer') {
1651     $form->{"$form->{ct}_id"} = "";
1652   } else {
1653     $form->{"statement_1"} = 1;
1654   }
1655
1656   RP->aging(\%myconfig, \%$form);
1657
1658   print_form();
1659
1660   $form->redirect($locale->text('Statements sent to printer!'))
1661     if ($form->{media} eq 'printer');
1662
1663   $lxdebug->leave_sub();
1664 }
1665
1666 sub print_form {
1667   $lxdebug->enter_sub();
1668
1669   $auth->assert('general_ledger');
1670
1671   my %replacements =
1672     (
1673      "ä" => "ae", "ö" => "oe", "ü" => "ue",
1674      "Ä" => "Ae", "Ö" => "Oe", "Ãœ" => "Ue",
1675      "ß" => "ss",
1676      " " => "_"
1677     );
1678
1679   foreach my $key (keys %replacements) {
1680     my $new_key = SL::Iconv::convert("ISO-8859-15", $dbcharset, $key);
1681     $replacements{$new_key} = $replacements{$key} if $new_key ne $key;
1682   }
1683
1684   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1685
1686   $form->{templates} = "$myconfig{templates}";
1687
1688   my $suffix = "html";
1689   my $attachment_suffix = "html";
1690   if ($form->{format} eq 'postscript') {
1691     $form->{postscript} = 1;
1692     $suffix = "tex";
1693     $attachment_suffix = "ps";
1694   } elsif ($form->{format} eq 'pdf') {
1695     $form->{pdf} = 1;
1696     $suffix = "tex";
1697     $attachment_suffix = "pdf";
1698   }
1699
1700   $form->{IN}  = "$form->{type}.$suffix";
1701   $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1702
1703   # Save $form->{email} because it will be overwritten.
1704   $form->{EMAIL_RECIPIENT} = $form->{email};
1705
1706   $i = 0;
1707   while (@{ $form->{AG} }) {
1708
1709     $ref = shift @{ $form->{AG} };
1710
1711     if ($ctid != $ref->{ctid}) {
1712
1713       $ctid = $ref->{ctid};
1714       $i++;
1715
1716       if ($form->{"statement_$i"}) {
1717
1718         @a =
1719           (name, street, zipcode, city, country, contact, email,
1720            "$form->{ct}phone", "$form->{ct}fax");
1721         map { $form->{$_} = $ref->{$_} } @a;
1722
1723         $form->{ $form->{ct} } = $form->{name};
1724         $form->{"$form->{ct}_id"} = $ref->{ctid};
1725
1726         map { $form->{$_} = () } qw(invnumber invdate duedate);
1727         $form->{total} = 0;
1728         foreach $item (qw(c0 c30 c60 c90)) {
1729           $form->{$item} = ();
1730           $form->{"${item}total"} = 0;
1731         }
1732
1733         &statement_details($ref);
1734
1735         while ($ref) {
1736
1737           if (scalar(@{ $form->{AG} }) > 0) {
1738
1739             # one or more left to go
1740             if ($ctid == $form->{AG}->[0]->{ctid}) {
1741               $ref = shift @{ $form->{AG} };
1742               &statement_details($ref);
1743
1744               # any more?
1745               $ref = scalar(@{ $form->{AG} });
1746             } else {
1747               $ref = 0;
1748             }
1749           } else {
1750
1751             # set initial ref to 0
1752             $ref = 0;
1753           }
1754
1755         }
1756
1757         map {
1758           $form->{"${_}total"} =
1759             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
1760         } (c0, c30, c60, c90, "");
1761
1762         $form->{attachment_filename} = $locale->text("Statement") . "_$form->{todate}.$attachment_suffix";
1763         map({ $form->{attachment_filename} =~ s/$_/$replacements{$_}/g; } keys(%replacements));
1764
1765         $form->parse_template(\%myconfig, $userspath);
1766
1767       }
1768     }
1769   }
1770   # saving the history
1771   if(!exists $form->{addition} && $form->{id} ne "") {
1772     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1773         $form->{addition} = "PRINTED";
1774         $form->{what_done} = $form->{type};
1775         $form->save_history($form->dbconnect(\%myconfig));
1776   }
1777   # /saving the history 
1778   $lxdebug->leave_sub();
1779 }
1780
1781 sub statement_details {
1782   $lxdebug->enter_sub();
1783
1784   $auth->assert('general_ledger');
1785
1786   my ($ref) = @_;
1787
1788   push @{ $form->{invnumber} }, $ref->{invnumber};
1789   push @{ $form->{invdate} },   $ref->{transdate};
1790   push @{ $form->{duedate} },   $ref->{duedate};
1791
1792   foreach $item (qw(c0 c30 c60 c90)) {
1793     if ($ref->{exchangerate} * 1) {
1794       $ref->{$item} =
1795         $form->round_amount($ref->{$item} / $ref->{exchangerate}, 2);
1796     }
1797     $form->{"${item}total"} += $ref->{$item};
1798     $form->{total}          += $ref->{$item};
1799     push @{ $form->{$item} },
1800       $form->format_amount(\%myconfig, $ref->{$item}, 2);
1801   }
1802
1803   $lxdebug->leave_sub();
1804 }
1805
1806 sub generate_tax_report {
1807   $lxdebug->enter_sub();
1808
1809   $auth->assert('report');
1810
1811   RP->tax_report(\%myconfig, \%$form);
1812
1813   $descvar     = "$form->{accno}_description";
1814   $description = $form->escape($form->{$descvar});
1815   $ratevar     = "$form->{accno}_rate";
1816
1817   $department = $form->escape($form->{department});
1818
1819   # construct href
1820   $href =
1821     "$form->{script}?&action=generate_tax_report&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&accno=$form->{accno}&$descvar=$description&department=$department&$ratevar=$taxrate&report=$form->{report}";
1822
1823   # construct callback
1824   $description = $form->escape($form->{$descvar},   1);
1825   $department  = $form->escape($form->{department}, 1);
1826   $callback    =
1827     "$form->{script}?&action=generate_tax_report&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&accno=$form->{accno}&$descvar=$description&department=$department&$ratevar=$taxrate&report=$form->{report}";
1828
1829   $title = $form->escape($form->{title});
1830   $href .= "&title=$title";
1831   $title = $form->escape($form->{title}, 1);
1832   $callback .= "&title=$title";
1833
1834   $form->{title} = qq|$form->{title} $form->{"$form->{accno}_description"} |;
1835
1836   @columns =
1837     $form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1838
1839   foreach $item (@columns) {
1840     if ($form->{"l_$item"} eq "Y") {
1841       push @column_index, $item;
1842
1843       # add column to href and callback
1844       $callback .= "&l_$item=Y";
1845       $href     .= "&l_$item=Y";
1846     }
1847   }
1848
1849   if ($form->{l_subtotal} eq 'Y') {
1850     $callback .= "&l_subtotal=Y";
1851     $href     .= "&l_subtotal=Y";
1852   }
1853
1854   if ($form->{department}) {
1855     ($department) = split /--/, $form->{department};
1856     $option = $locale->text('Department') . " : $department";
1857   }
1858
1859   # if there are any dates
1860   if ($form->{fromdate} || $form->{todate}) {
1861     if ($form->{fromdate}) {
1862       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
1863     }
1864     if ($form->{todate}) {
1865       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
1866     }
1867
1868     $form->{period} = "$fromdate - $todate";
1869   } else {
1870     $form->{period} =
1871       $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1872   }
1873
1874   if ($form->{db} eq 'ar') {
1875     $name    = $locale->text('Customer');
1876     $invoice = 'is.pl';
1877     $arap    = 'ar.pl';
1878   }
1879   if ($form->{db} eq 'ap') {
1880     $name    = $locale->text('Vendor');
1881     $invoice = 'ir.pl';
1882     $arap    = 'ap.pl';
1883   }
1884
1885   $option .= "<br>" if $option;
1886   $option .= "$form->{period}";
1887
1888   $column_header{id} =
1889       qq|<th><a class=listheading href=$href&sort=id>|
1890     . $locale->text('ID')
1891     . qq|</th>|;
1892   $column_header{invnumber} =
1893       qq|<th><a class=listheading href=$href&sort=invnumber>|
1894     . $locale->text('Invoice')
1895     . qq|</th>|;
1896   $column_header{transdate} =
1897       qq|<th><a class=listheading href=$href&sort=transdate>|
1898     . $locale->text('Date')
1899     . qq|</th>|;
1900   $column_header{netamount} =
1901     qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
1902   $column_header{tax} =
1903     qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
1904   $column_header{amount} =
1905     qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
1906
1907   $column_header{name} =
1908     qq|<th><a class=listheading href=$href&sort=name>$name</th>|;
1909
1910   $form->header;
1911
1912   print qq|
1913 <body>
1914
1915 <table width=100%>
1916   <tr>
1917     <th class=listtop colspan=$colspan>$form->{title}</th>
1918   </tr>
1919   <tr height="5"></tr>
1920   <tr>
1921     <td>$option</td>
1922   </tr>
1923   <tr>
1924     <td>
1925       <table width=100%>
1926         <tr class=listheading>
1927 |;
1928
1929   map { print "$column_header{$_}\n" } @column_index;
1930
1931   print qq|
1932         </tr>
1933 |;
1934
1935   # add sort and escape callback
1936   $callback = $form->escape($callback . "&sort=$form->{sort}");
1937
1938   if (@{ $form->{TR} }) {
1939     $sameitem = $form->{TR}->[0]->{ $form->{sort} };
1940   }
1941
1942   foreach $ref (@{ $form->{TR} }) {
1943
1944     $module = ($ref->{invoice}) ? $invoice : $arap;
1945
1946     if ($form->{l_subtotal} eq 'Y') {
1947       if ($sameitem ne $ref->{ $form->{sort} }) {
1948         &tax_subtotal;
1949         $sameitem = $ref->{ $form->{sort} };
1950       }
1951     }
1952
1953     $totalnetamount += $ref->{netamount};
1954     $totaltax       += $ref->{tax};
1955     $ref->{amount} = $ref->{netamount} + $ref->{tax};
1956
1957     $subtotalnetamount += $ref->{netamount};
1958     $subtotaltax       += $ref->{tax};
1959
1960     map {
1961       $ref->{$_} = $form->format_amount(\%myconfig, $ref->{$_}, 2, "&nbsp;");
1962     } qw(netamount tax amount);
1963
1964     $column_data{id}        = qq|<td>$ref->{id}</td>|;
1965     $column_data{invnumber} =
1966       qq|<td><a href=$module?action=edit&id=$ref->{id}&callback=$callback>$ref->{invnumber}</a></td>|;
1967     $column_data{transdate} = qq|<td>$ref->{transdate}</td>|;
1968     $column_data{name}      = qq|<td>$ref->{name}&nbsp;</td>|;
1969
1970     map { $column_data{$_} = qq|<td align=right>$ref->{$_}</td>| }
1971       qw(netamount tax amount);
1972
1973     $i++;
1974     $i %= 2;
1975     print qq|
1976         <tr class=listrow$i>
1977 |;
1978
1979     map { print "$column_data{$_}\n" } @column_index;
1980
1981     print qq|
1982         </tr>
1983 |;
1984
1985   }
1986
1987   if ($form->{l_subtotal} eq 'Y') {
1988     &tax_subtotal;
1989   }
1990
1991   map { $column_data{$_} = qq|<th>&nbsp;</th>| } @column_index;
1992
1993   print qq|
1994         </tr>
1995         <tr class=listtotal>
1996 |;
1997
1998   $total =
1999     $form->format_amount(\%myconfig, $totalnetamount + $totaltax, 2, "&nbsp;");
2000   $totalnetamount =
2001     $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;");
2002   $totaltax = $form->format_amount(\%myconfig, $totaltax, 2, "&nbsp;");
2003
2004   $column_data{netamount} =
2005     qq|<th class=listtotal align=right>$totalnetamount</th>|;
2006   $column_data{tax}    = qq|<th class=listtotal align=right>$totaltax</th>|;
2007   $column_data{amount} = qq|<th class=listtotal align=right>$total</th>|;
2008
2009   map { print "$column_data{$_}\n" } @column_index;
2010
2011   print qq|
2012         </tr>
2013       </table>
2014     </td>
2015   </tr>
2016   <tr>
2017     <td><hr size=3 noshade></td>
2018   </tr>
2019 </table>
2020
2021 </body>
2022 </html>
2023 |;
2024
2025   $lxdebug->leave_sub();
2026 }
2027
2028 sub tax_subtotal {
2029   $lxdebug->enter_sub();
2030
2031   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
2032
2033   $subtotalnetamount =
2034     $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;");
2035   $subtotaltax = $form->format_amount(\%myconfig, $subtotaltax, 2, "&nbsp;");
2036   $subtotal =
2037     $form->format_amount(\%myconfig, $subtotalnetamount + $subtotaltax,
2038                          2, "&nbsp;");
2039
2040   $column_data{netamount} =
2041     "<th class=listsubtotal align=right>$subtotalnetamount</th>";
2042   $column_data{tax} = "<th class=listsubtotal align=right>$subtotaltax</th>";
2043   $column_data{amount} = "<th class=listsubtotal align=right>$subtotal</th>";
2044
2045   $subtotalnetamount = 0;
2046   $subtotaltax       = 0;
2047
2048   print qq|
2049         <tr class=listsubtotal>
2050 |;
2051   map { print "\n$column_data{$_}" } @column_index;
2052
2053   print qq|
2054         </tr>
2055 |;
2056
2057   $lxdebug->leave_sub();
2058 }
2059
2060 sub list_payments {
2061   $lxdebug->enter_sub();
2062
2063   $auth->assert('cash');
2064
2065   if ($form->{account}) {
2066     ($form->{paymentaccounts}) = split /--/, $form->{account};
2067   }
2068   if ($form->{department}) {
2069     ($department, $form->{department_id}) = split /--/, $form->{department};
2070     $option = $locale->text('Department') . " : $department";
2071   }
2072
2073   RP->payments(\%myconfig, \%$form);
2074
2075   my @hidden_variables = qw(account title department reference source memo fromdate todate
2076                             fx_transaction db prepayment paymentaccounts sort);
2077
2078   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
2079   $form->{callback} = $href;
2080
2081   my @columns     = qw(transdate invnumber name paid source memo);
2082   my %column_defs = (
2083     'name'      => { 'text' => $locale->text('Description'), },
2084     'invnumber' => { 'text' => $locale->text('Reference'), },
2085     'transdate' => { 'text' => $locale->text('Date'), },
2086     'paid'      => { 'text' => $locale->text('Amount'), },
2087     'source'    => { 'text' => $locale->text('Source'), },
2088     'memo'      => { 'text' => $locale->text('Memo'), },
2089   );
2090   my %column_alignment = ('paid' => 'right');
2091
2092   map { $column_defs{$_}->{link} = $href . "&sort=$_" } grep { $_ ne 'paid' } @columns;
2093
2094   my @options;
2095   if ($form->{fromdate}) {
2096     push @options, $locale->text('From') . "&nbsp;" . $locale->date(\%myconfig, $form->{fromdate}, 1);
2097   }
2098   if ($form->{todate}) {
2099     push @options, $locale->text('bis') . "&nbsp;" . $locale->date(\%myconfig, $form->{todate}, 1);
2100   }
2101
2102   my $report = SL::ReportGenerator->new(\%myconfig, $form);
2103
2104   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
2105
2106   $report->set_options('top_info_text'         => join("\n", @options),
2107                        'output_format'         => 'HTML',
2108                        'title'                 => $form->{title},
2109                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
2110                        'std_column_visibility' => 1,
2111     );
2112   $report->set_options_from_form();
2113
2114   $report->set_columns(%column_defs);
2115   $report->set_column_order(@columns);
2116
2117   $report->set_export_options('list_payments', @hidden_variables);
2118
2119   $report->set_sort_indicator($form->{sort}, 1);
2120
2121   my $total_paid    = 0;
2122
2123   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
2124     next unless @{ $form->{ $ref->{id} } };
2125
2126     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
2127
2128     my $subtotal_paid = 0;
2129
2130     foreach my $payment (@{ $form->{ $ref->{id} } }) {
2131       my $module = $payment->{module};
2132       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
2133       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
2134
2135       $subtotal_paid += $payment->{paid};
2136       $total_paid    += $payment->{paid};
2137
2138       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
2139
2140       my $row = { };
2141
2142       foreach my $column (@columns) {
2143         $row->{$column} = {
2144           'data'  => $payment->{$column},
2145           'align' => $column_alignment{$column},
2146         };
2147       }
2148
2149       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
2150
2151       $report->add_data($row);
2152     }
2153
2154     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
2155     $row->{paid} = {
2156       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
2157       'align' => 'right',
2158       'class' => 'listsubtotal',
2159     };
2160
2161     $report->add_data($row);
2162   }
2163
2164   $report->add_separator();
2165
2166   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
2167   $row->{paid} = {
2168     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
2169     'align' => 'right',
2170     'class' => 'listtotal',
2171   };
2172
2173   $report->add_data($row);
2174
2175   $report->generate_with_headers();
2176
2177   $lxdebug->leave_sub();
2178 }
2179
2180 sub print_options {
2181   $lxdebug->enter_sub();
2182
2183   my ($dont_print) = @_;
2184
2185   $form->{sendmode} = "attachment";
2186
2187   $form->{"format"} =
2188     $form->{"format"} ? $form->{"format"} :
2189     $myconfig{"template_format"} ? $myconfig{"template_format"} :
2190     "pdf";
2191
2192   $form->{"copies"} =
2193     $form->{"copies"} ? $form->{"copies"} :
2194     $myconfig{"copies"} ? $myconfig{"copies"} :
2195     2;
2196
2197   $form->{PD}{ $form->{type} }     = "selected";
2198   $form->{DF}{ $form->{format} }   = "selected";
2199   $form->{OP}{ $form->{media} }    = "selected";
2200   $form->{SM}{ $form->{sendmode} } = "selected";
2201
2202   $type = qq|
2203             <option value=statement $form->{PD}{statement}>|
2204       . $locale->text('Statement');
2205
2206   if ($form->{media} eq 'email') {
2207     $media = qq|
2208             <option value=attachment $form->{SM}{attachment}>|
2209       . $locale->text('Attachment') . qq|
2210             <option value=inline $form->{SM}{inline}>| . $locale->text('In-line');
2211   } else {
2212     $media = qq|
2213             <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
2214     if ($myconfig{printer} && $latex_templates) {
2215       $media .= qq|
2216             <option value=printer $form->{OP}{printer}>|
2217         . $locale->text('Printer');
2218     }
2219   }
2220
2221   if ($latex_templates) {
2222     $format .= qq|
2223             <option value=html $form->{DF}{html}>| . $locale->text('HTML')
2224       . qq| <option value=pdf $form->{DF}{pdf}>| . $locale->text('PDF')
2225       . qq| <option value=postscript $form->{DF}{postscript}>| . $locale->text('Postscript');
2226   }
2227
2228   my $output = qq|
2229 <table>
2230   <tr>
2231     <td><select name=type>$type</select></td>
2232     <td><select name=format>$format</select></td>
2233     <td><select name=media>$media</select></td>
2234 |;
2235
2236   if ($myconfig{printer} && $latex_templates && $form->{media} ne 'email') {
2237     $output .= qq|
2238       <td>| . $locale->text('Copies') . qq|
2239       <input name=copies size=2 value=$form->{copies}></td>
2240 |;
2241   }
2242
2243   $output .= qq|
2244   </tr>
2245 </table>
2246 |;
2247
2248   print $output unless $dont_print;
2249
2250   $lxdebug->leave_sub();
2251
2252   return $output;
2253 }
2254
2255 sub generate_bwa {
2256   $lxdebug->enter_sub();
2257
2258   $auth->assert('report');
2259
2260   $form->{padding} = "&nbsp;&nbsp;";
2261   $form->{bold}    = "<b>";
2262   $form->{endbold} = "</b>";
2263   $form->{br}      = "<br>";
2264
2265   if ($form->{reporttype} eq "custom") {
2266
2267     #forgotten the year --> thisyear
2268     if ($form->{year} !~ m/^\d\d\d\d$/) {
2269       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
2270         /(\d\d\d\d)/;
2271       $form->{year} = $1;
2272     }
2273
2274     #yearly report
2275     if ($form->{duetyp} eq "13") {
2276       $form->{fromdate}        = "1.1.$form->{year}";
2277       $form->{todate}          = "31.12.$form->{year}";
2278       $form->{comparefromdate} = "1.01.$form->{year}";
2279       $form->{comparetodate}   = "31.12.$form->{year}";
2280     }
2281
2282     #Quater reports
2283     if ($form->{duetyp} eq "A") {
2284       $form->{fromdate}        = "1.1.$form->{year}";
2285       $form->{todate}          = "31.3.$form->{year}";
2286       $form->{comparefromdate} = "1.01.$form->{year}";
2287       $form->{comparetodate}   = "31.03.$form->{year}";
2288     }
2289     if ($form->{duetyp} eq "B") {
2290       $form->{fromdate}        = "1.4.$form->{year}";
2291       $form->{todate}          = "30.6.$form->{year}";
2292       $form->{comparefromdate} = "1.01.$form->{year}";
2293       $form->{comparetodate}   = "30.06.$form->{year}";
2294     }
2295     if ($form->{duetyp} eq "C") {
2296       $form->{fromdate}        = "1.7.$form->{year}";
2297       $form->{todate}          = "30.9.$form->{year}";
2298       $form->{comparefromdate} = "1.01.$form->{year}";
2299       $form->{comparetodate}   = "30.09.$form->{year}";
2300     }
2301     if ($form->{duetyp} eq "D") {
2302       $form->{fromdate}        = "1.10.$form->{year}";
2303       $form->{todate}          = "31.12.$form->{year}";
2304       $form->{comparefromdate} = "1.01.$form->{year}";
2305       $form->{comparetodate}   = "31.12.$form->{year}";
2306     }
2307
2308     #Monthly reports
2309   SWITCH: {
2310       $form->{duetyp} eq "1" && do {
2311         $form->{fromdate}        = "1.1.$form->{year}";
2312         $form->{todate}          = "31.1.$form->{year}";
2313         $form->{comparefromdate} = "1.01.$form->{year}";
2314         $form->{comparetodate}   = "31.01.$form->{year}";
2315         last SWITCH;
2316       };
2317       $form->{duetyp} eq "2" && do {
2318         $form->{fromdate} = "1.2.$form->{year}";
2319
2320         #this works from 1901 to 2099, 1900 and 2100 fail.
2321         $leap = ($form->{year} % 4 == 0) ? "29" : "28";
2322         $form->{todate}          = "$leap.2.$form->{year}";
2323         $form->{comparefromdate} = "1.01.$form->{year}";
2324         $form->{comparetodate}   = "$leap.02.$form->{year}";
2325         last SWITCH;
2326       };
2327       $form->{duetyp} eq "3" && do {
2328         $form->{fromdate}        = "1.3.$form->{year}";
2329         $form->{todate}          = "31.3.$form->{year}";
2330         $form->{comparefromdate} = "1.01.$form->{year}";
2331         $form->{comparetodate}   = "31.03.$form->{year}";
2332         last SWITCH;
2333       };
2334       $form->{duetyp} eq "4" && do {
2335         $form->{fromdate}        = "1.4.$form->{year}";
2336         $form->{todate}          = "30.4.$form->{year}";
2337         $form->{comparefromdate} = "1.01.$form->{year}";
2338         $form->{comparetodate}   = "30.04.$form->{year}";
2339         last SWITCH;
2340       };
2341       $form->{duetyp} eq "5" && do {
2342         $form->{fromdate}        = "1.5.$form->{year}";
2343         $form->{todate}          = "31.5.$form->{year}";
2344         $form->{comparefromdate} = "1.01.$form->{year}";
2345         $form->{comparetodate}   = "31.05.$form->{year}";
2346         last SWITCH;
2347       };
2348       $form->{duetyp} eq "6" && do {
2349         $form->{fromdate}        = "1.6.$form->{year}";
2350         $form->{todate}          = "30.6.$form->{year}";
2351         $form->{comparefromdate} = "1.01.$form->{year}";
2352         $form->{comparetodate}   = "30.06.$form->{year}";
2353         last SWITCH;
2354       };
2355       $form->{duetyp} eq "7" && do {
2356         $form->{fromdate}        = "1.7.$form->{year}";
2357         $form->{todate}          = "31.7.$form->{year}";
2358         $form->{comparefromdate} = "1.01.$form->{year}";
2359         $form->{comparetodate}   = "31.07.$form->{year}";
2360         last SWITCH;
2361       };
2362       $form->{duetyp} eq "8" && do {
2363         $form->{fromdate}        = "1.8.$form->{year}";
2364         $form->{todate}          = "31.8.$form->{year}";
2365         $form->{comparefromdate} = "1.01.$form->{year}";
2366         $form->{comparetodate}   = "31.08.$form->{year}";
2367         last SWITCH;
2368       };
2369       $form->{duetyp} eq "9" && do {
2370         $form->{fromdate}        = "1.9.$form->{year}";
2371         $form->{todate}          = "30.9.$form->{year}";
2372         $form->{comparefromdate} = "1.01.$form->{year}";
2373         $form->{comparetodate}   = "30.09.$form->{year}";
2374         last SWITCH;
2375       };
2376       $form->{duetyp} eq "10" && do {
2377         $form->{fromdate}        = "1.10.$form->{year}";
2378         $form->{todate}          = "31.10.$form->{year}";
2379         $form->{comparefromdate} = "1.01.$form->{year}";
2380         $form->{comparetodate}   = "31.10.$form->{year}";
2381         last SWITCH;
2382       };
2383       $form->{duetyp} eq "11" && do {
2384         $form->{fromdate}        = "1.11.$form->{year}";
2385         $form->{todate}          = "30.11.$form->{year}";
2386         $form->{comparefromdate} = "1.01.$form->{year}";
2387         $form->{comparetodate}   = "30.11.$form->{year}";
2388         last SWITCH;
2389       };
2390       $form->{duetyp} eq "12" && do {
2391         $form->{fromdate}        = "1.12.$form->{year}";
2392         $form->{todate}          = "31.12.$form->{year}";
2393         $form->{comparefromdate} = "1.01.$form->{year}";
2394         $form->{comparetodate}   = "31.12.$form->{year}";
2395         last SWITCH;
2396       };
2397     }
2398   } else {
2399     ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate});
2400     $form->{fromdate} = "${dd}.${mm}.${yy}";
2401     ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{todate});
2402     $form->{todate}          = "${dd}.${mm}.${yy}";
2403     $form->{comparefromdate} = "01.01.$yy";
2404     $form->{comparetodate}   = $form->{todate};
2405   }
2406
2407   RP->bwa(\%myconfig, \%$form);
2408
2409   ($form->{department}) = split /--/, $form->{department};
2410
2411   $form->{period} =
2412     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
2413   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
2414
2415   # if there are any dates construct a where
2416   if ($form->{fromdate} || $form->{todate}) {
2417
2418     unless ($form->{todate}) {
2419       $form->{todate} = $form->current_date(\%myconfig);
2420     }
2421
2422     my %germandate = ("dateformat" => "dd.mm.yyyy");
2423
2424     $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
2425     $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
2426
2427     $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
2428     $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
2429
2430     $form->{this_period} = "$shortfromdate\n$shorttodate";
2431     $form->{period}      =
2432         $locale->text('for Period')
2433       . qq|\n$longfromdate |
2434       . $locale->text('bis')
2435       . qq| $longtodate|;
2436   }
2437
2438   # setup variables for the form
2439   @a = qw(company address businessnumber);
2440   map { $form->{$_} = $myconfig{$_} } @a;
2441   $form->{templates} = $myconfig{templates};
2442
2443   $form->{IN} = "bwa.html";
2444
2445   $form->parse_template;
2446
2447   $lxdebug->leave_sub();
2448 }
2449