test action
[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);
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);
1010
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   );
1020
1021   my %column_alignment = ('statement' => 'center',
1022                           map { $_ => 'right' } qw(open amount));
1023
1024   $report->set_options('std_column_visibility' => 1);
1025   $report->set_columns(%column_defs);
1026   $report->set_column_order(@columns);
1027   my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype department fromdate);
1028   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
1029
1030   my @options;
1031   my $attachment_basename;
1032
1033   if ($form->{department}) {
1034     my ($department) = split /--/, $form->{department};
1035     push @options, $locale->text('Department') . " : $department";
1036     $form->{callback} .= "&department=" . E($department);
1037   }
1038
1039   if (($form->{arap} eq 'ar') && $form->{customer}) {
1040     push @options, $form->{customer};
1041     $attachment_basename = $locale->text('ar_aging_list');
1042     $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
1043   }
1044
1045   if (($form->{arap} eq 'ap') && $form->{vendor}) {
1046     push @options, $form->{vendor};
1047     $attachment_basename = $locale->text('ap_aging_list');
1048     $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
1049   }
1050
1051   if ($form->{fromdate}) {
1052     push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1053   } else {
1054     push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1055   }
1056
1057   $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1058
1059   $report->set_options('top_info_text'        => join("\n", @options),
1060                        'output_format'        => 'HTML',
1061                        'title'                => $form->{title},
1062                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
1063     );
1064   $report->set_options_from_form();
1065   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1066
1067   my $previous_ctid = 0;
1068   my $row_idx       = 0;
1069   my @periods       = qw(open amount);
1070   my %subtotals     = map { $_ => 0 } @periods;
1071   my %totals        = map { $_ => 0 } @periods;
1072
1073   foreach my $ref (@{ $form->{AG} }) {
1074     if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1075       $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1076     }
1077
1078     foreach my $key (@periods) {
1079       $subtotals{$key}  += $ref->{"$key"};
1080       $totals{$key}     += $ref->{"$key"};
1081       $ref->{"$key"}  = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
1082     }
1083
1084     my $row = { };
1085
1086     foreach my $column (@columns) {
1087       $row->{$column} = {
1088         'data'   => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1089         'align'  => $column_alignment{$column},
1090         'valign' => $column eq 'statement' ? 'center' : '',
1091       };
1092     }
1093
1094     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1095
1096     if ($previous_ctid != $ref->{ctid}) {
1097       $row->{statement}->{raw_data} =
1098           $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
1099         . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1100       $row->{ct}->{data} = $ref->{name};
1101
1102       $row_idx++;
1103     }
1104
1105     $previous_ctid = $ref->{ctid};
1106
1107     $report->add_data($row);
1108   }
1109
1110   $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1111
1112   $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1113
1114   if ($form->{arap} eq 'ar') {
1115     my $raw_top_info_text    = $form->parse_html_template('rp/aging_ar_top');
1116     my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1117                                                                                'PRINT_OPTIONS' => print_options(inline => 1), });
1118     $report->set_options('raw_top_info_text'    => $raw_top_info_text,
1119                          'raw_bottom_info_text' => $raw_bottom_info_text);
1120   }
1121
1122   setup_rp_aging_action_bar(arap => $form->{arap});
1123   $report->generate_with_headers();
1124
1125   $main::lxdebug->leave_sub();
1126 }
1127
1128 sub send_email {
1129   $main::lxdebug->enter_sub();
1130
1131   $main::auth->assert('general_ledger');
1132
1133   my $form     = $main::form;
1134   my %myconfig = %main::myconfig;
1135   my $locale   = $main::locale;
1136
1137   $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1138     unless $form->{subject};
1139
1140   RP->aging(\%myconfig, \%$form);
1141
1142
1143   my $email_form  = delete $form->{email_form};
1144   my %field_names = (to => 'email');
1145
1146   $form->{ $field_names{$_} // $_ } = $email_form->{$_} for keys %{ $email_form };
1147   $form->{media} = 'email';
1148   print_form();
1149
1150   $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1151
1152   $main::lxdebug->leave_sub();
1153 }
1154
1155 sub print {
1156   $main::lxdebug->enter_sub();
1157
1158   $main::auth->assert('general_ledger');
1159
1160   my $form     = $main::form;
1161   my %myconfig = %main::myconfig;
1162   my $locale   = $main::locale;
1163
1164   if ($form->{media} eq 'printer') {
1165     $form->error($locale->text('Select postscript or PDF!'))
1166       if ($form->{format} !~ /(postscript|pdf)/);
1167   }
1168
1169   my $selected = 0;
1170   for my $i (1 .. $form->{rowcount}) {
1171     if ($form->{"statement_$i"}) {
1172       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1173       $selected = 1;
1174       last;
1175     }
1176   }
1177
1178   $form->error($locale->text('Nothing selected!')) unless $selected;
1179
1180   if ($form->{media} eq 'printer') {
1181     $form->{"$form->{ct}_id"} = "";
1182   } else {
1183     $form->{"statement_1"} = 1;
1184   }
1185
1186   RP->aging(\%myconfig, \%$form);
1187
1188   print_form();
1189
1190   $form->redirect($locale->text('Statements sent to printer!'))
1191     if ($form->{media} eq 'printer');
1192
1193   $main::lxdebug->leave_sub();
1194 }
1195
1196 sub print_form {
1197   $main::lxdebug->enter_sub();
1198
1199   $main::auth->assert('general_ledger');
1200
1201   my $form     = $main::form;
1202   my %myconfig = %main::myconfig;
1203   my $locale   = $main::locale;
1204
1205   my $defaults = SL::DB::Default->get;
1206   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
1207   $form->{templates} = $defaults->templates;
1208
1209   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1210
1211   my $suffix = "html";
1212   my $attachment_suffix = "html";
1213   if ($form->{format} eq 'postscript') {
1214     $form->{postscript} = 1;
1215     $suffix = "tex";
1216     $attachment_suffix = "ps";
1217   } elsif ($form->{format} eq 'pdf') {
1218     $form->{pdf} = 1;
1219     $suffix = "tex";
1220     $attachment_suffix = "pdf";
1221   }
1222
1223   $form->{IN}  = "$form->{type}.$suffix";
1224   $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1225
1226   # Save $form->{email} because it will be overwritten.
1227   $form->{EMAIL_RECIPIENT} = $form->{email};
1228
1229   my $i = 0;
1230   my $ctid;
1231   while (@{ $form->{AG} }) {
1232
1233     my $ref = shift @{ $form->{AG} };
1234
1235     if ($ctid != $ref->{ctid}) {
1236
1237       $ctid = $ref->{ctid};
1238       $i++;
1239
1240       if ($form->{"statement_$i"}) {
1241
1242         my @a =
1243           ("name", "street", "zipcode", "city", "country", "contact", "email",
1244            "$form->{ct}phone", "$form->{ct}fax");
1245         map { $form->{$_} = $ref->{$_} } @a;
1246
1247         $form->{ $form->{ct} } = $form->{name};
1248         $form->{"$form->{ct}_id"} = $ref->{ctid};
1249
1250         map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
1251         $form->{total} = 0;
1252         foreach my $item (qw(c0 c30 c60 c90)) {
1253           $form->{$item} = ();
1254           $form->{"${item}total"} = 0;
1255         }
1256
1257         &statement_details($ref);
1258
1259         while ($ref) {
1260
1261           if (scalar(@{ $form->{AG} }) > 0) {
1262
1263             # one or more left to go
1264             if ($ctid == $form->{AG}->[0]->{ctid}) {
1265               $ref = shift @{ $form->{AG} };
1266               &statement_details($ref);
1267
1268               # any more?
1269               $ref = scalar(@{ $form->{AG} });
1270             } else {
1271               $ref = 0;
1272             }
1273           } else {
1274
1275             # set initial ref to 0
1276             $ref = 0;
1277           }
1278
1279         }
1280
1281         map {
1282           $form->{"${_}total"} =
1283             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
1284         } ('c0', 'c30', 'c60', 'c90', "");
1285
1286         $form->{attachment_filename} =  $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
1287         $form->{attachment_filename} =~ s/\s+/_/g;
1288
1289         $form->parse_template(\%myconfig);
1290
1291       }
1292     }
1293   }
1294   # saving the history
1295   if(!exists $form->{addition} && $form->{id} ne "") {
1296     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1297     $form->{addition} = "PRINTED";
1298     $form->{what_done} = $form->{type};
1299     $form->save_history;
1300   }
1301   # /saving the history
1302   $main::lxdebug->leave_sub();
1303 }
1304
1305 sub statement_details {
1306   $main::lxdebug->enter_sub();
1307
1308   $main::auth->assert('general_ledger');
1309
1310   my $form     = $main::form;
1311   my %myconfig = %main::myconfig;
1312   my $locale   = $main::locale;
1313
1314   my ($ref) = @_;
1315
1316   push @{ $form->{invnumber} }, $ref->{invnumber};
1317   push @{ $form->{invdate} },   $ref->{transdate};
1318   push @{ $form->{duedate} },   $ref->{duedate};
1319   push @{ $form->{amount} },    $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
1320   push @{ $form->{open} },      $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
1321
1322   foreach my $item (qw(c0 c30 c60 c90)) {
1323     if ($ref->{exchangerate} * 1) {
1324       # add only the open amount of the invoice to the aging, not the total amount
1325       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
1326       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
1327       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
1328       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
1329     }
1330     $form->{"${item}total"} += $ref->{$item};
1331     $form->{total}          += $ref->{$item};
1332     push @{ $form->{$item} },
1333       $form->format_amount(\%myconfig, $ref->{$item}, 2);
1334   }
1335
1336   $main::lxdebug->leave_sub();
1337 }
1338
1339 sub generate_tax_report {
1340   $::lxdebug->enter_sub;
1341   $::auth->assert('report');
1342
1343   RP->tax_report(\%::myconfig, $::form);
1344
1345   my $descvar     = "$::form->{accno}_description";
1346   my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
1347
1348   # construct href
1349   my $href     =
1350   my $callback = build_std_url('action=generate_tax_report', $descvar,
1351     qw(fromdate todate db method accno department report title));
1352
1353   my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1354   my @column_index;
1355
1356   for my $item (@columns, 'subtotal') {
1357     if ($::form->{"l_$item"} eq "Y") {
1358       $callback .= "&l_$item=Y";
1359       $href     .= "&l_$item=Y";
1360     }
1361   }
1362
1363   for my $item (@columns) {
1364     if ($::form->{"l_$item"} eq "Y") {
1365       push @column_index, $item;
1366     }
1367   }
1368
1369   my @options;
1370   if ($::form->{department}) {
1371     my ($department) = split /--/, $::form->{department};
1372     push @options, $::locale->text('Department') . " : $department";
1373   }
1374
1375   # if there are any dates
1376   if ($::form->{fromdate} || $::form->{todate}) {
1377     my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
1378     my $todate   = $::form->{todate}   ? $::locale->date(\%::myconfig, $::form->{todate}, 1)   : '';
1379     push @options, "$fromdate - $todate";
1380   } else {
1381     push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
1382   }
1383
1384   my ($name, $invoice, $arap);
1385   if ($::form->{db} eq 'ar') {
1386     $name    = $::locale->text('Customer');
1387     $invoice = 'is.pl';
1388     $arap    = 'ar.pl';
1389   }
1390   if ($::form->{db} eq 'ap') {
1391     $name    = $::locale->text('Vendor');
1392     $invoice = 'ir.pl';
1393     $arap    = 'ap.pl';
1394   }
1395
1396   my %column_header = (
1397     id        => $::locale->text('ID'),
1398     invnumber => $::locale->text('Invoice'),
1399     transdate => $::locale->text('Date'),
1400     netamount => $::locale->text('Amount'),
1401     tax       => $::locale->text('Tax'),
1402     amount    => $::locale->text('Total'),
1403     name      => $name,
1404   );
1405
1406   my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
1407
1408   $callback .= "&sort=$::form->{sort}";
1409
1410   my $sameitem;
1411   if (@{ $::form->{TR} }) {
1412     $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
1413   }
1414
1415   my ($totalnetamount, $totaltax, @data);
1416   for my $ref (@{ $::form->{TR} }) {
1417
1418     my $module = ($ref->{invoice}) ? $invoice : $arap;
1419
1420     if ($::form->{l_subtotal} eq 'Y') {
1421       if ($sameitem ne $ref->{ $::form->{sort} }) {
1422         push @data, {
1423           subtotal  => 1,
1424           netamount => $subtotalnetamount,
1425           tax       => $subtotaltax,
1426           amount    => $subtotal,
1427         };
1428         $subtotalnetamount = 0;
1429         $subtotaltax       = 0;
1430         $sameitem          = $ref->{ $::form->{sort} };
1431       }
1432     }
1433
1434     $subtotalnetamount += $ref->{netamount};
1435     $subtotaltax       += $ref->{tax};
1436     $totalnetamount    += $ref->{netamount};
1437     $totaltax          += $ref->{tax};
1438     $ref->{amount}      = $ref->{netamount} + $ref->{tax};
1439
1440     push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
1441     $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
1442     $data[-1]{$_}{numeric}     = 1 for qw(netamount tax amount);
1443   }
1444
1445   if ($::form->{l_subtotal} eq 'Y') {
1446     push @data, {
1447       subtotal  => 1,
1448       netamount => $subtotalnetamount,
1449       tax       => $subtotaltax,
1450       amount    => $subtotal,
1451     };
1452   }
1453
1454   push @data, {
1455     total     => 1,
1456     netamount => $totalnetamount,
1457     tax       => $totaltax,
1458     amount    => $totalnetamount + $totaltax,
1459   };
1460
1461   $::form->header;
1462   print $::form->parse_html_template('rp/tax_report', {
1463     column_index  => \@column_index,
1464     column_header => \%column_header,
1465     column_sorted => \%column_sorted,
1466     sort_base     => $href,
1467     DATA          => \@data,
1468     options       => \@options,
1469   });
1470
1471   $::lxdebug->leave_sub;
1472 }
1473
1474 sub list_payments {
1475   $main::lxdebug->enter_sub();
1476
1477   $main::auth->assert('cash');
1478
1479   my $form     = $main::form;
1480   my %myconfig = %main::myconfig;
1481   my $locale   = $main::locale;
1482
1483   if ($form->{account}) {
1484     ($form->{paymentaccounts}) = split /--/, $form->{account};
1485   }
1486
1487   my $option;
1488   if ($form->{department}) {
1489     (my $department, $form->{department_id}) = split /--/, $form->{department};
1490     $option = $locale->text('Department') . " : $department";
1491   }
1492
1493   report_generator_set_default_sort('transdate', 1);
1494
1495   RP->payments(\%myconfig, \%$form);
1496
1497   my @hidden_variables = qw(account title department reference source memo fromdate todate
1498                             fx_transaction db prepayment paymentaccounts sort);
1499
1500   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
1501   $form->{callback} = $href;
1502
1503   my @columns     = qw(transdate invnumber name paid source memo);
1504   my %column_defs = (
1505     'name'      => { 'text' => $locale->text('Description'), },
1506     'invnumber' => { 'text' => $locale->text('Reference'), },
1507     'transdate' => { 'text' => $locale->text('Date'), },
1508     'paid'      => { 'text' => $locale->text('Amount'), },
1509     'source'    => { 'text' => $locale->text('Source'), },
1510     'memo'      => { 'text' => $locale->text('Memo'), },
1511   );
1512   my %column_alignment = ('paid' => 'right');
1513
1514   foreach my $name (grep { $_ ne 'paid' } @columns) {
1515     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1516     $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
1517   }
1518
1519   my @options;
1520   if ($form->{fromdate}) {
1521     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
1522   }
1523   if ($form->{todate}) {
1524     push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1525   }
1526
1527   my $report = SL::ReportGenerator->new(\%myconfig, $form);
1528
1529   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
1530
1531   $report->set_options('top_info_text'         => join("\n", @options),
1532                        'output_format'         => 'HTML',
1533                        'title'                 => $form->{title},
1534                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
1535                        'std_column_visibility' => 1,
1536     );
1537   $report->set_options_from_form();
1538
1539   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1540
1541   $report->set_columns(%column_defs);
1542   $report->set_column_order(@columns);
1543
1544   $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
1545
1546   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1547
1548   my $total_paid    = 0;
1549
1550   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
1551     next unless @{ $form->{ $ref->{id} } };
1552
1553     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
1554
1555     my $subtotal_paid = 0;
1556
1557     foreach my $payment (@{ $form->{ $ref->{id} } }) {
1558       my $module = $payment->{module};
1559       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
1560       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
1561
1562       $subtotal_paid += $payment->{paid};
1563       $total_paid    += $payment->{paid};
1564
1565       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
1566
1567       my $row = { };
1568
1569       foreach my $column (@columns) {
1570         $row->{$column} = {
1571           'data'  => $payment->{$column},
1572           'align' => $column_alignment{$column},
1573         };
1574       }
1575
1576       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
1577
1578       $report->add_data($row);
1579     }
1580
1581     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
1582     $row->{paid} = {
1583       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
1584       'align' => 'right',
1585       'class' => 'listsubtotal',
1586     };
1587
1588     $report->add_data($row);
1589   }
1590
1591   $report->add_separator();
1592
1593   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
1594   $row->{paid} = {
1595     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
1596     'align' => 'right',
1597     'class' => 'listtotal',
1598   };
1599
1600   $report->add_data($row);
1601
1602   $report->generate_with_headers();
1603
1604   $main::lxdebug->leave_sub();
1605 }
1606
1607 sub print_options {
1608   $::lxdebug->enter_sub;
1609
1610   my ($dont_print) = @_;
1611
1612   $::form->{sendmode} = "attachment";
1613   $::form->{format} ||= $::myconfig{template_format} || "pdf";
1614   $::form->{copies} ||= $::myconfig{copies}          || 2;
1615
1616   $::form->{PD}{ $::form->{type} }     = "selected";
1617   $::form->{DF}{ $::form->{format} }   = "selected";
1618   $::form->{OP}{ $::form->{media} }    = "selected";
1619   $::form->{SM}{ $::form->{sendmode} } = "selected";
1620
1621   my $output = $::form->parse_html_template('rp/print_options', {
1622     is_email    => $::form->{media} eq 'email',
1623   });
1624
1625   print $output unless $dont_print;
1626
1627   $::lxdebug->leave_sub;
1628
1629   return $output;
1630 }
1631
1632 sub generate_bwa {
1633   $main::lxdebug->enter_sub();
1634
1635   $main::auth->assert('report');
1636
1637   my $form     = $main::form;
1638   my %myconfig = %main::myconfig;
1639   my $locale   = $main::locale;
1640
1641   $form->{padding} = "&nbsp;&nbsp;";
1642   $form->{bold}    = "<b>";
1643   $form->{endbold} = "</b>";
1644   $form->{br}      = "<br>";
1645
1646   if ($form->{reporttype} eq "custom") {
1647
1648     #forgotten the year --> thisyear
1649     if ($form->{year} !~ m/^\d\d\d\d$/) {
1650       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1651         /(\d\d\d\d)/;
1652       $form->{year} = $1;
1653     }
1654
1655     #yearly report
1656     if ($form->{duetyp} eq "13") {
1657       $form->{fromdate}        = "1.1.$form->{year}";
1658       $form->{todate}          = "31.12.$form->{year}";
1659       $form->{comparefromdate} = "1.01.$form->{year}";
1660       $form->{comparetodate}   = "31.12.$form->{year}";
1661     }
1662
1663     #Quater reports
1664     if ($form->{duetyp} eq "A") {
1665       $form->{fromdate}        = "1.1.$form->{year}";
1666       $form->{todate}          = "31.3.$form->{year}";
1667       $form->{comparefromdate} = "1.01.$form->{year}";
1668       $form->{comparetodate}   = "31.03.$form->{year}";
1669     }
1670     if ($form->{duetyp} eq "B") {
1671       $form->{fromdate}        = "1.4.$form->{year}";
1672       $form->{todate}          = "30.6.$form->{year}";
1673       $form->{comparefromdate} = "1.01.$form->{year}";
1674       $form->{comparetodate}   = "30.06.$form->{year}";
1675     }
1676     if ($form->{duetyp} eq "C") {
1677       $form->{fromdate}        = "1.7.$form->{year}";
1678       $form->{todate}          = "30.9.$form->{year}";
1679       $form->{comparefromdate} = "1.01.$form->{year}";
1680       $form->{comparetodate}   = "30.09.$form->{year}";
1681     }
1682     if ($form->{duetyp} eq "D") {
1683       $form->{fromdate}        = "1.10.$form->{year}";
1684       $form->{todate}          = "31.12.$form->{year}";
1685       $form->{comparefromdate} = "1.01.$form->{year}";
1686       $form->{comparetodate}   = "31.12.$form->{year}";
1687     }
1688
1689     #Monthly reports
1690   SWITCH: {
1691       $form->{duetyp} eq "1" && do {
1692         $form->{fromdate}        = "1.1.$form->{year}";
1693         $form->{todate}          = "31.1.$form->{year}";
1694         $form->{comparefromdate} = "1.01.$form->{year}";
1695         $form->{comparetodate}   = "31.01.$form->{year}";
1696         last SWITCH;
1697       };
1698       $form->{duetyp} eq "2" && do {
1699         $form->{fromdate} = "1.2.$form->{year}";
1700
1701         #this works from 1901 to 2099, 1900 and 2100 fail.
1702         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1703         $form->{todate}          = "$leap.2.$form->{year}";
1704         $form->{comparefromdate} = "1.01.$form->{year}";
1705         $form->{comparetodate}   = "$leap.02.$form->{year}";
1706         last SWITCH;
1707       };
1708       $form->{duetyp} eq "3" && do {
1709         $form->{fromdate}        = "1.3.$form->{year}";
1710         $form->{todate}          = "31.3.$form->{year}";
1711         $form->{comparefromdate} = "1.01.$form->{year}";
1712         $form->{comparetodate}   = "31.03.$form->{year}";
1713         last SWITCH;
1714       };
1715       $form->{duetyp} eq "4" && do {
1716         $form->{fromdate}        = "1.4.$form->{year}";
1717         $form->{todate}          = "30.4.$form->{year}";
1718         $form->{comparefromdate} = "1.01.$form->{year}";
1719         $form->{comparetodate}   = "30.04.$form->{year}";
1720         last SWITCH;
1721       };
1722       $form->{duetyp} eq "5" && do {
1723         $form->{fromdate}        = "1.5.$form->{year}";
1724         $form->{todate}          = "31.5.$form->{year}";
1725         $form->{comparefromdate} = "1.01.$form->{year}";
1726         $form->{comparetodate}   = "31.05.$form->{year}";
1727         last SWITCH;
1728       };
1729       $form->{duetyp} eq "6" && do {
1730         $form->{fromdate}        = "1.6.$form->{year}";
1731         $form->{todate}          = "30.6.$form->{year}";
1732         $form->{comparefromdate} = "1.01.$form->{year}";
1733         $form->{comparetodate}   = "30.06.$form->{year}";
1734         last SWITCH;
1735       };
1736       $form->{duetyp} eq "7" && do {
1737         $form->{fromdate}        = "1.7.$form->{year}";
1738         $form->{todate}          = "31.7.$form->{year}";
1739         $form->{comparefromdate} = "1.01.$form->{year}";
1740         $form->{comparetodate}   = "31.07.$form->{year}";
1741         last SWITCH;
1742       };
1743       $form->{duetyp} eq "8" && do {
1744         $form->{fromdate}        = "1.8.$form->{year}";
1745         $form->{todate}          = "31.8.$form->{year}";
1746         $form->{comparefromdate} = "1.01.$form->{year}";
1747         $form->{comparetodate}   = "31.08.$form->{year}";
1748         last SWITCH;
1749       };
1750       $form->{duetyp} eq "9" && do {
1751         $form->{fromdate}        = "1.9.$form->{year}";
1752         $form->{todate}          = "30.9.$form->{year}";
1753         $form->{comparefromdate} = "1.01.$form->{year}";
1754         $form->{comparetodate}   = "30.09.$form->{year}";
1755         last SWITCH;
1756       };
1757       $form->{duetyp} eq "10" && do {
1758         $form->{fromdate}        = "1.10.$form->{year}";
1759         $form->{todate}          = "31.10.$form->{year}";
1760         $form->{comparefromdate} = "1.01.$form->{year}";
1761         $form->{comparetodate}   = "31.10.$form->{year}";
1762         last SWITCH;
1763       };
1764       $form->{duetyp} eq "11" && do {
1765         $form->{fromdate}        = "1.11.$form->{year}";
1766         $form->{todate}          = "30.11.$form->{year}";
1767         $form->{comparefromdate} = "1.01.$form->{year}";
1768         $form->{comparetodate}   = "30.11.$form->{year}";
1769         last SWITCH;
1770       };
1771       $form->{duetyp} eq "12" && do {
1772         $form->{fromdate}        = "1.12.$form->{year}";
1773         $form->{todate}          = "31.12.$form->{year}";
1774         $form->{comparefromdate} = "1.01.$form->{year}";
1775         $form->{comparetodate}   = "31.12.$form->{year}";
1776         last SWITCH;
1777       };
1778     }
1779     hotfix_reformat_date();
1780   } else {
1781     # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
1782     # ansonsten ist die prüfung in RP.pm
1783     # if (defined ($form->{fromdate|todate}=='..'))
1784     # immer wahr
1785     if ($form->{fromdate}){
1786       my $datetime = $locale->parse_date_to_object($form->{fromdate});
1787       $datetime->set( month      => 1,
1788                       day        => 1);
1789       $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
1790     }
1791     if ($form->{todate}){
1792       $form->{comparetodate}   = $form->{todate};
1793     }
1794   }
1795
1796   RP->bwa(\%myconfig, \%$form);
1797
1798   ($form->{department}) = split /--/, $form->{department};
1799
1800   $form->{period} =
1801     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1802   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1803
1804   # if there are any dates construct a where
1805   if ($form->{fromdate} || $form->{todate}) {
1806
1807     unless ($form->{todate}) {
1808       $form->{todate} = $form->current_date(\%myconfig);
1809     }
1810
1811     my %germandate = ("dateformat" => "dd.mm.yyyy");
1812
1813     my $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
1814     my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
1815
1816     my $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
1817     my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
1818
1819     $form->{this_period} = "$shortfromdate\n$shorttodate";
1820     $form->{period}      =
1821         $locale->text('for Period')
1822       . qq|\n$longfromdate |
1823       . $locale->text('bis')
1824       . qq| $longtodate|;
1825   }
1826
1827   $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
1828
1829   if ( $form->{method} eq 'cash' ) {
1830     $form->{accounting_method} = $locale->text('Cash accounting');
1831   } elsif ( $form->{method} eq 'accrual' ) {
1832     $form->{accounting_method} = $locale->text('Accrual accounting');
1833   } else {
1834     $form->{accounting_method} = "";
1835   };
1836
1837   $form->{title} = $locale->text('BWA');
1838
1839   $::request->layout->add_stylesheets('bwa.css');
1840   $form->header;
1841   print $form->parse_html_template('rp/bwa');
1842
1843   $main::lxdebug->leave_sub();
1844 }
1845 ###
1846 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
1847 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
1848 sub hotfix_reformat_date {
1849
1850   $main::lxdebug->enter_sub();
1851
1852   my $form     = $main::form;
1853   my %myconfig = %main::myconfig;
1854   my $locale   = $main::locale;
1855
1856   if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
1857     my $current_dateformat = $myconfig{dateformat};
1858     $myconfig{dateformat} = 'dd.mm.yyyy';
1859     $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
1860     $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
1861     $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
1862       unless (!defined ($form->{comparefromdate}));
1863     $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
1864       unless (!defined ($form->{comparetodate}));
1865
1866     # Und wieder zurücksetzen
1867     $myconfig{dateformat} =  $current_dateformat; #'dd.mm.yyyy';
1868   } # Ende Hotifx Bug 1388
1869
1870   $main::lxdebug->leave_sub();
1871
1872 }
1873
1874 sub setup_rp_aging_action_bar {
1875   my %params = @_;
1876
1877   return unless $params{arap} eq 'ar';
1878
1879   for my $bar ($::request->layout->get('actionbar')) {
1880     $bar->add(
1881       combobox => [
1882         action => [
1883           t8('Print'),
1884           call   => [ 'kivi.SalesPurchase.show_print_dialog' ],
1885           checks => [ [ 'kivi.check_if_entries_selected', '[name^=statement_]' ] ],
1886         ],
1887         action => [
1888           t8('E Mail'),
1889           call   => [ 'kivi.SalesPurchase.show_email_dialog', 'send_email' ],
1890           checks => [ [ 'kivi.check_if_entries_selected', '[name^=statement_]' ] ],
1891         ],
1892       ],
1893     );
1894   }
1895 }
1896
1897 sub setup_rp_report_action_bar {
1898   my %params = @_;
1899
1900   for my $bar ($::request->layout->get('actionbar')) {
1901     $bar->add(
1902       action => [
1903         t8('Continue'),
1904         submit    => [ '#form', { action => 'continue' } ],
1905         accesskey => 'enter',
1906       ],
1907     );
1908   }
1909 }
1910
1911 1;