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