+ my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
+
+ my ($null, $department_id) = split /--/, $form->{department};
+
+ my $query;
+ my $dpt_where;
+ my $dpt_join;
+ my $project;
+ my $where = "1 = 1";
+ my $glwhere = "";
+ my $subwhere = "";
+ my $ARwhere = "";
+ my $APwhere = '';
+ my $arwhere = "";
+ my $item;
+
+ if ($fromdate) {
+ if ($form->{method} eq 'cash') {
+ $subwhere .= " AND transdate >= '$fromdate'";
+ $glwhere = " AND ac.transdate >= '$fromdate'";
+ $ARwhere .= " AND acc.transdate >= '$fromdate'";
+ $APwhere .= " AND AP.transdate >= '$fromdate'";
+ }
+ $where .= " AND ac.transdate >= '$fromdate'";
+ }
+
+ if ($todate) {
+ $where .= " AND ac.transdate <= '$todate'";
+ $ARwhere .= " AND acc.transdate <= '$todate'";
+ $subwhere .= " AND transdate <= '$todate'";
+ $APwhere .= " AND AP.transdate <= '$todate'";
+ }
+
+ if ($department_id) {
+ $dpt_join = qq|
+ JOIN department t ON (a.department_id = t.id)
+ |;
+ $dpt_where = qq|
+ AND t.id = $department_id
+ |;
+ }
+
+ if ($form->{project_id}) {
+ $project = qq|
+ AND ac.project_id = $form->{project_id}
+ |;
+ }
+#########################################
+# Method eq 'cash' = IST Versteuerung
+#########################################
+
+ if ($form->{method} eq 'cash') {
+
+ $query = qq|
+ SELECT
+ -- Alle tatsaechlichen Zahlungseingaenge
+ -- im Voranmeldezeitraum erfassen
+ -- (Teilzahlungen werden prozentual auf verschiedene Steuern aufgeteilt)
+ SUM( ac.amount *
+ -- Bezahlt / Rechnungssumme
+ (
+ SELECT SUM(acc.amount)
+ FROM acc_trans acc
+ INNER JOIN chart c ON (acc.chart_id = c.id
+ AND c.link like '%AR_paid%')
+ WHERE
+ 1=1
+ $ARwhere
+ AND acc.trans_id = ac.trans_id
+ )
+ /
+ (
+ select amount from ar where id = ac.trans_id
+ )
+ ) AS amount,
+ tk.pos_ustva
+ FROM acc_trans ac
+ LEFT JOIN chart c ON (c.id = ac.chart_id)
+ LEFT JOIN ar ON (ar.id = ac.trans_id)
+ LEFT JOIN taxkeys tk ON (
+ tk.id = (
+ SELECT id FROM taxkeys
+ WHERE chart_id = ac.chart_id
+ AND taxkey_id = ac.taxkey
+ AND startdate <= COALESCE(ar.deliverydate, ar.transdate)
+ ORDER BY startdate DESC LIMIT 1
+ )
+ )
+ WHERE
+ 1=1
+ -- Here no where, please. All Transactions ever should be
+ -- testet if they are paied in the USTVA report period.
+ GROUP BY tk.pos_ustva
+
+ UNION -- alle Ausgaben AP erfassen
+
+ SELECT
+ sum(ac.amount) AS amount,
+ tk.pos_ustva
+ FROM acc_trans ac
+ JOIN AP ON (AP.id = ac.trans_id )
+ JOIN chart c ON (c.id = ac.chart_id)
+ LEFT JOIN taxkeys tk ON (
+ tk.id = (
+ SELECT id FROM taxkeys
+ WHERE chart_id=ac.chart_id
+ AND taxkey_id=ac.taxkey
+ AND startdate <= COALESCE(AP.transdate)
+ ORDER BY startdate DESC LIMIT 1
+ )
+ )
+ WHERE
+ 1=1
+ $APwhere
+ $dpt_where
+ $project
+ GROUP BY tk.pos_ustva
+
+ UNION -- alle Ausgaben und Einnahmen direkter gl Buchungen erfassen
+
+ SELECT sum
+ ( - ac.amount) AS amount,
+ tk.pos_ustva
+ FROM acc_trans ac
+ JOIN chart c ON (c.id = ac.chart_id)
+ JOIN gl a ON (a.id = ac.trans_id)
+ LEFT JOIN taxkeys tk ON (
+ tk.id = (
+ SELECT id FROM taxkeys
+ WHERE chart_id=ac.chart_id
+ AND taxkey_id=ac.taxkey
+
+ AND startdate <= COALESCE(ac.transdate)
+ ORDER BY startdate DESC LIMIT 1
+ )
+ )
+
+ $dpt_join
+ WHERE $where
+ $dpt_from
+ $project
+ GROUP BY tk.pos_ustva
+ |;
+
+ } else {
+#########################################
+# Method eq 'accrual' = Soll Versteuerung
+#########################################
+
+ if ($department_id) {
+ $dpt_join = qq|
+ JOIN dpt_trans t ON (t.trans_id = ac.trans_id)
+ |;
+ $dpt_where = qq|
+ AND t.department_id = $department_id
+ |;
+ }
+
+ $query = qq|
+ SELECT sum
+ (
+ CASE WHEN c.link LIKE '%AR%' THEN ac.amount * -1
+ WHEN c.link LIKE '%AP%' THEN ac.amount * 1
+ END
+ ) AS amount, c.$category
+ FROM acc_trans ac
+ JOIN chart c ON (c.id = ac.chart_id)
+ $dpt_join
+ WHERE $where
+ $dpt_where
+ $project
+ GROUP BY c.$category
+ |;
+ }
+
+ my @accno;
+ my $accno;
+ my $ref;
+
+ # Show all $query in Debuglevel LXDebug::QUERY
+ $callingdetails = (caller (0))[3];
+ $main::lxdebug->message(LXDebug::QUERY, "$callingdetails \$query=\n $query");
+
+ my $sth = $dbh->prepare($query);
+ $sth->execute || $form->dberror($query);
+
+ while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
+# Bug 365 solved?!
+ $ref->{amount} *= -1;
+ if ($category eq "pos_bwa") {
+ if ($last_period) {
+ $form->{ $ref->{$category} }{kumm} += $ref->{amount};
+ } else {
+ $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
+ }
+ } else {
+ $form->{ $ref->{$category} } += $ref->{amount};
+ }
+ }
+ $sth->finish;
+
+ $main::lxdebug->leave_sub();
+}
+
+
+
+1;