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., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
31 # backend code for reports
33 #======================================================================
39 use List::Util qw(sum);
44 # new implementation of balance sheet
47 # stuff missing from the original implementation:
50 # - proper testing for heading charts
51 # - transmission from $form to TMPL realm is not as clear as i'd like
53 sub get_openbalance_date {
54 my ($closedto, $target) = map { $::locale->parse_date_to_object(\%::myconfig, $_) } @_;
56 return unless $closedto;
58 $closedto->subtract(years => 1) while ($target - $closedto)->is_negative;
59 $closedto->add(days => 1);
60 return $::locale->format_date(\%::myconfig, $closedto);
64 $main::lxdebug->enter_sub();
66 my $myconfig = \%main::myconfig;
67 my $form = $main::form;
68 my $dbh = $::form->get_standard_dbh;
71 my @categories = qw(A C L Q);
73 # if there are any dates construct a where
74 if ($form->{asofdate}) {
75 $form->{period} = $form->{this_period} = conv_dateq($form->{asofdate});
78 # get end of financial year and convert to Date format
79 my ($closedto) = selectfirst_array_query($form, $dbh, 'SELECT closedto FROM defaults');
81 # get date of last opening balance
82 my $startdate = get_openbalance_date($closedto, $form->{asofdate});
84 get_accounts($dbh, $last_period, $startdate, $form->{asofdate}, $form, \@categories);
86 # if there are any compare dates
87 if ($form->{compareasofdate}) {
90 $startdate = get_openbalance_date($closedto, $form->{compareasofdate});
92 get_accounts($dbh, $last_period, $startdate, $form->{compareasofdate}, $form, \@categories);
93 $form->{last_period} = conv_dateq($form->{compareasofdate});
96 # now we got $form->{A}{accno}{ } assets
97 # and $form->{L}{accno}{ } liabilities
98 # and $form->{Q}{accno}{ } equity
99 # build asset accounts
101 my %account = ('A' => { 'ml' => -1 },
102 'L' => { 'ml' => 1 },
103 'Q' => { 'ml' => 1 });
107 foreach my $category (grep { !/C/ } @categories) {
109 $TMPL_DATA->{$category} = [];
110 my $ml = $account{$category}{ml};
112 foreach my $key (sort keys %{ $form->{$category} }) {
114 my $row = { %{ $form->{$category}{$key} } };
116 # if charttype "heading" - calculate this entry, start a new batch of charts belonging to this heading and skip the rest bo the loop
117 # header charts are not real charts. start a sub aggregation with them, but don't calculate anything with them
118 if ($row->{charttype} eq "H") {
119 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
120 $row->{subdescription} = $account{$category}{subdescription};
121 $row->{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
122 $row->{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
125 $row->{subheader} = 1;
126 $account{$category}{subthis} = $row->{this};
127 $account{$category}{sublast} = $row->{last};
128 $account{$category}{subdescription} = $row->{description};
129 $account{$category}{subtotal} = 1;
134 next unless $form->{l_heading};
137 for my $period (qw(this last)) {
138 next if ($period eq 'last' && !$last_period);
140 $row->{$period} *= $ml;
143 push @{ $TMPL_DATA->{$category} }, $row;
146 # resolve heading/subtotal
147 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
148 $TMPL_DATA->{$category}[-1]{subdescription} = $account{$category}{subdescription};
149 $TMPL_DATA->{$category}[-1]{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
150 $TMPL_DATA->{$category}[-1]{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
153 $TMPL_DATA->{total}{$category}{this} = sum map { $_->{this} } @{ $TMPL_DATA->{$category} };
154 $TMPL_DATA->{total}{$category}{last} = sum map { $_->{last} } @{ $TMPL_DATA->{$category} };
157 for my $period (qw(this last)) {
158 next if ($period eq 'last' && !$last_period);
160 $form->{E}{$period} = $TMPL_DATA->{total}{A}{$period} - $TMPL_DATA->{total}{L}{$period} - $TMPL_DATA->{total}{Q}{$period};
161 $TMPL_DATA->{total}{Q}{$period} += $form->{E}{$period};
162 $TMPL_DATA->{total}{$period} = $TMPL_DATA->{total}{L}{$period} + $TMPL_DATA->{total}{Q}{$period};
164 $form->{E}{description}='nicht verbuchter Gewinn/Verlust';
165 push @{ $TMPL_DATA->{Q} }, $form->{E};
167 $main::lxdebug->leave_sub();
173 $main::lxdebug->enter_sub();
175 my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
177 my ($null, $department_id) = split /--/, $form->{department};
181 my $dpt_where_without_arapgl = '';
188 my $dec = $form->{decimalplaces};
190 my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |;
194 qq|SELECT c.accno, c.description, c.category
196 WHERE (c.charttype = 'H')
200 $sth = prepare_execute_query($form, $dbh, $query);
202 my @headingaccounts = ();
203 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
204 $form->{ $ref->{category} }{ $ref->{accno} }{description} =
205 "$ref->{description}";
206 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
207 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
209 push @headingaccounts, $ref->{accno};
214 # if l_ob is selected l_cb is always ignored
215 if ( $form->{l_ob} ) {
216 $where .= ' AND ac.ob_transaction is true '
217 } elsif ( not $form->{l_cb} ) {
218 $where .= ' AND ac.cb_transaction is false ';
222 $fromdate = conv_dateq($fromdate);
223 if ($form->{method} eq 'cash') {
224 $subwhere .= " AND (transdate >= $fromdate)";
225 $glwhere = " AND (ac.transdate >= $fromdate)";
227 $where .= " AND (ac.transdate >= $fromdate)";
232 $todate = conv_dateq($todate);
233 $where .= " AND (ac.transdate <= $todate)";
234 $subwhere .= " AND (transdate <= $todate)";
237 if ($department_id) {
238 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
241 if ($form->{project_id}) {
242 # Diese Bedingung wird derzeit niemals wahr sein, da man in Bericht->Bilanz keine
243 # Projekte auswählen kann
244 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
247 if ($form->{method} eq 'cash') {
249 qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
251 JOIN chart c ON (c.id = ac.chart_id)
252 JOIN ar a ON (a.id = ac.trans_id)
260 JOIN chart c ON (a.chart_id = c.id)
261 WHERE (link LIKE '%AR_paid%')
265 GROUP BY c.accno, c.description, c.category
269 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
271 JOIN chart c ON (c.id = ac.chart_id)
272 JOIN ap a ON (a.id = ac.trans_id)
280 JOIN chart c ON (a.chart_id = c.id)
281 WHERE (link LIKE '%AP_paid%')
285 GROUP BY c.accno, c.description, c.category
289 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
291 JOIN chart c ON (c.id = ac.chart_id)
292 JOIN gl a ON (a.id = ac.trans_id)
297 AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
299 GROUP BY c.accno, c.description, c.category |;
301 if ($form->{project_id}) {
302 # s.o. keine Projektauswahl in Bilanz
307 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
309 JOIN ar a ON (a.id = ac.trans_id)
310 JOIN parts p ON (ac.parts_id = p.id)
311 JOIN chart c on (p.income_accno_id = c.id)
312 -- use transdate from subwhere
313 WHERE (c.category = 'I')
320 JOIN chart c ON (a.chart_id = c.id)
321 WHERE (link LIKE '%AR_paid%')
325 GROUP BY c.accno, c.description, c.category
329 SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
331 JOIN ap a ON (a.id = ac.trans_id)
332 JOIN parts p ON (ac.parts_id = p.id)
333 JOIN chart c on (p.expense_accno_id = c.id)
334 WHERE (c.category = 'E')
341 JOIN chart c ON (a.chart_id = c.id)
342 WHERE 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)";
482 $todate = conv_dateq($todate);
483 $subwhere .= " AND (transdate <= $todate)";
484 $where .= " AND (ac.transdate <= $todate)";
485 $prwhere .= " AND (a.transdate <= $todate)";
486 $inwhere .= " AND (acc.transdate <= $todate)";
489 if ($department_id) {
490 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
493 if ($form->{project_id}) {
494 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
498 # GUV patch by Ronny Rentner (Bug 1190)
500 # GUV IST-Versteuerung
502 # Alle tatsaechlichen _Zahlungseingaenge_
503 # im Zeitraum erfassen
504 # (Teilzahlungen werden prozentual auf verschiedene Steuern aufgeteilt)
508 if ($form->{method} eq 'cash') {
511 SELECT SUM( ac.amount * CASE WHEN COALESCE((SELECT amount FROM ar WHERE id = ac.trans_id), 0) != 0 THEN
512 /* ar amount is not zero, so we can divide by amount */
513 (SELECT SUM(acc.amount) * -1
515 INNER JOIN chart c ON (acc.chart_id = c.id AND c.link LIKE '%AR_paid%')
516 WHERE 1=1 $inwhere AND acc.trans_id = ac.trans_id)
517 / (SELECT amount FROM ar WHERE id = ac.trans_id)
519 /* 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 */
521 ) AS amount, c.$category
523 LEFT JOIN chart c ON (c.id = ac.chart_id)
524 LEFT JOIN ar ON (ar.id = ac.trans_id)
525 LEFT JOIN taxkeys tk ON (tk.id = (
526 SELECT id FROM taxkeys
527 WHERE chart_id = ac.chart_id
528 AND startdate <= COALESCE(ar.deliverydate,ar.transdate)
529 ORDER BY startdate DESC LIMIT 1
532 WHERE ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE 1=1 $subwhere)
537 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
539 JOIN chart c ON (c.id = ac.chart_id)
540 JOIN ar a ON (a.id = ac.trans_id)
541 WHERE $where $dpt_where
542 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a JOIN chart c ON (a.chart_id = c.id) WHERE (link LIKE '%AR_paid%') $subwhere)
548 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
550 JOIN chart c ON (c.id = ac.chart_id)
551 JOIN ap a ON (a.id = ac.trans_id)
552 WHERE $where $dpt_where
553 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a JOIN chart c ON (a.chart_id = c.id) WHERE (link LIKE '%AP_paid%') $subwhere)
559 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
561 JOIN chart c ON (c.id = ac.chart_id)
562 JOIN gl a ON (a.id = ac.trans_id)
563 WHERE $where $dpt_where $glwhere
564 AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
569 if ($form->{project_id}) {
573 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
575 JOIN ar a ON (a.id = ac.trans_id)
576 JOIN parts p ON (ac.parts_id = p.id)
577 JOIN chart c on (p.income_accno_id = c.id)
578 WHERE (c.category = 'I') $prwhere $dpt_where
579 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a JOIN chart c ON (a.chart_id = c.id) WHERE (link LIKE '%AR_paid%') $subwhere)
585 SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category
587 JOIN ap a ON (a.id = ac.trans_id)
588 JOIN parts p ON (ac.parts_id = p.id)
589 JOIN chart c on (p.expense_accno_id = c.id)
590 WHERE (c.category = 'E') $prwhere $dpt_where
591 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a JOIN chart c ON (a.chart_id = c.id) WHERE (link LIKE '%AP_paid%') $subwhere)
597 } else { # if ($form->{method} eq 'cash')
598 if ($department_id) {
599 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
600 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
601 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
602 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
606 SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
608 JOIN chart c ON (c.id = ac.chart_id)
610 $dpt_where_without_arapgl
612 GROUP BY c.$category |;
614 if ($form->{project_id}) {
618 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
620 JOIN ar a ON (a.id = ac.trans_id)
621 JOIN parts p ON (ac.parts_id = p.id)
622 JOIN chart c on (p.income_accno_id = c.id)
623 WHERE (c.category = 'I')
631 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
633 JOIN ap a ON (a.id = ac.trans_id)
634 JOIN parts p ON (ac.parts_id = p.id)
635 JOIN chart c on (p.expense_accno_id = c.id)
636 WHERE (c.category = 'E')
640 GROUP BY c.$category |;
648 foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
649 if ($category eq "pos_bwa") {
651 $form->{ $ref->{$category} }{kumm} += $ref->{amount};
653 $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
656 $form->{ $ref->{$category} } += $ref->{amount};
660 $main::lxdebug->leave_sub();
664 $main::lxdebug->enter_sub();
666 my ($self, $myconfig, $form, %options) = @_;
668 my $dbh = $form->dbconnect($myconfig);
670 my ($query, $sth, $ref);
673 my ($null, $department_id) = split /--/, $form->{department};
674 my @headingaccounts = ();
676 my $dpt_where_without_arapgl;
680 my $invwhere = $where;
682 if ($department_id) {
683 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
684 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
685 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
686 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
689 # project_id only applies to getting transactions
690 # it has nothing to do with a trial balance
691 # but we use the same function to collect information
693 if ($form->{project_id}) {
694 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
697 my $acc_cash_where = "";
698 # my $ar_cash_where = "";
699 # my $ap_cash_where = "";
702 if ($form->{method} eq "cash") {
704 qq| AND (ac.trans_id IN (
707 WHERE datepaid >= '$form->{fromdate}'
708 AND datepaid <= '$form->{todate}'
714 WHERE datepaid >= '$form->{fromdate}'
715 AND datepaid <= '$form->{todate}'
721 WHERE transdate >= '$form->{fromdate}'
722 AND transdate <= '$form->{todate}'
724 # $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
727 if ($options{beginning_balances}) {
728 foreach my $prefix (qw(from to)) {
729 next if ($form->{"${prefix}date"});
731 my $min_max = $prefix eq 'from' ? 'min' : 'max';
732 $query = qq|SELECT ${min_max}(transdate)
735 $dpt_where_without_arapgl
737 ($form->{"${prefix}date"}) = selectfirst_array_query($form, $dbh, $query);
740 # get beginning balances
742 qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
744 LEFT JOIN chart c ON (ac.chart_id = c.id)
745 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
746 $dpt_where_without_arapgl
748 GROUP BY c.accno, c.category, c.description |;
750 $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
752 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
754 if ($ref->{amount} != 0 || $form->{all_accounts}) {
755 $trb{ $ref->{accno} }{description} = $ref->{description};
756 $trb{ $ref->{accno} }{charttype} = 'A';
757 $trb{ $ref->{accno} }{beginning_balance} = $ref->{amount};
759 if ($ref->{amount} > 0) {
760 $trb{ $ref->{accno} }{haben_eb} = $ref->{amount};
762 $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1;
764 $trb{ $ref->{accno} }{category} = $ref->{category};
773 qq|SELECT c.accno, c.description, c.category
775 WHERE c.charttype = 'H'
778 $sth = prepare_execute_query($form, $dbh, $query);
780 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
781 $trb{ $ref->{accno} }{description} = $ref->{description};
782 $trb{ $ref->{accno} }{charttype} = 'H';
783 $trb{ $ref->{accno} }{category} = $ref->{category};
785 push @headingaccounts, $ref->{accno};
791 my $saldowhere = " 1 = 1 ";
792 my $sumwhere = " 1 = 1 ";
794 my $sumsubwhere = '';
795 my $saldosubwhere = '';
796 my $glsaldowhere = '';
801 my ($fromdate, $todate);
803 if ($form->{fromdate} || $form->{todate}) {
804 if ($form->{fromdate}) {
805 $fromdate = conv_dateq($form->{fromdate});
806 $tofrom .= " AND (ac.transdate >= $fromdate)";
807 $subwhere .= " AND (ac.transdate >= $fromdate)";
808 $sumsubwhere .= " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
809 $saldosubwhere .= " AND (ac,transdate>=(select date_trunc('year', date $fromdate))) ";
810 $invwhere .= " AND (a.transdate >= $fromdate)";
811 $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
812 $glwhere = " AND (ac.transdate >= $fromdate)";
813 $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
815 if ($form->{todate}) {
816 $todate = conv_dateq($form->{todate});
817 $tofrom .= " AND (ac.transdate <= $todate)";
818 $invwhere .= " AND (a.transdate <= $todate)";
819 $saldosubwhere .= " AND (ac.transdate <= $todate)";
820 $sumsubwhere .= " AND (ac.transdate <= $todate)";
821 $subwhere .= " AND (ac.transdate <= $todate)";
822 $glwhere .= " AND (ac.transdate <= $todate)";
823 $glsumwhere .= " AND (ac.transdate <= $todate) ";
824 $glsaldowhere .= " AND (ac.transdate <= $todate) ";
828 if ($form->{method} eq "cash") {
830 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) |;
831 $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) |;
833 $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) |;
835 $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
836 $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
837 $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
841 SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
843 JOIN chart c ON (c.id = ac.chart_id)
845 $dpt_where_without_arapgl
847 GROUP BY c.accno, c.description, c.category |;
849 if ($form->{project_id}) {
851 -- add project transactions from invoice
855 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
857 JOIN ar a ON (ac.trans_id = a.id)
858 JOIN parts p ON (ac.parts_id = p.id)
859 JOIN chart c ON (p.income_accno_id = c.id)
863 GROUP BY c.accno, c.description, c.category
867 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
869 JOIN ap a ON (ac.trans_id = a.id)
870 JOIN parts p ON (ac.parts_id = p.id)
871 JOIN chart c ON (p.expense_accno_id = c.id)
875 GROUP BY c.accno, c.description, c.category
879 $query .= qq| ORDER BY accno|;
881 $sth = prepare_execute_query($form, $dbh, $query);
883 # calculate the debit and credit in the period
884 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
885 $trb{ $ref->{accno} }{description} = $ref->{description};
886 $trb{ $ref->{accno} }{charttype} = 'A';
887 $trb{ $ref->{accno} }{category} = $ref->{category};
888 $trb{ $ref->{accno} }{amount} += $ref->{amount};
892 # prepare query for each account
893 my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
897 (SELECT SUM(ac.amount) * -1
899 JOIN chart c ON (c.id = ac.chart_id)
901 $dpt_where_without_arapgl
904 AND (c.accno = ?)) AS debit,
906 (SELECT SUM(ac.amount)
908 JOIN chart c ON (c.id = ac.chart_id)
910 $dpt_where_without_arapgl
913 AND c.accno = ?) AS credit,
914 (SELECT SUM(ac.amount)
916 JOIN chart c ON (ac.chart_id = c.id)
918 $dpt_where_without_arapgl
920 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
922 (SELECT SUM(ac.amount)
924 JOIN chart c ON (ac.chart_id = c.id)
926 $dpt_where_without_arapgl
929 AND c.accno = ?) AS sum_credit,
931 (SELECT SUM(ac.amount)
933 JOIN chart c ON (ac.chart_id = c.id)
935 $dpt_where_without_arapgl
938 AND c.accno = ?) AS sum_debit,
940 (SELECT max(ac.transdate) FROM acc_trans ac
941 JOIN chart c ON (ac.chart_id = c.id)
943 $dpt_where_without_arapgl
945 AND c.accno = ?) AS last_transaction
950 $drcr = prepare_query($form, $dbh, $q_drcr);
952 if ($form->{project_id}) {
953 # prepare query for each account
956 (SELECT SUM(ac.sellprice * ac.qty) * -1
958 JOIN parts p ON (ac.parts_id = p.id)
959 JOIN ap a ON (ac.trans_id = a.id)
960 JOIN chart c ON (p.expense_accno_id = c.id)
964 AND c.accno = ?) AS debit,
966 (SELECT SUM(ac.sellprice * ac.qty)
968 JOIN parts p ON (ac.parts_id = p.id)
969 JOIN ar a ON (ac.trans_id = a.id)
970 JOIN chart c ON (p.income_accno_id = c.id)
974 AND c.accno = ?) AS credit,
976 (SELECT SUM(ac.amount)
978 JOIN chart c ON (ac.chart_id = c.id)
980 $dpt_where_without_arapgl
982 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
984 (SELECT SUM(ac.amount)
986 JOIN chart c ON (ac.chart_id = c.id)
988 $dpt_where_without_arapgl
991 AND c.accno = ?) AS sum_credit,
993 (SELECT SUM(ac.amount)
995 JOIN chart c ON (ac.chart_id = c.id)
997 $dpt_where_without_arapgl
1000 AND c.accno = ?) AS sum_debit,
1003 (SELECT max(ac.transdate) FROM acc_trans ac
1004 JOIN chart c ON (ac.chart_id = c.id)
1006 $dpt_where_without_arapgl
1008 AND c.accno = ?) AS last_transaction
1011 $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
1015 my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
1017 foreach my $accno (sort keys %trb) {
1020 $ref->{accno} = $accno;
1021 map { $ref->{$_} = $trb{$accno}{$_} }
1022 qw(description category charttype amount soll_eb haben_eb beginning_balance);
1024 $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
1026 if ($trb{$accno}{charttype} eq 'A') {
1029 do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1031 ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
1032 my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
1033 $last_transaction = "";
1034 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
1035 $ref->{debit} += $debit;
1036 $ref->{credit} += $credit;
1038 $ref->{haben_saldo} += $saldo;
1040 $ref->{soll_saldo} += $saldo * -1;
1042 $ref->{last_transaction} = $last_transaction;
1043 $ref->{soll_kumuliert} = $soll_kumuliert * -1;
1044 $ref->{haben_kumuliert} = $haben_kumuliert;
1048 if ($form->{project_id}) {
1051 do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1053 ($debit, $credit) = (0, 0);
1054 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
1055 $ref->{debit} += $debit;
1056 $ref->{credit} += $credit;
1058 $ref->{haben_saldo} += $saldo;
1060 $ref->{soll_saldo} += $saldo * -1;
1062 $ref->{soll_kumuliert} += $soll_kumuliert * -1;
1063 $ref->{haben_kumuliert} += $haben_kumuliert;
1065 $project_drcr->finish;
1068 $ref->{debit} = $form->round_amount($ref->{debit}, 2);
1069 $ref->{credit} = $form->round_amount($ref->{credit}, 2);
1071 if ($ref->{haben_saldo} != 0) {
1072 $ref->{haben_saldo} = $ref->{haben_saldo} + $ref->{beginning_balance};
1073 if ($ref->{haben_saldo} < 0) {
1074 $ref->{soll_saldo} = $form->round_amount(($ref->{haben_saldo} *- 1), 2);
1075 $ref->{haben_saldo} = 0;
1078 $ref->{soll_saldo} = $ref->{soll_saldo} - $ref->{beginning_balance};
1079 if ($ref->{soll_saldo} < 0) {
1080 $ref->{haben_saldo} = $form->round_amount(($ref->{soll_saldo} * -1), 2);
1081 $ref->{soll_saldo} = 0;
1084 $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
1085 $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
1086 $ref->{haben_kumuliert} = $form->round_amount($ref->{haben_kumuliert}, 2);
1087 $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
1092 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1093 $accno = pop @accno;
1095 $trb{$accno}{debit} += $ref->{debit};
1096 $trb{$accno}{credit} += $ref->{credit};
1097 $trb{$accno}{soll_saldo} += $ref->{soll_saldo};
1098 $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
1099 $trb{$accno}{soll_kumuliert} += $ref->{soll_kumuliert};
1100 $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
1103 push @{ $form->{TB} }, $ref;
1109 # debits and credits for headings
1110 foreach my $accno (@headingaccounts) {
1111 foreach $ref (@{ $form->{TB} }) {
1112 if ($accno eq $ref->{accno}) {
1113 $ref->{debit} = $trb{$accno}{debit};
1114 $ref->{credit} = $trb{$accno}{credit};
1115 $ref->{soll_saldo} = $trb{$accno}{soll_saldo};
1116 $ref->{haben_saldo} = $trb{$accno}{haben_saldo};
1117 $ref->{soll_kumuliert} = $trb{$accno}{soll_kumuliert};
1118 $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert};
1123 $main::lxdebug->leave_sub();
1127 $main::lxdebug->enter_sub();
1128 my ($self, $dbh, $form) = @_;
1129 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
1130 my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
1131 my $sth = $dbh->prepare($query);
1132 while(my $ref = $sth->fetchrow_hashref()) {
1133 $ref->{invnumer} =~ s/Storno zu //g;
1134 $form->{storno}{$ref->{invnumber}} = 1;
1136 $main::lxdebug->leave_sub();
1140 $main::lxdebug->enter_sub();
1142 my ($self, $myconfig, $form) = @_;
1144 # connect to database
1145 my $dbh = $form->dbconnect($myconfig);
1147 my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
1149 # falls customer ziehen wir die offene forderungsliste
1150 # anderfalls für die lieferanten die offenen verbindlichkeitne
1151 if ($form->{ct} eq "customer") {
1164 $ct_id = "${ct}_id";
1166 # erweiterung um einen freien zeitraum oder einen stichtag
1167 # mit entsprechender altersstrukturliste (s.a. Bug 1842)
1168 # eine neue variable an der oberfläche eingeführt, somit ist
1169 # todate == freier zeitrau und fordate == stichtag
1171 my ($review_of_aging_list, $todate, $fromdate, $fromwhere, $fordate);
1173 if ($form->{reporttype} eq 'custom') { # altersstrukturliste
1175 # explizit rausschmeissen was man für diesen bericht nicht braucht
1176 delete $form->{fromdate};
1177 delete $form->{todate};
1179 # an der oberfläche ist das tagesaktuelle datum vorausgewählt
1180 # falls es dennoch per Benutzereingabe gelöscht wird, lieber wieder vorbelegen
1181 # ferner muss für die spätere DB-Abfrage muss todate gesetzt sein.
1182 $form->{fordate} = $form->current_date($myconfig) unless ($form->{fordate});
1183 $fordate = conv_dateq($form->{fordate});
1186 if ($form->{review_of_aging_list}) { # falls die liste leer ist, alles anzeigen
1187 if ($form->{review_of_aging_list} =~ m "-") { # .. periode von bis
1188 my @period = split(/-/, $form->{review_of_aging_list}); # ... von periode bis periode
1189 $review_of_aging_list = " AND $period[0] < (date $fordate) - duedate
1190 AND (date $fordate) - duedate < $period[1]";
1192 $form->{review_of_aging_list} =~ s/[^0-9]//g; # größer 120 das substitute ist nur für das '>' zeichen
1193 $review_of_aging_list = " AND $form->{review_of_aging_list} < (date $fordate) - duedate";
1196 } else { # freier zeitraum OHNE review_of_aging_list
1197 $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
1198 $todate = conv_dateq($form->{todate});
1199 $fromdate = conv_dateq($form->{fromdate});
1200 $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
1202 my $where = " 1 = 1 ";
1205 if ($form->{$ct_id}) {
1206 $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
1207 } elsif ($form->{ $form->{ct} }) {
1208 $where .= qq| AND (ct.name ILIKE | . $dbh->quote('%' . $form->{$ct} . '%') . qq|)|;
1213 if ($form->{department}) {
1214 my ($null, $department_id) = split /--/, $form->{department};
1215 $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
1216 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1217 $where_dpt = qq| AND (${arap}.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1221 SELECT ${ct}.id AS ctid, ${ct}.name,
1222 street, zipcode, city, country, contact, email,
1223 phone as customerphone, fax as customerfax, ${ct}number,
1224 "invnumber", "transdate",
1225 (amount - COALESCE((SELECT sum(amount)*$ml FROM acc_trans LEFT JOIN chart ON (acc_trans.chart_id=chart.id) WHERE link ilike '%paid%' AND acc_trans.trans_id=${arap}.id AND acc_trans.transdate <= (date $todate)),0)) as "open", "amount",
1226 "duedate", invoice, ${arap}.id, date_part('days', now() - duedate) as overduedays,
1229 WHERE (${arap}.curr = exchangerate.curr)
1230 AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
1232 WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
1233 AND NOT COALESCE (${arap}.storno, 'f')
1234 AND (${arap}.${ct}_id = ${ct}.id)
1237 AND (transdate <= (date $todate) $fromwhere )
1238 $review_of_aging_list
1239 ORDER BY ctid, transdate, invnumber |;
1241 my $sth_details = prepare_query($form, $dbh, $q_details);
1243 # select outstanding vendors or customers, depends on $ct
1245 qq|SELECT DISTINCT ct.id, ct.name
1246 FROM $ct ct, $arap a
1249 AND (a.${ct_id} = ct.id)
1250 AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
1251 AND (a.transdate <= $todate $fromwhere)
1254 my $sth = prepare_execute_query($form, $dbh, $query);
1257 # for each company that has some stuff outstanding
1258 while (my ($id) = $sth->fetchrow_array) {
1259 do_statement($form, $sth_details, $q_details, $id);
1261 while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
1262 $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
1263 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
1264 push @{ $form->{AG} }, $ref;
1267 $sth_details->finish;
1276 $main::lxdebug->leave_sub();
1280 $main::lxdebug->enter_sub();
1282 my ($self, $myconfig, $form) = @_;
1284 # connect to database
1285 my $dbh = $form->dbconnect($myconfig);
1287 my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
1290 qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
1293 ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
1294 selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
1297 $main::lxdebug->leave_sub();
1301 $main::lxdebug->enter_sub();
1303 my ($self, $myconfig, $form) = @_;
1305 # connect to database
1306 my $dbh = $form->dbconnect($myconfig);
1308 my ($null, $department_id) = split /--/, $form->{department};
1311 my $where = "1 = 1";
1313 if ($department_id) {
1314 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
1319 if ($form->{accno}) {
1320 $accno = $form->{accno};
1321 $rate = $form->{"$form->{accno}_rate"};
1322 $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
1328 if ($form->{db} eq 'ar') {
1329 $table = "customer";
1336 my $arap = lc($ARAP);
1338 my $transdate = "a.transdate";
1340 if ($form->{method} eq 'cash') {
1341 $transdate = "a.datepaid";
1343 my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
1350 JOIN chart c ON (a.chart_id = c.id)
1351 WHERE (link LIKE '%${ARAP}_paid%')
1352 AND (transdate <= $todate)
1357 # if there are any dates construct a where
1358 $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
1359 $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
1361 my $ml = ($form->{db} eq 'ar') ? 1 : -1;
1363 my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
1364 $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
1367 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
1368 ac.amount * $ml AS tax
1370 JOIN ${arap} a ON (a.id = ac.trans_id)
1371 JOIN chart ch ON (ch.id = ac.chart_id)
1372 JOIN $table n ON (n.id = a.${table}_id)
1376 AND (a.invoice = '0')
1380 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
1381 i.sellprice * i.qty * $rate * $ml AS tax
1383 JOIN ${arap} a ON (a.id = ac.trans_id)
1384 JOIN chart ch ON (ch.id = ac.chart_id)
1385 JOIN $table n ON (n.id = a.${table}_id)
1386 JOIN ${table}tax t ON (t.${table}_id = n.id)
1387 JOIN invoice i ON (i.trans_id = a.id)
1391 AND (a.invoice = '1')
1392 ORDER BY $sortorder|;
1394 $form->{TR} = selectall_hashref_query($form, $dbh, $query);
1398 $main::lxdebug->leave_sub();
1401 sub paymentaccounts {
1402 $main::lxdebug->enter_sub();
1404 my ($self, $myconfig, $form) = @_;
1406 # connect to database, turn AutoCommit off
1407 my $dbh = $form->dbconnect_noauto($myconfig);
1409 my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
1411 # get A(R|P)_paid accounts
1413 qq|SELECT accno, description
1415 WHERE link LIKE '%${ARAP}_paid%'|;
1416 $form->{PR} = selectall_hashref_query($form, $dbh, $query);
1420 $main::lxdebug->leave_sub();
1424 $main::lxdebug->enter_sub();
1426 my ($self, $myconfig, $form) = @_;
1428 # connect to database, turn AutoCommit off
1429 my $dbh = $form->dbconnect_noauto($myconfig);
1434 if ($form->{db} eq 'ar') {
1435 $table = 'customer';
1446 if ($form->{department_id}) {
1447 $where = qq| AND (a.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
1450 if ($form->{fromdate}) {
1451 $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
1453 if ($form->{todate}) {
1454 $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
1456 if (!$form->{fx_transaction}) {
1457 $where .= " AND ac.fx_transaction = '0'";
1462 if ($form->{reference}) {
1463 $reference = $dbh->quote('%' . $form->{reference} . '%');
1464 $invnumber = " AND (a.invnumber LIKE $reference)";
1465 $reference = " AND (a.reference LIKE $reference)";
1467 if ($form->{source}) {
1468 $where .= " AND (ac.source ILIKE " . $dbh->quote('%' . $form->{source} . '%') . ") ";
1470 if ($form->{memo}) {
1471 $where .= " AND (ac.memo ILIKE " . $dbh->quote('%' . $form->{memo} . '%') . ") ";
1474 my %sort_columns = (
1475 'transdate' => [ qw(transdate lower_invnumber lower_name) ],
1476 'invnumber' => [ qw(lower_invnumber lower_name transdate) ],
1477 'name' => [ qw(lower_name transdate) ],
1478 'source' => [ qw(lower_source) ],
1479 'memo' => [ qw(lower_memo) ],
1481 my %lowered_columns = (
1482 'invnumber' => { 'gl' => 'a.reference', 'arap' => 'a.invnumber', },
1483 'memo' => { 'gl' => 'ac.memo', 'arap' => 'ac.memo', },
1484 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
1485 'name' => { 'gl' => 'a.description', 'arap' => 'c.name', },
1488 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
1489 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
1490 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
1493 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
1494 foreach my $spec (@{ $sort_columns{$sortkey} }) {
1495 next if ($spec !~ m/^lower_(.*)$/);
1498 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
1501 $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
1502 $sth = prepare_query($form, $dbh, $query);
1505 qq|SELECT c.name, a.invnumber, a.ordnumber,
1506 ac.transdate, ac.amount * $ml AS paid, ac.source,
1507 a.invoice, a.id, ac.memo, '${arap}' AS module
1508 $columns_for_sorting{arap}
1510 JOIN $arap a ON (ac.trans_id = a.id)
1511 JOIN $table c ON (c.id = a.${table}_id)
1512 WHERE (ac.chart_id = ?)
1518 SELECT a.description, a.reference, NULL AS ordnumber,
1519 ac.transdate, ac.amount * $ml AS paid, ac.source,
1520 '0' as invoice, a.id, ac.memo, 'gl' AS module
1521 $columns_for_sorting{gl}
1523 JOIN gl a ON (a.id = ac.trans_id)
1524 WHERE (ac.chart_id = ?)
1527 AND (ac.amount * $ml) > 0
1529 ORDER BY $sortorder|;
1530 my $sth_details = prepare_query($form, $dbh, $q_details);
1534 # cycle through each id
1535 foreach my $accno (split(/ /, $form->{paymentaccounts})) {
1536 do_statement($form, $sth, $query, $accno);
1537 my $ref = $sth->fetchrow_hashref();
1538 push(@{ $form->{PR} }, $ref);
1541 $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
1543 do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
1544 while (my $pr = $sth_details->fetchrow_hashref()) {
1545 push(@{ $form->{ $ref->{id} } }, $pr);
1547 $sth_details->finish();
1552 $main::lxdebug->leave_sub();
1556 $main::lxdebug->enter_sub();
1558 my ($self, $myconfig, $form) = @_;
1560 # connect to database
1561 my $dbh = $form->dbconnect($myconfig);
1563 my $last_period = 0;
1566 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);
1568 $form->{decimalplaces} *= 1;
1570 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
1572 # if there are any compare dates
1574 if ($form->{fromdate} || $form->{todate}) {
1576 if ($form->{fromdate}) {
1577 $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1580 $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1583 my $kummfromdate = $form->{comparefromdate};
1584 my $kummtodate = $form->{comparetodate};
1585 &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
1588 my @periods = qw(jetzt kumm);
1589 my @gesamtleistung = qw(1 3);
1590 my @gesamtkosten = qw (10 11 12 13 14 15 16 17 18 20);
1592 qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
1594 foreach my $key (@periods) {
1595 $form->{ "$key" . "gesamtleistung" } = 0;
1596 $form->{ "$key" . "gesamtkosten" } = 0;
1598 foreach $category (@categories) {
1600 if (defined($form->{$category}{$key})) {
1601 $form->{"$key$category"} =
1602 $form->format_amount($myconfig,
1603 $form->round_amount($form->{$category}{$key}, 2
1605 $form->{decimalplaces},
1609 foreach my $item (@gesamtleistung) {
1610 $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
1612 $form->{ "$key" . "gesamtleistung" } -= $form->{2}{$key};
1614 foreach my $item (@gesamtkosten) {
1615 $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
1617 $form->{ "$key" . "rohertrag" } =
1618 $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
1619 $form->{ "$key" . "betriebrohertrag" } =
1620 $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
1621 $form->{ "$key" . "betriebsergebnis" } =
1622 $form->{ "$key" . "betriebrohertrag" } -
1623 $form->{ "$key" . "gesamtkosten" };
1624 $form->{ "$key" . "neutraleraufwand" } =
1625 $form->{19}{$key} + $form->{30}{$key} + $form->{31}{$key};
1626 $form->{ "$key" . "neutralerertrag" } =
1627 $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
1628 $form->{ "$key" . "ergebnisvorsteuern" } =
1629 $form->{ "$key" . "betriebsergebnis" } -
1630 $form->{ "$key" . "neutraleraufwand" } +
1631 $form->{ "$key" . "neutralerertrag" };
1632 $form->{ "$key" . "ergebnis" } =
1633 $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
1635 if ($form->{ "$key" . "gesamtleistung" } > 0) {
1636 foreach $category (@categories) {
1637 if (defined($form->{$category}{$key})) {
1638 $form->{ "$key" . "gl" . "$category" } =
1639 $form->format_amount(
1641 $form->round_amount(
1642 ($form->{$category}{$key} /
1643 $form->{ "$key" . "gesamtleistung" } * 100
1645 $form->{decimalplaces}
1647 $form->{decimalplaces},
1651 foreach my $item (@ergebnisse) {
1652 $form->{ "$key" . "gl" . "$item" } =
1653 $form->format_amount($myconfig,
1654 $form->round_amount(
1655 ( $form->{ "$key" . "$item" } /
1656 $form->{ "$key" . "gesamtleistung" } * 100
1658 $form->{decimalplaces}
1660 $form->{decimalplaces},
1665 if ($form->{ "$key" . "gesamtkosten" } > 0) {
1666 foreach $category (@categories) {
1667 if (defined($form->{$category}{$key})) {
1668 $form->{ "$key" . "gk" . "$category" } =
1669 $form->format_amount($myconfig,
1670 $form->round_amount(
1671 ($form->{$category}{$key} /
1672 $form->{ "$key" . "gesamtkosten" } * 100
1674 $form->{decimalplaces}
1676 $form->{decimalplaces},
1680 foreach my $item (@ergebnisse) {
1681 $form->{ "$key" . "gk" . "$item" } =
1682 $form->format_amount($myconfig,
1683 $form->round_amount(
1684 ($form->{ "$key" . "$item" } /
1685 $form->{ "$key" . "gesamtkosten" } * 100
1687 $form->{decimalplaces}
1689 $form->{decimalplaces},
1694 if ($form->{10}{$key} > 0) {
1695 foreach $category (@categories) {
1696 if (defined($form->{$category}{$key})) {
1697 $form->{ "$key" . "pk" . "$category" } =
1698 $form->format_amount(
1700 $form->round_amount(
1701 ($form->{$category}{$key} / $form->{10}{$key} * 100),
1702 $form->{decimalplaces}
1704 $form->{decimalplaces},
1708 foreach my $item (@ergebnisse) {
1709 $form->{ "$key" . "pk" . "$item" } =
1710 $form->format_amount($myconfig,
1711 $form->round_amount(
1712 ($form->{ "$key" . "$item" } /
1713 $form->{10}{$key} * 100
1715 $form->{decimalplaces}
1717 $form->{decimalplaces},
1722 if ($form->{4}{$key} > 0) {
1723 foreach $category (@categories) {
1724 if (defined($form->{$category}{$key})) {
1725 $form->{ "$key" . "auf" . "$category" } =
1726 $form->format_amount(
1728 $form->round_amount(
1729 ($form->{$category}{$key} / $form->{4}{$key} * 100),
1730 $form->{decimalplaces}
1732 $form->{decimalplaces},
1736 foreach my $item (@ergebnisse) {
1737 $form->{ "$key" . "auf" . "$item" } =
1738 $form->format_amount($myconfig,
1739 $form->round_amount(
1740 ($form->{ "$key" . "$item" } /
1741 $form->{4}{$key} * 100
1743 $form->{decimalplaces}
1745 $form->{decimalplaces},
1750 foreach my $item (@ergebnisse) {
1751 $form->{ "$key" . "$item" } =
1752 $form->format_amount($myconfig,
1753 $form->round_amount($form->{ "$key" . "$item" },
1754 $form->{decimalplaces}
1756 $form->{decimalplaces},
1763 $main::lxdebug->leave_sub();
1766 sub income_statement {
1767 $main::lxdebug->enter_sub();
1769 my ($self, $myconfig, $form) = @_;
1771 # connect to database
1772 my $dbh = $form->dbconnect($myconfig);
1774 my $last_period = 0;
1775 my @categories_einnahmen = qw(1 2 3 4 5 6 7);
1776 my @categories_ausgaben =
1777 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);
1779 my @ergebnisse = qw(sumeura sumeurb guvsumme);
1781 $form->{decimalplaces} *= 1;
1785 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
1789 foreach my $item (@categories_einnahmen) {
1790 $form->{"eur${item}"} =
1791 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1792 $form->{"sumeura"} += $form->{$item};
1794 foreach my $item (@categories_ausgaben) {
1795 $form->{"eur${item}"} =
1796 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1797 $form->{"sumeurb"} += $form->{$item};
1800 $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
1802 foreach my $item (@ergebnisse) {
1804 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1806 $main::lxdebug->leave_sub();