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);
47 # new implementation of balance sheet
50 # stuff missing from the original implementation:
53 # - proper testing for heading charts
54 # - transmission from $form to TMPL realm is not as clear as i'd like
57 $main::lxdebug->enter_sub();
61 my $myconfig = \%main::myconfig;
62 my $form = $main::form;
63 my $dbh = $::form->get_standard_dbh;
66 my @categories = qw(A C L Q);
68 # if there are any dates construct a where
69 if ($form->{asofdate}) {
70 $form->{period} = $form->{this_period} = conv_dateq($form->{asofdate});
73 # get starting date for calculating balance
74 $form->{this_startdate} = $self->get_balance_starting_date($form->{asofdate});
76 get_accounts($dbh, $last_period, $form->{this_startdate}, $form->{asofdate}, $form, \@categories);
78 # if there are any compare dates
79 if ($form->{compareasofdate}) {
82 $form->{last_startdate} = $self->get_balance_starting_date($form->{compareasofdate});
84 get_accounts($dbh, $last_period, $form->{last_startdate} , $form->{compareasofdate}, $form, \@categories);
85 $form->{last_period} = conv_dateq($form->{compareasofdate});
88 # now we got $form->{A}{accno}{ } assets
89 # and $form->{L}{accno}{ } liabilities
90 # and $form->{Q}{accno}{ } equity
91 # build asset accounts
93 my %account = ('A' => { 'ml' => -1 },
95 'Q' => { 'ml' => 1 });
99 foreach my $category (grep { !/C/ } @categories) {
101 $TMPL_DATA->{$category} = [];
102 my $ml = $account{$category}{ml};
104 foreach my $key (sort keys %{ $form->{$category} }) {
106 my $row = { %{ $form->{$category}{$key} } };
108 # if charttype "heading" - calculate this entry, start a new batch of charts belonging to this heading and skip the rest bo the loop
109 # header charts are not real charts. start a sub aggregation with them, but don't calculate anything with them
110 if ($row->{charttype} eq "H") {
111 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
112 $row->{subdescription} = $account{$category}{subdescription};
113 $row->{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
114 $row->{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
117 $row->{subheader} = 1;
118 $account{$category}{subthis} = $row->{this};
119 $account{$category}{sublast} = $row->{last};
120 $account{$category}{subdescription} = $row->{description};
121 $account{$category}{subtotal} = 1;
126 next unless $form->{l_heading};
129 for my $period (qw(this last)) {
130 next if ($period eq 'last' && !$last_period);
132 $row->{$period} *= $ml;
135 push @{ $TMPL_DATA->{$category} }, $row;
138 # resolve heading/subtotal
139 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
140 $TMPL_DATA->{$category}[-1]{subdescription} = $account{$category}{subdescription};
141 $TMPL_DATA->{$category}[-1]{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
142 $TMPL_DATA->{$category}[-1]{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
145 $TMPL_DATA->{total}{$category}{this} = sum map { $_->{this} } @{ $TMPL_DATA->{$category} };
146 $TMPL_DATA->{total}{$category}{last} = sum map { $_->{last} } @{ $TMPL_DATA->{$category} };
149 for my $period (qw(this last)) {
150 next if ($period eq 'last' && !$last_period);
152 $form->{E}{$period} = $TMPL_DATA->{total}{A}{$period} - $TMPL_DATA->{total}{L}{$period} - $TMPL_DATA->{total}{Q}{$period};
153 $TMPL_DATA->{total}{Q}{$period} += $form->{E}{$period};
154 $TMPL_DATA->{total}{$period} = $TMPL_DATA->{total}{L}{$period} + $TMPL_DATA->{total}{Q}{$period};
156 $form->{E}{description}='nicht verbuchter Gewinn/Verlust';
157 push @{ $TMPL_DATA->{Q} }, $form->{E};
159 $main::lxdebug->leave_sub();
165 $main::lxdebug->enter_sub();
167 my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
169 my ($null, $department_id) = split /--/, $form->{department};
173 my $dpt_where_without_arapgl = '';
180 my $dec = $form->{decimalplaces};
182 my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |;
186 qq|SELECT c.accno, c.description, c.category
188 WHERE (c.charttype = 'H')
192 $sth = prepare_execute_query($form, $dbh, $query);
194 my @headingaccounts = ();
195 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
196 $form->{ $ref->{category} }{ $ref->{accno} }{description} =
197 "$ref->{description}";
198 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
199 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
201 push @headingaccounts, $ref->{accno};
206 # filter for opening and closing bookings
207 # if l_ob is selected l_cb is always ignored
208 if ( $last_period ) {
209 # ob/cb-settings for "compared to" balance
210 if ( $form->{l_ob_compared} ) {
211 $where .= ' AND ac.ob_transaction is true '
212 } elsif ( not $form->{l_cb_compared} ) {
213 $where .= ' AND ac.cb_transaction is false ';
216 # ob/cb-settings for "as of" balance
217 if ( $form->{l_ob} ) {
218 $where .= ' AND ac.ob_transaction is true '
219 } elsif ( not $form->{l_cb} ) {
220 $where .= ' AND ac.cb_transaction is false ';
226 $fromdate = conv_dateq($fromdate);
227 if ($form->{method} eq 'cash') {
228 $subwhere .= " AND (transdate >= $fromdate)";
229 $glwhere = " AND (ac.transdate >= $fromdate)";
231 $where .= " AND (ac.transdate >= $fromdate)";
236 $todate = conv_dateq($todate);
237 $where .= " AND (ac.transdate <= $todate)";
238 $subwhere .= " AND (transdate <= $todate)";
241 if ($department_id) {
242 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
245 if ($form->{project_id}) {
246 # Diese Bedingung wird derzeit niemals wahr sein, da man in Bericht->Bilanz keine
247 # Projekte auswählen kann
248 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
251 if ($form->{method} eq 'cash') {
253 qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
255 JOIN chart c ON (c.id = ac.chart_id)
256 JOIN ar a ON (a.id = ac.trans_id)
264 WHERE (a.chart_link LIKE '%AR_paid%')
268 GROUP BY c.accno, c.description, c.category
272 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
274 JOIN chart c ON (c.id = ac.chart_id)
275 JOIN ap a ON (a.id = ac.trans_id)
283 WHERE (a.chart_link LIKE '%AP_paid%')
287 GROUP BY c.accno, c.description, c.category
291 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
293 JOIN chart c ON (c.id = ac.chart_id)
294 JOIN gl a ON (a.id = ac.trans_id)
299 AND NOT ((ac.chart_link = 'AR') OR (ac.chart_link = 'AP'))
301 GROUP BY c.accno, c.description, c.category |;
303 if ($form->{project_id}) {
304 # s.o. keine Projektauswahl in Bilanz
309 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
311 JOIN ar a ON (a.id = ac.trans_id)
312 JOIN parts p ON (ac.parts_id = p.id)
313 JOIN chart c on (p.income_accno_id = c.id)
314 -- use transdate from subwhere
315 WHERE (c.category = 'I')
322 WHERE (a.chart_link LIKE '%AR_paid%')
326 GROUP BY c.accno, c.description, c.category
330 SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
332 JOIN ap a ON (a.id = ac.trans_id)
333 JOIN parts p ON (ac.parts_id = p.id)
334 JOIN chart c on (p.expense_accno_id = c.id)
335 WHERE (c.category = 'E')
342 WHERE a.chart_link LIKE '%AP_paid%'
346 GROUP BY c.accno, c.description, c.category |;
349 } else { # if ($form->{method} eq 'cash')
350 if ($department_id) {
351 $dpt_where = qq| AND a.department_id = | . conv_i($department_id);
352 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
353 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
354 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
358 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
360 JOIN chart c ON (c.id = ac.chart_id)
362 $dpt_where_without_arapgl
365 GROUP BY c.accno, c.description, c.category |;
367 if ($form->{project_id}) {
368 # s.o. keine Projektauswahl in Bilanz
372 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
374 JOIN ar a ON (a.id = ac.trans_id)
375 JOIN parts p ON (ac.parts_id = p.id)
376 JOIN chart c on (p.income_accno_id = c.id)
377 -- use transdate from subwhere
378 WHERE (c.category = 'I')
382 GROUP BY c.accno, c.description, c.category
386 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
388 JOIN ap a ON (a.id = ac.trans_id)
389 JOIN parts p ON (ac.parts_id = p.id)
390 JOIN chart c on (p.expense_accno_id = c.id)
391 WHERE (c.category = 'E')
395 GROUP BY c.accno, c.description, c.category |;
403 $sth = prepare_execute_query($form, $dbh, $query);
405 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
407 if ($ref->{category} eq 'C') {
408 $ref->{category} = 'A';
411 # get last heading account
412 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
416 $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
418 $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
422 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
423 $form->{ $ref->{category} }{ $ref->{accno} }{description} = $ref->{description};
424 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
427 $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
429 $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
434 # remove accounts with zero balance
435 foreach $category (@{$categories}) {
436 foreach $accno (keys %{ $form->{$category} }) {
437 $form->{$category}{$accno}{last} = $form->round_amount($form->{$category}{$accno}{last}, $dec);
438 $form->{$category}{$accno}{this} = $form->round_amount($form->{$category}{$accno}{this}, $dec);
440 delete $form->{$category}{$accno}
441 if ( $form->{$category}{$accno}{this} == 0
442 && $form->{$category}{$accno}{last} == 0);
446 $main::lxdebug->leave_sub();
450 $main::lxdebug->enter_sub();
452 my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
454 my ($null, $department_id) = split /--/, $form->{department};
458 my $dpt_where_without_arapgl;
467 $where .= ' AND ac.cb_transaction is false ' unless $form->{l_cb};
470 $fromdate = conv_dateq($fromdate);
471 if ($form->{method} eq 'cash') {
472 $subwhere .= " AND (transdate >= $fromdate)";
473 $glwhere = " AND (ac.transdate >= $fromdate)";
474 $prwhere = " AND (a.transdate >= $fromdate)";
475 $inwhere = " AND (acc.transdate >= $fromdate)";
477 $where .= " AND (ac.transdate >= $fromdate)";
478 # hotfix for projectfilter in guv and bwa
479 # fromdate is otherwise ignored if project is selected
480 $prwhere = " AND (a.transdate >= $fromdate)";
485 $todate = conv_dateq($todate);
486 $subwhere .= " AND (transdate <= $todate)";
487 $where .= " AND (ac.transdate <= $todate)";
488 $prwhere .= " AND (a.transdate <= $todate)";
489 $inwhere .= " AND (acc.transdate <= $todate)";
492 if ($department_id) {
493 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
496 if ($form->{project_id}) {
497 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
501 # GUV patch by Ronny Rentner (Bug 1190)
503 # GUV IST-Versteuerung
505 # Alle tatsaechlichen _Zahlungseingaenge_
506 # im Zeitraum erfassen
507 # (Teilzahlungen werden prozentual auf verschiedene Steuern aufgeteilt)
511 if ($form->{method} eq 'cash') {
514 SELECT SUM( ac.amount * CASE WHEN COALESCE((SELECT amount FROM ar a WHERE id = ac.trans_id $dpt_where), 0) != 0 THEN
515 /* ar amount is not zero, so we can divide by amount */
516 (SELECT SUM(acc.amount) * -1
519 AND acc.trans_id = ac.trans_id
520 AND acc.chart_link LIKE '%AR_paid%')
521 / (SELECT amount FROM ar WHERE id = ac.trans_id)
523 /* 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 */
525 ) AS amount, c.$category, c.accno, c.description
527 LEFT JOIN chart c ON (c.id = ac.chart_id)
528 LEFT JOIN ar ON (ar.id = ac.trans_id)
529 WHERE ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE 1=1 $subwhere)
531 GROUP BY c.$category, c.accno, c.description
534 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
536 JOIN chart c ON (c.id = ac.chart_id)
537 JOIN ar a ON (a.id = ac.trans_id)
538 WHERE $where $dpt_where
539 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AR_paid%') $subwhere)
541 GROUP BY c.$category, c.accno, c.description
545 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
547 JOIN chart c ON (c.id = ac.chart_id)
548 JOIN ap a ON (a.id = ac.trans_id)
549 WHERE $where $dpt_where
550 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AP_paid%') $subwhere)
552 GROUP BY c.$category, c.accno, c.description
556 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
558 JOIN chart c ON (c.id = ac.chart_id)
559 JOIN gl a ON (a.id = ac.trans_id)
560 WHERE $where $dpt_where $glwhere
561 AND NOT ((ac.chart_link = 'AR') OR (ac.chart_link = 'AP'))
563 GROUP BY c.$category, c.accno, c.description
566 if ($form->{project_id}) {
570 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
572 JOIN ar a ON (a.id = ac.trans_id)
573 JOIN parts p ON (ac.parts_id = p.id)
574 JOIN chart c on (p.income_accno_id = c.id)
575 WHERE (c.category = 'I') $prwhere $dpt_where
576 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AR_paid%') $subwhere)
578 GROUP BY c.$category, c.accno, c.description
582 SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
584 JOIN ap a ON (a.id = ac.trans_id)
585 JOIN parts p ON (ac.parts_id = p.id)
586 JOIN chart c on (p.expense_accno_id = c.id)
587 WHERE (c.category = 'E') $prwhere $dpt_where
588 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AP_paid%') $subwhere)
590 GROUP BY c.$category, c.accno, c.description
594 } else { # if ($form->{method} eq 'cash')
595 if ($department_id) {
596 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
597 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
598 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
599 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
603 SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
605 JOIN chart c ON (c.id = ac.chart_id)
607 $dpt_where_without_arapgl
609 GROUP BY c.$category, c.accno, c.description |;
611 if ($form->{project_id}) {
615 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
617 JOIN ar a ON (a.id = ac.trans_id)
618 JOIN parts p ON (ac.parts_id = p.id)
619 JOIN chart c on (p.income_accno_id = c.id)
620 WHERE (c.category = 'I')
624 GROUP BY c.$category, c.accno, c.description
628 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category, c.accno, c.description
630 JOIN ap a ON (a.id = ac.trans_id)
631 JOIN parts p ON (ac.parts_id = p.id)
632 JOIN chart c on (p.expense_accno_id = c.id)
633 WHERE (c.category = 'E')
637 GROUP BY c.$category, c.accno, c.description |;
645 # store information for chart list in $form->{charts}
646 foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
647 unless ( defined $form->{charts}->{$ref->{accno}} ) {
648 # a chart may appear several times in the resulting hashref, init it the first time
649 $form->{charts}->{$ref->{accno}} = { amount => 0,
650 "$category" => $ref->{"$category"},
651 accno => $ref->{accno},
652 description => $ref->{description},
655 if ($category eq "pos_bwa") {
657 $form->{ $ref->{$category} }{kumm} += $ref->{amount};
659 $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
660 # only increase chart amount for current period, not last_period
661 $form->{charts}->{$ref->{accno}}->{amount} += $ref->{amount},
664 $form->{ $ref->{$category} } += $ref->{amount};
665 $form->{charts}->{$ref->{accno}}->{amount} += $ref->{amount}; # no last_period for eur
669 $main::lxdebug->leave_sub();
673 $main::lxdebug->enter_sub();
675 my ($self, $myconfig, $form, %options) = @_;
677 my $dbh = SL::DB->client->dbh;
679 my ($query, $sth, $ref);
682 my ($null, $department_id) = split /--/, $form->{department};
683 my @headingaccounts = ();
685 my $dpt_where_without_arapgl;
686 my ($customer_where, $customer_join, $customer_no_union);
690 my $invwhere = $where;
692 if ($department_id) {
693 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
694 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
695 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
696 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
698 if ($form->{customer_id}) {
699 $customer_join = qq| JOIN ar a ON (ac.trans_id = a.id) |;
700 $customer_where = qq| AND (a.customer_id = | . conv_i($form->{customer_id}, 'NULL') . qq|) |;
701 $customer_no_union = qq| AND 1=0 |;
704 # project_id only applies to getting transactions
705 # it has nothing to do with a trial balance
706 # but we use the same function to collect information
708 if ($form->{project_id}) {
709 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
712 my $acc_cash_where = "";
713 # my $ar_cash_where = "";
714 # my $ap_cash_where = "";
717 if ($form->{method} eq "cash") {
719 qq| AND (ac.trans_id IN (
722 WHERE datepaid >= '$form->{fromdate}'
723 AND datepaid <= '$form->{todate}'
729 WHERE datepaid >= '$form->{fromdate}'
730 AND datepaid <= '$form->{todate}'
736 WHERE transdate >= '$form->{fromdate}'
737 AND transdate <= '$form->{todate}'
739 # $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
742 if ($options{beginning_balances}) {
743 foreach my $prefix (qw(from to)) {
744 next if ($form->{"${prefix}date"});
746 my $min_max = $prefix eq 'from' ? 'min' : 'max';
747 $query = qq|SELECT ${min_max}(transdate)
751 $dpt_where_without_arapgl
755 ($form->{"${prefix}date"}) = selectfirst_array_query($form, $dbh, $query);
758 # get beginning balances
760 qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
762 LEFT JOIN chart c ON (ac.chart_id = c.id)
764 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
765 $dpt_where_without_arapgl
768 GROUP BY c.accno, c.category, c.description |;
770 $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
772 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
774 if ($ref->{amount} != 0 || $form->{all_accounts}) {
775 $trb{ $ref->{accno} }{description} = $ref->{description};
776 $trb{ $ref->{accno} }{charttype} = 'A';
777 $trb{ $ref->{accno} }{beginning_balance} = $ref->{amount};
779 if ($ref->{amount} > 0) {
780 $trb{ $ref->{accno} }{haben_eb} = $ref->{amount};
782 $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1;
784 $trb{ $ref->{accno} }{category} = $ref->{category};
793 qq|SELECT c.accno, c.description, c.category
795 WHERE c.charttype = 'H'
798 $sth = prepare_execute_query($form, $dbh, $query);
800 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
801 $trb{ $ref->{accno} }{description} = $ref->{description};
802 $trb{ $ref->{accno} }{charttype} = 'H';
803 $trb{ $ref->{accno} }{category} = $ref->{category};
805 push @headingaccounts, $ref->{accno};
811 my $saldowhere = " 1 = 1 ";
812 my $sumwhere = " 1 = 1 ";
814 my $sumsubwhere = '';
815 my $saldosubwhere = '';
816 my $glsaldowhere = '';
821 my ($fromdate, $todate, $fetch_accounts_before_from);
823 if ($form->{fromdate} || $form->{todate}) {
824 if ($form->{fromdate}) {
825 $fromdate = conv_dateq($form->{fromdate});
826 $tofrom .= " AND (ac.transdate >= $fromdate)";
827 $subwhere .= " AND (ac.transdate >= $fromdate)";
828 $sumsubwhere .= " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
829 $saldosubwhere .= " AND (ac,transdate>=(select date_trunc('year', date $fromdate))) ";
830 $invwhere .= " AND (a.transdate >= $fromdate)";
831 $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
832 $glwhere = " AND (ac.transdate >= $fromdate)";
833 $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
835 if ($form->{todate}) {
836 $todate = conv_dateq($form->{todate});
837 $tofrom .= " AND (ac.transdate <= $todate)";
838 $invwhere .= " AND (a.transdate <= $todate)";
839 $saldosubwhere .= " AND (ac.transdate <= $todate)";
840 $sumsubwhere .= " AND (ac.transdate <= $todate)";
841 $subwhere .= " AND (ac.transdate <= $todate)";
842 $glwhere .= " AND (ac.transdate <= $todate)";
843 $glsumwhere .= " AND (ac.transdate <= $todate) ";
844 $glsaldowhere .= " AND (ac.transdate <= $todate) ";
848 if ($form->{method} eq "cash") {
850 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) |;
851 $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) |;
853 $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) |;
855 $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
856 $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
857 $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
859 # get all entries before fromdate, which are not yet fetched
860 # TODO dpt_where_without_arapgl and project - project calculation seems bogus anyway
861 # TODO use fiscal_year_startdate for the whole trial balance
862 # anyway, if the last booking is in a deviating fiscal year, this already improves the query
863 my $fiscal_year_startdate = conv_dateq($self->get_balance_starting_date($form->{fromdate}));
864 $fetch_accounts_before_from = qq|SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
865 FROM acc_trans ac JOIN chart c ON (c.id = ac.chart_id) WHERE 1 = 1 AND (ac.transdate <= $fromdate)
866 AND (ac.transdate >= $fiscal_year_startdate)
867 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)
868 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)
869 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL))
870 GROUP BY c.accno, c.description, c.category ORDER BY accno|;
874 SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
876 JOIN chart c ON (c.id = ac.chart_id)
879 $dpt_where_without_arapgl
881 GROUP BY c.accno, c.description, c.category |;
883 if ($form->{project_id}) {
885 -- add project transactions from invoice
889 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
891 JOIN ar a ON (ac.trans_id = a.id)
892 JOIN parts p ON (ac.parts_id = p.id)
893 JOIN chart c ON (p.income_accno_id = c.id)
898 GROUP BY c.accno, c.description, c.category
902 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
904 JOIN ap a ON (ac.trans_id = a.id)
905 JOIN parts p ON (ac.parts_id = p.id)
906 JOIN chart c ON (p.expense_accno_id = c.id)
911 GROUP BY c.accno, c.description, c.category
915 $query .= qq| ORDER BY accno|;
917 $sth = prepare_execute_query($form, $dbh, $query);
919 # calculate the debit and credit in the period
920 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
921 $trb{ $ref->{accno} }{description} = $ref->{description};
922 $trb{ $ref->{accno} }{charttype} = 'A';
923 $trb{ $ref->{accno} }{category} = $ref->{category};
924 $trb{ $ref->{accno} }{amount} += $ref->{amount};
928 if ($form->{method} ne "cash") { # better eq 'accrual'
929 $sth = prepare_execute_query($form, $dbh, $fetch_accounts_before_from);
930 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
931 $trb{ $ref->{accno} }{description} = $ref->{description};
932 $trb{ $ref->{accno} }{charttype} = 'A';
933 $trb{ $ref->{accno} }{category} = $ref->{category};
934 $trb{ $ref->{accno} }{amount} += $ref->{amount};
939 # prepare query for each account
940 my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
944 (SELECT SUM(ac.amount) * -1
946 JOIN chart c ON (c.id = ac.chart_id)
949 $dpt_where_without_arapgl
953 AND (c.accno = ?)) AS debit,
955 (SELECT SUM(ac.amount)
957 JOIN chart c ON (c.id = ac.chart_id)
960 $dpt_where_without_arapgl
964 AND c.accno = ?) AS credit,
965 (SELECT SUM(ac.amount)
967 JOIN chart c ON (ac.chart_id = c.id)
970 $dpt_where_without_arapgl
973 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
975 (SELECT SUM(ac.amount)
977 JOIN chart c ON (ac.chart_id = c.id)
980 $dpt_where_without_arapgl
984 AND c.accno = ?) AS sum_credit,
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_debit,
997 (SELECT max(ac.transdate) FROM acc_trans ac
998 JOIN chart c ON (ac.chart_id = c.id)
1001 $dpt_where_without_arapgl
1004 AND c.accno = ?) AS last_transaction
1009 $drcr = prepare_query($form, $dbh, $q_drcr);
1011 if ($form->{project_id}) {
1012 # prepare query for each account
1015 (SELECT SUM(ac.sellprice * ac.qty) * -1
1017 JOIN parts p ON (ac.parts_id = p.id)
1018 JOIN ap a ON (ac.trans_id = a.id)
1019 JOIN chart c ON (p.expense_accno_id = c.id)
1024 AND c.accno = ?) AS debit,
1026 (SELECT SUM(ac.sellprice * ac.qty)
1028 JOIN parts p ON (ac.parts_id = p.id)
1029 JOIN ar a ON (ac.trans_id = a.id)
1030 JOIN chart c ON (p.income_accno_id = c.id)
1035 AND c.accno = ?) AS credit,
1037 (SELECT SUM(ac.amount)
1039 JOIN chart c ON (ac.chart_id = c.id)
1042 $dpt_where_without_arapgl
1046 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
1048 (SELECT SUM(ac.amount)
1050 JOIN chart c ON (ac.chart_id = c.id)
1053 $dpt_where_without_arapgl
1058 AND c.accno = ?) AS sum_credit,
1060 (SELECT SUM(ac.amount)
1062 JOIN chart c ON (ac.chart_id = c.id)
1066 $dpt_where_without_arapgl
1070 AND c.accno = ?) AS sum_debit,
1073 (SELECT max(ac.transdate) FROM acc_trans ac
1074 JOIN chart c ON (ac.chart_id = c.id)
1077 $dpt_where_without_arapgl
1080 AND c.accno = ?) AS last_transaction
1083 $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
1087 my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
1089 foreach my $accno (sort keys %trb) {
1092 $ref->{accno} = $accno;
1093 map { $ref->{$_} = $trb{$accno}{$_} }
1094 qw(description category charttype amount soll_eb haben_eb beginning_balance);
1096 $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
1098 if ($trb{$accno}{charttype} eq 'A') {
1101 do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1103 ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
1104 my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
1105 $last_transaction = "";
1106 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
1107 $ref->{debit} += $debit;
1108 $ref->{credit} += $credit;
1110 $ref->{haben_saldo} += $saldo;
1112 $ref->{soll_saldo} += $saldo * -1;
1114 $ref->{last_transaction} = $last_transaction;
1115 $ref->{soll_kumuliert} = $soll_kumuliert * -1;
1116 $ref->{haben_kumuliert} = $haben_kumuliert;
1120 if ($form->{project_id}) {
1123 do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1125 ($debit, $credit) = (0, 0);
1126 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
1127 $ref->{debit} += $debit;
1128 $ref->{credit} += $credit;
1130 $ref->{haben_saldo} += $saldo;
1132 $ref->{soll_saldo} += $saldo * -1;
1134 $ref->{soll_kumuliert} += $soll_kumuliert * -1;
1135 $ref->{haben_kumuliert} += $haben_kumuliert;
1137 $project_drcr->finish;
1140 $ref->{debit} = $form->round_amount($ref->{debit}, 2);
1141 $ref->{credit} = $form->round_amount($ref->{credit}, 2);
1143 if ($ref->{haben_saldo} != 0) {
1144 $ref->{haben_saldo} = $ref->{haben_saldo} + $ref->{beginning_balance};
1145 if ($ref->{haben_saldo} < 0) {
1146 $ref->{soll_saldo} = $form->round_amount(($ref->{haben_saldo} *- 1), 2);
1147 $ref->{haben_saldo} = 0;
1150 $ref->{soll_saldo} = $ref->{soll_saldo} - $ref->{beginning_balance};
1151 if ($ref->{soll_saldo} < 0) {
1152 $ref->{haben_saldo} = $form->round_amount(($ref->{soll_saldo} * -1), 2);
1153 $ref->{soll_saldo} = 0;
1156 $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
1157 $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
1158 $ref->{haben_kumuliert} = $form->round_amount($ref->{haben_kumuliert}, 2);
1159 $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
1164 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1165 $accno = pop @accno;
1167 $trb{$accno}{debit} += $ref->{debit};
1168 $trb{$accno}{credit} += $ref->{credit};
1169 $trb{$accno}{soll_saldo} += $ref->{soll_saldo};
1170 $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
1171 $trb{$accno}{soll_kumuliert} += $ref->{soll_kumuliert};
1172 $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
1175 push @{ $form->{TB} }, $ref;
1179 # debits and credits for headings
1180 foreach my $accno (@headingaccounts) {
1181 foreach $ref (@{ $form->{TB} }) {
1182 if ($accno eq $ref->{accno}) {
1183 $ref->{debit} = $trb{$accno}{debit};
1184 $ref->{credit} = $trb{$accno}{credit};
1185 $ref->{soll_saldo} = $trb{$accno}{soll_saldo};
1186 $ref->{haben_saldo} = $trb{$accno}{haben_saldo};
1187 $ref->{soll_kumuliert} = $trb{$accno}{soll_kumuliert};
1188 $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert};
1193 $main::lxdebug->leave_sub();
1197 $main::lxdebug->enter_sub();
1198 my ($self, $dbh, $form) = @_;
1199 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
1200 my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
1201 my $sth = $dbh->prepare($query);
1202 while(my $ref = $sth->fetchrow_hashref()) {
1203 $ref->{invnumer} =~ s/Storno zu //g;
1204 $form->{storno}{$ref->{invnumber}} = 1;
1206 $main::lxdebug->leave_sub();
1210 $main::lxdebug->enter_sub();
1212 my ($self, $myconfig, $form) = @_;
1214 # connect to database
1215 my $dbh = SL::DB->client->dbh;
1217 my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
1219 # falls customer ziehen wir die offene forderungsliste
1220 # anderfalls für die lieferanten die offenen verbindlichkeitne
1221 if ($form->{ct} eq "customer") {
1234 $ct_id = "${ct}_id";
1236 # erweiterung um einen freien zeitraum oder einen stichtag
1237 # mit entsprechender altersstrukturliste (s.a. Bug 1842)
1238 # eine neue variable an der oberfläche eingeführt, somit ist
1239 # todate == freier zeitrau und fordate == stichtag
1240 # duedate_where == nur fällige rechnungen anzeigen
1242 my ($review_of_aging_list, $todate, $fromdate, $fromwhere, $fordate,
1245 if ($form->{reporttype} eq 'custom') { # altersstrukturliste, nur fällige
1247 # explizit rausschmeissen was man für diesen bericht nicht braucht
1248 delete $form->{fromdate};
1249 delete $form->{todate};
1251 # an der oberfläche ist das tagesaktuelle datum vorausgewählt
1252 # falls es dennoch per Benutzereingabe gelöscht wird, lieber wieder vorbelegen
1253 # ferner muss für die spätere DB-Abfrage muss todate gesetzt sein.
1254 $form->{fordate} = $form->current_date($myconfig) unless ($form->{fordate});
1255 $fordate = conv_dateq($form->{fordate});
1258 if ($form->{review_of_aging_list}) { # falls die liste leer ist, alles anzeigen
1259 if ($form->{review_of_aging_list} =~ m "-") { # .. periode von bis
1260 my @period = split(/-/, $form->{review_of_aging_list}); # ... von periode bis periode
1261 $review_of_aging_list = " AND $period[0] < (date $fordate) - duedate
1262 AND (date $fordate) - duedate < $period[1]";
1264 $form->{review_of_aging_list} =~ s/[^0-9]//g; # größer 120 das substitute ist nur für das '>' zeichen
1265 $review_of_aging_list = " AND $form->{review_of_aging_list} < (date $fordate) - duedate";
1268 $duedate_where = " AND (date $fordate) - duedate >= 0 ";
1269 } else { # freier zeitraum, nur rechnungsdatum und OHNE review_of_aging_list
1270 $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
1271 $todate = conv_dateq($form->{todate});
1272 $fromdate = conv_dateq($form->{fromdate});
1273 $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
1275 my $where = " 1 = 1 ";
1278 if ($form->{$ct_id}) {
1279 $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
1280 } elsif ($form->{ $form->{ct} }) {
1281 $where .= qq| AND (ct.name ILIKE | . $dbh->quote(like($form->{$ct})) . qq|)|;
1286 if ($form->{department}) {
1287 my ($null, $department_id) = split /--/, $form->{department};
1288 $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
1289 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1290 $where_dpt = qq| AND (${arap}.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1294 SELECT ${ct}.id AS ctid, ${ct}.name,
1295 street, zipcode, city, country, contact, email,
1296 phone as customerphone, fax as customerfax, ${ct}number,
1297 "invnumber", "transdate",
1298 (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",
1299 "duedate", invoice, ${arap}.id, date_part('days', now() - duedate) as overduedays,
1302 WHERE (${arap}.currency_id = exchangerate.currency_id)
1303 AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
1305 WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
1306 AND NOT COALESCE (${arap}.storno, 'f')
1307 AND (${arap}.${ct}_id = ${ct}.id)
1310 AND (transdate <= (date $todate) $fromwhere )
1311 $review_of_aging_list
1313 ORDER BY ctid, transdate, invnumber |;
1315 my $sth_details = prepare_query($form, $dbh, $q_details);
1317 # select outstanding vendors or customers, depends on $ct
1319 qq|SELECT DISTINCT ct.id, ct.name
1320 FROM $ct ct, $arap a
1323 AND (a.${ct_id} = ct.id)
1324 AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
1325 AND (a.transdate <= $todate $fromwhere)
1328 my $sth = prepare_execute_query($form, $dbh, $query);
1331 # for each company that has some stuff outstanding
1332 while (my ($id) = $sth->fetchrow_array) {
1333 do_statement($form, $sth_details, $q_details, $id);
1335 while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
1336 $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
1337 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
1338 push @{ $form->{AG} }, $ref;
1341 $sth_details->finish;
1347 $main::lxdebug->leave_sub();
1351 $main::lxdebug->enter_sub();
1353 my ($self, $myconfig, $form) = @_;
1355 my $dbh = SL::DB->client->dbh;
1357 my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
1360 qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
1363 ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
1364 selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
1366 $main::lxdebug->leave_sub();
1370 $main::lxdebug->enter_sub();
1372 my ($self, $myconfig, $form) = @_;
1374 my $dbh = SL::DB->client->dbh;
1376 my ($null, $department_id) = split /--/, $form->{department};
1379 my $where = "1 = 1";
1381 if ($department_id) {
1382 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
1387 if ($form->{accno}) {
1388 $accno = $form->{accno};
1389 $rate = $form->{"$form->{accno}_rate"};
1390 $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
1396 if ($form->{db} eq 'ar') {
1397 $table = "customer";
1404 my $arap = lc($ARAP);
1406 my $transdate = "a.transdate";
1408 if ($form->{method} eq 'cash') {
1409 $transdate = "a.datepaid";
1411 my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
1418 WHERE (a.chart_link LIKE '%${ARAP}_paid%')
1419 AND (transdate <= $todate)
1424 # if there are any dates construct a where
1425 $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
1426 $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
1428 my $ml = ($form->{db} eq 'ar') ? 1 : -1;
1430 my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
1431 $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
1434 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
1435 ac.amount * $ml AS tax
1437 JOIN ${arap} a ON (a.id = ac.trans_id)
1438 JOIN chart ch ON (ch.id = ac.chart_id)
1439 JOIN $table n ON (n.id = a.${table}_id)
1443 AND (a.invoice = '0')
1447 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
1448 i.sellprice * i.qty * $rate * $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)
1453 JOIN ${table}tax t ON (t.${table}_id = n.id)
1454 JOIN invoice i ON (i.trans_id = a.id)
1458 AND (a.invoice = '1')
1459 ORDER BY $sortorder|;
1461 $form->{TR} = selectall_hashref_query($form, $dbh, $query);
1463 $main::lxdebug->leave_sub();
1466 sub paymentaccounts {
1467 $main::lxdebug->enter_sub();
1469 my ($self, $myconfig, $form) = @_;
1471 # connect to database, turn AutoCommit off
1472 my $dbh = SL::DB->client->dbh;
1474 my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
1476 # get A(R|P)_paid accounts
1478 qq|SELECT accno, description
1480 WHERE link LIKE '%${ARAP}_paid%'|;
1481 $form->{PR} = selectall_hashref_query($form, $dbh, $query);
1483 $main::lxdebug->leave_sub();
1487 $main::lxdebug->enter_sub();
1489 my ($self, $myconfig, $form) = @_;
1491 # connect to database, turn AutoCommit off
1492 my $dbh = SL::DB->client->dbh;
1497 if ($form->{db} eq 'ar') {
1498 $table = 'customer';
1509 if ($form->{department_id}) {
1510 $where = qq| AND (a.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
1513 if ($form->{fromdate}) {
1514 $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
1516 if ($form->{todate}) {
1517 $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
1519 if (!$form->{fx_transaction}) {
1520 $where .= " AND ac.fx_transaction = '0'";
1525 if ($form->{reference}) {
1526 $reference = $dbh->quote(like($form->{reference}));
1527 $invnumber = " AND (a.invnumber LIKE $reference)";
1528 $reference = " AND (a.reference LIKE $reference)";
1530 if ($form->{source}) {
1531 $where .= " AND (ac.source ILIKE " . $dbh->quote(like($form->{source})) . ") ";
1533 if ($form->{memo}) {
1534 $where .= " AND (ac.memo ILIKE " . $dbh->quote(like($form->{memo})) . ") ";
1537 my %sort_columns = (
1538 'transdate' => [ qw(transdate lower_invnumber lower_name) ],
1539 'invnumber' => [ qw(lower_invnumber lower_name transdate) ],
1540 'name' => [ qw(lower_name transdate) ],
1541 'source' => [ qw(lower_source) ],
1542 'memo' => [ qw(lower_memo) ],
1544 my %lowered_columns = (
1545 'invnumber' => { 'gl' => 'a.reference', 'arap' => 'a.invnumber', },
1546 'memo' => { 'gl' => 'ac.memo', 'arap' => 'ac.memo', },
1547 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
1548 'name' => { 'gl' => 'a.description', 'arap' => 'c.name', },
1551 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
1552 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
1553 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
1556 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
1557 foreach my $spec (@{ $sort_columns{$sortkey} }) {
1558 next if ($spec !~ m/^lower_(.*)$/);
1561 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
1564 $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
1565 $sth = prepare_query($form, $dbh, $query);
1568 qq|SELECT c.name, a.invnumber, a.ordnumber,
1569 ac.transdate, ac.amount * $ml AS paid, ac.source,
1570 a.invoice, a.id, ac.memo, '${arap}' AS module
1571 $columns_for_sorting{arap}
1573 JOIN $arap a ON (ac.trans_id = a.id)
1574 JOIN $table c ON (c.id = a.${table}_id)
1575 WHERE (ac.chart_id = ?)
1581 SELECT a.description, a.reference, NULL AS ordnumber,
1582 ac.transdate, ac.amount * $ml AS paid, ac.source,
1583 '0' as invoice, a.id, ac.memo, 'gl' AS module
1584 $columns_for_sorting{gl}
1586 JOIN gl a ON (a.id = ac.trans_id)
1587 WHERE (ac.chart_id = ?)
1590 AND (ac.amount * $ml) > 0
1592 ORDER BY $sortorder|;
1593 my $sth_details = prepare_query($form, $dbh, $q_details);
1597 # cycle through each id
1598 foreach my $accno (split(/ /, $form->{paymentaccounts})) {
1599 do_statement($form, $sth, $query, $accno);
1600 my $ref = $sth->fetchrow_hashref();
1601 push(@{ $form->{PR} }, $ref);
1604 $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
1606 do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
1607 while (my $pr = $sth_details->fetchrow_hashref()) {
1608 push(@{ $form->{ $ref->{id} } }, $pr);
1610 $sth_details->finish();
1613 $main::lxdebug->leave_sub();
1617 $main::lxdebug->enter_sub();
1619 my ($self, $myconfig, $form) = @_;
1621 my $dbh = SL::DB->client->dbh;
1623 my $last_period = 0;
1626 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);
1628 $form->{decimalplaces} *= 1;
1630 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
1632 # if there are any compare dates
1634 if ($form->{fromdate} || $form->{todate}) {
1636 if ($form->{fromdate}) {
1637 $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1640 $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1643 my $kummfromdate = $form->{comparefromdate};
1644 my $kummtodate = $form->{comparetodate};
1645 &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
1648 my @periods = qw(jetzt kumm);
1649 my @gesamtleistung = qw(1 3);
1650 my @gesamtkosten = qw (10 11 12 13 14 15 16 17 18 20);
1652 qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
1654 foreach my $key (@periods) {
1655 $form->{ "$key" . "gesamtleistung" } = 0;
1656 $form->{ "$key" . "gesamtkosten" } = 0;
1658 foreach $category (@categories) {
1660 if (defined($form->{$category}{$key})) {
1661 $form->{"$key$category"} =
1662 $form->format_amount($myconfig,
1663 $form->round_amount($form->{$category}{$key}, 2
1665 $form->{decimalplaces},
1669 foreach my $item (@gesamtleistung) {
1670 $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
1672 $form->{ "$key" . "gesamtleistung" } -= $form->{2}{$key};
1674 foreach my $item (@gesamtkosten) {
1675 $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
1677 $form->{ "$key" . "rohertrag" } =
1678 $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
1679 $form->{ "$key" . "betriebrohertrag" } =
1680 $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
1681 $form->{ "$key" . "betriebsergebnis" } =
1682 $form->{ "$key" . "betriebrohertrag" } -
1683 $form->{ "$key" . "gesamtkosten" };
1684 $form->{ "$key" . "neutraleraufwand" } =
1685 $form->{19}{$key} + $form->{30}{$key} + $form->{31}{$key};
1686 $form->{ "$key" . "neutralerertrag" } =
1687 $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
1688 $form->{ "$key" . "ergebnisvorsteuern" } =
1689 $form->{ "$key" . "betriebsergebnis" } -
1690 $form->{ "$key" . "neutraleraufwand" } +
1691 $form->{ "$key" . "neutralerertrag" };
1692 $form->{ "$key" . "ergebnis" } =
1693 $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
1695 if ($form->{ "$key" . "gesamtleistung" } > 0) {
1696 foreach $category (@categories) {
1697 if (defined($form->{$category}{$key})) {
1698 $form->{ "$key" . "gl" . "$category" } =
1699 $form->format_amount(
1701 $form->round_amount(
1702 ($form->{$category}{$key} /
1703 $form->{ "$key" . "gesamtleistung" } * 100
1705 $form->{decimalplaces}
1707 $form->{decimalplaces},
1711 foreach my $item (@ergebnisse) {
1712 $form->{ "$key" . "gl" . "$item" } =
1713 $form->format_amount($myconfig,
1714 $form->round_amount(
1715 ( $form->{ "$key" . "$item" } /
1716 $form->{ "$key" . "gesamtleistung" } * 100
1718 $form->{decimalplaces}
1720 $form->{decimalplaces},
1725 if ($form->{ "$key" . "gesamtkosten" } > 0) {
1726 foreach $category (@categories) {
1727 if (defined($form->{$category}{$key})) {
1728 $form->{ "$key" . "gk" . "$category" } =
1729 $form->format_amount($myconfig,
1730 $form->round_amount(
1731 ($form->{$category}{$key} /
1732 $form->{ "$key" . "gesamtkosten" } * 100
1734 $form->{decimalplaces}
1736 $form->{decimalplaces},
1740 foreach my $item (@ergebnisse) {
1741 $form->{ "$key" . "gk" . "$item" } =
1742 $form->format_amount($myconfig,
1743 $form->round_amount(
1744 ($form->{ "$key" . "$item" } /
1745 $form->{ "$key" . "gesamtkosten" } * 100
1747 $form->{decimalplaces}
1749 $form->{decimalplaces},
1754 if ($form->{10}{$key} > 0) {
1755 foreach $category (@categories) {
1756 if (defined($form->{$category}{$key})) {
1757 $form->{ "$key" . "pk" . "$category" } =
1758 $form->format_amount(
1760 $form->round_amount(
1761 ($form->{$category}{$key} / $form->{10}{$key} * 100),
1762 $form->{decimalplaces}
1764 $form->{decimalplaces},
1768 foreach my $item (@ergebnisse) {
1769 $form->{ "$key" . "pk" . "$item" } =
1770 $form->format_amount($myconfig,
1771 $form->round_amount(
1772 ($form->{ "$key" . "$item" } /
1773 $form->{10}{$key} * 100
1775 $form->{decimalplaces}
1777 $form->{decimalplaces},
1782 if ($form->{4}{$key} > 0) {
1783 foreach $category (@categories) {
1784 if (defined($form->{$category}{$key})) {
1785 $form->{ "$key" . "auf" . "$category" } =
1786 $form->format_amount(
1788 $form->round_amount(
1789 ($form->{$category}{$key} / $form->{4}{$key} * 100),
1790 $form->{decimalplaces}
1792 $form->{decimalplaces},
1796 foreach my $item (@ergebnisse) {
1797 $form->{ "$key" . "auf" . "$item" } =
1798 $form->format_amount($myconfig,
1799 $form->round_amount(
1800 ($form->{ "$key" . "$item" } /
1801 $form->{4}{$key} * 100
1803 $form->{decimalplaces}
1805 $form->{decimalplaces},
1810 foreach my $item (@ergebnisse) {
1811 $form->{ "$key" . "$item" } =
1812 $form->format_amount($myconfig,
1813 $form->round_amount($form->{ "$key" . "$item" },
1814 $form->{decimalplaces}
1816 $form->{decimalplaces},
1822 $main::lxdebug->leave_sub();
1825 sub income_statement {
1826 $main::lxdebug->enter_sub();
1828 my ($self, $myconfig, $form) = @_;
1830 # connect to database
1831 my $dbh = $form->dbconnect($myconfig);
1833 my $last_period = 0;
1834 my @categories_einnahmen = qw(1 2 3 4 5 6 7);
1835 my @categories_ausgaben =
1836 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);
1838 my @ergebnisse = qw(sumeura sumeurb guvsumme);
1840 $form->{decimalplaces} *= 1;
1844 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
1848 foreach my $item (@categories_einnahmen) {
1849 $form->{"eur${item}"} =
1850 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1851 $form->{"sumeura"} += $form->{$item};
1853 foreach my $item (@categories_ausgaben) {
1854 $form->{"eur${item}"} =
1855 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1856 $form->{"sumeurb"} += $form->{$item};
1859 $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
1861 foreach my $item (@ergebnisse) {
1863 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1865 $main::lxdebug->leave_sub();
1868 sub erfolgsrechnung {
1869 $main::lxdebug->enter_sub();
1871 my ($self, $myconfig, $form) = @_;
1872 $form->{company} = $::instance_conf->get_company;
1873 $form->{address} = $::instance_conf->get_address;
1874 $form->{fromdate} = DateTime->new(year => 2000, month => 1, day => 1)->to_kivitendo unless $form->{fromdate};
1875 $form->{todate} = $form->current_date(%{$myconfig}) unless $form->{todate};
1877 my %categories = (I => "ERTRAG", E => "AUFWAND");
1878 my $fromdate = conv_dateq($form->{fromdate});
1879 my $todate = conv_dateq($form->{todate});
1883 foreach my $category ('I', 'E') {
1885 name => $categories{$category},
1887 accounts => get_accounts_ch($category),
1889 foreach my $account (@{$category{accounts}}) {
1890 $account->{total} += get_total_ch($account->{id}, $fromdate, $todate);
1891 $category{total} += $account->{total};
1892 $account->{total} = $form->format_amount($myconfig, $form->parse_amount($myconfig, $account->{total}), 2);
1894 $form->{total} += $category{total};
1895 $category{total} = $form->format_amount($myconfig, $form->parse_amount($myconfig, $category{total}), 2);
1896 push(@{$form->{categories}}, \%category);
1898 $form->{total} = $form->format_amount($myconfig, $form->parse_amount($myconfig, $form->{total}), 2);
1900 $main::lxdebug->leave_sub();
1904 sub get_accounts_ch {
1905 $main::lxdebug->enter_sub();
1907 my ($category) = @_;
1910 if ($category eq 'I') {
1911 $inclusion = "AND pos_er = NULL OR pos_er = '1'";
1912 } elsif ($category eq 'E') {
1913 $inclusion = "AND pos_er = NULL OR pos_er = '6'";
1919 SELECT id, accno, description, category
1921 WHERE category = ? $inclusion
1924 my $accounts = _query($query, $category);
1926 $main::lxdebug->leave_sub();
1931 $main::lxdebug->enter_sub();
1933 my ($chart_id, $fromdate, $todate) = @_;
1942 $total += _query($query, $chart_id, $fromdate, $todate)->[0]->{sum};
1944 $main::lxdebug->leave_sub();
1948 sub _query {return selectall_hashref_query($::form, $::form->get_standard_dbh, @_);}