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