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