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 WHERE (a.chart_link LIKE '%AR_paid%')
264 GROUP BY c.accno, c.description, c.category
268 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
270 JOIN chart c ON (c.id = ac.chart_id)
271 JOIN ap a ON (a.id = ac.trans_id)
279 WHERE (a.chart_link LIKE '%AP_paid%')
283 GROUP BY c.accno, c.description, c.category
287 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
289 JOIN chart c ON (c.id = ac.chart_id)
290 JOIN gl a ON (a.id = ac.trans_id)
295 AND NOT ((ac.chart_link = 'AR') OR (ac.chart_link = 'AP'))
297 GROUP BY c.accno, c.description, c.category |;
299 if ($form->{project_id}) {
300 # s.o. keine Projektauswahl in Bilanz
305 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
307 JOIN ar a ON (a.id = ac.trans_id)
308 JOIN parts p ON (ac.parts_id = p.id)
309 JOIN chart c on (p.income_accno_id = c.id)
310 -- use transdate from subwhere
311 WHERE (c.category = 'I')
318 WHERE (a.chart_link LIKE '%AR_paid%')
322 GROUP BY c.accno, c.description, c.category
326 SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
328 JOIN ap a ON (a.id = ac.trans_id)
329 JOIN parts p ON (ac.parts_id = p.id)
330 JOIN chart c on (p.expense_accno_id = c.id)
331 WHERE (c.category = 'E')
338 WHERE a.chart_link LIKE '%AP_paid%'
342 GROUP BY c.accno, c.description, c.category |;
345 } else { # if ($form->{method} eq 'cash')
346 if ($department_id) {
347 $dpt_where = qq| AND a.department_id = | . conv_i($department_id);
348 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
349 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
350 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
354 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
356 JOIN chart c ON (c.id = ac.chart_id)
358 $dpt_where_without_arapgl
361 GROUP BY c.accno, c.description, c.category |;
363 if ($form->{project_id}) {
364 # s.o. keine Projektauswahl in Bilanz
368 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
370 JOIN ar a ON (a.id = ac.trans_id)
371 JOIN parts p ON (ac.parts_id = p.id)
372 JOIN chart c on (p.income_accno_id = c.id)
373 -- use transdate from subwhere
374 WHERE (c.category = 'I')
378 GROUP BY c.accno, c.description, c.category
382 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
384 JOIN ap a ON (a.id = ac.trans_id)
385 JOIN parts p ON (ac.parts_id = p.id)
386 JOIN chart c on (p.expense_accno_id = c.id)
387 WHERE (c.category = 'E')
391 GROUP BY c.accno, c.description, c.category |;
399 $sth = prepare_execute_query($form, $dbh, $query);
401 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
403 if ($ref->{category} eq 'C') {
404 $ref->{category} = 'A';
407 # get last heading account
408 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
412 $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
414 $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
418 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
419 $form->{ $ref->{category} }{ $ref->{accno} }{description} = $ref->{description};
420 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
423 $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
425 $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
430 # remove accounts with zero balance
431 foreach $category (@{$categories}) {
432 foreach $accno (keys %{ $form->{$category} }) {
433 $form->{$category}{$accno}{last} = $form->round_amount($form->{$category}{$accno}{last}, $dec);
434 $form->{$category}{$accno}{this} = $form->round_amount($form->{$category}{$accno}{this}, $dec);
436 delete $form->{$category}{$accno}
437 if ( $form->{$category}{$accno}{this} == 0
438 && $form->{$category}{$accno}{last} == 0);
442 $main::lxdebug->leave_sub();
446 $main::lxdebug->enter_sub();
448 my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
450 my ($null, $department_id) = split /--/, $form->{department};
454 my $dpt_where_without_arapgl;
463 $where .= ' AND ac.cb_transaction is false ' unless $form->{l_cb};
466 $fromdate = conv_dateq($fromdate);
467 if ($form->{method} eq 'cash') {
468 $subwhere .= " AND (transdate >= $fromdate)";
469 $glwhere = " AND (ac.transdate >= $fromdate)";
470 $prwhere = " AND (a.transdate >= $fromdate)";
471 $inwhere = " AND (acc.transdate >= $fromdate)";
473 $where .= " AND (ac.transdate >= $fromdate)";
478 $todate = conv_dateq($todate);
479 $subwhere .= " AND (transdate <= $todate)";
480 $where .= " AND (ac.transdate <= $todate)";
481 $prwhere .= " AND (a.transdate <= $todate)";
482 $inwhere .= " AND (acc.transdate <= $todate)";
485 if ($department_id) {
486 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
489 if ($form->{project_id}) {
490 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
494 # GUV patch by Ronny Rentner (Bug 1190)
496 # GUV IST-Versteuerung
498 # Alle tatsaechlichen _Zahlungseingaenge_
499 # im Zeitraum erfassen
500 # (Teilzahlungen werden prozentual auf verschiedene Steuern aufgeteilt)
504 if ($form->{method} eq 'cash') {
507 SELECT SUM( ac.amount * CASE WHEN COALESCE((SELECT amount FROM ar WHERE id = ac.trans_id), 0) != 0 THEN
508 /* ar amount is not zero, so we can divide by amount */
509 (SELECT SUM(acc.amount) * -1
512 AND acc.trans_id = ac.trans_id
513 AND acc.chart_link LIKE '%AR_paid%')
514 / (SELECT amount FROM ar WHERE id = ac.trans_id)
516 /* 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 */
518 ) AS amount, c.$category
520 LEFT JOIN chart c ON (c.id = ac.chart_id)
521 LEFT JOIN ar ON (ar.id = ac.trans_id)
522 LEFT JOIN taxkeys tk ON (tk.id = (
523 SELECT id FROM taxkeys
524 WHERE chart_id = ac.chart_id
525 AND startdate <= COALESCE(ar.deliverydate,ar.transdate)
526 ORDER BY startdate DESC LIMIT 1
529 WHERE ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE 1=1 $subwhere)
534 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
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)
545 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
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)
556 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
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'))
566 if ($form->{project_id}) {
570 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
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)
582 SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category
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)
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
605 JOIN chart c ON (c.id = ac.chart_id)
607 $dpt_where_without_arapgl
609 GROUP BY c.$category |;
611 if ($form->{project_id}) {
615 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
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')
628 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
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 |;
645 foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
646 if ($category eq "pos_bwa") {
648 $form->{ $ref->{$category} }{kumm} += $ref->{amount};
650 $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
653 $form->{ $ref->{$category} } += $ref->{amount};
657 $main::lxdebug->leave_sub();
661 $main::lxdebug->enter_sub();
663 my ($self, $myconfig, $form, %options) = @_;
665 my $dbh = $form->dbconnect($myconfig);
667 my ($query, $sth, $ref);
670 my ($null, $department_id) = split /--/, $form->{department};
671 my @headingaccounts = ();
673 my $dpt_where_without_arapgl;
677 my $invwhere = $where;
679 if ($department_id) {
680 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
681 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
682 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
683 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
686 # project_id only applies to getting transactions
687 # it has nothing to do with a trial balance
688 # but we use the same function to collect information
690 if ($form->{project_id}) {
691 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
694 my $acc_cash_where = "";
695 # my $ar_cash_where = "";
696 # my $ap_cash_where = "";
699 if ($form->{method} eq "cash") {
701 qq| AND (ac.trans_id IN (
704 WHERE datepaid >= '$form->{fromdate}'
705 AND datepaid <= '$form->{todate}'
711 WHERE datepaid >= '$form->{fromdate}'
712 AND datepaid <= '$form->{todate}'
718 WHERE transdate >= '$form->{fromdate}'
719 AND transdate <= '$form->{todate}'
721 # $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
724 if ($options{beginning_balances}) {
725 foreach my $prefix (qw(from to)) {
726 next if ($form->{"${prefix}date"});
728 my $min_max = $prefix eq 'from' ? 'min' : 'max';
729 $query = qq|SELECT ${min_max}(transdate)
732 $dpt_where_without_arapgl
734 ($form->{"${prefix}date"}) = selectfirst_array_query($form, $dbh, $query);
737 # get beginning balances
739 qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
741 LEFT JOIN chart c ON (ac.chart_id = c.id)
742 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
743 $dpt_where_without_arapgl
745 GROUP BY c.accno, c.category, c.description |;
747 $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
749 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
751 if ($ref->{amount} != 0 || $form->{all_accounts}) {
752 $trb{ $ref->{accno} }{description} = $ref->{description};
753 $trb{ $ref->{accno} }{charttype} = 'A';
754 $trb{ $ref->{accno} }{beginning_balance} = $ref->{amount};
756 if ($ref->{amount} > 0) {
757 $trb{ $ref->{accno} }{haben_eb} = $ref->{amount};
759 $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1;
761 $trb{ $ref->{accno} }{category} = $ref->{category};
770 qq|SELECT c.accno, c.description, c.category
772 WHERE c.charttype = 'H'
775 $sth = prepare_execute_query($form, $dbh, $query);
777 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
778 $trb{ $ref->{accno} }{description} = $ref->{description};
779 $trb{ $ref->{accno} }{charttype} = 'H';
780 $trb{ $ref->{accno} }{category} = $ref->{category};
782 push @headingaccounts, $ref->{accno};
788 my $saldowhere = " 1 = 1 ";
789 my $sumwhere = " 1 = 1 ";
791 my $sumsubwhere = '';
792 my $saldosubwhere = '';
793 my $glsaldowhere = '';
798 my ($fromdate, $todate);
800 if ($form->{fromdate} || $form->{todate}) {
801 if ($form->{fromdate}) {
802 $fromdate = conv_dateq($form->{fromdate});
803 $tofrom .= " AND (ac.transdate >= $fromdate)";
804 $subwhere .= " AND (ac.transdate >= $fromdate)";
805 $sumsubwhere .= " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
806 $saldosubwhere .= " AND (ac,transdate>=(select date_trunc('year', date $fromdate))) ";
807 $invwhere .= " AND (a.transdate >= $fromdate)";
808 $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
809 $glwhere = " AND (ac.transdate >= $fromdate)";
810 $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
812 if ($form->{todate}) {
813 $todate = conv_dateq($form->{todate});
814 $tofrom .= " AND (ac.transdate <= $todate)";
815 $invwhere .= " AND (a.transdate <= $todate)";
816 $saldosubwhere .= " AND (ac.transdate <= $todate)";
817 $sumsubwhere .= " AND (ac.transdate <= $todate)";
818 $subwhere .= " AND (ac.transdate <= $todate)";
819 $glwhere .= " AND (ac.transdate <= $todate)";
820 $glsumwhere .= " AND (ac.transdate <= $todate) ";
821 $glsaldowhere .= " AND (ac.transdate <= $todate) ";
825 if ($form->{method} eq "cash") {
827 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) |;
828 $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) |;
830 $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) |;
832 $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
833 $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
834 $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
838 SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
840 JOIN chart c ON (c.id = ac.chart_id)
842 $dpt_where_without_arapgl
844 GROUP BY c.accno, c.description, c.category |;
846 if ($form->{project_id}) {
848 -- add project transactions from invoice
852 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
854 JOIN ar a ON (ac.trans_id = a.id)
855 JOIN parts p ON (ac.parts_id = p.id)
856 JOIN chart c ON (p.income_accno_id = c.id)
860 GROUP BY c.accno, c.description, c.category
864 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
866 JOIN ap a ON (ac.trans_id = a.id)
867 JOIN parts p ON (ac.parts_id = p.id)
868 JOIN chart c ON (p.expense_accno_id = c.id)
872 GROUP BY c.accno, c.description, c.category
876 $query .= qq| ORDER BY accno|;
878 $sth = prepare_execute_query($form, $dbh, $query);
880 # calculate the debit and credit in the period
881 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
882 $trb{ $ref->{accno} }{description} = $ref->{description};
883 $trb{ $ref->{accno} }{charttype} = 'A';
884 $trb{ $ref->{accno} }{category} = $ref->{category};
885 $trb{ $ref->{accno} }{amount} += $ref->{amount};
889 # prepare query for each account
890 my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
894 (SELECT SUM(ac.amount) * -1
896 JOIN chart c ON (c.id = ac.chart_id)
898 $dpt_where_without_arapgl
901 AND (c.accno = ?)) AS debit,
903 (SELECT SUM(ac.amount)
905 JOIN chart c ON (c.id = ac.chart_id)
907 $dpt_where_without_arapgl
910 AND c.accno = ?) AS credit,
911 (SELECT SUM(ac.amount)
913 JOIN chart c ON (ac.chart_id = c.id)
915 $dpt_where_without_arapgl
917 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
919 (SELECT SUM(ac.amount)
921 JOIN chart c ON (ac.chart_id = c.id)
923 $dpt_where_without_arapgl
926 AND c.accno = ?) AS sum_credit,
928 (SELECT SUM(ac.amount)
930 JOIN chart c ON (ac.chart_id = c.id)
932 $dpt_where_without_arapgl
935 AND c.accno = ?) AS sum_debit,
937 (SELECT max(ac.transdate) FROM acc_trans ac
938 JOIN chart c ON (ac.chart_id = c.id)
940 $dpt_where_without_arapgl
942 AND c.accno = ?) AS last_transaction
947 $drcr = prepare_query($form, $dbh, $q_drcr);
949 if ($form->{project_id}) {
950 # prepare query for each account
953 (SELECT SUM(ac.sellprice * ac.qty) * -1
955 JOIN parts p ON (ac.parts_id = p.id)
956 JOIN ap a ON (ac.trans_id = a.id)
957 JOIN chart c ON (p.expense_accno_id = c.id)
961 AND c.accno = ?) AS debit,
963 (SELECT SUM(ac.sellprice * ac.qty)
965 JOIN parts p ON (ac.parts_id = p.id)
966 JOIN ar a ON (ac.trans_id = a.id)
967 JOIN chart c ON (p.income_accno_id = c.id)
971 AND c.accno = ?) AS credit,
973 (SELECT SUM(ac.amount)
975 JOIN chart c ON (ac.chart_id = c.id)
977 $dpt_where_without_arapgl
979 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
981 (SELECT SUM(ac.amount)
983 JOIN chart c ON (ac.chart_id = c.id)
985 $dpt_where_without_arapgl
988 AND c.accno = ?) AS sum_credit,
990 (SELECT SUM(ac.amount)
992 JOIN chart c ON (ac.chart_id = c.id)
994 $dpt_where_without_arapgl
997 AND c.accno = ?) AS sum_debit,
1000 (SELECT max(ac.transdate) FROM acc_trans ac
1001 JOIN chart c ON (ac.chart_id = c.id)
1003 $dpt_where_without_arapgl
1005 AND c.accno = ?) AS last_transaction
1008 $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
1012 my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
1014 foreach my $accno (sort keys %trb) {
1017 $ref->{accno} = $accno;
1018 map { $ref->{$_} = $trb{$accno}{$_} }
1019 qw(description category charttype amount soll_eb haben_eb beginning_balance);
1021 $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
1023 if ($trb{$accno}{charttype} eq 'A') {
1026 do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1028 ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
1029 my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
1030 $last_transaction = "";
1031 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
1032 $ref->{debit} += $debit;
1033 $ref->{credit} += $credit;
1035 $ref->{haben_saldo} += $saldo;
1037 $ref->{soll_saldo} += $saldo * -1;
1039 $ref->{last_transaction} = $last_transaction;
1040 $ref->{soll_kumuliert} = $soll_kumuliert * -1;
1041 $ref->{haben_kumuliert} = $haben_kumuliert;
1045 if ($form->{project_id}) {
1048 do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1050 ($debit, $credit) = (0, 0);
1051 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
1052 $ref->{debit} += $debit;
1053 $ref->{credit} += $credit;
1055 $ref->{haben_saldo} += $saldo;
1057 $ref->{soll_saldo} += $saldo * -1;
1059 $ref->{soll_kumuliert} += $soll_kumuliert * -1;
1060 $ref->{haben_kumuliert} += $haben_kumuliert;
1062 $project_drcr->finish;
1065 $ref->{debit} = $form->round_amount($ref->{debit}, 2);
1066 $ref->{credit} = $form->round_amount($ref->{credit}, 2);
1068 if ($ref->{haben_saldo} != 0) {
1069 $ref->{haben_saldo} = $ref->{haben_saldo} + $ref->{beginning_balance};
1070 if ($ref->{haben_saldo} < 0) {
1071 $ref->{soll_saldo} = $form->round_amount(($ref->{haben_saldo} *- 1), 2);
1072 $ref->{haben_saldo} = 0;
1075 $ref->{soll_saldo} = $ref->{soll_saldo} - $ref->{beginning_balance};
1076 if ($ref->{soll_saldo} < 0) {
1077 $ref->{haben_saldo} = $form->round_amount(($ref->{soll_saldo} * -1), 2);
1078 $ref->{soll_saldo} = 0;
1081 $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
1082 $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
1083 $ref->{haben_kumuliert} = $form->round_amount($ref->{haben_kumuliert}, 2);
1084 $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
1089 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1090 $accno = pop @accno;
1092 $trb{$accno}{debit} += $ref->{debit};
1093 $trb{$accno}{credit} += $ref->{credit};
1094 $trb{$accno}{soll_saldo} += $ref->{soll_saldo};
1095 $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
1096 $trb{$accno}{soll_kumuliert} += $ref->{soll_kumuliert};
1097 $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
1100 push @{ $form->{TB} }, $ref;
1106 # debits and credits for headings
1107 foreach my $accno (@headingaccounts) {
1108 foreach $ref (@{ $form->{TB} }) {
1109 if ($accno eq $ref->{accno}) {
1110 $ref->{debit} = $trb{$accno}{debit};
1111 $ref->{credit} = $trb{$accno}{credit};
1112 $ref->{soll_saldo} = $trb{$accno}{soll_saldo};
1113 $ref->{haben_saldo} = $trb{$accno}{haben_saldo};
1114 $ref->{soll_kumuliert} = $trb{$accno}{soll_kumuliert};
1115 $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert};
1120 $main::lxdebug->leave_sub();
1124 $main::lxdebug->enter_sub();
1125 my ($self, $dbh, $form) = @_;
1126 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
1127 my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
1128 my $sth = $dbh->prepare($query);
1129 while(my $ref = $sth->fetchrow_hashref()) {
1130 $ref->{invnumer} =~ s/Storno zu //g;
1131 $form->{storno}{$ref->{invnumber}} = 1;
1133 $main::lxdebug->leave_sub();
1137 $main::lxdebug->enter_sub();
1139 my ($self, $myconfig, $form) = @_;
1141 # connect to database
1142 my $dbh = $form->dbconnect($myconfig);
1144 my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
1146 # falls customer ziehen wir die offene forderungsliste
1147 # anderfalls für die lieferanten die offenen verbindlichkeitne
1148 if ($form->{ct} eq "customer") {
1161 $ct_id = "${ct}_id";
1163 # erweiterung um einen freien zeitraum oder einen stichtag
1164 # mit entsprechender altersstrukturliste (s.a. Bug 1842)
1165 # eine neue variable an der oberfläche eingeführt, somit ist
1166 # todate == freier zeitrau und fordate == stichtag
1167 # duedate_where == nur fällige rechnungen anzeigen
1169 my ($review_of_aging_list, $todate, $fromdate, $fromwhere, $fordate,
1172 if ($form->{reporttype} eq 'custom') { # altersstrukturliste, nur fällige
1174 # explizit rausschmeissen was man für diesen bericht nicht braucht
1175 delete $form->{fromdate};
1176 delete $form->{todate};
1178 # an der oberfläche ist das tagesaktuelle datum vorausgewählt
1179 # falls es dennoch per Benutzereingabe gelöscht wird, lieber wieder vorbelegen
1180 # ferner muss für die spätere DB-Abfrage muss todate gesetzt sein.
1181 $form->{fordate} = $form->current_date($myconfig) unless ($form->{fordate});
1182 $fordate = conv_dateq($form->{fordate});
1185 if ($form->{review_of_aging_list}) { # falls die liste leer ist, alles anzeigen
1186 if ($form->{review_of_aging_list} =~ m "-") { # .. periode von bis
1187 my @period = split(/-/, $form->{review_of_aging_list}); # ... von periode bis periode
1188 $review_of_aging_list = " AND $period[0] < (date $fordate) - duedate
1189 AND (date $fordate) - duedate < $period[1]";
1191 $form->{review_of_aging_list} =~ s/[^0-9]//g; # größer 120 das substitute ist nur für das '>' zeichen
1192 $review_of_aging_list = " AND $form->{review_of_aging_list} < (date $fordate) - duedate";
1195 $duedate_where = " AND (date $fordate) - duedate >= 0 ";
1196 } else { # freier zeitraum, nur rechnungsdatum und 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 WHERE chart_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
1240 ORDER BY ctid, transdate, invnumber |;
1242 my $sth_details = prepare_query($form, $dbh, $q_details);
1244 # select outstanding vendors or customers, depends on $ct
1246 qq|SELECT DISTINCT ct.id, ct.name
1247 FROM $ct ct, $arap a
1250 AND (a.${ct_id} = ct.id)
1251 AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
1252 AND (a.transdate <= $todate $fromwhere)
1255 my $sth = prepare_execute_query($form, $dbh, $query);
1258 # for each company that has some stuff outstanding
1259 while (my ($id) = $sth->fetchrow_array) {
1260 do_statement($form, $sth_details, $q_details, $id);
1262 while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
1263 $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
1264 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
1265 push @{ $form->{AG} }, $ref;
1268 $sth_details->finish;
1277 $main::lxdebug->leave_sub();
1281 $main::lxdebug->enter_sub();
1283 my ($self, $myconfig, $form) = @_;
1285 # connect to database
1286 my $dbh = $form->dbconnect($myconfig);
1288 my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
1291 qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
1294 ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
1295 selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
1298 $main::lxdebug->leave_sub();
1302 $main::lxdebug->enter_sub();
1304 my ($self, $myconfig, $form) = @_;
1306 # connect to database
1307 my $dbh = $form->dbconnect($myconfig);
1309 my ($null, $department_id) = split /--/, $form->{department};
1312 my $where = "1 = 1";
1314 if ($department_id) {
1315 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
1320 if ($form->{accno}) {
1321 $accno = $form->{accno};
1322 $rate = $form->{"$form->{accno}_rate"};
1323 $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
1329 if ($form->{db} eq 'ar') {
1330 $table = "customer";
1337 my $arap = lc($ARAP);
1339 my $transdate = "a.transdate";
1341 if ($form->{method} eq 'cash') {
1342 $transdate = "a.datepaid";
1344 my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
1351 WHERE (a.chart_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();