1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
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.
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,
29 #======================================================================
31 # module for Chart of Accounts, Income Statement and Balance Sheet
32 # search and edit transactions posted by the GL, AR and AP
34 #======================================================================
36 use POSIX qw(strftime);
40 use SL::ReportGenerator;
42 require "bin/mozilla/reportgenerator.pl";
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')
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')
78 sub chart_of_accounts {
79 $main::lxdebug->enter_sub();
81 my $form = $main::form;
82 my %myconfig = %main::myconfig;
83 my $locale = $main::locale;
85 $main::auth->assert('report');
87 $form->{title} = $locale->text('Chart of Accounts');
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";
94 CA->all_accounts(\%myconfig, \%$form);
96 my @columns = qw(accno description debit credit);
98 'accno' => { 'text' => $locale->text('Account'), },
99 'description' => { 'text' => $locale->text('Description'), },
100 'debit' => { 'text' => $locale->text('Debit'), },
101 'credit' => { 'text' => $locale->text('Credit'), },
104 my $report = SL::ReportGenerator->new(\%myconfig, $form);
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,
111 $report->set_options_from_form();
112 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
114 $report->set_columns(%column_defs);
115 $report->set_column_order(@columns);
117 $report->set_export_options('chart_of_accounts');
119 $report->set_sort_indicator($form->{sort}, 1);
121 my %totals = ('debit' => 0, 'credit' => 0);
123 foreach my $ca (@{ $form->{CA} }) {
124 next unless defined $ca->{amount};
127 foreach (qw(debit credit)) {
128 $totals{$_} += $ca->{$_} * 1;
129 $ca->{$_} = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_});
132 map { $row->{$_} = { 'data' => $ca->{$_} } } @columns;
134 map { $row->{$_}->{align} = 'right' } qw(debit credit);
135 map { $row->{$_}->{class} = 'listheading' } @columns if ($ca->{charttype} eq "H");
137 $row->{accno}->{link} = build_std_url('action=list', 'accno=' . E($ca->{accno}), 'description=' . E($ca->{description}));
139 $report->add_data($row);
142 my $row = { map { $_ => { 'class' => 'listtotal', 'align' => 'right' } } @columns };
143 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals{$_}, 2) } qw(debit credit);
145 $report->add_separator();
146 $report->add_data($row);
148 $report->generate_with_headers();
150 $main::lxdebug->leave_sub();
154 $::lxdebug->enter_sub;
155 $::auth->assert('report');
157 $::form->{title} = $::locale->text('List Transactions') . " - " . $::locale->text('Account') . " $::form->{accno}" . " - " . $::form->{description};
160 print $::form->parse_html_template('ca/list', {
161 year => DateTime->today->year,
164 $::lxdebug->leave_sub;
167 sub format_debit_credit {
168 $main::lxdebug->enter_sub();
172 my $form = $main::form;
173 my %myconfig = %main::myconfig;
174 my $locale = $main::locale;
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)');
179 $main::lxdebug->leave_sub();
181 return $formatted_dc;
185 sub list_transactions {
186 $main::lxdebug->enter_sub();
188 my $form = $main::form;
189 my %myconfig = %main::myconfig;
190 my $locale = $main::locale;
191 my $defaults = SL::DB::Default->get;
193 $main::auth->assert('report');
195 $form->{title} = $locale->text('Account') . " $form->{accno} - $form->{description}";
197 if ($form->{reporttype} eq "custom") {
199 #forgotten the year --> thisyear
200 if ($form->{year} !~ m/^\d\d\d\d$/) {
201 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
207 if ($form->{duetyp} eq "13") {
208 $form->{fromdate} = "1.1.$form->{year}";
209 $form->{todate} = "31.12.$form->{year}";
213 if ($form->{duetyp} eq "A") {
214 $form->{fromdate} = "1.1.$form->{year}";
215 $form->{todate} = "31.3.$form->{year}";
217 if ($form->{duetyp} eq "B") {
218 $form->{fromdate} = "1.4.$form->{year}";
219 $form->{todate} = "30.6.$form->{year}";
221 if ($form->{duetyp} eq "C") {
222 $form->{fromdate} = "1.7.$form->{year}";
223 $form->{todate} = "30.9.$form->{year}";
225 if ($form->{duetyp} eq "D") {
226 $form->{fromdate} = "1.10.$form->{year}";
227 $form->{todate} = "31.12.$form->{year}";
232 $form->{duetyp} eq "1" && do {
233 $form->{fromdate} = "1.1.$form->{year}";
234 $form->{todate} = "31.1.$form->{year}";
237 $form->{duetyp} eq "2" && do {
238 $form->{fromdate} = "1.2.$form->{year}";
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}";
245 $form->{duetyp} eq "3" && do {
246 $form->{fromdate} = "1.3.$form->{year}";
247 $form->{todate} = "31.3.$form->{year}";
250 $form->{duetyp} eq "4" && do {
251 $form->{fromdate} = "1.4.$form->{year}";
252 $form->{todate} = "30.4.$form->{year}";
255 $form->{duetyp} eq "5" && do {
256 $form->{fromdate} = "1.5.$form->{year}";
257 $form->{todate} = "31.5.$form->{year}";
260 $form->{duetyp} eq "6" && do {
261 $form->{fromdate} = "1.6.$form->{year}";
262 $form->{todate} = "30.6.$form->{year}";
265 $form->{duetyp} eq "7" && do {
266 $form->{fromdate} = "1.7.$form->{year}";
267 $form->{todate} = "31.7.$form->{year}";
270 $form->{duetyp} eq "8" && do {
271 $form->{fromdate} = "1.8.$form->{year}";
272 $form->{todate} = "31.8.$form->{year}";
275 $form->{duetyp} eq "9" && do {
276 $form->{fromdate} = "1.9.$form->{year}";
277 $form->{todate} = "30.9.$form->{year}";
280 $form->{duetyp} eq "10" && do {
281 $form->{fromdate} = "1.10.$form->{year}";
282 $form->{todate} = "31.10.$form->{year}";
285 $form->{duetyp} eq "11" && do {
286 $form->{fromdate} = "1.11.$form->{year}";
287 $form->{todate} = "30.11.$form->{year}";
290 $form->{duetyp} eq "12" && do {
291 $form->{fromdate} = "1.12.$form->{year}";
292 $form->{todate} = "31.12.$form->{year}";
298 CA->all_transactions(\%myconfig, \%$form);
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};
307 if ($form->{department}) {
308 my ($department) = split /--/, $form->{department};
309 push @options, $locale->text('Department') . " : $department";
311 if ($form->{projectnumber}) {
312 push @options, $locale->text('Project Number') . " : $form->{projectnumber}<br>";
316 if ($form->{fromdate} || $form->{todate}) {
317 my ($fromdate, $todate);
319 if ($form->{fromdate}) {
320 $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
322 if ($form->{todate}) {
323 $todate = $locale->date(\%myconfig, $form->{todate}, 1);
326 $period = "$fromdate - $todate";
329 $period = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
332 push @options, $period;
334 $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
335 push (@options, $form->{print_date});
337 $form->{company} = $locale->text('Company') . " " . $defaults->company;
338 push (@options, $form->{company});
340 my @columns = qw(transdate reference description gegenkonto debit credit ustkonto ustrate balance);
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 %'), },
353 my @hidden_variables = qw(accno fromdate todate description accounttype l_heading subtotal department projectnumber project_id sort method);
355 my $link = build_std_url('action=list_transactions', grep { $form->{$_} } @hidden_variables);
357 $form->{callback} = $link . '&sort=' . E($form->{sort});
359 my %column_alignment = map { $_ => 'right' } qw(debit credit balance);
361 my @custom_headers = ();
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, },
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, },
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'), },
395 my $report = SL::ReportGenerator->new(\%myconfig, $form);
396 $report->set_custom_headers(@custom_headers);
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,
404 $report->set_options_from_form();
405 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
407 $report->set_columns(%column_defs);
408 $report->set_column_order(@columns);
410 $report->set_export_options('list_transactions', @hidden_variables);
412 $report->set_sort_indicator($form->{sort}, 1);
414 $column_defs{balance}->{visible} = 1;
416 my $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
420 my %totals = ( 'debit' => 0, 'credit' => 0 );
421 my %subtotals = ( 'debit' => 0, 'credit' => 0 );
422 my ($previous_index, $row_set);
424 foreach my $ca (@{ $form->{CA} }) {
426 foreach (qw(debit credit)) {
427 $subtotals{$_} += $ca->{$_};
428 $totals{$_} += $ca->{$_};
429 if ($_ =~ /debit.*/) {
434 $form->{balance}= $form->{balance} + $ca->{$_} * $ml;
435 $ca->{$_} = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_} != 0);
439 if (($form->{subtotal})
440 && (($idx == scalar @{ $form->{CA} } - 1)
441 || ($ca->{$form->{sort}} ne $form->{CA}->[$idx + 1]->{$form->{sort}}))) {
447 $ca->{ustrate} = $form->format_amount(\%myconfig, $ca->{ustrate} * 100, 2) if ($ca->{ustrate} != 0);
449 if ($ca->{memo} ne "") {
450 $ca->{description} .= " \n " . $ca->{memo};
455 foreach my $gegenkonto (@{ $ca->{GEGENKONTO} }) {
456 if ($ca->{gegenkonto} eq "") {
457 $ca->{gegenkonto} = $gegenkonto->{accno};
459 $ca->{gegenkonto} .= ", " . $gegenkonto->{accno};
466 'align' => $column_alignment{$_},
470 $row->{balance}->{data} = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
472 if ($ca->{index} ne $previous_index) {
473 # $report->add_data($row_set) if ($row_set);
476 $previous_index = $ca->{index};
478 $row->{reference}->{link} = build_std_url("script=$ca->{module}.pl", 'action=edit', 'id=' . E($ca->{id}), 'callback');
480 } elsif ($ca->{index} eq $previous_index) {
481 map { $row->{$_}->{data} = '' } qw(reference description);
482 $row->{transdate}->{data} = '' if ($form->{sort} eq 'transdate');
487 push @{ $row_set }, $row;
489 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, 'listsubtotal') if ($do_subtotal);
493 $report->add_data($row_set);
497 $report->add_data($row_set) if ($row_set);
499 $report->add_separator();
501 my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, 'listtotal');
504 $row->{balance}->{data} = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
506 $report->add_data($row);
509 $report->add_separator();
513 'class' => 'listtotal',
516 'data' => $locale->text('EB-Wert'),
517 'class' => 'listtotal',
520 'data' => $locale->text('Saldo neu'),
522 'class' => 'listtotal',
525 'data' => $locale->text('Jahresverkehrszahlen neu'),
528 'class' => 'listtotal',
534 'class' => 'listtotal',
538 $report->add_data($row);
539 my $saldo_new = format_debit_credit($form->{saldo_new});
543 'class' => 'listtotal',
546 'data' => $eb_string,
547 'class' => 'listtotal',
550 'data' => $saldo_new,
552 'class' => 'listtotal',
555 'data' => $form->format_amount(\%myconfig, abs($form->{current_balance_debit}) , 2) . " S",
556 'class' => 'listtotal',
559 'data' => $form->format_amount(\%myconfig, $form->{current_balance_credit}, 2) . " H",
560 'class' => 'listtotal',
565 'class' => 'listtotal',
569 $report->add_data($row);
571 $report->generate_with_headers();
573 $main::lxdebug->leave_sub();
576 sub create_subtotal_row {
577 $main::lxdebug->enter_sub();
579 my $form = $main::form;
580 my %myconfig = %main::myconfig;
582 my ($totals, $columns, $column_alignment, $class) = @_;
584 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
586 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(credit debit);
588 map { $totals->{$_} = 0 } qw(debit credit);
590 $main::lxdebug->leave_sub();