Feat: Quartalsbericht — Stornozeilen mit Minusbeträgen, nach Datum sortiert, Summen netten sich heraus (SW by-v976)

This commit is contained in:
rene 2026-05-15 13:27:05 +02:00
parent b10b3140eb
commit 6104132714
5 changed files with 81 additions and 32 deletions

View file

@ -413,25 +413,72 @@ def get_quarterly(year: int, q: int, admin=Depends(require_admin)):
period = f"Q{q} {year} ({labels[q]} {ends[q]})"
with db() as conn:
# Alle Rechnungen außer Entwürfe — Stornierte bleiben mit 0€ für lückenlose Nummerierung
# Alle Rechnungen außer Entwürfe im Quartal (nach Ausstellungsdatum)
rows = conn.execute(
"SELECT * FROM invoices WHERE status != 'draft' AND created_at >= ? AND created_at <= ? ORDER BY id",
"SELECT * FROM invoices WHERE status != 'draft' AND created_at >= ? AND created_at <= ? ORDER BY created_at ASC",
(from_date, to_date + "T23:59:59Z")
).fetchall()
# Summen nur für paid/sent (Stornierte zählen nicht zum Umsatz)
active = [r for r in rows if r["status"] in ("paid", "sent")]
total_net = sum(r["amount_net"] for r in active)
total_tax = sum(r["tax_amount"] for r in active)
total_gross = sum(r["amount_gross"] for r in active)
# Stornorechnungen die im Quartal ausgestellt wurden (cancelled_at im Zeitraum,
# auch wenn die Originalrechnung außerhalb des Quartals liegt)
storno_rows = conn.execute(
"SELECT * FROM invoices WHERE status = 'cancelled' AND cancelled_at >= ? AND cancelled_at <= ?",
(from_date, to_date + "T23:59:59Z")
).fetchall()
# Buchungseinträge aufbauen
entries = []
# Originalrechnungen (paid, sent — mit positivem Betrag)
for r in rows:
d = _row_to_dict(r)
if d["status"] in ("paid", "sent"):
entries.append(d)
elif d["status"] == "cancelled":
# Originalrechnung erscheint mit positivem Betrag (wurde ausgestellt)
entries.append(d)
# Stornozeilen: negative Beträge, Datum = cancelled_at, Nummer = cancellation_number
storno_ids_already = {r["id"] for r in rows}
for r in storno_rows:
d = _row_to_dict(r)
storno_entry = {
"invoice_number": d["cancellation_number"] or f"ST-{d['invoice_number']}",
"recipient_name": d["recipient_name"],
"recipient_email": d["recipient_email"],
"created_at": d["cancelled_at"],
"service_period": d["service_period"],
"amount_net": -round(d["amount_net"], 2),
"tax_amount": -round(d.get("tax_amount") or 0, 2),
"amount_gross": -round(d["amount_gross"], 2),
"paid_amount": None,
"status": "storno",
"sent_at": None,
"paid_at": None,
"cancellation_number": d["cancellation_number"],
"notes": f"Storno zu {d['invoice_number']}",
}
entries.append(storno_entry)
# Wenn Original NICHT im Quartal aber Storno schon → Original trotzdem zeigen
if r["id"] not in storno_ids_already:
orig = _row_to_dict(r)
entries.append(orig)
# Nach Datum sortieren
entries.sort(key=lambda e: (e.get("created_at") or ""))
# Summen: Originalrechnungen positiv + Stornos negativ
total_net = sum(e["amount_net"] for e in entries if e["status"] != "cancelled")
total_tax = sum(e.get("tax_amount") or 0 for e in entries if e["status"] != "cancelled")
total_gross = sum(e["amount_gross"] for e in entries if e["status"] != "cancelled")
return {
"period": period,
"invoices": [_row_to_dict(r) for r in rows],
"total_net": round(total_net, 2),
"total_tax": round(total_tax, 2),
"period": period,
"invoices": entries,
"total_net": round(total_net, 2),
"total_tax": round(total_tax, 2),
"total_gross": round(total_gross, 2),
"count": len(rows),
"count": len(entries),
}