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