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 #======================================================================
41 $main::lxdebug->enter_sub();
43 my ($self, $myconfig, $form) = @_;
46 my $dbh = $form->dbconnect($myconfig);
49 my @categories = qw(A C L Q);
51 # if there are any dates construct a where
52 if ($form->{asofdate}) {
53 $form->{period} = $form->{this_period} = conv_dateq($form->{asofdate});
56 $form->{decimalplaces} *= 1;
58 &get_accounts($dbh, $last_period, "", $form->{asofdate}, $form,
61 # if there are any compare dates
62 if ($form->{compareasofdate}) {
65 &get_accounts($dbh, $last_period, "", $form->{compareasofdate},
68 $form->{last_period} = conv_dateq($form->{compareasofdate});
75 # now we got $form->{A}{accno}{ } assets
76 # and $form->{L}{accno}{ } liabilities
77 # and $form->{Q}{accno}{ } equity
78 # build asset accounts
84 'A' => { 'label' => 'asset',
88 'L' => { 'label' => 'liability',
89 'labels' => 'liabilities',
92 'Q' => { 'label' => 'equity',
97 foreach my $category (grep { !/C/ } @categories) {
99 foreach $key (sort keys %{ $form->{$category} }) {
101 $str = ($form->{l_heading}) ? $form->{padding} : "";
103 if ($form->{$category}{$key}{charttype} eq "A") {
106 ? "$form->{$category}{$key}{accno} - $form->{$category}{$key}{description}"
107 : "$form->{$category}{$key}{description}";
109 if ($form->{$category}{$key}{charttype} eq "H") {
110 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
112 push(@{ $form->{"$account{$category}{label}_account"} },
113 "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
115 push(@{ $form->{"$account{$category}{label}_this_period"} },
116 $form->format_amount(
118 $account{$category}{subthis} * $account{$category}{ml},
119 $form->{decimalplaces}, $dash
123 push(@{ $form->{"$account{$category}{label}_last_period"} },
124 $form->format_amount(
126 $account{$category}{sublast} * $account{$category}{ml},
127 $form->{decimalplaces}, $dash
133 "$form->{bold}$form->{$category}{$key}{description}$form->{endbold}";
135 $account{$category}{subthis} = $form->{$category}{$key}{this};
136 $account{$category}{sublast} = $form->{$category}{$key}{last};
137 $account{$category}{subdescription} =
138 $form->{$category}{$key}{description};
139 $account{$category}{subtotal} = 1;
141 $form->{$category}{$key}{this} = 0;
142 $form->{$category}{$key}{last} = 0;
144 next unless $form->{l_heading};
149 # push description onto array
150 push(@{ $form->{"$account{$category}{label}_account"} }, $str);
152 if ($form->{$category}{$key}{charttype} eq 'A') {
153 $form->{"total_$account{$category}{labels}_this_period"} +=
154 $form->{$category}{$key}{this} * $account{$category}{ml};
158 push(@{ $form->{"$account{$category}{label}_this_period"} },
159 $form->format_amount(
161 $form->{$category}{$key}{this} * $account{$category}{ml},
162 $form->{decimalplaces}, $dash
166 $form->{"total_$account{$category}{labels}_last_period"} +=
167 $form->{$category}{$key}{last} * $account{$category}{ml};
169 push(@{ $form->{"$account{$category}{label}_last_period"} },
170 $form->format_amount(
172 $form->{$category}{$key}{last} * $account{$category}{ml},
173 $form->{decimalplaces}, $dash
178 $str = ($form->{l_heading}) ? $form->{padding} : "";
179 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
180 push(@{ $form->{"$account{$category}{label}_account"} },
181 "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
183 push(@{ $form->{"$account{$category}{label}_this_period"} },
184 $form->format_amount(
186 $account{$category}{subthis} * $account{$category}{ml},
187 $form->{decimalplaces}, $dash
191 push(@{ $form->{"$account{$category}{label}_last_period"} },
192 $form->format_amount(
194 $account{$category}{sublast} * $account{$category}{ml},
195 $form->{decimalplaces}, $dash
202 # totals for assets, liabilities
203 $form->{total_assets_this_period} =
204 $form->round_amount($form->{total_assets_this_period},
205 $form->{decimalplaces});
206 $form->{total_liabilities_this_period} =
207 $form->round_amount($form->{total_liabilities_this_period},
208 $form->{decimalplaces});
209 $form->{total_equity_this_period} =
210 $form->round_amount($form->{total_equity_this_period},
211 $form->{decimalplaces});
214 $form->{earnings_this_period} =
215 $form->{total_assets_this_period} -
216 $form->{total_liabilities_this_period} - $form->{total_equity_this_period};
218 push(@{ $form->{equity_this_period} },
219 $form->format_amount($myconfig,
220 $form->{earnings_this_period},
221 $form->{decimalplaces}, "- "
224 $form->{total_equity_this_period} =
226 $form->{total_equity_this_period} + $form->{earnings_this_period},
227 $form->{decimalplaces});
229 # add liability + equity
230 $form->{total_this_period} =
231 $form->format_amount(
233 $form->{total_liabilities_this_period} + $form->{total_equity_this_period},
234 $form->{decimalplaces},
239 # totals for assets, liabilities
240 $form->{total_assets_last_period} =
241 $form->round_amount($form->{total_assets_last_period},
242 $form->{decimalplaces});
243 $form->{total_liabilities_last_period} =
244 $form->round_amount($form->{total_liabilities_last_period},
245 $form->{decimalplaces});
246 $form->{total_equity_last_period} =
247 $form->round_amount($form->{total_equity_last_period},
248 $form->{decimalplaces});
250 # calculate retained earnings
251 $form->{earnings_last_period} =
252 $form->{total_assets_last_period} -
253 $form->{total_liabilities_last_period} -
254 $form->{total_equity_last_period};
256 push(@{ $form->{equity_last_period} },
257 $form->format_amount($myconfig,
258 $form->{earnings_last_period},
259 $form->{decimalplaces}, "- "
262 $form->{total_equity_last_period} =
264 $form->{total_equity_last_period} + $form->{earnings_last_period},
265 $form->{decimalplaces});
267 # add liability + equity
268 $form->{total_last_period} =
269 $form->format_amount($myconfig,
270 $form->{total_liabilities_last_period} +
271 $form->{total_equity_last_period},
272 $form->{decimalplaces},
277 $form->{total_liabilities_last_period} =
278 $form->format_amount($myconfig,
279 $form->{total_liabilities_last_period},
280 $form->{decimalplaces}, "- ")
281 if ($form->{total_liabilities_last_period} != 0);
283 $form->{total_equity_last_period} =
284 $form->format_amount($myconfig,
285 $form->{total_equity_last_period},
286 $form->{decimalplaces}, "- ")
287 if ($form->{total_equity_last_period} != 0);
289 $form->{total_assets_last_period} =
290 $form->format_amount($myconfig,
291 $form->{total_assets_last_period},
292 $form->{decimalplaces}, "- ")
293 if ($form->{total_assets_last_period} != 0);
295 $form->{total_assets_this_period} =
296 $form->format_amount($myconfig,
297 $form->{total_assets_this_period},
298 $form->{decimalplaces}, "- ");
300 $form->{total_liabilities_this_period} =
301 $form->format_amount($myconfig,
302 $form->{total_liabilities_this_period},
303 $form->{decimalplaces}, "- ");
305 $form->{total_equity_this_period} =
306 $form->format_amount($myconfig,
307 $form->{total_equity_this_period},
308 $form->{decimalplaces}, "- ");
310 $main::lxdebug->leave_sub();
314 $main::lxdebug->enter_sub();
316 my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
318 my ($null, $department_id) = split /--/, $form->{department};
330 my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |;
334 qq|SELECT c.accno, c.description, c.category
336 WHERE (c.charttype = 'H')
340 $sth = prepare_execute_query($form, $dbh, $query);
342 my @headingaccounts = ();
343 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
344 $form->{ $ref->{category} }{ $ref->{accno} }{description} =
345 "$ref->{description}";
346 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
347 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
349 push @headingaccounts, $ref->{accno};
355 $fromdate = conv_dateq($fromdate);
356 if ($form->{method} eq 'cash') {
357 $subwhere .= " AND (transdate >= $fromdate)";
358 $glwhere = " AND (ac.transdate >= $fromdate)";
360 $where .= " AND (ac.transdate >= $fromdate)";
365 $todate = conv_dateq($todate);
366 $where .= " AND (ac.transdate <= $todate)";
367 $subwhere .= " AND (transdate <= $todate)";
370 if ($department_id) {
371 $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
372 $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|)|;
375 if ($form->{project_id}) {
376 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
379 if ($form->{method} eq 'cash') {
381 qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
383 JOIN chart c ON (c.id = ac.chart_id)
384 JOIN ar a ON (a.id = ac.trans_id)
393 JOIN chart ON (chart_id = id)
394 WHERE (link LIKE '%AR_paid%')
398 GROUP BY c.accno, c.description, c.category
402 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
404 JOIN chart c ON (c.id = ac.chart_id)
405 JOIN ap a ON (a.id = ac.trans_id)
414 JOIN chart ON (chart_id = id)
415 WHERE (link LIKE '%AP_paid%')
419 GROUP BY c.accno, c.description, c.category
423 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
425 JOIN chart c ON (c.id = ac.chart_id)
426 JOIN gl a ON (a.id = ac.trans_id)
432 AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
434 GROUP BY c.accno, c.description, c.category |;
436 if ($form->{project_id}) {
441 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
443 JOIN ar a ON (a.id = ac.trans_id)
444 JOIN parts p ON (ac.parts_id = p.id)
445 JOIN chart c on (p.income_accno_id = c.id)
447 -- use transdate from subwhere
448 WHERE (c.category = 'I')
455 JOIN chart ON (chart_id = id)
456 WHERE (link LIKE '%AR_paid%')
460 GROUP BY c.accno, c.description, c.category
464 SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
466 JOIN ap a ON (a.id = ac.trans_id)
467 JOIN parts p ON (ac.parts_id = p.id)
468 JOIN chart c on (p.expense_accno_id = c.id)
470 WHERE (c.category = 'E')
477 JOIN chart ON (chart_id = id)
478 WHERE link LIKE '%AP_paid%'
482 GROUP BY c.accno, c.description, c.category |;
485 } else { # if ($form->{method} eq 'cash')
486 if ($department_id) {
487 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
488 $dpt_where = qq| AND t.department_id = $department_id |;
492 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
494 JOIN chart c ON (c.id = ac.chart_id)
500 GROUP BY c.accno, c.description, c.category |;
502 if ($form->{project_id}) {
506 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
508 JOIN ar a ON (a.id = ac.trans_id)
509 JOIN parts p ON (ac.parts_id = p.id)
510 JOIN chart c on (p.income_accno_id = c.id)
512 -- use transdate from subwhere
513 WHERE (c.category = 'I')
517 GROUP BY c.accno, c.description, c.category
521 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
523 JOIN ap a ON (a.id = ac.trans_id)
524 JOIN parts p ON (ac.parts_id = p.id)
525 JOIN chart c on (p.expense_accno_id = c.id)
527 WHERE (c.category = 'E')
531 GROUP BY c.accno, c.description, c.category |;
539 my $sth = prepare_execute_query($form, $dbh, $query);
541 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
543 if ($ref->{category} eq 'C') {
544 $ref->{category} = 'A';
547 # get last heading account
548 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
552 $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
554 $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
558 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
559 $form->{ $ref->{category} }{ $ref->{accno} }{description} =
561 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
564 $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
566 $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
571 # remove accounts with zero balance
572 foreach $category (@{$categories}) {
573 foreach $accno (keys %{ $form->{$category} }) {
574 $form->{$category}{$accno}{last} =
575 $form->round_amount($form->{$category}{$accno}{last},
576 $form->{decimalplaces});
577 $form->{$category}{$accno}{this} =
578 $form->round_amount($form->{$category}{$accno}{this},
579 $form->{decimalplaces});
581 delete $form->{$category}{$accno}
582 if ( $form->{$category}{$accno}{this} == 0
583 && $form->{$category}{$accno}{last} == 0);
587 $main::lxdebug->leave_sub();
591 $main::lxdebug->enter_sub();
593 my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
595 my ($null, $department_id) = split /--/, $form->{department};
608 $fromdate = conv_dateq($fromdate);
609 if ($form->{method} eq 'cash') {
610 $subwhere .= " AND (transdate >= $fromdate)";
611 $glwhere = " AND (ac.transdate >= $fromdate)";
612 $prwhere = " AND (ar.transdate >= $fromdate)";
614 $where .= " AND (ac.transdate >= $fromdate)";
619 $todate = conv_dateq($todate);
620 $subwhere .= " AND (transdate <= $todate)";
621 $where .= " AND (ac.transdate <= $todate)";
622 $prwhere .= " AND (ar.transdate <= $todate)";
625 if ($department_id) {
626 $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
627 $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|) |;
630 if ($form->{project_id}) {
631 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
634 if ($form->{method} eq 'cash') {
637 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
639 JOIN chart c ON (c.id = ac.chart_id)
640 JOIN ar a ON (a.id = ac.trans_id)
642 WHERE $where $dpt_where
643 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AR_paid%') $subwhere)
649 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
651 JOIN chart c ON (c.id = ac.chart_id)
652 JOIN ap a ON (a.id = ac.trans_id)
654 WHERE $where $dpt_where
655 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AP_paid%') $subwhere)
661 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
663 JOIN chart c ON (c.id = ac.chart_id)
664 JOIN gl a ON (a.id = ac.trans_id)
666 WHERE $where $dpt_where $glwhere
667 AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
674 if ($form->{project_id}) {
678 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
680 JOIN ar a ON (a.id = ac.trans_id)
681 JOIN parts p ON (ac.parts_id = p.id)
682 JOIN chart c on (p.income_accno_id = c.id)
684 WHERE (c.category = 'I') $prwhere $dpt_where
685 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AR_paid%') $subwhere)
691 SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category
693 JOIN ap a ON (a.id = ac.trans_id)
694 JOIN parts p ON (ac.parts_id = p.id)
695 JOIN chart c on (p.expense_accno_id = c.id)
697 WHERE (c.category = 'E') $prwhere $dpt_where
698 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AP_paid%') $subwhere)
704 } else { # if ($form->{method} eq 'cash')
705 if ($department_id) {
706 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
707 $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
711 SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
713 JOIN chart c ON (c.id = ac.chart_id)
718 GROUP BY c.$category |;
720 if ($form->{project_id}) {
724 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
726 JOIN ar a ON (a.id = ac.trans_id)
727 JOIN parts p ON (ac.parts_id = p.id)
728 JOIN chart c on (p.income_accno_id = c.id)
730 WHERE (c.category = 'I')
738 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
740 JOIN ap a ON (a.id = ac.trans_id)
741 JOIN parts p ON (ac.parts_id = p.id)
742 JOIN chart c on (p.expense_accno_id = c.id)
744 WHERE (c.category = 'E')
748 GROUP BY c.$category |;
756 my $sth = prepare_execute_query($form, $dbh, $query);
758 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
759 if ($category eq "pos_bwa") {
761 $form->{ $ref->{$category} }{kumm} += $ref->{amount};
763 $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
766 $form->{ $ref->{$category} } += $ref->{amount};
771 $main::lxdebug->leave_sub();
775 $main::lxdebug->enter_sub();
777 my ($self, $myconfig, $form) = @_;
779 my $dbh = $form->dbconnect($myconfig);
781 my ($query, $sth, $ref);
784 my ($null, $department_id) = split /--/, $form->{department};
785 my @headingaccounts = ();
791 my $invwhere = $where;
793 if ($department_id) {
794 $dpt_join = qq| JOIN dpt_trans t ON (ac.trans_id = t.trans_id) |;
795 $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
798 # project_id only applies to getting transactions
799 # it has nothing to do with a trial balance
800 # but we use the same function to collect information
802 if ($form->{project_id}) {
803 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
806 my $acc_cash_where = "";
807 my $ar_cash_where = "";
808 my $ap_cash_where = "";
811 if ($form->{method} eq "cash") {
812 $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}')) |;
813 $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
816 # get beginning balances
818 qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
820 LEFT JOIN chart c ON (ac.chart_id = c.id)
822 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction $acc_cash_where
825 GROUP BY c.accno, c.category, c.description |;
827 $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
829 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
831 if ($ref->{amount} != 0 || $form->{all_accounts}) {
832 $trb{ $ref->{accno} }{description} = $ref->{description};
833 $trb{ $ref->{accno} }{charttype} = 'A';
835 if ($ref->{amount} > 0) {
836 $trb{ $ref->{accno} }{haben_eb} = $ref->{amount};
838 $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1;
840 $trb{ $ref->{accno} }{category} = $ref->{category};
849 qq|SELECT c.accno, c.description, c.category
851 WHERE c.charttype = 'H'
854 $sth = prepare_execute_query($form, $dbh, $query);
856 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
857 $trb{ $ref->{accno} }{description} = $ref->{description};
858 $trb{ $ref->{accno} }{charttype} = 'H';
859 $trb{ $ref->{accno} }{category} = $ref->{category};
861 push @headingaccounts, $ref->{accno};
867 $saldowhere = " 1 = 1 ";
868 $sumwhere = " 1 = 1 ";
871 if ($form->{fromdate} || $form->{todate}) {
872 if ($form->{fromdate}) {
873 my $fromdate = conv_dateq($form->{fromdate});
874 $tofrom .= " AND (ac.transdate >= $fromdate)";
875 $subwhere .= " AND (transdate >= $fromdate)";
876 $sumsubwhere .= " AND (transdate >= (select date_trunc('year', date $fromdate))) ";
877 $saldosubwhere .= " AND transdate>=(select date_trunc('year', date $fromdate)) ";
878 $invwhere .= " AND (a.transdate >= $fromdate)";
879 $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
880 $glwhere = " AND (ac.transdate >= $fromdate)";
881 $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
883 if ($form->{todate}) {
884 my $todate = conv_dateq($form->{todate});
885 $tofrom .= " AND (ac.transdate <= $todate)";
886 $invwhere .= " AND (a.transdate <= $todate)";
887 $saldosubwhere .= " AND (transdate <= $todate)";
888 $sumsubwhere .= " AND (transdate <= $todate)";
889 $subwhere .= " AND (transdate <= $todate)";
890 $glwhere .= " AND (ac.transdate <= $todate)";
891 $glsumwhere .= " AND (ac.transdate <= $todate) ";
892 $glsaldowhere .= " AND (ac.transdate <= $todate) ";
896 if ($form->{method} eq "cash") {
898 qq| AND ((ac.trans_id IN (SELECT id from ar) AND
903 JOIN chart ON (chart_id = id)
904 WHERE (link LIKE '%AR_paid%')
909 (ac.trans_id in (SELECT id from ap) AND
914 JOIN chart ON (chart_id = id)
915 WHERE (link LIKE '%AP_paid%')
920 (ac.trans_id in (SELECT id from gl)
924 qq| AND ((ac.trans_id IN (SELECT id from ar) AND
929 JOIN chart ON (chart_id = id)
930 WHERE (link LIKE '%AR_paid%')
935 (ac.trans_id in (SELECT id from ap) AND
940 JOIN chart ON (chart_id = id)
941 WHERE (link LIKE '%AP_paid%')
946 (ac.trans_id in (SELECT id from gl)
950 qq| AND ((ac.trans_id IN (SELECT id from ar) AND
955 JOIN chart ON (chart_id = id)
956 WHERE (link LIKE '%AR_paid%')
961 (ac.trans_id in (SELECT id from ap) AND
966 JOIN chart ON (chart_id = id)
967 WHERE (link LIKE '%AP_paid%')
972 (ac.trans_id in (SELECT id from gl)
977 $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
978 $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
979 $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
983 SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
985 JOIN chart c ON (c.id = ac.chart_id)
990 GROUP BY c.accno, c.description, c.category |;
992 if ($form->{project_id}) {
994 -- add project transactions from invoice
998 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
1000 JOIN ar a ON (ac.trans_id = a.id)
1001 JOIN parts p ON (ac.parts_id = p.id)
1002 JOIN chart c ON (p.income_accno_id = c.id)
1007 GROUP BY c.accno, c.description, c.category
1011 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
1013 JOIN ap a ON (ac.trans_id = a.id)
1014 JOIN parts p ON (ac.parts_id = p.id)
1015 JOIN chart c ON (p.expense_accno_id = c.id)
1020 GROUP BY c.accno, c.description, c.category
1024 $query .= qq| ORDER BY accno|;
1026 $sth = prepare_execute_query($form, $dbh, $query);
1028 # calculate the debit and credit in the period
1029 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1030 $trb{ $ref->{accno} }{description} = $ref->{description};
1031 $trb{ $ref->{accno} }{charttype} = 'A';
1032 $trb{ $ref->{accno} }{category} = $ref->{category};
1033 $trb{ $ref->{accno} }{amount} += $ref->{amount};
1037 # prepare query for each account
1038 my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
1042 (SELECT SUM(ac.amount) * -1
1044 JOIN chart c ON (c.id = ac.chart_id)
1050 AND (c.accno = ?)) AS debit,
1052 (SELECT SUM(ac.amount)
1054 JOIN chart c ON (c.id = ac.chart_id)
1060 AND c.accno = ?) AS credit,
1061 (SELECT SUM(ac.amount)
1063 JOIN chart c ON (ac.chart_id = c.id)
1068 AND c.accno = ?) AS saldo,
1070 (SELECT SUM(ac.amount)
1072 JOIN chart c ON (ac.chart_id = c.id)
1078 AND c.accno = ?) AS sum_credit,
1080 (SELECT SUM(ac.amount)
1082 JOIN chart c ON (ac.chart_id = c.id)
1088 AND c.accno = ?) AS sum_debit,
1090 (SELECT max(ac.transdate) FROM acc_trans ac
1091 JOIN chart c ON (ac.chart_id = c.id)
1096 AND c.accno = ?) AS last_transaction
1101 $drcr = prepare_query($form, $dbh, $q_drcr);
1103 if ($form->{project_id}) {
1104 # prepare query for each account
1107 (SELECT SUM(ac.sellprice * ac.qty) * -1
1109 JOIN parts p ON (ac.parts_id = p.id)
1110 JOIN ap a ON (ac.trans_id = a.id)
1111 JOIN chart c ON (p.expense_accno_id = c.id)
1116 AND c.accno = ?) AS debit,
1118 (SELECT SUM(ac.sellprice * ac.qty)
1120 JOIN parts p ON (ac.parts_id = p.id)
1121 JOIN ar a ON (ac.trans_id = a.id)
1122 JOIN chart c ON (p.income_accno_id = c.id)
1127 AND c.accno = ?) AS credit,
1129 (SELECT SUM(ac.amount)
1131 JOIN chart c ON (ac.chart_id = c.id)
1136 AND c.accno = ?) AS saldo,
1138 (SELECT SUM(ac.amount)
1140 JOIN chart c ON (ac.chart_id = c.id)
1146 AND c.accno = ?) AS sum_credit,
1148 (SELECT SUM(ac.amount)
1150 JOIN chart c ON (ac.chart_id = c.id)
1156 AND c.accno = ?) AS sum_debit,
1159 (SELECT max(ac.transdate) FROM acc_trans ac
1160 JOIN chart c ON (ac.chart_id = c.id)
1165 AND c.accno = ?) AS last_transaction
1168 $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
1172 my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
1174 foreach my $accno (sort keys %trb) {
1177 $ref->{accno} = $accno;
1178 map { $ref->{$_} = $trb{$accno}{$_} }
1179 qw(description category charttype amount soll_eb haben_eb);
1181 $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
1183 if ($trb{$accno}{charttype} eq 'A') {
1186 do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1188 ($debit, $credit, $saldo, $haben_saldo, $soll_saldo, $soll_kumuliert, $haben_kumuliert) = (0, 0, 0, 0, 0, 0, 0);
1189 $last_transaction = "";
1190 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
1191 $ref->{debit} += $debit;
1192 $ref->{credit} += $credit;
1194 $ref->{haben_saldo} += $saldo;
1196 $ref->{soll_saldo} += $saldo * -1;
1198 $ref->{last_transaction} = $last_transaction;
1199 $ref->{soll_kumuliert} = $soll_kumuliert * -1;
1200 $ref->{haben_kumuliert} = $haben_kumuliert;
1204 if ($form->{project_id}) {
1207 do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1209 ($debit, $credit) = (0, 0);
1210 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
1211 $ref->{debit} += $debit;
1212 $ref->{credit} += $credit;
1214 $ref->{haben_saldo} += $saldo;
1216 $ref->{soll_saldo} += $saldo * -1;
1218 $ref->{soll_kumuliert} += $soll_kumuliert * -1;
1219 $ref->{haben_kumuliert} += $haben_kumuliert;
1221 $project_drcr->finish;
1224 $ref->{debit} = $form->round_amount($ref->{debit}, 2);
1225 $ref->{credit} = $form->round_amount($ref->{credit}, 2);
1226 $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
1227 $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
1228 $ref->{haben_kumuliert} = $form->round_amount($ref->{haben_kumuliert}, 2);
1229 $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
1233 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1234 $accno = pop @accno;
1236 $trb{$accno}{debit} += $ref->{debit};
1237 $trb{$accno}{credit} += $ref->{credit};
1238 $trb{$accno}{soll_saldo} += $ref->{soll_saldo};
1239 $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
1240 $trb{$accno}{soll_kumuliert} += $ref->{soll_kumuliert};
1241 $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
1244 push @{ $form->{TB} }, $ref;
1250 # debits and credits for headings
1251 foreach $accno (@headingaccounts) {
1252 foreach $ref (@{ $form->{TB} }) {
1253 if ($accno eq $ref->{accno}) {
1254 $ref->{debit} = $trb{$accno}{debit};
1255 $ref->{credit} = $trb{$accno}{credit};
1256 $ref->{soll_saldo} = $trb{$accno}{soll_saldo};
1257 $ref->{haben_saldo} = $trb{$accno}{haben_saldo};
1258 $ref->{soll_kumuliert} = $trb{$accno}{soll_kumuliert};
1259 $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert}; }
1263 $main::lxdebug->leave_sub();
1267 $main::lxdebug->enter_sub();
1268 my ($self, $dbh, $form) = @_;
1269 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
1270 my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
1271 my $sth = $dbh->prepare($query);
1272 while(my $ref = $sth->fetchrow_hashref()) {
1273 $ref->{invnumer} =~ s/Storno zu //g;
1274 $form->{storno}{$ref->{invnumber}} = 1;
1276 $main::lxdebug->leave_sub();
1280 $main::lxdebug->enter_sub();
1282 my ($self, $myconfig, $form) = @_;
1284 # connect to database
1285 my $dbh = $form->dbconnect($myconfig);
1287 my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
1289 if ($form->{ct} eq "customer") {
1302 $ct_id = "${ct}_id";
1304 $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
1305 my $todate = conv_dateq($form->{todate});
1307 my $where = " 1 = 1 ";
1310 if ($form->{$ct_id}) {
1311 $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
1312 } elsif ($form->{ $form->{ct} }) {
1313 $where .= qq| AND (ct.name ILIKE | . $dbh->quote('%' . $form->{$ct} . '%') . qq|)|;
1317 if ($form->{department}) {
1318 ($null, $department_id) = split /--/, $form->{department};
1319 $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
1320 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1324 -- between 0-30 days
1326 SELECT ${ct}.id AS ctid, ${ct}.name,
1327 street, zipcode, city, country, contact, email,
1328 phone as customerphone, fax as customerfax, ${ct}number,
1329 "invnumber", "transdate",
1330 (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",
1331 "duedate", invoice, ${arap}.id,
1334 WHERE (${arap}.curr = exchangerate.curr)
1335 AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
1337 WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
1338 AND (${arap}.storno IS FALSE)
1339 AND (${arap}.${ct}_id = ${ct}.id)
1341 AND (transdate <= (date $todate))
1343 ORDER BY ctid, transdate, invnumber |;
1345 my $sth_details = prepare_query($form, $dbh, $q_details);
1347 # select outstanding vendors or customers, depends on $ct
1349 qq|SELECT DISTINCT ct.id, ct.name
1350 FROM $ct ct, $arap a
1353 AND (a.${ct_id} = ct.id)
1354 AND (a.paid != a.amount)
1355 AND (a.transdate <= $todate)
1358 my $sth = prepare_execute_query($form, $dbh, $query);
1361 # for each company that has some stuff outstanding
1362 while (my ($id) = $sth->fetchrow_array) {
1363 do_statement($form, $sth_details, $q_details, $id);
1365 while (my $ref = $sth_details->fetchrow_hashref(NAME_lc)) {
1366 $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
1367 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
1368 push @{ $form->{AG} }, $ref;
1371 $sth_details->finish;
1380 $main::lxdebug->leave_sub();
1384 $main::lxdebug->enter_sub();
1386 my ($self, $myconfig, $form) = @_;
1388 # connect to database
1389 my $dbh = $form->dbconnect($myconfig);
1391 my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
1394 qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
1397 ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
1398 selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
1401 $main::lxdebug->leave_sub();
1404 sub get_taxaccounts {
1405 $main::lxdebug->enter_sub();
1407 my ($self, $myconfig, $form) = @_;
1409 # connect to database
1410 my $dbh = $form->dbconnect($myconfig);
1414 qq|SELECT c.accno, c.description, t.rate
1416 WHERE (c.link LIKE '%CT_tax%') AND (c.id = t.chart_id)
1418 $form->{taxaccounts} = selectall_hashref_quert($form, $dbh, $query);
1422 $main::lxdebug->leave_sub();
1426 $main::lxdebug->enter_sub();
1428 my ($self, $myconfig, $form) = @_;
1430 # connect to database
1431 my $dbh = $form->dbconnect($myconfig);
1433 my ($null, $department_id) = split /--/, $form->{department};
1436 my $where = "1 = 1";
1438 if ($department_id) {
1439 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
1444 if ($form->{accno}) {
1445 $accno = $form->{accno};
1446 $rate = $form->{"$form->{accno}_rate"};
1447 $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
1453 if ($form->{db} eq 'ar') {
1454 $table = "customer";
1461 my $arap = lc($ARAP);
1463 my $transdate = "a.transdate";
1465 if ($form->{method} eq 'cash') {
1466 $transdate = "a.datepaid";
1468 my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
1475 JOIN chart ON (chart_id = id)
1476 WHERE (link LIKE '%${ARAP}_paid%')
1477 AND (transdate <= $todate)
1482 # if there are any dates construct a where
1483 $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
1484 $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
1486 my $ml = ($form->{db} eq 'ar') ? 1 : -1;
1488 my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
1489 $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
1491 if ($form->{report} !~ /nontaxable/) {
1493 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
1494 ac.amount * $ml AS tax
1496 JOIN ${arap} a ON (a.id = ac.trans_id)
1497 JOIN chart ch ON (ch.id = ac.chart_id)
1498 JOIN $table n ON (n.id = a.${table}_id)
1502 AND (a.invoice = '0')
1506 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
1507 i.sellprice * i.qty * $rate * $ml AS tax
1509 JOIN ${arap} a ON (a.id = ac.trans_id)
1510 JOIN chart ch ON (ch.id = ac.chart_id)
1511 JOIN $table n ON (n.id = a.${table}_id)
1512 JOIN ${table}tax t ON (t.${table}_id = n.id)
1513 JOIN invoice i ON (i.trans_id = a.id)
1514 JOIN partstax p ON (p.parts_id = i.parts_id)
1518 AND (a.invoice = '1')
1519 ORDER BY $sortorder|;
1521 # only gather up non-taxable transactions
1523 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount
1525 JOIN ${arap} a ON (a.id = ac.trans_id)
1526 JOIN $table n ON (n.id = a.${table}_id)
1529 AND (a.invoice = '0')
1530 AND (a.netamount = a.amount)
1534 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount
1536 JOIN ${arap} a ON (a.id = ac.trans_id)
1537 JOIN $table n ON (n.id = a.${table}_id)
1538 JOIN invoice i ON (i.trans_id = a.id)
1541 AND (a.invoice = '1')
1543 a.${table}_id NOT IN (SELECT ${table}_id FROM ${table}tax t (${table}_id))
1545 i.parts_id NOT IN (SELECT parts_id FROM partstax p (parts_id))
1547 GROUP BY a.id, a.invnumber, $transdate, n.name, i.sellprice, i.qty
1548 ORDER by $sortorder|;
1551 $form->{TR} = selectall_hashref_query($form, $dbh, $query);
1555 $main::lxdebug->leave_sub();
1558 sub paymentaccounts {
1559 $main::lxdebug->enter_sub();
1561 my ($self, $myconfig, $form) = @_;
1563 # connect to database, turn AutoCommit off
1564 my $dbh = $form->dbconnect_noauto($myconfig);
1566 my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
1568 # get A(R|P)_paid accounts
1570 qq|SELECT accno, description
1572 WHERE link LIKE '%${ARAP}_paid%'|;
1573 $form->{PR} = selectall_hashref_query($form, $dbh, $query);
1577 $main::lxdebug->leave_sub();
1581 $main::lxdebug->enter_sub();
1583 my ($self, $myconfig, $form) = @_;
1585 # connect to database, turn AutoCommit off
1586 my $dbh = $form->dbconnect_noauto($myconfig);
1590 if ($form->{db} eq 'ar') {
1591 $table = 'customer';
1603 if ($form->{department_id}) {
1604 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
1605 $where = qq| AND (t.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
1608 if ($form->{fromdate}) {
1609 $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
1611 if ($form->{todate}) {
1612 $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
1614 if (!$form->{fx_transaction}) {
1615 $where .= " AND ac.fx_transaction = '0'";
1620 if ($form->{reference}) {
1621 $reference = $dbh->quote('%' . $form->{reference} . '%');
1622 $invnumber = " AND (a.invnumber LIKE $reference)";
1623 $reference = " AND (g.reference LIKE $reference)";
1625 if ($form->{source}) {
1626 $where .= " AND (ac.source ILIKE " . $dbh->quote('%' . $form->{source} . '%') . ") ";
1628 if ($form->{memo}) {
1629 $where .= " AND (ac.memo ILIKE " . $dbh->quote('%' . $form->{memo} . '%') . ") ";
1632 my $sortorder = join(', ', qw(name invnumber ordnumber transdate source));
1633 $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(transdate invnumber name source memo)));
1635 $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
1636 my $sth = prepare_query($form, $dbh, $query);
1639 qq|SELECT c.name, a.invnumber, a.ordnumber,
1640 ac.transdate, ac.amount * $ml AS paid, ac.source,
1641 a.invoice, a.id, ac.memo, '${arap}' AS module
1643 JOIN $arap a ON (ac.trans_id = a.id)
1644 JOIN $table c ON (c.id = a.${table}_id)
1646 WHERE (ac.chart_id = ?)
1652 SELECT g.description, g.reference, NULL AS ordnumber,
1653 ac.transdate, ac.amount * $ml AS paid, ac.source,
1654 '0' as invoice, g.id, ac.memo, 'gl' AS module
1656 JOIN gl g ON (g.id = ac.trans_id)
1658 WHERE (ac.chart_id = ?)
1661 AND (ac.amount * $ml) > 0
1663 ORDER BY $sortorder|;
1664 my $sth_details = prepare_query($form, $dbh, $q_details);
1668 # cycle through each id
1669 foreach my $accno (split(/ /, $form->{paymentaccounts})) {
1670 do_statement($form, $sth, $query, $accno);
1671 my $ref = $sth->fetchrow_hashref();
1672 push(@{ $form->{PR} }, $ref);
1675 $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
1677 do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
1678 while (my $pr = $sth_details->fetchrow_hashref()) {
1679 push(@{ $form->{ $ref->{id} } }, $pr);
1681 $sth_details->finish();
1686 $main::lxdebug->leave_sub();
1690 $main::lxdebug->enter_sub();
1692 my ($self, $myconfig, $form) = @_;
1694 # connect to database
1695 my $dbh = $form->dbconnect($myconfig);
1697 my $last_period = 0;
1700 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);
1702 $form->{decimalplaces} *= 1;
1704 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
1706 # if there are any compare dates
1707 if ($form->{fromdate} || $form->{todate}) {
1709 if ($form->{fromdate}) {
1710 $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1713 $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1716 $kummfromdate = $form->{comparefromdate};
1717 $kummtodate = $form->{comparetodate};
1718 &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
1721 @periods = qw(jetzt kumm);
1722 @gesamtleistung = qw(1 2 3);
1723 @gesamtkosten = qw (10 11 12 13 14 15 16 17 18 19 20);
1725 qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
1727 foreach $key (@periods) {
1728 $form->{ "$key" . "gesamtleistung" } = 0;
1729 $form->{ "$key" . "gesamtkosten" } = 0;
1731 foreach $category (@categories) {
1733 if (defined($form->{$category}{$key})) {
1734 $form->{"$key$category"} =
1735 $form->format_amount($myconfig,
1736 $form->round_amount($form->{$category}{$key}, 2
1738 $form->{decimalplaces},
1742 foreach $item (@gesamtleistung) {
1743 $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
1745 foreach $item (@gesamtkosten) {
1746 $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
1748 $form->{ "$key" . "rohertrag" } =
1749 $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
1750 $form->{ "$key" . "betriebrohertrag" } =
1751 $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
1752 $form->{ "$key" . "betriebsergebnis" } =
1753 $form->{ "$key" . "betriebrohertrag" } -
1754 $form->{ "$key" . "gesamtkosten" };
1755 $form->{ "$key" . "neutraleraufwand" } =
1756 $form->{30}{$key} + $form->{31}{$key};
1757 $form->{ "$key" . "neutralertrag" } =
1758 $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
1759 $form->{ "$key" . "ergebnisvorsteuern" } =
1760 $form->{ "$key" . "betriebsergebnis" } -
1761 $form->{ "$key" . "neutraleraufwand" } +
1762 $form->{ "$key" . "neutralertrag" };
1763 $form->{ "$key" . "ergebnis" } =
1764 $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
1766 if ($form->{ "$key" . "gesamtleistung" } > 0) {
1767 foreach $category (@categories) {
1768 if (defined($form->{$category}{$key})) {
1769 $form->{ "$key" . "gl" . "$category" } =
1770 $form->format_amount(
1772 $form->round_amount(
1773 ($form->{$category}{$key} /
1774 $form->{ "$key" . "gesamtleistung" } * 100
1776 $form->{decimalplaces}
1778 $form->{decimalplaces},
1782 foreach $item (@ergebnisse) {
1783 $form->{ "$key" . "gl" . "$item" } =
1784 $form->format_amount($myconfig,
1785 $form->round_amount(
1786 ( $form->{ "$key" . "$item" } /
1787 $form->{ "$key" . "gesamtleistung" } * 100
1789 $form->{decimalplaces}
1791 $form->{decimalplaces},
1796 if ($form->{ "$key" . "gesamtkosten" } > 0) {
1797 foreach $category (@categories) {
1798 if (defined($form->{$category}{$key})) {
1799 $form->{ "$key" . "gk" . "$category" } =
1800 $form->format_amount($myconfig,
1801 $form->round_amount(
1802 ($form->{$category}{$key} /
1803 $form->{ "$key" . "gesamtkosten" } * 100
1805 $form->{decimalplaces}
1807 $form->{decimalplaces},
1811 foreach $item (@ergebnisse) {
1812 $form->{ "$key" . "gk" . "$item" } =
1813 $form->format_amount($myconfig,
1814 $form->round_amount(
1815 ($form->{ "$key" . "$item" } /
1816 $form->{ "$key" . "gesamtkosten" } * 100
1818 $form->{decimalplaces}
1820 $form->{decimalplaces},
1825 if ($form->{10}{$key} > 0) {
1826 foreach $category (@categories) {
1827 if (defined($form->{$category}{$key})) {
1828 $form->{ "$key" . "pk" . "$category" } =
1829 $form->format_amount(
1831 $form->round_amount(
1832 ($form->{$category}{$key} / $form->{10}{$key} * 100),
1833 $form->{decimalplaces}
1835 $form->{decimalplaces},
1839 foreach $item (@ergebnisse) {
1840 $form->{ "$key" . "pk" . "$item" } =
1841 $form->format_amount($myconfig,
1842 $form->round_amount(
1843 ($form->{ "$key" . "$item" } /
1844 $form->{10}{$key} * 100
1846 $form->{decimalplaces}
1848 $form->{decimalplaces},
1853 if ($form->{4}{$key} > 0) {
1854 foreach $category (@categories) {
1855 if (defined($form->{$category}{$key})) {
1856 $form->{ "$key" . "auf" . "$category" } =
1857 $form->format_amount(
1859 $form->round_amount(
1860 ($form->{$category}{$key} / $form->{4}{$key} * 100),
1861 $form->{decimalplaces}
1863 $form->{decimalplaces},
1867 foreach $item (@ergebnisse) {
1868 $form->{ "$key" . "auf" . "$item" } =
1869 $form->format_amount($myconfig,
1870 $form->round_amount(
1871 ($form->{ "$key" . "$item" } /
1872 $form->{4}{$key} * 100
1874 $form->{decimalplaces}
1876 $form->{decimalplaces},
1881 foreach $item (@ergebnisse) {
1882 $form->{ "$key" . "$item" } =
1883 $form->format_amount($myconfig,
1884 $form->round_amount($form->{ "$key" . "$item" },
1885 $form->{decimalplaces}
1887 $form->{decimalplaces},
1894 $main::lxdebug->leave_sub();
1898 $main::lxdebug->enter_sub();
1900 my ($self, $myconfig, $form) = @_;
1902 # connect to database
1903 my $dbh = $form->dbconnect($myconfig);
1905 my $last_period = 0;
1906 my @categories_cent = qw(51r 511 86r 861 97r 971 93r 931
1907 96 66 43 45 53 62 65 67);
1908 my @categories_euro = qw(48 51 86 91 97 93 94);
1909 $form->{decimalplaces} *= 1;
1911 foreach $item (@categories_cent) {
1912 $form->{"$item"} = 0;
1914 foreach $item (@categories_euro) {
1915 $form->{"$item"} = 0;
1918 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_ustva");
1920 # foreach $item (@categories_cent) {
1921 # if ($form->{$item}{"jetzt"} > 0) {
1922 # $form->{$item} = $form->{$item}{"jetzt"};
1923 # delete $form->{$item}{"jetzt"};
1926 # foreach $item (@categories_euro) {
1927 # if ($form->{$item}{"jetzt"} > 0) {
1928 # $form->{$item} = $form->{$item}{"jetzt"};
1929 # delete $form->{$item}{"jetzt"};
1930 # } foreach $item (@categories_cent) {
1931 # if ($form->{$item}{"jetzt"} > 0) {
1932 # $form->{$item} = $form->{$item}{"jetzt"};
1933 # delete $form->{$item}{"jetzt"};
1936 # foreach $item (@categories_euro) {
1937 # if ($form->{$item}{"jetzt"} > 0) {
1938 # $form->{$item} = $form->{$item}{"jetzt"};
1939 # delete $form->{$item}{"jetzt"};
1946 # Berechnung der USTVA Formularfelder
1948 $form->{"51r"} = $form->{"511"};
1949 $form->{"86r"} = $form->{"861"};
1950 $form->{"97r"} = $form->{"971"};
1951 $form->{"93r"} = $form->{"931"};
1953 #$form->{"96"} = $form->{"94"} * 0.16;
1955 $form->{"51r"} + $form->{"86r"} + $form->{"97r"} + $form->{"93r"} +
1957 $form->{"45"} = $form->{"43"};
1958 $form->{"53"} = $form->{"43"};
1959 $form->{"62"} = $form->{"43"} - $form->{"66"};
1960 $form->{"65"} = $form->{"43"} - $form->{"66"};
1961 $form->{"67"} = $form->{"43"} - $form->{"66"};
1963 foreach $item (@categories_cent) {
1965 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),
1969 foreach $item (@categories_euro) {
1971 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0),
1977 $main::lxdebug->leave_sub();
1980 sub income_statement {
1981 $main::lxdebug->enter_sub();
1983 my ($self, $myconfig, $form) = @_;
1985 # connect to database
1986 my $dbh = $form->dbconnect($myconfig);
1988 my $last_period = 0;
1989 my @categories_einnahmen = qw(1 2 3 4 5 6 7);
1990 my @categories_ausgaben =
1991 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);
1993 my @ergebnisse = qw(sumeura sumeurb guvsumme);
1995 $form->{decimalplaces} *= 1;
1997 foreach $item (@categories_einnahmen) {
2000 foreach $item (@categories_ausgaben) {
2004 foreach $item (@ergebnisse) {
2008 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
2011 foreach $item (@categories_einnahmen) {
2012 $form->{"eur${item}"} =
2013 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
2014 $form->{"sumeura"} += $form->{$item};
2016 foreach $item (@categories_ausgaben) {
2017 $form->{"eur${item}"} =
2018 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
2019 $form->{"sumeurb"} += $form->{$item};
2022 $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
2024 foreach $item (@ergebnisse) {
2026 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
2028 $main::lxdebug->leave_sub();