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