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