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}";
158 print $::form->parse_html_template('ca/list', {
159 year => DateTime->today->year,
160 cash => $::instance_conf->get_accounting_method eq 'cash',
163 $::lxdebug->leave_sub;
166 sub format_debit_credit {
167 $main::lxdebug->enter_sub();
171 my $form = $main::form;
172 my %myconfig = %main::myconfig;
173 my $locale = $main::locale;
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)');
178 $main::lxdebug->leave_sub();
180 return $formatted_dc;
184 sub list_transactions {
185 $main::lxdebug->enter_sub();
187 my $form = $main::form;
188 my %myconfig = %main::myconfig;
189 my $locale = $main::locale;
191 $main::auth->assert('report');
193 $form->{title} = $locale->text('Account') . " $form->{accno} - $form->{description}";
195 if ($form->{reporttype} eq "custom") {
197 #forgotten the year --> thisyear
198 if ($form->{year} !~ m/^\d\d\d\d$/) {
199 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
205 if ($form->{duetyp} eq "13") {
206 $form->{fromdate} = "1.1.$form->{year}";
207 $form->{todate} = "31.12.$form->{year}";
211 if ($form->{duetyp} eq "A") {
212 $form->{fromdate} = "1.1.$form->{year}";
213 $form->{todate} = "31.3.$form->{year}";
215 if ($form->{duetyp} eq "B") {
216 $form->{fromdate} = "1.4.$form->{year}";
217 $form->{todate} = "30.6.$form->{year}";
219 if ($form->{duetyp} eq "C") {
220 $form->{fromdate} = "1.7.$form->{year}";
221 $form->{todate} = "30.9.$form->{year}";
223 if ($form->{duetyp} eq "D") {
224 $form->{fromdate} = "1.10.$form->{year}";
225 $form->{todate} = "31.12.$form->{year}";
230 $form->{duetyp} eq "1" && do {
231 $form->{fromdate} = "1.1.$form->{year}";
232 $form->{todate} = "31.1.$form->{year}";
235 $form->{duetyp} eq "2" && do {
236 $form->{fromdate} = "1.2.$form->{year}";
238 #this works from 1901 to 2099, 1900 and 2100 fail.
239 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
240 $form->{todate} = "$leap.2.$form->{year}";
243 $form->{duetyp} eq "3" && do {
244 $form->{fromdate} = "1.3.$form->{year}";
245 $form->{todate} = "31.3.$form->{year}";
248 $form->{duetyp} eq "4" && do {
249 $form->{fromdate} = "1.4.$form->{year}";
250 $form->{todate} = "30.4.$form->{year}";
253 $form->{duetyp} eq "5" && do {
254 $form->{fromdate} = "1.5.$form->{year}";
255 $form->{todate} = "31.5.$form->{year}";
258 $form->{duetyp} eq "6" && do {
259 $form->{fromdate} = "1.6.$form->{year}";
260 $form->{todate} = "30.6.$form->{year}";
263 $form->{duetyp} eq "7" && do {
264 $form->{fromdate} = "1.7.$form->{year}";
265 $form->{todate} = "31.7.$form->{year}";
268 $form->{duetyp} eq "8" && do {
269 $form->{fromdate} = "1.8.$form->{year}";
270 $form->{todate} = "31.8.$form->{year}";
273 $form->{duetyp} eq "9" && do {
274 $form->{fromdate} = "1.9.$form->{year}";
275 $form->{todate} = "30.9.$form->{year}";
278 $form->{duetyp} eq "10" && do {
279 $form->{fromdate} = "1.10.$form->{year}";
280 $form->{todate} = "31.10.$form->{year}";
283 $form->{duetyp} eq "11" && do {
284 $form->{fromdate} = "1.11.$form->{year}";
285 $form->{todate} = "30.11.$form->{year}";
288 $form->{duetyp} eq "12" && do {
289 $form->{fromdate} = "1.12.$form->{year}";
290 $form->{todate} = "31.12.$form->{year}";
296 CA->all_transactions(\%myconfig, \%$form);
298 $form->{saldo_old} += $form->{beginning_balance};
299 $form->{saldo_new} += $form->{beginning_balance};
300 my $saldo_old = format_debit_credit($form->{saldo_old});
301 my $eb_string = format_debit_credit($form->{beginning_balance});
302 $form->{balance} = $form->{saldo_old};
305 if ($form->{department}) {
306 my ($department) = split /--/, $form->{department};
307 push @options, $locale->text('Department') . " : $department";
309 if ($form->{projectnumber}) {
310 push @options, $locale->text('Project Number') . " : $form->{projectnumber}<br>";
314 if ($form->{fromdate} || $form->{todate}) {
315 my ($fromdate, $todate);
317 if ($form->{fromdate}) {
318 $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
320 if ($form->{todate}) {
321 $todate = $locale->date(\%myconfig, $form->{todate}, 1);
324 $period = "$fromdate - $todate";
327 $period = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
330 push @options, $period;
332 $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
333 push (@options, $form->{print_date});
335 $form->{company} = $locale->text('Company') . " " . $myconfig{company};
336 push (@options, $form->{company});
338 my @columns = qw(transdate reference description gegenkonto debit credit ustkonto ustrate balance);
340 'transdate' => { 'text' => $locale->text('Date'), },
341 'reference' => { 'text' => $locale->text('Reference'), },
342 'description' => { 'text' => $locale->text('Description'), },
343 'debit' => { 'text' => $locale->text('Debit'), },
344 'credit' => { 'text' => $locale->text('Credit'), },
345 'gegenkonto' => { 'text' => $locale->text('Gegenkonto'), },
346 'ustkonto' => { 'text' => $locale->text('USt-Konto'), },
347 'balance' => { 'text' => $locale->text('Balance'), },
348 'ustrate' => { 'text' => $locale->text('Satz %'), },
351 my @hidden_variables = qw(accno fromdate todate description accounttype l_heading subtotal department projectnumber project_id sort method);
353 my $link = build_std_url('action=list_transactions', grep { $form->{$_} } @hidden_variables);
355 $form->{callback} = $link . '&sort=' . E($form->{sort});
357 my %column_alignment = map { $_ => 'right' } qw(debit credit balance);
359 my @custom_headers = ();
361 push @custom_headers, [
362 { 'text' => 'Letzte Buchung', },
363 { 'text' => 'EB-Wert', },
364 { 'text' => 'Saldo alt', 'colspan' => 2, },
365 { 'text' => 'Jahresverkehrszahlen alt', 'colspan' => 2, },
366 { 'text' => '', 'colspan' => 2, },
368 push @custom_headers, [
369 { 'text' => $form->{last_transaction}, },
370 { 'text' => $eb_string, },
371 { 'text' => $saldo_old, 'colspan' => 2, },
372 { 'text' => $form->format_amount(\%myconfig, abs($form->{old_balance_debit}), 2) . " S", },
373 { 'text' => $form->format_amount(\%myconfig, $form->{old_balance_credit}, 2) . " H", },
374 { 'text' => '', 'colspan' => 2, },
377 push @custom_headers, [
378 { 'text' => $locale->text('Date'), 'link' => $link . "&sort=transdate", },
379 { 'text' => $locale->text('Reference'), 'link' => $link . "&sort=reference", },
380 { 'text' => $locale->text('Description'), 'link' => $link . "&sort=description", },
381 { 'text' => $locale->text('Gegenkonto'), },
382 { 'text' => $locale->text('Debit'), },
383 { 'text' => $locale->text('Credit'), },
384 { 'text' => $locale->text('USt-Konto'), },
385 { 'text' => $locale->text('Satz %'), },
386 { 'text' => $locale->text('Balance'), },
393 my $report = SL::ReportGenerator->new(\%myconfig, $form);
394 $report->set_custom_headers(@custom_headers);
396 $report->set_options('top_info_text' => join("\n", @options),
397 'output_format' => 'HTML',
398 'title' => $form->{title},
399 'attachment_basename' => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
400 'std_column_visibility' => 1,
402 $report->set_options_from_form();
403 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
405 $report->set_columns(%column_defs);
406 $report->set_column_order(@columns);
408 $report->set_export_options('list_transactions', @hidden_variables);
410 $report->set_sort_indicator($form->{sort}, 1);
412 $column_defs{balance}->{visible} = 1;
414 my $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
418 my %totals = ( 'debit' => 0, 'credit' => 0 );
419 my %subtotals = ( 'debit' => 0, 'credit' => 0 );
420 my ($previous_index, $row_set);
422 foreach my $ca (@{ $form->{CA} }) {
424 foreach (qw(debit credit)) {
425 $subtotals{$_} += $ca->{$_};
426 $totals{$_} += $ca->{$_};
427 if ($_ =~ /debit.*/) {
432 $form->{balance}= $form->{balance} + $ca->{$_} * $ml;
433 $ca->{$_} = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_} != 0);
437 if (($form->{subtotal})
438 && (($idx == scalar @{ $form->{CA} } - 1)
439 || ($ca->{$form->{sort}} ne $form->{CA}->[$idx + 1]->{$form->{sort}}))) {
445 $ca->{ustrate} = $form->format_amount(\%myconfig, $ca->{ustrate} * 100, 2) if ($ca->{ustrate} != 0);
447 if ($ca->{memo} ne "") {
448 $ca->{description} .= " \n " . $ca->{memo};
453 foreach my $gegenkonto (@{ $ca->{GEGENKONTO} }) {
454 if ($ca->{gegenkonto} eq "") {
455 $ca->{gegenkonto} = $gegenkonto->{accno};
457 $ca->{gegenkonto} .= ", " . $gegenkonto->{accno};
464 'align' => $column_alignment{$_},
468 $row->{balance}->{data} = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
470 if ($ca->{index} ne $previous_index) {
471 # $report->add_data($row_set) if ($row_set);
474 $previous_index = $ca->{index};
476 $row->{reference}->{link} = build_std_url("script=$ca->{module}.pl", 'action=edit', 'id=' . E($ca->{id}), 'callback');
478 } elsif ($ca->{index} eq $previous_index) {
479 map { $row->{$_}->{data} = '' } qw(reference description);
480 $row->{transdate}->{data} = '' if ($form->{sort} eq 'transdate');
485 push @{ $row_set }, $row;
487 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, 'listsubtotal') if ($do_subtotal);
491 $report->add_data($row_set);
495 $report->add_data($row_set) if ($row_set);
497 $report->add_separator();
499 my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, 'listtotal');
502 $row->{balance}->{data} = $form->format_amount(\%myconfig, $form->{balance}, 2, 'DRCR');
504 $report->add_data($row);
507 $report->add_separator();
511 'class' => 'listtotal',
514 'data' => $locale->text('EB-Wert'),
515 'class' => 'listtotal',
518 'data' => $locale->text('Saldo neu'),
520 'class' => 'listtotal',
523 'data' => $locale->text('Jahresverkehrszahlen neu'),
526 'class' => 'listtotal',
532 'class' => 'listtotal',
536 $report->add_data($row);
537 my $saldo_new = format_debit_credit($form->{saldo_new});
541 'class' => 'listtotal',
544 'data' => $eb_string,
545 'class' => 'listtotal',
548 'data' => $saldo_new,
550 'class' => 'listtotal',
553 'data' => $form->format_amount(\%myconfig, abs($form->{current_balance_debit}) , 2) . " S",
554 'class' => 'listtotal',
557 'data' => $form->format_amount(\%myconfig, $form->{current_balance_credit}, 2) . " H",
558 'class' => 'listtotal',
563 'class' => 'listtotal',
567 $report->add_data($row);
569 $report->generate_with_headers();
571 $main::lxdebug->leave_sub();
574 sub create_subtotal_row {
575 $main::lxdebug->enter_sub();
577 my $form = $main::form;
578 my %myconfig = %main::myconfig;
580 my ($totals, $columns, $column_alignment, $class) = @_;
582 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
584 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(credit debit);
586 map { $totals->{$_} = 0 } qw(debit credit);
588 $main::lxdebug->leave_sub();