Erfolgsrechnung 1st draft
[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::DB::Default;
41 use SL::DB::Project;
42 use SL::DB::Customer;
43 use SL::PE;
44 use SL::RP;
45 use SL::Iconv;
46 use SL::ReportGenerator;
47 use Data::Dumper;
48 use List::MoreUtils qw(any);
49
50 require "bin/mozilla/arap.pl";
51 require "bin/mozilla/common.pl";
52 require "bin/mozilla/reportgenerator.pl";
53
54 # note: this file was particularly hard to strictify.
55 # alot of the vars are passed carelessly between invocations
56 # should there be any missing vars, declare them globally
57 use strict;
58
59 # this is for our long dates
60 # $locale->text('January')
61 # $locale->text('February')
62 # $locale->text('March')
63 # $locale->text('April')
64 # $locale->text('May ')
65 # $locale->text('June')
66 # $locale->text('July')
67 # $locale->text('August')
68 # $locale->text('September')
69 # $locale->text('October')
70 # $locale->text('November')
71 # $locale->text('December')
72
73 # this is for our short month
74 # $locale->text('Jan')
75 # $locale->text('Feb')
76 # $locale->text('Mar')
77 # $locale->text('Apr')
78 # $locale->text('May')
79 # $locale->text('Jun')
80 # $locale->text('Jul')
81 # $locale->text('Aug')
82 # $locale->text('Sep')
83 # $locale->text('Oct')
84 # $locale->text('Nov')
85 # $locale->text('Dec')
86
87 # $locale->text('Balance Sheet')
88 # $locale->text('Income Statement')
89 # $locale->text('Trial Balance')
90 # $locale->text('AR Aging')
91 # $locale->text('AP Aging')
92 # $locale->text('Search AR Aging')
93 # $locale->text('Search AP Aging')
94 # $locale->text('Tax collected')
95 # $locale->text('Tax paid')
96 # $locale->text('Receipts')
97 # $locale->text('Payments')
98 # $locale->text('Project Transactions')
99 # $locale->text('Business evaluation')
100
101 # $form->parse_html_template('rp/html_report_susa')
102
103 my $rp_access_map = {
104   'projects'           => 'report',
105   'ar_aging'           => 'general_ledger',
106   'ap_aging'           => 'general_ledger',
107   'receipts'           => 'cash',
108   'payments'           => 'cash',
109   'trial_balance'      => 'report',
110   'income_statement'   => 'report',
111   'income_statement_ch'=> 'report',
112   'bwa'                => 'report',
113   'balance_sheet'      => 'report',
114 };
115
116 sub check_rp_access {
117   my $form     = $main::form;
118
119   my $right   = $rp_access_map->{$form->{report}};
120   $right    ||= 'DOES_NOT_EXIST';
121
122   $main::auth->assert($right);
123 }
124
125 sub report {
126   $::lxdebug->enter_sub;
127
128   check_rp_access();
129
130   my %title = (
131     balance_sheet        => $::locale->text('Balance Sheet'),
132     income_statement     => $::locale->text('Income Statement'),
133     income_statement_ch     => ('Erfolgsrechnung'),
134     trial_balance        => $::locale->text('Trial Balance'),
135     ar_aging             => $::locale->text('Search AR Aging'),
136     ap_aging             => $::locale->text('Search AP Aging'),
137     tax_collected        => $::locale->text('Tax collected'),
138     tax_paid             => $::locale->text('Tax paid'),
139     receipts             => $::locale->text('Receipts'),
140     payments             => $::locale->text('Payments'),
141     projects             => $::locale->text('Project Transactions'),
142     bwa                  => $::locale->text('Business evaluation'),
143   );
144
145   $::form->{title} = $title{$::form->{report}};
146   $::request->{layout}->add_javascripts('autocomplete_customer.js');
147
148   # get departments
149   $::form->all_departments(\%::myconfig);
150   if (@{ $::form->{all_departments} || [] }) {
151     $::form->{selectdepartment} = "<option>\n";
152     map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
153   }
154
155   $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
156
157   my $is_projects            = $::form->{report} eq "projects";
158   my $is_income_statement    = $::form->{report} eq "income_statement";
159   my $is_income_statement_ch = $::form->{report} eq "income_statement_ch";
160   my $is_bwa                 = $::form->{report} eq "bwa";
161   my $is_balance_sheet       = $::form->{report} eq "balance_sheet";
162   my $is_trial_balance       = $::form->{report} eq "trial_balance";
163   my $is_aging               = $::form->{report} =~ /^a[rp]_aging$/;
164   my $is_payments            = $::form->{report} =~ /(receipts|payments)$/;
165
166   my ($label, $nextsub, $vc);
167   if ($is_aging) {
168     my $is_sales  = $::form->{report} eq 'ar_aging';
169     $label        = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
170     $::form->{vc} = $is_sales ? 'customer' : 'vendor';
171
172     $nextsub = "generate_$::form->{report}";
173
174     # setup vc selection
175     $::form->all_vc(\%::myconfig, $::form->{vc}, $is_sales ? "AR" : "AP");
176     $vc .= "<option>$_->{name}--$_->{id}\n" for @{ $::form->{"all_$::form->{vc}"} };
177     $vc = ($vc)
178         ? qq|<select name=$::form->{vc} class="initial_focus"><option>\n$vc</select>|
179         : qq|<input name=$::form->{vc} size=35 class="initial_focus">|;
180   }
181
182   my ($selection, $paymentaccounts);
183   if ($is_payments) {
184     $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
185
186     RP->paymentaccounts(\%::myconfig, $::form);
187
188     $selection = "<option>\n";
189     for my $ref (@{ $::form->{PR} }) {
190       $paymentaccounts .= "$ref->{accno} ";
191       $selection       .= "<option>$ref->{accno}--$ref->{description}\n";
192     }
193   }
194
195   $::form->header;
196   print $::form->parse_html_template('rp/report', {
197     paymentaccounts        => $paymentaccounts,
198     selection              => $selection,
199     is_aging               => $is_aging,
200     vc                     => $vc,
201     label                  => $label,
202     year                   => DateTime->today->year,
203     today                  => DateTime->today,
204     nextsub                => $nextsub,
205     accrual                => $::instance_conf->get_accounting_method ne 'cash',
206     cash                   => $::instance_conf->get_accounting_method eq 'cash',
207     is_payments            => $is_payments,
208     is_trial_balance       => $is_trial_balance,
209     is_balance_sheet       => $is_balance_sheet,
210     is_bwa                 => $is_bwa,
211     is_income_statement    => $is_income_statement,
212     is_income_statement_ch => $is_income_statement_ch,
213     is_projects            => $is_projects,
214   });
215
216   $::lxdebug->leave_sub;
217 }
218
219 sub continue { call_sub($main::form->{"nextsub"}); }
220
221 sub generate_income_statement {
222   $main::lxdebug->enter_sub();
223
224   $main::auth->assert('report');
225
226   my $form     = $main::form;
227   my %myconfig = %main::myconfig;
228   my $locale   = $main::locale;
229
230   $form->{padding} = "&nbsp;&nbsp;";
231   $form->{bold}    = "<b>";
232   $form->{endbold} = "</b>";
233   $form->{br}      = "<br>";
234
235   if ($form->{reporttype} eq "custom") {
236
237     #forgotten the year --> thisyear
238     if ($form->{year} !~ m/^\d\d\d\d$/) {
239       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
240         /(\d\d\d\d)/;
241       $form->{year} = $1;
242     }
243
244     #yearly report
245     if ($form->{duetyp} eq "13") {
246       $form->{fromdate} = "1.1.$form->{year}";
247       $form->{todate}   = "31.12.$form->{year}";
248     }
249
250     #Quater reports
251     if ($form->{duetyp} eq "A") {
252       $form->{fromdate} = "1.1.$form->{year}";
253       $form->{todate}   = "31.3.$form->{year}";
254     }
255     if ($form->{duetyp} eq "B") {
256       $form->{fromdate} = "1.4.$form->{year}";
257       $form->{todate}   = "30.6.$form->{year}";
258     }
259     if ($form->{duetyp} eq "C") {
260       $form->{fromdate} = "1.7.$form->{year}";
261       $form->{todate}   = "30.9.$form->{year}";
262     }
263     if ($form->{duetyp} eq "D") {
264       $form->{fromdate} = "1.10.$form->{year}";
265       $form->{todate}   = "31.12.$form->{year}";
266     }
267
268     #Monthly reports
269   SWITCH: {
270       $form->{duetyp} eq "1" && do {
271         $form->{fromdate} = "1.1.$form->{year}";
272         $form->{todate}   = "31.1.$form->{year}";
273         last SWITCH;
274       };
275       $form->{duetyp} eq "2" && do {
276         $form->{fromdate} = "1.2.$form->{year}";
277
278         #this works from 1901 to 2099, 1900 and 2100 fail.
279         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
280         $form->{todate} = "$leap.2.$form->{year}";
281         last SWITCH;
282       };
283       $form->{duetyp} eq "3" && do {
284         $form->{fromdate} = "1.3.$form->{year}";
285         $form->{todate}   = "31.3.$form->{year}";
286         last SWITCH;
287       };
288       $form->{duetyp} eq "4" && do {
289         $form->{fromdate} = "1.4.$form->{year}";
290         $form->{todate}   = "30.4.$form->{year}";
291         last SWITCH;
292       };
293       $form->{duetyp} eq "5" && do {
294         $form->{fromdate} = "1.5.$form->{year}";
295         $form->{todate}   = "31.5.$form->{year}";
296         last SWITCH;
297       };
298       $form->{duetyp} eq "6" && do {
299         $form->{fromdate} = "1.6.$form->{year}";
300         $form->{todate}   = "30.6.$form->{year}";
301         last SWITCH;
302       };
303       $form->{duetyp} eq "7" && do {
304         $form->{fromdate} = "1.7.$form->{year}";
305         $form->{todate}   = "31.7.$form->{year}";
306         last SWITCH;
307       };
308       $form->{duetyp} eq "8" && do {
309         $form->{fromdate} = "1.8.$form->{year}";
310         $form->{todate}   = "31.8.$form->{year}";
311         last SWITCH;
312       };
313       $form->{duetyp} eq "9" && do {
314         $form->{fromdate} = "1.9.$form->{year}";
315         $form->{todate}   = "30.9.$form->{year}";
316         last SWITCH;
317       };
318       $form->{duetyp} eq "10" && do {
319         $form->{fromdate} = "1.10.$form->{year}";
320         $form->{todate}   = "31.10.$form->{year}";
321         last SWITCH;
322       };
323       $form->{duetyp} eq "11" && do {
324         $form->{fromdate} = "1.11.$form->{year}";
325         $form->{todate}   = "30.11.$form->{year}";
326         last SWITCH;
327       };
328       $form->{duetyp} eq "12" && do {
329         $form->{fromdate} = "1.12.$form->{year}";
330         $form->{todate}   = "31.12.$form->{year}";
331         last SWITCH;
332       };
333     }
334     hotfix_reformat_date();
335   } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
336
337   RP->income_statement(\%myconfig, \%$form);
338
339   ($form->{department}) = split /--/, $form->{department};
340
341   $form->{period} =
342     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
343   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
344
345   # if there are any dates construct a where
346   if ($form->{fromdate} || $form->{todate}) {
347
348     unless ($form->{todate}) {
349       $form->{todate} = $form->current_date(\%myconfig);
350     }
351
352     my $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1);
353     my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
354
355     my $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1);
356     my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
357
358     $form->{this_period} = "$shortfromdate\n$shorttodate";
359     $form->{period}      =
360         $locale->text('for Period')
361       . qq|\n$longfromdate |
362       . $locale->text('Bis')
363       . qq| $longtodate|;
364   }
365
366   if ($form->{comparefromdate} || $form->{comparetodate}) {
367     my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
368     my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
369
370     my $longcomparetodate  = $locale->date(\%myconfig, $form->{comparetodate}, 1);
371     my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
372
373     $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
374     $form->{period} .=
375         "\n$longcomparefromdate "
376       . $locale->text('Bis')
377       . qq| $longcomparetodate|;
378   }
379
380   if ( $::instance_conf->get_profit_determination eq 'balance' ) {
381     $form->{title} = $locale->text('Income Statement');
382   } elsif ( $::instance_conf->get_profit_determination eq 'income' ) {
383     $form->{title} = $locale->text('Net Income Statement');
384   } else {
385     $form->{title} = "";
386   };
387
388   if ( $form->{method} eq 'cash' ) {
389     $form->{accounting_method} = $locale->text('Cash accounting');
390   } elsif ( $form->{method} eq 'accrual' ) {
391     $form->{accounting_method} = $locale->text('Accrual accounting');
392   } else {
393     $form->{accounting_method} = "";
394   };
395
396   $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
397
398   $form->header;
399   print $form->parse_html_template('rp/income_statement');
400
401   $main::lxdebug->leave_sub();
402 }
403
404 sub generate_income_statement_ch {
405   $main::lxdebug->enter_sub();
406
407   $main::auth->assert('report');
408
409   my $form     = $main::form;
410   my %myconfig = %main::myconfig;
411   my $locale   = $main::locale;
412
413   my $defaults = SL::DB::Default->get;
414   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
415   $form->{templates} = $defaults->templates;
416
417   $form->{padding} = "&nbsp;&nbsp;";
418   $form->{bold}    = "<b>";
419   $form->{endbold} = "</b>";
420   $form->{br}      = "<br>";
421
422   if ($form->{reporttype} eq "custom") {
423
424     #forgotten the year --> thisyear
425     if ($form->{year} !~ m/^\d\d\d\d$/) {
426       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
427         /(\d\d\d\d)/;
428       $form->{year} = $1;
429     }
430
431     #yearly report
432     if ($form->{duetyp} eq "13") {
433       $form->{fromdate} = "1.1.$form->{year}";
434       $form->{todate}   = "31.12.$form->{year}";
435     }
436    #Quater reports
437     if ($form->{duetyp} eq "A") {
438       $form->{fromdate} = "1.1.$form->{year}";
439       $form->{todate}   = "31.3.$form->{year}";
440     }
441     if ($form->{duetyp} eq "B") {
442       $form->{fromdate} = "1.4.$form->{year}";
443       $form->{todate}   = "30.6.$form->{year}";
444     }
445     if ($form->{duetyp} eq "C") {
446       $form->{fromdate} = "1.7.$form->{year}";
447       $form->{todate}   = "30.9.$form->{year}";
448     }
449     if ($form->{duetyp} eq "D") {
450       $form->{fromdate} = "1.10.$form->{year}";
451       $form->{todate}   = "31.12.$form->{year}";
452     }
453   #Monthly reports
454   SWITCH: {
455       $form->{duetyp} eq "1" && do {
456         $form->{fromdate} = "1.1.$form->{year}";
457         $form->{todate}   = "31.1.$form->{year}";
458         last SWITCH;
459       };
460       $form->{duetyp} eq "2" && do {
461         $form->{fromdate} = "1.2.$form->{year}";
462
463         #this works from 1901 to 2099, 1900 and 2100 fail.
464         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
465         $form->{todate} = "$leap.2.$form->{year}";
466         last SWITCH;
467       };
468       $form->{duetyp} eq "3" && do {
469         $form->{fromdate} = "1.3.$form->{year}";
470         $form->{todate}   = "31.3.$form->{year}";
471         last SWITCH;
472       };
473       $form->{duetyp} eq "4" && do {
474         $form->{fromdate} = "1.4.$form->{year}";
475         $form->{todate}   = "30.4.$form->{year}";
476         last SWITCH;
477       };
478       $form->{duetyp} eq "5" && do {
479         $form->{fromdate} = "1.5.$form->{year}";
480         $form->{todate}   = "31.5.$form->{year}";
481         last SWITCH;
482       };
483       $form->{duetyp} eq "6" && do {
484         $form->{fromdate} = "1.6.$form->{year}";
485         $form->{todate}   = "30.6.$form->{year}";
486         last SWITCH;
487       };
488       $form->{duetyp} eq "7" && do {
489         $form->{fromdate} = "1.7.$form->{year}";
490         $form->{todate}   = "31.7.$form->{year}";
491         last SWITCH;
492       };
493     $form->{duetyp} eq "8" && do {
494         $form->{fromdate} = "1.8.$form->{year}";
495         $form->{todate}   = "31.8.$form->{year}";
496         last SWITCH;
497       };
498       $form->{duetyp} eq "9" && do {
499         $form->{fromdate} = "1.9.$form->{year}";
500         $form->{todate}   = "30.9.$form->{year}";
501         last SWITCH;
502       };
503       $form->{duetyp} eq "10" && do {
504         $form->{fromdate} = "1.10.$form->{year}";
505         $form->{todate}   = "31.10.$form->{year}";
506         last SWITCH;
507       };
508       $form->{duetyp} eq "11" && do {
509         $form->{fromdate} = "1.11.$form->{year}";
510         $form->{todate}   = "30.11.$form->{year}";
511         last SWITCH;
512       };
513       $form->{duetyp} eq "12" && do {
514         $form->{fromdate} = "1.12.$form->{year}";
515         $form->{todate}   = "31.12.$form->{year}";
516         last SWITCH;
517       };
518     }
519     hotfix_reformat_date();
520   } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
521
522    RP->income_statement_ch(\%myconfig, \%$form);
523
524   ($form->{department}) = split /--/, $form->{department};
525
526   $form->{period} =
527     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
528   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
529
530   # if there are any dates construct a where
531   if ($form->{fromdate} || $form->{todate}) {
532
533     unless ($form->{todate}) {
534       $form->{todate} = $form->current_date(\%myconfig);
535     }
536
537     my $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1);
538     my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
539
540     my $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1);
541     my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
542
543     $form->{this_period} = "$shortfromdate\n$shorttodate";
544     $form->{period}      =
545         $locale->text('for Period')
546       . qq|\n$longfromdate |
547       . $locale->text('Bis')
548       . qq| $longtodate|;
549   }
550
551   if ($form->{comparefromdate} || $form->{comparetodate}) {
552     my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
553     my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
554
555     my $longcomparetodate  = $locale->date(\%myconfig, $form->{comparetodate}, 1);
556     my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
557
558     $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
559     $form->{period} .=
560         "\n$longcomparefromdate "
561       . $locale->text('Bis')
562       . qq| $longcomparetodate|;
563   }
564
565    $form->{IN} = "erfolgsrechnung.html";
566
567   $form->parse_template;
568
569   $main::lxdebug->leave_sub();
570 }
571
572
573 sub generate_balance_sheet {
574   $::lxdebug->enter_sub;
575   $::auth->assert('report');
576
577   $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
578   $::form->{padding}       = "&nbsp;&nbsp;";
579   $::form->{bold}          = "<b>";
580   $::form->{endbold}       = "</b>";
581   $::form->{br}            = "<br>";
582
583   my $data = RP->balance_sheet(\%::myconfig, $::form);
584
585   $::form->{asofdate}    ||= $::form->current_date;
586   $::form->{report_title}  = $::locale->text('Balance Sheet');
587   $::form->{report_date} ||= $::form->current_date;
588
589   ($::form->{department}) = split /--/, $::form->{department};
590
591   # define Current Earnings account
592   my $padding = $::form->{l_heading} ? $::form->{padding} : "";
593   push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
594
595   $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
596   $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
597
598 #  balance sheet isn't read from print templates anymore,
599 #  instead use template in rp
600 #  $::form->{IN} = "balance_sheet.html";
601
602   $::form->header;
603   print $::form->parse_html_template('rp/balance_sheet', $data);
604
605   $::lxdebug->leave_sub;
606 }
607
608 sub generate_projects {
609   $main::lxdebug->enter_sub();
610
611   $main::auth->assert('report');
612
613   my $form     = $main::form;
614   my %myconfig = %main::myconfig;
615   my $locale   = $main::locale;
616
617   my $project            = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
618   $form->{projectnumber} = $project ? $project->projectnumber : '';
619
620   $form->{nextsub} = "generate_projects";
621   $form->{title}   = $locale->text('Project Transactions');
622   RP->trial_balance(\%myconfig, \%$form);
623
624   list_accounts('generate_projects');
625
626   $main::lxdebug->leave_sub();
627 }
628
629 # Antonio Gallardo
630 #
631 # D.S. Feb 16, 2001
632 # included links to display transactions for period entered
633 # added headers and subtotals
634 #
635 sub generate_trial_balance {
636   $main::lxdebug->enter_sub();
637
638   $main::auth->assert('report');
639
640   my $form     = $main::form;
641   my %myconfig = %main::myconfig;
642   my $locale   = $main::locale;
643   my $defaults = SL::DB::Default->get;
644
645   if ($form->{reporttype} eq "custom") {
646
647     #forgotten the year --> thisyear
648     if ($form->{year} !~ m/^\d\d\d\d$/) {
649       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
650         /(\d\d\d\d)/;
651       $form->{year} = $1;
652     }
653
654     #yearly report
655     if ($form->{duetyp} eq "13") {
656       $form->{fromdate} = "1.1.$form->{year}";
657       $form->{todate}   = "31.12.$form->{year}";
658     }
659
660     #Quater reports
661     if ($form->{duetyp} eq "A") {
662       $form->{fromdate} = "1.1.$form->{year}";
663       $form->{todate}   = "31.3.$form->{year}";
664     }
665     if ($form->{duetyp} eq "B") {
666       $form->{fromdate} = "1.4.$form->{year}";
667       $form->{todate}   = "30.6.$form->{year}";
668     }
669     if ($form->{duetyp} eq "C") {
670       $form->{fromdate} = "1.7.$form->{year}";
671       $form->{todate}   = "30.9.$form->{year}";
672     }
673     if ($form->{duetyp} eq "D") {
674       $form->{fromdate} = "1.10.$form->{year}";
675       $form->{todate}   = "31.12.$form->{year}";
676     }
677
678     #Monthly reports
679   SWITCH: {
680       $form->{duetyp} eq "1" && do {
681         $form->{fromdate} = "1.1.$form->{year}";
682         $form->{todate}   = "31.1.$form->{year}";
683         last SWITCH;
684       };
685       $form->{duetyp} eq "2" && do {
686         $form->{fromdate} = "1.2.$form->{year}";
687
688         #this works from 1901 to 2099, 1900 and 2100 fail.
689         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
690         $form->{todate} = "$leap.2.$form->{year}";
691         last SWITCH;
692       };
693       $form->{duetyp} eq "3" && do {
694         $form->{fromdate} = "1.3.$form->{year}";
695         $form->{todate}   = "31.3.$form->{year}";
696         last SWITCH;
697       };
698       $form->{duetyp} eq "4" && do {
699         $form->{fromdate} = "1.4.$form->{year}";
700         $form->{todate}   = "30.4.$form->{year}";
701         last SWITCH;
702       };
703       $form->{duetyp} eq "5" && do {
704         $form->{fromdate} = "1.5.$form->{year}";
705         $form->{todate}   = "31.5.$form->{year}";
706         last SWITCH;
707       };
708       $form->{duetyp} eq "6" && do {
709         $form->{fromdate} = "1.6.$form->{year}";
710         $form->{todate}   = "30.6.$form->{year}";
711         last SWITCH;
712       };
713       $form->{duetyp} eq "7" && do {
714         $form->{fromdate} = "1.7.$form->{year}";
715         $form->{todate}   = "31.7.$form->{year}";
716         last SWITCH;
717       };
718       $form->{duetyp} eq "8" && do {
719         $form->{fromdate} = "1.8.$form->{year}";
720         $form->{todate}   = "31.8.$form->{year}";
721         last SWITCH;
722       };
723       $form->{duetyp} eq "9" && do {
724         $form->{fromdate} = "1.9.$form->{year}";
725         $form->{todate}   = "30.9.$form->{year}";
726         last SWITCH;
727       };
728       $form->{duetyp} eq "10" && do {
729         $form->{fromdate} = "1.10.$form->{year}";
730         $form->{todate}   = "31.10.$form->{year}";
731         last SWITCH;
732       };
733       $form->{duetyp} eq "11" && do {
734         $form->{fromdate} = "1.11.$form->{year}";
735         $form->{todate}   = "30.11.$form->{year}";
736         last SWITCH;
737       };
738       $form->{duetyp} eq "12" && do {
739         $form->{fromdate} = "1.12.$form->{year}";
740         $form->{todate}   = "31.12.$form->{year}";
741         last SWITCH;
742       };
743     }
744     hotfix_reformat_date();
745   }
746
747
748   # get for each account initial balance, debits and credits
749   RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
750
751
752   $form->{rowcount} = scalar @{ $form->{TB} || [] };
753   $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
754
755   my @columns = (
756     "accno",               "description",
757     "last_transaction",    "soll_eb",
758     "haben_eb",
759     "soll",                "haben",
760     "soll_kumuliert",      "haben_kumuliert",
761     "soll_saldo",          "haben_saldo"
762   );
763
764
765   my $attachment_basename = $locale->text('trial_balance');
766   my $report              = SL::ReportGenerator->new(\%myconfig, $form);
767
768   my @hidden_variables    = qw(fromdate todate year method);
769
770   my $href                = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
771
772   my %column_defs         = (
773     'accno'               => { 'text' => $locale->text('Account'), },
774     'description'         => { 'text' => $locale->text('Description'), },
775     'last_transaction'    => { 'text' => $locale->text('Last Transaction'), },
776     'soll_eb'             => { 'text' => $locale->text('Debit Starting Balance'), },
777     'haben_eb'            => { 'text' => $locale->text('Credit Starting Balance'), },
778     'soll'                => { 'text' => $locale->text('Debit'), },
779     'haben'               => { 'text' => $locale->text('Credit'), },
780     'soll_kumuliert'      => { 'text' => $locale->text('Sum Debit'), },
781     'haben_kumuliert'     => { 'text' => $locale->text('Sum Credit'), },
782     'soll_saldo'          => { 'text' => $locale->text('Saldo Debit'), },
783     'haben_saldo'         => { 'text' => $locale->text('Saldo Credit'), }
784   );
785
786
787
788   my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
789
790   map { $column_defs{$_}->{visible} =  1 } @columns;
791
792   $report->set_columns(%column_defs);
793   $report->set_column_order(@columns);
794
795   $report->set_export_options('generate_trial_balance', @hidden_variables);
796
797   my @options;
798
799
800   $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
801
802   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
803   push (@options, $form->{print_date});
804
805   $form->{company} = $locale->text('Company') . " " . $defaults->company;
806   push (@options, $form->{company});
807
808   if ($::form->{customer_id}) {
809     my $customer = SL::DB::Manager::Customer->find_by(id => $::form->{customer_id});
810     push @options, $::locale->text('Customer') . ' ' . $customer->displayable_name;
811   }
812
813
814   $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
815
816   my @custom_headers = ([
817     { text => $::locale->text('Account'),          rowspan => 2, },
818     { text => $::locale->text('Description'),      rowspan => 2, },
819     { text => $::locale->text('Last Transaction'), rowspan => 2, },
820     { text => $::locale->text('Starting Balance'), colspan => 2, },
821     { text => $::locale->text('Sum for')   . " $form->{template_fromto}", colspan => 2, },
822     { text => $::locale->text('Sum per')   . " $form->{template_to}",     colspan => 2, },
823     { text => $::locale->text('Saldo per') . " $form->{template_to}",     colspan => 2, },
824   ], [
825     { text => '', },
826     { text => '', },
827     { text => '', },
828     { text => $::locale->text('Assets'), },
829     { text => $::locale->text('Equity'), },
830     { text => $::locale->text('Debit'),  },
831     { text => $::locale->text('Credit'), },
832     { text => $::locale->text('Debit'),  },
833     { text => $::locale->text('Credit'), },
834     { text => $::locale->text('Debit'),  },
835     { text => $::locale->text('Credit'), },
836   ]);
837
838   $report->set_options('output_format'        => 'HTML',
839                        'top_info_text'        => join("\n", @options),
840                        'title'                => $form->{title},
841                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
842                        'html_template'        => 'rp/html_report_susa',
843                        'pdf_template'         => 'rp/html_report_susa',
844     );
845   $report->set_custom_headers(@custom_headers);
846   $report->set_options_from_form();
847   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
848
849   # add sort and escape callback, this one we use for the add sub
850   $form->{callback} = $href .= "&sort=$form->{sort}";
851
852   # escape callback for href
853   my $callback = $form->escape($href);
854
855   my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
856
857   my %totals    = map { $_ => 0 } @subtotal_columns;
858
859   my $edit_url = build_std_url('action=edit', 'type', 'vc');
860
861   my $idx;
862   foreach my $accno (@{ $form->{TB} || [] }) {
863
864     $accno->{soll} = $accno->{debit};
865     $accno->{haben} = $accno->{credit};
866     map { $totals{$_}    += $accno->{$_} } @subtotal_columns;
867
868     map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
869       qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
870
871     my $row = { };
872
873     foreach my $column (@columns) {
874       $row->{$column} = {
875         'data'  => $accno->{$column},
876         'align' => $column_alignment{$column},
877       };
878     }
879
880     $row->{accno}->{link} = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($accno->{accno}), 'description=' . E($accno->{description}), 'fromdate=' . E($form->{fromdate}), 'todate=' . E($form->{todate}), 'method=' . E($form->{method}));
881
882     my $row_set = [ $row ];
883
884
885     $report->add_data($row_set);
886
887     $idx++;
888   }
889
890   $report->add_separator();
891
892   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
893
894   $report->generate_with_headers();
895
896   $main::lxdebug->leave_sub();
897
898 }
899
900 sub create_subtotal_row {
901   $main::lxdebug->enter_sub();
902
903   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
904
905   my $form     = $main::form;
906   my %myconfig = %main::myconfig;
907   my $locale   = $main::locale;
908
909   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
910
911   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
912
913   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
914
915   map { $totals->{$_} = 0 } @{ $subtotal_columns };
916
917   $main::lxdebug->leave_sub();
918
919   return $row;
920 }
921
922 sub create_list_accounts_subtotal_row {
923   $main::lxdebug->enter_sub();
924
925   my ($subtotals, $columns, $fields, $class) = @_;
926
927   my $form     = $main::form;
928   my %myconfig = %main::myconfig;
929   my $locale   = $main::locale;
930
931   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
932
933   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
934
935   $main::lxdebug->leave_sub();
936
937   return $row;
938 }
939
940 sub list_accounts {
941   $main::lxdebug->enter_sub();
942
943   my ($action) = @_;
944
945   my $form     = $main::form;
946   my %myconfig = %main::myconfig;
947   my $locale   = $main::locale;
948
949   my @options;
950   if ($form->{department}) {
951     my ($department) = split /--/, $form->{department};
952     push @options, $locale->text('Department') . " : $department";
953   }
954   if ($form->{projectnumber}) {
955     push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
956   }
957
958   # if there are any dates
959   if ($form->{fromdate} || $form->{todate}) {
960     my ($fromdate, $todate);
961
962     if ($form->{fromdate}) {
963       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
964     }
965     if ($form->{todate}) {
966       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
967     }
968
969     push @options, "$fromdate - $todate";
970
971   } else {
972     push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
973   }
974
975   my @columns     = qw(accno description begbalance debit credit endbalance);
976   my %column_defs = (
977     'accno'       => { 'text' => $locale->text('Account'), },
978     'description' => { 'text' => $locale->text('Description'), },
979     'debit'       => { 'text' => $locale->text('Debit'), },
980     'credit'      => { 'text' => $locale->text('Credit'), },
981     'begbalance'  => { 'text' => $locale->text('Balance'), },
982     'endbalance'  => { 'text' => $locale->text('Balance'), },
983   );
984   my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
985
986   my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
987
988   $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
989
990   my $report = SL::ReportGenerator->new(\%myconfig, $form);
991
992   $report->set_options('top_info_text'         => join("\n", @options),
993                        'output_format'         => 'HTML',
994                        'title'                 => $form->{title},
995                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
996                        'std_column_visibility' => 1,
997     );
998   $report->set_options_from_form();
999   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1000
1001   $report->set_columns(%column_defs);
1002   $report->set_column_order(@columns);
1003
1004   $report->set_export_options($action, @hidden_variables);
1005
1006   my @totals_columns = qw(credit debit begbalance endbalance);
1007   my %subtotals      = map { $_ => 0 } @totals_columns;
1008   my %totals         = map { $_ => 0 } @totals_columns;
1009   my $found_heading  = 0;
1010   my @tb             = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
1011
1012   # sort the whole thing by account numbers and display
1013   foreach my $idx (0 .. scalar(@tb) - 1) {
1014     my $ref  = $tb[$idx];
1015     my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
1016
1017     my $ml   = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
1018
1019     my $row  = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
1020
1021     if ($ref->{charttype} eq 'H') {
1022       next unless ($form->{l_heading});
1023
1024       %subtotals                   = map { $_ => 0 } @totals_columns;
1025       $found_heading               = 1;
1026       $row->{description}->{class} = 'listheading';
1027       $row->{description}->{data}  = $ref->{description};
1028
1029       $report->add_data($row);
1030
1031       next;
1032     }
1033
1034     foreach (qw(debit credit)) {
1035       $subtotals{$_} += $ref->{$_};
1036       $totals{$_}    += $ref->{$_};
1037     }
1038
1039     $subtotals{begbalance} += $ref->{balance} * $ml;
1040     $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
1041
1042     map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
1043     map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
1044
1045     $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
1046     $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
1047
1048     $report->add_data($row);
1049
1050     if ($form->{l_heading} && $found_heading &&
1051         (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
1052       $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
1053     }
1054   }
1055
1056   $report->add_separator();
1057
1058   $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
1059
1060   $report->generate_with_headers();
1061
1062   $main::lxdebug->leave_sub();
1063 }
1064
1065 sub generate_ar_aging {
1066   $main::lxdebug->enter_sub();
1067
1068   $main::auth->assert('general_ledger');
1069
1070   my $form     = $main::form;
1071   my %myconfig = %main::myconfig;
1072   my $locale   = $main::locale;
1073
1074   # split customer
1075   ($form->{customer}) = split(/--/, $form->{customer});
1076
1077   $form->{ct}   = "customer";
1078   $form->{arap} = "ar";
1079
1080   $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
1081
1082   RP->aging(\%myconfig, \%$form);
1083   aging();
1084
1085   $main::lxdebug->leave_sub();
1086 }
1087
1088 sub generate_ap_aging {
1089   $main::lxdebug->enter_sub();
1090
1091   $main::auth->assert('general_ledger');
1092
1093   my $form     = $main::form;
1094   my %myconfig = %main::myconfig;
1095   my $locale   = $main::locale;
1096
1097   # split vendor
1098   ($form->{vendor}) = split(/--/, $form->{vendor});
1099
1100   $form->{ct}   = "vendor";
1101   $form->{arap} = "ap";
1102
1103   $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
1104
1105   RP->aging(\%myconfig, \%$form);
1106   aging();
1107
1108   $main::lxdebug->leave_sub();
1109 }
1110
1111 sub create_aging_subtotal_row {
1112   $main::lxdebug->enter_sub();
1113
1114   my ($subtotals, $columns, $periods, $class) = @_;
1115
1116   my $form     = $main::form;
1117   my %myconfig = %main::myconfig;
1118   my $locale   = $main::locale;
1119
1120   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
1121
1122   foreach (@{ $periods }) {
1123     $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
1124     $subtotals->{$_}      = 0;
1125   }
1126
1127   $main::lxdebug->leave_sub();
1128
1129   return $row;
1130 }
1131
1132 sub aging {
1133   $main::lxdebug->enter_sub();
1134
1135   $main::auth->assert('general_ledger');
1136
1137   my $form     = $main::form;
1138   my %myconfig = %main::myconfig;
1139   my $locale   = $main::locale;
1140   my $cgi      = $::request->{cgi};
1141
1142   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1143
1144   my @columns = qw(statement ct invnumber transdate duedate amount open);
1145
1146   my %column_defs = (
1147     'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
1148     'ct'        => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1149     'invnumber' => { 'text' => $locale->text('Invoice'), },
1150     'transdate' => { 'text' => $locale->text('Date'), },
1151     'duedate'   => { 'text' => $locale->text('Due'), },
1152     'amount'    => { 'text' => $locale->text('Amount'), },
1153     'open'      => { 'text' => $locale->text('Open'), },
1154   );
1155
1156   my %column_alignment = ('statement' => 'center',
1157                           map { $_ => 'right' } qw(open amount));
1158
1159   $report->set_options('std_column_visibility' => 1);
1160   $report->set_columns(%column_defs);
1161   $report->set_column_order(@columns);
1162
1163   my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype);
1164   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
1165
1166   my @options;
1167   my $attachment_basename;
1168
1169   if ($form->{department}) {
1170     my ($department) = split /--/, $form->{department};
1171     push @options, $locale->text('Department') . " : $department";
1172     $form->{callback} .= "&department=" . E($department);
1173   }
1174
1175   if (($form->{arap} eq 'ar') && $form->{customer}) {
1176     push @options, $form->{customer};
1177     $attachment_basename = $locale->text('ar_aging_list');
1178     $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
1179   }
1180
1181   if (($form->{arap} eq 'ap') && $form->{vendor}) {
1182     push @options, $form->{vendor};
1183     $attachment_basename = $locale->text('ap_aging_list');
1184     $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
1185   }
1186
1187   if ($form->{fromdate}) {
1188     push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1189   } else {
1190     push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1191   }
1192
1193   $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1194
1195   $report->set_options('top_info_text'        => join("\n", @options),
1196                        'output_format'        => 'HTML',
1197                        'title'                => $form->{title},
1198                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
1199     );
1200   $report->set_options_from_form();
1201   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1202
1203   my $previous_ctid = 0;
1204   my $row_idx       = 0;
1205   my @periods       = qw(open amount);
1206   my %subtotals     = map { $_ => 0 } @periods;
1207   my %totals        = map { $_ => 0 } @periods;
1208
1209   foreach my $ref (@{ $form->{AG} }) {
1210     if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1211       $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1212     }
1213
1214     foreach my $key (@periods) {
1215       $subtotals{$key}  += $ref->{"$key"};
1216       $totals{$key}     += $ref->{"$key"};
1217       $ref->{"$key"}  = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
1218     }
1219
1220     my $row = { };
1221
1222     foreach my $column (@columns) {
1223       $row->{$column} = {
1224         'data'   => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1225         'align'  => $column_alignment{$column},
1226         'valign' => $column eq 'statement' ? 'center' : '',
1227       };
1228     }
1229
1230     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1231
1232     if ($previous_ctid != $ref->{ctid}) {
1233       $row->{statement}->{raw_data} =
1234           $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
1235         . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1236       $row->{ct}->{data} = $ref->{name};
1237
1238       $row_idx++;
1239     }
1240
1241     $previous_ctid = $ref->{ctid};
1242
1243     $report->add_data($row);
1244   }
1245
1246   $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1247
1248   $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1249
1250   if ($form->{arap} eq 'ar') {
1251     my $raw_top_info_text    = $form->parse_html_template('rp/aging_ar_top');
1252     my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1253                                                                                'PRINT_OPTIONS' => print_options(inline => 1), });
1254     $report->set_options('raw_top_info_text'    => $raw_top_info_text,
1255                          'raw_bottom_info_text' => $raw_bottom_info_text);
1256   }
1257
1258   $report->generate_with_headers();
1259
1260   $main::lxdebug->leave_sub();
1261 }
1262
1263 sub select_all {
1264   $main::lxdebug->enter_sub();
1265
1266   my $form     = $main::form;
1267   my %myconfig = %main::myconfig;
1268   my $locale   = $main::locale;
1269
1270   RP->aging(\%myconfig, \%$form);
1271
1272   map { $_->{checked} = "checked" } @{ $form->{AG} };
1273
1274   &aging;
1275
1276   $main::lxdebug->leave_sub();
1277 }
1278
1279 sub e_mail {
1280   $::lxdebug->enter_sub;
1281   $::auth->assert('general_ledger');
1282
1283   # get name and email addresses
1284   my $selected = 0;
1285   for my $i (1 .. $::form->{rowcount}) {
1286     next unless $::form->{"statement_$i"};
1287     $::form->{"$::form->{ct}_id"} = $::form->{"$::form->{ct}_id_$i"};
1288     RP->get_customer(\%::myconfig, $::form);
1289     $selected = 1;
1290     last;
1291   }
1292
1293   $::form->error($::locale->text('Nothing selected!')) unless $selected;
1294
1295   $::form->{media} = "email";
1296
1297   # save all other variables
1298   my @hidden_values;
1299   for my $key (keys %$::form) {
1300     next if any { $key eq $_ } qw(login password action email cc bcc subject message type sendmode format header);
1301     next unless '' eq ref $::form->{$key};
1302     push @hidden_values, $key;
1303   }
1304
1305   $::form->header;
1306   print $::form->parse_html_template('rp/e_mail', {
1307     show_bcc      => $::auth->assert('email_bcc', 'may fail'),
1308     print_options => print_options(inline => 1),
1309     hidden_values => \@hidden_values,
1310   });
1311
1312   $::lxdebug->leave_sub;
1313 }
1314
1315 sub send_email {
1316   $main::lxdebug->enter_sub();
1317
1318   $main::auth->assert('general_ledger');
1319
1320   my $form     = $main::form;
1321   my %myconfig = %main::myconfig;
1322   my $locale   = $main::locale;
1323
1324   $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1325     unless $form->{subject};
1326
1327   RP->aging(\%myconfig, \%$form);
1328
1329   $form->{"statement_1"} = 1;
1330
1331   $form->{media} = 'email';
1332   print_form();
1333
1334   $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1335
1336   $main::lxdebug->leave_sub();
1337 }
1338
1339 sub print {
1340   $main::lxdebug->enter_sub();
1341
1342   $main::auth->assert('general_ledger');
1343
1344   my $form     = $main::form;
1345   my %myconfig = %main::myconfig;
1346   my $locale   = $main::locale;
1347
1348   if ($form->{media} eq 'printer') {
1349     $form->error($locale->text('Select postscript or PDF!'))
1350       if ($form->{format} !~ /(postscript|pdf)/);
1351   }
1352
1353   my $selected = 0;
1354   for my $i (1 .. $form->{rowcount}) {
1355     if ($form->{"statement_$i"}) {
1356       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1357       $selected = 1;
1358       last;
1359     }
1360   }
1361
1362   $form->error($locale->text('Nothing selected!')) unless $selected;
1363
1364   if ($form->{media} eq 'printer') {
1365     $form->{"$form->{ct}_id"} = "";
1366   } else {
1367     $form->{"statement_1"} = 1;
1368   }
1369
1370   RP->aging(\%myconfig, \%$form);
1371
1372   print_form();
1373
1374   $form->redirect($locale->text('Statements sent to printer!'))
1375     if ($form->{media} eq 'printer');
1376
1377   $main::lxdebug->leave_sub();
1378 }
1379
1380 sub print_form {
1381   $main::lxdebug->enter_sub();
1382
1383   $main::auth->assert('general_ledger');
1384
1385   my $form     = $main::form;
1386   my %myconfig = %main::myconfig;
1387   my $locale   = $main::locale;
1388
1389   my $defaults = SL::DB::Default->get;
1390   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
1391   $form->{templates} = $defaults->templates;
1392
1393   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1394
1395   my $suffix = "html";
1396   my $attachment_suffix = "html";
1397   if ($form->{format} eq 'postscript') {
1398     $form->{postscript} = 1;
1399     $suffix = "tex";
1400     $attachment_suffix = "ps";
1401   } elsif ($form->{format} eq 'pdf') {
1402     $form->{pdf} = 1;
1403     $suffix = "tex";
1404     $attachment_suffix = "pdf";
1405   }
1406
1407   $form->{IN}  = "$form->{type}.$suffix";
1408   $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1409
1410   # Save $form->{email} because it will be overwritten.
1411   $form->{EMAIL_RECIPIENT} = $form->{email};
1412
1413   my $i = 0;
1414   my $ctid;
1415   while (@{ $form->{AG} }) {
1416
1417     my $ref = shift @{ $form->{AG} };
1418
1419     if ($ctid != $ref->{ctid}) {
1420
1421       $ctid = $ref->{ctid};
1422       $i++;
1423
1424       if ($form->{"statement_$i"}) {
1425
1426         my @a =
1427           ("name", "street", "zipcode", "city", "country", "contact", "email",
1428            "$form->{ct}phone", "$form->{ct}fax");
1429         map { $form->{$_} = $ref->{$_} } @a;
1430
1431         $form->{ $form->{ct} } = $form->{name};
1432         $form->{"$form->{ct}_id"} = $ref->{ctid};
1433
1434         map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
1435         $form->{total} = 0;
1436         foreach my $item (qw(c0 c30 c60 c90)) {
1437           $form->{$item} = ();
1438           $form->{"${item}total"} = 0;
1439         }
1440
1441         &statement_details($ref);
1442
1443         while ($ref) {
1444
1445           if (scalar(@{ $form->{AG} }) > 0) {
1446
1447             # one or more left to go
1448             if ($ctid == $form->{AG}->[0]->{ctid}) {
1449               $ref = shift @{ $form->{AG} };
1450               &statement_details($ref);
1451
1452               # any more?
1453               $ref = scalar(@{ $form->{AG} });
1454             } else {
1455               $ref = 0;
1456             }
1457           } else {
1458
1459             # set initial ref to 0
1460             $ref = 0;
1461           }
1462
1463         }
1464
1465         map {
1466           $form->{"${_}total"} =
1467             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
1468         } ('c0', 'c30', 'c60', 'c90', "");
1469
1470         $form->{attachment_filename} =  $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
1471         $form->{attachment_filename} =~ s/\s+/_/g;
1472
1473         $form->parse_template(\%myconfig);
1474
1475       }
1476     }
1477   }
1478   # saving the history
1479   if(!exists $form->{addition} && $form->{id} ne "") {
1480     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1481     $form->{addition} = "PRINTED";
1482     $form->{what_done} = $form->{type};
1483     $form->save_history;
1484   }
1485   # /saving the history
1486   $main::lxdebug->leave_sub();
1487 }
1488
1489 sub statement_details {
1490   $main::lxdebug->enter_sub();
1491
1492   $main::auth->assert('general_ledger');
1493
1494   my $form     = $main::form;
1495   my %myconfig = %main::myconfig;
1496   my $locale   = $main::locale;
1497
1498   my ($ref) = @_;
1499
1500   push @{ $form->{invnumber} }, $ref->{invnumber};
1501   push @{ $form->{invdate} },   $ref->{transdate};
1502   push @{ $form->{duedate} },   $ref->{duedate};
1503   push @{ $form->{amount} },    $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
1504   push @{ $form->{open} },      $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
1505
1506   foreach my $item (qw(c0 c30 c60 c90)) {
1507     if ($ref->{exchangerate} * 1) {
1508       # add only the open amount of the invoice to the aging, not the total amount
1509       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
1510       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
1511       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
1512       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
1513     }
1514     $form->{"${item}total"} += $ref->{$item};
1515     $form->{total}          += $ref->{$item};
1516     push @{ $form->{$item} },
1517       $form->format_amount(\%myconfig, $ref->{$item}, 2);
1518   }
1519
1520   $main::lxdebug->leave_sub();
1521 }
1522
1523 sub generate_tax_report {
1524   $::lxdebug->enter_sub;
1525   $::auth->assert('report');
1526
1527   RP->tax_report(\%::myconfig, $::form);
1528
1529   my $descvar     = "$::form->{accno}_description";
1530   my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
1531
1532   # construct href
1533   my $href     =
1534   my $callback = build_std_url('action=generate_tax_report', $descvar,
1535     qw(fromdate todate db method accno department report title));
1536
1537   my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1538   my @column_index;
1539
1540   for my $item (@columns, 'subtotal') {
1541     if ($::form->{"l_$item"} eq "Y") {
1542       $callback .= "&l_$item=Y";
1543       $href     .= "&l_$item=Y";
1544     }
1545   }
1546
1547   for my $item (@columns) {
1548     if ($::form->{"l_$item"} eq "Y") {
1549       push @column_index, $item;
1550     }
1551   }
1552
1553   my @options;
1554   if ($::form->{department}) {
1555     my ($department) = split /--/, $::form->{department};
1556     push @options, $::locale->text('Department') . " : $department";
1557   }
1558
1559   # if there are any dates
1560   if ($::form->{fromdate} || $::form->{todate}) {
1561     my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
1562     my $todate   = $::form->{todate}   ? $::locale->date(\%::myconfig, $::form->{todate}, 1)   : '';
1563     push @options, "$fromdate - $todate";
1564   } else {
1565     push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
1566   }
1567
1568   my ($name, $invoice, $arap);
1569   if ($::form->{db} eq 'ar') {
1570     $name    = $::locale->text('Customer');
1571     $invoice = 'is.pl';
1572     $arap    = 'ar.pl';
1573   }
1574   if ($::form->{db} eq 'ap') {
1575     $name    = $::locale->text('Vendor');
1576     $invoice = 'ir.pl';
1577     $arap    = 'ap.pl';
1578   }
1579
1580   my %column_header = (
1581     id        => $::locale->text('ID'),
1582     invnumber => $::locale->text('Invoice'),
1583     transdate => $::locale->text('Date'),
1584     netamount => $::locale->text('Amount'),
1585     tax       => $::locale->text('Tax'),
1586     amount    => $::locale->text('Total'),
1587     name      => $name,
1588   );
1589
1590   my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
1591
1592   $callback .= "&sort=$::form->{sort}";
1593
1594   my $sameitem;
1595   if (@{ $::form->{TR} }) {
1596     $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
1597   }
1598
1599   my ($totalnetamount, $totaltax, @data);
1600   for my $ref (@{ $::form->{TR} }) {
1601
1602     my $module = ($ref->{invoice}) ? $invoice : $arap;
1603
1604     if ($::form->{l_subtotal} eq 'Y') {
1605       if ($sameitem ne $ref->{ $::form->{sort} }) {
1606         push @data, {
1607           subtotal  => 1,
1608           netamount => $subtotalnetamount,
1609           tax       => $subtotaltax,
1610           amount    => $subtotal,
1611         };
1612         $subtotalnetamount = 0;
1613         $subtotaltax       = 0;
1614         $sameitem          = $ref->{ $::form->{sort} };
1615       }
1616     }
1617
1618     $subtotalnetamount += $ref->{netamount};
1619     $subtotaltax       += $ref->{tax};
1620     $totalnetamount    += $ref->{netamount};
1621     $totaltax          += $ref->{tax};
1622     $ref->{amount}      = $ref->{netamount} + $ref->{tax};
1623
1624     push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
1625     $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
1626     $data[-1]{$_}{numeric}     = 1 for qw(netamount tax amount);
1627   }
1628
1629   if ($::form->{l_subtotal} eq 'Y') {
1630     push @data, {
1631       subtotal  => 1,
1632       netamount => $subtotalnetamount,
1633       tax       => $subtotaltax,
1634       amount    => $subtotal,
1635     };
1636   }
1637
1638   push @data, {
1639     total     => 1,
1640     netamount => $totalnetamount,
1641     tax       => $totaltax,
1642     amount    => $totalnetamount + $totaltax,
1643   };
1644
1645   $::form->header;
1646   print $::form->parse_html_template('rp/tax_report', {
1647     column_index  => \@column_index,
1648     column_header => \%column_header,
1649     column_sorted => \%column_sorted,
1650     sort_base     => $href,
1651     DATA          => \@data,
1652     options       => \@options,
1653   });
1654
1655   $::lxdebug->leave_sub;
1656 }
1657
1658 sub list_payments {
1659   $main::lxdebug->enter_sub();
1660
1661   $main::auth->assert('cash');
1662
1663   my $form     = $main::form;
1664   my %myconfig = %main::myconfig;
1665   my $locale   = $main::locale;
1666
1667   if ($form->{account}) {
1668     ($form->{paymentaccounts}) = split /--/, $form->{account};
1669   }
1670
1671   my $option;
1672   if ($form->{department}) {
1673     (my $department, $form->{department_id}) = split /--/, $form->{department};
1674     $option = $locale->text('Department') . " : $department";
1675   }
1676
1677   report_generator_set_default_sort('transdate', 1);
1678
1679   RP->payments(\%myconfig, \%$form);
1680
1681   my @hidden_variables = qw(account title department reference source memo fromdate todate
1682                             fx_transaction db prepayment paymentaccounts sort);
1683
1684   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
1685   $form->{callback} = $href;
1686
1687   my @columns     = qw(transdate invnumber name paid source memo);
1688   my %column_defs = (
1689     'name'      => { 'text' => $locale->text('Description'), },
1690     'invnumber' => { 'text' => $locale->text('Reference'), },
1691     'transdate' => { 'text' => $locale->text('Date'), },
1692     'paid'      => { 'text' => $locale->text('Amount'), },
1693     'source'    => { 'text' => $locale->text('Source'), },
1694     'memo'      => { 'text' => $locale->text('Memo'), },
1695   );
1696   my %column_alignment = ('paid' => 'right');
1697
1698   foreach my $name (grep { $_ ne 'paid' } @columns) {
1699     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1700     $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
1701   }
1702
1703   my @options;
1704   if ($form->{fromdate}) {
1705     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
1706   }
1707   if ($form->{todate}) {
1708     push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1709   }
1710
1711   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1712
1713   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
1714
1715   $report->set_options('top_info_text'         => join("\n", @options),
1716                        'output_format'         => 'HTML',
1717                        'title'                 => $form->{title},
1718                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
1719                        'std_column_visibility' => 1,
1720     );
1721   $report->set_options_from_form();
1722
1723   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1724
1725   $report->set_columns(%column_defs);
1726   $report->set_column_order(@columns);
1727
1728   $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
1729
1730   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1731
1732   my $total_paid    = 0;
1733
1734   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
1735     next unless @{ $form->{ $ref->{id} } };
1736
1737     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
1738
1739     my $subtotal_paid = 0;
1740
1741     foreach my $payment (@{ $form->{ $ref->{id} } }) {
1742       my $module = $payment->{module};
1743       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
1744       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
1745
1746       $subtotal_paid += $payment->{paid};
1747       $total_paid    += $payment->{paid};
1748
1749       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
1750
1751       my $row = { };
1752
1753       foreach my $column (@columns) {
1754         $row->{$column} = {
1755           'data'  => $payment->{$column},
1756           'align' => $column_alignment{$column},
1757         };
1758       }
1759
1760       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
1761
1762       $report->add_data($row);
1763     }
1764
1765     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
1766     $row->{paid} = {
1767       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
1768       'align' => 'right',
1769       'class' => 'listsubtotal',
1770     };
1771
1772     $report->add_data($row);
1773   }
1774
1775   $report->add_separator();
1776
1777   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
1778   $row->{paid} = {
1779     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
1780     'align' => 'right',
1781     'class' => 'listtotal',
1782   };
1783
1784   $report->add_data($row);
1785
1786   $report->generate_with_headers();
1787
1788   $main::lxdebug->leave_sub();
1789 }
1790
1791 sub print_options {
1792   $::lxdebug->enter_sub;
1793
1794   my ($dont_print) = @_;
1795
1796   $::form->{sendmode} = "attachment";
1797   $::form->{format} ||= $::myconfig{template_format} || "pdf";
1798   $::form->{copies} ||= $::myconfig{copies}          || 2;
1799
1800   $::form->{PD}{ $::form->{type} }     = "selected";
1801   $::form->{DF}{ $::form->{format} }   = "selected";
1802   $::form->{OP}{ $::form->{media} }    = "selected";
1803   $::form->{SM}{ $::form->{sendmode} } = "selected";
1804
1805   my $output = $::form->parse_html_template('rp/print_options', {
1806     got_printer => $::myconfig{printer},
1807     show_latex  => $::lx_office_conf{print_templates}->{latex},
1808     is_email    => $::form->{media} eq 'email',
1809   });
1810
1811   print $output unless $dont_print;
1812
1813   $::lxdebug->leave_sub;
1814
1815   return $output;
1816 }
1817
1818 sub generate_bwa {
1819   $main::lxdebug->enter_sub();
1820
1821   $main::auth->assert('report');
1822
1823   my $form     = $main::form;
1824   my %myconfig = %main::myconfig;
1825   my $locale   = $main::locale;
1826
1827   $form->{padding} = "&nbsp;&nbsp;";
1828   $form->{bold}    = "<b>";
1829   $form->{endbold} = "</b>";
1830   $form->{br}      = "<br>";
1831
1832   if ($form->{reporttype} eq "custom") {
1833
1834     #forgotten the year --> thisyear
1835     if ($form->{year} !~ m/^\d\d\d\d$/) {
1836       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1837         /(\d\d\d\d)/;
1838       $form->{year} = $1;
1839     }
1840
1841     #yearly report
1842     if ($form->{duetyp} eq "13") {
1843       $form->{fromdate}        = "1.1.$form->{year}";
1844       $form->{todate}          = "31.12.$form->{year}";
1845       $form->{comparefromdate} = "1.01.$form->{year}";
1846       $form->{comparetodate}   = "31.12.$form->{year}";
1847     }
1848
1849     #Quater reports
1850     if ($form->{duetyp} eq "A") {
1851       $form->{fromdate}        = "1.1.$form->{year}";
1852       $form->{todate}          = "31.3.$form->{year}";
1853       $form->{comparefromdate} = "1.01.$form->{year}";
1854       $form->{comparetodate}   = "31.03.$form->{year}";
1855     }
1856     if ($form->{duetyp} eq "B") {
1857       $form->{fromdate}        = "1.4.$form->{year}";
1858       $form->{todate}          = "30.6.$form->{year}";
1859       $form->{comparefromdate} = "1.01.$form->{year}";
1860       $form->{comparetodate}   = "30.06.$form->{year}";
1861     }
1862     if ($form->{duetyp} eq "C") {
1863       $form->{fromdate}        = "1.7.$form->{year}";
1864       $form->{todate}          = "30.9.$form->{year}";
1865       $form->{comparefromdate} = "1.01.$form->{year}";
1866       $form->{comparetodate}   = "30.09.$form->{year}";
1867     }
1868     if ($form->{duetyp} eq "D") {
1869       $form->{fromdate}        = "1.10.$form->{year}";
1870       $form->{todate}          = "31.12.$form->{year}";
1871       $form->{comparefromdate} = "1.01.$form->{year}";
1872       $form->{comparetodate}   = "31.12.$form->{year}";
1873     }
1874
1875     #Monthly reports
1876   SWITCH: {
1877       $form->{duetyp} eq "1" && do {
1878         $form->{fromdate}        = "1.1.$form->{year}";
1879         $form->{todate}          = "31.1.$form->{year}";
1880         $form->{comparefromdate} = "1.01.$form->{year}";
1881         $form->{comparetodate}   = "31.01.$form->{year}";
1882         last SWITCH;
1883       };
1884       $form->{duetyp} eq "2" && do {
1885         $form->{fromdate} = "1.2.$form->{year}";
1886
1887         #this works from 1901 to 2099, 1900 and 2100 fail.
1888         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1889         $form->{todate}          = "$leap.2.$form->{year}";
1890         $form->{comparefromdate} = "1.01.$form->{year}";
1891         $form->{comparetodate}   = "$leap.02.$form->{year}";
1892         last SWITCH;
1893       };
1894       $form->{duetyp} eq "3" && do {
1895         $form->{fromdate}        = "1.3.$form->{year}";
1896         $form->{todate}          = "31.3.$form->{year}";
1897         $form->{comparefromdate} = "1.01.$form->{year}";
1898         $form->{comparetodate}   = "31.03.$form->{year}";
1899         last SWITCH;
1900       };
1901       $form->{duetyp} eq "4" && do {
1902         $form->{fromdate}        = "1.4.$form->{year}";
1903         $form->{todate}          = "30.4.$form->{year}";
1904         $form->{comparefromdate} = "1.01.$form->{year}";
1905         $form->{comparetodate}   = "30.04.$form->{year}";
1906         last SWITCH;
1907       };
1908       $form->{duetyp} eq "5" && do {
1909         $form->{fromdate}        = "1.5.$form->{year}";
1910         $form->{todate}          = "31.5.$form->{year}";
1911         $form->{comparefromdate} = "1.01.$form->{year}";
1912         $form->{comparetodate}   = "31.05.$form->{year}";
1913         last SWITCH;
1914       };
1915       $form->{duetyp} eq "6" && do {
1916         $form->{fromdate}        = "1.6.$form->{year}";
1917         $form->{todate}          = "30.6.$form->{year}";
1918         $form->{comparefromdate} = "1.01.$form->{year}";
1919         $form->{comparetodate}   = "30.06.$form->{year}";
1920         last SWITCH;
1921       };
1922       $form->{duetyp} eq "7" && do {
1923         $form->{fromdate}        = "1.7.$form->{year}";
1924         $form->{todate}          = "31.7.$form->{year}";
1925         $form->{comparefromdate} = "1.01.$form->{year}";
1926         $form->{comparetodate}   = "31.07.$form->{year}";
1927         last SWITCH;
1928       };
1929       $form->{duetyp} eq "8" && do {
1930         $form->{fromdate}        = "1.8.$form->{year}";
1931         $form->{todate}          = "31.8.$form->{year}";
1932         $form->{comparefromdate} = "1.01.$form->{year}";
1933         $form->{comparetodate}   = "31.08.$form->{year}";
1934         last SWITCH;
1935       };
1936       $form->{duetyp} eq "9" && do {
1937         $form->{fromdate}        = "1.9.$form->{year}";
1938         $form->{todate}          = "30.9.$form->{year}";
1939         $form->{comparefromdate} = "1.01.$form->{year}";
1940         $form->{comparetodate}   = "30.09.$form->{year}";
1941         last SWITCH;
1942       };
1943       $form->{duetyp} eq "10" && do {
1944         $form->{fromdate}        = "1.10.$form->{year}";
1945         $form->{todate}          = "31.10.$form->{year}";
1946         $form->{comparefromdate} = "1.01.$form->{year}";
1947         $form->{comparetodate}   = "31.10.$form->{year}";
1948         last SWITCH;
1949       };
1950       $form->{duetyp} eq "11" && do {
1951         $form->{fromdate}        = "1.11.$form->{year}";
1952         $form->{todate}          = "30.11.$form->{year}";
1953         $form->{comparefromdate} = "1.01.$form->{year}";
1954         $form->{comparetodate}   = "30.11.$form->{year}";
1955         last SWITCH;
1956       };
1957       $form->{duetyp} eq "12" && do {
1958         $form->{fromdate}        = "1.12.$form->{year}";
1959         $form->{todate}          = "31.12.$form->{year}";
1960         $form->{comparefromdate} = "1.01.$form->{year}";
1961         $form->{comparetodate}   = "31.12.$form->{year}";
1962         last SWITCH;
1963       };
1964     }
1965     hotfix_reformat_date();
1966   } else {
1967     # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
1968     # ansonsten ist die prüfung in RP.pm
1969     # if (defined ($form->{fromdate|todate}=='..'))
1970     # immer wahr
1971     if ($form->{fromdate}){
1972       my $datetime = $locale->parse_date_to_object($form->{fromdate});
1973       $datetime->set( month      => 1,
1974                       day        => 1);
1975       $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
1976     }
1977     if ($form->{todate}){
1978       $form->{comparetodate}   = $form->{todate};
1979     }
1980   }
1981
1982   RP->bwa(\%myconfig, \%$form);
1983
1984   ($form->{department}) = split /--/, $form->{department};
1985
1986   $form->{period} =
1987     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1988   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1989
1990   # if there are any dates construct a where
1991   if ($form->{fromdate} || $form->{todate}) {
1992
1993     unless ($form->{todate}) {
1994       $form->{todate} = $form->current_date(\%myconfig);
1995     }
1996
1997     my %germandate = ("dateformat" => "dd.mm.yyyy");
1998
1999     my $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
2000     my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
2001
2002     my $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
2003     my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
2004
2005     $form->{this_period} = "$shortfromdate\n$shorttodate";
2006     $form->{period}      =
2007         $locale->text('for Period')
2008       . qq|\n$longfromdate |
2009       . $locale->text('bis')
2010       . qq| $longtodate|;
2011   }
2012
2013   $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
2014
2015   if ( $form->{method} eq 'cash' ) {
2016     $form->{accounting_method} = $locale->text('Cash accounting');
2017   } elsif ( $form->{method} eq 'accrual' ) {
2018     $form->{accounting_method} = $locale->text('Accrual accounting');
2019   } else {
2020     $form->{accounting_method} = "";
2021   };
2022
2023   $form->{title} = $locale->text('BWA');
2024
2025   $::request->layout->add_stylesheets('bwa.css');
2026   $form->header;
2027   print $form->parse_html_template('rp/bwa');
2028
2029   $main::lxdebug->leave_sub();
2030 }
2031 ###
2032 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
2033 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
2034 sub hotfix_reformat_date {
2035
2036   $main::lxdebug->enter_sub();
2037
2038   my $form     = $main::form;
2039   my %myconfig = %main::myconfig;
2040   my $locale   = $main::locale;
2041
2042   if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
2043     my $current_dateformat = $myconfig{dateformat};
2044     $myconfig{dateformat} = 'dd.mm.yyyy';
2045     $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
2046     $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
2047     $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
2048       unless (!defined ($form->{comparefromdate}));
2049     $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
2050       unless (!defined ($form->{comparetodate}));
2051
2052     # Und wieder zurücksetzen
2053     $myconfig{dateformat} =  $current_dateformat; #'dd.mm.yyyy';
2054   } # Ende Hotifx Bug 1388
2055
2056   $main::lxdebug->leave_sub();
2057
2058 }
2059 1;