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 # filter for opening and closing bookings
215 # if l_ob is selected l_cb is always ignored
216 if ( $last_period ) {
217 # ob/cb-settings for "compared to" balance
218 if ( $form->{l_ob_compared} ) {
219 $where .= ' AND ac.ob_transaction is true '
220 } elsif ( not $form->{l_cb_compared} ) {
221 $where .= ' AND ac.cb_transaction is false ';
224 # ob/cb-settings for "as of" balance
225 if ( $form->{l_ob} ) {
226 $where .= ' AND ac.ob_transaction is true '
227 } elsif ( not $form->{l_cb} ) {
228 $where .= ' AND ac.cb_transaction is false ';
234 $fromdate = conv_dateq($fromdate);
235 if ($form->{method} eq 'cash') {
236 $subwhere .= " AND (transdate >= $fromdate)";
237 $glwhere = " AND (ac.transdate >= $fromdate)";
239 $where .= " AND (ac.transdate >= $fromdate)";
244 $todate = conv_dateq($todate);
245 $where .= " AND (ac.transdate <= $todate)";
246 $subwhere .= " AND (transdate <= $todate)";
249 if ($department_id) {
250 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
253 if ($form->{project_id}) {
254 # Diese Bedingung wird derzeit niemals wahr sein, da man in Bericht->Bilanz keine
255 # Projekte auswählen kann
256 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
259 if ($form->{method} eq 'cash') {
261 qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
263 JOIN chart c ON (c.id = ac.chart_id)
264 JOIN ar a ON (a.id = ac.trans_id)
272 WHERE (a.chart_link LIKE '%AR_paid%')
276 GROUP BY c.accno, c.description, c.category
280 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
282 JOIN chart c ON (c.id = ac.chart_id)
283 JOIN ap a ON (a.id = ac.trans_id)
291 WHERE (a.chart_link LIKE '%AP_paid%')
295 GROUP BY c.accno, c.description, c.category
299 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
301 JOIN chart c ON (c.id = ac.chart_id)
302 JOIN gl a ON (a.id = ac.trans_id)
307 AND NOT ((ac.chart_link = 'AR') OR (ac.chart_link = 'AP'))
309 GROUP BY c.accno, c.description, c.category |;
311 if ($form->{project_id}) {
312 # s.o. keine Projektauswahl in Bilanz
317 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
319 JOIN ar a ON (a.id = ac.trans_id)
320 JOIN parts p ON (ac.parts_id = p.id)
321 JOIN chart c on (p.income_accno_id = c.id)
322 -- use transdate from subwhere
323 WHERE (c.category = 'I')
330 WHERE (a.chart_link LIKE '%AR_paid%')
334 GROUP BY c.accno, c.description, c.category
338 SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
340 JOIN ap a ON (a.id = ac.trans_id)
341 JOIN parts p ON (ac.parts_id = p.id)
342 JOIN chart c on (p.expense_accno_id = c.id)
343 WHERE (c.category = 'E')
350 WHERE a.chart_link LIKE '%AP_paid%'
354 GROUP BY c.accno, c.description, c.category |;
357 } else { # if ($form->{method} eq 'cash')
358 if ($department_id) {
359 $dpt_where = qq| AND a.department_id = | . conv_i($department_id);
360 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
361 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
362 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
366 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
368 JOIN chart c ON (c.id = ac.chart_id)
370 $dpt_where_without_arapgl
373 GROUP BY c.accno, c.description, c.category |;
375 if ($form->{project_id}) {
376 # s.o. keine Projektauswahl in Bilanz
380 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
382 JOIN ar a ON (a.id = ac.trans_id)
383 JOIN parts p ON (ac.parts_id = p.id)
384 JOIN chart c on (p.income_accno_id = c.id)
385 -- use transdate from subwhere
386 WHERE (c.category = 'I')
390 GROUP BY c.accno, c.description, c.category
394 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
396 JOIN ap a ON (a.id = ac.trans_id)
397 JOIN parts p ON (ac.parts_id = p.id)
398 JOIN chart c on (p.expense_accno_id = c.id)
399 WHERE (c.category = 'E')
403 GROUP BY c.accno, c.description, c.category |;
411 $sth = prepare_execute_query($form, $dbh, $query);
413 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
415 if ($ref->{category} eq 'C') {
416 $ref->{category} = 'A';
419 # get last heading account
420 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
424 $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
426 $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
430 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
431 $form->{ $ref->{category} }{ $ref->{accno} }{description} = $ref->{description};
432 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
435 $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
437 $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
442 # remove accounts with zero balance
443 foreach $category (@{$categories}) {
444 foreach $accno (keys %{ $form->{$category} }) {
445 $form->{$category}{$accno}{last} = $form->round_amount($form->{$category}{$accno}{last}, $dec);
446 $form->{$category}{$accno}{this} = $form->round_amount($form->{$category}{$accno}{this}, $dec);
448 delete $form->{$category}{$accno}
449 if ( $form->{$category}{$accno}{this} == 0
450 && $form->{$category}{$accno}{last} == 0);
454 $main::lxdebug->leave_sub();
458 $main::lxdebug->enter_sub();
460 my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
462 my ($null, $department_id) = split /--/, $form->{department};
466 my $dpt_where_without_arapgl;
475 $where .= ' AND ac.cb_transaction is false ' unless $form->{l_cb};
478 $fromdate = conv_dateq($fromdate);
479 if ($form->{method} eq 'cash') {
480 $subwhere .= " AND (transdate >= $fromdate)";
481 $glwhere = " AND (ac.transdate >= $fromdate)";
482 $prwhere = " AND (a.transdate >= $fromdate)";
483 $inwhere = " AND (acc.transdate >= $fromdate)";
485 $where .= " AND (ac.transdate >= $fromdate)";
490 $todate = conv_dateq($todate);
491 $subwhere .= " AND (transdate <= $todate)";
492 $where .= " AND (ac.transdate <= $todate)";
493 $prwhere .= " AND (a.transdate <= $todate)";
494 $inwhere .= " AND (acc.transdate <= $todate)";
497 if ($department_id) {
498 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
501 if ($form->{project_id}) {
502 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
506 # GUV patch by Ronny Rentner (Bug 1190)
508 # GUV IST-Versteuerung
510 # Alle tatsaechlichen _Zahlungseingaenge_
511 # im Zeitraum erfassen
512 # (Teilzahlungen werden prozentual auf verschiedene Steuern aufgeteilt)
516 if ($form->{method} eq 'cash') {
519 SELECT SUM( ac.amount * CASE WHEN COALESCE((SELECT amount FROM ar WHERE id = ac.trans_id), 0) != 0 THEN
520 /* ar amount is not zero, so we can divide by amount */
521 (SELECT SUM(acc.amount) * -1
524 AND acc.trans_id = ac.trans_id
525 AND acc.chart_link LIKE '%AR_paid%')
526 / (SELECT amount FROM ar WHERE id = ac.trans_id)
528 /* ar amount is zero, or we are checking with a non-ar-transaction, so we return 0 in both cases as multiplicator of ac.amount */
530 ) AS amount, c.$category
532 LEFT JOIN chart c ON (c.id = ac.chart_id)
533 LEFT JOIN ar ON (ar.id = ac.trans_id)
534 WHERE ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE 1=1 $subwhere)
539 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
541 JOIN chart c ON (c.id = ac.chart_id)
542 JOIN ar a ON (a.id = ac.trans_id)
543 WHERE $where $dpt_where
544 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AR_paid%') $subwhere)
550 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
552 JOIN chart c ON (c.id = ac.chart_id)
553 JOIN ap a ON (a.id = ac.trans_id)
554 WHERE $where $dpt_where
555 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AP_paid%') $subwhere)
561 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
563 JOIN chart c ON (c.id = ac.chart_id)
564 JOIN gl a ON (a.id = ac.trans_id)
565 WHERE $where $dpt_where $glwhere
566 AND NOT ((ac.chart_link = 'AR') OR (ac.chart_link = 'AP'))
571 if ($form->{project_id}) {
575 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
577 JOIN ar a ON (a.id = ac.trans_id)
578 JOIN parts p ON (ac.parts_id = p.id)
579 JOIN chart c on (p.income_accno_id = c.id)
580 WHERE (c.category = 'I') $prwhere $dpt_where
581 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AR_paid%') $subwhere)
587 SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category
589 JOIN ap a ON (a.id = ac.trans_id)
590 JOIN parts p ON (ac.parts_id = p.id)
591 JOIN chart c on (p.expense_accno_id = c.id)
592 WHERE (c.category = 'E') $prwhere $dpt_where
593 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AP_paid%') $subwhere)
599 } else { # if ($form->{method} eq 'cash')
600 if ($department_id) {
601 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
602 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
603 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
604 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
608 SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
610 JOIN chart c ON (c.id = ac.chart_id)
612 $dpt_where_without_arapgl
614 GROUP BY c.$category |;
616 if ($form->{project_id}) {
620 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
622 JOIN ar a ON (a.id = ac.trans_id)
623 JOIN parts p ON (ac.parts_id = p.id)
624 JOIN chart c on (p.income_accno_id = c.id)
625 WHERE (c.category = 'I')
633 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
635 JOIN ap a ON (a.id = ac.trans_id)
636 JOIN parts p ON (ac.parts_id = p.id)
637 JOIN chart c on (p.expense_accno_id = c.id)
638 WHERE (c.category = 'E')
642 GROUP BY c.$category |;
650 foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
651 if ($category eq "pos_bwa") {
653 $form->{ $ref->{$category} }{kumm} += $ref->{amount};
655 $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
658 $form->{ $ref->{$category} } += $ref->{amount};
662 $main::lxdebug->leave_sub();
666 $main::lxdebug->enter_sub();
668 my ($self, $myconfig, $form, %options) = @_;
670 my $dbh = $form->dbconnect($myconfig);
672 my ($query, $sth, $ref);
675 my ($null, $department_id) = split /--/, $form->{department};
676 my @headingaccounts = ();
678 my $dpt_where_without_arapgl;
682 my $invwhere = $where;
684 if ($department_id) {
685 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
686 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
687 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
688 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
691 # project_id only applies to getting transactions
692 # it has nothing to do with a trial balance
693 # but we use the same function to collect information
695 if ($form->{project_id}) {
696 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
699 my $acc_cash_where = "";
700 # my $ar_cash_where = "";
701 # my $ap_cash_where = "";
704 if ($form->{method} eq "cash") {
706 qq| AND (ac.trans_id IN (
709 WHERE datepaid >= '$form->{fromdate}'
710 AND datepaid <= '$form->{todate}'
716 WHERE datepaid >= '$form->{fromdate}'
717 AND datepaid <= '$form->{todate}'
723 WHERE transdate >= '$form->{fromdate}'
724 AND transdate <= '$form->{todate}'
726 # $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
729 if ($options{beginning_balances}) {
730 foreach my $prefix (qw(from to)) {
731 next if ($form->{"${prefix}date"});
733 my $min_max = $prefix eq 'from' ? 'min' : 'max';
734 $query = qq|SELECT ${min_max}(transdate)
737 $dpt_where_without_arapgl
739 ($form->{"${prefix}date"}) = selectfirst_array_query($form, $dbh, $query);
742 # get beginning balances
744 qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
746 LEFT JOIN chart c ON (ac.chart_id = c.id)
747 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
748 $dpt_where_without_arapgl
750 GROUP BY c.accno, c.category, c.description |;
752 $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
754 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
756 if ($ref->{amount} != 0 || $form->{all_accounts}) {
757 $trb{ $ref->{accno} }{description} = $ref->{description};
758 $trb{ $ref->{accno} }{charttype} = 'A';
759 $trb{ $ref->{accno} }{beginning_balance} = $ref->{amount};
761 if ($ref->{amount} > 0) {
762 $trb{ $ref->{accno} }{haben_eb} = $ref->{amount};
764 $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1;
766 $trb{ $ref->{accno} }{category} = $ref->{category};
775 qq|SELECT c.accno, c.description, c.category
777 WHERE c.charttype = 'H'
780 $sth = prepare_execute_query($form, $dbh, $query);
782 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
783 $trb{ $ref->{accno} }{description} = $ref->{description};
784 $trb{ $ref->{accno} }{charttype} = 'H';
785 $trb{ $ref->{accno} }{category} = $ref->{category};
787 push @headingaccounts, $ref->{accno};
793 my $saldowhere = " 1 = 1 ";
794 my $sumwhere = " 1 = 1 ";
796 my $sumsubwhere = '';
797 my $saldosubwhere = '';
798 my $glsaldowhere = '';
803 my ($fromdate, $todate);
805 if ($form->{fromdate} || $form->{todate}) {
806 if ($form->{fromdate}) {
807 $fromdate = conv_dateq($form->{fromdate});
808 $tofrom .= " AND (ac.transdate >= $fromdate)";
809 $subwhere .= " AND (ac.transdate >= $fromdate)";
810 $sumsubwhere .= " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
811 $saldosubwhere .= " AND (ac,transdate>=(select date_trunc('year', date $fromdate))) ";
812 $invwhere .= " AND (a.transdate >= $fromdate)";
813 $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
814 $glwhere = " AND (ac.transdate >= $fromdate)";
815 $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
817 if ($form->{todate}) {
818 $todate = conv_dateq($form->{todate});
819 $tofrom .= " AND (ac.transdate <= $todate)";
820 $invwhere .= " AND (a.transdate <= $todate)";
821 $saldosubwhere .= " AND (ac.transdate <= $todate)";
822 $sumsubwhere .= " AND (ac.transdate <= $todate)";
823 $subwhere .= " AND (ac.transdate <= $todate)";
824 $glwhere .= " AND (ac.transdate <= $todate)";
825 $glsumwhere .= " AND (ac.transdate <= $todate) ";
826 $glsaldowhere .= " AND (ac.transdate <= $todate) ";
830 if ($form->{method} eq "cash") {
832 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) |;
833 $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) |;
835 $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) |;
837 $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
838 $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
839 $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
843 SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
845 JOIN chart c ON (c.id = ac.chart_id)
847 $dpt_where_without_arapgl
849 GROUP BY c.accno, c.description, c.category |;
851 if ($form->{project_id}) {
853 -- add project transactions from invoice
857 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
859 JOIN ar a ON (ac.trans_id = a.id)
860 JOIN parts p ON (ac.parts_id = p.id)
861 JOIN chart c ON (p.income_accno_id = c.id)
865 GROUP BY c.accno, c.description, c.category
869 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
871 JOIN ap a ON (ac.trans_id = a.id)
872 JOIN parts p ON (ac.parts_id = p.id)
873 JOIN chart c ON (p.expense_accno_id = c.id)
877 GROUP BY c.accno, c.description, c.category
881 $query .= qq| ORDER BY accno|;
883 $sth = prepare_execute_query($form, $dbh, $query);
885 # calculate the debit and credit in the period
886 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
887 $trb{ $ref->{accno} }{description} = $ref->{description};
888 $trb{ $ref->{accno} }{charttype} = 'A';
889 $trb{ $ref->{accno} }{category} = $ref->{category};
890 $trb{ $ref->{accno} }{amount} += $ref->{amount};
894 # prepare query for each account
895 my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
899 (SELECT SUM(ac.amount) * -1
901 JOIN chart c ON (c.id = ac.chart_id)
903 $dpt_where_without_arapgl
906 AND (c.accno = ?)) AS debit,
908 (SELECT SUM(ac.amount)
910 JOIN chart c ON (c.id = ac.chart_id)
912 $dpt_where_without_arapgl
915 AND c.accno = ?) AS credit,
916 (SELECT SUM(ac.amount)
918 JOIN chart c ON (ac.chart_id = c.id)
920 $dpt_where_without_arapgl
922 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
924 (SELECT SUM(ac.amount)
926 JOIN chart c ON (ac.chart_id = c.id)
928 $dpt_where_without_arapgl
931 AND c.accno = ?) AS sum_credit,
933 (SELECT SUM(ac.amount)
935 JOIN chart c ON (ac.chart_id = c.id)
937 $dpt_where_without_arapgl
940 AND c.accno = ?) AS sum_debit,
942 (SELECT max(ac.transdate) FROM acc_trans ac
943 JOIN chart c ON (ac.chart_id = c.id)
945 $dpt_where_without_arapgl
947 AND c.accno = ?) AS last_transaction
952 $drcr = prepare_query($form, $dbh, $q_drcr);
954 if ($form->{project_id}) {
955 # prepare query for each account
958 (SELECT SUM(ac.sellprice * ac.qty) * -1
960 JOIN parts p ON (ac.parts_id = p.id)
961 JOIN ap a ON (ac.trans_id = a.id)
962 JOIN chart c ON (p.expense_accno_id = c.id)
966 AND c.accno = ?) AS debit,
968 (SELECT SUM(ac.sellprice * ac.qty)
970 JOIN parts p ON (ac.parts_id = p.id)
971 JOIN ar a ON (ac.trans_id = a.id)
972 JOIN chart c ON (p.income_accno_id = c.id)
976 AND c.accno = ?) AS credit,
978 (SELECT SUM(ac.amount)
980 JOIN chart c ON (ac.chart_id = c.id)
982 $dpt_where_without_arapgl
984 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
986 (SELECT SUM(ac.amount)
988 JOIN chart c ON (ac.chart_id = c.id)
990 $dpt_where_without_arapgl
993 AND c.accno = ?) AS sum_credit,
995 (SELECT SUM(ac.amount)
997 JOIN chart c ON (ac.chart_id = c.id)
999 $dpt_where_without_arapgl
1002 AND c.accno = ?) AS sum_debit,
1005 (SELECT max(ac.transdate) FROM acc_trans ac
1006 JOIN chart c ON (ac.chart_id = c.id)
1008 $dpt_where_without_arapgl
1010 AND c.accno = ?) AS last_transaction
1013 $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
1017 my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
1019 foreach my $accno (sort keys %trb) {
1022 $ref->{accno} = $accno;
1023 map { $ref->{$_} = $trb{$accno}{$_} }
1024 qw(description category charttype amount soll_eb haben_eb beginning_balance);
1026 $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
1028 if ($trb{$accno}{charttype} eq 'A') {
1031 do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1033 ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
1034 my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
1035 $last_transaction = "";
1036 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
1037 $ref->{debit} += $debit;
1038 $ref->{credit} += $credit;
1040 $ref->{haben_saldo} += $saldo;
1042 $ref->{soll_saldo} += $saldo * -1;
1044 $ref->{last_transaction} = $last_transaction;
1045 $ref->{soll_kumuliert} = $soll_kumuliert * -1;
1046 $ref->{haben_kumuliert} = $haben_kumuliert;
1050 if ($form->{project_id}) {
1053 do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1055 ($debit, $credit) = (0, 0);
1056 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
1057 $ref->{debit} += $debit;
1058 $ref->{credit} += $credit;
1060 $ref->{haben_saldo} += $saldo;
1062 $ref->{soll_saldo} += $saldo * -1;
1064 $ref->{soll_kumuliert} += $soll_kumuliert * -1;
1065 $ref->{haben_kumuliert} += $haben_kumuliert;
1067 $project_drcr->finish;
1070 $ref->{debit} = $form->round_amount($ref->{debit}, 2);
1071 $ref->{credit} = $form->round_amount($ref->{credit}, 2);
1073 if ($ref->{haben_saldo} != 0) {
1074 $ref->{haben_saldo} = $ref->{haben_saldo} + $ref->{beginning_balance};
1075 if ($ref->{haben_saldo} < 0) {
1076 $ref->{soll_saldo} = $form->round_amount(($ref->{haben_saldo} *- 1), 2);
1077 $ref->{haben_saldo} = 0;
1080 $ref->{soll_saldo} = $ref->{soll_saldo} - $ref->{beginning_balance};
1081 if ($ref->{soll_saldo} < 0) {
1082 $ref->{haben_saldo} = $form->round_amount(($ref->{soll_saldo} * -1), 2);
1083 $ref->{soll_saldo} = 0;
1086 $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
1087 $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
1088 $ref->{haben_kumuliert} = $form->round_amount($ref->{haben_kumuliert}, 2);
1089 $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
1094 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1095 $accno = pop @accno;
1097 $trb{$accno}{debit} += $ref->{debit};
1098 $trb{$accno}{credit} += $ref->{credit};
1099 $trb{$accno}{soll_saldo} += $ref->{soll_saldo};
1100 $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
1101 $trb{$accno}{soll_kumuliert} += $ref->{soll_kumuliert};
1102 $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
1105 push @{ $form->{TB} }, $ref;
1111 # debits and credits for headings
1112 foreach my $accno (@headingaccounts) {
1113 foreach $ref (@{ $form->{TB} }) {
1114 if ($accno eq $ref->{accno}) {
1115 $ref->{debit} = $trb{$accno}{debit};
1116 $ref->{credit} = $trb{$accno}{credit};
1117 $ref->{soll_saldo} = $trb{$accno}{soll_saldo};
1118 $ref->{haben_saldo} = $trb{$accno}{haben_saldo};
1119 $ref->{soll_kumuliert} = $trb{$accno}{soll_kumuliert};
1120 $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert};
1125 $main::lxdebug->leave_sub();
1129 $main::lxdebug->enter_sub();
1130 my ($self, $dbh, $form) = @_;
1131 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
1132 my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
1133 my $sth = $dbh->prepare($query);
1134 while(my $ref = $sth->fetchrow_hashref()) {
1135 $ref->{invnumer} =~ s/Storno zu //g;
1136 $form->{storno}{$ref->{invnumber}} = 1;
1138 $main::lxdebug->leave_sub();
1142 $main::lxdebug->enter_sub();
1144 my ($self, $myconfig, $form) = @_;
1146 # connect to database
1147 my $dbh = $form->dbconnect($myconfig);
1149 my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
1151 # falls customer ziehen wir die offene forderungsliste
1152 # anderfalls für die lieferanten die offenen verbindlichkeitne
1153 if ($form->{ct} eq "customer") {
1166 $ct_id = "${ct}_id";
1168 # erweiterung um einen freien zeitraum oder einen stichtag
1169 # mit entsprechender altersstrukturliste (s.a. Bug 1842)
1170 # eine neue variable an der oberfläche eingeführt, somit ist
1171 # todate == freier zeitrau und fordate == stichtag
1172 # duedate_where == nur fällige rechnungen anzeigen
1174 my ($review_of_aging_list, $todate, $fromdate, $fromwhere, $fordate,
1177 if ($form->{reporttype} eq 'custom') { # altersstrukturliste, nur fällige
1179 # explizit rausschmeissen was man für diesen bericht nicht braucht
1180 delete $form->{fromdate};
1181 delete $form->{todate};
1183 # an der oberfläche ist das tagesaktuelle datum vorausgewählt
1184 # falls es dennoch per Benutzereingabe gelöscht wird, lieber wieder vorbelegen
1185 # ferner muss für die spätere DB-Abfrage muss todate gesetzt sein.
1186 $form->{fordate} = $form->current_date($myconfig) unless ($form->{fordate});
1187 $fordate = conv_dateq($form->{fordate});
1190 if ($form->{review_of_aging_list}) { # falls die liste leer ist, alles anzeigen
1191 if ($form->{review_of_aging_list} =~ m "-") { # .. periode von bis
1192 my @period = split(/-/, $form->{review_of_aging_list}); # ... von periode bis periode
1193 $review_of_aging_list = " AND $period[0] < (date $fordate) - duedate
1194 AND (date $fordate) - duedate < $period[1]";
1196 $form->{review_of_aging_list} =~ s/[^0-9]//g; # größer 120 das substitute ist nur für das '>' zeichen
1197 $review_of_aging_list = " AND $form->{review_of_aging_list} < (date $fordate) - duedate";
1200 $duedate_where = " AND (date $fordate) - duedate >= 0 ";
1201 } else { # freier zeitraum, nur rechnungsdatum und OHNE review_of_aging_list
1202 $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
1203 $todate = conv_dateq($form->{todate});
1204 $fromdate = conv_dateq($form->{fromdate});
1205 $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
1207 my $where = " 1 = 1 ";
1210 if ($form->{$ct_id}) {
1211 $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
1212 } elsif ($form->{ $form->{ct} }) {
1213 $where .= qq| AND (ct.name ILIKE | . $dbh->quote('%' . $form->{$ct} . '%') . qq|)|;
1218 if ($form->{department}) {
1219 my ($null, $department_id) = split /--/, $form->{department};
1220 $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
1221 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1222 $where_dpt = qq| AND (${arap}.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1226 SELECT ${ct}.id AS ctid, ${ct}.name,
1227 street, zipcode, city, country, contact, email,
1228 phone as customerphone, fax as customerfax, ${ct}number,
1229 "invnumber", "transdate",
1230 (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",
1231 "duedate", invoice, ${arap}.id, date_part('days', now() - duedate) as overduedays,
1234 WHERE (${arap}.currency_id = exchangerate.currency_id)
1235 AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
1237 WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
1238 AND NOT COALESCE (${arap}.storno, 'f')
1239 AND (${arap}.${ct}_id = ${ct}.id)
1242 AND (transdate <= (date $todate) $fromwhere )
1243 $review_of_aging_list
1245 ORDER BY ctid, transdate, invnumber |;
1247 my $sth_details = prepare_query($form, $dbh, $q_details);
1249 # select outstanding vendors or customers, depends on $ct
1251 qq|SELECT DISTINCT ct.id, ct.name
1252 FROM $ct ct, $arap a
1255 AND (a.${ct_id} = ct.id)
1256 AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
1257 AND (a.transdate <= $todate $fromwhere)
1260 my $sth = prepare_execute_query($form, $dbh, $query);
1263 # for each company that has some stuff outstanding
1264 while (my ($id) = $sth->fetchrow_array) {
1265 do_statement($form, $sth_details, $q_details, $id);
1267 while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
1268 $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
1269 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
1270 push @{ $form->{AG} }, $ref;
1273 $sth_details->finish;
1282 $main::lxdebug->leave_sub();
1286 $main::lxdebug->enter_sub();
1288 my ($self, $myconfig, $form) = @_;
1290 # connect to database
1291 my $dbh = $form->dbconnect($myconfig);
1293 my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
1296 qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
1299 ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
1300 selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
1303 $main::lxdebug->leave_sub();
1307 $main::lxdebug->enter_sub();
1309 my ($self, $myconfig, $form) = @_;
1311 # connect to database
1312 my $dbh = $form->dbconnect($myconfig);
1314 my ($null, $department_id) = split /--/, $form->{department};
1317 my $where = "1 = 1";
1319 if ($department_id) {
1320 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
1325 if ($form->{accno}) {
1326 $accno = $form->{accno};
1327 $rate = $form->{"$form->{accno}_rate"};
1328 $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
1334 if ($form->{db} eq 'ar') {
1335 $table = "customer";
1342 my $arap = lc($ARAP);
1344 my $transdate = "a.transdate";
1346 if ($form->{method} eq 'cash') {
1347 $transdate = "a.datepaid";
1349 my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
1356 WHERE (a.chart_link LIKE '%${ARAP}_paid%')
1357 AND (transdate <= $todate)
1362 # if there are any dates construct a where
1363 $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
1364 $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
1366 my $ml = ($form->{db} eq 'ar') ? 1 : -1;
1368 my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
1369 $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
1372 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
1373 ac.amount * $ml AS tax
1375 JOIN ${arap} a ON (a.id = ac.trans_id)
1376 JOIN chart ch ON (ch.id = ac.chart_id)
1377 JOIN $table n ON (n.id = a.${table}_id)
1381 AND (a.invoice = '0')
1385 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
1386 i.sellprice * i.qty * $rate * $ml AS tax
1388 JOIN ${arap} a ON (a.id = ac.trans_id)
1389 JOIN chart ch ON (ch.id = ac.chart_id)
1390 JOIN $table n ON (n.id = a.${table}_id)
1391 JOIN ${table}tax t ON (t.${table}_id = n.id)
1392 JOIN invoice i ON (i.trans_id = a.id)
1396 AND (a.invoice = '1')
1397 ORDER BY $sortorder|;
1399 $form->{TR} = selectall_hashref_query($form, $dbh, $query);
1403 $main::lxdebug->leave_sub();
1406 sub paymentaccounts {
1407 $main::lxdebug->enter_sub();
1409 my ($self, $myconfig, $form) = @_;
1411 # connect to database, turn AutoCommit off
1412 my $dbh = $form->dbconnect_noauto($myconfig);
1414 my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
1416 # get A(R|P)_paid accounts
1418 qq|SELECT accno, description
1420 WHERE link LIKE '%${ARAP}_paid%'|;
1421 $form->{PR} = selectall_hashref_query($form, $dbh, $query);
1425 $main::lxdebug->leave_sub();
1429 $main::lxdebug->enter_sub();
1431 my ($self, $myconfig, $form) = @_;
1433 # connect to database, turn AutoCommit off
1434 my $dbh = $form->dbconnect_noauto($myconfig);
1439 if ($form->{db} eq 'ar') {
1440 $table = 'customer';
1451 if ($form->{department_id}) {
1452 $where = qq| AND (a.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
1455 if ($form->{fromdate}) {
1456 $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
1458 if ($form->{todate}) {
1459 $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
1461 if (!$form->{fx_transaction}) {
1462 $where .= " AND ac.fx_transaction = '0'";
1467 if ($form->{reference}) {
1468 $reference = $dbh->quote('%' . $form->{reference} . '%');
1469 $invnumber = " AND (a.invnumber LIKE $reference)";
1470 $reference = " AND (a.reference LIKE $reference)";
1472 if ($form->{source}) {
1473 $where .= " AND (ac.source ILIKE " . $dbh->quote('%' . $form->{source} . '%') . ") ";
1475 if ($form->{memo}) {
1476 $where .= " AND (ac.memo ILIKE " . $dbh->quote('%' . $form->{memo} . '%') . ") ";
1479 my %sort_columns = (
1480 'transdate' => [ qw(transdate lower_invnumber lower_name) ],
1481 'invnumber' => [ qw(lower_invnumber lower_name transdate) ],
1482 'name' => [ qw(lower_name transdate) ],
1483 'source' => [ qw(lower_source) ],
1484 'memo' => [ qw(lower_memo) ],
1486 my %lowered_columns = (
1487 'invnumber' => { 'gl' => 'a.reference', 'arap' => 'a.invnumber', },
1488 'memo' => { 'gl' => 'ac.memo', 'arap' => 'ac.memo', },
1489 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
1490 'name' => { 'gl' => 'a.description', 'arap' => 'c.name', },
1493 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
1494 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
1495 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
1498 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
1499 foreach my $spec (@{ $sort_columns{$sortkey} }) {
1500 next if ($spec !~ m/^lower_(.*)$/);
1503 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
1506 $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
1507 $sth = prepare_query($form, $dbh, $query);
1510 qq|SELECT c.name, a.invnumber, a.ordnumber,
1511 ac.transdate, ac.amount * $ml AS paid, ac.source,
1512 a.invoice, a.id, ac.memo, '${arap}' AS module
1513 $columns_for_sorting{arap}
1515 JOIN $arap a ON (ac.trans_id = a.id)
1516 JOIN $table c ON (c.id = a.${table}_id)
1517 WHERE (ac.chart_id = ?)
1523 SELECT a.description, a.reference, NULL AS ordnumber,
1524 ac.transdate, ac.amount * $ml AS paid, ac.source,
1525 '0' as invoice, a.id, ac.memo, 'gl' AS module
1526 $columns_for_sorting{gl}
1528 JOIN gl a ON (a.id = ac.trans_id)
1529 WHERE (ac.chart_id = ?)
1532 AND (ac.amount * $ml) > 0
1534 ORDER BY $sortorder|;
1535 my $sth_details = prepare_query($form, $dbh, $q_details);
1539 # cycle through each id
1540 foreach my $accno (split(/ /, $form->{paymentaccounts})) {
1541 do_statement($form, $sth, $query, $accno);
1542 my $ref = $sth->fetchrow_hashref();
1543 push(@{ $form->{PR} }, $ref);
1546 $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
1548 do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
1549 while (my $pr = $sth_details->fetchrow_hashref()) {
1550 push(@{ $form->{ $ref->{id} } }, $pr);
1552 $sth_details->finish();
1557 $main::lxdebug->leave_sub();
1561 $main::lxdebug->enter_sub();
1563 my ($self, $myconfig, $form) = @_;
1565 # connect to database
1566 my $dbh = $form->dbconnect($myconfig);
1568 my $last_period = 0;
1571 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);
1573 $form->{decimalplaces} *= 1;
1575 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
1577 # if there are any compare dates
1579 if ($form->{fromdate} || $form->{todate}) {
1581 if ($form->{fromdate}) {
1582 $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1585 $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1588 my $kummfromdate = $form->{comparefromdate};
1589 my $kummtodate = $form->{comparetodate};
1590 &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
1593 my @periods = qw(jetzt kumm);
1594 my @gesamtleistung = qw(1 3);
1595 my @gesamtkosten = qw (10 11 12 13 14 15 16 17 18 20);
1597 qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
1599 foreach my $key (@periods) {
1600 $form->{ "$key" . "gesamtleistung" } = 0;
1601 $form->{ "$key" . "gesamtkosten" } = 0;
1603 foreach $category (@categories) {
1605 if (defined($form->{$category}{$key})) {
1606 $form->{"$key$category"} =
1607 $form->format_amount($myconfig,
1608 $form->round_amount($form->{$category}{$key}, 2
1610 $form->{decimalplaces},
1614 foreach my $item (@gesamtleistung) {
1615 $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
1617 $form->{ "$key" . "gesamtleistung" } -= $form->{2}{$key};
1619 foreach my $item (@gesamtkosten) {
1620 $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
1622 $form->{ "$key" . "rohertrag" } =
1623 $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
1624 $form->{ "$key" . "betriebrohertrag" } =
1625 $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
1626 $form->{ "$key" . "betriebsergebnis" } =
1627 $form->{ "$key" . "betriebrohertrag" } -
1628 $form->{ "$key" . "gesamtkosten" };
1629 $form->{ "$key" . "neutraleraufwand" } =
1630 $form->{19}{$key} + $form->{30}{$key} + $form->{31}{$key};
1631 $form->{ "$key" . "neutralerertrag" } =
1632 $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
1633 $form->{ "$key" . "ergebnisvorsteuern" } =
1634 $form->{ "$key" . "betriebsergebnis" } -
1635 $form->{ "$key" . "neutraleraufwand" } +
1636 $form->{ "$key" . "neutralerertrag" };
1637 $form->{ "$key" . "ergebnis" } =
1638 $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
1640 if ($form->{ "$key" . "gesamtleistung" } > 0) {
1641 foreach $category (@categories) {
1642 if (defined($form->{$category}{$key})) {
1643 $form->{ "$key" . "gl" . "$category" } =
1644 $form->format_amount(
1646 $form->round_amount(
1647 ($form->{$category}{$key} /
1648 $form->{ "$key" . "gesamtleistung" } * 100
1650 $form->{decimalplaces}
1652 $form->{decimalplaces},
1656 foreach my $item (@ergebnisse) {
1657 $form->{ "$key" . "gl" . "$item" } =
1658 $form->format_amount($myconfig,
1659 $form->round_amount(
1660 ( $form->{ "$key" . "$item" } /
1661 $form->{ "$key" . "gesamtleistung" } * 100
1663 $form->{decimalplaces}
1665 $form->{decimalplaces},
1670 if ($form->{ "$key" . "gesamtkosten" } > 0) {
1671 foreach $category (@categories) {
1672 if (defined($form->{$category}{$key})) {
1673 $form->{ "$key" . "gk" . "$category" } =
1674 $form->format_amount($myconfig,
1675 $form->round_amount(
1676 ($form->{$category}{$key} /
1677 $form->{ "$key" . "gesamtkosten" } * 100
1679 $form->{decimalplaces}
1681 $form->{decimalplaces},
1685 foreach my $item (@ergebnisse) {
1686 $form->{ "$key" . "gk" . "$item" } =
1687 $form->format_amount($myconfig,
1688 $form->round_amount(
1689 ($form->{ "$key" . "$item" } /
1690 $form->{ "$key" . "gesamtkosten" } * 100
1692 $form->{decimalplaces}
1694 $form->{decimalplaces},
1699 if ($form->{10}{$key} > 0) {
1700 foreach $category (@categories) {
1701 if (defined($form->{$category}{$key})) {
1702 $form->{ "$key" . "pk" . "$category" } =
1703 $form->format_amount(
1705 $form->round_amount(
1706 ($form->{$category}{$key} / $form->{10}{$key} * 100),
1707 $form->{decimalplaces}
1709 $form->{decimalplaces},
1713 foreach my $item (@ergebnisse) {
1714 $form->{ "$key" . "pk" . "$item" } =
1715 $form->format_amount($myconfig,
1716 $form->round_amount(
1717 ($form->{ "$key" . "$item" } /
1718 $form->{10}{$key} * 100
1720 $form->{decimalplaces}
1722 $form->{decimalplaces},
1727 if ($form->{4}{$key} > 0) {
1728 foreach $category (@categories) {
1729 if (defined($form->{$category}{$key})) {
1730 $form->{ "$key" . "auf" . "$category" } =
1731 $form->format_amount(
1733 $form->round_amount(
1734 ($form->{$category}{$key} / $form->{4}{$key} * 100),
1735 $form->{decimalplaces}
1737 $form->{decimalplaces},
1741 foreach my $item (@ergebnisse) {
1742 $form->{ "$key" . "auf" . "$item" } =
1743 $form->format_amount($myconfig,
1744 $form->round_amount(
1745 ($form->{ "$key" . "$item" } /
1746 $form->{4}{$key} * 100
1748 $form->{decimalplaces}
1750 $form->{decimalplaces},
1755 foreach my $item (@ergebnisse) {
1756 $form->{ "$key" . "$item" } =
1757 $form->format_amount($myconfig,
1758 $form->round_amount($form->{ "$key" . "$item" },
1759 $form->{decimalplaces}
1761 $form->{decimalplaces},
1768 $main::lxdebug->leave_sub();
1771 sub income_statement {
1772 $main::lxdebug->enter_sub();
1774 my ($self, $myconfig, $form) = @_;
1776 # connect to database
1777 my $dbh = $form->dbconnect($myconfig);
1779 my $last_period = 0;
1780 my @categories_einnahmen = qw(1 2 3 4 5 6 7);
1781 my @categories_ausgaben =
1782 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);
1784 my @ergebnisse = qw(sumeura sumeurb guvsumme);
1786 $form->{decimalplaces} *= 1;
1790 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
1794 foreach my $item (@categories_einnahmen) {
1795 $form->{"eur${item}"} =
1796 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1797 $form->{"sumeura"} += $form->{$item};
1799 foreach my $item (@categories_ausgaben) {
1800 $form->{"eur${item}"} =
1801 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1802 $form->{"sumeurb"} += $form->{$item};
1805 $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
1807 foreach my $item (@ergebnisse) {
1809 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1811 $main::lxdebug->leave_sub();