1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
15 # Contributors: Benjamin Lee <benjaminlee@consultant.com>
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
31 # backend code for reports
33 #======================================================================
39 use List::Util qw(sum);
44 # new implementation of balance sheet
47 # stuff missing from the original implementation:
50 # - proper testing for heading charts
51 # - transmission from $form to TMPL realm is not as clear as i'd like
53 sub get_openbalance_date {
54 my ($closedto, $target) = map { $::locale->parse_date_to_object(\%::myconfig, $_) } @_;
56 return unless $closedto;
58 $closedto->subtract(years => 1) while ($target - $closedto)->is_negative;
59 $closedto->add(days => 1);
60 return $::locale->format_date(\%::myconfig, $closedto);
64 $main::lxdebug->enter_sub();
66 my $myconfig = \%main::myconfig;
67 my $form = $main::form;
68 my $dbh = $::form->get_standard_dbh;
71 my @categories = qw(A C L Q);
73 # if there are any dates construct a where
74 if ($form->{asofdate}) {
75 $form->{period} = $form->{this_period} = conv_dateq($form->{asofdate});
78 # get end of financial year and convert to Date format
79 my ($closedto) = selectfirst_array_query($form, $dbh, 'SELECT closedto FROM defaults');
81 # get date of last opening balance
82 my $startdate = get_openbalance_date($closedto, $form->{asofdate});
84 get_accounts($dbh, $last_period, $startdate, $form->{asofdate}, $form, \@categories);
86 # if there are any compare dates
87 if ($form->{compareasofdate}) {
90 $startdate = get_openbalance_date($closedto, $form->{compareasofdate});
92 get_accounts($dbh, $last_period, $startdate, $form->{compareasofdate}, $form, \@categories);
93 $form->{last_period} = conv_dateq($form->{compareasofdate});
96 # now we got $form->{A}{accno}{ } assets
97 # and $form->{L}{accno}{ } liabilities
98 # and $form->{Q}{accno}{ } equity
99 # build asset accounts
101 my %account = ('A' => { 'ml' => -1 },
102 'L' => { 'ml' => 1 },
103 'Q' => { 'ml' => 1 });
107 foreach my $category (grep { !/C/ } @categories) {
109 $TMPL_DATA->{$category} = [];
110 my $ml = $account{$category}{ml};
112 foreach my $key (sort keys %{ $form->{$category} }) {
114 my $row = { %{ $form->{$category}{$key} } };
116 # if charttype "heading" - calculate this entry, start a new batch of charts belonging to this heading and skip the rest bo the loop
117 # header charts are not real charts. start a sub aggregation with them, but don't calculate anything with them
118 if ($row->{charttype} eq "H") {
119 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
120 $row->{subdescription} = $account{$category}{subdescription};
121 $row->{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
122 $row->{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
125 $row->{subheader} = 1;
126 $account{$category}{subthis} = $row->{this};
127 $account{$category}{sublast} = $row->{last};
128 $account{$category}{subdescription} = $row->{description};
129 $account{$category}{subtotal} = 1;
134 next unless $form->{l_heading};
137 for my $period (qw(this last)) {
138 next if ($period eq 'last' && !$last_period);
140 $row->{$period} *= $ml;
143 push @{ $TMPL_DATA->{$category} }, $row;
146 # resolve heading/subtotal
147 if ($account{$category}{subtotal} && $form->{l_subtotal}) {
148 $TMPL_DATA->{$category}[-1]{subdescription} = $account{$category}{subdescription};
149 $TMPL_DATA->{$category}[-1]{this} = $account{$category}{subthis} * $ml; # format: $dec, $dash
150 $TMPL_DATA->{$category}[-1]{last} = $account{$category}{sublast} * $ml if $last_period; # format: $dec, $dash
153 $TMPL_DATA->{total}{$category}{this} = sum map { $_->{this} } @{ $TMPL_DATA->{$category} };
154 $TMPL_DATA->{total}{$category}{last} = sum map { $_->{last} } @{ $TMPL_DATA->{$category} };
157 for my $period (qw(this last)) {
158 next if ($period eq 'last' && !$last_period);
160 $form->{E}{$period} = $TMPL_DATA->{total}{A}{$period} - $TMPL_DATA->{total}{L}{$period} - $TMPL_DATA->{total}{Q}{$period};
161 $TMPL_DATA->{total}{Q}{$period} += $form->{E}{$period};
162 $TMPL_DATA->{total}{$period} = $TMPL_DATA->{total}{L}{$period} + $TMPL_DATA->{total}{Q}{$period};
164 $form->{E}{description}='nicht verbuchter Gewinn/Verlust';
165 push @{ $TMPL_DATA->{Q} }, $form->{E};
167 $main::lxdebug->leave_sub();
173 $main::lxdebug->enter_sub();
175 my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
177 my ($null, $department_id) = split /--/, $form->{department};
188 my $dec = $form->{decimalplaces};
190 my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |;
194 qq|SELECT c.accno, c.description, c.category
196 WHERE (c.charttype = 'H')
200 $sth = prepare_execute_query($form, $dbh, $query);
202 my @headingaccounts = ();
203 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
204 $form->{ $ref->{category} }{ $ref->{accno} }{description} =
205 "$ref->{description}";
206 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
207 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
209 push @headingaccounts, $ref->{accno};
215 $fromdate = conv_dateq($fromdate);
216 if ($form->{method} eq 'cash') {
217 $subwhere .= " AND (transdate >= $fromdate)";
218 $glwhere = " AND (ac.transdate >= $fromdate)";
220 $where .= " AND (ac.transdate >= $fromdate)";
225 $todate = conv_dateq($todate);
226 $where .= " AND (ac.transdate <= $todate)";
227 $subwhere .= " AND (transdate <= $todate)";
230 if ($department_id) {
231 $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
232 $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|)|;
235 if ($form->{project_id}) {
236 # Diese Bedingung wird derzeit niemals wahr sein, da man in Bericht->Bilanz keine
237 # Projekte auswählen kann
238 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
241 if ($form->{method} eq 'cash') {
243 qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
245 JOIN chart c ON (c.id = ac.chart_id)
246 JOIN ar a ON (a.id = ac.trans_id)
255 JOIN chart c ON (a.chart_id = c.id)
256 WHERE (link LIKE '%AR_paid%')
260 GROUP BY c.accno, c.description, c.category
264 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
266 JOIN chart c ON (c.id = ac.chart_id)
267 JOIN ap a ON (a.id = ac.trans_id)
276 JOIN chart c ON (a.chart_id = c.id)
277 WHERE (link LIKE '%AP_paid%')
281 GROUP BY c.accno, c.description, c.category
285 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
287 JOIN chart c ON (c.id = ac.chart_id)
288 JOIN gl a ON (a.id = ac.trans_id)
294 AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
296 GROUP BY c.accno, c.description, c.category |;
298 if ($form->{project_id}) {
299 # s.o. keine Projektauswahl in Bilanz
304 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
306 JOIN ar a ON (a.id = ac.trans_id)
307 JOIN parts p ON (ac.parts_id = p.id)
308 JOIN chart c on (p.income_accno_id = c.id)
310 -- use transdate from subwhere
311 WHERE (c.category = 'I')
318 JOIN chart c ON (a.chart_id = c.id)
319 WHERE (link LIKE '%AR_paid%')
323 GROUP BY c.accno, c.description, c.category
327 SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
329 JOIN ap a ON (a.id = ac.trans_id)
330 JOIN parts p ON (ac.parts_id = p.id)
331 JOIN chart c on (p.expense_accno_id = c.id)
333 WHERE (c.category = 'E')
340 JOIN chart c ON (a.chart_id = c.id)
341 WHERE link LIKE '%AP_paid%'
345 GROUP BY c.accno, c.description, c.category |;
348 } else { # if ($form->{method} eq 'cash')
349 if ($department_id) {
350 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
351 $dpt_where = qq| AND t.department_id = | . conv_i($department_id);
355 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
357 JOIN chart c ON (c.id = ac.chart_id)
363 GROUP BY c.accno, c.description, c.category |;
365 if ($form->{project_id}) {
366 # s.o. keine Projektauswahl in Bilanz
370 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
372 JOIN ar a ON (a.id = ac.trans_id)
373 JOIN parts p ON (ac.parts_id = p.id)
374 JOIN chart c on (p.income_accno_id = c.id)
376 -- use transdate from subwhere
377 WHERE (c.category = 'I')
381 GROUP BY c.accno, c.description, c.category
385 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
387 JOIN ap a ON (a.id = ac.trans_id)
388 JOIN parts p ON (ac.parts_id = p.id)
389 JOIN chart c on (p.expense_accno_id = c.id)
391 WHERE (c.category = 'E')
395 GROUP BY c.accno, c.description, c.category |;
403 $sth = prepare_execute_query($form, $dbh, $query);
405 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
407 if ($ref->{category} eq 'C') {
408 $ref->{category} = 'A';
411 # get last heading account
412 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
416 $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
418 $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
422 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
423 $form->{ $ref->{category} }{ $ref->{accno} }{description} = $ref->{description};
424 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
427 $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
429 $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
434 # remove accounts with zero balance
435 foreach $category (@{$categories}) {
436 foreach $accno (keys %{ $form->{$category} }) {
437 $form->{$category}{$accno}{last} = $form->round_amount($form->{$category}{$accno}{last}, $dec);
438 $form->{$category}{$accno}{this} = $form->round_amount($form->{$category}{$accno}{this}, $dec);
440 delete $form->{$category}{$accno}
441 if ( $form->{$category}{$accno}{this} == 0
442 && $form->{$category}{$accno}{last} == 0);
446 $main::lxdebug->leave_sub();
450 $main::lxdebug->enter_sub();
452 my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
454 my ($null, $department_id) = split /--/, $form->{department};
468 $fromdate = conv_dateq($fromdate);
469 if ($form->{method} eq 'cash') {
470 $subwhere .= " AND (transdate >= $fromdate)";
471 $glwhere = " AND (ac.transdate >= $fromdate)";
472 $prwhere = " AND (a.transdate >= $fromdate)";
473 $inwhere = " AND (acc.transdate >= $fromdate)";
475 $where .= " AND (ac.transdate >= $fromdate)";
480 $todate = conv_dateq($todate);
481 $subwhere .= " AND (transdate <= $todate)";
482 $where .= " AND (ac.transdate <= $todate)";
483 $prwhere .= " AND (a.transdate <= $todate)";
484 $inwhere .= " AND (acc.transdate <= $todate)";
487 if ($department_id) {
488 $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
489 $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|) |;
492 if ($form->{project_id}) {
493 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
497 # GUV patch by Ronny Rentner (Bug 1190)
499 # GUV IST-Versteuerung
501 # Alle tatsaechlichen _Zahlungseingaenge_
502 # im Zeitraum erfassen
503 # (Teilzahlungen werden prozentual auf verschiedene Steuern aufgeteilt)
507 if ($form->{method} eq 'cash') {
510 SELECT SUM( ac.amount *
511 (SELECT SUM(acc.amount) * -1
513 INNER JOIN chart c ON (acc.chart_id = c.id AND c.link LIKE '%AR_paid%')
514 WHERE 1=1 $inwhere AND acc.trans_id = ac.trans_id)
515 / (SELECT amount FROM ar WHERE id = ac.trans_id)
516 ) AS amount, c.pos_eur
518 LEFT JOIN chart c ON (c.id = ac.chart_id)
519 LEFT JOIN ar ON (ar.id = ac.trans_id)
520 LEFT JOIN taxkeys tk ON (tk.id = (
521 SELECT id FROM taxkeys
522 WHERE chart_id = ac.chart_id
523 AND startdate <= COALESCE(ar.deliverydate,ar.transdate)
524 ORDER BY startdate DESC LIMIT 1
527 WHERE ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE 1=1 $subwhere)
532 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
534 JOIN chart c ON (c.id = ac.chart_id)
535 JOIN ar a ON (a.id = ac.trans_id)
537 WHERE $where $dpt_where
538 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a JOIN chart c ON (a.chart_id = c.id) WHERE (link LIKE '%AR_paid%') $subwhere)
544 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
546 JOIN chart c ON (c.id = ac.chart_id)
547 JOIN ap a ON (a.id = ac.trans_id)
549 WHERE $where $dpt_where
550 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a JOIN chart c ON (a.chart_id = c.id) WHERE (link LIKE '%AP_paid%') $subwhere)
556 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
558 JOIN chart c ON (c.id = ac.chart_id)
559 JOIN gl a ON (a.id = ac.trans_id)
561 WHERE $where $dpt_where $glwhere
562 AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
567 if ($form->{project_id}) {
571 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
573 JOIN ar a ON (a.id = ac.trans_id)
574 JOIN parts p ON (ac.parts_id = p.id)
575 JOIN chart c on (p.income_accno_id = c.id)
577 WHERE (c.category = 'I') $prwhere $dpt_where
578 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a JOIN chart c ON (a.chart_id = c.id) WHERE (link LIKE '%AR_paid%') $subwhere)
584 SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category
586 JOIN ap a ON (a.id = ac.trans_id)
587 JOIN parts p ON (ac.parts_id = p.id)
588 JOIN chart c on (p.expense_accno_id = c.id)
590 WHERE (c.category = 'E') $prwhere $dpt_where
591 AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a JOIN chart c ON (a.chart_id = c.id) WHERE (link LIKE '%AP_paid%') $subwhere)
597 } else { # if ($form->{method} eq 'cash')
598 if ($department_id) {
599 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
600 $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
604 SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
606 JOIN chart c ON (c.id = ac.chart_id)
611 GROUP BY c.$category |;
613 if ($form->{project_id}) {
617 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
619 JOIN ar a ON (a.id = ac.trans_id)
620 JOIN parts p ON (ac.parts_id = p.id)
621 JOIN chart c on (p.income_accno_id = c.id)
623 WHERE (c.category = 'I')
631 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
633 JOIN ap a ON (a.id = ac.trans_id)
634 JOIN parts p ON (ac.parts_id = p.id)
635 JOIN chart c on (p.expense_accno_id = c.id)
637 WHERE (c.category = 'E')
641 GROUP BY c.$category |;
649 foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
650 if ($category eq "pos_bwa") {
652 $form->{ $ref->{$category} }{kumm} += $ref->{amount};
654 $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
657 $form->{ $ref->{$category} } += $ref->{amount};
661 $main::lxdebug->leave_sub();
665 $main::lxdebug->enter_sub();
667 my ($self, $myconfig, $form, %options) = @_;
669 my $dbh = $form->dbconnect($myconfig);
671 my ($query, $sth, $ref);
674 my ($null, $department_id) = split /--/, $form->{department};
675 my @headingaccounts = ();
681 my $invwhere = $where;
683 if ($department_id) {
684 $dpt_join = qq| JOIN dpt_trans t ON (ac.trans_id = t.trans_id) |;
685 $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
688 # project_id only applies to getting transactions
689 # it has nothing to do with a trial balance
690 # but we use the same function to collect information
692 if ($form->{project_id}) {
693 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
696 my $acc_cash_where = "";
697 # my $ar_cash_where = "";
698 # my $ap_cash_where = "";
701 if ($form->{method} eq "cash") {
703 qq| AND (ac.trans_id IN (
706 WHERE datepaid >= '$form->{fromdate}'
707 AND datepaid <= '$form->{todate}'
713 WHERE datepaid >= '$form->{fromdate}'
714 AND datepaid <= '$form->{todate}'
720 WHERE transdate >= '$form->{fromdate}'
721 AND transdate <= '$form->{todate}'
723 # $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
726 if ($options{beginning_balances}) {
727 foreach my $prefix (qw(from to)) {
728 next if ($form->{"${prefix}date"});
730 my $min_max = $prefix eq 'from' ? 'min' : 'max';
731 $query = qq|SELECT ${min_max}(transdate)
737 ($form->{"${prefix}date"}) = selectfirst_array_query($form, $dbh, $query);
740 # get beginning balances
742 qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
744 LEFT JOIN chart c ON (ac.chart_id = c.id)
746 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
749 GROUP BY c.accno, c.category, c.description |;
751 $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
753 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
755 if ($ref->{amount} != 0 || $form->{all_accounts}) {
756 $trb{ $ref->{accno} }{description} = $ref->{description};
757 $trb{ $ref->{accno} }{charttype} = 'A';
758 $trb{ $ref->{accno} }{beginning_balance} = $ref->{amount};
760 if ($ref->{amount} > 0) {
761 $trb{ $ref->{accno} }{haben_eb} = $ref->{amount};
763 $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1;
765 $trb{ $ref->{accno} }{category} = $ref->{category};
774 qq|SELECT c.accno, c.description, c.category
776 WHERE c.charttype = 'H'
779 $sth = prepare_execute_query($form, $dbh, $query);
781 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
782 $trb{ $ref->{accno} }{description} = $ref->{description};
783 $trb{ $ref->{accno} }{charttype} = 'H';
784 $trb{ $ref->{accno} }{category} = $ref->{category};
786 push @headingaccounts, $ref->{accno};
792 my $saldowhere = " 1 = 1 ";
793 my $sumwhere = " 1 = 1 ";
795 my $sumsubwhere = '';
796 my $saldosubwhere = '';
797 my $glsaldowhere = '';
802 my ($fromdate, $todate);
804 if ($form->{fromdate} || $form->{todate}) {
805 if ($form->{fromdate}) {
806 $fromdate = conv_dateq($form->{fromdate});
807 $tofrom .= " AND (ac.transdate >= $fromdate)";
808 $subwhere .= " AND (ac.transdate >= $fromdate)";
809 $sumsubwhere .= " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
810 $saldosubwhere .= " AND (ac,transdate>=(select date_trunc('year', date $fromdate))) ";
811 $invwhere .= " AND (a.transdate >= $fromdate)";
812 $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
813 $glwhere = " AND (ac.transdate >= $fromdate)";
814 $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
816 if ($form->{todate}) {
817 $todate = conv_dateq($form->{todate});
818 $tofrom .= " AND (ac.transdate <= $todate)";
819 $invwhere .= " AND (a.transdate <= $todate)";
820 $saldosubwhere .= " AND (ac.transdate <= $todate)";
821 $sumsubwhere .= " AND (ac.transdate <= $todate)";
822 $subwhere .= " AND (ac.transdate <= $todate)";
823 $glwhere .= " AND (ac.transdate <= $todate)";
824 $glsumwhere .= " AND (ac.transdate <= $todate) ";
825 $glsaldowhere .= " AND (ac.transdate <= $todate) ";
829 if ($form->{method} eq "cash") {
831 qq| AND(ac.trans_id IN (SELECT id FROM ar WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM ap WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM gl WHERE transdate>= $fromdate AND transdate<= $todate)) AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL) |;
832 $saldowhere .= qq| AND(ac.trans_id IN (SELECT id FROM ar WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM ap WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM gl WHERE transdate>= $fromdate AND transdate<= $todate)) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL) |;
834 $sumwhere .= qq| AND(ac.trans_id IN (SELECT id FROM ar WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM ap WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM gl WHERE transdate>= $fromdate AND transdate<= $todate)) AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL) |;
836 $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
837 $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
838 $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
842 SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
844 JOIN chart c ON (c.id = ac.chart_id)
849 GROUP BY c.accno, c.description, c.category |;
851 if ($form->{project_id}) {
853 -- add project transactions from invoice
857 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
859 JOIN ar a ON (ac.trans_id = a.id)
860 JOIN parts p ON (ac.parts_id = p.id)
861 JOIN chart c ON (p.income_accno_id = c.id)
866 GROUP BY c.accno, c.description, c.category
870 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
872 JOIN ap a ON (ac.trans_id = a.id)
873 JOIN parts p ON (ac.parts_id = p.id)
874 JOIN chart c ON (p.expense_accno_id = c.id)
879 GROUP BY c.accno, c.description, c.category
883 $query .= qq| ORDER BY accno|;
885 $sth = prepare_execute_query($form, $dbh, $query);
887 # calculate the debit and credit in the period
888 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
889 $trb{ $ref->{accno} }{description} = $ref->{description};
890 $trb{ $ref->{accno} }{charttype} = 'A';
891 $trb{ $ref->{accno} }{category} = $ref->{category};
892 $trb{ $ref->{accno} }{amount} += $ref->{amount};
896 # prepare query for each account
897 my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
901 (SELECT SUM(ac.amount) * -1
903 JOIN chart c ON (c.id = ac.chart_id)
909 AND (c.accno = ?)) AS debit,
911 (SELECT SUM(ac.amount)
913 JOIN chart c ON (c.id = ac.chart_id)
919 AND c.accno = ?) AS credit,
920 (SELECT SUM(ac.amount)
922 JOIN chart c ON (ac.chart_id = c.id)
927 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
929 (SELECT SUM(ac.amount)
931 JOIN chart c ON (ac.chart_id = c.id)
937 AND c.accno = ?) AS sum_credit,
939 (SELECT SUM(ac.amount)
941 JOIN chart c ON (ac.chart_id = c.id)
947 AND c.accno = ?) AS sum_debit,
949 (SELECT max(ac.transdate) FROM acc_trans ac
950 JOIN chart c ON (ac.chart_id = c.id)
955 AND c.accno = ?) AS last_transaction
960 $drcr = prepare_query($form, $dbh, $q_drcr);
962 if ($form->{project_id}) {
963 # prepare query for each account
966 (SELECT SUM(ac.sellprice * ac.qty) * -1
968 JOIN parts p ON (ac.parts_id = p.id)
969 JOIN ap a ON (ac.trans_id = a.id)
970 JOIN chart c ON (p.expense_accno_id = c.id)
975 AND c.accno = ?) AS debit,
977 (SELECT SUM(ac.sellprice * ac.qty)
979 JOIN parts p ON (ac.parts_id = p.id)
980 JOIN ar a ON (ac.trans_id = a.id)
981 JOIN chart c ON (p.income_accno_id = c.id)
986 AND c.accno = ?) AS credit,
988 (SELECT SUM(ac.amount)
990 JOIN chart c ON (ac.chart_id = c.id)
995 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
997 (SELECT SUM(ac.amount)
999 JOIN chart c ON (ac.chart_id = c.id)
1005 AND c.accno = ?) AS sum_credit,
1007 (SELECT SUM(ac.amount)
1009 JOIN chart c ON (ac.chart_id = c.id)
1015 AND c.accno = ?) AS sum_debit,
1018 (SELECT max(ac.transdate) FROM acc_trans ac
1019 JOIN chart c ON (ac.chart_id = c.id)
1024 AND c.accno = ?) AS last_transaction
1027 $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
1031 my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
1033 foreach my $accno (sort keys %trb) {
1036 $ref->{accno} = $accno;
1037 map { $ref->{$_} = $trb{$accno}{$_} }
1038 qw(description category charttype amount soll_eb haben_eb beginning_balance);
1040 $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
1042 if ($trb{$accno}{charttype} eq 'A') {
1045 do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1047 ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
1048 my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
1049 $last_transaction = "";
1050 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
1051 $ref->{debit} += $debit;
1052 $ref->{credit} += $credit;
1054 $ref->{haben_saldo} += $saldo;
1056 $ref->{soll_saldo} += $saldo * -1;
1058 $ref->{last_transaction} = $last_transaction;
1059 $ref->{soll_kumuliert} = $soll_kumuliert * -1;
1060 $ref->{haben_kumuliert} = $haben_kumuliert;
1064 if ($form->{project_id}) {
1067 do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1069 ($debit, $credit) = (0, 0);
1070 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
1071 $ref->{debit} += $debit;
1072 $ref->{credit} += $credit;
1074 $ref->{haben_saldo} += $saldo;
1076 $ref->{soll_saldo} += $saldo * -1;
1078 $ref->{soll_kumuliert} += $soll_kumuliert * -1;
1079 $ref->{haben_kumuliert} += $haben_kumuliert;
1081 $project_drcr->finish;
1084 $ref->{debit} = $form->round_amount($ref->{debit}, 2);
1085 $ref->{credit} = $form->round_amount($ref->{credit}, 2);
1087 if ($ref->{haben_saldo} != 0) {
1088 $ref->{haben_saldo} = $ref->{haben_saldo} + $ref->{beginning_balance};
1089 if ($ref->{haben_saldo} < 0) {
1090 $ref->{soll_saldo} = $form->round_amount(($ref->{haben_saldo} *- 1), 2);
1091 $ref->{haben_saldo} = 0;
1094 $ref->{soll_saldo} = $ref->{soll_saldo} - $ref->{beginning_balance};
1095 if ($ref->{soll_saldo} < 0) {
1096 $ref->{haben_saldo} = $form->round_amount(($ref->{soll_saldo} * -1), 2);
1097 $ref->{soll_saldo} = 0;
1100 $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
1101 $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
1102 $ref->{haben_kumuliert} = $form->round_amount($ref->{haben_kumuliert}, 2);
1103 $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
1108 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1109 $accno = pop @accno;
1111 $trb{$accno}{debit} += $ref->{debit};
1112 $trb{$accno}{credit} += $ref->{credit};
1113 $trb{$accno}{soll_saldo} += $ref->{soll_saldo};
1114 $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
1115 $trb{$accno}{soll_kumuliert} += $ref->{soll_kumuliert};
1116 $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
1119 push @{ $form->{TB} }, $ref;
1125 # debits and credits for headings
1126 foreach my $accno (@headingaccounts) {
1127 foreach $ref (@{ $form->{TB} }) {
1128 if ($accno eq $ref->{accno}) {
1129 $ref->{debit} = $trb{$accno}{debit};
1130 $ref->{credit} = $trb{$accno}{credit};
1131 $ref->{soll_saldo} = $trb{$accno}{soll_saldo};
1132 $ref->{haben_saldo} = $trb{$accno}{haben_saldo};
1133 $ref->{soll_kumuliert} = $trb{$accno}{soll_kumuliert};
1134 $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert};
1139 $main::lxdebug->leave_sub();
1143 $main::lxdebug->enter_sub();
1144 my ($self, $dbh, $form) = @_;
1145 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
1146 my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
1147 my $sth = $dbh->prepare($query);
1148 while(my $ref = $sth->fetchrow_hashref()) {
1149 $ref->{invnumer} =~ s/Storno zu //g;
1150 $form->{storno}{$ref->{invnumber}} = 1;
1152 $main::lxdebug->leave_sub();
1156 $main::lxdebug->enter_sub();
1158 my ($self, $myconfig, $form) = @_;
1160 # connect to database
1161 my $dbh = $form->dbconnect($myconfig);
1163 my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
1165 if ($form->{ct} eq "customer") {
1178 $ct_id = "${ct}_id";
1180 $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
1181 my $todate = conv_dateq($form->{todate});
1182 my $fromdate = conv_dateq($form->{fromdate});
1184 my $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
1186 my $where = " 1 = 1 ";
1189 if ($form->{$ct_id}) {
1190 $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
1191 } elsif ($form->{ $form->{ct} }) {
1192 $where .= qq| AND (ct.name ILIKE | . $dbh->quote('%' . $form->{$ct} . '%') . qq|)|;
1196 if ($form->{department}) {
1197 my ($null, $department_id) = split /--/, $form->{department};
1198 $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
1199 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1201 my $review_of_aging_list;
1202 if ($form->{review_of_aging_list}) {
1203 if ($form->{review_of_aging_list} =~ m "-"){
1204 my @period = split(/-/, $form->{review_of_aging_list});
1205 $review_of_aging_list = " AND $period[0] < date_part('days', now() - duedate)
1206 AND date_part('days', now() - duedate) < $period[1]";
1208 $form->{review_of_aging_list} =~ s/[^0-9]//g;
1209 $review_of_aging_list = " AND $form->{review_of_aging_list} < date_part('days', now() - duedate)";
1215 SELECT ${ct}.id AS ctid, ${ct}.name,
1216 street, zipcode, city, country, contact, email,
1217 phone as customerphone, fax as customerfax, ${ct}number,
1218 "invnumber", "transdate",
1219 (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",
1220 "duedate", invoice, ${arap}.id,
1223 WHERE (${arap}.curr = exchangerate.curr)
1224 AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
1226 WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
1227 AND NOT COALESCE (${arap}.storno, 'f')
1228 AND (${arap}.${ct}_id = ${ct}.id)
1230 AND (transdate <= (date $todate) $fromwhere )
1231 $review_of_aging_list
1232 ORDER BY ctid, transdate, invnumber |;
1234 my $sth_details = prepare_query($form, $dbh, $q_details);
1236 # select outstanding vendors or customers, depends on $ct
1238 qq|SELECT DISTINCT ct.id, ct.name
1239 FROM $ct ct, $arap a
1242 AND (a.${ct_id} = ct.id)
1243 AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
1244 AND (a.transdate <= $todate $fromwhere)
1247 my $sth = prepare_execute_query($form, $dbh, $query);
1250 # for each company that has some stuff outstanding
1251 while (my ($id) = $sth->fetchrow_array) {
1252 do_statement($form, $sth_details, $q_details, $id);
1254 while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
1255 $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
1256 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
1257 push @{ $form->{AG} }, $ref;
1260 $sth_details->finish;
1269 $main::lxdebug->leave_sub();
1273 $main::lxdebug->enter_sub();
1275 my ($self, $myconfig, $form) = @_;
1277 # connect to database
1278 my $dbh = $form->dbconnect($myconfig);
1280 my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
1283 qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
1286 ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
1287 selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
1290 $main::lxdebug->leave_sub();
1293 sub get_taxaccounts {
1294 $main::lxdebug->enter_sub();
1296 my ($self, $myconfig, $form) = @_;
1298 # connect to database
1299 my $dbh = $form->dbconnect($myconfig);
1303 qq|SELECT c.accno, c.description, t.rate
1305 WHERE (c.link LIKE '%CT_tax%') AND (c.id = t.chart_id)
1307 $form->{taxaccounts} = selectall_hashref_quert($form, $dbh, $query);
1311 $main::lxdebug->leave_sub();
1315 $main::lxdebug->enter_sub();
1317 my ($self, $myconfig, $form) = @_;
1319 # connect to database
1320 my $dbh = $form->dbconnect($myconfig);
1322 my ($null, $department_id) = split /--/, $form->{department};
1325 my $where = "1 = 1";
1327 if ($department_id) {
1328 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
1333 if ($form->{accno}) {
1334 $accno = $form->{accno};
1335 $rate = $form->{"$form->{accno}_rate"};
1336 $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
1342 if ($form->{db} eq 'ar') {
1343 $table = "customer";
1350 my $arap = lc($ARAP);
1352 my $transdate = "a.transdate";
1354 if ($form->{method} eq 'cash') {
1355 $transdate = "a.datepaid";
1357 my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
1364 JOIN chart c ON (a.chart_id = c.id)
1365 WHERE (link LIKE '%${ARAP}_paid%')
1366 AND (transdate <= $todate)
1371 # if there are any dates construct a where
1372 $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
1373 $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
1375 my $ml = ($form->{db} eq 'ar') ? 1 : -1;
1377 my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
1378 $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
1381 if ($form->{report} !~ /nontaxable/) {
1383 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
1384 ac.amount * $ml AS tax
1386 JOIN ${arap} a ON (a.id = ac.trans_id)
1387 JOIN chart ch ON (ch.id = ac.chart_id)
1388 JOIN $table n ON (n.id = a.${table}_id)
1392 AND (a.invoice = '0')
1396 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
1397 i.sellprice * i.qty * $rate * $ml AS tax
1399 JOIN ${arap} a ON (a.id = ac.trans_id)
1400 JOIN chart ch ON (ch.id = ac.chart_id)
1401 JOIN $table n ON (n.id = a.${table}_id)
1402 JOIN ${table}tax t ON (t.${table}_id = n.id)
1403 JOIN invoice i ON (i.trans_id = a.id)
1404 JOIN partstax p ON (p.parts_id = i.parts_id)
1408 AND (a.invoice = '1')
1409 ORDER BY $sortorder|;
1411 # only gather up non-taxable transactions
1413 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount
1415 JOIN ${arap} a ON (a.id = ac.trans_id)
1416 JOIN $table n ON (n.id = a.${table}_id)
1419 AND (a.invoice = '0')
1420 AND (a.netamount = a.amount)
1424 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount
1426 JOIN ${arap} a ON (a.id = ac.trans_id)
1427 JOIN $table n ON (n.id = a.${table}_id)
1428 JOIN invoice i ON (i.trans_id = a.id)
1431 AND (a.invoice = '1')
1433 a.${table}_id NOT IN (SELECT ${table}_id FROM ${table}tax t (${table}_id))
1435 i.parts_id NOT IN (SELECT parts_id FROM partstax p (parts_id))
1437 GROUP BY a.id, a.invnumber, $transdate, n.name, i.sellprice, i.qty
1438 ORDER by $sortorder|;
1441 $form->{TR} = selectall_hashref_query($form, $dbh, $query);
1445 $main::lxdebug->leave_sub();
1448 sub paymentaccounts {
1449 $main::lxdebug->enter_sub();
1451 my ($self, $myconfig, $form) = @_;
1453 # connect to database, turn AutoCommit off
1454 my $dbh = $form->dbconnect_noauto($myconfig);
1456 my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
1458 # get A(R|P)_paid accounts
1460 qq|SELECT accno, description
1462 WHERE link LIKE '%${ARAP}_paid%'|;
1463 $form->{PR} = selectall_hashref_query($form, $dbh, $query);
1467 $main::lxdebug->leave_sub();
1471 $main::lxdebug->enter_sub();
1473 my ($self, $myconfig, $form) = @_;
1475 # connect to database, turn AutoCommit off
1476 my $dbh = $form->dbconnect_noauto($myconfig);
1481 if ($form->{db} eq 'ar') {
1482 $table = 'customer';
1494 if ($form->{department_id}) {
1495 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
1496 $where = qq| AND (t.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
1499 if ($form->{fromdate}) {
1500 $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
1502 if ($form->{todate}) {
1503 $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
1505 if (!$form->{fx_transaction}) {
1506 $where .= " AND ac.fx_transaction = '0'";
1511 if ($form->{reference}) {
1512 $reference = $dbh->quote('%' . $form->{reference} . '%');
1513 $invnumber = " AND (a.invnumber LIKE $reference)";
1514 $reference = " AND (g.reference LIKE $reference)";
1516 if ($form->{source}) {
1517 $where .= " AND (ac.source ILIKE " . $dbh->quote('%' . $form->{source} . '%') . ") ";
1519 if ($form->{memo}) {
1520 $where .= " AND (ac.memo ILIKE " . $dbh->quote('%' . $form->{memo} . '%') . ") ";
1523 my %sort_columns = (
1524 'transdate' => [ qw(transdate lower_invnumber lower_name) ],
1525 'invnumber' => [ qw(lower_invnumber lower_name transdate) ],
1526 'name' => [ qw(lower_name transdate) ],
1527 'source' => [ qw(lower_source) ],
1528 'memo' => [ qw(lower_memo) ],
1530 my %lowered_columns = (
1531 'invnumber' => { 'gl' => 'g.reference', 'arap' => 'a.invnumber', },
1532 'memo' => { 'gl' => 'ac.memo', 'arap' => 'ac.memo', },
1533 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
1534 'name' => { 'gl' => 'g.description', 'arap' => 'c.name', },
1537 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
1538 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
1539 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
1542 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
1543 foreach my $spec (@{ $sort_columns{$sortkey} }) {
1544 next if ($spec !~ m/^lower_(.*)$/);
1547 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
1550 $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
1551 $sth = prepare_query($form, $dbh, $query);
1554 qq|SELECT c.name, a.invnumber, a.ordnumber,
1555 ac.transdate, ac.amount * $ml AS paid, ac.source,
1556 a.invoice, a.id, ac.memo, '${arap}' AS module
1557 $columns_for_sorting{arap}
1559 JOIN $arap a ON (ac.trans_id = a.id)
1560 JOIN $table c ON (c.id = a.${table}_id)
1562 WHERE (ac.chart_id = ?)
1568 SELECT g.description, g.reference, NULL AS ordnumber,
1569 ac.transdate, ac.amount * $ml AS paid, ac.source,
1570 '0' as invoice, g.id, ac.memo, 'gl' AS module
1571 $columns_for_sorting{gl}
1573 JOIN gl g ON (g.id = ac.trans_id)
1575 WHERE (ac.chart_id = ?)
1578 AND (ac.amount * $ml) > 0
1580 ORDER BY $sortorder|;
1581 my $sth_details = prepare_query($form, $dbh, $q_details);
1585 # cycle through each id
1586 foreach my $accno (split(/ /, $form->{paymentaccounts})) {
1587 do_statement($form, $sth, $query, $accno);
1588 my $ref = $sth->fetchrow_hashref();
1589 push(@{ $form->{PR} }, $ref);
1592 $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
1594 do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
1595 while (my $pr = $sth_details->fetchrow_hashref()) {
1596 push(@{ $form->{ $ref->{id} } }, $pr);
1598 $sth_details->finish();
1603 $main::lxdebug->leave_sub();
1607 $main::lxdebug->enter_sub();
1609 my ($self, $myconfig, $form) = @_;
1611 # connect to database
1612 my $dbh = $form->dbconnect($myconfig);
1614 my $last_period = 0;
1617 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);
1619 $form->{decimalplaces} *= 1;
1621 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
1623 # if there are any compare dates
1625 if ($form->{fromdate} || $form->{todate}) {
1627 if ($form->{fromdate}) {
1628 $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1631 $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1634 my $kummfromdate = $form->{comparefromdate};
1635 my $kummtodate = $form->{comparetodate};
1636 &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
1639 my @periods = qw(jetzt kumm);
1640 my @gesamtleistung = qw(1 3);
1641 my @gesamtkosten = qw (10 11 12 13 14 15 16 17 18 19 20);
1643 qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
1645 foreach my $key (@periods) {
1646 $form->{ "$key" . "gesamtleistung" } = 0;
1647 $form->{ "$key" . "gesamtkosten" } = 0;
1649 foreach $category (@categories) {
1651 if (defined($form->{$category}{$key})) {
1652 $form->{"$key$category"} =
1653 $form->format_amount($myconfig,
1654 $form->round_amount($form->{$category}{$key}, 2
1656 $form->{decimalplaces},
1660 foreach my $item (@gesamtleistung) {
1661 $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
1663 $form->{ "$key" . "gesamtleistung" } -= $form->{2}{$key};
1665 foreach my $item (@gesamtkosten) {
1666 $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
1668 $form->{ "$key" . "rohertrag" } =
1669 $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
1670 $form->{ "$key" . "betriebrohertrag" } =
1671 $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
1672 $form->{ "$key" . "betriebsergebnis" } =
1673 $form->{ "$key" . "betriebrohertrag" } -
1674 $form->{ "$key" . "gesamtkosten" };
1675 $form->{ "$key" . "neutraleraufwand" } =
1676 $form->{30}{$key} + $form->{31}{$key};
1677 $form->{ "$key" . "neutralertrag" } =
1678 $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
1679 $form->{ "$key" . "ergebnisvorsteuern" } =
1680 $form->{ "$key" . "betriebsergebnis" } -
1681 $form->{ "$key" . "neutraleraufwand" } +
1682 $form->{ "$key" . "neutralertrag" };
1683 $form->{ "$key" . "ergebnis" } =
1684 $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
1686 if ($form->{ "$key" . "gesamtleistung" } > 0) {
1687 foreach $category (@categories) {
1688 if (defined($form->{$category}{$key})) {
1689 $form->{ "$key" . "gl" . "$category" } =
1690 $form->format_amount(
1692 $form->round_amount(
1693 ($form->{$category}{$key} /
1694 $form->{ "$key" . "gesamtleistung" } * 100
1696 $form->{decimalplaces}
1698 $form->{decimalplaces},
1702 foreach my $item (@ergebnisse) {
1703 $form->{ "$key" . "gl" . "$item" } =
1704 $form->format_amount($myconfig,
1705 $form->round_amount(
1706 ( $form->{ "$key" . "$item" } /
1707 $form->{ "$key" . "gesamtleistung" } * 100
1709 $form->{decimalplaces}
1711 $form->{decimalplaces},
1716 if ($form->{ "$key" . "gesamtkosten" } > 0) {
1717 foreach $category (@categories) {
1718 if (defined($form->{$category}{$key})) {
1719 $form->{ "$key" . "gk" . "$category" } =
1720 $form->format_amount($myconfig,
1721 $form->round_amount(
1722 ($form->{$category}{$key} /
1723 $form->{ "$key" . "gesamtkosten" } * 100
1725 $form->{decimalplaces}
1727 $form->{decimalplaces},
1731 foreach my $item (@ergebnisse) {
1732 $form->{ "$key" . "gk" . "$item" } =
1733 $form->format_amount($myconfig,
1734 $form->round_amount(
1735 ($form->{ "$key" . "$item" } /
1736 $form->{ "$key" . "gesamtkosten" } * 100
1738 $form->{decimalplaces}
1740 $form->{decimalplaces},
1745 if ($form->{10}{$key} > 0) {
1746 foreach $category (@categories) {
1747 if (defined($form->{$category}{$key})) {
1748 $form->{ "$key" . "pk" . "$category" } =
1749 $form->format_amount(
1751 $form->round_amount(
1752 ($form->{$category}{$key} / $form->{10}{$key} * 100),
1753 $form->{decimalplaces}
1755 $form->{decimalplaces},
1759 foreach my $item (@ergebnisse) {
1760 $form->{ "$key" . "pk" . "$item" } =
1761 $form->format_amount($myconfig,
1762 $form->round_amount(
1763 ($form->{ "$key" . "$item" } /
1764 $form->{10}{$key} * 100
1766 $form->{decimalplaces}
1768 $form->{decimalplaces},
1773 if ($form->{4}{$key} > 0) {
1774 foreach $category (@categories) {
1775 if (defined($form->{$category}{$key})) {
1776 $form->{ "$key" . "auf" . "$category" } =
1777 $form->format_amount(
1779 $form->round_amount(
1780 ($form->{$category}{$key} / $form->{4}{$key} * 100),
1781 $form->{decimalplaces}
1783 $form->{decimalplaces},
1787 foreach my $item (@ergebnisse) {
1788 $form->{ "$key" . "auf" . "$item" } =
1789 $form->format_amount($myconfig,
1790 $form->round_amount(
1791 ($form->{ "$key" . "$item" } /
1792 $form->{4}{$key} * 100
1794 $form->{decimalplaces}
1796 $form->{decimalplaces},
1801 foreach my $item (@ergebnisse) {
1802 $form->{ "$key" . "$item" } =
1803 $form->format_amount($myconfig,
1804 $form->round_amount($form->{ "$key" . "$item" },
1805 $form->{decimalplaces}
1807 $form->{decimalplaces},
1814 $main::lxdebug->leave_sub();
1818 $main::lxdebug->enter_sub();
1820 my ($self, $myconfig, $form) = @_;
1822 # connect to database
1823 my $dbh = $form->dbconnect($myconfig);
1825 my $last_period = 0;
1826 my @categories_cent = qw(51r 511 86r 861 97r 971 93r 931
1827 96 66 43 45 53 62 65 67);
1828 my @categories_euro = qw(48 51 86 91 97 93 94);
1829 $form->{decimalplaces} *= 1;
1831 foreach my $item (@categories_cent) {
1832 $form->{"$item"} = 0;
1834 foreach my $item (@categories_euro) {
1835 $form->{"$item"} = 0;
1838 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_ustva");
1840 # foreach $item (@categories_cent) {
1841 # if ($form->{$item}{"jetzt"} > 0) {
1842 # $form->{$item} = $form->{$item}{"jetzt"};
1843 # delete $form->{$item}{"jetzt"};
1846 # foreach $item (@categories_euro) {
1847 # if ($form->{$item}{"jetzt"} > 0) {
1848 # $form->{$item} = $form->{$item}{"jetzt"};
1849 # delete $form->{$item}{"jetzt"};
1850 # } foreach $item (@categories_cent) {
1851 # if ($form->{$item}{"jetzt"} > 0) {
1852 # $form->{$item} = $form->{$item}{"jetzt"};
1853 # delete $form->{$item}{"jetzt"};
1856 # foreach $item (@categories_euro) {
1857 # if ($form->{$item}{"jetzt"} > 0) {
1858 # $form->{$item} = $form->{$item}{"jetzt"};
1859 # delete $form->{$item}{"jetzt"};
1866 # Berechnung der USTVA Formularfelder
1868 $form->{"51r"} = $form->{"511"};
1869 $form->{"86r"} = $form->{"861"};
1870 $form->{"97r"} = $form->{"971"};
1871 $form->{"93r"} = $form->{"931"};
1873 #$form->{"96"} = $form->{"94"} * 0.16;
1875 $form->{"51r"} + $form->{"86r"} + $form->{"97r"} + $form->{"93r"} +
1877 $form->{"45"} = $form->{"43"};
1878 $form->{"53"} = $form->{"43"};
1879 $form->{"62"} = $form->{"43"} - $form->{"66"};
1880 $form->{"65"} = $form->{"43"} - $form->{"66"};
1881 $form->{"67"} = $form->{"43"} - $form->{"66"};
1883 foreach my $item (@categories_cent) {
1885 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),
1889 foreach my $item (@categories_euro) {
1891 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0),
1897 $main::lxdebug->leave_sub();
1900 sub income_statement {
1901 $main::lxdebug->enter_sub();
1903 my ($self, $myconfig, $form) = @_;
1905 # connect to database
1906 my $dbh = $form->dbconnect($myconfig);
1908 my $last_period = 0;
1909 my @categories_einnahmen = qw(1 2 3 4 5 6 7);
1910 my @categories_ausgaben =
1911 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);
1913 my @ergebnisse = qw(sumeura sumeurb guvsumme);
1915 $form->{decimalplaces} *= 1;
1919 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
1923 foreach my $item (@categories_einnahmen) {
1924 $form->{"eur${item}"} =
1925 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1926 $form->{"sumeura"} += $form->{$item};
1928 foreach my $item (@categories_ausgaben) {
1929 $form->{"eur${item}"} =
1930 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1931 $form->{"sumeurb"} += $form->{$item};
1934 $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
1936 foreach my $item (@ergebnisse) {
1938 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1940 $main::lxdebug->leave_sub();