8004e920ac93cb7c3fa3b8b4c0e77e02e269ee54
[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   $form->{padding} = "&nbsp;&nbsp;";
1198   $form->{bold}    = "<b>";
1199   $form->{endbold} = "</b>";
1200   $form->{br}      = "<br>";
1201
1202   RP->balance_sheet(\%myconfig, \%$form);
1203
1204   $form->{asofdate} = $form->current_date(\%myconfig) unless $form->{asofdate};
1205   $form->{period} =
1206     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1207
1208   ($form->{department}) = split /--/, $form->{department};
1209
1210   # define Current Earnings account
1211   $padding = ($form->{l_heading}) ? $form->{padding} : "";
1212   push(@{ $form->{equity_account} },
1213        $padding . $locale->text('Current Earnings'));
1214
1215   $form->{this_period} = $locale->date(\%myconfig, $form->{asofdate}, 0);
1216   $form->{last_period} =
1217     $locale->date(\%myconfig, $form->{compareasofdate}, 0);
1218
1219   $form->{IN} = "balance_sheet.html";
1220
1221   # setup company variables for the form
1222   map { $form->{$_} = $myconfig{$_}; } (qw(company address businessnumber nativecurr));
1223
1224   $form->{templates} = $myconfig{templates};
1225
1226   $form->parse_template;
1227
1228   $lxdebug->leave_sub();
1229 }
1230
1231 sub generate_projects {
1232   $lxdebug->enter_sub();
1233
1234   $auth->assert('report');
1235
1236   &get_project(generate_projects);
1237   $form->{projectnumber} = $form->{projectnumber_1};
1238
1239   $form->{nextsub} = "generate_projects";
1240   $form->{title}   = $locale->text('Project Transactions');
1241   RP->trial_balance(\%myconfig, \%$form);
1242
1243   list_accounts('generate_projects');
1244
1245   $lxdebug->leave_sub();
1246 }
1247
1248 # Antonio Gallardo
1249 #
1250 # D.S. Feb 16, 2001
1251 # included links to display transactions for period entered
1252 # added headers and subtotals
1253 #
1254 sub generate_trial_balance {
1255   $lxdebug->enter_sub();
1256
1257   $auth->assert('report');
1258
1259   if ($form->{reporttype} eq "custom") {
1260
1261     #forgotten the year --> thisyear
1262     if ($form->{year} !~ m/^\d\d\d\d$/) {
1263       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1264         /(\d\d\d\d)/;
1265       $form->{year} = $1;
1266     }
1267
1268     #yearly report
1269     if ($form->{duetyp} eq "13") {
1270       $form->{fromdate} = "1.1.$form->{year}";
1271       $form->{todate}   = "31.12.$form->{year}";
1272     }
1273
1274     #Quater reports
1275     if ($form->{duetyp} eq "A") {
1276       $form->{fromdate} = "1.1.$form->{year}";
1277       $form->{todate}   = "31.3.$form->{year}";
1278     }
1279     if ($form->{duetyp} eq "B") {
1280       $form->{fromdate} = "1.4.$form->{year}";
1281       $form->{todate}   = "30.6.$form->{year}";
1282     }
1283     if ($form->{duetyp} eq "C") {
1284       $form->{fromdate} = "1.7.$form->{year}";
1285       $form->{todate}   = "30.9.$form->{year}";
1286     }
1287     if ($form->{duetyp} eq "D") {
1288       $form->{fromdate} = "1.10.$form->{year}";
1289       $form->{todate}   = "31.12.$form->{year}";
1290     }
1291
1292     #Monthly reports
1293   SWITCH: {
1294       $form->{duetyp} eq "1" && do {
1295         $form->{fromdate} = "1.1.$form->{year}";
1296         $form->{todate}   = "31.1.$form->{year}";
1297         last SWITCH;
1298       };
1299       $form->{duetyp} eq "2" && do {
1300         $form->{fromdate} = "1.2.$form->{year}";
1301
1302         #this works from 1901 to 2099, 1900 and 2100 fail.
1303         $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1304         $form->{todate} = "$leap.2.$form->{year}";
1305         last SWITCH;
1306       };
1307       $form->{duetyp} eq "3" && do {
1308         $form->{fromdate} = "1.3.$form->{year}";
1309         $form->{todate}   = "31.3.$form->{year}";
1310         last SWITCH;
1311       };
1312       $form->{duetyp} eq "4" && do {
1313         $form->{fromdate} = "1.4.$form->{year}";
1314         $form->{todate}   = "30.4.$form->{year}";
1315         last SWITCH;
1316       };
1317       $form->{duetyp} eq "5" && do {
1318         $form->{fromdate} = "1.5.$form->{year}";
1319         $form->{todate}   = "31.5.$form->{year}";
1320         last SWITCH;
1321       };
1322       $form->{duetyp} eq "6" && do {
1323         $form->{fromdate} = "1.6.$form->{year}";
1324         $form->{todate}   = "30.6.$form->{year}";
1325         last SWITCH;
1326       };
1327       $form->{duetyp} eq "7" && do {
1328         $form->{fromdate} = "1.7.$form->{year}";
1329         $form->{todate}   = "31.7.$form->{year}";
1330         last SWITCH;
1331       };
1332       $form->{duetyp} eq "8" && do {
1333         $form->{fromdate} = "1.8.$form->{year}";
1334         $form->{todate}   = "31.8.$form->{year}";
1335         last SWITCH;
1336       };
1337       $form->{duetyp} eq "9" && do {
1338         $form->{fromdate} = "1.9.$form->{year}";
1339         $form->{todate}   = "30.9.$form->{year}";
1340         last SWITCH;
1341       };
1342       $form->{duetyp} eq "10" && do {
1343         $form->{fromdate} = "1.10.$form->{year}";
1344         $form->{todate}   = "31.10.$form->{year}";
1345         last SWITCH;
1346       };
1347       $form->{duetyp} eq "11" && do {
1348         $form->{fromdate} = "1.11.$form->{year}";
1349         $form->{todate}   = "30.11.$form->{year}";
1350         last SWITCH;
1351       };
1352       $form->{duetyp} eq "12" && do {
1353         $form->{fromdate} = "1.12.$form->{year}";
1354         $form->{todate}   = "31.12.$form->{year}";
1355         last SWITCH;
1356       };
1357     }
1358   }
1359
1360
1361   # get for each account initial balance, debits and credits
1362   RP->trial_balance(\%myconfig, \%$form);
1363
1364
1365   $form->{rowcount} = scalar @{ $form->{TB} };
1366
1367   my @columns = (
1368     "accno",               "description",
1369     "last_transaction",    "soll_eb",
1370     "haben_eb",
1371     "soll",                 "haben",
1372     "soll_kumuliert", "haben_kumuliert",
1373     "soll_saldo", "haben_saldo"
1374   );
1375
1376
1377   my $attachment_basename;
1378
1379   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1380
1381   my @hidden_variables = ();
1382   push @hidden_variables, qw(fromdate todate year cash );
1383
1384   my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
1385
1386   my %column_defs = (
1387     'accno'                   => { 'text' => $locale->text('Account Number'), },
1388     'description'             => { 'text' => $locale->text('Description'), },
1389     'last_transaction'        => { 'text' => $locale->text('Last Transaction'), },
1390     'soll_eb'                 => { 'text' => $locale->text('Debit Starting Balance'), },
1391     'haben_eb'                => { 'text' => $locale->text('Credit Starting Balance'), },
1392     'soll'                    => { 'text' => $locale->text('Debit'), },
1393     'haben'                   => { 'text' => $locale->text('Credit'), },
1394     'soll_kumuliert'          => { 'text' => $locale->text('Sum Debit'), },
1395     'haben_kumuliert'         => { 'text' => $locale->text('Sum Credit'), },
1396     'soll_saldo'              => { 'text' => $locale->text('Saldo Debit'), },
1397     'haben_saldo'                => { 'text' => $locale->text('Saldo Credit'), }
1398   );
1399
1400
1401
1402   my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
1403
1404   map { $column_defs{$_}->{visible} =  1 } @columns;
1405
1406   $report->set_columns(%column_defs);
1407   $report->set_column_order(@columns);
1408
1409   $report->set_export_options('trial_balance', @hidden_variables);
1410
1411   $report->set_sort_indicator($form->{sort}, 1);
1412
1413   my @options;
1414
1415
1416   $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . "&nbsp; - &nbsp;" . $locale->date(\%myconfig, $form->{todate}, 0);
1417   $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
1418
1419   $report->set_options('output_format'        => 'HTML',
1420                        'title'                => $form->{title},
1421                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
1422                        'html_template'        => 'rp/html_report_susa',
1423                        'pdf_template'         => 'rp/html_report_susa',
1424     );
1425   $report->set_options_from_form();
1426
1427   # add sort and escape callback, this one we use for the add sub
1428   $form->{callback} = $href .= "&sort=$form->{sort}";
1429
1430   # escape callback for href
1431   $callback = $form->escape($href);
1432
1433   my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
1434
1435   my %totals    = map { $_ => 0 } @subtotal_columns;
1436
1437   my $edit_url = build_std_url('action=edit', 'type', 'vc');
1438
1439   foreach $accno (@{ $form->{TB} }) {
1440
1441     $accno->{soll} = $accno->{debit};
1442     $accno->{haben} = $accno->{credit};
1443     map { $totals{$_}    += $accno->{$_} } @subtotal_columns;
1444
1445     map { $accno->{$_} = $form->format_amount(\%myconfig, $accno->{$_}, 2) } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
1446
1447     map { $accno->{$_} = ($accno->{$_} == 0) ? '' : $accno->{$_} } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
1448
1449     my $row = { };
1450
1451     foreach my $column (@columns) {
1452       $row->{$column} = {
1453         'data'  => $accno->{$column},
1454         'align' => $column_alignment{$column},
1455       };
1456     }
1457
1458
1459     $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
1460
1461     my $row_set = [ $row ];
1462
1463
1464     $report->add_data($row_set);
1465
1466     $idx++;
1467   }
1468
1469   $report->add_separator();
1470
1471   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1472
1473   $report->generate_with_headers();
1474
1475   $lxdebug->leave_sub();
1476
1477 }
1478
1479 sub create_subtotal_row {
1480   $lxdebug->enter_sub();
1481
1482   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
1483
1484   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
1485
1486   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
1487
1488   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
1489
1490   map { $totals->{$_} = 0 } @{ $subtotal_columns };
1491
1492   $lxdebug->leave_sub();
1493
1494   return $row;
1495 }
1496
1497 sub create_list_accounts_subtotal_row {
1498   $lxdebug->enter_sub();
1499
1500   my ($subtotals, $columns, $fields, $class) = @_;
1501
1502   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
1503
1504   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
1505
1506   $lxdebug->leave_sub();
1507
1508   return $row;
1509 }
1510
1511 sub list_accounts {
1512   $lxdebug->enter_sub();
1513
1514   my ($action) = @_;
1515
1516   my @options;
1517   if ($form->{department}) {
1518     my ($department) = split /--/, $form->{department};
1519     push @options, $locale->text('Department') . " : $department";
1520   }
1521   if ($form->{projectnumber}) {
1522     push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
1523   }
1524
1525   # if there are any dates
1526   if ($form->{fromdate} || $form->{todate}) {
1527     my ($fromdate, $todate);
1528
1529     if ($form->{fromdate}) {
1530       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
1531     }
1532     if ($form->{todate}) {
1533       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
1534     }
1535
1536     push @options, "$fromdate - $todate";
1537
1538   } else {
1539     push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1540   }
1541
1542   my @columns     = qw(accno description begbalance debit credit endbalance);
1543   my %column_defs = (
1544     'accno'       => { 'text' => $locale->text('Account'), },
1545     'description' => { 'text' => $locale->text('Description'), },
1546     'debit'       => { 'text' => $locale->text('Debit'), },
1547     'credit'      => { 'text' => $locale->text('Credit'), },
1548     'begbalance'  => { 'text' => $locale->text('Balance'), },
1549     'endbalance'  => { 'text' => $locale->text('Balance'), },
1550   );
1551   my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
1552
1553   my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
1554
1555   $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
1556
1557   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1558
1559   $report->set_options('top_info_text'         => join("\n", @options),
1560                        'output_format'         => 'HTML',
1561                        'title'                 => $form->{title},
1562                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
1563                        'std_column_visibility' => 1,
1564     );
1565   $report->set_options_from_form();
1566
1567   $report->set_columns(%column_defs);
1568   $report->set_column_order(@columns);
1569
1570   $report->set_export_options($action, @hidden_variables);
1571
1572   $report->set_sort_indicator('accno', 1);
1573
1574   my @totals_columns = qw(credit debit begbalance endbalance);
1575   my %subtotals      = map { $_ => 0 } @totals_columns;
1576   my %totals         = map { $_ => 0 } @totals_columns;
1577   my $found_heading  = 0;
1578   my @tb             = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} };
1579
1580   # sort the whole thing by account numbers and display
1581   foreach my $idx (0 .. scalar(@tb) - 1) {
1582     my $ref  = $tb[$idx];
1583     my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
1584
1585     my $ml   = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
1586
1587     my $row  = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
1588
1589     if ($ref->{charttype} eq 'H') {
1590       next unless ($form->{l_heading});
1591
1592       %subtotals                   = map { $_ => 0 } @totals_columns;
1593       $found_heading               = 1;
1594       $row->{description}->{class} = 'listheading';
1595       $row->{description}->{data}  = $ref->{description};
1596
1597       $report->add_data($row);
1598
1599       next;
1600     }
1601
1602     foreach (qw(debit credit)) {
1603       $subtotals{$_} += $ref->{$_};
1604       $totals{$_}    += $ref->{$_};
1605     }
1606
1607     $subtotals{begbalance} += $ref->{balance} * $ml;
1608     $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
1609
1610     map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
1611     map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
1612
1613     $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
1614     $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
1615
1616     $report->add_data($row);
1617
1618     if ($form->{l_heading} && $found_heading &&
1619         (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
1620       $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
1621     }
1622   }
1623
1624   $report->add_separator();
1625
1626   $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
1627
1628   $report->generate_with_headers();
1629
1630   $lxdebug->leave_sub();
1631 }
1632
1633 sub generate_ar_aging {
1634   $lxdebug->enter_sub();
1635
1636   $auth->assert('general_ledger');
1637
1638   # split customer
1639   ($form->{customer}) = split(/--/, $form->{customer});
1640
1641   $form->{ct}   = "customer";
1642   $form->{arap} = "ar";
1643
1644   $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
1645
1646   RP->aging(\%myconfig, \%$form);
1647   aging();
1648
1649   $lxdebug->leave_sub();
1650 }
1651
1652 sub generate_ap_aging {
1653   $lxdebug->enter_sub();
1654
1655   $auth->assert('general_ledger');
1656
1657   # split vendor
1658   ($form->{vendor}) = split(/--/, $form->{vendor});
1659
1660   $form->{ct}   = "vendor";
1661   $form->{arap} = "ap";
1662
1663   $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
1664
1665   RP->aging(\%myconfig, \%$form);
1666   aging();
1667
1668   $lxdebug->leave_sub();
1669 }
1670
1671 sub create_aging_subtotal_row {
1672   $lxdebug->enter_sub();
1673
1674   my ($subtotals, $columns, $periods, $class) = @_;
1675
1676   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
1677
1678   foreach (@{ $periods }) {
1679     $row->{"c$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
1680     $subtotals->{$_}      = 0;
1681   }
1682
1683   $lxdebug->leave_sub();
1684
1685   return $row;
1686 }
1687
1688 sub aging {
1689   $lxdebug->enter_sub();
1690
1691   $auth->assert('general_ledger');
1692
1693   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1694
1695   my @columns = qw(statement ct invnumber transdate duedate c0 c30 c60 c90);
1696
1697   my %column_defs = (
1698     'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
1699     'ct'        => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1700     'invnumber' => { 'text' => $locale->text('Invoice'), },
1701     'transdate' => { 'text' => $locale->text('Date'), },
1702     'duedate'   => { 'text' => $locale->text('Due'), },
1703     'c0'        => { 'text' => $locale->text('Current'), },
1704     'c30'       => { 'text' => '30', },
1705     'c60'       => { 'text' => '60', },
1706     'c90'       => { 'text' => '90', },
1707   );
1708
1709   my %column_alignment = ('statement' => 'center',
1710                           map { $_ => 'right' } qw(c0 c30 c60 c90));
1711
1712   $report->set_options('std_column_visibility' => 1);
1713   $report->set_columns(%column_defs);
1714   $report->set_column_order(@columns);
1715
1716   my @hidden_variables = qw(todate customer vendor arap title ct);
1717   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
1718
1719   my @options;
1720
1721   if ($form->{department}) {
1722     my ($department) = split /--/, $form->{department};
1723     push @options, $locale->text('Department') . " : $department";
1724     $form->{callback} .= "&department=" . E($department);
1725   }
1726
1727   if (($form->{arap} eq 'ar') && $form->{customer}) {
1728     push @options, $form->{customer};
1729   }
1730
1731   if (($form->{arap} eq 'ap') && $form->{vendor}) {
1732     push @options, $form->{vendor};
1733   }
1734
1735   push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1736
1737   my $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1738
1739   $report->set_options('top_info_text'        => join("\n", @options),
1740                        'output_format'        => 'HTML',
1741                        'title'                => $form->{title},
1742                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
1743     );
1744
1745   my $previous_ctid = 0;
1746   my $row_idx       = 0;
1747   my @periods       = qw(0 30 60 90);
1748   my %subtotals     = map { $_ => 0 } @periods;
1749   my %totals        = map { $_ => 0 } @periods;
1750
1751   foreach $ref (@{ $form->{AG} }) {
1752     if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1753       $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1754     }
1755
1756     foreach my $key (@periods) {
1757       $subtotals{$key}  += $ref->{"c${key}"};
1758       $totals{$key}     += $ref->{"c${key}"};
1759       $ref->{"c${key}"}  = $ref->{"c${key}"} != 0 ? $form->format_amount(\%myconfig, $ref->{"c${key}"}, 2) : '';
1760     }
1761
1762     my $row = { };
1763
1764     foreach my $column (@columns) {
1765       $row->{$column} = {
1766         'data'   => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1767         'align'  => $column_alignment{$column},
1768         'valign' => $column eq 'statement' ? 'center' : '',
1769       };
1770     }
1771
1772     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1773
1774     if ($previous_ctid != $ref->{ctid}) {
1775       $row->{statement}->{raw_data} =
1776           $cgi->hidden('-name' => "customer_id_${row_idx}", '-value' => $ref->{ctid})
1777         . $cgi->checkbox('-name' => "statement_${row_idx}", '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1778       $row->{ct}->{data} = $ref->{name};
1779
1780       $row_idx++;
1781     }
1782
1783     $previous_ctid = $ref->{ctid};
1784
1785     $report->add_data($row);
1786   }
1787
1788   $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1789
1790   $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1791
1792   if ($form->{arap} eq 'ar') {
1793     $raw_top_info_text    = $form->parse_html_template('rp/aging_ar_top');
1794     $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1795                                                                                'PRINT_OPTIONS' => print_options(inline => 1), });
1796     $report->set_options('raw_top_info_text'    => $raw_top_info_text,
1797                          'raw_bottom_info_text' => $raw_bottom_info_text);
1798   }
1799
1800   $report->set_options_from_form();
1801
1802   $report->generate_with_headers();
1803
1804   $lxdebug->leave_sub();
1805 }
1806
1807 sub select_all {
1808   $lxdebug->enter_sub();
1809
1810   RP->aging(\%myconfig, \%$form);
1811
1812   map { $_->{checked} = "checked" } @{ $form->{AG} };
1813
1814   &aging;
1815
1816   $lxdebug->leave_sub();
1817 }
1818
1819 sub e_mail {
1820   $lxdebug->enter_sub();
1821
1822   $auth->assert('general_ledger');
1823
1824   # get name and email addresses
1825   for $i (1 .. $form->{rowcount}) {
1826     if ($form->{"statement_$i"}) {
1827       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1828       RP->get_customer(\%myconfig, \%$form);
1829       $selected = 1;
1830       last;
1831     }
1832   }
1833
1834   $form->error($locale->text('Nothing selected!')) unless $selected;
1835
1836   if ($myconfig{role} eq 'admin') {
1837     $bcc = qq|
1838           <th align=right nowrap=true>| . $locale->text('Bcc') . qq|</th>
1839           <td><input name=bcc size=30 value="$form->{bcc}"></td>
1840 |;
1841   }
1842
1843   $title = $locale->text('E-mail Statement to') . " $form->{$form->{ct}}";
1844
1845   $form->{media} = "email";
1846
1847   $form->header;
1848
1849   print qq|
1850 <body>
1851
1852 <form method=post action=$form->{script}>
1853
1854 <table width=100%>
1855   <tr class=listtop>
1856     <th>$title</th>
1857   </tr>
1858   <tr height="5"></tr>
1859   <tr>
1860     <td>
1861       <table width=100%>
1862         <tr>
1863           <th align=right nowrap>| . $locale->text('E-mail') . qq|</th>
1864           <td><input name=email size=30 value="$form->{email}"></td>
1865           <th align=right nowrap>| . $locale->text('Cc') . qq|</th>
1866           <td><input name=cc size=30 value="$form->{cc}"></td>
1867         </tr>
1868         <tr>
1869           <th align=right nowrap>| . $locale->text('Subject') . qq|</th>
1870           <td><input name=subject size=30 value="$form->{subject}"></td>
1871           $bcc
1872         </tr>
1873       </table>
1874     </td>
1875   </tr>
1876   <tr>
1877     <td>
1878       <table width=100%>
1879         <tr>
1880           <th align=left nowrap>| . $locale->text('Message') . qq|</th>
1881         </tr>
1882         <tr>
1883           <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
1884         </tr>
1885       </table>
1886     </td>
1887   </tr>
1888   <tr>
1889     <td>
1890 |;
1891
1892   &print_options;
1893
1894   map { delete $form->{$_} }
1895     qw(action email cc bcc subject message type sendmode format header);
1896
1897   # save all other variables
1898   foreach $key (keys %$form) {
1899     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1900     $form->{$key} =~ s/\"/&quot;/g;
1901     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1902   }
1903
1904   print qq|
1905     </td>
1906   </tr>
1907   <tr>
1908     <td><hr size=3 noshade></td>
1909   </tr>
1910 </table>
1911
1912 <input type=hidden name=nextsub value=send_email>
1913
1914 <br>
1915 <input name=action class=submit type=submit value="|
1916     . $locale->text('Continue') . qq|">
1917 </form>
1918
1919 </body>
1920 </html>
1921 |;
1922
1923   $lxdebug->leave_sub();
1924 }
1925
1926 sub send_email {
1927   $lxdebug->enter_sub();
1928
1929   $auth->assert('general_ledger');
1930
1931   $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1932     unless $form->{subject};
1933
1934   RP->aging(\%myconfig, \%$form);
1935
1936   $form->{"statement_1"} = 1;
1937
1938   $form->{media} = 'email';
1939   print_form();
1940
1941   $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1942
1943   $lxdebug->leave_sub();
1944 }
1945
1946 sub print {
1947   $lxdebug->enter_sub();
1948
1949   $auth->assert('general_ledger');
1950
1951   if ($form->{media} eq 'printer') {
1952     $form->error($locale->text('Select postscript or PDF!'))
1953       if ($form->{format} !~ /(postscript|pdf)/);
1954   }
1955
1956   for $i (1 .. $form->{rowcount}) {
1957     if ($form->{"statement_$i"}) {
1958       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1959       $selected = 1;
1960       last;
1961     }
1962   }
1963
1964   $form->error($locale->text('Nothing selected!')) unless $selected;
1965
1966   if ($form->{media} eq 'printer') {
1967     $form->{"$form->{ct}_id"} = "";
1968   } else {
1969     $form->{"statement_1"} = 1;
1970   }
1971
1972   RP->aging(\%myconfig, \%$form);
1973
1974   print_form();
1975
1976   $form->redirect($locale->text('Statements sent to printer!'))
1977     if ($form->{media} eq 'printer');
1978
1979   $lxdebug->leave_sub();
1980 }
1981
1982 sub print_form {
1983   $lxdebug->enter_sub();
1984
1985   $auth->assert('general_ledger');
1986
1987   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1988
1989   $form->{templates} = "$myconfig{templates}";
1990
1991   my $suffix = "html";
1992   my $attachment_suffix = "html";
1993   if ($form->{format} eq 'postscript') {
1994     $form->{postscript} = 1;
1995     $suffix = "tex";
1996     $attachment_suffix = "ps";
1997   } elsif ($form->{format} eq 'pdf') {
1998     $form->{pdf} = 1;
1999     $suffix = "tex";
2000     $attachment_suffix = "pdf";
2001   }
2002
2003   $form->{IN}  = "$form->{type}.$suffix";
2004   $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
2005
2006   # Save $form->{email} because it will be overwritten.
2007   $form->{EMAIL_RECIPIENT} = $form->{email};
2008
2009   $i = 0;
2010   while (@{ $form->{AG} }) {
2011
2012     $ref = shift @{ $form->{AG} };
2013
2014     if ($ctid != $ref->{ctid}) {
2015
2016       $ctid = $ref->{ctid};
2017       $i++;
2018
2019       if ($form->{"statement_$i"}) {
2020
2021         @a =
2022           (name, street, zipcode, city, country, contact, email,
2023            "$form->{ct}phone", "$form->{ct}fax");
2024         map { $form->{$_} = $ref->{$_} } @a;
2025
2026         $form->{ $form->{ct} } = $form->{name};
2027         $form->{"$form->{ct}_id"} = $ref->{ctid};
2028
2029         map { $form->{$_} = () } qw(invnumber invdate duedate);
2030         $form->{total} = 0;
2031         foreach $item (qw(c0 c30 c60 c90)) {
2032           $form->{$item} = ();
2033           $form->{"${item}total"} = 0;
2034         }
2035
2036         &statement_details($ref);
2037
2038         while ($ref) {
2039
2040           if (scalar(@{ $form->{AG} }) > 0) {
2041
2042             # one or more left to go
2043             if ($ctid == $form->{AG}->[0]->{ctid}) {
2044               $ref = shift @{ $form->{AG} };
2045               &statement_details($ref);
2046
2047               # any more?
2048               $ref = scalar(@{ $form->{AG} });
2049             } else {
2050               $ref = 0;
2051             }
2052           } else {
2053
2054             # set initial ref to 0
2055             $ref = 0;
2056           }
2057
2058         }
2059
2060         map {
2061           $form->{"${_}total"} =
2062             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
2063         } (c0, c30, c60, c90, "");
2064
2065         $form->{attachment_filename} =  $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
2066         $form->{attachment_filename} =~ s/\s+/_/g;
2067
2068         $form->parse_template(\%myconfig, $userspath);
2069
2070       }
2071     }
2072   }
2073   # saving the history
2074   if(!exists $form->{addition} && $form->{id} ne "") {
2075     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
2076         $form->{addition} = "PRINTED";
2077         $form->{what_done} = $form->{type};
2078         $form->save_history($form->dbconnect(\%myconfig));
2079   }
2080   # /saving the history 
2081   $lxdebug->leave_sub();
2082 }
2083
2084 sub statement_details {
2085   $lxdebug->enter_sub();
2086
2087   $auth->assert('general_ledger');
2088
2089   my ($ref) = @_;
2090
2091   push @{ $form->{invnumber} }, $ref->{invnumber};
2092   push @{ $form->{invdate} },   $ref->{transdate};
2093   push @{ $form->{duedate} },   $ref->{duedate};
2094
2095   foreach $item (qw(c0 c30 c60 c90)) {
2096     if ($ref->{exchangerate} * 1) {
2097       $ref->{$item} =
2098         $form->round_amount($ref->{$item} / $ref->{exchangerate}, 2);
2099     }
2100     $form->{"${item}total"} += $ref->{$item};
2101     $form->{total}          += $ref->{$item};
2102     push @{ $form->{$item} },
2103       $form->format_amount(\%myconfig, $ref->{$item}, 2);
2104   }
2105
2106   $lxdebug->leave_sub();
2107 }
2108
2109 sub generate_tax_report {
2110   $lxdebug->enter_sub();
2111
2112   $auth->assert('report');
2113
2114   RP->tax_report(\%myconfig, \%$form);
2115
2116   $descvar     = "$form->{accno}_description";
2117   $description = $form->escape($form->{$descvar});
2118   $ratevar     = "$form->{accno}_rate";
2119
2120   $department = $form->escape($form->{department});
2121
2122   # construct href
2123   $href =
2124     "$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}";
2125
2126   # construct callback
2127   $description = $form->escape($form->{$descvar},   1);
2128   $department  = $form->escape($form->{department}, 1);
2129   $callback    =
2130     "$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}";
2131
2132   $title = $form->escape($form->{title});
2133   $href .= "&title=$title";
2134   $title = $form->escape($form->{title}, 1);
2135   $callback .= "&title=$title";
2136
2137   $form->{title} = qq|$form->{title} $form->{"$form->{accno}_description"} |;
2138
2139   @columns =
2140     $form->sort_columns(qw(id transdate invnumber name netamount tax amount));
2141
2142   foreach $item (@columns) {
2143     if ($form->{"l_$item"} eq "Y") {
2144       push @column_index, $item;
2145
2146       # add column to href and callback
2147       $callback .= "&l_$item=Y";
2148       $href     .= "&l_$item=Y";
2149     }
2150   }
2151
2152   if ($form->{l_subtotal} eq 'Y') {
2153     $callback .= "&l_subtotal=Y";
2154     $href     .= "&l_subtotal=Y";
2155   }
2156
2157   if ($form->{department}) {
2158     ($department) = split /--/, $form->{department};
2159     $option = $locale->text('Department') . " : $department";
2160   }
2161
2162   # if there are any dates
2163   if ($form->{fromdate} || $form->{todate}) {
2164     if ($form->{fromdate}) {
2165       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
2166     }
2167     if ($form->{todate}) {
2168       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
2169     }
2170
2171     $form->{period} = "$fromdate - $todate";
2172   } else {
2173     $form->{period} =
2174       $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
2175   }
2176
2177   if ($form->{db} eq 'ar') {
2178     $name    = $locale->text('Customer');
2179     $invoice = 'is.pl';
2180     $arap    = 'ar.pl';
2181   }
2182   if ($form->{db} eq 'ap') {
2183     $name    = $locale->text('Vendor');
2184     $invoice = 'ir.pl';
2185     $arap    = 'ap.pl';
2186   }
2187
2188   $option .= "<br>" if $option;
2189   $option .= "$form->{period}";
2190
2191   $column_header{id} =
2192       qq|<th><a class=listheading href=$href&sort=id>|
2193     . $locale->text('ID')
2194     . qq|</th>|;
2195   $column_header{invnumber} =
2196       qq|<th><a class=listheading href=$href&sort=invnumber>|
2197     . $locale->text('Invoice')
2198     . qq|</th>|;
2199   $column_header{transdate} =
2200       qq|<th><a class=listheading href=$href&sort=transdate>|
2201     . $locale->text('Date')
2202     . qq|</th>|;
2203   $column_header{netamount} =
2204     qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
2205   $column_header{tax} =
2206     qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
2207   $column_header{amount} =
2208     qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
2209
2210   $column_header{name} =
2211     qq|<th><a class=listheading href=$href&sort=name>$name</th>|;
2212
2213   $form->header;
2214
2215   print qq|
2216 <body>
2217
2218 <table width=100%>
2219   <tr>
2220     <th class=listtop colspan=$colspan>$form->{title}</th>
2221   </tr>
2222   <tr height="5"></tr>
2223   <tr>
2224     <td>$option</td>
2225   </tr>
2226   <tr>
2227     <td>
2228       <table width=100%>
2229         <tr class=listheading>
2230 |;
2231
2232   map { print "$column_header{$_}\n" } @column_index;
2233
2234   print qq|
2235         </tr>
2236 |;
2237
2238   # add sort and escape callback
2239   $callback = $form->escape($callback . "&sort=$form->{sort}");
2240
2241   if (@{ $form->{TR} }) {
2242     $sameitem = $form->{TR}->[0]->{ $form->{sort} };
2243   }
2244
2245   foreach $ref (@{ $form->{TR} }) {
2246
2247     $module = ($ref->{invoice}) ? $invoice : $arap;
2248
2249     if ($form->{l_subtotal} eq 'Y') {
2250       if ($sameitem ne $ref->{ $form->{sort} }) {
2251         &tax_subtotal;
2252         $sameitem = $ref->{ $form->{sort} };
2253       }
2254     }
2255
2256     $totalnetamount += $ref->{netamount};
2257     $totaltax       += $ref->{tax};
2258     $ref->{amount} = $ref->{netamount} + $ref->{tax};
2259
2260     $subtotalnetamount += $ref->{netamount};
2261     $subtotaltax       += $ref->{tax};
2262
2263     map {
2264       $ref->{$_} = $form->format_amount(\%myconfig, $ref->{$_}, 2, "&nbsp;");
2265     } qw(netamount tax amount);
2266
2267     $column_data{id}        = qq|<td>$ref->{id}</td>|;
2268     $column_data{invnumber} =
2269       qq|<td><a href=$module?action=edit&id=$ref->{id}&callback=$callback>$ref->{invnumber}</a></td>|;
2270     $column_data{transdate} = qq|<td>$ref->{transdate}</td>|;
2271     $column_data{name}      = qq|<td>$ref->{name}&nbsp;</td>|;
2272
2273     map { $column_data{$_} = qq|<td align=right>$ref->{$_}</td>| }
2274       qw(netamount tax amount);
2275
2276     $i++;
2277     $i %= 2;
2278     print qq|
2279         <tr class=listrow$i>
2280 |;
2281
2282     map { print "$column_data{$_}\n" } @column_index;
2283
2284     print qq|
2285         </tr>
2286 |;
2287
2288   }
2289
2290   if ($form->{l_subtotal} eq 'Y') {
2291     &tax_subtotal;
2292   }
2293
2294   map { $column_data{$_} = qq|<th>&nbsp;</th>| } @column_index;
2295
2296   print qq|
2297         </tr>
2298         <tr class=listtotal>
2299 |;
2300
2301   $total =
2302     $form->format_amount(\%myconfig, $totalnetamount + $totaltax, 2, "&nbsp;");
2303   $totalnetamount =
2304     $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;");
2305   $totaltax = $form->format_amount(\%myconfig, $totaltax, 2, "&nbsp;");
2306
2307   $column_data{netamount} =
2308     qq|<th class=listtotal align=right>$totalnetamount</th>|;
2309   $column_data{tax}    = qq|<th class=listtotal align=right>$totaltax</th>|;
2310   $column_data{amount} = qq|<th class=listtotal align=right>$total</th>|;
2311
2312   map { print "$column_data{$_}\n" } @column_index;
2313
2314   print qq|
2315         </tr>
2316       </table>
2317     </td>
2318   </tr>
2319   <tr>
2320     <td><hr size=3 noshade></td>
2321   </tr>
2322 </table>
2323
2324 </body>
2325 </html>
2326 |;
2327
2328   $lxdebug->leave_sub();
2329 }
2330
2331 sub tax_subtotal {
2332   $lxdebug->enter_sub();
2333
2334   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
2335
2336   $subtotalnetamount =
2337     $form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;");
2338   $subtotaltax = $form->format_amount(\%myconfig, $subtotaltax, 2, "&nbsp;");
2339   $subtotal =
2340     $form->format_amount(\%myconfig, $subtotalnetamount + $subtotaltax,
2341                          2, "&nbsp;");
2342
2343   $column_data{netamount} =
2344     "<th class=listsubtotal align=right>$subtotalnetamount</th>";
2345   $column_data{tax} = "<th class=listsubtotal align=right>$subtotaltax</th>";
2346   $column_data{amount} = "<th class=listsubtotal align=right>$subtotal</th>";
2347
2348   $subtotalnetamount = 0;
2349   $subtotaltax       = 0;
2350
2351   print qq|
2352         <tr class=listsubtotal>
2353 |;
2354   map { print "\n$column_data{$_}" } @column_index;
2355
2356   print qq|
2357         </tr>
2358 |;
2359
2360   $lxdebug->leave_sub();
2361 }
2362
2363 sub list_payments {
2364   $lxdebug->enter_sub();
2365
2366   $auth->assert('cash');
2367
2368   if ($form->{account}) {
2369     ($form->{paymentaccounts}) = split /--/, $form->{account};
2370   }
2371   if ($form->{department}) {
2372     ($department, $form->{department_id}) = split /--/, $form->{department};
2373     $option = $locale->text('Department') . " : $department";
2374   }
2375
2376   RP->payments(\%myconfig, \%$form);
2377
2378   my @hidden_variables = qw(account title department reference source memo fromdate todate
2379                             fx_transaction db prepayment paymentaccounts sort);
2380
2381   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
2382   $form->{callback} = $href;
2383
2384   my @columns     = qw(transdate invnumber name paid source memo);
2385   my %column_defs = (
2386     'name'      => { 'text' => $locale->text('Description'), },
2387     'invnumber' => { 'text' => $locale->text('Reference'), },
2388     'transdate' => { 'text' => $locale->text('Date'), },
2389     'paid'      => { 'text' => $locale->text('Amount'), },
2390     'source'    => { 'text' => $locale->text('Source'), },
2391     'memo'      => { 'text' => $locale->text('Memo'), },
2392   );
2393   my %column_alignment = ('paid' => 'right');
2394
2395   map { $column_defs{$_}->{link} = $href . "&sort=$_" } grep { $_ ne 'paid' } @columns;
2396
2397   my @options;
2398   if ($form->{fromdate}) {
2399     push @options, $locale->text('From') . "&nbsp;" . $locale->date(\%myconfig, $form->{fromdate}, 1);
2400   }
2401   if ($form->{todate}) {
2402     push @options, $locale->text('bis') . "&nbsp;" . $locale->date(\%myconfig, $form->{todate}, 1);
2403   }
2404
2405   my $report = SL::ReportGenerator->new(\%myconfig, $form);
2406
2407   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
2408
2409   $report->set_options('top_info_text'         => join("\n", @options),
2410                        'output_format'         => 'HTML',
2411                        'title'                 => $form->{title},
2412                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
2413                        'std_column_visibility' => 1,
2414     );
2415   $report->set_options_from_form();
2416
2417   $report->set_columns(%column_defs);
2418   $report->set_column_order(@columns);
2419
2420   $report->set_export_options('list_payments', @hidden_variables);
2421
2422   $report->set_sort_indicator($form->{sort}, 1);
2423
2424   my $total_paid    = 0;
2425
2426   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
2427     next unless @{ $form->{ $ref->{id} } };
2428
2429     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
2430
2431     my $subtotal_paid = 0;
2432
2433     foreach my $payment (@{ $form->{ $ref->{id} } }) {
2434       my $module = $payment->{module};
2435       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
2436       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
2437
2438       $subtotal_paid += $payment->{paid};
2439       $total_paid    += $payment->{paid};
2440
2441       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
2442
2443       my $row = { };
2444
2445       foreach my $column (@columns) {
2446         $row->{$column} = {
2447           'data'  => $payment->{$column},
2448           'align' => $column_alignment{$column},
2449         };
2450       }
2451
2452       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
2453
2454       $report->add_data($row);
2455     }
2456
2457     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
2458     $row->{paid} = {
2459       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
2460       'align' => 'right',
2461       'class' => 'listsubtotal',
2462     };
2463
2464     $report->add_data($row);
2465   }
2466
2467   $report->add_separator();
2468
2469   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
2470   $row->{paid} = {
2471     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
2472     'align' => 'right',
2473     'class' => 'listtotal',
2474   };
2475
2476   $report->add_data($row);
2477
2478   $report->generate_with_headers();
2479
2480   $lxdebug->leave_sub();
2481 }
2482
2483 sub print_options {
2484   $lxdebug->enter_sub();
2485
2486   my ($dont_print) = @_;
2487
2488   $form->{sendmode} = "attachment";
2489
2490   $form->{"format"} =
2491     $form->{"format"} ? $form->{"format"} :
2492     $myconfig{"template_format"} ? $myconfig{"template_format"} :
2493     "pdf";
2494
2495   $form->{"copies"} =
2496     $form->{"copies"} ? $form->{"copies"} :
2497     $myconfig{"copies"} ? $myconfig{"copies"} :
2498     2;
2499
2500   $form->{PD}{ $form->{type} }     = "selected";
2501   $form->{DF}{ $form->{format} }   = "selected";
2502   $form->{OP}{ $form->{media} }    = "selected";
2503   $form->{SM}{ $form->{sendmode} } = "selected";
2504
2505   $type = qq|
2506             <option value=statement $form->{PD}{statement}>|
2507       . $locale->text('Statement');
2508
2509   if ($form->{media} eq 'email') {
2510     $media = qq|
2511             <option value=attachment $form->{SM}{attachment}>|
2512       . $locale->text('Attachment') . qq|
2513             <option value=inline $form->{SM}{inline}>| . $locale->text('In-line');
2514   } else {
2515     $media = qq|
2516             <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
2517     if ($myconfig{printer} && $latex_templates) {
2518       $media .= qq|
2519             <option value=printer $form->{OP}{printer}>|
2520         . $locale->text('Printer');
2521     }
2522   }
2523
2524   if ($latex_templates) {
2525     $format .= qq|
2526             <option value=html $form->{DF}{html}>| . $locale->text('HTML')
2527       . qq| <option value=pdf $form->{DF}{pdf}>| . $locale->text('PDF')
2528       . qq| <option value=postscript $form->{DF}{postscript}>| . $locale->text('Postscript');
2529   }
2530
2531   my $output = qq|
2532 <table>
2533   <tr>
2534     <td><select name=type>$type</select></td>
2535     <td><select name=format>$format</select></td>
2536     <td><select name=media>$media</select></td>
2537 |;
2538
2539   if ($myconfig{printer} && $latex_templates && $form->{media} ne 'email') {
2540     $output .= qq|
2541       <td>| . $locale->text('Copies') . qq|
2542       <input name=copies size=2 value=$form->{copies}></td>
2543 |;
2544   }
2545
2546   $output .= qq|
2547   </tr>
2548 </table>
2549 |;
2550
2551   print $output unless $dont_print;
2552
2553   $lxdebug->leave_sub();
2554
2555   return $output;
2556 }
2557
2558 sub generate_bwa {
2559   $lxdebug->enter_sub();
2560
2561   $auth->assert('report');
2562
2563   $form->{padding} = "&nbsp;&nbsp;";
2564   $form->{bold}    = "<b>";
2565   $form->{endbold} = "</b>";
2566   $form->{br}      = "<br>";
2567
2568   if ($form->{reporttype} eq "custom") {
2569
2570     #forgotten the year --> thisyear
2571     if ($form->{year} !~ m/^\d\d\d\d$/) {
2572       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
2573         /(\d\d\d\d)/;
2574       $form->{year} = $1;
2575     }
2576
2577     #yearly report
2578     if ($form->{duetyp} eq "13") {
2579       $form->{fromdate}        = "1.1.$form->{year}";
2580       $form->{todate}          = "31.12.$form->{year}";
2581       $form->{comparefromdate} = "1.01.$form->{year}";
2582       $form->{comparetodate}   = "31.12.$form->{year}";
2583     }
2584
2585     #Quater reports
2586     if ($form->{duetyp} eq "A") {
2587       $form->{fromdate}        = "1.1.$form->{year}";
2588       $form->{todate}          = "31.3.$form->{year}";
2589       $form->{comparefromdate} = "1.01.$form->{year}";
2590       $form->{comparetodate}   = "31.03.$form->{year}";
2591     }
2592     if ($form->{duetyp} eq "B") {
2593       $form->{fromdate}        = "1.4.$form->{year}";
2594       $form->{todate}          = "30.6.$form->{year}";
2595       $form->{comparefromdate} = "1.01.$form->{year}";
2596       $form->{comparetodate}   = "30.06.$form->{year}";
2597     }
2598     if ($form->{duetyp} eq "C") {
2599       $form->{fromdate}        = "1.7.$form->{year}";
2600       $form->{todate}          = "30.9.$form->{year}";
2601       $form->{comparefromdate} = "1.01.$form->{year}";
2602       $form->{comparetodate}   = "30.09.$form->{year}";
2603     }
2604     if ($form->{duetyp} eq "D") {
2605       $form->{fromdate}        = "1.10.$form->{year}";
2606       $form->{todate}          = "31.12.$form->{year}";
2607       $form->{comparefromdate} = "1.01.$form->{year}";
2608       $form->{comparetodate}   = "31.12.$form->{year}";
2609     }
2610
2611     #Monthly reports
2612   SWITCH: {
2613       $form->{duetyp} eq "1" && do {
2614         $form->{fromdate}        = "1.1.$form->{year}";
2615         $form->{todate}          = "31.1.$form->{year}";
2616         $form->{comparefromdate} = "1.01.$form->{year}";
2617         $form->{comparetodate}   = "31.01.$form->{year}";
2618         last SWITCH;
2619       };
2620       $form->{duetyp} eq "2" && do {
2621         $form->{fromdate} = "1.2.$form->{year}";
2622
2623         #this works from 1901 to 2099, 1900 and 2100 fail.
2624         $leap = ($form->{year} % 4 == 0) ? "29" : "28";
2625         $form->{todate}          = "$leap.2.$form->{year}";
2626         $form->{comparefromdate} = "1.01.$form->{year}";
2627         $form->{comparetodate}   = "$leap.02.$form->{year}";
2628         last SWITCH;
2629       };
2630       $form->{duetyp} eq "3" && do {
2631         $form->{fromdate}        = "1.3.$form->{year}";
2632         $form->{todate}          = "31.3.$form->{year}";
2633         $form->{comparefromdate} = "1.01.$form->{year}";
2634         $form->{comparetodate}   = "31.03.$form->{year}";
2635         last SWITCH;
2636       };
2637       $form->{duetyp} eq "4" && do {
2638         $form->{fromdate}        = "1.4.$form->{year}";
2639         $form->{todate}          = "30.4.$form->{year}";
2640         $form->{comparefromdate} = "1.01.$form->{year}";
2641         $form->{comparetodate}   = "30.04.$form->{year}";
2642         last SWITCH;
2643       };
2644       $form->{duetyp} eq "5" && do {
2645         $form->{fromdate}        = "1.5.$form->{year}";
2646         $form->{todate}          = "31.5.$form->{year}";
2647         $form->{comparefromdate} = "1.01.$form->{year}";
2648         $form->{comparetodate}   = "31.05.$form->{year}";
2649         last SWITCH;
2650       };
2651       $form->{duetyp} eq "6" && do {
2652         $form->{fromdate}        = "1.6.$form->{year}";
2653         $form->{todate}          = "30.6.$form->{year}";
2654         $form->{comparefromdate} = "1.01.$form->{year}";
2655         $form->{comparetodate}   = "30.06.$form->{year}";
2656         last SWITCH;
2657       };
2658       $form->{duetyp} eq "7" && do {
2659         $form->{fromdate}        = "1.7.$form->{year}";
2660         $form->{todate}          = "31.7.$form->{year}";
2661         $form->{comparefromdate} = "1.01.$form->{year}";
2662         $form->{comparetodate}   = "31.07.$form->{year}";
2663         last SWITCH;
2664       };
2665       $form->{duetyp} eq "8" && do {
2666         $form->{fromdate}        = "1.8.$form->{year}";
2667         $form->{todate}          = "31.8.$form->{year}";
2668         $form->{comparefromdate} = "1.01.$form->{year}";
2669         $form->{comparetodate}   = "31.08.$form->{year}";
2670         last SWITCH;
2671       };
2672       $form->{duetyp} eq "9" && do {
2673         $form->{fromdate}        = "1.9.$form->{year}";
2674         $form->{todate}          = "30.9.$form->{year}";
2675         $form->{comparefromdate} = "1.01.$form->{year}";
2676         $form->{comparetodate}   = "30.09.$form->{year}";
2677         last SWITCH;
2678       };
2679       $form->{duetyp} eq "10" && do {
2680         $form->{fromdate}        = "1.10.$form->{year}";
2681         $form->{todate}          = "31.10.$form->{year}";
2682         $form->{comparefromdate} = "1.01.$form->{year}";
2683         $form->{comparetodate}   = "31.10.$form->{year}";
2684         last SWITCH;
2685       };
2686       $form->{duetyp} eq "11" && do {
2687         $form->{fromdate}        = "1.11.$form->{year}";
2688         $form->{todate}          = "30.11.$form->{year}";
2689         $form->{comparefromdate} = "1.01.$form->{year}";
2690         $form->{comparetodate}   = "30.11.$form->{year}";
2691         last SWITCH;
2692       };
2693       $form->{duetyp} eq "12" && do {
2694         $form->{fromdate}        = "1.12.$form->{year}";
2695         $form->{todate}          = "31.12.$form->{year}";
2696         $form->{comparefromdate} = "1.01.$form->{year}";
2697         $form->{comparetodate}   = "31.12.$form->{year}";
2698         last SWITCH;
2699       };
2700     }
2701   } else {
2702     ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate});
2703     $form->{fromdate} = "${dd}.${mm}.${yy}";
2704     ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{todate});
2705     $form->{todate}          = "${dd}.${mm}.${yy}";
2706     $form->{comparefromdate} = "01.01.$yy";
2707     $form->{comparetodate}   = $form->{todate};
2708   }
2709
2710   RP->bwa(\%myconfig, \%$form);
2711
2712   ($form->{department}) = split /--/, $form->{department};
2713
2714   $form->{period} =
2715     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
2716   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
2717
2718   # if there are any dates construct a where
2719   if ($form->{fromdate} || $form->{todate}) {
2720
2721     unless ($form->{todate}) {
2722       $form->{todate} = $form->current_date(\%myconfig);
2723     }
2724
2725     my %germandate = ("dateformat" => "dd.mm.yyyy");
2726
2727     $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
2728     $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
2729
2730     $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
2731     $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
2732
2733     $form->{this_period} = "$shortfromdate\n$shorttodate";
2734     $form->{period}      =
2735         $locale->text('for Period')
2736       . qq|\n$longfromdate |
2737       . $locale->text('bis')
2738       . qq| $longtodate|;
2739   }
2740
2741   # setup variables for the form
2742   @a = qw(company address businessnumber);
2743   map { $form->{$_} = $myconfig{$_} } @a;
2744   $form->{templates} = $myconfig{templates};
2745
2746   $form->{IN} = "bwa.html";
2747
2748   $form->parse_template;
2749
2750   $lxdebug->leave_sub();
2751 }
2752