setupPoints und setupDateFormat in ein partial Layout verschoben
[kivitendo-erp.git] / bin / mozilla / ca.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) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # module for Chart of Accounts, Income Statement and Balance Sheet
31 # search and edit transactions posted by the GL, AR and AP
32 #
33 #======================================================================
34
35 use POSIX qw(strftime);
36
37 use SL::CA;
38 use SL::ReportGenerator;
39
40 require "bin/mozilla/reportgenerator.pl";
41
42 use strict;
43
44 1;
45
46 # end of main
47
48 # this is for our long dates
49 # $locale->text('January')
50 # $locale->text('February')
51 # $locale->text('March')
52 # $locale->text('April')
53 # $locale->text('May ')
54 # $locale->text('June')
55 # $locale->text('July')
56 # $locale->text('August')
57 # $locale->text('September')
58 # $locale->text('October')
59 # $locale->text('November')
60 # $locale->text('December')
61
62 # this is for our short month
63 # $locale->text('Jan')
64 # $locale->text('Feb')
65 # $locale->text('Mar')
66 # $locale->text('Apr')
67 # $locale->text('May')
68 # $locale->text('Jun')
69 # $locale->text('Jul')
70 # $locale->text('Aug')
71 # $locale->text('Sep')
72 # $locale->text('Oct')
73 # $locale->text('Nov')
74 # $locale->text('Dec')
75
76 sub chart_of_accounts {
77   $main::lxdebug->enter_sub();
78
79   my $form     = $main::form;
80   my %myconfig = %main::myconfig;
81   my $locale   = $main::locale;
82
83   $main::auth->assert('report');
84
85   $form->{title} = $locale->text('Chart of Accounts');
86
87   if ( $::instance_conf->get_accounting_method eq 'cash' ) {
88     # $form->{method} can probably be made redundant now that we have get_accounting_method
89     $form->{method} = "cash";
90   }
91
92   CA->all_accounts(\%myconfig, \%$form);
93
94   my @columns     = qw(accno description debit credit);
95   my %column_defs = (
96     'accno'       => { 'text' => $locale->text('Account'), },
97     'description' => { 'text' => $locale->text('Description'), },
98     'debit'       => { 'text' => $locale->text('Debit'), },
99     'credit'      => { 'text' => $locale->text('Credit'), },
100   );
101
102   my $report = SL::ReportGenerator->new(\%myconfig, $form);
103
104   $report->set_options('output_format'         => 'HTML',
105                        'title'                 => $form->{title},
106                        'attachment_basename'   => $locale->text('chart_of_accounts') . strftime('_%Y%m%d', localtime time),
107                        'std_column_visibility' => 1,
108     );
109   $report->set_options_from_form();
110   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
111
112   $report->set_columns(%column_defs);
113   $report->set_column_order(@columns);
114
115   $report->set_export_options('chart_of_accounts');
116
117   $report->set_sort_indicator($form->{sort}, 1);
118
119   my %totals = ('debit' => 0, 'credit' => 0);
120
121   foreach my $ca (@{ $form->{CA} }) {
122     next unless defined $ca->{amount};
123     my $row = { };
124
125     foreach (qw(debit credit)) {
126       $totals{$_} += $ca->{$_} * 1;
127       $ca->{$_}    = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_});
128     }
129
130     map { $row->{$_} = { 'data' => $ca->{$_} } } @columns;
131
132     map { $row->{$_}->{align} = 'right'       } qw(debit credit);
133     map { $row->{$_}->{class} = 'listheading' } @columns if ($ca->{charttype} eq "H");
134
135     $row->{accno}->{link} = build_std_url('action=list', 'accno=' . E($ca->{accno}), 'description=' . E($ca->{description}));
136
137     $report->add_data($row);
138   }
139
140   my $row = { map { $_ => { 'class' => 'listtotal', 'align' => 'right' } } @columns };
141   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals{$_}, 2) } qw(debit credit);
142
143   $report->add_separator();
144   $report->add_data($row);
145
146   $report->generate_with_headers();
147
148   $main::lxdebug->leave_sub();
149 }
150
151 sub list {
152   $::lxdebug->enter_sub;
153   $::auth->assert('report');
154
155   $::form->{title} = $::locale->text('List Transactions') . " - " . $::locale->text('Account') . " $::form->{accno}";
156
157   my $onload = qq|focus()|;
158
159   $::form->header;
160   print $::form->parse_html_template('ca/list', {
161     onload => $onload,
162     year => DateTime->today->year,
163     cash => $::instance_conf->get_accounting_method eq 'cash',
164   });
165
166   $::lxdebug->leave_sub;
167 }
168
169 sub format_debit_credit {
170   $main::lxdebug->enter_sub();
171
172   my $dc = shift;
173
174   my $form     = $main::form;
175   my %myconfig = %main::myconfig;
176   my $locale   = $main::locale;
177
178   my $formatted_dc  = $form->format_amount(\%myconfig, abs($dc), 2) . ' ';
179   $formatted_dc    .= ($dc > 0) ? $locale->text('Credit (one letter abbreviation)') : $locale->text('Debit (one letter abbreviation)');
180
181   $main::lxdebug->leave_sub();
182
183   return $formatted_dc;
184 }
185
186
187 sub list_transactions {
188   $main::lxdebug->enter_sub();
189
190   my $form     = $main::form;
191   my %myconfig = %main::myconfig;
192   my $locale   = $main::locale;
193
194   $main::auth->assert('report');
195
196   $form->{title} = $locale->text('Account') . " $form->{accno} - $form->{description}";
197
198   if ($form->{reporttype} eq "custom") {
199
200     #forgotten the year --> thisyear
201     if ($form->{year} !~ m/^\d\d\d\d$/) {
202       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
203         /(\d\d\d\d)/;
204       $form->{year} = $1;
205     }
206
207     #yearly report
208     if ($form->{duetyp} eq "13") {
209       $form->{fromdate} = "1.1.$form->{year}";
210       $form->{todate}   = "31.12.$form->{year}";
211     }
212
213     #Quater reports
214     if ($form->{duetyp} eq "A") {
215       $form->{fromdate} = "1.1.$form->{year}";
216       $form->{todate}   = "31.3.$form->{year}";
217     }
218     if ($form->{duetyp} eq "B") {
219       $form->{fromdate} = "1.4.$form->{year}";
220       $form->{todate}   = "30.6.$form->{year}";
221     }
222     if ($form->{duetyp} eq "C") {
223       $form->{fromdate} = "1.7.$form->{year}";
224       $form->{todate}   = "30.9.$form->{year}";
225     }
226     if ($form->{duetyp} eq "D") {
227       $form->{fromdate} = "1.10.$form->{year}";
228       $form->{todate}   = "31.12.$form->{year}";
229     }
230
231     #Monthly reports
232   SWITCH: {
233       $form->{duetyp} eq "1" && do {
234         $form->{fromdate} = "1.1.$form->{year}";
235         $form->{todate}   = "31.1.$form->{year}";
236         last SWITCH;
237       };
238       $form->{duetyp} eq "2" && do {
239         $form->{fromdate} = "1.2.$form->{year}";
240
241         #this works from 1901 to 2099, 1900 and 2100 fail.
242         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
243         $form->{todate} = "$leap.2.$form->{year}";
244         last SWITCH;
245       };
246       $form->{duetyp} eq "3" && do {
247         $form->{fromdate} = "1.3.$form->{year}";
248         $form->{todate}   = "31.3.$form->{year}";
249         last SWITCH;
250       };
251       $form->{duetyp} eq "4" && do {
252         $form->{fromdate} = "1.4.$form->{year}";
253         $form->{todate}   = "30.4.$form->{year}";
254         last SWITCH;
255       };
256       $form->{duetyp} eq "5" && do {
257         $form->{fromdate} = "1.5.$form->{year}";
258         $form->{todate}   = "31.5.$form->{year}";
259         last SWITCH;
260       };
261       $form->{duetyp} eq "6" && do {
262         $form->{fromdate} = "1.6.$form->{year}";
263         $form->{todate}   = "30.6.$form->{year}";
264         last SWITCH;
265       };
266       $form->{duetyp} eq "7" && do {
267         $form->{fromdate} = "1.7.$form->{year}";
268         $form->{todate}   = "31.7.$form->{year}";
269         last SWITCH;
270       };
271       $form->{duetyp} eq "8" && do {
272         $form->{fromdate} = "1.8.$form->{year}";
273         $form->{todate}   = "31.8.$form->{year}";
274         last SWITCH;
275       };
276       $form->{duetyp} eq "9" && do {
277         $form->{fromdate} = "1.9.$form->{year}";
278         $form->{todate}   = "30.9.$form->{year}";
279         last SWITCH;
280       };
281       $form->{duetyp} eq "10" && do {
282         $form->{fromdate} = "1.10.$form->{year}";
283         $form->{todate}   = "31.10.$form->{year}";
284         last SWITCH;
285       };
286       $form->{duetyp} eq "11" && do {
287         $form->{fromdate} = "1.11.$form->{year}";
288         $form->{todate}   = "30.11.$form->{year}";
289         last SWITCH;
290       };
291       $form->{duetyp} eq "12" && do {
292         $form->{fromdate} = "1.12.$form->{year}";
293         $form->{todate}   = "31.12.$form->{year}";
294         last SWITCH;
295       };
296     }
297   }
298
299   CA->all_transactions(\%myconfig, \%$form);
300
301   $form->{saldo_old} += $form->{beginning_balance};
302   $form->{saldo_new} += $form->{beginning_balance};
303   my $saldo_old = format_debit_credit($form->{saldo_old});
304   my $eb_string = format_debit_credit($form->{beginning_balance});
305   $form->{balance} = $form->{saldo_old};
306
307   my @options;
308   if ($form->{department}) {
309     my ($department) = split /--/, $form->{department};
310     push @options, $locale->text('Department') . " : $department";
311   }
312   if ($form->{projectnumber}) {
313     push @options, $locale->text('Project Number') . " : $form->{projectnumber}<br>";
314   }
315
316   my $period;
317   if ($form->{fromdate} || $form->{todate}) {
318     my ($fromdate, $todate);
319
320     if ($form->{fromdate}) {
321       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
322     }
323     if ($form->{todate}) {
324       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
325     }
326
327     $period = "$fromdate - $todate";
328
329   } else {
330     $period = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
331   }
332
333   push @options, $period;
334
335   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
336   push (@options, $form->{print_date});
337
338   $form->{company} = $locale->text('Company') . " " . $myconfig{company};
339   push (@options, $form->{company});
340
341   my @columns     = qw(transdate reference description gegenkonto debit credit ustkonto ustrate balance);
342   my %column_defs = (
343     'transdate'   => { 'text' => $locale->text('Date'), },
344     'reference'   => { 'text' => $locale->text('Reference'), },
345     'description' => { 'text' => $locale->text('Description'), },
346     'debit'       => { 'text' => $locale->text('Debit'), },
347     'credit'      => { 'text' => $locale->text('Credit'), },
348     'gegenkonto'  => { 'text' => $locale->text('Gegenkonto'), },
349     'ustkonto'    => { 'text' => $locale->text('USt-Konto'), },
350     'balance'     => { 'text' => $locale->text('Balance'), },
351     'ustrate'     => { 'text' => $locale->text('Satz %'), },
352  );
353
354   my @hidden_variables = qw(accno fromdate todate description accounttype l_heading subtotal department projectnumber project_id sort method);
355
356   my $link = build_std_url('action=list_transactions', grep { $form->{$_} } @hidden_variables);
357
358   $form->{callback} = $link . '&sort=' . E($form->{sort});
359
360   my %column_alignment = map { $_ => 'right' } qw(debit credit balance);
361
362   my @custom_headers = ();
363  # Zeile 1:
364  push @custom_headers, [
365    { 'text' => 'Letzte Buchung', },
366    { 'text' => 'EB-Wert', },
367    { 'text' => 'Saldo alt', 'colspan' => 2, },
368    { 'text' => 'Jahresverkehrszahlen alt', 'colspan' => 2, },
369    { 'text' => '', 'colspan' => 2, },
370  ];
371  push @custom_headers, [
372    { 'text' => $form->{last_transaction}, },
373    { 'text' => $eb_string, },
374    { 'text' => $saldo_old, 'colspan' => 2, },
375    { 'text' => $form->format_amount(\%myconfig, abs($form->{old_balance_debit}), 2) . " S", },
376    { 'text' => $form->format_amount(\%myconfig, $form->{old_balance_credit}, 2) . " H", },
377    { 'text' => '', 'colspan' => 2, },
378  ];
379  # Zeile 2:
380  push @custom_headers, [
381    { 'text' => $locale->text('Date'), 'link' => $link . "&sort=transdate", },
382    { 'text' => $locale->text('Reference'), 'link' => $link . "&sort=reference",  },
383    { 'text' => $locale->text('Description'), 'link' => $link . "&sort=description",  },
384    { 'text' => $locale->text('Gegenkonto'), },
385    { 'text' => $locale->text('Debit'), },
386    { 'text' => $locale->text('Credit'), },
387    { 'text' => $locale->text('USt-Konto'), },
388    { 'text' => $locale->text('Satz %'), },
389    { 'text' => $locale->text('Balance'), },
390  ];
391
392
393
394
395
396   my $report = SL::ReportGenerator->new(\%myconfig, $form);
397   $report->set_custom_headers(@custom_headers);
398
399   $report->set_options('top_info_text'         => join("\n", @options),
400                        'output_format'         => 'HTML',
401                        'title'                 => $form->{title},
402                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
403                        'std_column_visibility' => 1,
404     );
405   $report->set_options_from_form();
406   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
407
408   $report->set_columns(%column_defs);
409   $report->set_column_order(@columns);
410
411   $report->set_export_options('list_transactions', @hidden_variables);
412
413   $report->set_sort_indicator($form->{sort}, 1);
414
415   $column_defs{balance}->{visible} = 1;
416
417   my $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
418
419
420   my $idx       = 0;
421   my %totals    = ( 'debit' => 0, 'credit' => 0 );
422   my %subtotals = ( 'debit' => 0, 'credit' => 0 );
423   my ($previous_index, $row_set);
424
425   foreach my $ca (@{ $form->{CA} }) {
426
427     foreach (qw(debit credit)) {
428       $subtotals{$_} += $ca->{$_};
429       $totals{$_}    += $ca->{$_};
430       if ($_ =~ /debit.*/) {
431         $ml = -1;
432       } else {
433         $ml = 1;
434       }
435       $form->{balance}= $form->{balance} + $ca->{$_} * $ml;
436       $ca->{$_}       = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_} != 0);
437     }
438
439     my $do_subtotal = 0;
440     if (($form->{subtotal})
441         && (($idx == scalar @{ $form->{CA} } - 1)
442             || ($ca->{$form->{sort}} ne $form->{CA}->[$idx + 1]->{$form->{sort}}))) {
443       $do_subtotal = 1;
444     }
445
446     my $row = { };
447
448     $ca->{ustrate} = $form->format_amount(\%myconfig, $ca->{ustrate} * 100, 2) if ($ca->{ustrate} != 0);
449
450     if ($ca->{memo} ne "") {
451       $ca->{description} .= " \n " . $ca->{memo};
452     }
453
454
455
456     foreach my $gegenkonto (@{ $ca->{GEGENKONTO} }) {
457       if ($ca->{gegenkonto} eq "") {
458         $ca->{gegenkonto} = $gegenkonto->{accno};
459       } else {
460         $ca->{gegenkonto} .= ", " . $gegenkonto->{accno};
461       }
462     }
463
464     foreach (@columns) {
465       $row->{$_} = {
466         'data'  => $ca->{$_},
467         'align' => $column_alignment{$_},
468       };
469     }
470
471     $row->{balance}->{data}        = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
472
473     if ($ca->{index} ne $previous_index) {
474 #       $report->add_data($row_set) if ($row_set);
475
476 #       $row_set         = [ ];
477       $previous_index  = $ca->{index};
478
479       $row->{reference}->{link} = build_std_url("script=$ca->{module}.pl", 'action=edit', 'id=' . E($ca->{id}), 'callback');
480
481     } elsif ($ca->{index} eq $previous_index) {
482       map { $row->{$_}->{data} = '' } qw(reference description);
483       $row->{transdate}->{data} = '' if ($form->{sort} eq 'transdate');
484     }
485
486     my $row_set = [];
487
488     push @{ $row_set }, $row;
489
490     push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, 'listsubtotal') if ($do_subtotal);
491
492
493     $idx++;
494     $report->add_data($row_set);
495
496   }
497
498   $report->add_data($row_set) if ($row_set);
499
500   $report->add_separator();
501
502   my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, 'listtotal');
503
504
505   $row->{balance}->{data}        = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
506
507   $report->add_data($row);
508
509
510   $report->add_separator();
511   $row = {
512      'transdate' => {
513        'data'    => "",
514        'class' => 'listtotal',
515      },
516      'reference' => {
517        'data'    => $locale->text('EB-Wert'),
518        'class' => 'listtotal',
519      },
520      'description'      => {
521        'data'    => $locale->text('Saldo neu'),
522        'colspan' => 2,
523        'class' => 'listtotal',
524      },
525      'debit'      => {
526        'data'    => $locale->text('Jahresverkehrszahlen neu'),
527        'colspan' => 2,
528        'align' => 'left',
529        'class' => 'listtotal',
530     },
531      'ustkonto'      => {
532        'data'    => '',
533        'colspan' => 2,
534        'align' => 'left',
535        'class' => 'listtotal',
536     },
537   };
538
539   $report->add_data($row);
540   my $saldo_new = format_debit_credit($form->{saldo_new});
541   $row = {
542      'transdate' => {
543        'data'    => "",
544        'class' => 'listtotal',
545      },
546      'reference' => {
547        'data'    => $eb_string,
548        'class' => 'listtotal',
549      },
550      'description'      => {
551        'data'    => $saldo_new,
552        'colspan' => 2,
553        'class' => 'listtotal',
554      },
555      'debit'      => {
556        'data'    => $form->format_amount(\%myconfig, abs($form->{current_balance_debit}) , 2) . " S",
557        'class' => 'listtotal',
558      },
559       'credit'      => {
560        'data'    => $form->format_amount(\%myconfig, $form->{current_balance_credit}, 2) . " H",
561        'class' => 'listtotal',
562      },
563       'ustkonto'      => {
564        'data'    => "",
565        'colspan' => 2,
566        'class' => 'listtotal',
567      },
568   };
569
570   $report->add_data($row);
571
572   $report->generate_with_headers();
573
574   $main::lxdebug->leave_sub();
575 }
576
577 sub create_subtotal_row {
578   $main::lxdebug->enter_sub();
579
580   my $form     = $main::form;
581   my %myconfig = %main::myconfig;
582
583   my ($totals, $columns, $column_alignment, $class) = @_;
584
585   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
586
587   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(credit debit);
588
589   map { $totals->{$_} = 0 } qw(debit credit);
590
591   $main::lxdebug->leave_sub();
592
593   return $row;
594 }