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