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