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 $form->{decimalplaces} *= 1;
70 my $dec = $form->{decimalplaces};
72 get_accounts($dbh, $last_period, "", $form->{asofdate}, $form, \@categories);
74 # if there are any compare dates
75 if ($form->{compareasofdate}) {
77 get_accounts($dbh, $last_period, "", $form->{compareasofdate}, $form, \@categories);
78 $form->{last_period} = conv_dateq($form->{compareasofdate});
81 # now we got $form->{A}{accno}{ } assets
82 # and $form->{L}{accno}{ } liabilities
83 # and $form->{Q}{accno}{ } equity
84 # build asset accounts
86 my %account = ('A' => { 'ml' => -1 },
88 'Q' => { 'ml' => 1 });
92 foreach my $category (grep { !/C/ } @categories) {
94 $TMPL_DATA->{$category} = [];
95 my $ml = $account{$category}{ml};
97 foreach my $key (sort keys %{ $form->{$category} }) {
99 my $row = { %{ $form->{$category}{$key} } };
101 # if charttype "heading" - calculate this entry, start a new batch of charts belonging to this heading and skip the rest bo the loop
102 # header charts are not real charts. start a sub aggregation with them, but don't calculate anything with them
103 if ($row->{charttype} eq "H") {
104 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
105 $row->{subdescription} = $account{$category}{subdescription};
106 $row->{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
107 $row->{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
110 $row->{subheader} = 1;
111 $account{$category}{subthis} = $row->{this};
112 $account{$category}{sublast} = $row->{last};
113 $account{$category}{subdescription} = $row->{description};
114 $account{$category}{subtotal} = 1;
119 next unless $form->{l_heading};
122 for my $period (qw(this last)) {
123 next if ($period eq 'last' && !$last_period);
125 $row->{$period} *= $ml;
126 $form->{total}{$category}{$period} += $row->{$period}; # if ($row->{charttype} eq 'A') { # why??
129 push @{ $TMPL_DATA->{$category} }, $row;
132 # resolve heading/subtotal
133 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
134 $TMPL_DATA->{$category}[-1]{subdescription} = $account{$category}{subdescription};
135 $TMPL_DATA->{$category}[-1]{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
136 $TMPL_DATA->{$category}[-1]{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
139 $TMPL_DATA->{total}{$category}{this} = sum map { $_->{this} } @{ $TMPL_DATA->{$category} };
140 $TMPL_DATA->{total}{$category}{last} = sum map { $_->{last} } @{ $TMPL_DATA->{$category} };
143 for my $period (qw(this last)) {
144 next if ($period eq 'last' && !$last_period);
146 $form->{E}{$period} = $form->{total}{A}{$period} - $form->{total}{L}{$period} - $form->{total}{Q}{$period};
147 $form->{total}{Q}{$period} += $form->{E}{$period};
148 $TMPL_DATA->{total}{Q}{$period} = $form->{total}{Q}{$period};
149 $TMPL_DATA->{total}{$period} = $form->{total}{L}{$period} + $form->{total}{Q}{$period};
152 push @{ $TMPL_DATA->{Q} }, $form->{E};
154 $main::lxdebug->leave_sub();
160 $main::lxdebug->enter_sub();
162 my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
164 my ($null, $department_id) = split /--/, $form->{department};
175 my $dec = $form->{decimalplaces};
177 my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |;
181 qq|SELECT c.accno, c.description, c.category
183 WHERE (c.charttype = 'H')
187 $sth = prepare_execute_query($form, $dbh, $query);
189 my @headingaccounts = ();
190 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
191 $form->{ $ref->{category} }{ $ref->{accno} }{description} =
192 "$ref->{description}";
193 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
194 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
196 push @headingaccounts, $ref->{accno};
202 $fromdate = conv_dateq($fromdate);
203 if ($form->{method} eq 'cash') {
204 $subwhere .= " AND (transdate >= $fromdate)";
205 $glwhere = " AND (ac.transdate >= $fromdate)";
207 $where .= " AND (ac.transdate >= $fromdate)";
212 $todate = conv_dateq($todate);
213 $where .= " AND (ac.transdate <= $todate)";
214 $subwhere .= " AND (transdate <= $todate)";
217 if ($department_id) {
218 $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
219 $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|)|;
222 if ($form->{project_id}) {
223 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
226 if ($form->{method} eq 'cash') {
228 qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
230 JOIN chart c ON (c.id = ac.chart_id)
231 JOIN ar a ON (a.id = ac.trans_id)
240 JOIN chart ON (chart_id = id)
241 WHERE (link LIKE '%AR_paid%')
245 GROUP BY c.accno, c.description, c.category
249 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
251 JOIN chart c ON (c.id = ac.chart_id)
252 JOIN ap a ON (a.id = ac.trans_id)
261 JOIN chart ON (chart_id = id)
262 WHERE (link LIKE '%AP_paid%')
266 GROUP BY c.accno, c.description, c.category
270 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
272 JOIN chart c ON (c.id = ac.chart_id)
273 JOIN gl a ON (a.id = ac.trans_id)
279 AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
281 GROUP BY c.accno, c.description, c.category |;
283 if ($form->{project_id}) {
288 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
290 JOIN ar a ON (a.id = ac.trans_id)
291 JOIN parts p ON (ac.parts_id = p.id)
292 JOIN chart c on (p.income_accno_id = c.id)
294 -- use transdate from subwhere
295 WHERE (c.category = 'I')
302 JOIN chart ON (chart_id = id)
303 WHERE (link LIKE '%AR_paid%')
307 GROUP BY c.accno, c.description, c.category
311 SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
313 JOIN ap a ON (a.id = ac.trans_id)
314 JOIN parts p ON (ac.parts_id = p.id)
315 JOIN chart c on (p.expense_accno_id = c.id)
317 WHERE (c.category = 'E')
324 JOIN chart ON (chart_id = id)
325 WHERE link LIKE '%AP_paid%'
329 GROUP BY c.accno, c.description, c.category |;
332 } else { # if ($form->{method} eq 'cash')
333 if ($department_id) {
334 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
335 $dpt_where = qq| AND t.department_id = $department_id |;
339 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
341 JOIN chart c ON (c.id = ac.chart_id)
347 GROUP BY c.accno, c.description, c.category |;
349 if ($form->{project_id}) {
353 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
355 JOIN ar a ON (a.id = ac.trans_id)
356 JOIN parts p ON (ac.parts_id = p.id)
357 JOIN chart c on (p.income_accno_id = c.id)
359 -- use transdate from subwhere
360 WHERE (c.category = 'I')
364 GROUP BY c.accno, c.description, c.category
368 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
370 JOIN ap a ON (a.id = ac.trans_id)
371 JOIN parts p ON (ac.parts_id = p.id)
372 JOIN chart c on (p.expense_accno_id = c.id)
374 WHERE (c.category = 'E')
378 GROUP BY c.accno, c.description, c.category |;
386 $sth = prepare_execute_query($form, $dbh, $query);
388 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
390 if ($ref->{category} eq 'C') {
391 $ref->{category} = 'A';
394 # get last heading account
395 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
399 $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
401 $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
405 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
406 $form->{ $ref->{category} }{ $ref->{accno} }{description} = $ref->{description};
407 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
410 $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
412 $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
417 # remove accounts with zero balance
418 foreach $category (@{$categories}) {
419 foreach $accno (keys %{ $form->{$category} }) {
420 $form->{$category}{$accno}{last} = $form->round_amount($form->{$category}{$accno}{last}, $dec);
421 $form->{$category}{$accno}{this} = $form->round_amount($form->{$category}{$accno}{this}, $dec);
423 delete $form->{$category}{$accno}
424 if ( $form->{$category}{$accno}{this} == 0
425 && $form->{$category}{$accno}{last} == 0);
429 $main::lxdebug->leave_sub();
433 $main::lxdebug->enter_sub();
435 my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
437 my ($null, $department_id) = split /--/, $form->{department};
450 $fromdate = conv_dateq($fromdate);
451 if ($form->{method} eq 'cash') {
452 $subwhere .= " AND (transdate >= $fromdate)";
453 $glwhere = " AND (ac.transdate >= $fromdate)";
454 $prwhere = " AND (a.transdate >= $fromdate)";
456 $where .= " AND (ac.transdate >= $fromdate)";
461 $todate = conv_dateq($todate);
462 $subwhere .= " AND (transdate <= $todate)";
463 $where .= " AND (ac.transdate <= $todate)";
464 $prwhere .= " AND (a.transdate <= $todate)";
467 if ($department_id) {
468 $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
469 $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|) |;
472 if ($form->{project_id}) {
473 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
476 if ($form->{method} eq 'cash') {
479 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
481 JOIN chart c ON (c.id = ac.chart_id)
482 JOIN ar a ON (a.id = ac.trans_id)
484 WHERE $where $dpt_where
485 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AR_paid%') $subwhere)
491 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
493 JOIN chart c ON (c.id = ac.chart_id)
494 JOIN ap a ON (a.id = ac.trans_id)
496 WHERE $where $dpt_where
497 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AP_paid%') $subwhere)
503 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
505 JOIN chart c ON (c.id = ac.chart_id)
506 JOIN gl a ON (a.id = ac.trans_id)
508 WHERE $where $dpt_where $glwhere
509 AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
514 if ($form->{project_id}) {
518 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
520 JOIN ar a ON (a.id = ac.trans_id)
521 JOIN parts p ON (ac.parts_id = p.id)
522 JOIN chart c on (p.income_accno_id = c.id)
524 WHERE (c.category = 'I') $prwhere $dpt_where
525 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AR_paid%') $subwhere)
531 SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category
533 JOIN ap a ON (a.id = ac.trans_id)
534 JOIN parts p ON (ac.parts_id = p.id)
535 JOIN chart c on (p.expense_accno_id = c.id)
537 WHERE (c.category = 'E') $prwhere $dpt_where
538 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AP_paid%') $subwhere)
544 } else { # if ($form->{method} eq 'cash')
545 if ($department_id) {
546 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
547 $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
551 SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
553 JOIN chart c ON (c.id = ac.chart_id)
558 GROUP BY c.$category |;
560 if ($form->{project_id}) {
564 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
566 JOIN ar a ON (a.id = ac.trans_id)
567 JOIN parts p ON (ac.parts_id = p.id)
568 JOIN chart c on (p.income_accno_id = c.id)
570 WHERE (c.category = 'I')
578 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
580 JOIN ap a ON (a.id = ac.trans_id)
581 JOIN parts p ON (ac.parts_id = p.id)
582 JOIN chart c on (p.expense_accno_id = c.id)
584 WHERE (c.category = 'E')
588 GROUP BY c.$category |;
596 my $sth = prepare_execute_query($form, $dbh, $query);
598 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
599 if ($category eq "pos_bwa") {
601 $form->{ $ref->{$category} }{kumm} += $ref->{amount};
603 $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
606 $form->{ $ref->{$category} } += $ref->{amount};
611 $main::lxdebug->leave_sub();
615 $main::lxdebug->enter_sub();
617 my ($self, $myconfig, $form) = @_;
619 my $dbh = $form->dbconnect($myconfig);
621 my ($query, $sth, $ref);
624 my ($null, $department_id) = split /--/, $form->{department};
625 my @headingaccounts = ();
631 my $invwhere = $where;
633 if ($department_id) {
634 $dpt_join = qq| JOIN dpt_trans t ON (ac.trans_id = t.trans_id) |;
635 $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
638 # project_id only applies to getting transactions
639 # it has nothing to do with a trial balance
640 # but we use the same function to collect information
642 if ($form->{project_id}) {
643 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
646 my $acc_cash_where = "";
647 # my $ar_cash_where = "";
648 # my $ap_cash_where = "";
651 if ($form->{method} eq "cash") {
652 $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}')) |;
653 # $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
656 # get beginning balances
658 qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
660 LEFT JOIN chart c ON (ac.chart_id = c.id)
662 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction $acc_cash_where
665 GROUP BY c.accno, c.category, c.description |;
667 $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
669 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
671 if ($ref->{amount} != 0 || $form->{all_accounts}) {
672 $trb{ $ref->{accno} }{description} = $ref->{description};
673 $trb{ $ref->{accno} }{charttype} = 'A';
675 if ($ref->{amount} > 0) {
676 $trb{ $ref->{accno} }{haben_eb} = $ref->{amount};
678 $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1;
680 $trb{ $ref->{accno} }{category} = $ref->{category};
689 qq|SELECT c.accno, c.description, c.category
691 WHERE c.charttype = 'H'
694 $sth = prepare_execute_query($form, $dbh, $query);
696 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
697 $trb{ $ref->{accno} }{description} = $ref->{description};
698 $trb{ $ref->{accno} }{charttype} = 'H';
699 $trb{ $ref->{accno} }{category} = $ref->{category};
701 push @headingaccounts, $ref->{accno};
707 my $saldowhere = " 1 = 1 ";
708 my $sumwhere = " 1 = 1 ";
710 my $sumsubwhere = '';
711 my $saldosubwhere = '';
712 my $glsaldowhere = '';
718 if ($form->{fromdate} || $form->{todate}) {
719 if ($form->{fromdate}) {
720 my $fromdate = conv_dateq($form->{fromdate});
721 $tofrom .= " AND (ac.transdate >= $fromdate)";
722 $subwhere .= " AND (transdate >= $fromdate)";
723 $sumsubwhere .= " AND (transdate >= (select date_trunc('year', date $fromdate))) ";
724 $saldosubwhere .= " AND transdate>=(select date_trunc('year', date $fromdate)) ";
725 $invwhere .= " AND (a.transdate >= $fromdate)";
726 $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
727 $glwhere = " AND (ac.transdate >= $fromdate)";
728 $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
730 if ($form->{todate}) {
731 my $todate = conv_dateq($form->{todate});
732 $tofrom .= " AND (ac.transdate <= $todate)";
733 $invwhere .= " AND (a.transdate <= $todate)";
734 $saldosubwhere .= " AND (transdate <= $todate)";
735 $sumsubwhere .= " AND (transdate <= $todate)";
736 $subwhere .= " AND (transdate <= $todate)";
737 $glwhere .= " AND (ac.transdate <= $todate)";
738 $glsumwhere .= " AND (ac.transdate <= $todate) ";
739 $glsaldowhere .= " AND (ac.transdate <= $todate) ";
743 if ($form->{method} eq "cash") {
745 qq| AND ((ac.trans_id IN (SELECT id from ar) AND
750 JOIN chart ON (chart_id = id)
751 WHERE (link LIKE '%AR_paid%')
756 (ac.trans_id in (SELECT id from ap) AND
761 JOIN chart ON (chart_id = id)
762 WHERE (link LIKE '%AP_paid%')
767 (ac.trans_id in (SELECT id from gl)
771 qq| AND ((ac.trans_id IN (SELECT id from ar) AND
776 JOIN chart ON (chart_id = id)
777 WHERE (link LIKE '%AR_paid%')
782 (ac.trans_id in (SELECT id from ap) AND
787 JOIN chart ON (chart_id = id)
788 WHERE (link LIKE '%AP_paid%')
793 (ac.trans_id in (SELECT id from gl)
797 qq| AND ((ac.trans_id IN (SELECT id from ar) AND
802 JOIN chart ON (chart_id = id)
803 WHERE (link LIKE '%AR_paid%')
808 (ac.trans_id in (SELECT id from ap) AND
813 JOIN chart ON (chart_id = id)
814 WHERE (link LIKE '%AP_paid%')
819 (ac.trans_id in (SELECT id from gl)
824 $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
825 $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
826 $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
830 SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
832 JOIN chart c ON (c.id = ac.chart_id)
837 GROUP BY c.accno, c.description, c.category |;
839 if ($form->{project_id}) {
841 -- add project transactions from invoice
845 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
847 JOIN ar a ON (ac.trans_id = a.id)
848 JOIN parts p ON (ac.parts_id = p.id)
849 JOIN chart c ON (p.income_accno_id = c.id)
854 GROUP BY c.accno, c.description, c.category
858 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
860 JOIN ap a ON (ac.trans_id = a.id)
861 JOIN parts p ON (ac.parts_id = p.id)
862 JOIN chart c ON (p.expense_accno_id = c.id)
867 GROUP BY c.accno, c.description, c.category
871 $query .= qq| ORDER BY accno|;
873 $sth = prepare_execute_query($form, $dbh, $query);
875 # calculate the debit and credit in the period
876 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
877 $trb{ $ref->{accno} }{description} = $ref->{description};
878 $trb{ $ref->{accno} }{charttype} = 'A';
879 $trb{ $ref->{accno} }{category} = $ref->{category};
880 $trb{ $ref->{accno} }{amount} += $ref->{amount};
884 # prepare query for each account
885 my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
889 (SELECT SUM(ac.amount) * -1
891 JOIN chart c ON (c.id = ac.chart_id)
897 AND (c.accno = ?)) AS debit,
899 (SELECT SUM(ac.amount)
901 JOIN chart c ON (c.id = ac.chart_id)
907 AND c.accno = ?) AS credit,
908 (SELECT SUM(ac.amount)
910 JOIN chart c ON (ac.chart_id = c.id)
915 AND c.accno = ?) AS saldo,
917 (SELECT SUM(ac.amount)
919 JOIN chart c ON (ac.chart_id = c.id)
925 AND c.accno = ?) AS sum_credit,
927 (SELECT SUM(ac.amount)
929 JOIN chart c ON (ac.chart_id = c.id)
935 AND c.accno = ?) AS sum_debit,
937 (SELECT max(ac.transdate) FROM acc_trans ac
938 JOIN chart c ON (ac.chart_id = c.id)
943 AND c.accno = ?) AS last_transaction
948 $drcr = prepare_query($form, $dbh, $q_drcr);
950 if ($form->{project_id}) {
951 # prepare query for each account
954 (SELECT SUM(ac.sellprice * ac.qty) * -1
956 JOIN parts p ON (ac.parts_id = p.id)
957 JOIN ap a ON (ac.trans_id = a.id)
958 JOIN chart c ON (p.expense_accno_id = c.id)
963 AND c.accno = ?) AS debit,
965 (SELECT SUM(ac.sellprice * ac.qty)
967 JOIN parts p ON (ac.parts_id = p.id)
968 JOIN ar a ON (ac.trans_id = a.id)
969 JOIN chart c ON (p.income_accno_id = c.id)
974 AND c.accno = ?) AS credit,
976 (SELECT SUM(ac.amount)
978 JOIN chart c ON (ac.chart_id = c.id)
983 AND c.accno = ?) AS saldo,
985 (SELECT SUM(ac.amount)
987 JOIN chart c ON (ac.chart_id = c.id)
993 AND c.accno = ?) AS sum_credit,
995 (SELECT SUM(ac.amount)
997 JOIN chart c ON (ac.chart_id = c.id)
1003 AND c.accno = ?) AS sum_debit,
1006 (SELECT max(ac.transdate) FROM acc_trans ac
1007 JOIN chart c ON (ac.chart_id = c.id)
1012 AND c.accno = ?) AS last_transaction
1015 $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
1019 my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
1021 foreach my $accno (sort keys %trb) {
1024 $ref->{accno} = $accno;
1025 map { $ref->{$_} = $trb{$accno}{$_} }
1026 qw(description category charttype amount soll_eb haben_eb);
1028 $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
1030 if ($trb{$accno}{charttype} eq 'A') {
1033 do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1035 ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
1036 my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
1037 $last_transaction = "";
1038 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
1039 $ref->{debit} += $debit;
1040 $ref->{credit} += $credit;
1042 $ref->{haben_saldo} += $saldo;
1044 $ref->{soll_saldo} += $saldo * -1;
1046 $ref->{last_transaction} = $last_transaction;
1047 $ref->{soll_kumuliert} = $soll_kumuliert * -1;
1048 $ref->{haben_kumuliert} = $haben_kumuliert;
1052 if ($form->{project_id}) {
1055 do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1057 ($debit, $credit) = (0, 0);
1058 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
1059 $ref->{debit} += $debit;
1060 $ref->{credit} += $credit;
1062 $ref->{haben_saldo} += $saldo;
1064 $ref->{soll_saldo} += $saldo * -1;
1066 $ref->{soll_kumuliert} += $soll_kumuliert * -1;
1067 $ref->{haben_kumuliert} += $haben_kumuliert;
1069 $project_drcr->finish;
1072 $ref->{debit} = $form->round_amount($ref->{debit}, 2);
1073 $ref->{credit} = $form->round_amount($ref->{credit}, 2);
1074 $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
1075 $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
1076 $ref->{haben_kumuliert} = $form->round_amount($ref->{haben_kumuliert}, 2);
1077 $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
1082 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1083 $accno = pop @accno;
1085 $trb{$accno}{debit} += $ref->{debit};
1086 $trb{$accno}{credit} += $ref->{credit};
1087 $trb{$accno}{soll_saldo} += $ref->{soll_saldo};
1088 $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
1089 $trb{$accno}{soll_kumuliert} += $ref->{soll_kumuliert};
1090 $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
1093 push @{ $form->{TB} }, $ref;
1099 # debits and credits for headings
1100 foreach my $accno (@headingaccounts) {
1101 foreach $ref (@{ $form->{TB} }) {
1102 if ($accno eq $ref->{accno}) {
1103 $ref->{debit} = $trb{$accno}{debit};
1104 $ref->{credit} = $trb{$accno}{credit};
1105 $ref->{soll_saldo} = $trb{$accno}{soll_saldo};
1106 $ref->{haben_saldo} = $trb{$accno}{haben_saldo};
1107 $ref->{soll_kumuliert} = $trb{$accno}{soll_kumuliert};
1108 $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert}; }
1112 $main::lxdebug->leave_sub();
1116 $main::lxdebug->enter_sub();
1117 my ($self, $dbh, $form) = @_;
1118 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
1119 my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
1120 my $sth = $dbh->prepare($query);
1121 while(my $ref = $sth->fetchrow_hashref()) {
1122 $ref->{invnumer} =~ s/Storno zu //g;
1123 $form->{storno}{$ref->{invnumber}} = 1;
1125 $main::lxdebug->leave_sub();
1129 $main::lxdebug->enter_sub();
1131 my ($self, $myconfig, $form) = @_;
1133 # connect to database
1134 my $dbh = $form->dbconnect($myconfig);
1136 my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
1138 if ($form->{ct} eq "customer") {
1151 $ct_id = "${ct}_id";
1153 $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
1154 my $todate = conv_dateq($form->{todate});
1155 my $fromdate = conv_dateq($form->{fromdate});
1157 my $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
1159 my $where = " 1 = 1 ";
1162 if ($form->{$ct_id}) {
1163 $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
1164 } elsif ($form->{ $form->{ct} }) {
1165 $where .= qq| AND (ct.name ILIKE | . $dbh->quote('%' . $form->{$ct} . '%') . qq|)|;
1169 if ($form->{department}) {
1170 my ($null, $department_id) = split /--/, $form->{department};
1171 $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
1172 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1176 -- between 0-30 days
1178 SELECT ${ct}.id AS ctid, ${ct}.name,
1179 street, zipcode, city, country, contact, email,
1180 phone as customerphone, fax as customerfax, ${ct}number,
1181 "invnumber", "transdate",
1182 (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",
1183 "duedate", invoice, ${arap}.id,
1186 WHERE (${arap}.curr = exchangerate.curr)
1187 AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
1189 WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
1190 AND (${arap}.storno IS FALSE)
1191 AND (${arap}.${ct}_id = ${ct}.id)
1193 AND (transdate <= (date $todate) $fromwhere )
1195 ORDER BY ctid, transdate, invnumber |;
1197 my $sth_details = prepare_query($form, $dbh, $q_details);
1199 # select outstanding vendors or customers, depends on $ct
1201 qq|SELECT DISTINCT ct.id, ct.name
1202 FROM $ct ct, $arap a
1205 AND (a.${ct_id} = ct.id)
1206 AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
1207 AND (a.transdate <= $todate $fromwhere)
1210 my $sth = prepare_execute_query($form, $dbh, $query);
1213 # for each company that has some stuff outstanding
1214 while (my ($id) = $sth->fetchrow_array) {
1215 do_statement($form, $sth_details, $q_details, $id);
1217 while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
1218 $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
1219 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
1220 push @{ $form->{AG} }, $ref;
1223 $sth_details->finish;
1232 $main::lxdebug->leave_sub();
1236 $main::lxdebug->enter_sub();
1238 my ($self, $myconfig, $form) = @_;
1240 # connect to database
1241 my $dbh = $form->dbconnect($myconfig);
1243 my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
1246 qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
1249 ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
1250 selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
1253 $main::lxdebug->leave_sub();
1256 sub get_taxaccounts {
1257 $main::lxdebug->enter_sub();
1259 my ($self, $myconfig, $form) = @_;
1261 # connect to database
1262 my $dbh = $form->dbconnect($myconfig);
1266 qq|SELECT c.accno, c.description, t.rate
1268 WHERE (c.link LIKE '%CT_tax%') AND (c.id = t.chart_id)
1270 $form->{taxaccounts} = selectall_hashref_quert($form, $dbh, $query);
1274 $main::lxdebug->leave_sub();
1278 $main::lxdebug->enter_sub();
1280 my ($self, $myconfig, $form) = @_;
1282 # connect to database
1283 my $dbh = $form->dbconnect($myconfig);
1285 my ($null, $department_id) = split /--/, $form->{department};
1288 my $where = "1 = 1";
1290 if ($department_id) {
1291 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
1296 if ($form->{accno}) {
1297 $accno = $form->{accno};
1298 $rate = $form->{"$form->{accno}_rate"};
1299 $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
1305 if ($form->{db} eq 'ar') {
1306 $table = "customer";
1313 my $arap = lc($ARAP);
1315 my $transdate = "a.transdate";
1317 if ($form->{method} eq 'cash') {
1318 $transdate = "a.datepaid";
1320 my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
1327 JOIN chart ON (chart_id = id)
1328 WHERE (link LIKE '%${ARAP}_paid%')
1329 AND (transdate <= $todate)
1334 # if there are any dates construct a where
1335 $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
1336 $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
1338 my $ml = ($form->{db} eq 'ar') ? 1 : -1;
1340 my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
1341 $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
1344 if ($form->{report} !~ /nontaxable/) {
1346 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
1347 ac.amount * $ml AS tax
1349 JOIN ${arap} a ON (a.id = ac.trans_id)
1350 JOIN chart ch ON (ch.id = ac.chart_id)
1351 JOIN $table n ON (n.id = a.${table}_id)
1355 AND (a.invoice = '0')
1359 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
1360 i.sellprice * i.qty * $rate * $ml AS tax
1362 JOIN ${arap} a ON (a.id = ac.trans_id)
1363 JOIN chart ch ON (ch.id = ac.chart_id)
1364 JOIN $table n ON (n.id = a.${table}_id)
1365 JOIN ${table}tax t ON (t.${table}_id = n.id)
1366 JOIN invoice i ON (i.trans_id = a.id)
1367 JOIN partstax p ON (p.parts_id = i.parts_id)
1371 AND (a.invoice = '1')
1372 ORDER BY $sortorder|;
1374 # only gather up non-taxable transactions
1376 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount
1378 JOIN ${arap} a ON (a.id = ac.trans_id)
1379 JOIN $table n ON (n.id = a.${table}_id)
1382 AND (a.invoice = '0')
1383 AND (a.netamount = a.amount)
1387 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount
1389 JOIN ${arap} a ON (a.id = ac.trans_id)
1390 JOIN $table n ON (n.id = a.${table}_id)
1391 JOIN invoice i ON (i.trans_id = a.id)
1394 AND (a.invoice = '1')
1396 a.${table}_id NOT IN (SELECT ${table}_id FROM ${table}tax t (${table}_id))
1398 i.parts_id NOT IN (SELECT parts_id FROM partstax p (parts_id))
1400 GROUP BY a.id, a.invnumber, $transdate, n.name, i.sellprice, i.qty
1401 ORDER by $sortorder|;
1404 $form->{TR} = selectall_hashref_query($form, $dbh, $query);
1408 $main::lxdebug->leave_sub();
1411 sub paymentaccounts {
1412 $main::lxdebug->enter_sub();
1414 my ($self, $myconfig, $form) = @_;
1416 # connect to database, turn AutoCommit off
1417 my $dbh = $form->dbconnect_noauto($myconfig);
1419 my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
1421 # get A(R|P)_paid accounts
1423 qq|SELECT accno, description
1425 WHERE link LIKE '%${ARAP}_paid%'|;
1426 $form->{PR} = selectall_hashref_query($form, $dbh, $query);
1430 $main::lxdebug->leave_sub();
1434 $main::lxdebug->enter_sub();
1436 my ($self, $myconfig, $form) = @_;
1438 # connect to database, turn AutoCommit off
1439 my $dbh = $form->dbconnect_noauto($myconfig);
1444 if ($form->{db} eq 'ar') {
1445 $table = 'customer';
1457 if ($form->{department_id}) {
1458 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
1459 $where = qq| AND (t.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
1462 if ($form->{fromdate}) {
1463 $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
1465 if ($form->{todate}) {
1466 $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
1468 if (!$form->{fx_transaction}) {
1469 $where .= " AND ac.fx_transaction = '0'";
1474 if ($form->{reference}) {
1475 $reference = $dbh->quote('%' . $form->{reference} . '%');
1476 $invnumber = " AND (a.invnumber LIKE $reference)";
1477 $reference = " AND (g.reference LIKE $reference)";
1479 if ($form->{source}) {
1480 $where .= " AND (ac.source ILIKE " . $dbh->quote('%' . $form->{source} . '%') . ") ";
1482 if ($form->{memo}) {
1483 $where .= " AND (ac.memo ILIKE " . $dbh->quote('%' . $form->{memo} . '%') . ") ";
1486 my %sort_columns = (
1487 'transdate' => [ qw(transdate lower_invnumber lower_name) ],
1488 'invnumber' => [ qw(lower_invnumber lower_name transdate) ],
1489 'name' => [ qw(lower_name transdate) ],
1490 'source' => [ qw(lower_source) ],
1491 'memo' => [ qw(lower_memo) ],
1493 my %lowered_columns = (
1494 'invnumber' => { 'gl' => 'g.reference', 'arap' => 'a.invnumber', },
1495 'memo' => { 'gl' => 'ac.memo', 'arap' => 'ac.memo', },
1496 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
1497 'name' => { 'gl' => 'g.description', 'arap' => 'c.name', },
1500 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
1501 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
1502 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
1505 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
1506 foreach my $spec (@{ $sort_columns{$sortkey} }) {
1507 next if ($spec !~ m/^lower_(.*)$/);
1510 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
1513 $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
1514 $sth = prepare_query($form, $dbh, $query);
1517 qq|SELECT c.name, a.invnumber, a.ordnumber,
1518 ac.transdate, ac.amount * $ml AS paid, ac.source,
1519 a.invoice, a.id, ac.memo, '${arap}' AS module
1520 $columns_for_sorting{arap}
1522 JOIN $arap a ON (ac.trans_id = a.id)
1523 JOIN $table c ON (c.id = a.${table}_id)
1525 WHERE (ac.chart_id = ?)
1531 SELECT g.description, g.reference, NULL AS ordnumber,
1532 ac.transdate, ac.amount * $ml AS paid, ac.source,
1533 '0' as invoice, g.id, ac.memo, 'gl' AS module
1534 $columns_for_sorting{gl}
1536 JOIN gl g ON (g.id = ac.trans_id)
1538 WHERE (ac.chart_id = ?)
1541 AND (ac.amount * $ml) > 0
1543 ORDER BY $sortorder|;
1544 my $sth_details = prepare_query($form, $dbh, $q_details);
1548 # cycle through each id
1549 foreach my $accno (split(/ /, $form->{paymentaccounts})) {
1550 do_statement($form, $sth, $query, $accno);
1551 my $ref = $sth->fetchrow_hashref();
1552 push(@{ $form->{PR} }, $ref);
1555 $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
1557 do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
1558 while (my $pr = $sth_details->fetchrow_hashref()) {
1559 push(@{ $form->{ $ref->{id} } }, $pr);
1561 $sth_details->finish();
1566 $main::lxdebug->leave_sub();
1570 $main::lxdebug->enter_sub();
1572 my ($self, $myconfig, $form) = @_;
1574 # connect to database
1575 my $dbh = $form->dbconnect($myconfig);
1577 my $last_period = 0;
1580 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);
1582 $form->{decimalplaces} *= 1;
1584 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
1586 # if there are any compare dates
1588 if ($form->{fromdate} || $form->{todate}) {
1590 if ($form->{fromdate}) {
1591 $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1594 $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1597 my $kummfromdate = $form->{comparefromdate};
1598 my $kummtodate = $form->{comparetodate};
1599 &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
1602 my @periods = qw(jetzt kumm);
1603 my @gesamtleistung = qw(1 2 3);
1604 my @gesamtkosten = qw (10 11 12 13 14 15 16 17 18 19 20);
1606 qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
1608 foreach my $key (@periods) {
1609 $form->{ "$key" . "gesamtleistung" } = 0;
1610 $form->{ "$key" . "gesamtkosten" } = 0;
1612 foreach $category (@categories) {
1614 if (defined($form->{$category}{$key})) {
1615 $form->{"$key$category"} =
1616 $form->format_amount($myconfig,
1617 $form->round_amount($form->{$category}{$key}, 2
1619 $form->{decimalplaces},
1623 foreach my $item (@gesamtleistung) {
1624 $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
1626 foreach my $item (@gesamtkosten) {
1627 $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
1629 $form->{ "$key" . "rohertrag" } =
1630 $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
1631 $form->{ "$key" . "betriebrohertrag" } =
1632 $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
1633 $form->{ "$key" . "betriebsergebnis" } =
1634 $form->{ "$key" . "betriebrohertrag" } -
1635 $form->{ "$key" . "gesamtkosten" };
1636 $form->{ "$key" . "neutraleraufwand" } =
1637 $form->{30}{$key} + $form->{31}{$key};
1638 $form->{ "$key" . "neutralertrag" } =
1639 $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
1640 $form->{ "$key" . "ergebnisvorsteuern" } =
1641 $form->{ "$key" . "betriebsergebnis" } -
1642 $form->{ "$key" . "neutraleraufwand" } +
1643 $form->{ "$key" . "neutralertrag" };
1644 $form->{ "$key" . "ergebnis" } =
1645 $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
1647 if ($form->{ "$key" . "gesamtleistung" } > 0) {
1648 foreach $category (@categories) {
1649 if (defined($form->{$category}{$key})) {
1650 $form->{ "$key" . "gl" . "$category" } =
1651 $form->format_amount(
1653 $form->round_amount(
1654 ($form->{$category}{$key} /
1655 $form->{ "$key" . "gesamtleistung" } * 100
1657 $form->{decimalplaces}
1659 $form->{decimalplaces},
1663 foreach my $item (@ergebnisse) {
1664 $form->{ "$key" . "gl" . "$item" } =
1665 $form->format_amount($myconfig,
1666 $form->round_amount(
1667 ( $form->{ "$key" . "$item" } /
1668 $form->{ "$key" . "gesamtleistung" } * 100
1670 $form->{decimalplaces}
1672 $form->{decimalplaces},
1677 if ($form->{ "$key" . "gesamtkosten" } > 0) {
1678 foreach $category (@categories) {
1679 if (defined($form->{$category}{$key})) {
1680 $form->{ "$key" . "gk" . "$category" } =
1681 $form->format_amount($myconfig,
1682 $form->round_amount(
1683 ($form->{$category}{$key} /
1684 $form->{ "$key" . "gesamtkosten" } * 100
1686 $form->{decimalplaces}
1688 $form->{decimalplaces},
1692 foreach my $item (@ergebnisse) {
1693 $form->{ "$key" . "gk" . "$item" } =
1694 $form->format_amount($myconfig,
1695 $form->round_amount(
1696 ($form->{ "$key" . "$item" } /
1697 $form->{ "$key" . "gesamtkosten" } * 100
1699 $form->{decimalplaces}
1701 $form->{decimalplaces},
1706 if ($form->{10}{$key} > 0) {
1707 foreach $category (@categories) {
1708 if (defined($form->{$category}{$key})) {
1709 $form->{ "$key" . "pk" . "$category" } =
1710 $form->format_amount(
1712 $form->round_amount(
1713 ($form->{$category}{$key} / $form->{10}{$key} * 100),
1714 $form->{decimalplaces}
1716 $form->{decimalplaces},
1720 foreach my $item (@ergebnisse) {
1721 $form->{ "$key" . "pk" . "$item" } =
1722 $form->format_amount($myconfig,
1723 $form->round_amount(
1724 ($form->{ "$key" . "$item" } /
1725 $form->{10}{$key} * 100
1727 $form->{decimalplaces}
1729 $form->{decimalplaces},
1734 if ($form->{4}{$key} > 0) {
1735 foreach $category (@categories) {
1736 if (defined($form->{$category}{$key})) {
1737 $form->{ "$key" . "auf" . "$category" } =
1738 $form->format_amount(
1740 $form->round_amount(
1741 ($form->{$category}{$key} / $form->{4}{$key} * 100),
1742 $form->{decimalplaces}
1744 $form->{decimalplaces},
1748 foreach my $item (@ergebnisse) {
1749 $form->{ "$key" . "auf" . "$item" } =
1750 $form->format_amount($myconfig,
1751 $form->round_amount(
1752 ($form->{ "$key" . "$item" } /
1753 $form->{4}{$key} * 100
1755 $form->{decimalplaces}
1757 $form->{decimalplaces},
1762 foreach my $item (@ergebnisse) {
1763 $form->{ "$key" . "$item" } =
1764 $form->format_amount($myconfig,
1765 $form->round_amount($form->{ "$key" . "$item" },
1766 $form->{decimalplaces}
1768 $form->{decimalplaces},
1775 $main::lxdebug->leave_sub();
1779 $main::lxdebug->enter_sub();
1781 my ($self, $myconfig, $form) = @_;
1783 # connect to database
1784 my $dbh = $form->dbconnect($myconfig);
1786 my $last_period = 0;
1787 my @categories_cent = qw(51r 511 86r 861 97r 971 93r 931
1788 96 66 43 45 53 62 65 67);
1789 my @categories_euro = qw(48 51 86 91 97 93 94);
1790 $form->{decimalplaces} *= 1;
1792 foreach my $item (@categories_cent) {
1793 $form->{"$item"} = 0;
1795 foreach my $item (@categories_euro) {
1796 $form->{"$item"} = 0;
1799 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_ustva");
1801 # foreach $item (@categories_cent) {
1802 # if ($form->{$item}{"jetzt"} > 0) {
1803 # $form->{$item} = $form->{$item}{"jetzt"};
1804 # delete $form->{$item}{"jetzt"};
1807 # foreach $item (@categories_euro) {
1808 # if ($form->{$item}{"jetzt"} > 0) {
1809 # $form->{$item} = $form->{$item}{"jetzt"};
1810 # delete $form->{$item}{"jetzt"};
1811 # } foreach $item (@categories_cent) {
1812 # if ($form->{$item}{"jetzt"} > 0) {
1813 # $form->{$item} = $form->{$item}{"jetzt"};
1814 # delete $form->{$item}{"jetzt"};
1817 # foreach $item (@categories_euro) {
1818 # if ($form->{$item}{"jetzt"} > 0) {
1819 # $form->{$item} = $form->{$item}{"jetzt"};
1820 # delete $form->{$item}{"jetzt"};
1827 # Berechnung der USTVA Formularfelder
1829 $form->{"51r"} = $form->{"511"};
1830 $form->{"86r"} = $form->{"861"};
1831 $form->{"97r"} = $form->{"971"};
1832 $form->{"93r"} = $form->{"931"};
1834 #$form->{"96"} = $form->{"94"} * 0.16;
1836 $form->{"51r"} + $form->{"86r"} + $form->{"97r"} + $form->{"93r"} +
1838 $form->{"45"} = $form->{"43"};
1839 $form->{"53"} = $form->{"43"};
1840 $form->{"62"} = $form->{"43"} - $form->{"66"};
1841 $form->{"65"} = $form->{"43"} - $form->{"66"};
1842 $form->{"67"} = $form->{"43"} - $form->{"66"};
1844 foreach my $item (@categories_cent) {
1846 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),
1850 foreach my $item (@categories_euro) {
1852 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0),
1858 $main::lxdebug->leave_sub();
1861 sub income_statement {
1862 $main::lxdebug->enter_sub();
1864 my ($self, $myconfig, $form) = @_;
1866 # connect to database
1867 my $dbh = $form->dbconnect($myconfig);
1869 my $last_period = 0;
1870 my @categories_einnahmen = qw(1 2 3 4 5 6 7);
1871 my @categories_ausgaben =
1872 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);
1874 my @ergebnisse = qw(sumeura sumeurb guvsumme);
1876 $form->{decimalplaces} *= 1;
1878 foreach my $item (@categories_einnahmen) {
1881 foreach my $item (@categories_ausgaben) {
1885 foreach my $item (@ergebnisse) {
1889 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
1892 foreach my $item (@categories_einnahmen) {
1893 $form->{"eur${item}"} =
1894 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
1895 $form->{"sumeura"} += $form->{$item};
1897 foreach my $item (@categories_ausgaben) {
1898 $form->{"eur${item}"} =
1899 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
1900 $form->{"sumeurb"} += $form->{$item};
1903 $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
1905 foreach my $item (@ergebnisse) {
1907 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
1909 $main::lxdebug->leave_sub();