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