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