| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- from typing import Literal
- from urllib.parse import quote
- from fastapi import HTTPException
- from sqlalchemy import text
- from sqlalchemy.orm import Session
- from .jinja_templates import templates
- HistoryScope = Literal["invoice", "open", "vehicle", "debtor"]
- def history_context(db: Session, pkey: dict[str, str], section: str, scope: HistoryScope = "invoice") -> dict:
- values = {"client_db": pkey["Client_DB"], "beleg_nr": pkey["Beleg_Nr"], "scope": scope}
- invoice = (
- db.execute(
- text("SELECT TOP 1 * FROM dbo.Forderungen_Kopf WHERE Client_DB = :client_db AND Beleg_Nr = :beleg_nr"),
- values,
- )
- .mappings()
- .first()
- )
- if invoice is None:
- raise HTTPException(status_code=404, detail="Forderung nicht gefunden")
- return section_context(db, pkey, invoice, section, scope)
- def section_context(db: Session, pkey: dict[str, str], invoice, section: str, scope: HistoryScope = "invoice") -> dict:
- # Neither the customer nor the vehicle can be supplied by the browser.
- invoice = getattr(invoice, "_mapping", invoice)
- vehicle_missing = not str(invoice["Fahrzeug_Nr"] or "").strip()
- values = {"client_db": pkey["Client_DB"], "beleg_nr": pkey["Beleg_Nr"], "scope": scope}
- rows = []
- if scope != "vehicle" or not vehicle_missing:
- query = templates.env.get_template(f"forderungen/details/queries/forderungen_{section}.sql").render(scope=scope)
- rows = db.execute(
- text(query).execution_options(query_name=f"forderungen/details/queries/forderungen_{section}.sql"), values
- ).fetchall()
- return {
- "forderungen_details": [invoice],
- f"forderungen_{section}": rows,
- f"{section}_scope": scope,
- "vehicle_missing": vehicle_missing,
- "detail_url": "/app/forderungen/details/" + quote(f"{pkey['Client_DB']}_{pkey['Beleg_Nr']}", safe=""),
- }
|