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