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:
49 # - format (2 places, varying signs of negative amounts)
50 # - rounding (might not be necessary)
51 # - accno and subdescription
52 # - proper testing for heading charts
53 # - transmission from $form to TMPL realm is not as clear as i'd like
55 $main::lxdebug->enter_sub();
57 my $myconfig = \%main::myconfig;
58 my $form = $main::form;
59 my $dbh = $form->get_standard_dbh($myconfig);
62 my @categories = qw(A C L Q);
64 # if there are any dates construct a where
65 if ($form->{asofdate}) {
66 $form->{period} = $form->{this_period} = conv_dateq($form->{asofdate});
69 get_accounts($dbh, $last_period, "", $form->{asofdate}, $form, \@categories);
71 # if there are any compare dates
72 if ($form->{compareasofdate}) {
74 get_accounts($dbh, $last_period, "", $form->{compareasofdate}, $form, \@categories);
75 $form->{last_period} = conv_dateq($form->{compareasofdate});
78 # now we got $form->{A}{accno}{ } assets
79 # and $form->{L}{accno}{ } liabilities
80 # and $form->{Q}{accno}{ } equity
81 # build asset accounts
83 my %account = ('A' => { 'ml' => -1 },
85 'Q' => { 'ml' => 1 });
89 foreach my $category (grep { !/C/ } @categories) {
91 $TMPL_DATA->{$category} = [];
92 my $ml = $account{$category}{ml};
94 foreach my $key (sort keys %{ $form->{$category} }) {
96 my $row = { %{ $form->{$category}{$key} } };
98 # if charttype "heading" - calculate this entry, start a new batch of charts belonging to this heading and skip the rest bo the loop
99 # header charts are not real charts. start a sub aggregation with them, but don't calculate anything with them
100 if ($row->{charttype} eq "H") {
101 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
102 $row->{subdescription} = $account{$category}{subdescription};
103 $row->{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
104 $row->{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
107 $row->{subheader} = 1;
108 $account{$category}{subthis} = $row->{this};
109 $account{$category}{sublast} = $row->{last};
110 $account{$category}{subdescription} = $row->{description};
111 $account{$category}{subtotal} = 1;
116 next unless $form->{l_heading};
119 for my $period (qw(this last)) {
120 next if ($period eq 'last' && !$last_period);
122 $row->{$period} *= $ml;
123 $form->{total}{$category}{$period} += $row->{$period}; # if ($row->{charttype} eq 'A') { # why??
126 push @{ $TMPL_DATA->{$category} }, $row;
129 # resolve heading/subtotal
130 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
131 $TMPL_DATA->{$category}[-1]{subdescription} = $account{$category}{subdescription};
132 $TMPL_DATA->{$category}[-1]{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
133 $TMPL_DATA->{$category}[-1]{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
136 $TMPL_DATA->{total}{$category}{this} = sum map { $_->{this} } @{ $TMPL_DATA->{$category} };
137 $TMPL_DATA->{total}{$category}{last} = sum map { $_->{last} } @{ $TMPL_DATA->{$category} };
140 for my $period (qw(this last)) {
141 next if ($period eq 'last' && !$last_period);
143 $form->{E}{$period} = $form->{total}{A}{$period} - $form->{total}{L}{$period} - $form->{total}{Q}{$period};
144 $form->{total}{Q}{$period} += $form->{E}{$period};
145 $TMPL_DATA->{total}{Q}{$period} = $form->{total}{Q}{$period};
146 $TMPL_DATA->{total}{$period} = $form->{total}{L}{$period} + $form->{total}{Q}{$period};
149 push @{ $TMPL_DATA->{Q} }, $form->{E};
151 $main::lxdebug->leave_sub();
157 $main::lxdebug->enter_sub();
159 my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
161 my ($null, $department_id) = split /--/, $form->{department};
172 my $dec = $form->{decimalplaces};
174 my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |;
178 qq|SELECT c.accno, c.description, c.category
180 WHERE (c.charttype = 'H')
184 $sth = prepare_execute_query($form, $dbh, $query);
186 my @headingaccounts = ();
187 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
188 $form->{ $ref->{category} }{ $ref->{accno} }{description} =
189 "$ref->{description}";
190 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
191 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
193 push @headingaccounts, $ref->{accno};
199 $fromdate = conv_dateq($fromdate);
200 if ($form->{method} eq 'cash') {
201 $subwhere .= " AND (transdate >= $fromdate)";
202 $glwhere = " AND (ac.transdate >= $fromdate)";
204 $where .= " AND (ac.transdate >= $fromdate)";
209 $todate = conv_dateq($todate);
210 $where .= " AND (ac.transdate <= $todate)";
211 $subwhere .= " AND (transdate <= $todate)";
214 if ($department_id) {
215 $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
216 $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|)|;
219 if ($form->{project_id}) {
220 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
223 if ($form->{method} eq 'cash') {
225 qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
227 JOIN chart c ON (c.id = ac.chart_id)
228 JOIN ar a ON (a.id = ac.trans_id)
237 JOIN chart ON (chart_id = id)
238 WHERE (link LIKE '%AR_paid%')
242 GROUP BY c.accno, c.description, c.category
246 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
248 JOIN chart c ON (c.id = ac.chart_id)
249 JOIN ap a ON (a.id = ac.trans_id)
258 JOIN chart ON (chart_id = id)
259 WHERE (link LIKE '%AP_paid%')
263 GROUP BY c.accno, c.description, c.category
267 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
269 JOIN chart c ON (c.id = ac.chart_id)
270 JOIN gl a ON (a.id = ac.trans_id)
276 AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
278 GROUP BY c.accno, c.description, c.category |;
280 if ($form->{project_id}) {
285 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
287 JOIN ar a ON (a.id = ac.trans_id)
288 JOIN parts p ON (ac.parts_id = p.id)
289 JOIN chart c on (p.income_accno_id = c.id)
291 -- use transdate from subwhere
292 WHERE (c.category = 'I')
299 JOIN chart ON (chart_id = id)
300 WHERE (link LIKE '%AR_paid%')
304 GROUP BY c.accno, c.description, c.category
308 SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
310 JOIN ap a ON (a.id = ac.trans_id)
311 JOIN parts p ON (ac.parts_id = p.id)
312 JOIN chart c on (p.expense_accno_id = c.id)
314 WHERE (c.category = 'E')
321 JOIN chart ON (chart_id = id)
322 WHERE link LIKE '%AP_paid%'
326 GROUP BY c.accno, c.description, c.category |;
329 } else { # if ($form->{method} eq 'cash')
330 if ($department_id) {
331 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
332 $dpt_where = qq| AND t.department_id = $department_id |;
336 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
338 JOIN chart c ON (c.id = ac.chart_id)
344 GROUP BY c.accno, c.description, c.category |;
346 if ($form->{project_id}) {
350 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
352 JOIN ar a ON (a.id = ac.trans_id)
353 JOIN parts p ON (ac.parts_id = p.id)
354 JOIN chart c on (p.income_accno_id = c.id)
356 -- use transdate from subwhere
357 WHERE (c.category = 'I')
361 GROUP BY c.accno, c.description, c.category
365 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
367 JOIN ap a ON (a.id = ac.trans_id)
368 JOIN parts p ON (ac.parts_id = p.id)
369 JOIN chart c on (p.expense_accno_id = c.id)
371 WHERE (c.category = 'E')
375 GROUP BY c.accno, c.description, c.category |;
383 $sth = prepare_execute_query($form, $dbh, $query);
385 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
387 if ($ref->{category} eq 'C') {
388 $ref->{category} = 'A';
391 # get last heading account
392 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
396 $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
398 $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
402 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
403 $form->{ $ref->{category} }{ $ref->{accno} }{description} = $ref->{description};
404 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
407 $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
409 $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
414 # remove accounts with zero balance
415 foreach $category (@{$categories}) {
416 foreach $accno (keys %{ $form->{$category} }) {
417 $form->{$category}{$accno}{last} = $form->round_amount($form->{$category}{$accno}{last}, $dec);
418 $form->{$category}{$accno}{this} = $form->round_amount($form->{$category}{$accno}{this}, $dec);
420 delete $form->{$category}{$accno}
421 if ( $form->{$category}{$accno}{this} == 0
422 && $form->{$category}{$accno}{last} == 0);
426 $main::lxdebug->leave_sub();
430 $main::lxdebug->enter_sub();
432 my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
434 my ($null, $department_id) = split /--/, $form->{department};
447 $fromdate = conv_dateq($fromdate);
448 if ($form->{method} eq 'cash') {
449 $subwhere .= " AND (transdate >= $fromdate)";
450 $glwhere = " AND (ac.transdate >= $fromdate)";
451 $prwhere = " AND (a.transdate >= $fromdate)";
453 $where .= " AND (ac.transdate >= $fromdate)";
458 $todate = conv_dateq($todate);
459 $subwhere .= " AND (transdate <= $todate)";
460 $where .= " AND (ac.transdate <= $todate)";
461 $prwhere .= " AND (a.transdate <= $todate)";
464 if ($department_id) {
465 $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
466 $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|) |;
469 if ($form->{project_id}) {
470 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
473 if ($form->{method} eq 'cash') {
476 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
478 JOIN chart c ON (c.id = ac.chart_id)
479 JOIN ar a ON (a.id = ac.trans_id)
481 WHERE $where $dpt_where
482 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AR_paid%') $subwhere)
488 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
490 JOIN chart c ON (c.id = ac.chart_id)
491 JOIN ap a ON (a.id = ac.trans_id)
493 WHERE $where $dpt_where
494 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AP_paid%') $subwhere)
500 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
502 JOIN chart c ON (c.id = ac.chart_id)
503 JOIN gl a ON (a.id = ac.trans_id)
505 WHERE $where $dpt_where $glwhere
506 AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
511 if ($form->{project_id}) {
515 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
517 JOIN ar a ON (a.id = ac.trans_id)
518 JOIN parts p ON (ac.parts_id = p.id)
519 JOIN chart c on (p.income_accno_id = c.id)
521 WHERE (c.category = 'I') $prwhere $dpt_where
522 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AR_paid%') $subwhere)
528 SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category
530 JOIN ap a ON (a.id = ac.trans_id)
531 JOIN parts p ON (ac.parts_id = p.id)
532 JOIN chart c on (p.expense_accno_id = c.id)
534 WHERE (c.category = 'E') $prwhere $dpt_where
535 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AP_paid%') $subwhere)
541 } else { # if ($form->{method} eq 'cash')
542 if ($department_id) {
543 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
544 $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
548 SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
550 JOIN chart c ON (c.id = ac.chart_id)
555 GROUP BY c.$category |;
557 if ($form->{project_id}) {
561 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
563 JOIN ar a ON (a.id = ac.trans_id)
564 JOIN parts p ON (ac.parts_id = p.id)
565 JOIN chart c on (p.income_accno_id = c.id)
567 WHERE (c.category = 'I')
575 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
577 JOIN ap a ON (a.id = ac.trans_id)
578 JOIN parts p ON (ac.parts_id = p.id)
579 JOIN chart c on (p.expense_accno_id = c.id)
581 WHERE (c.category = 'E')
585 GROUP BY c.$category |;
593 my $sth = prepare_execute_query($form, $dbh, $query);
595 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
596 if ($category eq "pos_bwa") {
598 $form->{ $ref->{$category} }{kumm} += $ref->{amount};
600 $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
603 $form->{ $ref->{$category} } += $ref->{amount};
608 $main::lxdebug->leave_sub();
612 $main::lxdebug->enter_sub();
614 my ($self, $myconfig, $form) = @_;
616 my $dbh = $form->dbconnect($myconfig);
618 my ($query, $sth, $ref);
621 my ($null, $department_id) = split /--/, $form->{department};
622 my @headingaccounts = ();
628 my $invwhere = $where;
630 if ($department_id) {
631 $dpt_join = qq| JOIN dpt_trans t ON (ac.trans_id = t.trans_id) |;
632 $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
635 # project_id only applies to getting transactions
636 # it has nothing to do with a trial balance
637 # but we use the same function to collect information
639 if ($form->{project_id}) {
640 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
643 my $acc_cash_where = "";
644 # my $ar_cash_where = "";
645 # my $ap_cash_where = "";
648 if ($form->{method} eq "cash") {
649 $acc_cash_where = qq| AND (ac.trans_id IN (SELECT id FROM ar WHERE datepaid>='$form->{fromdate}' AND datepaid<='$form->{todate}' UNION SELECT id FROM ap WHERE datepaid>='$form->{fromdate}' AND datepaid<='$form->{todate}' UNION SELECT id FROM gl WHERE transdate>='$form->{fromdate}' AND transdate<='$form->{todate}')) |;
650 # $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
653 # get beginning balances
655 qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
657 LEFT JOIN chart c ON (ac.chart_id = c.id)
659 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction $acc_cash_where
662 GROUP BY c.accno, c.category, c.description |;
664 $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
666 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
668 if ($ref->{amount} != 0 || $form->{all_accounts}) {
669 $trb{ $ref->{accno} }{description} = $ref->{description};
670 $trb{ $ref->{accno} }{charttype} = 'A';
672 if ($ref->{amount} > 0) {
673 $trb{ $ref->{accno} }{haben_eb} = $ref->{amount};
675 $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1;
677 $trb{ $ref->{accno} }{category} = $ref->{category};
686 qq|SELECT c.accno, c.description, c.category
688 WHERE c.charttype = 'H'
691 $sth = prepare_execute_query($form, $dbh, $query);
693 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
694 $trb{ $ref->{accno} }{description} = $ref->{description};
695 $trb{ $ref->{accno} }{charttype} = 'H';
696 $trb{ $ref->{accno} }{category} = $ref->{category};
698 push @headingaccounts, $ref->{accno};
704 my $saldowhere = " 1 = 1 ";
705 my $sumwhere = " 1 = 1 ";
707 my $sumsubwhere = '';
708 my $saldosubwhere = '';
709 my $glsaldowhere = '';
715 if ($form->{fromdate} || $form->{todate}) {
716 if ($form->{fromdate}) {
717 my $fromdate = conv_dateq($form->{fromdate});
718 $tofrom .= " AND (ac.transdate >= $fromdate)";
719 $subwhere .= " AND (transdate >= $fromdate)";
720 $sumsubwhere .= " AND (transdate >= (select date_trunc('year', date $fromdate))) ";
721 $saldosubwhere .= " AND transdate>=(select date_trunc('year', date $fromdate)) ";
722 $invwhere .= " AND (a.transdate >= $fromdate)";
723 $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
724 $glwhere = " AND (ac.transdate >= $fromdate)";
725 $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
727 if ($form->{todate}) {
728 my $todate = conv_dateq($form->{todate});
729 $tofrom .= " AND (ac.transdate <= $todate)";
730 $invwhere .= " AND (a.transdate <= $todate)";
731 $saldosubwhere .= " AND (transdate <= $todate)";
732 $sumsubwhere .= " AND (transdate <= $todate)";
733 $subwhere .= " AND (transdate <= $todate)";
734 $glwhere .= " AND (ac.transdate <= $todate)";
735 $glsumwhere .= " AND (ac.transdate <= $todate) ";
736 $glsaldowhere .= " AND (ac.transdate <= $todate) ";
740 if ($form->{method} eq "cash") {
742 qq| AND ((ac.trans_id IN (SELECT id from ar) AND
747 JOIN chart ON (chart_id = id)
748 WHERE (link LIKE '%AR_paid%')
753 (ac.trans_id in (SELECT id from ap) AND
758 JOIN chart ON (chart_id = id)
759 WHERE (link LIKE '%AP_paid%')
764 (ac.trans_id in (SELECT id from gl)
768 qq| AND ((ac.trans_id IN (SELECT id from ar) AND
773 JOIN chart ON (chart_id = id)
774 WHERE (link LIKE '%AR_paid%')
779 (ac.trans_id in (SELECT id from ap) AND
784 JOIN chart ON (chart_id = id)
785 WHERE (link LIKE '%AP_paid%')
790 (ac.trans_id in (SELECT id from gl)
794 qq| AND ((ac.trans_id IN (SELECT id from ar) AND
799 JOIN chart ON (chart_id = id)
800 WHERE (link LIKE '%AR_paid%')
805 (ac.trans_id in (SELECT id from ap) AND
810 JOIN chart ON (chart_id = id)
811 WHERE (link LIKE '%AP_paid%')
816 (ac.trans_id in (SELECT id from gl)
821 $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
822 $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
823 $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
827 SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
829 JOIN chart c ON (c.id = ac.chart_id)
834 GROUP BY c.accno, c.description, c.category |;
836 if ($form->{project_id}) {
838 -- add project transactions from invoice
842 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
844 JOIN ar a ON (ac.trans_id = a.id)
845 JOIN parts p ON (ac.parts_id = p.id)
846 JOIN chart c ON (p.income_accno_id = c.id)
851 GROUP BY c.accno, c.description, c.category
855 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
857 JOIN ap a ON (ac.trans_id = a.id)
858 JOIN parts p ON (ac.parts_id = p.id)
859 JOIN chart c ON (p.expense_accno_id = c.id)
864 GROUP BY c.accno, c.description, c.category
868 $query .= qq| ORDER BY accno|;
870 $sth = prepare_execute_query($form, $dbh, $query);
872 # calculate the debit and credit in the period
873 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
874 $trb{ $ref->{accno} }{description} = $ref->{description};
875 $trb{ $ref->{accno} }{charttype} = 'A';
876 $trb{ $ref->{accno} }{category} = $ref->{category};
877 $trb{ $ref->{accno} }{amount} += $ref->{amount};
881 # prepare query for each account
882 my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
886 (SELECT SUM(ac.amount) * -1
888 JOIN chart c ON (c.id = ac.chart_id)
894 AND (c.accno = ?)) AS debit,
896 (SELECT SUM(ac.amount)
898 JOIN chart c ON (c.id = ac.chart_id)
904 AND c.accno = ?) AS credit,
905 (SELECT SUM(ac.amount)
907 JOIN chart c ON (ac.chart_id = c.id)
912 AND c.accno = ?) AS saldo,
914 (SELECT SUM(ac.amount)
916 JOIN chart c ON (ac.chart_id = c.id)
922 AND c.accno = ?) AS sum_credit,
924 (SELECT SUM(ac.amount)
926 JOIN chart c ON (ac.chart_id = c.id)
932 AND c.accno = ?) AS sum_debit,
934 (SELECT max(ac.transdate) FROM acc_trans ac
935 JOIN chart c ON (ac.chart_id = c.id)
940 AND c.accno = ?) AS last_transaction
945 $drcr = prepare_query($form, $dbh, $q_drcr);
947 if ($form->{project_id}) {
948 # prepare query for each account
951 (SELECT SUM(ac.sellprice * ac.qty) * -1
953 JOIN parts p ON (ac.parts_id = p.id)
954 JOIN ap a ON (ac.trans_id = a.id)
955 JOIN chart c ON (p.expense_accno_id = c.id)
960 AND c.accno = ?) AS debit,
962 (SELECT SUM(ac.sellprice * ac.qty)
964 JOIN parts p ON (ac.parts_id = p.id)
965 JOIN ar a ON (ac.trans_id = a.id)
966 JOIN chart c ON (p.income_accno_id = c.id)
971 AND c.accno = ?) AS credit,
973 (SELECT SUM(ac.amount)
975 JOIN chart c ON (ac.chart_id = c.id)
980 AND c.accno = ?) AS saldo,
982 (SELECT SUM(ac.amount)
984 JOIN chart c ON (ac.chart_id = c.id)
990 AND c.accno = ?) AS sum_credit,
992 (SELECT SUM(ac.amount)
994 JOIN chart c ON (ac.chart_id = c.id)
1000 AND c.accno = ?) AS sum_debit,
1003 (SELECT max(ac.transdate) FROM acc_trans ac
1004 JOIN chart c ON (ac.chart_id = c.id)
1009 AND c.accno = ?) AS last_transaction
1012 $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
1016 my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
1018 foreach my $accno (sort keys %trb) {
1021 $ref->{accno} = $accno;
1022 map { $ref->{$_} = $trb{$accno}{$_} }
1023 qw(description category charttype amount soll_eb haben_eb);
1025 $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
1027 if ($trb{$accno}{charttype} eq 'A') {
1030 do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1032 ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
1033 my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
1034 $last_transaction = "";
1035 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
1036 $ref->{debit} += $debit;
1037 $ref->{credit} += $credit;
1039 $ref->{haben_saldo} += $saldo;
1041 $ref->{soll_saldo} += $saldo * -1;
1043 $ref->{last_transaction} = $last_transaction;
1044 $ref->{soll_kumuliert} = $soll_kumuliert * -1;
1045 $ref->{haben_kumuliert} = $haben_kumuliert;
1049 if ($form->{project_id}) {
1052 do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1054 ($debit, $credit) = (0, 0);
1055 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
1056 $ref->{debit} += $debit;
1057 $ref->{credit} += $credit;
1059 $ref->{haben_saldo} += $saldo;
1061 $ref->{soll_saldo} += $saldo * -1;
1063 $ref->{soll_kumuliert} += $soll_kumuliert * -1;
1064 $ref->{haben_kumuliert} += $haben_kumuliert;
1066 $project_drcr->finish;
1069 $ref->{debit} = $form->round_amount($ref->{debit}, 2);
1070 $ref->{credit} = $form->round_amount($ref->{credit}, 2);
1071 $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
1072 $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
1073 $ref->{haben_kumuliert} = $form->round_amount($ref->{haben_kumuliert}, 2);
1074 $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
1079 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1080 $accno = pop @accno;
1082 $trb{$accno}{debit} += $ref->{debit};
1083 $trb{$accno}{credit} += $ref->{credit};
1084 $trb{$accno}{soll_saldo} += $ref->{soll_saldo};
1085 $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
1086 $trb{$accno}{soll_kumuliert} += $ref->{soll_kumuliert};
1087 $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
1090 push @{ $form->{TB} }, $ref;
1096 # debits and credits for headings
1097 foreach my $accno (@headingaccounts) {
1098 foreach $ref (@{ $form->{TB} }) {
1099 if ($accno eq $ref->{accno}) {
1100 $ref->{debit} = $trb{$accno}{debit};
1101 $ref->{credit} = $trb{$accno}{credit};
1102 $ref->{soll_saldo} = $trb{$accno}{soll_saldo};
1103 $ref->{haben_saldo} = $trb{$accno}{haben_saldo};
1104 $ref->{soll_kumuliert} = $trb{$accno}{soll_kumuliert};
1105 $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert}; }
1109 $main::lxdebug->leave_sub();
1113 $main::lxdebug->enter_sub();
1114 my ($self, $dbh, $form) = @_;
1115 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
1116 my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
1117 my $sth = $dbh->prepare($query);
1118 while(my $ref = $sth->fetchrow_hashref()) {
1119 $ref->{invnumer} =~ s/Storno zu //g;
1120 $form->{storno}{$ref->{invnumber}} = 1;
1122 $main::lxdebug->leave_sub();
1126 $main::lxdebug->enter_sub();
1128 my ($self, $myconfig, $form) = @_;
1130 # connect to database
1131 my $dbh = $form->dbconnect($myconfig);
1133 my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
1135 if ($form->{ct} eq "customer") {
1148 $ct_id = "${ct}_id";
1150 $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
1151 my $todate = conv_dateq($form->{todate});
1152 my $fromdate = conv_dateq($form->{fromdate});
1154 my $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
1156 my $where = " 1 = 1 ";
1159 if ($form->{$ct_id}) {
1160 $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
1161 } elsif ($form->{ $form->{ct} }) {
1162 $where .= qq| AND (ct.name ILIKE | . $dbh->quote('%' . $form->{$ct} . '%') . qq|)|;
1166 if ($form->{department}) {
1167 my ($null, $department_id) = split /--/, $form->{department};
1168 $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
1169 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1173 -- between 0-30 days
1175 SELECT ${ct}.id AS ctid, ${ct}.name,
1176 street, zipcode, city, country, contact, email,
1177 phone as customerphone, fax as customerfax, ${ct}number,
1178 "invnumber", "transdate",
1179 (amount - COALESCE((SELECT sum(amount)*$ml FROM acc_trans LEFT JOIN chart ON (acc_trans.chart_id=chart.id) WHERE link ilike '%paid%' AND acc_trans.trans_id=${arap}.id AND acc_trans.transdate <= (date $todate)),0)) as "open", "amount",
1180 "duedate", invoice, ${arap}.id,
1183 WHERE (${arap}.curr = exchangerate.curr)
1184 AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
1186 WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
1187 AND (${arap}.storno IS FALSE)
1188 AND (${arap}.${ct}_id = ${ct}.id)
1190 AND (transdate <= (date $todate) $fromwhere )
1192 ORDER BY ctid, transdate, invnumber |;
1194 my $sth_details = prepare_query($form, $dbh, $q_details);
1196 # select outstanding vendors or customers, depends on $ct
1198 qq|SELECT DISTINCT ct.id, ct.name
1199 FROM $ct ct, $arap a
1202 AND (a.${ct_id} = ct.id)
1203 AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
1204 AND (a.transdate <= $todate $fromwhere)
1207 my $sth = prepare_execute_query($form, $dbh, $query);
1210 # for each company that has some stuff outstanding
1211 while (my ($id) = $sth->fetchrow_array) {
1212 do_statement($form, $sth_details, $q_details, $id);
1214 while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
1215 $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
1216 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
1217 push @{ $form->{AG} }, $ref;
1220 $sth_details->finish;
1229 $main::lxdebug->leave_sub();
1233 $main::lxdebug->enter_sub();
1235 my ($self, $myconfig, $form) = @_;
1237 # connect to database
1238 my $dbh = $form->dbconnect($myconfig);
1240 my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
1243 qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
1246 ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
1247 selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
1250 $main::lxdebug->leave_sub();
1253 sub get_taxaccounts {
1254 $main::lxdebug->enter_sub();
1256 my ($self, $myconfig, $form) = @_;
1258 # connect to database
1259 my $dbh = $form->dbconnect($myconfig);
1263 qq|SELECT c.accno, c.description, t.rate
1265 WHERE (c.link LIKE '%CT_tax%') AND (c.id = t.chart_id)
1267 $form->{taxaccounts} = selectall_hashref_quert($form, $dbh, $query);
1271 $main::lxdebug->leave_sub();
1275 $main::lxdebug->enter_sub();
1277 my ($self, $myconfig, $form) = @_;
1279 # connect to database
1280 my $dbh = $form->dbconnect($myconfig);
1282 my ($null, $department_id) = split /--/, $form->{department};
1285 my $where = "1 = 1";
1287 if ($department_id) {
1288 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
1293 if ($form->{accno}) {
1294 $accno = $form->{accno};
1295 $rate = $form->{"$form->{accno}_rate"};
1296 $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
1302 if ($form->{db} eq 'ar') {
1303 $table = "customer";
1310 my $arap = lc($ARAP);
1312 my $transdate = "a.transdate";
1314 if ($form->{method} eq 'cash') {
1315 $transdate = "a.datepaid";
1317 my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
1324 JOIN chart ON (chart_id = id)
1325 WHERE (link LIKE '%${ARAP}_paid%')
1326 AND (transdate <= $todate)
1331 # if there are any dates construct a where
1332 $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
1333 $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
1335 my $ml = ($form->{db} eq 'ar') ? 1 : -1;
1337 my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
1338 $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
1341 if ($form->{report} !~ /nontaxable/) {
1343 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
1344 ac.amount * $ml AS tax
1346 JOIN ${arap} a ON (a.id = ac.trans_id)
1347 JOIN chart ch ON (ch.id = ac.chart_id)
1348 JOIN $table n ON (n.id = a.${table}_id)
1352 AND (a.invoice = '0')
1356 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
1357 i.sellprice * i.qty * $rate * $ml AS tax
1359 JOIN ${arap} a ON (a.id = ac.trans_id)
1360 JOIN chart ch ON (ch.id = ac.chart_id)
1361 JOIN $table n ON (n.id = a.${table}_id)
1362 JOIN ${table}tax t ON (t.${table}_id = n.id)
1363 JOIN invoice i ON (i.trans_id = a.id)
1364 JOIN partstax p ON (p.parts_id = i.parts_id)
1368 AND (a.invoice = '1')
1369 ORDER BY $sortorder|;
1371 # only gather up non-taxable transactions
1373 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount
1375 JOIN ${arap} a ON (a.id = ac.trans_id)
1376 JOIN $table n ON (n.id = a.${table}_id)
1379 AND (a.invoice = '0')
1380 AND (a.netamount = a.amount)
1384 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount
1386 JOIN ${arap} a ON (a.id = ac.trans_id)
1387 JOIN $table n ON (n.id = a.${table}_id)
1388 JOIN invoice i ON (i.trans_id = a.id)
1391 AND (a.invoice = '1')
1393 a.${table}_id NOT IN (SELECT ${table}_id FROM ${table}tax t (${table}_id))
1395 i.parts_id NOT IN (SELECT parts_id FROM partstax p (parts_id))
1397 GROUP BY a.id, a.invnumber, $transdate, n.name, i.sellprice, i.qty
1398 ORDER by $sortorder|;
1401 $form->{TR} = selectall_hashref_query($form, $dbh, $query);
1405 $main::lxdebug->leave_sub();
1408 sub paymentaccounts {
1409 $main::lxdebug->enter_sub();
1411 my ($self, $myconfig, $form) = @_;
1413 # connect to database, turn AutoCommit off
1414 my $dbh = $form->dbconnect_noauto($myconfig);
1416 my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
1418 # get A(R|P)_paid accounts
1420 qq|SELECT accno, description
1422 WHERE link LIKE '%${ARAP}_paid%'|;
1423 $form->{PR} = selectall_hashref_query($form, $dbh, $query);
1427 $main::lxdebug->leave_sub();
1431 $main::lxdebug->enter_sub();
1433 my ($self, $myconfig, $form) = @_;
1435 # connect to database, turn AutoCommit off
1436 my $dbh = $form->dbconnect_noauto($myconfig);
1441 if ($form->{db} eq 'ar') {
1442 $table = 'customer';
1454 if ($form->{department_id}) {
1455 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
1456 $where = qq| AND (t.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
1459 if ($form->{fromdate}) {
1460 $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
1462 if ($form->{todate}) {
1463 $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
1465 if (!$form->{fx_transaction}) {
1466 $where .= " AND ac.fx_transaction = '0'";
1471 if ($form->{reference}) {
1472 $reference = $dbh->quote('%' . $form->{reference} . '%');
1473 $invnumber = " AND (a.invnumber LIKE $reference)";
1474 $reference = " AND (g.reference LIKE $reference)";
1476 if ($form->{source}) {
1477 $where .= " AND (ac.source ILIKE " . $dbh->quote('%' . $form->{source} . '%') . ") ";
1479 if ($form->{memo}) {
1480 $where .= " AND (ac.memo ILIKE " . $dbh->quote('%' . $form->{memo} . '%') . ") ";
1483 my %sort_columns = (
1484 'transdate' => [ qw(transdate lower_invnumber lower_name) ],
1485 'invnumber' => [ qw(lower_invnumber lower_name transdate) ],
1486 'name' => [ qw(lower_name transdate) ],
1487 'source' => [ qw(lower_source) ],
1488 'memo' => [ qw(lower_memo) ],
1490 my %lowered_columns = (
1491 'invnumber' => { 'gl' => 'g.reference', 'arap' => 'a.invnumber', },
1492 'memo' => { 'gl' => 'ac.memo', 'arap' => 'ac.memo', },
1493 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
1494 'name' => { 'gl' => 'g.description', 'arap' => 'c.name', },
1497 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
1498 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
1499 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
1502 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
1503 foreach my $spec (@{ $sort_columns{$sortkey} }) {
1504 next if ($spec !~ m/^lower_(.*)$/);
1507 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
1510 $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
1511 $sth = prepare_query($form, $dbh, $query);
1514 qq|SELECT c.name, a.invnumber, a.ordnumber,
1515 ac.transdate, ac.amount * $ml AS paid, ac.source,
1516 a.invoice, a.id, ac.memo, '${arap}' AS module
1517 $columns_for_sorting{arap}
1519 JOIN $arap a ON (ac.trans_id = a.id)
1520 JOIN $table c ON (c.id = a.${table}_id)
1522 WHERE (ac.chart_id = ?)
1528 SELECT g.description, g.reference, NULL AS ordnumber,
1529 ac.transdate, ac.amount * $ml AS paid, ac.source,
1530 '0' as invoice, g.id, ac.memo, 'gl' AS module
1531 $columns_for_sorting{gl}
1533 JOIN gl g ON (g.id = ac.trans_id)
1535 WHERE (ac.chart_id = ?)
1538 AND (ac.amount * $ml) > 0
1540 ORDER BY $sortorder|;
1541 my $sth_details = prepare_query($form, $dbh, $q_details);
1545 # cycle through each id
1546 foreach my $accno (split(/ /, $form->{paymentaccounts})) {
1547 do_statement($form, $sth, $query, $accno);
1548 my $ref = $sth->fetchrow_hashref();
1549 push(@{ $form->{PR} }, $ref);
1552 $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
1554 do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
1555 while (my $pr = $sth_details->fetchrow_hashref()) {
1556 push(@{ $form->{ $ref->{id} } }, $pr);
1558 $sth_details->finish();
1563 $main::lxdebug->leave_sub();
1567 $main::lxdebug->enter_sub();
1569 my ($self, $myconfig, $form) = @_;
1571 # connect to database
1572 my $dbh = $form->dbconnect($myconfig);
1574 my $last_period = 0;
1577 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);
1579 $form->{decimalplaces} *= 1;
1581 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
1583 # if there are any compare dates
1585 if ($form->{fromdate} || $form->{todate}) {
1587 if ($form->{fromdate}) {
1588 $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1591 $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1594 my $kummfromdate = $form->{comparefromdate};
1595 my $kummtodate = $form->{comparetodate};
1596 &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
1599 my @periods = qw(jetzt kumm);
1600 my @gesamtleistung = qw(1 2 3);
1601 my @gesamtkosten = qw (10 11 12 13 14 15 16 17 18 19 20);
1603 qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
1605 foreach my $key (@periods) {
1606 $form->{ "$key" . "gesamtleistung" } = 0;
1607 $form->{ "$key" . "gesamtkosten" } = 0;
1609 foreach $category (@categories) {
1611 if (defined($form->{$category}{$key})) {
1612 $form->{"$key$category"} =
1613 $form->format_amount($myconfig,
1614 $form->round_amount($form->{$category}{$key}, 2
1616 $form->{decimalplaces},
1620 foreach my $item (@gesamtleistung) {
1621 $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
1623 foreach my $item (@gesamtkosten) {
1624 $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
1626 $form->{ "$key" . "rohertrag" } =
1627 $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
1628 $form->{ "$key" . "betriebrohertrag" } =
1629 $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
1630 $form->{ "$key" . "betriebsergebnis" } =
1631 $form->{ "$key" . "betriebrohertrag" } -
1632 $form->{ "$key" . "gesamtkosten" };
1633 $form->{ "$key" . "neutraleraufwand" } =
1634 $form->{30}{$key} + $form->{31}{$key};
1635 $form->{ "$key" . "neutralertrag" } =
1636 $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
1637 $form->{ "$key" . "ergebnisvorsteuern" } =
1638 $form->{ "$key" . "betriebsergebnis" } -
1639 $form->{ "$key" . "neutraleraufwand" } +
1640 $form->{ "$key" . "neutralertrag" };
1641 $form->{ "$key" . "ergebnis" } =
1642 $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
1644 if ($form->{ "$key" . "gesamtleistung" } > 0) {
1645 foreach $category (@categories) {
1646 if (defined($form->{$category}{$key})) {
1647 $form->{ "$key" . "gl" . "$category" } =
1648 $form->format_amount(
1650 $form->round_amount(
1651 ($form->{$category}{$key} /
1652 $form->{ "$key" . "gesamtleistung" } * 100
1654 $form->{decimalplaces}
1656 $form->{decimalplaces},
1660 foreach my $item (@ergebnisse) {
1661 $form->{ "$key" . "gl" . "$item" } =
1662 $form->format_amount($myconfig,
1663 $form->round_amount(
1664 ( $form->{ "$key" . "$item" } /
1665 $form->{ "$key" . "gesamtleistung" } * 100
1667 $form->{decimalplaces}
1669 $form->{decimalplaces},
1674 if ($form->{ "$key" . "gesamtkosten" } > 0) {
1675 foreach $category (@categories) {
1676 if (defined($form->{$category}{$key})) {
1677 $form->{ "$key" . "gk" . "$category" } =
1678 $form->format_amount($myconfig,
1679 $form->round_amount(
1680 ($form->{$category}{$key} /
1681 $form->{ "$key" . "gesamtkosten" } * 100
1683 $form->{decimalplaces}
1685 $form->{decimalplaces},
1689 foreach my $item (@ergebnisse) {
1690 $form->{ "$key" . "gk" . "$item" } =
1691 $form->format_amount($myconfig,
1692 $form->round_amount(
1693 ($form->{ "$key" . "$item" } /
1694 $form->{ "$key" . "gesamtkosten" } * 100
1696 $form->{decimalplaces}
1698 $form->{decimalplaces},
1703 if ($form->{10}{$key} > 0) {
1704 foreach $category (@categories) {
1705 if (defined($form->{$category}{$key})) {
1706 $form->{ "$key" . "pk" . "$category" } =
1707 $form->format_amount(
1709 $form->round_amount(
1710 ($form->{$category}{$key} / $form->{10}{$key} * 100),
1711 $form->{decimalplaces}
1713 $form->{decimalplaces},
1717 foreach my $item (@ergebnisse) {
1718 $form->{ "$key" . "pk" . "$item" } =
1719 $form->format_amount($myconfig,
1720 $form->round_amount(
1721 ($form->{ "$key" . "$item" } /
1722 $form->{10}{$key} * 100
1724 $form->{decimalplaces}
1726 $form->{decimalplaces},
1731 if ($form->{4}{$key} > 0) {
1732 foreach $category (@categories) {
1733 if (defined($form->{$category}{$key})) {
1734 $form->{ "$key" . "auf" . "$category" } =
1735 $form->format_amount(
1737 $form->round_amount(
1738 ($form->{$category}{$key} / $form->{4}{$key} * 100),
1739 $form->{decimalplaces}
1741 $form->{decimalplaces},
1745 foreach my $item (@ergebnisse) {
1746 $form->{ "$key" . "auf" . "$item" } =
1747 $form->format_amount($myconfig,
1748 $form->round_amount(
1749 ($form->{ "$key" . "$item" } /
1750 $form->{4}{$key} * 100
1752 $form->{decimalplaces}
1754 $form->{decimalplaces},
1759 foreach my $item (@ergebnisse) {
1760 $form->{ "$key" . "$item" } =
1761 $form->format_amount($myconfig,
1762 $form->round_amount($form->{ "$key" . "$item" },
1763 $form->{decimalplaces}
1765 $form->{decimalplaces},
1772 $main::lxdebug->leave_sub();
1776 $main::lxdebug->enter_sub();
1778 my ($self, $myconfig, $form) = @_;
1780 # connect to database
1781 my $dbh = $form->dbconnect($myconfig);
1783 my $last_period = 0;
1784 my @categories_cent = qw(51r 511 86r 861 97r 971 93r 931
1785 96 66 43 45 53 62 65 67);
1786 my @categories_euro = qw(48 51 86 91 97 93 94);
1787 $form->{decimalplaces} *= 1;
1789 foreach my $item (@categories_cent) {
1790 $form->{"$item"} = 0;
1792 foreach my $item (@categories_euro) {
1793 $form->{"$item"} = 0;
1796 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_ustva");
1798 # foreach $item (@categories_cent) {
1799 # if ($form->{$item}{"jetzt"} > 0) {
1800 # $form->{$item} = $form->{$item}{"jetzt"};
1801 # delete $form->{$item}{"jetzt"};
1804 # foreach $item (@categories_euro) {
1805 # if ($form->{$item}{"jetzt"} > 0) {
1806 # $form->{$item} = $form->{$item}{"jetzt"};
1807 # delete $form->{$item}{"jetzt"};
1808 # } foreach $item (@categories_cent) {
1809 # if ($form->{$item}{"jetzt"} > 0) {
1810 # $form->{$item} = $form->{$item}{"jetzt"};
1811 # delete $form->{$item}{"jetzt"};
1814 # foreach $item (@categories_euro) {
1815 # if ($form->{$item}{"jetzt"} > 0) {
1816 # $form->{$item} = $form->{$item}{"jetzt"};
1817 # delete $form->{$item}{"jetzt"};
1824 # Berechnung der USTVA Formularfelder
1826 $form->{"51r"} = $form->{"511"};
1827 $form->{"86r"} = $form->{"861"};
1828 $form->{"97r"} = $form->{"971"};
1829 $form->{"93r"} = $form->{"931"};
1831 #$form->{"96"} = $form->{"94"} * 0.16;
1833 $form->{"51r"} + $form->{"86r"} + $form->{"97r"} + $form->{"93r"} +
1835 $form->{"45"} = $form->{"43"};
1836 $form->{"53"} = $form->{"43"};
1837 $form->{"62"} = $form->{"43"} - $form->{"66"};
1838 $form->{"65"} = $form->{"43"} - $form->{"66"};
1839 $form->{"67"} = $form->{"43"} - $form->{"66"};
1841 foreach my $item (@categories_cent) {
1843 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),
1847 foreach my $item (@categories_euro) {
1849 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0),
1855 $main::lxdebug->leave_sub();
1858 sub income_statement {
1859 $main::lxdebug->enter_sub();
1861 my ($self, $myconfig, $form) = @_;
1863 # connect to database
1864 my $dbh = $form->dbconnect($myconfig);
1866 my $last_period = 0;
1867 my @categories_einnahmen = qw(1 2 3 4 5 6 7);
1868 my @categories_ausgaben =
1869 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);
1871 my @ergebnisse = qw(sumeura sumeurb guvsumme);
1873 $form->{decimalplaces} *= 1;
1875 foreach my $item (@categories_einnahmen) {
1878 foreach my $item (@categories_ausgaben) {
1882 foreach my $item (@ergebnisse) {
1886 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
1889 foreach my $item (@categories_einnahmen) {
1890 $form->{"eur${item}"} =
1891 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
1892 $form->{"sumeura"} += $form->{$item};
1894 foreach my $item (@categories_ausgaben) {
1895 $form->{"eur${item}"} =
1896 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
1897 $form->{"sumeurb"} += $form->{$item};
1900 $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
1902 foreach my $item (@ergebnisse) {
1904 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
1906 $main::lxdebug->leave_sub();