- my $dbh = $form->dbconnect($myconfig);
-
- if ($form->{method} eq "cash") {
- $acc_cash_where = qq| AND (a.trans_id IN (SELECT id FROM ar WHERE datepaid>=(select date_trunc('year', current_date)) UNION SELECT id FROM ap WHERE datepaid>=(select date_trunc('year', current_date)) UNION SELECT id FROM gl WHERE transdate>=(select date_trunc('year', current_date)))) |;
- } else {
- $acc_cash_where = " AND ((select date_trunc('year', a.transdate::date)) >= (select date_trunc('year', current_date)))";
+ my $dbh = SL::DB->client->dbh;
+
+ # bug 1071 Warum sollte bei Erreichen eines neuen Jahres die Kontenübersicht nur noch die
+ # bereits bebuchten Konten anzeigen?
+ # Folgende Erweiterung:
+ # 1.) Gehe zurück bis zu dem Datum an dem die Bücher geschlossen wurden
+ # 2.) Falls die Bücher noch nie geschlossen wurden, gehe zurück bis zum Bearbeitungsstart
+ # COALESCE((SELECT closedto FROM defaults),(SELECT itime FROM defaults))
+ # PROBLEM: Das date_trunc schneidet auf den 1.1.20XX ab und KEINE Buchungen werden angezeigt
+ # Lösung: date_trunc rausgeworfen und nicht mehr auf itime geprüft, sondern auf die erste Buchung
+ # in transdate jan 11.04.2011
+
+ my $closedto_sql = "COALESCE((SELECT closedto FROM defaults),
+ (SELECT transdate from acc_trans order by transdate limit 1))";
+
+ if ($form->{method} eq "cash") { # EÜR
+ $acc_cash_where = qq| AND (a.trans_id IN (SELECT id FROM ar WHERE datepaid>= $closedto_sql
+ UNION SELECT id FROM ap WHERE datepaid>= $closedto_sql
+ UNION SELECT id FROM gl WHERE transdate>= $closedto_sql
+ )) |;
+ } else { # Bilanzierung
+ $acc_cash_where = " AND (a.transdate >= $closedto_sql) ";