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