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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
30 # module for Chart of Accounts, Income Statement and Balance Sheet
31 # search and edit transactions posted by the GL, AR and AP
33 #======================================================================
35 use POSIX qw(strftime);
38 use SL::ReportGenerator;
40 require "bin/mozilla/reportgenerator.pl";
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')
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')
76 sub chart_of_accounts {
77 $main::lxdebug->enter_sub();
79 my $form = $main::form;
80 my %myconfig = %main::myconfig;
81 my $locale = $main::locale;
83 $main::auth->assert('report');
85 $form->{title} = $locale->text('Chart of Accounts');
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";
92 CA->all_accounts(\%myconfig, \%$form);
94 my @columns = qw(accno description debit credit);
96 'accno' => { 'text' => $locale->text('Account'), },
97 'description' => { 'text' => $locale->text('Description'), },
98 'debit' => { 'text' => $locale->text('Debit'), },
99 'credit' => { 'text' => $locale->text('Credit'), },
102 my $report = SL::ReportGenerator->new(\%myconfig, $form);
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,
109 $report->set_options_from_form();
110 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
112 $report->set_columns(%column_defs);
113 $report->set_column_order(@columns);
115 $report->set_export_options('chart_of_accounts');
117 $report->set_sort_indicator($form->{sort}, 1);
119 my %totals = ('debit' => 0, 'credit' => 0);
121 foreach my $ca (@{ $form->{CA} }) {
122 next unless defined $ca->{amount};
125 foreach (qw(debit credit)) {
126 $totals{$_} += $ca->{$_} * 1;
127 $ca->{$_} = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_});
130 map { $row->{$_} = { 'data' => $ca->{$_} } } @columns;
132 map { $row->{$_}->{align} = 'right' } qw(debit credit);
133 map { $row->{$_}->{class} = 'listheading' } @columns if ($ca->{charttype} eq "H");
135 $row->{accno}->{link} = build_std_url('action=list', 'accno=' . E($ca->{accno}), 'description=' . E($ca->{description}));
137 $report->add_data($row);
140 my $row = { map { $_ => { 'class' => 'listtotal', 'align' => 'right' } } @columns };
141 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals{$_}, 2) } qw(debit credit);
143 $report->add_separator();
144 $report->add_data($row);
146 $report->generate_with_headers();
148 $main::lxdebug->leave_sub();
152 $::lxdebug->enter_sub;
153 $::auth->assert('report');
155 $::form->{title} = $::locale->text('List Transactions') . " - " . $::locale->text('Account') . " $::form->{accno}";
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|')|;
162 print $::form->parse_html_template('ca/list', {
164 year => DateTime->today->year,
165 cash => $::instance_conf->get_accounting_method eq 'cash',
168 $::lxdebug->leave_sub;
171 sub format_debit_credit {
172 $main::lxdebug->enter_sub();
176 my $form = $main::form;
177 my %myconfig = %main::myconfig;
178 my $locale = $main::locale;
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)');
183 $main::lxdebug->leave_sub();
185 return $formatted_dc;
189 sub list_transactions {
190 $main::lxdebug->enter_sub();
192 my $form = $main::form;
193 my %myconfig = %main::myconfig;
194 my $locale = $main::locale;
196 $main::auth->assert('report');
198 $form->{title} = $locale->text('Account') . " $form->{accno} - $form->{description}";
200 if ($form->{reporttype} eq "custom") {
202 #forgotten the year --> thisyear
203 if ($form->{year} !~ m/^\d\d\d\d$/) {
204 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
210 if ($form->{duetyp} eq "13") {
211 $form->{fromdate} = "1.1.$form->{year}";
212 $form->{todate} = "31.12.$form->{year}";
216 if ($form->{duetyp} eq "A") {
217 $form->{fromdate} = "1.1.$form->{year}";
218 $form->{todate} = "31.3.$form->{year}";
220 if ($form->{duetyp} eq "B") {
221 $form->{fromdate} = "1.4.$form->{year}";
222 $form->{todate} = "30.6.$form->{year}";
224 if ($form->{duetyp} eq "C") {
225 $form->{fromdate} = "1.7.$form->{year}";
226 $form->{todate} = "30.9.$form->{year}";
228 if ($form->{duetyp} eq "D") {
229 $form->{fromdate} = "1.10.$form->{year}";
230 $form->{todate} = "31.12.$form->{year}";
235 $form->{duetyp} eq "1" && do {
236 $form->{fromdate} = "1.1.$form->{year}";
237 $form->{todate} = "31.1.$form->{year}";
240 $form->{duetyp} eq "2" && do {
241 $form->{fromdate} = "1.2.$form->{year}";
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}";
248 $form->{duetyp} eq "3" && do {
249 $form->{fromdate} = "1.3.$form->{year}";
250 $form->{todate} = "31.3.$form->{year}";
253 $form->{duetyp} eq "4" && do {
254 $form->{fromdate} = "1.4.$form->{year}";
255 $form->{todate} = "30.4.$form->{year}";
258 $form->{duetyp} eq "5" && do {
259 $form->{fromdate} = "1.5.$form->{year}";
260 $form->{todate} = "31.5.$form->{year}";
263 $form->{duetyp} eq "6" && do {
264 $form->{fromdate} = "1.6.$form->{year}";
265 $form->{todate} = "30.6.$form->{year}";
268 $form->{duetyp} eq "7" && do {
269 $form->{fromdate} = "1.7.$form->{year}";
270 $form->{todate} = "31.7.$form->{year}";
273 $form->{duetyp} eq "8" && do {
274 $form->{fromdate} = "1.8.$form->{year}";
275 $form->{todate} = "31.8.$form->{year}";
278 $form->{duetyp} eq "9" && do {
279 $form->{fromdate} = "1.9.$form->{year}";
280 $form->{todate} = "30.9.$form->{year}";
283 $form->{duetyp} eq "10" && do {
284 $form->{fromdate} = "1.10.$form->{year}";
285 $form->{todate} = "31.10.$form->{year}";
288 $form->{duetyp} eq "11" && do {
289 $form->{fromdate} = "1.11.$form->{year}";
290 $form->{todate} = "30.11.$form->{year}";
293 $form->{duetyp} eq "12" && do {
294 $form->{fromdate} = "1.12.$form->{year}";
295 $form->{todate} = "31.12.$form->{year}";
301 CA->all_transactions(\%myconfig, \%$form);
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};
310 if ($form->{department}) {
311 my ($department) = split /--/, $form->{department};
312 push @options, $locale->text('Department') . " : $department";
314 if ($form->{projectnumber}) {
315 push @options, $locale->text('Project Number') . " : $form->{projectnumber}<br>";
319 if ($form->{fromdate} || $form->{todate}) {
320 my ($fromdate, $todate);
322 if ($form->{fromdate}) {
323 $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
325 if ($form->{todate}) {
326 $todate = $locale->date(\%myconfig, $form->{todate}, 1);
329 $period = "$fromdate - $todate";
332 $period = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
335 push @options, $period;
337 $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
338 push (@options, $form->{print_date});
340 $form->{company} = $locale->text('Company') . " " . $myconfig{company};
341 push (@options, $form->{company});
343 my @columns = qw(transdate reference description gegenkonto debit credit ustkonto ustrate balance);
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 %'), },
356 my @hidden_variables = qw(accno fromdate todate description accounttype l_heading subtotal department projectnumber project_id sort method);
358 my $link = build_std_url('action=list_transactions', grep { $form->{$_} } @hidden_variables);
360 $form->{callback} = $link . '&sort=' . E($form->{sort});
362 my %column_alignment = map { $_ => 'right' } qw(debit credit balance);
364 my @custom_headers = ();
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, },
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, },
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'), },
398 my $report = SL::ReportGenerator->new(\%myconfig, $form);
399 $report->set_custom_headers(@custom_headers);
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,
407 $report->set_options_from_form();
408 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
410 $report->set_columns(%column_defs);
411 $report->set_column_order(@columns);
413 $report->set_export_options('list_transactions', @hidden_variables);
415 $report->set_sort_indicator($form->{sort}, 1);
417 $column_defs{balance}->{visible} = 1;
419 my $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
423 my %totals = ( 'debit' => 0, 'credit' => 0 );
424 my %subtotals = ( 'debit' => 0, 'credit' => 0 );
425 my ($previous_index, $row_set);
427 foreach my $ca (@{ $form->{CA} }) {
429 foreach (qw(debit credit)) {
430 $subtotals{$_} += $ca->{$_};
431 $totals{$_} += $ca->{$_};
432 if ($_ =~ /debit.*/) {
437 $form->{balance}= $form->{balance} + $ca->{$_} * $ml;
438 $ca->{$_} = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_} != 0);
442 if (($form->{subtotal})
443 && (($idx == scalar @{ $form->{CA} } - 1)
444 || ($ca->{$form->{sort}} ne $form->{CA}->[$idx + 1]->{$form->{sort}}))) {
450 $ca->{ustrate} = $form->format_amount(\%myconfig, $ca->{ustrate} * 100, 2) if ($ca->{ustrate} != 0);
452 if ($ca->{memo} ne "") {
453 $ca->{description} .= " \n " . $ca->{memo};
458 foreach my $gegenkonto (@{ $ca->{GEGENKONTO} }) {
459 if ($ca->{gegenkonto} eq "") {
460 $ca->{gegenkonto} = $gegenkonto->{accno};
462 $ca->{gegenkonto} .= ", " . $gegenkonto->{accno};
469 'align' => $column_alignment{$_},
473 $row->{balance}->{data} = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
475 if ($ca->{index} ne $previous_index) {
476 # $report->add_data($row_set) if ($row_set);
479 $previous_index = $ca->{index};
481 $row->{reference}->{link} = build_std_url("script=$ca->{module}.pl", 'action=edit', 'id=' . E($ca->{id}), 'callback');
483 } elsif ($ca->{index} eq $previous_index) {
484 map { $row->{$_}->{data} = '' } qw(reference description);
485 $row->{transdate}->{data} = '' if ($form->{sort} eq 'transdate');
490 push @{ $row_set }, $row;
492 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, 'listsubtotal') if ($do_subtotal);
496 $report->add_data($row_set);
500 $report->add_data($row_set) if ($row_set);
502 $report->add_separator();
504 my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, 'listtotal');
507 $row->{balance}->{data} = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
509 $report->add_data($row);
512 $report->add_separator();
516 'class' => 'listtotal',
519 'data' => $locale->text('EB-Wert'),
520 'class' => 'listtotal',
523 'data' => $locale->text('Saldo neu'),
525 'class' => 'listtotal',
528 'data' => $locale->text('Jahresverkehrszahlen neu'),
531 'class' => 'listtotal',
537 'class' => 'listtotal',
541 $report->add_data($row);
542 my $saldo_new = format_debit_credit($form->{saldo_new});
546 'class' => 'listtotal',
549 'data' => $eb_string,
550 'class' => 'listtotal',
553 'data' => $saldo_new,
555 'class' => 'listtotal',
558 'data' => $form->format_amount(\%myconfig, abs($form->{current_balance_debit}) , 2) . " S",
559 'class' => 'listtotal',
562 'data' => $form->format_amount(\%myconfig, $form->{current_balance_credit}, 2) . " H",
563 'class' => 'listtotal',
568 'class' => 'listtotal',
572 $report->add_data($row);
574 $report->generate_with_headers();
576 $main::lxdebug->leave_sub();
579 sub create_subtotal_row {
580 $main::lxdebug->enter_sub();
582 my $form = $main::form;
583 my %myconfig = %main::myconfig;
585 my ($totals, $columns, $column_alignment, $class) = @_;
587 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
589 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(credit debit);
591 map { $totals->{$_} = 0 } qw(debit credit);
593 $main::lxdebug->leave_sub();