Nachtrag zu rp.pl::report
[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   $main::lxdebug->enter_sub();
1353
1354   $main::auth->assert('report');
1355
1356   my $form     = $main::form;
1357   my %myconfig = %main::myconfig;
1358   my $locale   = $main::locale;
1359
1360   RP->tax_report(\%myconfig, \%$form);
1361
1362   my $descvar     = "$form->{accno}_description";
1363   my $description = $form->escape($form->{$descvar});
1364   my $ratevar     = "$form->{accno}_rate";
1365   my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
1366
1367   my $department = $form->escape($form->{department});
1368
1369   # construct href
1370   my $href =
1371     "$form->{script}?&action=generate_tax_report&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&accno=$form->{accno}&$descvar=$description&department=$department&report=$form->{report}";
1372
1373   # construct callback
1374   $description = $form->escape($form->{$descvar},   1);
1375   $department  = $form->escape($form->{department}, 1);
1376   my $callback    =
1377     "$form->{script}?&action=generate_tax_report&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&accno=$form->{accno}&$descvar=$description&department=$department&report=$form->{report}";
1378
1379   my $title = $form->escape($form->{title});
1380   $href .= "&title=$title";
1381   $title = $form->escape($form->{title}, 1);
1382   $callback .= "&title=$title";
1383
1384   $form->{title} = qq|$form->{title} $form->{"$form->{accno}_description"} |;
1385
1386   my @columns =
1387     $form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1388   my @column_index;
1389
1390   foreach my $item (@columns) {
1391     if ($form->{"l_$item"} eq "Y") {
1392       push @column_index, $item;
1393
1394       # add column to href and callback
1395       $callback .= "&l_$item=Y";
1396       $href     .= "&l_$item=Y";
1397     }
1398   }
1399
1400   if ($form->{l_subtotal} eq 'Y') {
1401     $callback .= "&l_subtotal=Y";
1402     $href     .= "&l_subtotal=Y";
1403   }
1404
1405   my $option;
1406   if ($form->{department}) {
1407     ($department) = split /--/, $form->{department};
1408     $option = $locale->text('Department') . " : $department";
1409   }
1410
1411   my ($fromdate, $todate);
1412   # if there are any dates
1413   if ($form->{fromdate} || $form->{todate}) {
1414     if ($form->{fromdate}) {
1415       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
1416     }
1417     if ($form->{todate}) {
1418       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
1419     }
1420
1421     $form->{period} = "$fromdate - $todate";
1422   } else {
1423     $form->{period} =
1424       $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1425   }
1426
1427   my ($name, $invoice, $arap);
1428   if ($form->{db} eq 'ar') {
1429     $name    = $locale->text('Customer');
1430     $invoice = 'is.pl';
1431     $arap    = 'ar.pl';
1432   }
1433   if ($form->{db} eq 'ap') {
1434     $name    = $locale->text('Vendor');
1435     $invoice = 'ir.pl';
1436     $arap    = 'ap.pl';
1437   }
1438
1439   $option .= "<br>" if $option;
1440   $option .= "$form->{period}";
1441
1442   my %column_header;
1443   $column_header{id}        = qq|<th><a class=listheading href=$href&sort=id>| . $locale->text('ID') . qq|</th>|;
1444   $column_header{invnumber} = qq|<th><a class=listheading href=$href&sort=invnumber>| . $locale->text('Invoice') . qq|</th>|;
1445   $column_header{transdate} = qq|<th><a class=listheading href=$href&sort=transdate>| . $locale->text('Date') . qq|</th>|;
1446   $column_header{netamount} = qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
1447   $column_header{tax}       = qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
1448   $column_header{amount}    = qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
1449
1450   $column_header{name}      = qq|<th><a class=listheading href=$href&sort=name>$name</th>|;
1451
1452   $form->header;
1453
1454   print qq|
1455 <body>
1456
1457 <table width=100%>
1458   <tr>
1459     <th class=listtop>$form->{title}</th>
1460   </tr>
1461   <tr height="5"></tr>
1462   <tr>
1463     <td>$option</td>
1464   </tr>
1465   <tr>
1466     <td>
1467       <table width=100%>
1468         <tr class=listheading>
1469 |;
1470
1471   map { print "$column_header{$_}\n" } @column_index;
1472
1473   print qq|
1474         </tr>
1475 |;
1476
1477   # add sort and escape callback
1478   $callback = $form->escape($callback . "&sort=$form->{sort}");
1479
1480   my $sameitem;
1481   if (@{ $form->{TR} }) {
1482     $sameitem = $form->{TR}->[0]->{ $form->{sort} };
1483   }
1484
1485   my ($totalnetamount, $totaltax);
1486   my ($i);
1487   foreach my $ref (@{ $form->{TR} }) {
1488
1489     my $module = ($ref->{invoice}) ? $invoice : $arap;
1490
1491     if ($form->{l_subtotal} eq 'Y') {
1492       if ($sameitem ne $ref->{ $form->{sort} }) {
1493         tax_subtotal(\@column_index, \$subtotalnetamount, \$subtotaltax, \$subtotal);
1494         $sameitem = $ref->{ $form->{sort} };
1495       }
1496     }
1497
1498     $totalnetamount += $ref->{netamount};
1499     $totaltax       += $ref->{tax};
1500     $ref->{amount} = $ref->{netamount} + $ref->{tax};
1501
1502     $subtotalnetamount += $ref->{netamount};
1503     $subtotaltax       += $ref->{tax};
1504
1505     map {
1506       $ref->{$_} = $form->format_amount(\%myconfig, $ref->{$_}, 2, "&nbsp;");
1507     } qw(netamount tax amount);
1508
1509     my %column_data;
1510     $column_data{id}        = qq|<td>$ref->{id}</td>|;
1511     $column_data{invnumber} =
1512       qq|<td><a href=$module?action=edit&id=$ref->{id}&callback=$callback>$ref->{invnumber}</a></td>|;
1513     $column_data{transdate} = qq|<td>$ref->{transdate}</td>|;
1514     $column_data{name}      = qq|<td>$ref->{name}&nbsp;</td>|;
1515
1516     map { $column_data{$_} = qq|<td align=right>$ref->{$_}</td>| }
1517       qw(netamount tax amount);
1518
1519     $i++;
1520     $i %= 2;
1521     print qq|
1522         <tr class=listrow$i>
1523 |;
1524
1525     map { print "$column_data{$_}\n" } @column_index;
1526
1527     print qq|
1528         </tr>
1529 |;
1530
1531   }
1532
1533   if ($form->{l_subtotal} eq 'Y') {
1534     tax_subtotal(\@column_index, \$subtotalnetamount, \$subtotaltax, \$subtotal);
1535   }
1536
1537   my %column_data;
1538   map { $column_data{$_} = qq|<th>&nbsp;</th>| } @column_index;
1539
1540   print qq|
1541         </tr>
1542         <tr class=listtotal>
1543 |;
1544
1545   my $total          = $form->format_amount(\%myconfig, $totalnetamount + $totaltax, 2, "&nbsp;");
1546   $totalnetamount = $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;");
1547   $totaltax       = $form->format_amount(\%myconfig, $totaltax, 2, "&nbsp;");
1548
1549   $column_data{netamount} = qq|<th class=listtotal align=right>$totalnetamount</th>|;
1550   $column_data{tax}    = qq|<th class=listtotal align=right>$totaltax</th>|;
1551   $column_data{amount} = qq|<th class=listtotal align=right>$total</th>|;
1552
1553   map { print "$column_data{$_}\n" } @column_index;
1554
1555   print qq|
1556         </tr>
1557       </table>
1558     </td>
1559   </tr>
1560   <tr>
1561     <td><hr size=3 noshade></td>
1562   </tr>
1563 </table>
1564
1565 </body>
1566 </html>
1567 |;
1568
1569   $main::lxdebug->leave_sub();
1570 }
1571
1572 sub tax_subtotal {
1573   $main::lxdebug->enter_sub();
1574
1575   my ($column_index, $subtotalnetamount, $subtotaltax, $subtotal) = @_;
1576
1577   my $form     = $main::form;
1578   my %myconfig = %main::myconfig;
1579   my $locale   = $main::locale;
1580
1581   my %column_data;
1582   map { $column_data{$_} = "<td>&nbsp;</td>" } @{ $column_index };
1583
1584   $$subtotalnetamount = $form->format_amount(\%myconfig, $$subtotalnetamount, 2, "&nbsp;");
1585   $$subtotaltax       = $form->format_amount(\%myconfig, $$subtotaltax, 2, "&nbsp;");
1586   $$subtotal          = $form->format_amount(\%myconfig, $$subtotalnetamount + $$subtotaltax, 2, "&nbsp;");
1587
1588   $column_data{netamount} = "<th class=listsubtotal align=right>$$subtotalnetamount</th>";
1589   $column_data{tax}       = "<th class=listsubtotal align=right>$$subtotaltax</th>";
1590   $column_data{amount}    = "<th class=listsubtotal align=right>$$subtotal</th>";
1591
1592   $$subtotalnetamount = 0;
1593   $$subtotaltax       = 0;
1594
1595   print qq|
1596         <tr class=listsubtotal>
1597 |;
1598   map { print "\n$column_data{$_}" } @{ $column_index };
1599
1600   print qq|
1601         </tr>
1602 |;
1603
1604   $main::lxdebug->leave_sub();
1605 }
1606
1607 sub list_payments {
1608   $main::lxdebug->enter_sub();
1609
1610   $main::auth->assert('cash');
1611
1612   my $form     = $main::form;
1613   my %myconfig = %main::myconfig;
1614   my $locale   = $main::locale;
1615
1616   if ($form->{account}) {
1617     ($form->{paymentaccounts}) = split /--/, $form->{account};
1618   }
1619
1620   my $option;
1621   if ($form->{department}) {
1622     (my $department, $form->{department_id}) = split /--/, $form->{department};
1623     $option = $locale->text('Department') . " : $department";
1624   }
1625
1626   report_generator_set_default_sort('transdate', 1);
1627
1628   RP->payments(\%myconfig, \%$form);
1629
1630   my @hidden_variables = qw(account title department reference source memo fromdate todate
1631                             fx_transaction db prepayment paymentaccounts sort);
1632
1633   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
1634   $form->{callback} = $href;
1635
1636   my @columns     = qw(transdate invnumber name paid source memo);
1637   my %column_defs = (
1638     'name'      => { 'text' => $locale->text('Description'), },
1639     'invnumber' => { 'text' => $locale->text('Reference'), },
1640     'transdate' => { 'text' => $locale->text('Date'), },
1641     'paid'      => { 'text' => $locale->text('Amount'), },
1642     'source'    => { 'text' => $locale->text('Source'), },
1643     'memo'      => { 'text' => $locale->text('Memo'), },
1644   );
1645   my %column_alignment = ('paid' => 'right');
1646
1647   foreach my $name (grep { $_ ne 'paid' } @columns) {
1648     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1649     $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
1650   }
1651
1652   my @options;
1653   if ($form->{fromdate}) {
1654     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
1655   }
1656   if ($form->{todate}) {
1657     push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1658   }
1659
1660   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1661
1662   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
1663
1664   $report->set_options('top_info_text'         => join("\n", @options),
1665                        'output_format'         => 'HTML',
1666                        'title'                 => $form->{title},
1667                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
1668                        'std_column_visibility' => 1,
1669     );
1670   $report->set_options_from_form();
1671
1672   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1673
1674   $report->set_columns(%column_defs);
1675   $report->set_column_order(@columns);
1676
1677   $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
1678
1679   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1680
1681   my $total_paid    = 0;
1682
1683   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
1684     next unless @{ $form->{ $ref->{id} } };
1685
1686     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
1687
1688     my $subtotal_paid = 0;
1689
1690     foreach my $payment (@{ $form->{ $ref->{id} } }) {
1691       my $module = $payment->{module};
1692       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
1693       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
1694
1695       $subtotal_paid += $payment->{paid};
1696       $total_paid    += $payment->{paid};
1697
1698       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
1699
1700       my $row = { };
1701
1702       foreach my $column (@columns) {
1703         $row->{$column} = {
1704           'data'  => $payment->{$column},
1705           'align' => $column_alignment{$column},
1706         };
1707       }
1708
1709       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
1710
1711       $report->add_data($row);
1712     }
1713
1714     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
1715     $row->{paid} = {
1716       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
1717       'align' => 'right',
1718       'class' => 'listsubtotal',
1719     };
1720
1721     $report->add_data($row);
1722   }
1723
1724   $report->add_separator();
1725
1726   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
1727   $row->{paid} = {
1728     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
1729     'align' => 'right',
1730     'class' => 'listtotal',
1731   };
1732
1733   $report->add_data($row);
1734
1735   $report->generate_with_headers();
1736
1737   $main::lxdebug->leave_sub();
1738 }
1739
1740 sub print_options {
1741   $main::lxdebug->enter_sub();
1742
1743   my ($dont_print) = @_;
1744
1745   my $form     = $main::form;
1746   my %myconfig = %main::myconfig;
1747   my $locale   = $main::locale;
1748
1749   $form->{sendmode} = "attachment";
1750
1751   $form->{"format"} =
1752     $form->{"format"} ? $form->{"format"} :
1753     $myconfig{"template_format"} ? $myconfig{"template_format"} :
1754     "pdf";
1755
1756   $form->{"copies"} =
1757     $form->{"copies"} ? $form->{"copies"} :
1758     $myconfig{"copies"} ? $myconfig{"copies"} :
1759     2;
1760
1761   $form->{PD}{ $form->{type} }     = "selected";
1762   $form->{DF}{ $form->{format} }   = "selected";
1763   $form->{OP}{ $form->{media} }    = "selected";
1764   $form->{SM}{ $form->{sendmode} } = "selected";
1765
1766   my ($media);
1767   my $type = qq|
1768             <option value=statement $form->{PD}{statement}>| . $locale->text('Statement');
1769
1770   if ($form->{media} eq 'email') {
1771     $media = qq|
1772             <option value=attachment $form->{SM}{attachment}>| . $locale->text('Attachment') . qq|
1773             <option value=inline $form->{SM}{inline}>| . $locale->text('In-line');
1774   } else {
1775     $media = qq|
1776             <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
1777     if ($myconfig{printer} && $::lx_office_conf{print_templates}->{latex}) {
1778       $media .= qq|
1779             <option value=printer $form->{OP}{printer}>| . $locale->text('Printer');
1780     }
1781   }
1782
1783   my $format;
1784   if ($::lx_office_conf{print_templates}->{latex}) {
1785     $format .= qq|
1786             <option value=html $form->{DF}{html}>| . $locale->text('HTML')
1787       . qq| <option value=pdf $form->{DF}{pdf}>| . $locale->text('PDF')
1788       . qq| <option value=postscript $form->{DF}{postscript}>| . $locale->text('Postscript');
1789   }
1790
1791   my $output = qq|
1792 <table>
1793   <tr>
1794     <td><select name=type>$type</select></td>
1795     <td><select name=format>$format</select></td>
1796     <td><select name=media>$media</select></td>
1797 |;
1798
1799   if ($myconfig{printer} && $::lx_office_conf{print_templates}->{latex} && $form->{media} ne 'email') {
1800     $output .= qq|
1801       <td>| . $locale->text('Copies') . qq|
1802       <input name=copies size=2 value=$form->{copies}></td>
1803 |;
1804   }
1805
1806   $output .= qq|
1807   </tr>
1808 </table>
1809 |;
1810
1811   print $output unless $dont_print;
1812
1813   $main::lxdebug->leave_sub();
1814
1815   return $output;
1816 }
1817
1818 sub generate_bwa {
1819   $main::lxdebug->enter_sub();
1820
1821   $main::auth->assert('report');
1822
1823   my $form     = $main::form;
1824   my %myconfig = %main::myconfig;
1825   my $locale   = $main::locale;
1826
1827   $form->{padding} = "&nbsp;&nbsp;";
1828   $form->{bold}    = "<b>";
1829   $form->{endbold} = "</b>";
1830   $form->{br}      = "<br>";
1831
1832   if ($form->{reporttype} eq "custom") {
1833
1834     #forgotten the year --> thisyear
1835     if ($form->{year} !~ m/^\d\d\d\d$/) {
1836       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1837         /(\d\d\d\d)/;
1838       $form->{year} = $1;
1839     }
1840
1841     #yearly report
1842     if ($form->{duetyp} eq "13") {
1843       $form->{fromdate}        = "1.1.$form->{year}";
1844       $form->{todate}          = "31.12.$form->{year}";
1845       $form->{comparefromdate} = "1.01.$form->{year}";
1846       $form->{comparetodate}   = "31.12.$form->{year}";
1847     }
1848
1849     #Quater reports
1850     if ($form->{duetyp} eq "A") {
1851       $form->{fromdate}        = "1.1.$form->{year}";
1852       $form->{todate}          = "31.3.$form->{year}";
1853       $form->{comparefromdate} = "1.01.$form->{year}";
1854       $form->{comparetodate}   = "31.03.$form->{year}";
1855     }
1856     if ($form->{duetyp} eq "B") {
1857       $form->{fromdate}        = "1.4.$form->{year}";
1858       $form->{todate}          = "30.6.$form->{year}";
1859       $form->{comparefromdate} = "1.01.$form->{year}";
1860       $form->{comparetodate}   = "30.06.$form->{year}";
1861     }
1862     if ($form->{duetyp} eq "C") {
1863       $form->{fromdate}        = "1.7.$form->{year}";
1864       $form->{todate}          = "30.9.$form->{year}";
1865       $form->{comparefromdate} = "1.01.$form->{year}";
1866       $form->{comparetodate}   = "30.09.$form->{year}";
1867     }
1868     if ($form->{duetyp} eq "D") {
1869       $form->{fromdate}        = "1.10.$form->{year}";
1870       $form->{todate}          = "31.12.$form->{year}";
1871       $form->{comparefromdate} = "1.01.$form->{year}";
1872       $form->{comparetodate}   = "31.12.$form->{year}";
1873     }
1874
1875     #Monthly reports
1876   SWITCH: {
1877       $form->{duetyp} eq "1" && do {
1878         $form->{fromdate}        = "1.1.$form->{year}";
1879         $form->{todate}          = "31.1.$form->{year}";
1880         $form->{comparefromdate} = "1.01.$form->{year}";
1881         $form->{comparetodate}   = "31.01.$form->{year}";
1882         last SWITCH;
1883       };
1884       $form->{duetyp} eq "2" && do {
1885         $form->{fromdate} = "1.2.$form->{year}";
1886
1887         #this works from 1901 to 2099, 1900 and 2100 fail.
1888         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1889         $form->{todate}          = "$leap.2.$form->{year}";
1890         $form->{comparefromdate} = "1.01.$form->{year}";
1891         $form->{comparetodate}   = "$leap.02.$form->{year}";
1892         last SWITCH;
1893       };
1894       $form->{duetyp} eq "3" && do {
1895         $form->{fromdate}        = "1.3.$form->{year}";
1896         $form->{todate}          = "31.3.$form->{year}";
1897         $form->{comparefromdate} = "1.01.$form->{year}";
1898         $form->{comparetodate}   = "31.03.$form->{year}";
1899         last SWITCH;
1900       };
1901       $form->{duetyp} eq "4" && do {
1902         $form->{fromdate}        = "1.4.$form->{year}";
1903         $form->{todate}          = "30.4.$form->{year}";
1904         $form->{comparefromdate} = "1.01.$form->{year}";
1905         $form->{comparetodate}   = "30.04.$form->{year}";
1906         last SWITCH;
1907       };
1908       $form->{duetyp} eq "5" && do {
1909         $form->{fromdate}        = "1.5.$form->{year}";
1910         $form->{todate}          = "31.5.$form->{year}";
1911         $form->{comparefromdate} = "1.01.$form->{year}";
1912         $form->{comparetodate}   = "31.05.$form->{year}";
1913         last SWITCH;
1914       };
1915       $form->{duetyp} eq "6" && do {
1916         $form->{fromdate}        = "1.6.$form->{year}";
1917         $form->{todate}          = "30.6.$form->{year}";
1918         $form->{comparefromdate} = "1.01.$form->{year}";
1919         $form->{comparetodate}   = "30.06.$form->{year}";
1920         last SWITCH;
1921       };
1922       $form->{duetyp} eq "7" && do {
1923         $form->{fromdate}        = "1.7.$form->{year}";
1924         $form->{todate}          = "31.7.$form->{year}";
1925         $form->{comparefromdate} = "1.01.$form->{year}";
1926         $form->{comparetodate}   = "31.07.$form->{year}";
1927         last SWITCH;
1928       };
1929       $form->{duetyp} eq "8" && do {
1930         $form->{fromdate}        = "1.8.$form->{year}";
1931         $form->{todate}          = "31.8.$form->{year}";
1932         $form->{comparefromdate} = "1.01.$form->{year}";
1933         $form->{comparetodate}   = "31.08.$form->{year}";
1934         last SWITCH;
1935       };
1936       $form->{duetyp} eq "9" && do {
1937         $form->{fromdate}        = "1.9.$form->{year}";
1938         $form->{todate}          = "30.9.$form->{year}";
1939         $form->{comparefromdate} = "1.01.$form->{year}";
1940         $form->{comparetodate}   = "30.09.$form->{year}";
1941         last SWITCH;
1942       };
1943       $form->{duetyp} eq "10" && do {
1944         $form->{fromdate}        = "1.10.$form->{year}";
1945         $form->{todate}          = "31.10.$form->{year}";
1946         $form->{comparefromdate} = "1.01.$form->{year}";
1947         $form->{comparetodate}   = "31.10.$form->{year}";
1948         last SWITCH;
1949       };
1950       $form->{duetyp} eq "11" && do {
1951         $form->{fromdate}        = "1.11.$form->{year}";
1952         $form->{todate}          = "30.11.$form->{year}";
1953         $form->{comparefromdate} = "1.01.$form->{year}";
1954         $form->{comparetodate}   = "30.11.$form->{year}";
1955         last SWITCH;
1956       };
1957       $form->{duetyp} eq "12" && do {
1958         $form->{fromdate}        = "1.12.$form->{year}";
1959         $form->{todate}          = "31.12.$form->{year}";
1960         $form->{comparefromdate} = "1.01.$form->{year}";
1961         $form->{comparetodate}   = "31.12.$form->{year}";
1962         last SWITCH;
1963       };
1964     }
1965     hotfix_reformat_date();
1966   } else {
1967     # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
1968     # ansonsten ist die prüfung in RP.pm
1969     # if (defined ($form->{fromdate|todate}=='..'))
1970     # immer wahr
1971     if ($form->{fromdate}){
1972       my ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate});
1973       my $datetime = $locale->parse_date_to_object(\%myconfig, $form->{fromdate});
1974       $datetime->set( month      => 1,
1975                       day        => 1);
1976       $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
1977     }
1978     if ($form->{todate}){
1979       $form->{comparetodate}   = $form->{todate};
1980     }
1981   }
1982
1983   RP->bwa(\%myconfig, \%$form);
1984
1985   ($form->{department}) = split /--/, $form->{department};
1986
1987   $form->{period} =
1988     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1989   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1990
1991   # if there are any dates construct a where
1992   if ($form->{fromdate} || $form->{todate}) {
1993
1994     unless ($form->{todate}) {
1995       $form->{todate} = $form->current_date(\%myconfig);
1996     }
1997
1998     my %germandate = ("dateformat" => "dd.mm.yyyy");
1999
2000     my $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
2001     my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
2002
2003     my $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
2004     my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
2005
2006     $form->{this_period} = "$shortfromdate\n$shorttodate";
2007     $form->{period}      =
2008         $locale->text('for Period')
2009       . qq|\n$longfromdate |
2010       . $locale->text('bis')
2011       . qq| $longtodate|;
2012   }
2013
2014   # setup variables for the form
2015   my @a = qw(company address businessnumber);
2016   map { $form->{$_} = $myconfig{$_} } @a;
2017   $form->{templates} = $myconfig{templates};
2018
2019   $form->{IN} = "bwa.html";
2020
2021   $form->parse_template;
2022
2023   $main::lxdebug->leave_sub();
2024 }
2025 ###
2026 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
2027 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
2028 sub hotfix_reformat_date {
2029
2030   $main::lxdebug->enter_sub();
2031
2032   my $form     = $main::form;
2033   my %myconfig = %main::myconfig;
2034   my $locale   = $main::locale;
2035
2036   if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
2037     my $current_dateformat = $myconfig{dateformat};
2038     $myconfig{dateformat} = 'dd.mm.yyyy';
2039     $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
2040     $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
2041     $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
2042       unless (!defined ($form->{comparefromdate}));
2043     $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
2044       unless (!defined ($form->{comparetodate}));
2045
2046     # Und wieder zurücksetzen
2047     $myconfig{dateformat} =  $current_dateformat; #'dd.mm.yyyy';
2048   } # Ende Hotifx Bug 1388
2049
2050   $main::lxdebug->leave_sub();
2051
2052 }
2053 1;