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 SL::DB::Helper::AccountingPeriod qw(get_balance_starting_date);
40 use List::Util qw(sum);
46 # new implementation of balance sheet
49 # stuff missing from the original implementation:
52 # - proper testing for heading charts
53 # - transmission from $form to TMPL realm is not as clear as i'd like
56 $main::lxdebug->enter_sub();
60 my $myconfig = \%main::myconfig;
61 my $form = $main::form;
62 my $dbh = $::form->get_standard_dbh;
65 my @categories = qw(A C L Q);
67 # if there are any dates construct a where
68 if ($form->{asofdate}) {
69 $form->{period} = $form->{this_period} = conv_dateq($form->{asofdate});
72 # get starting date for calculating balance
73 $form->{this_startdate} = $self->get_balance_starting_date($form->{asofdate});
75 get_accounts($dbh, $last_period, $form->{this_startdate}, $form->{asofdate}, $form, \@categories);
77 # if there are any compare dates
78 if ($form->{compareasofdate}) {
81 $form->{last_startdate} = $self->get_balance_starting_date($form->{compareasofdate});
83 get_accounts($dbh, $last_period, $form->{last_startdate} , $form->{compareasofdate}, $form, \@categories);
84 $form->{last_period} = conv_dateq($form->{compareasofdate});
87 # now we got $form->{A}{accno}{ } assets
88 # and $form->{L}{accno}{ } liabilities
89 # and $form->{Q}{accno}{ } equity
90 # build asset accounts
92 my %account = ('A' => { 'ml' => -1 },
94 'Q' => { 'ml' => 1 });
98 foreach my $category (grep { !/C/ } @categories) {
100 $TMPL_DATA->{$category} = [];
101 my $ml = $account{$category}{ml};
103 foreach my $key (sort keys %{ $form->{$category} }) {
105 my $row = { %{ $form->{$category}{$key} } };
107 # if charttype "heading" - calculate this entry, start a new batch of charts belonging to this heading and skip the rest bo the loop
108 # header charts are not real charts. start a sub aggregation with them, but don't calculate anything with them
109 if ($row->{charttype} eq "H") {
110 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
111 $row->{subdescription} = $account{$category}{subdescription};
112 $row->{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
113 $row->{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
116 $row->{subheader} = 1;
117 $account{$category}{subthis} = $row->{this};
118 $account{$category}{sublast} = $row->{last};
119 $account{$category}{subdescription} = $row->{description};
120 $account{$category}{subtotal} = 1;
125 next unless $form->{l_heading};
128 for my $period (qw(this last)) {
129 next if ($period eq 'last' && !$last_period);
131 $row->{$period} *= $ml;
134 push @{ $TMPL_DATA->{$category} }, $row;
137 # resolve heading/subtotal
138 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
139 $TMPL_DATA->{$category}[-1]{subdescription} = $account{$category}{subdescription};
140 $TMPL_DATA->{$category}[-1]{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
141 $TMPL_DATA->{$category}[-1]{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
144 $TMPL_DATA->{total}{$category}{this} = sum map { $_->{this} } @{ $TMPL_DATA->{$category} };
145 $TMPL_DATA->{total}{$category}{last} = sum map { $_->{last} } @{ $TMPL_DATA->{$category} };
148 for my $period (qw(this last)) {
149 next if ($period eq 'last' && !$last_period);
151 $form->{E}{$period} = $TMPL_DATA->{total}{A}{$period} - $TMPL_DATA->{total}{L}{$period} - $TMPL_DATA->{total}{Q}{$period};
152 $TMPL_DATA->{total}{Q}{$period} += $form->{E}{$period};
153 $TMPL_DATA->{total}{$period} = $TMPL_DATA->{total}{L}{$period} + $TMPL_DATA->{total}{Q}{$period};
155 $form->{E}{description}='nicht verbuchter Gewinn/Verlust';
156 push @{ $TMPL_DATA->{Q} }, $form->{E};
158 $main::lxdebug->leave_sub();
164 $main::lxdebug->enter_sub();
166 my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
168 my ($null, $department_id) = split /--/, $form->{department};
172 my $dpt_where_without_arapgl = '';
179 my $dec = $form->{decimalplaces};
181 my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |;
185 qq|SELECT c.accno, c.description, c.category
187 WHERE (c.charttype = 'H')
191 $sth = prepare_execute_query($form, $dbh, $query);
193 my @headingaccounts = ();
194 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
195 $form->{ $ref->{category} }{ $ref->{accno} }{description} =
196 "$ref->{description}";
197 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
198 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
200 push @headingaccounts, $ref->{accno};
205 # filter for opening and closing bookings
206 # if l_ob is selected l_cb is always ignored
207 if ( $last_period ) {
208 # ob/cb-settings for "compared to" balance
209 if ( $form->{l_ob_compared} ) {
210 $where .= ' AND ac.ob_transaction is true '
211 } elsif ( not $form->{l_cb_compared} ) {
212 $where .= ' AND ac.cb_transaction is false ';
215 # ob/cb-settings for "as of" balance
216 if ( $form->{l_ob} ) {
217 $where .= ' AND ac.ob_transaction is true '
218 } elsif ( not $form->{l_cb} ) {
219 $where .= ' AND ac.cb_transaction is false ';
225 $fromdate = conv_dateq($fromdate);
226 if ($form->{method} eq 'cash') {
227 $subwhere .= " AND (transdate >= $fromdate)";
228 $glwhere = " AND (ac.transdate >= $fromdate)";
230 $where .= " AND (ac.transdate >= $fromdate)";
235 $todate = conv_dateq($todate);
236 $where .= " AND (ac.transdate <= $todate)";
237 $subwhere .= " AND (transdate <= $todate)";
240 if ($department_id) {
241 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
244 if ($form->{project_id}) {
245 # Diese Bedingung wird derzeit niemals wahr sein, da man in Bericht->Bilanz keine
246 # Projekte auswählen kann
247 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
250 if ($form->{method} eq 'cash') {
252 qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
254 JOIN chart c ON (c.id = ac.chart_id)
255 JOIN ar a ON (a.id = ac.trans_id)
263 WHERE (a.chart_link LIKE '%AR_paid%')
267 GROUP BY c.accno, c.description, c.category
271 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
273 JOIN chart c ON (c.id = ac.chart_id)
274 JOIN ap a ON (a.id = ac.trans_id)
282 WHERE (a.chart_link LIKE '%AP_paid%')
286 GROUP BY c.accno, c.description, c.category
290 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
292 JOIN chart c ON (c.id = ac.chart_id)
293 JOIN gl a ON (a.id = ac.trans_id)
298 AND NOT ((ac.chart_link = 'AR') OR (ac.chart_link = 'AP'))
300 GROUP BY c.accno, c.description, c.category |;
302 if ($form->{project_id}) {
303 # s.o. keine Projektauswahl in Bilanz
308 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
310 JOIN ar a ON (a.id = ac.trans_id)
311 JOIN parts p ON (ac.parts_id = p.id)
312 JOIN chart c on (p.income_accno_id = c.id)
313 -- use transdate from subwhere
314 WHERE (c.category = 'I')
321 WHERE (a.chart_link LIKE '%AR_paid%')
325 GROUP BY c.accno, c.description, c.category
329 SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
331 JOIN ap a ON (a.id = ac.trans_id)
332 JOIN parts p ON (ac.parts_id = p.id)
333 JOIN chart c on (p.expense_accno_id = c.id)
334 WHERE (c.category = 'E')
341 WHERE a.chart_link LIKE '%AP_paid%'
345 GROUP BY c.accno, c.description, c.category |;
348 } else { # if ($form->{method} eq 'cash')
349 if ($department_id) {
350 $dpt_where = qq| AND a.department_id = | . conv_i($department_id);
351 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
352 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
353 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
357 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
359 JOIN chart c ON (c.id = ac.chart_id)
361 $dpt_where_without_arapgl
364 GROUP BY c.accno, c.description, c.category |;
366 if ($form->{project_id}) {
367 # s.o. keine Projektauswahl in Bilanz
371 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
373 JOIN ar a ON (a.id = ac.trans_id)
374 JOIN parts p ON (ac.parts_id = p.id)
375 JOIN chart c on (p.income_accno_id = c.id)
376 -- use transdate from subwhere
377 WHERE (c.category = 'I')
381 GROUP BY c.accno, c.description, c.category
385 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
387 JOIN ap a ON (a.id = ac.trans_id)
388 JOIN parts p ON (ac.parts_id = p.id)
389 JOIN chart c on (p.expense_accno_id = c.id)
390 WHERE (c.category = 'E')
394 GROUP BY c.accno, c.description, c.category |;
402 $sth = prepare_execute_query($form, $dbh, $query);
404 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
406 if ($ref->{category} eq 'C') {
407 $ref->{category} = 'A';
410 # get last heading account
411 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
415 $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
417 $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
421 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
422 $form->{ $ref->{category} }{ $ref->{accno} }{description} = $ref->{description};
423 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
426 $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
428 $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
433 # remove accounts with zero balance
434 foreach $category (@{$categories}) {
435 foreach $accno (keys %{ $form->{$category} }) {
436 $form->{$category}{$accno}{last} = $form->round_amount($form->{$category}{$accno}{last}, $dec);
437 $form->{$category}{$accno}{this} = $form->round_amount($form->{$category}{$accno}{this}, $dec);
439 delete $form->{$category}{$accno}
440 if ( $form->{$category}{$accno}{this} == 0
441 && $form->{$category}{$accno}{last} == 0);
445 $main::lxdebug->leave_sub();
449 $main::lxdebug->enter_sub();
451 my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
453 my ($null, $department_id) = split /--/, $form->{department};
457 my $dpt_where_without_arapgl;
466 $where .= ' AND ac.cb_transaction is false ' unless $form->{l_cb};
469 $fromdate = conv_dateq($fromdate);
470 if ($form->{method} eq 'cash') {
471 $subwhere .= " AND (transdate >= $fromdate)";
472 $glwhere = " AND (ac.transdate >= $fromdate)";
473 $prwhere = " AND (a.transdate >= $fromdate)";
474 $inwhere = " AND (acc.transdate >= $fromdate)";
476 $where .= " AND (ac.transdate >= $fromdate)";
477 # hotfix for projectfilter in guv and bwa
478 # fromdate is otherwise ignored if project is selected
479 $prwhere = " AND (a.transdate >= $fromdate)";
484 $todate = conv_dateq($todate);
485 $subwhere .= " AND (transdate <= $todate)";
486 $where .= " AND (ac.transdate <= $todate)";
487 $prwhere .= " AND (a.transdate <= $todate)";
488 $inwhere .= " AND (acc.transdate <= $todate)";
491 if ($department_id) {
492 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
495 if ($form->{project_id}) {
496 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
500 # GUV patch by Ronny Rentner (Bug 1190)
502 # GUV IST-Versteuerung
504 # Alle tatsaechlichen _Zahlungseingaenge_
505 # im Zeitraum erfassen
506 # (Teilzahlungen werden prozentual auf verschiedene Steuern aufgeteilt)
510 if ($form->{method} eq 'cash') {
513 SELECT SUM( ac.amount * CASE WHEN COALESCE((SELECT amount FROM ar a WHERE id = ac.trans_id $dpt_where), 0) != 0 THEN
514 /* ar amount is not zero, so we can divide by amount */
515 (SELECT SUM(acc.amount) * -1
518 AND acc.trans_id = ac.trans_id
519 AND acc.chart_link LIKE '%AR_paid%')
520 / (SELECT amount FROM ar WHERE id = ac.trans_id)
522 /* 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 */
524 ) AS amount, c.$category
526 LEFT JOIN chart c ON (c.id = ac.chart_id)
527 LEFT JOIN ar ON (ar.id = ac.trans_id)
528 WHERE ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE 1=1 $subwhere)
533 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
535 JOIN chart c ON (c.id = ac.chart_id)
536 JOIN ar a ON (a.id = ac.trans_id)
537 WHERE $where $dpt_where
538 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AR_paid%') $subwhere)
544 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
546 JOIN chart c ON (c.id = ac.chart_id)
547 JOIN ap a ON (a.id = ac.trans_id)
548 WHERE $where $dpt_where
549 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AP_paid%') $subwhere)
555 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
557 JOIN chart c ON (c.id = ac.chart_id)
558 JOIN gl a ON (a.id = ac.trans_id)
559 WHERE $where $dpt_where $glwhere
560 AND NOT ((ac.chart_link = 'AR') OR (ac.chart_link = 'AP'))
565 if ($form->{project_id}) {
569 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
571 JOIN ar a ON (a.id = ac.trans_id)
572 JOIN parts p ON (ac.parts_id = p.id)
573 JOIN chart c on (p.income_accno_id = c.id)
574 WHERE (c.category = 'I') $prwhere $dpt_where
575 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AR_paid%') $subwhere)
581 SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category
583 JOIN ap a ON (a.id = ac.trans_id)
584 JOIN parts p ON (ac.parts_id = p.id)
585 JOIN chart c on (p.expense_accno_id = c.id)
586 WHERE (c.category = 'E') $prwhere $dpt_where
587 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AP_paid%') $subwhere)
593 } else { # if ($form->{method} eq 'cash')
594 if ($department_id) {
595 $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
596 $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
597 (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
598 (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
602 SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
604 JOIN chart c ON (c.id = ac.chart_id)
606 $dpt_where_without_arapgl
608 GROUP BY c.$category |;
610 if ($form->{project_id}) {
614 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
616 JOIN ar a ON (a.id = ac.trans_id)
617 JOIN parts p ON (ac.parts_id = p.id)
618 JOIN chart c on (p.income_accno_id = c.id)
619 WHERE (c.category = 'I')
627 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
629 JOIN ap a ON (a.id = ac.trans_id)
630 JOIN parts p ON (ac.parts_id = p.id)
631 JOIN chart c on (p.expense_accno_id = c.id)
632 WHERE (c.category = 'E')
636 GROUP BY c.$category |;
644 foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
645 if ($category eq "pos_bwa") {
647 $form->{ $ref->{$category} }{kumm} += $ref->{amount};
649 $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
652 $form->{ $ref->{$category} } += $ref->{amount};
656 $main::lxdebug->leave_sub();
660 $main::lxdebug->enter_sub();
662 my ($self, $myconfig, $form, %options) = @_;
664 my $dbh = SL::DB->client->dbh;
666 my ($query, $sth, $ref);
669 my ($null, $department_id) = split /--/, $form->{department};
670 my @headingaccounts = ();
672 my $dpt_where_without_arapgl;
673 my ($customer_where, $customer_join, $customer_no_union);
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);
685 if ($form->{customer_id}) {
686 $customer_join = qq| JOIN ar a ON (ac.trans_id = a.id) |;
687 $customer_where = qq| AND (a.customer_id = | . conv_i($form->{customer_id}, 'NULL') . qq|) |;
688 $customer_no_union = qq| AND 1=0 |;
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)
738 $dpt_where_without_arapgl
742 ($form->{"${prefix}date"}) = selectfirst_array_query($form, $dbh, $query);
745 # get beginning balances
747 qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
749 LEFT JOIN chart c ON (ac.chart_id = c.id)
751 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
752 $dpt_where_without_arapgl
755 GROUP BY c.accno, c.category, c.description |;
757 $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
759 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
761 if ($ref->{amount} != 0 || $form->{all_accounts}) {
762 $trb{ $ref->{accno} }{description} = $ref->{description};
763 $trb{ $ref->{accno} }{charttype} = 'A';
764 $trb{ $ref->{accno} }{beginning_balance} = $ref->{amount};
766 if ($ref->{amount} > 0) {
767 $trb{ $ref->{accno} }{haben_eb} = $ref->{amount};
769 $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1;
771 $trb{ $ref->{accno} }{category} = $ref->{category};
780 qq|SELECT c.accno, c.description, c.category
782 WHERE c.charttype = 'H'
785 $sth = prepare_execute_query($form, $dbh, $query);
787 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
788 $trb{ $ref->{accno} }{description} = $ref->{description};
789 $trb{ $ref->{accno} }{charttype} = 'H';
790 $trb{ $ref->{accno} }{category} = $ref->{category};
792 push @headingaccounts, $ref->{accno};
798 my $saldowhere = " 1 = 1 ";
799 my $sumwhere = " 1 = 1 ";
801 my $sumsubwhere = '';
802 my $saldosubwhere = '';
803 my $glsaldowhere = '';
808 my ($fromdate, $todate, $fetch_accounts_before_from);
810 if ($form->{fromdate} || $form->{todate}) {
811 if ($form->{fromdate}) {
812 $fromdate = conv_dateq($form->{fromdate});
813 $tofrom .= " AND (ac.transdate >= $fromdate)";
814 $subwhere .= " AND (ac.transdate >= $fromdate)";
815 $sumsubwhere .= " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
816 $saldosubwhere .= " AND (ac,transdate>=(select date_trunc('year', date $fromdate))) ";
817 $invwhere .= " AND (a.transdate >= $fromdate)";
818 $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
819 $glwhere = " AND (ac.transdate >= $fromdate)";
820 $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
822 if ($form->{todate}) {
823 $todate = conv_dateq($form->{todate});
824 $tofrom .= " AND (ac.transdate <= $todate)";
825 $invwhere .= " AND (a.transdate <= $todate)";
826 $saldosubwhere .= " AND (ac.transdate <= $todate)";
827 $sumsubwhere .= " AND (ac.transdate <= $todate)";
828 $subwhere .= " AND (ac.transdate <= $todate)";
829 $glwhere .= " AND (ac.transdate <= $todate)";
830 $glsumwhere .= " AND (ac.transdate <= $todate) ";
831 $glsaldowhere .= " AND (ac.transdate <= $todate) ";
835 if ($form->{method} eq "cash") {
837 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) |;
838 $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) |;
840 $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) |;
842 $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
843 $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
844 $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
846 # get all entries before fromdate, which are not yet fetched
847 # TODO dpt_where_without_arapgl and project - project calculation seems bogus anyway
848 # TODO use fiscal_year_startdate for the whole trial balance
849 # anyway, if the last booking is in a deviating fiscal year, this already improves the query
850 my $fiscal_year_startdate = conv_dateq($self->get_balance_starting_date($form->{fromdate}));
851 $fetch_accounts_before_from = qq|SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
852 FROM acc_trans ac JOIN chart c ON (c.id = ac.chart_id) WHERE 1 = 1 AND (ac.transdate <= $fromdate)
853 AND (ac.transdate >= $fiscal_year_startdate)
854 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)
855 AND c.accno NOT IN (SELECT c.accno FROM acc_trans ac JOIN chart c ON (c.id = ac.chart_id) WHERE 1 = 1 AND (ac.transdate >= $fromdate) AND (ac.transdate <= $todate)
856 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL))
857 GROUP BY c.accno, c.description, c.category ORDER BY accno|;
861 SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
863 JOIN chart c ON (c.id = ac.chart_id)
866 $dpt_where_without_arapgl
868 GROUP BY c.accno, c.description, c.category |;
870 if ($form->{project_id}) {
872 -- add project transactions from invoice
876 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
878 JOIN ar a ON (ac.trans_id = a.id)
879 JOIN parts p ON (ac.parts_id = p.id)
880 JOIN chart c ON (p.income_accno_id = c.id)
885 GROUP BY c.accno, c.description, c.category
889 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
891 JOIN ap a ON (ac.trans_id = a.id)
892 JOIN parts p ON (ac.parts_id = p.id)
893 JOIN chart c ON (p.expense_accno_id = c.id)
898 GROUP BY c.accno, c.description, c.category
902 $query .= qq| ORDER BY accno|;
904 $sth = prepare_execute_query($form, $dbh, $query);
906 # calculate the debit and credit in the period
907 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
908 $trb{ $ref->{accno} }{description} = $ref->{description};
909 $trb{ $ref->{accno} }{charttype} = 'A';
910 $trb{ $ref->{accno} }{category} = $ref->{category};
911 $trb{ $ref->{accno} }{amount} += $ref->{amount};
915 if ($form->{method} ne "cash") { # better eq 'accrual'
916 $sth = prepare_execute_query($form, $dbh, $fetch_accounts_before_from);
917 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
918 $trb{ $ref->{accno} }{description} = $ref->{description};
919 $trb{ $ref->{accno} }{charttype} = 'A';
920 $trb{ $ref->{accno} }{category} = $ref->{category};
921 $trb{ $ref->{accno} }{amount} += $ref->{amount};
926 # prepare query for each account
927 my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
931 (SELECT SUM(ac.amount) * -1
933 JOIN chart c ON (c.id = ac.chart_id)
936 $dpt_where_without_arapgl
940 AND (c.accno = ?)) AS debit,
942 (SELECT SUM(ac.amount)
944 JOIN chart c ON (c.id = ac.chart_id)
947 $dpt_where_without_arapgl
951 AND c.accno = ?) AS credit,
952 (SELECT SUM(ac.amount)
954 JOIN chart c ON (ac.chart_id = c.id)
957 $dpt_where_without_arapgl
960 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
962 (SELECT SUM(ac.amount)
964 JOIN chart c ON (ac.chart_id = c.id)
967 $dpt_where_without_arapgl
971 AND c.accno = ?) AS sum_credit,
973 (SELECT SUM(ac.amount)
975 JOIN chart c ON (ac.chart_id = c.id)
978 $dpt_where_without_arapgl
982 AND c.accno = ?) AS sum_debit,
984 (SELECT max(ac.transdate) FROM acc_trans ac
985 JOIN chart c ON (ac.chart_id = c.id)
988 $dpt_where_without_arapgl
991 AND c.accno = ?) AS last_transaction
996 $drcr = prepare_query($form, $dbh, $q_drcr);
998 if ($form->{project_id}) {
999 # prepare query for each account
1002 (SELECT SUM(ac.sellprice * ac.qty) * -1
1004 JOIN parts p ON (ac.parts_id = p.id)
1005 JOIN ap a ON (ac.trans_id = a.id)
1006 JOIN chart c ON (p.expense_accno_id = c.id)
1011 AND c.accno = ?) AS debit,
1013 (SELECT SUM(ac.sellprice * ac.qty)
1015 JOIN parts p ON (ac.parts_id = p.id)
1016 JOIN ar a ON (ac.trans_id = a.id)
1017 JOIN chart c ON (p.income_accno_id = c.id)
1022 AND c.accno = ?) AS credit,
1024 (SELECT SUM(ac.amount)
1026 JOIN chart c ON (ac.chart_id = c.id)
1029 $dpt_where_without_arapgl
1033 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
1035 (SELECT SUM(ac.amount)
1037 JOIN chart c ON (ac.chart_id = c.id)
1040 $dpt_where_without_arapgl
1045 AND c.accno = ?) AS sum_credit,
1047 (SELECT SUM(ac.amount)
1049 JOIN chart c ON (ac.chart_id = c.id)
1053 $dpt_where_without_arapgl
1057 AND c.accno = ?) AS sum_debit,
1060 (SELECT max(ac.transdate) FROM acc_trans ac
1061 JOIN chart c ON (ac.chart_id = c.id)
1064 $dpt_where_without_arapgl
1067 AND c.accno = ?) AS last_transaction
1070 $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
1074 my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
1076 foreach my $accno (sort keys %trb) {
1079 $ref->{accno} = $accno;
1080 map { $ref->{$_} = $trb{$accno}{$_} }
1081 qw(description category charttype amount soll_eb haben_eb beginning_balance);
1083 $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
1085 if ($trb{$accno}{charttype} eq 'A') {
1088 do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1090 ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
1091 my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
1092 $last_transaction = "";
1093 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
1094 $ref->{debit} += $debit;
1095 $ref->{credit} += $credit;
1097 $ref->{haben_saldo} += $saldo;
1099 $ref->{soll_saldo} += $saldo * -1;
1101 $ref->{last_transaction} = $last_transaction;
1102 $ref->{soll_kumuliert} = $soll_kumuliert * -1;
1103 $ref->{haben_kumuliert} = $haben_kumuliert;
1107 if ($form->{project_id}) {
1110 do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1112 ($debit, $credit) = (0, 0);
1113 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
1114 $ref->{debit} += $debit;
1115 $ref->{credit} += $credit;
1117 $ref->{haben_saldo} += $saldo;
1119 $ref->{soll_saldo} += $saldo * -1;
1121 $ref->{soll_kumuliert} += $soll_kumuliert * -1;
1122 $ref->{haben_kumuliert} += $haben_kumuliert;
1124 $project_drcr->finish;
1127 $ref->{debit} = $form->round_amount($ref->{debit}, 2);
1128 $ref->{credit} = $form->round_amount($ref->{credit}, 2);
1130 if ($ref->{haben_saldo} != 0) {
1131 $ref->{haben_saldo} = $ref->{haben_saldo} + $ref->{beginning_balance};
1132 if ($ref->{haben_saldo} < 0) {
1133 $ref->{soll_saldo} = $form->round_amount(($ref->{haben_saldo} *- 1), 2);
1134 $ref->{haben_saldo} = 0;
1137 $ref->{soll_saldo} = $ref->{soll_saldo} - $ref->{beginning_balance};
1138 if ($ref->{soll_saldo} < 0) {
1139 $ref->{haben_saldo} = $form->round_amount(($ref->{soll_saldo} * -1), 2);
1140 $ref->{soll_saldo} = 0;
1143 $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
1144 $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
1145 $ref->{haben_kumuliert} = $form->round_amount($ref->{haben_kumuliert}, 2);
1146 $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
1151 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1152 $accno = pop @accno;
1154 $trb{$accno}{debit} += $ref->{debit};
1155 $trb{$accno}{credit} += $ref->{credit};
1156 $trb{$accno}{soll_saldo} += $ref->{soll_saldo};
1157 $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
1158 $trb{$accno}{soll_kumuliert} += $ref->{soll_kumuliert};
1159 $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
1162 push @{ $form->{TB} }, $ref;
1166 # debits and credits for headings
1167 foreach my $accno (@headingaccounts) {
1168 foreach $ref (@{ $form->{TB} }) {
1169 if ($accno eq $ref->{accno}) {
1170 $ref->{debit} = $trb{$accno}{debit};
1171 $ref->{credit} = $trb{$accno}{credit};
1172 $ref->{soll_saldo} = $trb{$accno}{soll_saldo};
1173 $ref->{haben_saldo} = $trb{$accno}{haben_saldo};
1174 $ref->{soll_kumuliert} = $trb{$accno}{soll_kumuliert};
1175 $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert};
1180 $main::lxdebug->leave_sub();
1184 $main::lxdebug->enter_sub();
1185 my ($self, $dbh, $form) = @_;
1186 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
1187 my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
1188 my $sth = $dbh->prepare($query);
1189 while(my $ref = $sth->fetchrow_hashref()) {
1190 $ref->{invnumer} =~ s/Storno zu //g;
1191 $form->{storno}{$ref->{invnumber}} = 1;
1193 $main::lxdebug->leave_sub();
1197 $main::lxdebug->enter_sub();
1199 my ($self, $myconfig, $form) = @_;
1201 # connect to database
1202 my $dbh = SL::DB->client->dbh;
1204 my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
1206 # falls customer ziehen wir die offene forderungsliste
1207 # anderfalls für die lieferanten die offenen verbindlichkeitne
1208 if ($form->{ct} eq "customer") {
1221 $ct_id = "${ct}_id";
1223 # erweiterung um einen freien zeitraum oder einen stichtag
1224 # mit entsprechender altersstrukturliste (s.a. Bug 1842)
1225 # eine neue variable an der oberfläche eingeführt, somit ist
1226 # todate == freier zeitrau und fordate == stichtag
1227 # duedate_where == nur fällige rechnungen anzeigen
1229 my ($review_of_aging_list, $todate, $fromdate, $fromwhere, $fordate,
1232 if ($form->{reporttype} eq 'custom') { # altersstrukturliste, nur fällige
1234 # explizit rausschmeissen was man für diesen bericht nicht braucht
1235 delete $form->{fromdate};
1236 delete $form->{todate};
1238 # an der oberfläche ist das tagesaktuelle datum vorausgewählt
1239 # falls es dennoch per Benutzereingabe gelöscht wird, lieber wieder vorbelegen
1240 # ferner muss für die spätere DB-Abfrage muss todate gesetzt sein.
1241 $form->{fordate} = $form->current_date($myconfig) unless ($form->{fordate});
1242 $fordate = conv_dateq($form->{fordate});
1245 if ($form->{review_of_aging_list}) { # falls die liste leer ist, alles anzeigen
1246 if ($form->{review_of_aging_list} =~ m "-") { # .. periode von bis
1247 my @period = split(/-/, $form->{review_of_aging_list}); # ... von periode bis periode
1248 $review_of_aging_list = " AND $period[0] < (date $fordate) - duedate
1249 AND (date $fordate) - duedate < $period[1]";
1251 $form->{review_of_aging_list} =~ s/[^0-9]//g; # größer 120 das substitute ist nur für das '>' zeichen
1252 $review_of_aging_list = " AND $form->{review_of_aging_list} < (date $fordate) - duedate";
1255 $duedate_where = " AND (date $fordate) - duedate >= 0 ";
1256 } else { # freier zeitraum, nur rechnungsdatum und OHNE review_of_aging_list
1257 $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
1258 $todate = conv_dateq($form->{todate});
1259 $fromdate = conv_dateq($form->{fromdate});
1260 $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
1262 my $where = " 1 = 1 ";
1265 if ($form->{$ct_id}) {
1266 $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
1267 } elsif ($form->{ $form->{ct} }) {
1268 $where .= qq| AND (ct.name ILIKE | . $dbh->quote(like($form->{$ct})) . qq|)|;
1273 if ($form->{department}) {
1274 my ($null, $department_id) = split /--/, $form->{department};
1275 $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
1276 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1277 $where_dpt = qq| AND (${arap}.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1281 SELECT ${ct}.id AS ctid, ${ct}.name,
1282 street, zipcode, city, country, contact, email,
1283 phone as customerphone, fax as customerfax, ${ct}number,
1284 "invnumber", "transdate",
1285 (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",
1286 "duedate", invoice, ${arap}.id, date_part('days', now() - duedate) as overduedays,
1289 WHERE (${arap}.currency_id = exchangerate.currency_id)
1290 AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
1292 WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
1293 AND NOT COALESCE (${arap}.storno, 'f')
1294 AND (${arap}.${ct}_id = ${ct}.id)
1297 AND (transdate <= (date $todate) $fromwhere )
1298 $review_of_aging_list
1300 ORDER BY ctid, transdate, invnumber |;
1302 my $sth_details = prepare_query($form, $dbh, $q_details);
1304 # select outstanding vendors or customers, depends on $ct
1306 qq|SELECT DISTINCT ct.id, ct.name
1307 FROM $ct ct, $arap a
1310 AND (a.${ct_id} = ct.id)
1311 AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
1312 AND (a.transdate <= $todate $fromwhere)
1315 my $sth = prepare_execute_query($form, $dbh, $query);
1318 # for each company that has some stuff outstanding
1319 while (my ($id) = $sth->fetchrow_array) {
1320 do_statement($form, $sth_details, $q_details, $id);
1322 while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
1323 $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
1324 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
1325 push @{ $form->{AG} }, $ref;
1328 $sth_details->finish;
1334 $main::lxdebug->leave_sub();
1338 $main::lxdebug->enter_sub();
1340 my ($self, $myconfig, $form) = @_;
1342 my $dbh = SL::DB->client->dbh;
1344 my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
1347 qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
1350 ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
1351 selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
1353 $main::lxdebug->leave_sub();
1357 $main::lxdebug->enter_sub();
1359 my ($self, $myconfig, $form) = @_;
1361 my $dbh = SL::DB->client->dbh;
1363 my ($null, $department_id) = split /--/, $form->{department};
1366 my $where = "1 = 1";
1368 if ($department_id) {
1369 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
1374 if ($form->{accno}) {
1375 $accno = $form->{accno};
1376 $rate = $form->{"$form->{accno}_rate"};
1377 $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
1383 if ($form->{db} eq 'ar') {
1384 $table = "customer";
1391 my $arap = lc($ARAP);
1393 my $transdate = "a.transdate";
1395 if ($form->{method} eq 'cash') {
1396 $transdate = "a.datepaid";
1398 my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
1405 WHERE (a.chart_link LIKE '%${ARAP}_paid%')
1406 AND (transdate <= $todate)
1411 # if there are any dates construct a where
1412 $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
1413 $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
1415 my $ml = ($form->{db} eq 'ar') ? 1 : -1;
1417 my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
1418 $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
1421 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
1422 ac.amount * $ml AS tax
1424 JOIN ${arap} a ON (a.id = ac.trans_id)
1425 JOIN chart ch ON (ch.id = ac.chart_id)
1426 JOIN $table n ON (n.id = a.${table}_id)
1430 AND (a.invoice = '0')
1434 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
1435 i.sellprice * i.qty * $rate * $ml AS tax
1437 JOIN ${arap} a ON (a.id = ac.trans_id)
1438 JOIN chart ch ON (ch.id = ac.chart_id)
1439 JOIN $table n ON (n.id = a.${table}_id)
1440 JOIN ${table}tax t ON (t.${table}_id = n.id)
1441 JOIN invoice i ON (i.trans_id = a.id)
1445 AND (a.invoice = '1')
1446 ORDER BY $sortorder|;
1448 $form->{TR} = selectall_hashref_query($form, $dbh, $query);
1450 $main::lxdebug->leave_sub();
1453 sub paymentaccounts {
1454 $main::lxdebug->enter_sub();
1456 my ($self, $myconfig, $form) = @_;
1458 # connect to database, turn AutoCommit off
1459 my $dbh = SL::DB->client->dbh;
1461 my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
1463 # get A(R|P)_paid accounts
1465 qq|SELECT accno, description
1467 WHERE link LIKE '%${ARAP}_paid%'|;
1468 $form->{PR} = selectall_hashref_query($form, $dbh, $query);
1470 $main::lxdebug->leave_sub();
1474 $main::lxdebug->enter_sub();
1476 my ($self, $myconfig, $form) = @_;
1478 # connect to database, turn AutoCommit off
1479 my $dbh = SL::DB->client->dbh;
1484 if ($form->{db} eq 'ar') {
1485 $table = 'customer';
1496 if ($form->{department_id}) {
1497 $where = qq| AND (a.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
1500 if ($form->{fromdate}) {
1501 $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
1503 if ($form->{todate}) {
1504 $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
1506 if (!$form->{fx_transaction}) {
1507 $where .= " AND ac.fx_transaction = '0'";
1512 if ($form->{reference}) {
1513 $reference = $dbh->quote(like($form->{reference}));
1514 $invnumber = " AND (a.invnumber LIKE $reference)";
1515 $reference = " AND (a.reference LIKE $reference)";
1517 if ($form->{source}) {
1518 $where .= " AND (ac.source ILIKE " . $dbh->quote(like($form->{source})) . ") ";
1520 if ($form->{memo}) {
1521 $where .= " AND (ac.memo ILIKE " . $dbh->quote(like($form->{memo})) . ") ";
1524 my %sort_columns = (
1525 'transdate' => [ qw(transdate lower_invnumber lower_name) ],
1526 'invnumber' => [ qw(lower_invnumber lower_name transdate) ],
1527 'name' => [ qw(lower_name transdate) ],
1528 'source' => [ qw(lower_source) ],
1529 'memo' => [ qw(lower_memo) ],
1531 my %lowered_columns = (
1532 'invnumber' => { 'gl' => 'a.reference', 'arap' => 'a.invnumber', },
1533 'memo' => { 'gl' => 'ac.memo', 'arap' => 'ac.memo', },
1534 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
1535 'name' => { 'gl' => 'a.description', 'arap' => 'c.name', },
1538 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
1539 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
1540 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
1543 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
1544 foreach my $spec (@{ $sort_columns{$sortkey} }) {
1545 next if ($spec !~ m/^lower_(.*)$/);
1548 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
1551 $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
1552 $sth = prepare_query($form, $dbh, $query);
1555 qq|SELECT c.name, a.invnumber, a.ordnumber,
1556 ac.transdate, ac.amount * $ml AS paid, ac.source,
1557 a.invoice, a.id, ac.memo, '${arap}' AS module
1558 $columns_for_sorting{arap}
1560 JOIN $arap a ON (ac.trans_id = a.id)
1561 JOIN $table c ON (c.id = a.${table}_id)
1562 WHERE (ac.chart_id = ?)
1568 SELECT a.description, a.reference, NULL AS ordnumber,
1569 ac.transdate, ac.amount * $ml AS paid, ac.source,
1570 '0' as invoice, a.id, ac.memo, 'gl' AS module
1571 $columns_for_sorting{gl}
1573 JOIN gl a ON (a.id = ac.trans_id)
1574 WHERE (ac.chart_id = ?)
1577 AND (ac.amount * $ml) > 0
1579 ORDER BY $sortorder|;
1580 my $sth_details = prepare_query($form, $dbh, $q_details);
1584 # cycle through each id
1585 foreach my $accno (split(/ /, $form->{paymentaccounts})) {
1586 do_statement($form, $sth, $query, $accno);
1587 my $ref = $sth->fetchrow_hashref();
1588 push(@{ $form->{PR} }, $ref);
1591 $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
1593 do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
1594 while (my $pr = $sth_details->fetchrow_hashref()) {
1595 push(@{ $form->{ $ref->{id} } }, $pr);
1597 $sth_details->finish();
1600 $main::lxdebug->leave_sub();
1604 $main::lxdebug->enter_sub();
1606 my ($self, $myconfig, $form) = @_;
1608 my $dbh = SL::DB->client->dbh;
1610 my $last_period = 0;
1613 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);
1615 $form->{decimalplaces} *= 1;
1617 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
1619 # if there are any compare dates
1621 if ($form->{fromdate} || $form->{todate}) {
1623 if ($form->{fromdate}) {
1624 $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1627 $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1630 my $kummfromdate = $form->{comparefromdate};
1631 my $kummtodate = $form->{comparetodate};
1632 &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
1635 my @periods = qw(jetzt kumm);
1636 my @gesamtleistung = qw(1 3);
1637 my @gesamtkosten = qw (10 11 12 13 14 15 16 17 18 20);
1639 qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
1641 foreach my $key (@periods) {
1642 $form->{ "$key" . "gesamtleistung" } = 0;
1643 $form->{ "$key" . "gesamtkosten" } = 0;
1645 foreach $category (@categories) {
1647 if (defined($form->{$category}{$key})) {
1648 $form->{"$key$category"} =
1649 $form->format_amount($myconfig,
1650 $form->round_amount($form->{$category}{$key}, 2
1652 $form->{decimalplaces},
1656 foreach my $item (@gesamtleistung) {
1657 $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
1659 $form->{ "$key" . "gesamtleistung" } -= $form->{2}{$key};
1661 foreach my $item (@gesamtkosten) {
1662 $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
1664 $form->{ "$key" . "rohertrag" } =
1665 $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
1666 $form->{ "$key" . "betriebrohertrag" } =
1667 $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
1668 $form->{ "$key" . "betriebsergebnis" } =
1669 $form->{ "$key" . "betriebrohertrag" } -
1670 $form->{ "$key" . "gesamtkosten" };
1671 $form->{ "$key" . "neutraleraufwand" } =
1672 $form->{19}{$key} + $form->{30}{$key} + $form->{31}{$key};
1673 $form->{ "$key" . "neutralerertrag" } =
1674 $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
1675 $form->{ "$key" . "ergebnisvorsteuern" } =
1676 $form->{ "$key" . "betriebsergebnis" } -
1677 $form->{ "$key" . "neutraleraufwand" } +
1678 $form->{ "$key" . "neutralerertrag" };
1679 $form->{ "$key" . "ergebnis" } =
1680 $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
1682 if ($form->{ "$key" . "gesamtleistung" } > 0) {
1683 foreach $category (@categories) {
1684 if (defined($form->{$category}{$key})) {
1685 $form->{ "$key" . "gl" . "$category" } =
1686 $form->format_amount(
1688 $form->round_amount(
1689 ($form->{$category}{$key} /
1690 $form->{ "$key" . "gesamtleistung" } * 100
1692 $form->{decimalplaces}
1694 $form->{decimalplaces},
1698 foreach my $item (@ergebnisse) {
1699 $form->{ "$key" . "gl" . "$item" } =
1700 $form->format_amount($myconfig,
1701 $form->round_amount(
1702 ( $form->{ "$key" . "$item" } /
1703 $form->{ "$key" . "gesamtleistung" } * 100
1705 $form->{decimalplaces}
1707 $form->{decimalplaces},
1712 if ($form->{ "$key" . "gesamtkosten" } > 0) {
1713 foreach $category (@categories) {
1714 if (defined($form->{$category}{$key})) {
1715 $form->{ "$key" . "gk" . "$category" } =
1716 $form->format_amount($myconfig,
1717 $form->round_amount(
1718 ($form->{$category}{$key} /
1719 $form->{ "$key" . "gesamtkosten" } * 100
1721 $form->{decimalplaces}
1723 $form->{decimalplaces},
1727 foreach my $item (@ergebnisse) {
1728 $form->{ "$key" . "gk" . "$item" } =
1729 $form->format_amount($myconfig,
1730 $form->round_amount(
1731 ($form->{ "$key" . "$item" } /
1732 $form->{ "$key" . "gesamtkosten" } * 100
1734 $form->{decimalplaces}
1736 $form->{decimalplaces},
1741 if ($form->{10}{$key} > 0) {
1742 foreach $category (@categories) {
1743 if (defined($form->{$category}{$key})) {
1744 $form->{ "$key" . "pk" . "$category" } =
1745 $form->format_amount(
1747 $form->round_amount(
1748 ($form->{$category}{$key} / $form->{10}{$key} * 100),
1749 $form->{decimalplaces}
1751 $form->{decimalplaces},
1755 foreach my $item (@ergebnisse) {
1756 $form->{ "$key" . "pk" . "$item" } =
1757 $form->format_amount($myconfig,
1758 $form->round_amount(
1759 ($form->{ "$key" . "$item" } /
1760 $form->{10}{$key} * 100
1762 $form->{decimalplaces}
1764 $form->{decimalplaces},
1769 if ($form->{4}{$key} > 0) {
1770 foreach $category (@categories) {
1771 if (defined($form->{$category}{$key})) {
1772 $form->{ "$key" . "auf" . "$category" } =
1773 $form->format_amount(
1775 $form->round_amount(
1776 ($form->{$category}{$key} / $form->{4}{$key} * 100),
1777 $form->{decimalplaces}
1779 $form->{decimalplaces},
1783 foreach my $item (@ergebnisse) {
1784 $form->{ "$key" . "auf" . "$item" } =
1785 $form->format_amount($myconfig,
1786 $form->round_amount(
1787 ($form->{ "$key" . "$item" } /
1788 $form->{4}{$key} * 100
1790 $form->{decimalplaces}
1792 $form->{decimalplaces},
1797 foreach my $item (@ergebnisse) {
1798 $form->{ "$key" . "$item" } =
1799 $form->format_amount($myconfig,
1800 $form->round_amount($form->{ "$key" . "$item" },
1801 $form->{decimalplaces}
1803 $form->{decimalplaces},
1809 $main::lxdebug->leave_sub();
1812 sub income_statement {
1813 $main::lxdebug->enter_sub();
1815 my ($self, $myconfig, $form) = @_;
1817 # connect to database
1818 my $dbh = $form->dbconnect($myconfig);
1820 my $last_period = 0;
1821 my @categories_einnahmen = qw(1 2 3 4 5 6 7);
1822 my @categories_ausgaben =
1823 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);
1825 my @ergebnisse = qw(sumeura sumeurb guvsumme);
1827 $form->{decimalplaces} *= 1;
1831 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
1835 foreach my $item (@categories_einnahmen) {
1836 $form->{"eur${item}"} =
1837 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1838 $form->{"sumeura"} += $form->{$item};
1840 foreach my $item (@categories_ausgaben) {
1841 $form->{"eur${item}"} =
1842 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1843 $form->{"sumeurb"} += $form->{$item};
1846 $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
1848 foreach my $item (@ergebnisse) {
1850 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1852 $main::lxdebug->leave_sub();
1855 sub erfolgsrechnung {
1856 $main::lxdebug->enter_sub();
1858 my ($self, $myconfig, $form) = @_;
1859 $form->{company} = $::instance_conf->get_company;
1860 $form->{address} = $::instance_conf->get_address;
1861 $form->{fromdate} = DateTime->new(year => 2000, month => 1, day => 1)->to_kivitendo unless $form->{fromdate};
1862 $form->{todate} = $form->current_date(%{$myconfig}) unless $form->{todate};
1864 my %categories = (I => "ERTRAG", E => "AUFWAND");
1865 my $fromdate = conv_dateq($form->{fromdate});
1866 my $todate = conv_dateq($form->{todate});
1870 foreach my $category ('I', 'E') {
1872 name => $categories{$category},
1874 accounts => get_accounts_ch($category),
1876 foreach my $account (@{$category{accounts}}) {
1877 $account->{total} += ($account->{category} eq $category ? 1 : -1) * get_total_ch($account->{id}, $fromdate, $todate);
1878 $category{total} += $account->{total};
1879 $account->{total} = $form->format_amount($myconfig, $form->parse_amount($myconfig, $account->{total}), 2);
1881 $form->{total} += $category{total};
1882 $category{total} = $form->format_amount($myconfig, $form->parse_amount($myconfig, $category{total}), 2);
1883 push(@{$form->{categories}}, \%category);
1885 $form->{total} = $form->format_amount($myconfig, $form->parse_amount($myconfig, $form->{total}), 2);
1887 $main::lxdebug->leave_sub();
1891 sub get_accounts_ch {
1892 $main::lxdebug->enter_sub();
1894 my ($category) = @_;
1897 if ($category eq 'I') {
1898 $inclusion = "AND pos_er = NULL OR pos_er = '1'";
1899 } elsif ($category eq 'E') {
1900 $inclusion = "AND pos_er = NULL OR pos_er = '6'";
1906 SELECT id, accno, description, category
1908 WHERE category = ? $inclusion
1911 my $accounts = _query($query, $category);
1913 $main::lxdebug->leave_sub();
1918 $main::lxdebug->enter_sub();
1920 my ($chart_id, $fromdate, $todate) = @_;
1929 $total += _query($query, $chart_id, $fromdate, $todate)->[0]->{sum};
1931 $main::lxdebug->leave_sub();
1935 sub _query {return selectall_hashref_query($::form, $::form->get_standard_dbh, @_);}