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