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