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