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