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 # ich sehe keinen sinn das nochmal explizit ohne conv_i aufzurufen
350 # bitte prüfen und löschen jan 15.11.2009
351 # if ($department_id) {
352 # $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
353 # $dpt_where = qq| AND t.department_id = $department_id |;
357 SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
359 JOIN chart c ON (c.id = ac.chart_id)
365 GROUP BY c.accno, c.description, c.category |;
367 if ($form->{project_id}) {
368 # s.o. keine Projektauswahl in Bilanz
372 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
374 JOIN ar a ON (a.id = ac.trans_id)
375 JOIN parts p ON (ac.parts_id = p.id)
376 JOIN chart c on (p.income_accno_id = c.id)
378 -- use transdate from subwhere
379 WHERE (c.category = 'I')
383 GROUP BY c.accno, c.description, c.category
387 SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
389 JOIN ap a ON (a.id = ac.trans_id)
390 JOIN parts p ON (ac.parts_id = p.id)
391 JOIN chart c on (p.expense_accno_id = c.id)
393 WHERE (c.category = 'E')
397 GROUP BY c.accno, c.description, c.category |;
405 $sth = prepare_execute_query($form, $dbh, $query);
407 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
409 if ($ref->{category} eq 'C') {
410 $ref->{category} = 'A';
413 # get last heading account
414 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
418 $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
420 $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
424 $form->{ $ref->{category} }{ $ref->{accno} }{accno} = $ref->{accno};
425 $form->{ $ref->{category} }{ $ref->{accno} }{description} = $ref->{description};
426 $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
429 $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
431 $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
436 # remove accounts with zero balance
437 foreach $category (@{$categories}) {
438 foreach $accno (keys %{ $form->{$category} }) {
439 $form->{$category}{$accno}{last} = $form->round_amount($form->{$category}{$accno}{last}, $dec);
440 $form->{$category}{$accno}{this} = $form->round_amount($form->{$category}{$accno}{this}, $dec);
442 delete $form->{$category}{$accno}
443 if ( $form->{$category}{$accno}{this} == 0
444 && $form->{$category}{$accno}{last} == 0);
448 $main::lxdebug->leave_sub();
452 $main::lxdebug->enter_sub();
454 my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
456 my ($null, $department_id) = split /--/, $form->{department};
470 $fromdate = conv_dateq($fromdate);
471 if ($form->{method} eq 'cash') {
472 $subwhere .= " AND (transdate >= $fromdate)";
473 $glwhere = " AND (ac.transdate >= $fromdate)";
474 $prwhere = " AND (a.transdate >= $fromdate)";
475 $inwhere = " AND (acc.transdate >= $fromdate)";
477 $where .= " AND (ac.transdate >= $fromdate)";
482 $todate = conv_dateq($todate);
483 $subwhere .= " AND (transdate <= $todate)";
484 $where .= " AND (ac.transdate <= $todate)";
485 $prwhere .= " AND (a.transdate <= $todate)";
486 $inwhere .= " AND (acc.transdate <= $todate)";
489 if ($department_id) {
490 $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
491 $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|) |;
494 if ($form->{project_id}) {
495 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
499 # GUV patch by Ronny Rentner (Bug 1190)
501 # GUV IST-Versteuerung
503 # Alle tatsaechlichen _Zahlungseingaenge_
504 # im Zeitraum erfassen
505 # (Teilzahlungen werden prozentual auf verschiedene Steuern aufgeteilt)
509 if ($form->{method} eq 'cash') {
512 SELECT SUM( ac.amount *
513 (SELECT SUM(acc.amount) * -1
515 INNER JOIN chart c ON (acc.chart_id = c.id AND c.link LIKE '%AR_paid%')
516 WHERE 1=1 $inwhere AND acc.trans_id = ac.trans_id)
517 / (SELECT amount FROM ar WHERE id = ac.trans_id)
518 ) AS amount, c.pos_eur
520 LEFT JOIN chart c ON (c.id = ac.chart_id)
521 LEFT JOIN ar ON (ar.id = ac.trans_id)
522 LEFT JOIN taxkeys tk ON (tk.id = (
523 SELECT id FROM taxkeys
524 WHERE chart_id = ac.chart_id
525 AND startdate <= COALESCE(ar.deliverydate,ar.transdate)
526 ORDER BY startdate DESC LIMIT 1
529 WHERE ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE 1=1 $subwhere)
534 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
536 JOIN chart c ON (c.id = ac.chart_id)
537 JOIN ar a ON (a.id = ac.trans_id)
539 WHERE $where $dpt_where
540 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)
546 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
548 JOIN chart c ON (c.id = ac.chart_id)
549 JOIN ap a ON (a.id = ac.trans_id)
551 WHERE $where $dpt_where
552 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)
558 SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
560 JOIN chart c ON (c.id = ac.chart_id)
561 JOIN gl a ON (a.id = ac.trans_id)
563 WHERE $where $dpt_where $glwhere
564 AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
569 if ($form->{project_id}) {
573 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
575 JOIN ar a ON (a.id = ac.trans_id)
576 JOIN parts p ON (ac.parts_id = p.id)
577 JOIN chart c on (p.income_accno_id = c.id)
579 WHERE (c.category = 'I') $prwhere $dpt_where
580 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)
586 SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category
588 JOIN ap a ON (a.id = ac.trans_id)
589 JOIN parts p ON (ac.parts_id = p.id)
590 JOIN chart c on (p.expense_accno_id = c.id)
592 WHERE (c.category = 'E') $prwhere $dpt_where
593 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)
599 } else { # if ($form->{method} eq 'cash')
600 # s.o. jan 15.11.2009
601 # if ($department_id) {
602 # ($dpt_join, $dpt_where) = sql_department($department_id);
606 SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
608 JOIN chart c ON (c.id = ac.chart_id)
613 GROUP BY c.$category |;
615 if ($form->{project_id}) {
619 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
621 JOIN ar a ON (a.id = ac.trans_id)
622 JOIN parts p ON (ac.parts_id = p.id)
623 JOIN chart c on (p.income_accno_id = c.id)
625 WHERE (c.category = 'I')
633 SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
635 JOIN ap a ON (a.id = ac.trans_id)
636 JOIN parts p ON (ac.parts_id = p.id)
637 JOIN chart c on (p.expense_accno_id = c.id)
639 WHERE (c.category = 'E')
643 GROUP BY c.$category |;
651 foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
652 if ($category eq "pos_bwa") {
654 $form->{ $ref->{$category} }{kumm} += $ref->{amount};
656 $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
659 $form->{ $ref->{$category} } += $ref->{amount};
663 $main::lxdebug->leave_sub();
667 $main::lxdebug->enter_sub();
669 my ($self, $myconfig, $form, %options) = @_;
671 my $dbh = $form->dbconnect($myconfig);
673 my ($query, $sth, $ref);
676 my ($null, $department_id) = split /--/, $form->{department};
677 my @headingaccounts = ();
683 my $invwhere = $where;
685 if ($department_id) {
686 $dpt_join = qq| JOIN dpt_trans t ON (ac.trans_id = t.trans_id) |;
687 $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
690 # project_id only applies to getting transactions
691 # it has nothing to do with a trial balance
692 # but we use the same function to collect information
694 if ($form->{project_id}) {
695 $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
698 my $acc_cash_where = "";
699 # my $ar_cash_where = "";
700 # my $ap_cash_where = "";
703 if ($form->{method} eq "cash") {
705 qq| AND (ac.trans_id IN (
708 WHERE datepaid >= '$form->{fromdate}'
709 AND datepaid <= '$form->{todate}'
715 WHERE datepaid >= '$form->{fromdate}'
716 AND datepaid <= '$form->{todate}'
722 WHERE transdate >= '$form->{fromdate}'
723 AND transdate <= '$form->{todate}'
725 # $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
728 if ($options{beginning_balances}) {
729 foreach my $prefix (qw(from to)) {
730 next if ($form->{"${prefix}date"});
732 my $min_max = $prefix eq 'from' ? 'min' : 'max';
733 $query = qq|SELECT ${min_max}(transdate)
739 ($form->{"${prefix}date"}) = selectfirst_array_query($form, $dbh, $query);
742 # get beginning balances
744 qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
746 LEFT JOIN chart c ON (ac.chart_id = c.id)
748 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
751 GROUP BY c.accno, c.category, c.description |;
753 $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
755 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
757 if ($ref->{amount} != 0 || $form->{all_accounts}) {
758 $trb{ $ref->{accno} }{description} = $ref->{description};
759 $trb{ $ref->{accno} }{charttype} = 'A';
760 $trb{ $ref->{accno} }{beginning_balance} = $ref->{amount};
762 if ($ref->{amount} > 0) {
763 $trb{ $ref->{accno} }{haben_eb} = $ref->{amount};
765 $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1;
767 $trb{ $ref->{accno} }{category} = $ref->{category};
776 qq|SELECT c.accno, c.description, c.category
778 WHERE c.charttype = 'H'
781 $sth = prepare_execute_query($form, $dbh, $query);
783 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
784 $trb{ $ref->{accno} }{description} = $ref->{description};
785 $trb{ $ref->{accno} }{charttype} = 'H';
786 $trb{ $ref->{accno} }{category} = $ref->{category};
788 push @headingaccounts, $ref->{accno};
794 my $saldowhere = " 1 = 1 ";
795 my $sumwhere = " 1 = 1 ";
797 my $sumsubwhere = '';
798 my $saldosubwhere = '';
799 my $glsaldowhere = '';
804 my ($fromdate, $todate);
806 if ($form->{fromdate} || $form->{todate}) {
807 if ($form->{fromdate}) {
808 $fromdate = conv_dateq($form->{fromdate});
809 $tofrom .= " AND (ac.transdate >= $fromdate)";
810 $subwhere .= " AND (ac.transdate >= $fromdate)";
811 $sumsubwhere .= " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
812 $saldosubwhere .= " AND (ac,transdate>=(select date_trunc('year', date $fromdate))) ";
813 $invwhere .= " AND (a.transdate >= $fromdate)";
814 $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
815 $glwhere = " AND (ac.transdate >= $fromdate)";
816 $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
818 if ($form->{todate}) {
819 $todate = conv_dateq($form->{todate});
820 $tofrom .= " AND (ac.transdate <= $todate)";
821 $invwhere .= " AND (a.transdate <= $todate)";
822 $saldosubwhere .= " AND (ac.transdate <= $todate)";
823 $sumsubwhere .= " AND (ac.transdate <= $todate)";
824 $subwhere .= " AND (ac.transdate <= $todate)";
825 $glwhere .= " AND (ac.transdate <= $todate)";
826 $glsumwhere .= " AND (ac.transdate <= $todate) ";
827 $glsaldowhere .= " AND (ac.transdate <= $todate) ";
831 if ($form->{method} eq "cash") {
833 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) |;
834 $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) |;
836 $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) |;
838 $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
839 $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
840 $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
844 SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
846 JOIN chart c ON (c.id = ac.chart_id)
851 GROUP BY c.accno, c.description, c.category |;
853 if ($form->{project_id}) {
855 -- add project transactions from invoice
859 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
861 JOIN ar a ON (ac.trans_id = a.id)
862 JOIN parts p ON (ac.parts_id = p.id)
863 JOIN chart c ON (p.income_accno_id = c.id)
868 GROUP BY c.accno, c.description, c.category
872 SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
874 JOIN ap a ON (ac.trans_id = a.id)
875 JOIN parts p ON (ac.parts_id = p.id)
876 JOIN chart c ON (p.expense_accno_id = c.id)
881 GROUP BY c.accno, c.description, c.category
885 $query .= qq| ORDER BY accno|;
887 $sth = prepare_execute_query($form, $dbh, $query);
889 # calculate the debit and credit in the period
890 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
891 $trb{ $ref->{accno} }{description} = $ref->{description};
892 $trb{ $ref->{accno} }{charttype} = 'A';
893 $trb{ $ref->{accno} }{category} = $ref->{category};
894 $trb{ $ref->{accno} }{amount} += $ref->{amount};
898 # prepare query for each account
899 my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
903 (SELECT SUM(ac.amount) * -1
905 JOIN chart c ON (c.id = ac.chart_id)
911 AND (c.accno = ?)) AS debit,
913 (SELECT SUM(ac.amount)
915 JOIN chart c ON (c.id = ac.chart_id)
921 AND c.accno = ?) AS credit,
922 (SELECT SUM(ac.amount)
924 JOIN chart c ON (ac.chart_id = c.id)
929 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
931 (SELECT SUM(ac.amount)
933 JOIN chart c ON (ac.chart_id = c.id)
939 AND c.accno = ?) AS sum_credit,
941 (SELECT SUM(ac.amount)
943 JOIN chart c ON (ac.chart_id = c.id)
949 AND c.accno = ?) AS sum_debit,
951 (SELECT max(ac.transdate) FROM acc_trans ac
952 JOIN chart c ON (ac.chart_id = c.id)
957 AND c.accno = ?) AS last_transaction
962 $drcr = prepare_query($form, $dbh, $q_drcr);
964 if ($form->{project_id}) {
965 # prepare query for each account
968 (SELECT SUM(ac.sellprice * ac.qty) * -1
970 JOIN parts p ON (ac.parts_id = p.id)
971 JOIN ap a ON (ac.trans_id = a.id)
972 JOIN chart c ON (p.expense_accno_id = c.id)
977 AND c.accno = ?) AS debit,
979 (SELECT SUM(ac.sellprice * ac.qty)
981 JOIN parts p ON (ac.parts_id = p.id)
982 JOIN ar a ON (ac.trans_id = a.id)
983 JOIN chart c ON (p.income_accno_id = c.id)
988 AND c.accno = ?) AS credit,
990 (SELECT SUM(ac.amount)
992 JOIN chart c ON (ac.chart_id = c.id)
997 AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
999 (SELECT SUM(ac.amount)
1001 JOIN chart c ON (ac.chart_id = c.id)
1007 AND c.accno = ?) AS sum_credit,
1009 (SELECT SUM(ac.amount)
1011 JOIN chart c ON (ac.chart_id = c.id)
1017 AND c.accno = ?) AS sum_debit,
1020 (SELECT max(ac.transdate) FROM acc_trans ac
1021 JOIN chart c ON (ac.chart_id = c.id)
1026 AND c.accno = ?) AS last_transaction
1029 $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
1033 my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
1035 foreach my $accno (sort keys %trb) {
1038 $ref->{accno} = $accno;
1039 map { $ref->{$_} = $trb{$accno}{$_} }
1040 qw(description category charttype amount soll_eb haben_eb beginning_balance);
1042 $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
1044 if ($trb{$accno}{charttype} eq 'A') {
1047 do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1049 ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
1050 my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
1051 $last_transaction = "";
1052 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
1053 $ref->{debit} += $debit;
1054 $ref->{credit} += $credit;
1056 $ref->{haben_saldo} += $saldo;
1058 $ref->{soll_saldo} += $saldo * -1;
1060 $ref->{last_transaction} = $last_transaction;
1061 $ref->{soll_kumuliert} = $soll_kumuliert * -1;
1062 $ref->{haben_kumuliert} = $haben_kumuliert;
1066 if ($form->{project_id}) {
1069 do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
1071 ($debit, $credit) = (0, 0);
1072 while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
1073 $ref->{debit} += $debit;
1074 $ref->{credit} += $credit;
1076 $ref->{haben_saldo} += $saldo;
1078 $ref->{soll_saldo} += $saldo * -1;
1080 $ref->{soll_kumuliert} += $soll_kumuliert * -1;
1081 $ref->{haben_kumuliert} += $haben_kumuliert;
1083 $project_drcr->finish;
1086 $ref->{debit} = $form->round_amount($ref->{debit}, 2);
1087 $ref->{credit} = $form->round_amount($ref->{credit}, 2);
1089 if ($ref->{haben_saldo} != 0) {
1090 $ref->{haben_saldo} = $ref->{haben_saldo} + $ref->{beginning_balance};
1091 if ($ref->{haben_saldo} < 0) {
1092 $ref->{soll_saldo} = $form->round_amount(($ref->{haben_saldo} *- 1), 2);
1093 $ref->{haben_saldo} = 0;
1096 $ref->{soll_saldo} = $ref->{soll_saldo} - $ref->{beginning_balance};
1097 if ($ref->{soll_saldo} < 0) {
1098 $ref->{haben_saldo} = $form->round_amount(($ref->{soll_saldo} * -1), 2);
1099 $ref->{soll_saldo} = 0;
1102 $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
1103 $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
1104 $ref->{haben_kumuliert} = $form->round_amount($ref->{haben_kumuliert}, 2);
1105 $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
1110 @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
1111 $accno = pop @accno;
1113 $trb{$accno}{debit} += $ref->{debit};
1114 $trb{$accno}{credit} += $ref->{credit};
1115 $trb{$accno}{soll_saldo} += $ref->{soll_saldo};
1116 $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
1117 $trb{$accno}{soll_kumuliert} += $ref->{soll_kumuliert};
1118 $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
1121 push @{ $form->{TB} }, $ref;
1127 # debits and credits for headings
1128 foreach my $accno (@headingaccounts) {
1129 foreach $ref (@{ $form->{TB} }) {
1130 if ($accno eq $ref->{accno}) {
1131 $ref->{debit} = $trb{$accno}{debit};
1132 $ref->{credit} = $trb{$accno}{credit};
1133 $ref->{soll_saldo} = $trb{$accno}{soll_saldo};
1134 $ref->{haben_saldo} = $trb{$accno}{haben_saldo};
1135 $ref->{soll_kumuliert} = $trb{$accno}{soll_kumuliert};
1136 $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert};
1141 $main::lxdebug->leave_sub();
1145 $main::lxdebug->enter_sub();
1146 my ($self, $dbh, $form) = @_;
1147 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
1148 my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
1149 my $sth = $dbh->prepare($query);
1150 while(my $ref = $sth->fetchrow_hashref()) {
1151 $ref->{invnumer} =~ s/Storno zu //g;
1152 $form->{storno}{$ref->{invnumber}} = 1;
1154 $main::lxdebug->leave_sub();
1158 $main::lxdebug->enter_sub();
1160 my ($self, $myconfig, $form) = @_;
1162 # connect to database
1163 my $dbh = $form->dbconnect($myconfig);
1165 my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
1167 if ($form->{ct} eq "customer") {
1180 $ct_id = "${ct}_id";
1182 $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
1183 my $todate = conv_dateq($form->{todate});
1184 my $fromdate = conv_dateq($form->{fromdate});
1186 my $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
1188 my $where = " 1 = 1 ";
1191 if ($form->{$ct_id}) {
1192 $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
1193 } elsif ($form->{ $form->{ct} }) {
1194 $where .= qq| AND (ct.name ILIKE | . $dbh->quote('%' . $form->{$ct} . '%') . qq|)|;
1198 if ($form->{department}) {
1199 my ($null, $department_id) = split /--/, $form->{department};
1200 $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
1201 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
1203 my $review_of_aging_list;
1204 if ($form->{review_of_aging_list}) {
1205 if ($form->{review_of_aging_list} =~ m "-"){
1206 my @period = split(/-/, $form->{review_of_aging_list});
1207 $review_of_aging_list = " AND $period[0] < date_part('days', now() - duedate)
1208 AND date_part('days', now() - duedate) < $period[1]";
1210 $form->{review_of_aging_list} =~ s/[^0-9]//g;
1211 $review_of_aging_list = " AND $form->{review_of_aging_list} < date_part('days', now() - duedate)";
1217 SELECT ${ct}.id AS ctid, ${ct}.name,
1218 street, zipcode, city, country, contact, email,
1219 phone as customerphone, fax as customerfax, ${ct}number,
1220 "invnumber", "transdate",
1221 (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",
1222 "duedate", invoice, ${arap}.id,
1225 WHERE (${arap}.curr = exchangerate.curr)
1226 AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
1228 WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
1229 AND NOT COALESCE (${arap}.storno, 'f')
1230 AND (${arap}.${ct}_id = ${ct}.id)
1232 AND (transdate <= (date $todate) $fromwhere )
1233 $review_of_aging_list
1234 ORDER BY ctid, transdate, invnumber |;
1236 my $sth_details = prepare_query($form, $dbh, $q_details);
1238 # select outstanding vendors or customers, depends on $ct
1240 qq|SELECT DISTINCT ct.id, ct.name
1241 FROM $ct ct, $arap a
1244 AND (a.${ct_id} = ct.id)
1245 AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
1246 AND (a.transdate <= $todate $fromwhere)
1249 my $sth = prepare_execute_query($form, $dbh, $query);
1252 # for each company that has some stuff outstanding
1253 while (my ($id) = $sth->fetchrow_array) {
1254 do_statement($form, $sth_details, $q_details, $id);
1256 while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
1257 $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
1258 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
1259 push @{ $form->{AG} }, $ref;
1262 $sth_details->finish;
1271 $main::lxdebug->leave_sub();
1275 $main::lxdebug->enter_sub();
1277 my ($self, $myconfig, $form) = @_;
1279 # connect to database
1280 my $dbh = $form->dbconnect($myconfig);
1282 my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
1285 qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
1288 ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
1289 selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
1292 $main::lxdebug->leave_sub();
1295 sub get_taxaccounts {
1296 $main::lxdebug->enter_sub();
1298 my ($self, $myconfig, $form) = @_;
1300 # connect to database
1301 my $dbh = $form->dbconnect($myconfig);
1305 qq|SELECT c.accno, c.description, t.rate
1307 WHERE (c.link LIKE '%CT_tax%') AND (c.id = t.chart_id)
1309 $form->{taxaccounts} = selectall_hashref_quert($form, $dbh, $query);
1313 $main::lxdebug->leave_sub();
1317 $main::lxdebug->enter_sub();
1319 my ($self, $myconfig, $form) = @_;
1321 # connect to database
1322 my $dbh = $form->dbconnect($myconfig);
1324 my ($null, $department_id) = split /--/, $form->{department};
1327 my $where = "1 = 1";
1329 if ($department_id) {
1330 $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
1335 if ($form->{accno}) {
1336 $accno = $form->{accno};
1337 $rate = $form->{"$form->{accno}_rate"};
1338 $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
1344 if ($form->{db} eq 'ar') {
1345 $table = "customer";
1352 my $arap = lc($ARAP);
1354 my $transdate = "a.transdate";
1356 if ($form->{method} eq 'cash') {
1357 $transdate = "a.datepaid";
1359 my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
1366 JOIN chart c ON (a.chart_id = c.id)
1367 WHERE (link LIKE '%${ARAP}_paid%')
1368 AND (transdate <= $todate)
1373 # if there are any dates construct a where
1374 $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
1375 $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
1377 my $ml = ($form->{db} eq 'ar') ? 1 : -1;
1379 my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
1380 $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
1383 if ($form->{report} !~ /nontaxable/) {
1385 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
1386 ac.amount * $ml AS tax
1388 JOIN ${arap} a ON (a.id = ac.trans_id)
1389 JOIN chart ch ON (ch.id = ac.chart_id)
1390 JOIN $table n ON (n.id = a.${table}_id)
1394 AND (a.invoice = '0')
1398 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
1399 i.sellprice * i.qty * $rate * $ml AS tax
1401 JOIN ${arap} a ON (a.id = ac.trans_id)
1402 JOIN chart ch ON (ch.id = ac.chart_id)
1403 JOIN $table n ON (n.id = a.${table}_id)
1404 JOIN ${table}tax t ON (t.${table}_id = n.id)
1405 JOIN invoice i ON (i.trans_id = a.id)
1406 JOIN partstax p ON (p.parts_id = i.parts_id)
1410 AND (a.invoice = '1')
1411 ORDER BY $sortorder|;
1413 # only gather up non-taxable transactions
1415 qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount
1417 JOIN ${arap} a ON (a.id = ac.trans_id)
1418 JOIN $table n ON (n.id = a.${table}_id)
1421 AND (a.invoice = '0')
1422 AND (a.netamount = a.amount)
1426 SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount
1428 JOIN ${arap} a ON (a.id = ac.trans_id)
1429 JOIN $table n ON (n.id = a.${table}_id)
1430 JOIN invoice i ON (i.trans_id = a.id)
1433 AND (a.invoice = '1')
1435 a.${table}_id NOT IN (SELECT ${table}_id FROM ${table}tax t (${table}_id))
1437 i.parts_id NOT IN (SELECT parts_id FROM partstax p (parts_id))
1439 GROUP BY a.id, a.invnumber, $transdate, n.name, i.sellprice, i.qty
1440 ORDER by $sortorder|;
1443 $form->{TR} = selectall_hashref_query($form, $dbh, $query);
1447 $main::lxdebug->leave_sub();
1450 sub paymentaccounts {
1451 $main::lxdebug->enter_sub();
1453 my ($self, $myconfig, $form) = @_;
1455 # connect to database, turn AutoCommit off
1456 my $dbh = $form->dbconnect_noauto($myconfig);
1458 my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
1460 # get A(R|P)_paid accounts
1462 qq|SELECT accno, description
1464 WHERE link LIKE '%${ARAP}_paid%'|;
1465 $form->{PR} = selectall_hashref_query($form, $dbh, $query);
1469 $main::lxdebug->leave_sub();
1473 $main::lxdebug->enter_sub();
1475 my ($self, $myconfig, $form) = @_;
1477 # connect to database, turn AutoCommit off
1478 my $dbh = $form->dbconnect_noauto($myconfig);
1483 if ($form->{db} eq 'ar') {
1484 $table = 'customer';
1496 if ($form->{department_id}) {
1497 $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
1498 $where = qq| AND (t.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
1501 if ($form->{fromdate}) {
1502 $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
1504 if ($form->{todate}) {
1505 $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
1507 if (!$form->{fx_transaction}) {
1508 $where .= " AND ac.fx_transaction = '0'";
1513 if ($form->{reference}) {
1514 $reference = $dbh->quote('%' . $form->{reference} . '%');
1515 $invnumber = " AND (a.invnumber LIKE $reference)";
1516 $reference = " AND (g.reference LIKE $reference)";
1518 if ($form->{source}) {
1519 $where .= " AND (ac.source ILIKE " . $dbh->quote('%' . $form->{source} . '%') . ") ";
1521 if ($form->{memo}) {
1522 $where .= " AND (ac.memo ILIKE " . $dbh->quote('%' . $form->{memo} . '%') . ") ";
1525 my %sort_columns = (
1526 'transdate' => [ qw(transdate lower_invnumber lower_name) ],
1527 'invnumber' => [ qw(lower_invnumber lower_name transdate) ],
1528 'name' => [ qw(lower_name transdate) ],
1529 'source' => [ qw(lower_source) ],
1530 'memo' => [ qw(lower_memo) ],
1532 my %lowered_columns = (
1533 'invnumber' => { 'gl' => 'g.reference', 'arap' => 'a.invnumber', },
1534 'memo' => { 'gl' => 'ac.memo', 'arap' => 'ac.memo', },
1535 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
1536 'name' => { 'gl' => 'g.description', 'arap' => 'c.name', },
1539 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
1540 my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
1541 my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
1544 my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
1545 foreach my $spec (@{ $sort_columns{$sortkey} }) {
1546 next if ($spec !~ m/^lower_(.*)$/);
1549 map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
1552 $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
1553 $sth = prepare_query($form, $dbh, $query);
1556 qq|SELECT c.name, a.invnumber, a.ordnumber,
1557 ac.transdate, ac.amount * $ml AS paid, ac.source,
1558 a.invoice, a.id, ac.memo, '${arap}' AS module
1559 $columns_for_sorting{arap}
1561 JOIN $arap a ON (ac.trans_id = a.id)
1562 JOIN $table c ON (c.id = a.${table}_id)
1564 WHERE (ac.chart_id = ?)
1570 SELECT g.description, g.reference, NULL AS ordnumber,
1571 ac.transdate, ac.amount * $ml AS paid, ac.source,
1572 '0' as invoice, g.id, ac.memo, 'gl' AS module
1573 $columns_for_sorting{gl}
1575 JOIN gl g ON (g.id = ac.trans_id)
1577 WHERE (ac.chart_id = ?)
1580 AND (ac.amount * $ml) > 0
1582 ORDER BY $sortorder|;
1583 my $sth_details = prepare_query($form, $dbh, $q_details);
1587 # cycle through each id
1588 foreach my $accno (split(/ /, $form->{paymentaccounts})) {
1589 do_statement($form, $sth, $query, $accno);
1590 my $ref = $sth->fetchrow_hashref();
1591 push(@{ $form->{PR} }, $ref);
1594 $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
1596 do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
1597 while (my $pr = $sth_details->fetchrow_hashref()) {
1598 push(@{ $form->{ $ref->{id} } }, $pr);
1600 $sth_details->finish();
1605 $main::lxdebug->leave_sub();
1609 $main::lxdebug->enter_sub();
1611 my ($self, $myconfig, $form) = @_;
1613 # connect to database
1614 my $dbh = $form->dbconnect($myconfig);
1616 my $last_period = 0;
1619 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);
1621 $form->{decimalplaces} *= 1;
1623 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
1625 # if there are any compare dates
1627 if ($form->{fromdate} || $form->{todate}) {
1629 if ($form->{fromdate}) {
1630 $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1633 $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
1636 my $kummfromdate = $form->{comparefromdate};
1637 my $kummtodate = $form->{comparetodate};
1638 &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
1641 my @periods = qw(jetzt kumm);
1642 my @gesamtleistung = qw(1 3);
1643 my @gesamtkosten = qw (10 11 12 13 14 15 16 17 18 19 20);
1645 qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
1647 foreach my $key (@periods) {
1648 $form->{ "$key" . "gesamtleistung" } = 0;
1649 $form->{ "$key" . "gesamtkosten" } = 0;
1651 foreach $category (@categories) {
1653 if (defined($form->{$category}{$key})) {
1654 $form->{"$key$category"} =
1655 $form->format_amount($myconfig,
1656 $form->round_amount($form->{$category}{$key}, 2
1658 $form->{decimalplaces},
1662 foreach my $item (@gesamtleistung) {
1663 $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
1665 $form->{ "$key" . "gesamtleistung" } -= $form->{2}{$key};
1667 foreach my $item (@gesamtkosten) {
1668 $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
1670 $form->{ "$key" . "rohertrag" } =
1671 $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
1672 $form->{ "$key" . "betriebrohertrag" } =
1673 $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
1674 $form->{ "$key" . "betriebsergebnis" } =
1675 $form->{ "$key" . "betriebrohertrag" } -
1676 $form->{ "$key" . "gesamtkosten" };
1677 $form->{ "$key" . "neutraleraufwand" } =
1678 $form->{30}{$key} + $form->{31}{$key};
1679 $form->{ "$key" . "neutralertrag" } =
1680 $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
1681 $form->{ "$key" . "ergebnisvorsteuern" } =
1682 $form->{ "$key" . "betriebsergebnis" } -
1683 $form->{ "$key" . "neutraleraufwand" } +
1684 $form->{ "$key" . "neutralertrag" };
1685 $form->{ "$key" . "ergebnis" } =
1686 $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
1688 if ($form->{ "$key" . "gesamtleistung" } > 0) {
1689 foreach $category (@categories) {
1690 if (defined($form->{$category}{$key})) {
1691 $form->{ "$key" . "gl" . "$category" } =
1692 $form->format_amount(
1694 $form->round_amount(
1695 ($form->{$category}{$key} /
1696 $form->{ "$key" . "gesamtleistung" } * 100
1698 $form->{decimalplaces}
1700 $form->{decimalplaces},
1704 foreach my $item (@ergebnisse) {
1705 $form->{ "$key" . "gl" . "$item" } =
1706 $form->format_amount($myconfig,
1707 $form->round_amount(
1708 ( $form->{ "$key" . "$item" } /
1709 $form->{ "$key" . "gesamtleistung" } * 100
1711 $form->{decimalplaces}
1713 $form->{decimalplaces},
1718 if ($form->{ "$key" . "gesamtkosten" } > 0) {
1719 foreach $category (@categories) {
1720 if (defined($form->{$category}{$key})) {
1721 $form->{ "$key" . "gk" . "$category" } =
1722 $form->format_amount($myconfig,
1723 $form->round_amount(
1724 ($form->{$category}{$key} /
1725 $form->{ "$key" . "gesamtkosten" } * 100
1727 $form->{decimalplaces}
1729 $form->{decimalplaces},
1733 foreach my $item (@ergebnisse) {
1734 $form->{ "$key" . "gk" . "$item" } =
1735 $form->format_amount($myconfig,
1736 $form->round_amount(
1737 ($form->{ "$key" . "$item" } /
1738 $form->{ "$key" . "gesamtkosten" } * 100
1740 $form->{decimalplaces}
1742 $form->{decimalplaces},
1747 if ($form->{10}{$key} > 0) {
1748 foreach $category (@categories) {
1749 if (defined($form->{$category}{$key})) {
1750 $form->{ "$key" . "pk" . "$category" } =
1751 $form->format_amount(
1753 $form->round_amount(
1754 ($form->{$category}{$key} / $form->{10}{$key} * 100),
1755 $form->{decimalplaces}
1757 $form->{decimalplaces},
1761 foreach my $item (@ergebnisse) {
1762 $form->{ "$key" . "pk" . "$item" } =
1763 $form->format_amount($myconfig,
1764 $form->round_amount(
1765 ($form->{ "$key" . "$item" } /
1766 $form->{10}{$key} * 100
1768 $form->{decimalplaces}
1770 $form->{decimalplaces},
1775 if ($form->{4}{$key} > 0) {
1776 foreach $category (@categories) {
1777 if (defined($form->{$category}{$key})) {
1778 $form->{ "$key" . "auf" . "$category" } =
1779 $form->format_amount(
1781 $form->round_amount(
1782 ($form->{$category}{$key} / $form->{4}{$key} * 100),
1783 $form->{decimalplaces}
1785 $form->{decimalplaces},
1789 foreach my $item (@ergebnisse) {
1790 $form->{ "$key" . "auf" . "$item" } =
1791 $form->format_amount($myconfig,
1792 $form->round_amount(
1793 ($form->{ "$key" . "$item" } /
1794 $form->{4}{$key} * 100
1796 $form->{decimalplaces}
1798 $form->{decimalplaces},
1803 foreach my $item (@ergebnisse) {
1804 $form->{ "$key" . "$item" } =
1805 $form->format_amount($myconfig,
1806 $form->round_amount($form->{ "$key" . "$item" },
1807 $form->{decimalplaces}
1809 $form->{decimalplaces},
1816 $main::lxdebug->leave_sub();
1820 $main::lxdebug->enter_sub();
1822 my ($self, $myconfig, $form) = @_;
1824 # connect to database
1825 my $dbh = $form->dbconnect($myconfig);
1827 my $last_period = 0;
1828 my @categories_cent = qw(51r 511 86r 861 97r 971 93r 931
1829 96 66 43 45 53 62 65 67);
1830 my @categories_euro = qw(48 51 86 91 97 93 94);
1831 $form->{decimalplaces} *= 1;
1833 foreach my $item (@categories_cent) {
1834 $form->{"$item"} = 0;
1836 foreach my $item (@categories_euro) {
1837 $form->{"$item"} = 0;
1840 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_ustva");
1842 # foreach $item (@categories_cent) {
1843 # if ($form->{$item}{"jetzt"} > 0) {
1844 # $form->{$item} = $form->{$item}{"jetzt"};
1845 # delete $form->{$item}{"jetzt"};
1848 # foreach $item (@categories_euro) {
1849 # if ($form->{$item}{"jetzt"} > 0) {
1850 # $form->{$item} = $form->{$item}{"jetzt"};
1851 # delete $form->{$item}{"jetzt"};
1852 # } foreach $item (@categories_cent) {
1853 # if ($form->{$item}{"jetzt"} > 0) {
1854 # $form->{$item} = $form->{$item}{"jetzt"};
1855 # delete $form->{$item}{"jetzt"};
1858 # foreach $item (@categories_euro) {
1859 # if ($form->{$item}{"jetzt"} > 0) {
1860 # $form->{$item} = $form->{$item}{"jetzt"};
1861 # delete $form->{$item}{"jetzt"};
1868 # Berechnung der USTVA Formularfelder
1870 $form->{"51r"} = $form->{"511"};
1871 $form->{"86r"} = $form->{"861"};
1872 $form->{"97r"} = $form->{"971"};
1873 $form->{"93r"} = $form->{"931"};
1875 #$form->{"96"} = $form->{"94"} * 0.16;
1877 $form->{"51r"} + $form->{"86r"} + $form->{"97r"} + $form->{"93r"} +
1879 $form->{"45"} = $form->{"43"};
1880 $form->{"53"} = $form->{"43"};
1881 $form->{"62"} = $form->{"43"} - $form->{"66"};
1882 $form->{"65"} = $form->{"43"} - $form->{"66"};
1883 $form->{"67"} = $form->{"43"} - $form->{"66"};
1885 foreach my $item (@categories_cent) {
1887 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),
1891 foreach my $item (@categories_euro) {
1893 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0),
1899 $main::lxdebug->leave_sub();
1902 sub income_statement {
1903 $main::lxdebug->enter_sub();
1905 my ($self, $myconfig, $form) = @_;
1907 # connect to database
1908 my $dbh = $form->dbconnect($myconfig);
1910 my $last_period = 0;
1911 my @categories_einnahmen = qw(1 2 3 4 5 6 7);
1912 my @categories_ausgaben =
1913 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);
1915 my @ergebnisse = qw(sumeura sumeurb guvsumme);
1917 $form->{decimalplaces} *= 1;
1921 &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
1925 foreach my $item (@categories_einnahmen) {
1926 $form->{"eur${item}"} =
1927 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1928 $form->{"sumeura"} += $form->{$item};
1930 foreach my $item (@categories_ausgaben) {
1931 $form->{"eur${item}"} =
1932 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1933 $form->{"sumeurb"} += $form->{$item};
1936 $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
1938 foreach my $item (@ergebnisse) {
1940 $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
1942 $main::lxdebug->leave_sub();