1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
15 # Contributors: Benjamin Lee <benjaminlee@consultant.com>
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
30 #======================================================================
32 # backend code for reports
34 #======================================================================
40 use SL::DB::Helper::AccountingPeriod qw(get_balance_starting_date);
41 use List::Util qw(sum);
42 use List::UtilsBy qw(partition_by sort_by);
48 # new implementation of balance sheet
51 # stuff missing from the original implementation:
54 # - proper testing for heading charts
55 # - transmission from $form to TMPL realm is not as clear as i'd like
58 $main::lxdebug->enter_sub();
62 my $myconfig = \%main::myconfig;
63 my $form = $main::form;
64 my $dbh = $::form->get_standard_dbh;
67 my @categories = qw(A C L Q);
69 # if there are any dates construct a where
70 if ($form->{asofdate}) {
71 $form->{period} = $form->{this_period} = conv_dateq($form->{asofdate});
74 # get starting date for calculating balance
75 $form->{this_startdate} = $self->get_balance_starting_date($form->{asofdate});
77 get_accounts($dbh, $last_period, $form->{this_startdate}, $form->{asofdate}, $form, \@categories);
79 # if there are any compare dates
80 if ($form->{compareasofdate}) {
83 $form->{last_startdate} = $self->get_balance_starting_date($form->{compareasofdate});
85 get_accounts($dbh, $last_period, $form->{last_startdate} , $form->{compareasofdate}, $form, \@categories);
86 $form->{last_period} = conv_dateq($form->{compareasofdate});
89 # now we got $form->{A}{accno}{ } assets
90 # and $form->{L}{accno}{ } liabilities
91 # and $form->{Q}{accno}{ } equity
92 # build asset accounts
94 my %account = ('A' => { 'ml' => -1 },
96 'Q' => { 'ml' => 1 });
100 foreach my $category (grep { !/C/ } @categories) {
102 $TMPL_DATA->{$category} = [];
103 my $ml = $account{$category}{ml};
105 foreach my $key (sort keys %{ $form->{$category} }) {
107 my $row = { %{ $form->{$category}{$key} } };
109 # if charttype "heading" - calculate this entry, start a new batch of charts belonging to this heading and skip the rest bo the loop
110 # header charts are not real charts. start a sub aggregation with them, but don't calculate anything with them
111 if ($row->{charttype} eq "H") {
112 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
113 $row->{subdescription} = $account{$category}{subdescription};
114 $row->{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
115 $row->{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
118 $row->{subheader} = 1;
119 $account{$category}{subthis} = $row->{this};
120 $account{$category}{sublast} = $row->{last};
121 $account{$category}{subdescription} = $row->{description};
122 $account{$category}{subtotal} = 1;
127 next unless $form->{l_heading};
130 for my $period (qw(this last)) {
131 next if ($period eq 'last' && !$last_period);
133 $row->{$period} *= $ml;
136 push @{ $TMPL_DATA->{$category} }, $row;
139 # resolve heading/subtotal
140 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
141 $TMPL_DATA->{$category}[-1]{subdescription} = $account{$category}{subdescription};
142 $TMPL_DATA->{$category}[-1]{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
143 $TMPL_DATA->{$category}[-1]{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
146 $TMPL_DATA->{total}{$category}{this} = sum map { $_->{this} } @{ $TMPL_DATA->{$category} };
147 $TMPL_DATA->{total}{$category}{last} = sum map { $_->{last} } @{ $TMPL_DATA->{$category} };
150 for my $period (qw(this last)) {
151 next if ($period eq 'last' && !$last_period);
153 $form->{E}{$period} = $TMPL_DATA->{total}{A}{$period} - $TMPL_DATA->{total}{L}{$period} - $TMPL_DATA->{total}{Q}{$period};
154 $TMPL_DATA->{total}{Q}{$period} += $form->{E}{$period};
155 $TMPL_DATA->{total}{$period} = $TMPL_DATA->{total}{L}{$period} + $TMPL_DATA->{total}{Q}{$period};
157 $form->{E}{description}='nicht verbuchter Gewinn/Verlust';
158 push @{ $TMPL_DATA->{Q} }, $form->{E};
160 $main::lxdebug->leave_sub();
166 $main::lxdebug->enter_sub();
168 my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
170 my ($null, $department_id) = split /--/, $form->{department};
174 my $dpt_where_without_arapgl = '';
181 my $dec = $form->{decimalplaces};
183 my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |;
187 qq|SELECT c.accno, c.description, c.category
189 WHERE (c.charttype = 'H')
193 $sth = prepare_execute_query($form, $dbh, $query);
195 my @headingaccounts = ();
196 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
197 $form->{ $ref->{category} }{ $ref->{accno} }{description} =
198 "$ref->{description}";
199 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
200 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
202 push @headingaccounts, $ref->{accno};
207 # filter for opening and closing bookings
208 # if l_ob is selected l_cb is always ignored
209 if ( $last_period ) {
210 # ob/cb-settings for "compared to" balance
211 if ( $form->{l_ob_compared} ) {
212 $where .= ' AND ac.ob_transaction is true '
213 } elsif ( not $form->{l_cb_compared} ) {
214 $where .= ' AND ac.cb_transaction is false ';
217 # ob/cb-settings for "as of" balance
218 if ( $form->{l_ob} ) {
219 $where .= ' AND ac.ob_transaction is true '
220 } elsif ( not $form->{l_cb} ) {
221 $where .= ' AND ac.cb_transaction is false ';
227 $fromdate = conv_dateq($fromdate);
228 if ($form->{method} eq 'cash') {
229 $subwhere .= " AND (transdate >= $fromdate)";
230 $glwhere = " AND (ac.transdate >= $fromdate)";
232 $where .= " AND (ac.transdate >= $fromdate)";
237 $todate = conv_dateq($todate);
238 $where .= " AND (ac.transdate <= $todate)";
239 $subwhere .= " AND (transdate <= $todate)";
242 if ($department_id) {
243 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
246 if ($form->{project_id}) {
247 # Diese Bedingung wird derzeit niemals wahr sein, da man in Bericht->Bilanz keine
248 # Projekte auswählen kann
249 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
252 if ($form->{method} eq 'cash') {
254 qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
256 JOIN chart c ON (c.id = ac.chart_id)
257 JOIN ar a ON (a.id = ac.trans_id)
265 WHERE (a.chart_link LIKE '%AR_paid%')
269 GROUP BY c.accno, c.description, c.category
273 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
275 JOIN chart c ON (c.id = ac.chart_id)
276 JOIN ap a ON (a.id = ac.trans_id)
284 WHERE (a.chart_link LIKE '%AP_paid%')
288 GROUP BY c.accno, c.description, c.category
292 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
294 JOIN chart c ON (c.id = ac.chart_id)
295 JOIN gl a ON (a.id = ac.trans_id)
300 AND NOT ((ac.chart_link = 'AR') OR (ac.chart_link = 'AP'))
302 GROUP BY c.accno, c.description, c.category |;
304 if ($form->{project_id}) {
305 # s.o. keine Projektauswahl in Bilanz
310 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
312 JOIN ar a ON (a.id = ac.trans_id)
313 JOIN parts p ON (ac.parts_id = p.id)
314 JOIN taxzone_charts t ON (p.buchungsgruppen_id = t.id)
315 JOIN chart c on (t.income_accno_id = c.id)
316 -- use transdate from subwhere
317 WHERE (c.category = 'I')
324 WHERE (a.chart_link LIKE '%AR_paid%')
328 GROUP BY c.accno, c.description, c.category
332 SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
334 JOIN ap a ON (a.id = ac.trans_id)
335 JOIN parts p ON (ac.parts_id = p.id)
336 JOIN taxzone_charts t ON (p.buchungsgruppen_id = t.id)
337 JOIN chart c on (t.expense_accno_id = c.id)
338 WHERE (c.category = 'E')
345 WHERE a.chart_link LIKE '%AP_paid%'
349 GROUP BY c.accno, c.description, c.category |;
352 } else { # if ($form->{method} eq 'cash')
353 if ($department_id) {
354 $dpt_where = qq| AND a.department_id = | . conv_i($department_id);
355 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
356 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
357 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
361 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
363 JOIN chart c ON (c.id = ac.chart_id)
365 $dpt_where_without_arapgl
368 GROUP BY c.accno, c.description, c.category |;
370 if ($form->{project_id}) {
371 # s.o. keine Projektauswahl in Bilanz
375 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
377 JOIN ar a ON (a.id = ac.trans_id)
378 JOIN parts p ON (ac.parts_id = p.id)
379 JOIN taxzone_charts t ON (p.buchungsgruppen_id = t.id)
380 JOIN chart c on (t.income_accno_id = c.id)
381 -- use transdate from subwhere
382 WHERE (c.category = 'I')
386 GROUP BY c.accno, c.description, c.category
390 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
392 JOIN ap a ON (a.id = ac.trans_id)
393 JOIN parts p ON (ac.parts_id = p.id)
394 JOIN taxzone_charts t ON (p.buchungsgruppen_id = t.id)
395 JOIN chart c on (t.expense_accno_id = c.id)
396 WHERE (c.category = 'E')
400 GROUP BY c.accno, c.description, c.category |;
408 $sth = prepare_execute_query($form, $dbh, $query);
410 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
412 if ($ref->{category} eq 'C') {
413 $ref->{category} = 'A';
416 # get last heading account
417 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
421 $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
423 $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
427 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
428 $form->{ $ref->{category} }{ $ref->{accno} }{description} = $ref->{description};
429 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
432 $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
434 $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
439 # remove accounts with zero balance
440 foreach $category (@{$categories}) {
441 foreach $accno (keys %{ $form->{$category} }) {
442 $form->{$category}{$accno}{last} = $form->round_amount($form->{$category}{$accno}{last}, $dec);
443 $form->{$category}{$accno}{this} = $form->round_amount($form->{$category}{$accno}{this}, $dec);
445 delete $form->{$category}{$accno}
446 if ( $form->{$category}{$accno}{this} == 0
447 && $form->{$category}{$accno}{last} == 0);
451 $main::lxdebug->leave_sub();
455 $main::lxdebug->enter_sub();
457 my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
459 my ($null, $department_id) = split /--/, $form->{department};
463 my $dpt_where_without_arapgl;
472 $where .= ' AND ac.cb_transaction is false ' unless $form->{l_cb};
475 $fromdate = conv_dateq($fromdate);
476 if ($form->{method} eq 'cash') {
477 $subwhere .= " AND (transdate >= $fromdate)";
478 $glwhere = " AND (ac.transdate >= $fromdate)";
479 $prwhere = " AND (a.transdate >= $fromdate)";
480 $inwhere = " AND (acc.transdate >= $fromdate)";
482 $where .= " AND (ac.transdate >= $fromdate)";
483 # hotfix for projectfilter in guv and bwa
484 # fromdate is otherwise ignored if project is selected
485 $prwhere = " AND (a.transdate >= $fromdate)";
490 $todate = conv_dateq($todate);
491 $subwhere .= " AND (transdate <= $todate)";
492 $where .= " AND (ac.transdate <= $todate)";
493 $prwhere .= " AND (a.transdate <= $todate)";
494 $inwhere .= " AND (acc.transdate <= $todate)";
497 if ($department_id) {
498 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
501 if ($form->{project_id}) {
502 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
506 # GUV patch by Ronny Rentner (Bug 1190)
508 # GUV IST-Versteuerung
510 # Alle tatsaechlichen _Zahlungseingaenge_
511 # im Zeitraum erfassen
512 # (Teilzahlungen werden prozentual auf verschiedene Steuern aufgeteilt)
516 if ($form->{method} eq 'cash') {
519 SELECT SUM( ac.amount * CASE WHEN COALESCE((SELECT amount FROM ar a WHERE id = ac.trans_id $dpt_where), 0) != 0 THEN
520 /* ar amount is not zero, so we can divide by amount */
521 (SELECT SUM(acc.amount) * -1
524 AND acc.trans_id = ac.trans_id
525 AND acc.chart_link LIKE '%AR_paid%')
526 / (SELECT amount FROM ar WHERE id = ac.trans_id)
528 /* ar amount is zero, or we are checking with a non-ar-transaction, so we return 0 in both cases as multiplicator of ac.amount */
530 ) AS amount, c.$category, c.accno, c.description
532 LEFT JOIN chart c ON (c.id = ac.chart_id)
533 LEFT JOIN ar ON (ar.id = ac.trans_id)
534 WHERE ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE 1=1 $subwhere)
536 GROUP BY c.$category, c.accno, c.description
539 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
541 JOIN chart c ON (c.id = ac.chart_id)
542 JOIN ar a ON (a.id = ac.trans_id)
543 WHERE $where $dpt_where
544 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AR_paid%') $subwhere)
546 GROUP BY c.$category, c.accno, c.description
550 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
552 JOIN chart c ON (c.id = ac.chart_id)
553 JOIN ap a ON (a.id = ac.trans_id)
554 WHERE $where $dpt_where
555 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AP_paid%') $subwhere)
557 GROUP BY c.$category, c.accno, c.description
561 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
563 JOIN chart c ON (c.id = ac.chart_id)
564 JOIN gl a ON (a.id = ac.trans_id)
565 WHERE $where $dpt_where $glwhere
566 AND NOT ((ac.chart_link = 'AR') OR (ac.chart_link = 'AP'))
568 GROUP BY c.$category, c.accno, c.description
571 if ($form->{project_id}) {
575 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
577 JOIN ar a ON (a.id = ac.trans_id)
578 JOIN parts p ON (ac.parts_id = p.id)
579 JOIN taxzone_charts t ON (p.buchungsgruppen_id = t.id)
580 JOIN chart c on (t.income_accno_id = c.id)
581 WHERE (c.category = 'I') $prwhere $dpt_where
582 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AR_paid%') $subwhere)
584 GROUP BY c.$category, c.accno, c.description
588 SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
590 JOIN ap a ON (a.id = ac.trans_id)
591 JOIN parts p ON (ac.parts_id = p.id)
592 JOIN taxzone_charts t ON (p.buchungsgruppen_id = t.id)
593 JOIN chart c on (t.expense_accno_id = c.id)
594 WHERE (c.category = 'E') $prwhere $dpt_where
595 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AP_paid%') $subwhere)
597 GROUP BY c.$category, c.accno, c.description
601 } else { # if ($form->{method} eq 'cash')
602 if ($department_id) {
603 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
604 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
605 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
606 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
610 SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
612 JOIN chart c ON (c.id = ac.chart_id)
614 $dpt_where_without_arapgl
616 GROUP BY c.$category, c.accno, c.description |;
618 if ($form->{project_id}) {
622 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
624 JOIN ar a ON (a.id = ac.trans_id)
625 JOIN parts p ON (ac.parts_id = p.id)
626 JOIN taxzone_charts t ON (p.buchungsgruppen_id = t.id)
627 JOIN chart c on (t.income_accno_id = c.id)
628 WHERE (c.category = 'I')
632 GROUP BY c.$category, c.accno, c.description
636 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
638 JOIN ap a ON (a.id = ac.trans_id)
639 JOIN parts p ON (ac.parts_id = p.id)
640 JOIN taxzone_charts t ON (p.buchungsgruppen_id = t.id)
641 JOIN chart c on (t.expense_accno_id = c.id)
642 WHERE (c.category = 'E')
646 GROUP BY c.$category, c.accno, c.description |;
654 # store information for chart list in $form->{charts}
655 foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
656 unless ( defined $form->{charts}->{$ref->{accno}} ) {
657 # a chart may appear several times in the resulting hashref, init it the first time
658 $form->{charts}->{$ref->{accno}} = { amount => 0,
659 "$category" => $ref->{"$category"},
660 accno => $ref->{accno},
661 description => $ref->{description},
664 if ($category eq "pos_bwa") {
666 $form->{ $ref->{$category} }{kumm} += $ref->{amount};
668 $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
669 # only increase chart amount for current period, not last_period
670 $form->{charts}->{$ref->{accno}}->{amount} += $ref->{amount},
673 $form->{ $ref->{$category} } += $ref->{amount};
674 $form->{charts}->{$ref->{accno}}->{amount} += $ref->{amount}; # no last_period for eur
678 $main::lxdebug->leave_sub();
682 $main::lxdebug->enter_sub();
684 my ($self, $myconfig, $form, %options) = @_;
686 my $dbh = SL::DB->client->dbh;
688 my ($query, $sth, $ref);
691 my ($null, $department_id) = split /--/, $form->{department};
692 my @headingaccounts = ();
694 my $dpt_where_without_arapgl;
695 my ($customer_where, $customer_join, $customer_no_union);
699 my $invwhere = $where;
701 if ($department_id) {
702 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
703 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
704 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
705 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
707 if ($form->{customer_id}) {
708 $customer_join = qq| JOIN ar a ON (ac.trans_id = a.id) |;
709 $customer_where = qq| AND (a.customer_id = | . conv_i($form->{customer_id}, 'NULL') . qq|) |;
710 $customer_no_union = qq| AND 1=0 |;
713 # project_id only applies to getting transactions
714 # it has nothing to do with a trial balance
715 # but we use the same function to collect information
717 if ($form->{project_id}) {
718 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
721 my $acc_cash_where = "";
722 # my $ar_cash_where = "";
723 # my $ap_cash_where = "";
726 if ($form->{method} eq "cash") {
728 qq| AND (ac.trans_id IN (
731 WHERE datepaid >= '$form->{fromdate}'
732 AND datepaid <= '$form->{todate}'
738 WHERE datepaid >= '$form->{fromdate}'
739 AND datepaid <= '$form->{todate}'
745 WHERE transdate >= '$form->{fromdate}'
746 AND transdate <= '$form->{todate}'
748 # $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
751 if ($options{beginning_balances}) {
752 foreach my $prefix (qw(from to)) {
753 next if ($form->{"${prefix}date"});
755 my $min_max = $prefix eq 'from' ? 'min' : 'max';
756 $query = qq|SELECT ${min_max}(transdate)
760 $dpt_where_without_arapgl
764 ($form->{"${prefix}date"}) = selectfirst_array_query($form, $dbh, $query);
767 # get beginning balances
769 qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
771 LEFT JOIN chart c ON (ac.chart_id = c.id)
773 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
774 $dpt_where_without_arapgl
777 GROUP BY c.accno, c.category, c.description |;
779 $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
781 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
783 if ($ref->{amount} != 0 || $form->{all_accounts}) {
784 $trb{ $ref->{accno} }{description} = $ref->{description};
785 $trb{ $ref->{accno} }{charttype} = 'A';
786 $trb{ $ref->{accno} }{beginning_balance} = $ref->{amount};
788 if ($ref->{amount} > 0) {
789 $trb{ $ref->{accno} }{haben_eb} = $ref->{amount};
791 $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1;
793 $trb{ $ref->{accno} }{category} = $ref->{category};
802 qq|SELECT c.accno, c.description, c.category
804 WHERE c.charttype = 'H'
807 $sth = prepare_execute_query($form, $dbh, $query);
809 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
810 $trb{ $ref->{accno} }{description} = $ref->{description};
811 $trb{ $ref->{accno} }{charttype} = 'H';
812 $trb{ $ref->{accno} }{category} = $ref->{category};
814 push @headingaccounts, $ref->{accno};
820 my $saldowhere = " 1 = 1 ";
821 my $sumwhere = " 1 = 1 ";
823 my $sumsubwhere = '';
824 my $saldosubwhere = '';
825 my $glsaldowhere = '';
830 my ($fromdate, $todate, $fetch_accounts_before_from);
832 if ($form->{fromdate} || $form->{todate}) {
833 if ($form->{fromdate}) {
834 $fromdate = conv_dateq($form->{fromdate});
835 $tofrom .= " AND (ac.transdate >= $fromdate)";
836 $subwhere .= " AND (ac.transdate >= $fromdate)";
837 $sumsubwhere .= " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
838 $saldosubwhere .= " AND (ac,transdate>=(select date_trunc('year', date $fromdate))) ";
839 $invwhere .= " AND (a.transdate >= $fromdate)";
840 $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
841 $glwhere = " AND (ac.transdate >= $fromdate)";
842 $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
844 if ($form->{todate}) {
845 $todate = conv_dateq($form->{todate});
846 $tofrom .= " AND (ac.transdate <= $todate)";
847 $invwhere .= " AND (a.transdate <= $todate)";
848 $saldosubwhere .= " AND (ac.transdate <= $todate)";
849 $sumsubwhere .= " AND (ac.transdate <= $todate)";
850 $subwhere .= " AND (ac.transdate <= $todate)";
851 $glwhere .= " AND (ac.transdate <= $todate)";
852 $glsumwhere .= " AND (ac.transdate <= $todate) ";
853 $glsaldowhere .= " AND (ac.transdate <= $todate) ";
857 if ($form->{method} eq "cash") {
859 qq| AND(ac.trans_id IN (SELECT id FROM ar WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM ap WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM gl WHERE transdate>= $fromdate AND transdate<= $todate)) AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL) |;
860 $saldowhere .= qq| AND(ac.trans_id IN (SELECT id FROM ar WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM ap WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM gl WHERE transdate>= $fromdate AND transdate<= $todate)) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL) |;
862 $sumwhere .= qq| AND(ac.trans_id IN (SELECT id FROM ar WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM ap WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM gl WHERE transdate>= $fromdate AND transdate<= $todate)) AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL) |;
864 $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
865 $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
866 $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
868 # get all entries before fromdate, which are not yet fetched
869 # TODO dpt_where_without_arapgl and project - project calculation seems bogus anyway
870 # TODO use fiscal_year_startdate for the whole trial balance
871 # anyway, if the last booking is in a deviating fiscal year, this already improves the query
872 my $fiscal_year_startdate = conv_dateq($self->get_balance_starting_date($form->{fromdate}));
873 $fetch_accounts_before_from = qq|SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
874 FROM acc_trans ac JOIN chart c ON (c.id = ac.chart_id) WHERE 1 = 1 AND (ac.transdate <= $fromdate)
875 AND (ac.transdate >= $fiscal_year_startdate)
876 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)
877 AND c.accno NOT IN (SELECT c.accno FROM acc_trans ac JOIN chart c ON (c.id = ac.chart_id) WHERE 1 = 1 AND (ac.transdate >= $fromdate) AND (ac.transdate <= $todate)
878 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL))
879 GROUP BY c.accno, c.description, c.category ORDER BY accno|;
883 SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
885 JOIN chart c ON (c.id = ac.chart_id)
888 $dpt_where_without_arapgl
890 GROUP BY c.accno, c.description, c.category |;
892 if ($form->{project_id}) {
894 -- add project transactions from invoice
898 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
900 JOIN ar a ON (ac.trans_id = a.id)
901 JOIN parts p ON (ac.parts_id = p.id)
902 JOIN taxzone_charts t ON (p.buchungsgruppen_id = t.id)
903 JOIN chart c ON (t.income_accno_id = c.id)
908 GROUP BY c.accno, c.description, c.category
912 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
914 JOIN ap a ON (ac.trans_id = a.id)
915 JOIN parts p ON (ac.parts_id = p.id)
916 JOIN taxzone_charts t ON (p.buchungsgruppen_id = t.id)
917 JOIN chart c ON (t.expense_accno_id = c.id)
922 GROUP BY c.accno, c.description, c.category
926 $query .= qq| ORDER BY accno|;
928 $sth = prepare_execute_query($form, $dbh, $query);
930 # calculate the debit and credit in the period
931 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
932 $trb{ $ref->{accno} }{description} = $ref->{description};
933 $trb{ $ref->{accno} }{charttype} = 'A';
934 $trb{ $ref->{accno} }{category} = $ref->{category};
935 $trb{ $ref->{accno} }{amount} += $ref->{amount};
939 if ($form->{method} ne "cash") { # better eq 'accrual'
940 $sth = prepare_execute_query($form, $dbh, $fetch_accounts_before_from);
941 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
942 $trb{ $ref->{accno} }{description} = $ref->{description};
943 $trb{ $ref->{accno} }{charttype} = 'A';
944 $trb{ $ref->{accno} }{category} = $ref->{category};
945 $trb{ $ref->{accno} }{amount} += $ref->{amount};
950 # prepare query for each account
951 my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
955 (SELECT SUM(ac.amount) * -1
957 JOIN chart c ON (c.id = ac.chart_id)
960 $dpt_where_without_arapgl
964 AND (c.accno = ?)) AS debit,
966 (SELECT SUM(ac.amount)
968 JOIN chart c ON (c.id = ac.chart_id)
971 $dpt_where_without_arapgl
975 AND c.accno = ?) AS credit,
976 (SELECT SUM(ac.amount)
978 JOIN chart c ON (ac.chart_id = c.id)
981 $dpt_where_without_arapgl
984 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
986 (SELECT SUM(ac.amount)
988 JOIN chart c ON (ac.chart_id = c.id)
991 $dpt_where_without_arapgl
995 AND c.accno = ?) AS sum_credit,
997 (SELECT SUM(ac.amount)
999 JOIN chart c ON (ac.chart_id = c.id)
1002 $dpt_where_without_arapgl
1006 AND c.accno = ?) AS sum_debit,
1008 (SELECT max(ac.transdate) FROM acc_trans ac
1009 JOIN chart c ON (ac.chart_id = c.id)
1012 $dpt_where_without_arapgl
1015 AND c.accno = ?) AS last_transaction
1020 $drcr = prepare_query($form, $dbh, $q_drcr);
1022 if ($form->{project_id}) {
1023 # prepare query for each account
1026 (SELECT SUM(ac.sellprice * ac.qty) * -1
1028 JOIN parts p ON (ac.parts_id = p.id)
1029 JOIN ap a ON (ac.trans_id = a.id)
1030 JOIN taxzone_charts t ON (p.buchungsgruppen_id = t.id)
1031 JOIN chart c ON (t.expense_accno_id = c.id)
1036 AND c.accno = ?) AS debit,
1038 (SELECT SUM(ac.sellprice * ac.qty)
1040 JOIN parts p ON (ac.parts_id = p.id)
1041 JOIN ar a ON (ac.trans_id = a.id)
1042 JOIN taxzone_charts t ON (p.buchungsgruppen_id = t.id)
1043 JOIN chart c ON (t.income_accno_id = c.id)
1048 AND c.accno = ?) AS credit,
1050 (SELECT SUM(ac.amount)
1052 JOIN chart c ON (ac.chart_id = c.id)
1055 $dpt_where_without_arapgl
1059 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
1061 (SELECT SUM(ac.amount)
1063 JOIN chart c ON (ac.chart_id = c.id)
1066 $dpt_where_without_arapgl
1071 AND c.accno = ?) AS sum_credit,
1073 (SELECT SUM(ac.amount)
1075 JOIN chart c ON (ac.chart_id = c.id)
1079 $dpt_where_without_arapgl
1083 AND c.accno = ?) AS sum_debit,
1086 (SELECT max(ac.transdate) FROM acc_trans ac
1087 JOIN chart c ON (ac.chart_id = c.id)
1090 $dpt_where_without_arapgl
1093 AND c.accno = ?) AS last_transaction
1096 $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
1100 my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
1102 foreach my $accno (sort keys %trb) {
1105 $ref->{accno} = $accno;
1106 map { $ref->{$_} = $trb{$accno}{$_} }
1107 qw(description category charttype amount soll_eb haben_eb beginning_balance);
1109 $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
1111 if ($trb{$accno}{charttype} eq 'A') {
1114 do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1116 ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
1117 my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
1118 $last_transaction = "";
1119 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
1120 $ref->{debit} += $debit;
1121 $ref->{credit} += $credit;
1123 $ref->{haben_saldo} += $saldo;
1125 $ref->{soll_saldo} += $saldo * -1;
1127 $ref->{last_transaction} = $last_transaction;
1128 $ref->{soll_kumuliert} = $soll_kumuliert * -1;
1129 $ref->{haben_kumuliert} = $haben_kumuliert;
1133 if ($form->{project_id}) {
1136 do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1138 ($debit, $credit) = (0, 0);
1139 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
1140 $ref->{debit} += $debit;
1141 $ref->{credit} += $credit;
1143 $ref->{haben_saldo} += $saldo;
1145 $ref->{soll_saldo} += $saldo * -1;
1147 $ref->{soll_kumuliert} += $soll_kumuliert * -1;
1148 $ref->{haben_kumuliert} += $haben_kumuliert;
1150 $project_drcr->finish;
1153 $ref->{debit} = $form->round_amount($ref->{debit}, 2);
1154 $ref->{credit} = $form->round_amount($ref->{credit}, 2);
1156 if ($ref->{haben_saldo} != 0) {
1157 $ref->{haben_saldo} = $ref->{haben_saldo} + $ref->{beginning_balance};
1158 if ($ref->{haben_saldo} < 0) {
1159 $ref->{soll_saldo} = $form->round_amount(($ref->{haben_saldo} *- 1), 2);
1160 $ref->{haben_saldo} = 0;
1163 $ref->{soll_saldo} = $ref->{soll_saldo} - $ref->{beginning_balance};
1164 if ($ref->{soll_saldo} < 0) {
1165 $ref->{haben_saldo} = $form->round_amount(($ref->{soll_saldo} * -1), 2);
1166 $ref->{soll_saldo} = 0;
1169 $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
1170 $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
1171 $ref->{haben_kumuliert} = $form->round_amount($ref->{haben_kumuliert}, 2);
1172 $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
1177 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1178 $accno = pop @accno;
1180 $trb{$accno}{debit} += $ref->{debit};
1181 $trb{$accno}{credit} += $ref->{credit};
1182 $trb{$accno}{soll_saldo} += $ref->{soll_saldo};
1183 $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
1184 $trb{$accno}{soll_kumuliert} += $ref->{soll_kumuliert};
1185 $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
1188 push @{ $form->{TB} }, $ref;
1192 # debits and credits for headings
1193 foreach my $accno (@headingaccounts) {
1194 foreach $ref (@{ $form->{TB} }) {
1195 if ($accno eq $ref->{accno}) {
1196 $ref->{debit} = $trb{$accno}{debit};
1197 $ref->{credit} = $trb{$accno}{credit};
1198 $ref->{soll_saldo} = $trb{$accno}{soll_saldo};
1199 $ref->{haben_saldo} = $trb{$accno}{haben_saldo};
1200 $ref->{soll_kumuliert} = $trb{$accno}{soll_kumuliert};
1201 $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert};
1206 $main::lxdebug->leave_sub();
1210 $main::lxdebug->enter_sub();
1211 my ($self, $dbh, $form) = @_;
1212 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
1213 my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
1214 my $sth = $dbh->prepare($query);
1215 while(my $ref = $sth->fetchrow_hashref()) {
1216 $ref->{invnumer} =~ s/Storno zu //g;
1217 $form->{storno}{$ref->{invnumber}} = 1;
1219 $main::lxdebug->leave_sub();
1223 $main::lxdebug->enter_sub();
1225 my ($self, $myconfig, $form) = @_;
1227 # connect to database
1228 my $dbh = SL::DB->client->dbh;
1230 my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
1232 # falls customer ziehen wir die offene forderungsliste
1233 # anderfalls für die lieferanten die offenen verbindlichkeitne
1234 if ($form->{ct} eq "customer") {
1247 $ct_id = "${ct}_id";
1249 # erweiterung um einen freien zeitraum oder einen stichtag
1250 # mit entsprechender altersstrukturliste (s.a. Bug 1842)
1251 # eine neue variable an der oberfläche eingeführt, somit ist
1252 # todate == freier zeitrau und fordate == stichtag
1253 # duedate_where == nur fällige rechnungen anzeigen
1255 my ($review_of_aging_list, $todate, $fromdate, $fromwhere, $fordate,
1258 if ($form->{reporttype} eq 'custom') { # altersstrukturliste, nur fällige
1260 # explizit rausschmeissen was man für diesen bericht nicht braucht
1261 delete $form->{fromdate};
1262 delete $form->{todate};
1264 # an der oberfläche ist das tagesaktuelle datum vorausgewählt
1265 # falls es dennoch per Benutzereingabe gelöscht wird, lieber wieder vorbelegen
1266 # ferner muss für die spätere DB-Abfrage muss todate gesetzt sein.
1267 $form->{fordate} = $form->current_date($myconfig) unless ($form->{fordate});
1268 $fordate = conv_dateq($form->{fordate});
1271 if ($form->{review_of_aging_list}) { # falls die liste leer ist, alles anzeigen
1272 if ($form->{review_of_aging_list} =~ m "-") { # .. periode von bis
1273 my @period = split(/-/, $form->{review_of_aging_list}); # ... von periode bis periode
1274 $review_of_aging_list = " AND $period[0] < (date $fordate) - duedate
1275 AND (date $fordate) - duedate < $period[1]";
1277 $form->{review_of_aging_list} =~ s/[^0-9]//g; # größer 120 das substitute ist nur für das '>' zeichen
1278 $review_of_aging_list = " AND $form->{review_of_aging_list} < (date $fordate) - duedate";
1281 $duedate_where = " AND (date $fordate) - duedate >= 0 ";
1282 } else { # freier zeitraum, nur rechnungsdatum und OHNE review_of_aging_list
1283 $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
1284 $todate = conv_dateq($form->{todate});
1285 $fromdate = conv_dateq($form->{fromdate});
1286 $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
1288 my $where = " 1 = 1 ";
1291 if ($form->{$ct_id}) {
1292 $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
1293 } elsif ($form->{ $form->{ct} }) {
1294 $where .= qq| AND (ct.name ILIKE | . $dbh->quote(like($form->{$ct})) . qq|)|;
1299 if ($form->{department}) {
1300 my ($null, $department_id) = split /--/, $form->{department};
1301 $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
1302 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1303 $where_dpt = qq| AND (${arap}.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1307 SELECT ${ct}.id AS ctid, ${ct}.name,
1308 street, zipcode, city, country, contact, email,
1309 phone as customerphone, fax as customerfax, ${ct}number,
1310 "invnumber", "transdate",
1311 (amount - COALESCE((SELECT sum(amount)*$ml FROM acc_trans WHERE chart_link ilike '%paid%' AND acc_trans.trans_id=${arap}.id AND acc_trans.transdate <= (date $todate)),0)) as "open", "amount",
1312 "duedate", invoice, ${arap}.id, date_part('days', now() - duedate) as overduedays,
1315 WHERE (${arap}.currency_id = exchangerate.currency_id)
1316 AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
1318 WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
1319 AND NOT COALESCE (${arap}.storno, 'f')
1320 AND (${arap}.${ct}_id = ${ct}.id)
1323 AND (transdate <= (date $todate) $fromwhere )
1324 $review_of_aging_list
1326 ORDER BY ctid, transdate, invnumber |;
1328 my $sth_details = prepare_query($form, $dbh, $q_details);
1330 # select outstanding vendors or customers, depends on $ct
1332 qq|SELECT DISTINCT ct.id, ct.name
1333 FROM $ct ct, $arap a
1336 AND (a.${ct_id} = ct.id)
1337 AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
1338 AND (a.transdate <= $todate $fromwhere)
1341 my $sth = prepare_execute_query($form, $dbh, $query);
1344 # for each company that has some stuff outstanding
1345 while (my ($id) = $sth->fetchrow_array) {
1346 do_statement($form, $sth_details, $q_details, $id);
1348 while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
1349 $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
1350 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
1351 push @{ $form->{AG} }, $ref;
1354 $sth_details->finish;
1360 $main::lxdebug->leave_sub();
1364 $main::lxdebug->enter_sub();
1366 my ($self, $myconfig, $form) = @_;
1368 my $dbh = SL::DB->client->dbh;
1370 my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
1373 qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
1376 ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
1377 selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
1379 $main::lxdebug->leave_sub();
1383 $main::lxdebug->enter_sub();
1385 my ($self, $myconfig, $form) = @_;
1387 my $dbh = SL::DB->client->dbh;
1389 my ($null, $department_id) = split /--/, $form->{department};
1392 my $where = "1 = 1";
1394 if ($department_id) {
1395 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
1400 if ($form->{accno}) {
1401 $accno = $form->{accno};
1402 $rate = $form->{"$form->{accno}_rate"};
1403 $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
1409 if ($form->{db} eq 'ar') {
1410 $table = "customer";
1417 my $arap = lc($ARAP);
1419 my $transdate = "a.transdate";
1421 if ($form->{method} eq 'cash') {
1422 $transdate = "a.datepaid";
1424 my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
1431 WHERE (a.chart_link LIKE '%${ARAP}_paid%')
1432 AND (transdate <= $todate)
1437 # if there are any dates construct a where
1438 $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
1439 $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
1441 my $ml = ($form->{db} eq 'ar') ? 1 : -1;
1443 my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
1444 $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
1447 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
1448 ac.amount * $ml AS tax
1450 JOIN ${arap} a ON (a.id = ac.trans_id)
1451 JOIN chart ch ON (ch.id = ac.chart_id)
1452 JOIN $table n ON (n.id = a.${table}_id)
1456 AND (a.invoice = '0')
1460 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
1461 i.sellprice * i.qty * $rate * $ml AS tax
1463 JOIN ${arap} a ON (a.id = ac.trans_id)
1464 JOIN chart ch ON (ch.id = ac.chart_id)
1465 JOIN $table n ON (n.id = a.${table}_id)
1466 JOIN ${table}tax t ON (t.${table}_id = n.id)
1467 JOIN invoice i ON (i.trans_id = a.id)
1471 AND (a.invoice = '1')
1472 ORDER BY $sortorder|;
1474 $form->{TR} = selectall_hashref_query($form, $dbh, $query);
1476 $main::lxdebug->leave_sub();
1479 sub paymentaccounts {
1480 $main::lxdebug->enter_sub();
1482 my ($self, $myconfig, $form) = @_;
1484 # connect to database, turn AutoCommit off
1485 my $dbh = SL::DB->client->dbh;
1487 my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
1489 # get A(R|P)_paid accounts
1491 qq|SELECT accno, description
1493 WHERE link LIKE '%${ARAP}_paid%'|;
1494 $form->{PR} = selectall_hashref_query($form, $dbh, $query);
1496 $main::lxdebug->leave_sub();
1500 $main::lxdebug->enter_sub();
1502 my ($self, $myconfig, $form) = @_;
1504 # connect to database, turn AutoCommit off
1505 my $dbh = SL::DB->client->dbh;
1510 if ($form->{db} eq 'ar') {
1511 $table = 'customer';
1522 if ($form->{department_id}) {
1523 $where = qq| AND (a.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
1526 if ($form->{fromdate}) {
1527 $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
1529 if ($form->{todate}) {
1530 $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
1532 if (!$form->{fx_transaction}) {
1533 $where .= " AND ac.fx_transaction = '0'";
1538 if ($form->{reference}) {
1539 $reference = $dbh->quote(like($form->{reference}));
1540 $invnumber = " AND (a.invnumber LIKE $reference)";
1541 $reference = " AND (a.reference LIKE $reference)";
1543 if ($form->{source}) {
1544 $where .= " AND (ac.source ILIKE " . $dbh->quote(like($form->{source})) . ") ";
1546 if ($form->{memo}) {
1547 $where .= " AND (ac.memo ILIKE " . $dbh->quote(like($form->{memo})) . ") ";
1550 my %sort_columns = (
1551 'transdate' => [ qw(transdate lower_invnumber lower_name) ],
1552 'invnumber' => [ qw(lower_invnumber lower_name transdate) ],
1553 'name' => [ qw(lower_name transdate) ],
1554 'source' => [ qw(lower_source) ],
1555 'memo' => [ qw(lower_memo) ],
1557 my %lowered_columns = (
1558 'invnumber' => { 'gl' => 'a.reference', 'arap' => 'a.invnumber', },
1559 'memo' => { 'gl' => 'ac.memo', 'arap' => 'ac.memo', },
1560 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
1561 'name' => { 'gl' => 'a.description', 'arap' => 'c.name', },
1564 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
1565 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
1566 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
1569 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
1570 foreach my $spec (@{ $sort_columns{$sortkey} }) {
1571 next if ($spec !~ m/^lower_(.*)$/);
1574 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
1577 $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
1578 $sth = prepare_query($form, $dbh, $query);
1581 qq|SELECT c.name, a.invnumber, a.ordnumber,
1582 ac.transdate, ac.amount * $ml AS paid, ac.source,
1583 a.invoice, a.id, ac.memo, '${arap}' AS module
1584 $columns_for_sorting{arap}
1586 JOIN $arap a ON (ac.trans_id = a.id)
1587 JOIN $table c ON (c.id = a.${table}_id)
1588 WHERE (ac.chart_id = ?)
1594 SELECT a.description, a.reference, NULL AS ordnumber,
1595 ac.transdate, ac.amount * $ml AS paid, ac.source,
1596 '0' as invoice, a.id, ac.memo, 'gl' AS module
1597 $columns_for_sorting{gl}
1599 JOIN gl a ON (a.id = ac.trans_id)
1600 WHERE (ac.chart_id = ?)
1603 AND (ac.amount * $ml) > 0
1605 ORDER BY $sortorder|;
1606 my $sth_details = prepare_query($form, $dbh, $q_details);
1610 # cycle through each id
1611 foreach my $accno (split(/ /, $form->{paymentaccounts})) {
1612 do_statement($form, $sth, $query, $accno);
1613 my $ref = $sth->fetchrow_hashref();
1614 push(@{ $form->{PR} }, $ref);
1617 $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
1619 do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
1620 while (my $pr = $sth_details->fetchrow_hashref()) {
1621 push(@{ $form->{ $ref->{id} } }, $pr);
1623 $sth_details->finish();
1626 $main::lxdebug->leave_sub();
1630 $main::lxdebug->enter_sub();
1632 my ($self, $myconfig, $form) = @_;
1634 my $dbh = SL::DB->client->dbh;
1636 my $last_period = 0;
1639 qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40);
1641 $form->{decimalplaces} *= 1;
1643 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
1645 # if there are any compare dates
1647 if ($form->{fromdate} || $form->{todate}) {
1649 if ($form->{fromdate}) {
1650 $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1653 $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1656 my $kummfromdate = $form->{comparefromdate};
1657 my $kummtodate = $form->{comparetodate};
1658 &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
1661 my %charts_by_category =
1662 partition_by { $_->{pos_bwa} }
1663 sort_by { $_->{accno} }
1664 map { $form->{charts}->{$_} }
1665 keys %{ $form->{charts} };
1666 $form->{"charts_by_category"} = \%charts_by_category;
1668 $form->{category_names} = AM->get_bwa_categories($myconfig, $form);
1670 my @periods = qw(jetzt kumm);
1671 my @gesamtleistung = qw(1 3);
1672 my @gesamtkosten = qw (10 11 12 13 14 15 16 17 18 20);
1674 qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
1676 foreach my $key (@periods) {
1677 $form->{ "$key" . "gesamtleistung" } = 0;
1678 $form->{ "$key" . "gesamtkosten" } = 0;
1680 foreach $category (@categories) {
1682 if (defined($form->{$category}{$key})) {
1683 $form->{"$key$category"} =
1684 $form->format_amount($myconfig,
1685 $form->round_amount($form->{$category}{$key}, 2
1687 $form->{decimalplaces},
1691 foreach my $item (@gesamtleistung) {
1692 $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
1694 $form->{ "$key" . "gesamtleistung" } -= $form->{2}{$key};
1696 foreach my $item (@gesamtkosten) {
1697 $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
1699 $form->{ "$key" . "rohertrag" } =
1700 $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
1701 $form->{ "$key" . "betriebrohertrag" } =
1702 $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
1703 $form->{ "$key" . "betriebsergebnis" } =
1704 $form->{ "$key" . "betriebrohertrag" } -
1705 $form->{ "$key" . "gesamtkosten" };
1706 $form->{ "$key" . "neutraleraufwand" } =
1707 $form->{19}{$key} + $form->{30}{$key} + $form->{31}{$key};
1708 $form->{ "$key" . "neutralerertrag" } =
1709 $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
1710 $form->{ "$key" . "ergebnisvorsteuern" } =
1711 $form->{ "$key" . "betriebsergebnis" } -
1712 $form->{ "$key" . "neutraleraufwand" } +
1713 $form->{ "$key" . "neutralerertrag" };
1714 $form->{ "$key" . "ergebnis" } =
1715 $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
1717 if ($form->{ "$key" . "gesamtleistung" } > 0) {
1718 foreach $category (@categories) {
1719 if (defined($form->{$category}{$key})) {
1720 $form->{ "$key" . "gl" . "$category" } =
1721 $form->format_amount(
1723 $form->round_amount(
1724 ($form->{$category}{$key} /
1725 $form->{ "$key" . "gesamtleistung" } * 100
1727 $form->{decimalplaces}
1729 $form->{decimalplaces},
1733 foreach my $item (@ergebnisse) {
1734 $form->{ "$key" . "gl" . "$item" } =
1735 $form->format_amount($myconfig,
1736 $form->round_amount(
1737 ( $form->{ "$key" . "$item" } /
1738 $form->{ "$key" . "gesamtleistung" } * 100
1740 $form->{decimalplaces}
1742 $form->{decimalplaces},
1747 if ($form->{ "$key" . "gesamtkosten" } > 0) {
1748 foreach $category (@categories) {
1749 if (defined($form->{$category}{$key})) {
1750 $form->{ "$key" . "gk" . "$category" } =
1751 $form->format_amount($myconfig,
1752 $form->round_amount(
1753 ($form->{$category}{$key} /
1754 $form->{ "$key" . "gesamtkosten" } * 100
1756 $form->{decimalplaces}
1758 $form->{decimalplaces},
1762 foreach my $item (@ergebnisse) {
1763 $form->{ "$key" . "gk" . "$item" } =
1764 $form->format_amount($myconfig,
1765 $form->round_amount(
1766 ($form->{ "$key" . "$item" } /
1767 $form->{ "$key" . "gesamtkosten" } * 100
1769 $form->{decimalplaces}
1771 $form->{decimalplaces},
1776 if ($form->{10}{$key} > 0) {
1777 foreach $category (@categories) {
1778 if (defined($form->{$category}{$key})) {
1779 $form->{ "$key" . "pk" . "$category" } =
1780 $form->format_amount(
1782 $form->round_amount(
1783 ($form->{$category}{$key} / $form->{10}{$key} * 100),
1784 $form->{decimalplaces}
1786 $form->{decimalplaces},
1790 foreach my $item (@ergebnisse) {
1791 $form->{ "$key" . "pk" . "$item" } =
1792 $form->format_amount($myconfig,
1793 $form->round_amount(
1794 ($form->{ "$key" . "$item" } /
1795 $form->{10}{$key} * 100
1797 $form->{decimalplaces}
1799 $form->{decimalplaces},
1804 if ($form->{4}{$key} > 0) {
1805 foreach $category (@categories) {
1806 if (defined($form->{$category}{$key})) {
1807 $form->{ "$key" . "auf" . "$category" } =
1808 $form->format_amount(
1810 $form->round_amount(
1811 ($form->{$category}{$key} / $form->{4}{$key} * 100),
1812 $form->{decimalplaces}
1814 $form->{decimalplaces},
1818 foreach my $item (@ergebnisse) {
1819 $form->{ "$key" . "auf" . "$item" } =
1820 $form->format_amount($myconfig,
1821 $form->round_amount(
1822 ($form->{ "$key" . "$item" } /
1823 $form->{4}{$key} * 100
1825 $form->{decimalplaces}
1827 $form->{decimalplaces},
1832 foreach my $item (@ergebnisse) {
1833 $form->{ "$key" . "$item" } =
1834 $form->format_amount($myconfig,
1835 $form->round_amount($form->{ "$key" . "$item" },
1836 $form->{decimalplaces}
1838 $form->{decimalplaces},
1844 $main::lxdebug->leave_sub();
1847 sub income_statement {
1848 $main::lxdebug->enter_sub();
1850 my ($self, $myconfig, $form) = @_;
1852 # connect to database
1853 my $dbh = $form->dbconnect($myconfig);
1855 my $last_period = 0;
1856 my @categories_einnahmen = qw(1 2 3 4 5 6 7);
1857 my @categories_ausgaben =
1858 qw(8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31);
1860 my @ergebnisse = qw(sumeura sumeurb guvsumme);
1862 $form->{decimalplaces} *= 1;
1866 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
1870 # add extra information to form to be used by template
1871 my %charts_by_category =
1872 partition_by { $_->{pos_eur} }
1873 sort_by { $_->{accno} }
1874 map { $form->{charts}->{$_} }
1875 keys %{ $form->{charts} };
1876 $form->{"charts_by_category"} = \%charts_by_category;
1878 $form->{"categories_income"} = \@categories_einnahmen;
1879 $form->{"categories_expense"} = \@categories_ausgaben;
1881 $form->{category_names} = AM->get_eur_categories($myconfig, $form);
1885 foreach my $item (@categories_einnahmen) {
1886 $eur_amounts{$item} = $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1887 $form->{"sumeura"} += $form->{$item};
1889 foreach my $item (@categories_ausgaben) {
1890 $eur_amounts{$item} = $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1891 $form->{"sumeurb"} += $form->{$item};
1894 $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
1896 $form->{eur_amounts} = \%eur_amounts;
1898 foreach my $item (@ergebnisse) {
1900 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1902 $main::lxdebug->leave_sub();
1905 sub erfolgsrechnung {
1906 $main::lxdebug->enter_sub();
1908 my ($self, $myconfig, $form) = @_;
1909 $form->{company} = $::instance_conf->get_company;
1910 $form->{address} = $::instance_conf->get_address;
1911 $form->{fromdate} = DateTime->new(year => 2000, month => 1, day => 1)->to_kivitendo unless $form->{fromdate};
1912 $form->{todate} = $form->current_date(%{$myconfig}) unless $form->{todate};
1914 my %categories = (I => "ERTRAG", E => "AUFWAND");
1915 my $fromdate = conv_dateq($form->{fromdate});
1916 my $todate = conv_dateq($form->{todate});
1917 my $department_id = conv_i((split /--/, $form->{department})[1], 'NULL');
1921 foreach my $category ('I', 'E') {
1923 name => $categories{$category},
1925 accounts => get_accounts_ch($category)
1927 foreach my $account (@{$category{accounts}}) {
1928 $account->{total} = get_total_ch($department_id, $account->{id}, $fromdate, $todate);
1929 $category{total} += $account->{total};
1930 $account->{total} = $form->format_amount($myconfig, $form->round_amount($account->{total}, 2), 2);
1932 $form->{total} += $category{total};
1933 $category{total} = $form->format_amount($myconfig, $form->round_amount($category{total}, 2), 2);
1934 push(@{$form->{categories}}, \%category);
1936 $form->{total} = $form->format_amount($myconfig, $form->round_amount($form->{total}, 2), 2);
1938 $main::lxdebug->leave_sub();
1942 sub get_accounts_ch {
1943 $main::lxdebug->enter_sub();
1945 my ($category) = @_;
1946 my $inclusion = '' ;
1948 if ($category eq 'I') {
1949 $inclusion = "AND pos_er = NULL OR pos_er = '1'";
1950 } elsif ($category eq 'E') {
1951 $inclusion = "AND pos_er = NULL OR pos_er = '6'";
1957 SELECT id, accno, description, category
1959 WHERE category = ? $inclusion
1962 my $accounts = _query($query, $category);
1964 $main::lxdebug->leave_sub();
1969 $main::lxdebug->enter_sub();
1971 my ($department_id, $chart_id, $fromdate, $todate) = @_;
1980 if ($department_id) {
1981 $query .= qq| AND COALESCE(
1982 (SELECT department_id FROM ar WHERE ar.id=trans_id),
1983 (SELECT department_id FROM gl WHERE gl.id=trans_id),
1984 (SELECT department_id FROM ap WHERE ap.id=trans_id)
1986 $total += _query($query, $chart_id, $fromdate, $todate, $department_id)->[0]->{sum};
1988 $total += _query($query, $chart_id, $fromdate, $todate)->[0]->{sum};
1991 $main::lxdebug->leave_sub();
1995 sub _query {return selectall_hashref_query($::form, $::form->get_standard_dbh, @_);}