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