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