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