fcff5cf335c097ec09d0fde585ff9cffc531e9a9
[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   $onload .= qq|;setupDateFormat('$::myconfig{dateformat}', '|. $::locale->text("Falsches Datumsformat!") .qq|')|;
159   $onload .= qq|;setupPoints('$::myconfig{numberformat}', '|. $::locale->text("wrongformat") .qq|')|;
160
161   $::form->header;
162   print $::form->parse_html_template('ca/list', {
163     onload => $onload,
164     year => DateTime->today->year,
165     cash => $::instance_conf->get_accounting_method eq 'cash',
166   });
167
168   $::lxdebug->leave_sub;
169 }
170
171 sub format_debit_credit {
172   $main::lxdebug->enter_sub();
173
174   my $dc = shift;
175
176   my $form     = $main::form;
177   my %myconfig = %main::myconfig;
178   my $locale   = $main::locale;
179
180   my $formatted_dc  = $form->format_amount(\%myconfig, abs($dc), 2) . ' ';
181   $formatted_dc    .= ($dc > 0) ? $locale->text('Credit (one letter abbreviation)') : $locale->text('Debit (one letter abbreviation)');
182
183   $main::lxdebug->leave_sub();
184
185   return $formatted_dc;
186 }
187
188
189 sub list_transactions {
190   $main::lxdebug->enter_sub();
191
192   my $form     = $main::form;
193   my %myconfig = %main::myconfig;
194   my $locale   = $main::locale;
195
196   $main::auth->assert('report');
197
198   $form->{title} = $locale->text('Account') . " $form->{accno} - $form->{description}";
199
200   if ($form->{reporttype} eq "custom") {
201
202     #forgotten the year --> thisyear
203     if ($form->{year} !~ m/^\d\d\d\d$/) {
204       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
205         /(\d\d\d\d)/;
206       $form->{year} = $1;
207     }
208
209     #yearly report
210     if ($form->{duetyp} eq "13") {
211       $form->{fromdate} = "1.1.$form->{year}";
212       $form->{todate}   = "31.12.$form->{year}";
213     }
214
215     #Quater reports
216     if ($form->{duetyp} eq "A") {
217       $form->{fromdate} = "1.1.$form->{year}";
218       $form->{todate}   = "31.3.$form->{year}";
219     }
220     if ($form->{duetyp} eq "B") {
221       $form->{fromdate} = "1.4.$form->{year}";
222       $form->{todate}   = "30.6.$form->{year}";
223     }
224     if ($form->{duetyp} eq "C") {
225       $form->{fromdate} = "1.7.$form->{year}";
226       $form->{todate}   = "30.9.$form->{year}";
227     }
228     if ($form->{duetyp} eq "D") {
229       $form->{fromdate} = "1.10.$form->{year}";
230       $form->{todate}   = "31.12.$form->{year}";
231     }
232
233     #Monthly reports
234   SWITCH: {
235       $form->{duetyp} eq "1" && do {
236         $form->{fromdate} = "1.1.$form->{year}";
237         $form->{todate}   = "31.1.$form->{year}";
238         last SWITCH;
239       };
240       $form->{duetyp} eq "2" && do {
241         $form->{fromdate} = "1.2.$form->{year}";
242
243         #this works from 1901 to 2099, 1900 and 2100 fail.
244         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
245         $form->{todate} = "$leap.2.$form->{year}";
246         last SWITCH;
247       };
248       $form->{duetyp} eq "3" && do {
249         $form->{fromdate} = "1.3.$form->{year}";
250         $form->{todate}   = "31.3.$form->{year}";
251         last SWITCH;
252       };
253       $form->{duetyp} eq "4" && do {
254         $form->{fromdate} = "1.4.$form->{year}";
255         $form->{todate}   = "30.4.$form->{year}";
256         last SWITCH;
257       };
258       $form->{duetyp} eq "5" && do {
259         $form->{fromdate} = "1.5.$form->{year}";
260         $form->{todate}   = "31.5.$form->{year}";
261         last SWITCH;
262       };
263       $form->{duetyp} eq "6" && do {
264         $form->{fromdate} = "1.6.$form->{year}";
265         $form->{todate}   = "30.6.$form->{year}";
266         last SWITCH;
267       };
268       $form->{duetyp} eq "7" && do {
269         $form->{fromdate} = "1.7.$form->{year}";
270         $form->{todate}   = "31.7.$form->{year}";
271         last SWITCH;
272       };
273       $form->{duetyp} eq "8" && do {
274         $form->{fromdate} = "1.8.$form->{year}";
275         $form->{todate}   = "31.8.$form->{year}";
276         last SWITCH;
277       };
278       $form->{duetyp} eq "9" && do {
279         $form->{fromdate} = "1.9.$form->{year}";
280         $form->{todate}   = "30.9.$form->{year}";
281         last SWITCH;
282       };
283       $form->{duetyp} eq "10" && do {
284         $form->{fromdate} = "1.10.$form->{year}";
285         $form->{todate}   = "31.10.$form->{year}";
286         last SWITCH;
287       };
288       $form->{duetyp} eq "11" && do {
289         $form->{fromdate} = "1.11.$form->{year}";
290         $form->{todate}   = "30.11.$form->{year}";
291         last SWITCH;
292       };
293       $form->{duetyp} eq "12" && do {
294         $form->{fromdate} = "1.12.$form->{year}";
295         $form->{todate}   = "31.12.$form->{year}";
296         last SWITCH;
297       };
298     }
299   }
300
301   CA->all_transactions(\%myconfig, \%$form);
302
303   $form->{saldo_old} += $form->{beginning_balance};
304   $form->{saldo_new} += $form->{beginning_balance};
305   my $saldo_old = format_debit_credit($form->{saldo_old});
306   my $eb_string = format_debit_credit($form->{beginning_balance});
307   $form->{balance} = $form->{saldo_old};
308
309   my @options;
310   if ($form->{department}) {
311     my ($department) = split /--/, $form->{department};
312     push @options, $locale->text('Department') . " : $department";
313   }
314   if ($form->{projectnumber}) {
315     push @options, $locale->text('Project Number') . " : $form->{projectnumber}<br>";
316   }
317
318   my $period;
319   if ($form->{fromdate} || $form->{todate}) {
320     my ($fromdate, $todate);
321
322     if ($form->{fromdate}) {
323       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
324     }
325     if ($form->{todate}) {
326       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
327     }
328
329     $period = "$fromdate - $todate";
330
331   } else {
332     $period = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
333   }
334
335   push @options, $period;
336
337   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
338   push (@options, $form->{print_date});
339
340   $form->{company} = $locale->text('Company') . " " . $myconfig{company};
341   push (@options, $form->{company});
342
343   my @columns     = qw(transdate reference description gegenkonto debit credit ustkonto ustrate balance);
344   my %column_defs = (
345     'transdate'   => { 'text' => $locale->text('Date'), },
346     'reference'   => { 'text' => $locale->text('Reference'), },
347     'description' => { 'text' => $locale->text('Description'), },
348     'debit'       => { 'text' => $locale->text('Debit'), },
349     'credit'      => { 'text' => $locale->text('Credit'), },
350     'gegenkonto'  => { 'text' => $locale->text('Gegenkonto'), },
351     'ustkonto'    => { 'text' => $locale->text('USt-Konto'), },
352     'balance'     => { 'text' => $locale->text('Balance'), },
353     'ustrate'     => { 'text' => $locale->text('Satz %'), },
354  );
355
356   my @hidden_variables = qw(accno fromdate todate description accounttype l_heading subtotal department projectnumber project_id sort method);
357
358   my $link = build_std_url('action=list_transactions', grep { $form->{$_} } @hidden_variables);
359
360   $form->{callback} = $link . '&sort=' . E($form->{sort});
361
362   my %column_alignment = map { $_ => 'right' } qw(debit credit);
363
364   my @custom_headers = ();
365  # Zeile 1:
366  push @custom_headers, [
367    { 'text' => 'Letzte Buchung', },
368    { 'text' => 'EB-Wert', },
369    { 'text' => 'Saldo alt', 'colspan' => 2, },
370    { 'text' => 'Jahresverkehrszahlen alt', 'colspan' => 2, },
371    { 'text' => '', 'colspan' => 2, },
372  ];
373  push @custom_headers, [
374    { 'text' => $form->{last_transaction}, },
375    { 'text' => $eb_string, },
376    { 'text' => $saldo_old, 'colspan' => 2, },
377    { 'text' => $form->format_amount(\%myconfig, abs($form->{old_balance_debit}), 2) . " S", },
378    { 'text' => $form->format_amount(\%myconfig, $form->{old_balance_credit}, 2) . " H", },
379    { 'text' => '', 'colspan' => 2, },
380  ];
381  # Zeile 2:
382  push @custom_headers, [
383    { 'text' => $locale->text('Date'), 'link' => $link . "&sort=transdate", },
384    { 'text' => $locale->text('Reference'), 'link' => $link . "&sort=reference",  },
385    { 'text' => $locale->text('Description'), 'link' => $link . "&sort=description",  },
386    { 'text' => $locale->text('Gegenkonto'), },
387    { 'text' => $locale->text('Debit'), },
388    { 'text' => $locale->text('Credit'), },
389    { 'text' => $locale->text('USt-Konto'), },
390    { 'text' => $locale->text('Satz %'), },
391    { 'text' => $locale->text('Balance'), },
392  ];
393
394
395
396
397
398   my $report = SL::ReportGenerator->new(\%myconfig, $form);
399   $report->set_custom_headers(@custom_headers);
400
401   $report->set_options('top_info_text'         => join("\n", @options),
402                        'output_format'         => 'HTML',
403                        'title'                 => $form->{title},
404                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
405                        'std_column_visibility' => 1,
406     );
407   $report->set_options_from_form();
408   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
409
410   $report->set_columns(%column_defs);
411   $report->set_column_order(@columns);
412
413   $report->set_export_options('list_transactions', @hidden_variables);
414
415   $report->set_sort_indicator($form->{sort}, 1);
416
417   $column_defs{balance}->{visible} = 1;
418
419   my $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
420
421
422   my $idx       = 0;
423   my %totals    = ( 'debit' => 0, 'credit' => 0 );
424   my %subtotals = ( 'debit' => 0, 'credit' => 0 );
425   my ($previous_index, $row_set);
426
427   foreach my $ca (@{ $form->{CA} }) {
428
429     foreach (qw(debit credit)) {
430       $subtotals{$_} += $ca->{$_};
431       $totals{$_}    += $ca->{$_};
432       if ($_ =~ /debit.*/) {
433         $ml = -1;
434       } else {
435         $ml = 1;
436       }
437       $form->{balance}= $form->{balance} + $ca->{$_} * $ml;
438       $ca->{$_}       = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_} != 0);
439     }
440
441     my $do_subtotal = 0;
442     if (($form->{subtotal})
443         && (($idx == scalar @{ $form->{CA} } - 1)
444             || ($ca->{$form->{sort}} ne $form->{CA}->[$idx + 1]->{$form->{sort}}))) {
445       $do_subtotal = 1;
446     }
447
448     my $row = { };
449
450     $ca->{ustrate} = $form->format_amount(\%myconfig, $ca->{ustrate} * 100, 2) if ($ca->{ustrate} != 0);
451
452     if ($ca->{memo} ne "") {
453       $ca->{description} .= " \n " . $ca->{memo};
454     }
455
456
457
458     foreach my $gegenkonto (@{ $ca->{GEGENKONTO} }) {
459       if ($ca->{gegenkonto} eq "") {
460         $ca->{gegenkonto} = $gegenkonto->{accno};
461       } else {
462         $ca->{gegenkonto} .= ", " . $gegenkonto->{accno};
463       }
464     }
465
466     foreach (@columns) {
467       $row->{$_} = {
468         'data'  => $ca->{$_},
469         'align' => $column_alignment{$_},
470       };
471     }
472
473     $row->{balance}->{data}        = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
474
475     if ($ca->{index} ne $previous_index) {
476 #       $report->add_data($row_set) if ($row_set);
477
478 #       $row_set         = [ ];
479       $previous_index  = $ca->{index};
480
481       $row->{reference}->{link} = build_std_url("script=$ca->{module}.pl", 'action=edit', 'id=' . E($ca->{id}), 'callback');
482
483     } elsif ($ca->{index} eq $previous_index) {
484       map { $row->{$_}->{data} = '' } qw(reference description);
485       $row->{transdate}->{data} = '' if ($form->{sort} eq 'transdate');
486     }
487
488     my $row_set = [];
489
490     push @{ $row_set }, $row;
491
492     push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, 'listsubtotal') if ($do_subtotal);
493
494
495     $idx++;
496     $report->add_data($row_set);
497
498   }
499
500   $report->add_data($row_set) if ($row_set);
501
502   $report->add_separator();
503
504   my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, 'listtotal');
505
506
507   $row->{balance}->{data}        = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
508
509   $report->add_data($row);
510
511
512   $report->add_separator();
513   $row = {
514      'transdate' => {
515        'data'    => "",
516        'class' => 'listtotal',
517      },
518      'reference' => {
519        'data'    => $locale->text('EB-Wert'),
520        'class' => 'listtotal',
521      },
522      'description'      => {
523        'data'    => $locale->text('Saldo neu'),
524        'colspan' => 2,
525        'class' => 'listtotal',
526      },
527      'debit'      => {
528        'data'    => $locale->text('Jahresverkehrszahlen neu'),
529        'colspan' => 2,
530        'align' => 'left',
531        'class' => 'listtotal',
532     },
533      'ustkonto'      => {
534        'data'    => '',
535        'colspan' => 2,
536        'align' => 'left',
537        'class' => 'listtotal',
538     },
539   };
540
541   $report->add_data($row);
542   my $saldo_new = format_debit_credit($form->{saldo_new});
543   $row = {
544      'transdate' => {
545        'data'    => "",
546        'class' => 'listtotal',
547      },
548      'reference' => {
549        'data'    => $eb_string,
550        'class' => 'listtotal',
551      },
552      'description'      => {
553        'data'    => $saldo_new,
554        'colspan' => 2,
555        'class' => 'listtotal',
556      },
557      'debit'      => {
558        'data'    => $form->format_amount(\%myconfig, abs($form->{current_balance_debit}) , 2) . " S",
559        'class' => 'listtotal',
560      },
561       'credit'      => {
562        'data'    => $form->format_amount(\%myconfig, $form->{current_balance_credit}, 2) . " H",
563        'class' => 'listtotal',
564      },
565       'ustkonto'      => {
566        'data'    => "",
567        'colspan' => 2,
568        'class' => 'listtotal',
569      },
570   };
571
572   $report->add_data($row);
573
574   $report->generate_with_headers();
575
576   $main::lxdebug->leave_sub();
577 }
578
579 sub create_subtotal_row {
580   $main::lxdebug->enter_sub();
581
582   my $form     = $main::form;
583   my %myconfig = %main::myconfig;
584
585   my ($totals, $columns, $column_alignment, $class) = @_;
586
587   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
588
589   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(credit debit);
590
591   map { $totals->{$_} = 0 } qw(debit credit);
592
593   $main::lxdebug->leave_sub();
594
595   return $row;
596 }