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