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