|
|
@@ -7,9 +7,8 @@ from pathlib import Path
|
|
|
from typing import Annotated
|
|
|
from urllib.parse import unquote, urlencode
|
|
|
|
|
|
-import pandas as pd
|
|
|
from docxtpl import DocxTemplate
|
|
|
-from fastapi import APIRouter, Depends, Form, HTTPException, Request, Response, status
|
|
|
+from fastapi import APIRouter, Depends, Form, HTTPException, Request, status
|
|
|
from fastapi.responses import (
|
|
|
FileResponse,
|
|
|
HTMLResponse,
|
|
|
@@ -29,7 +28,12 @@ from .docuware_search import get_invoice_pdf_by_document_no
|
|
|
from .jinja_templates import templates
|
|
|
from .ldap import DirectoryUnavailable, ldap_authenticate
|
|
|
from .oauth_token import create_access_token
|
|
|
-from .permissions import Principal, current_principal, ensure_forderung_access, highest_role, load_entitlements
|
|
|
+from .permissions import (
|
|
|
+ current_principal,
|
|
|
+ ensure_forderung_access,
|
|
|
+ highest_role,
|
|
|
+ load_entitlements,
|
|
|
+)
|
|
|
|
|
|
router = APIRouter()
|
|
|
logger = logging.getLogger(__name__)
|
|
|
@@ -66,11 +70,6 @@ def login_get(request: Request, next: str | None = None):
|
|
|
return response
|
|
|
|
|
|
|
|
|
-@router.get("/select", response_class=HTMLResponse)
|
|
|
-def select_get(request: Request):
|
|
|
- return templates.TemplateResponse(request, "base/select.html", {"request": request})
|
|
|
-
|
|
|
-
|
|
|
@router.post("/login")
|
|
|
def login_post(
|
|
|
request: Request,
|
|
|
@@ -140,6 +139,11 @@ def logout(request: Request):
|
|
|
return response
|
|
|
|
|
|
|
|
|
+@router.get("/select", response_class=HTMLResponse)
|
|
|
+def select_get(request: Request):
|
|
|
+ return templates.TemplateResponse(request, "base/select.html", {"request": request})
|
|
|
+
|
|
|
+
|
|
|
@router.get("/app/forderungen/liste", response_class=HTMLResponse)
|
|
|
def forderungen_liste(request: Request, db: Session = Depends(get_session), limit: int = 100, page: int = 0):
|
|
|
principal = current_principal(request)
|
|
|
@@ -345,82 +349,22 @@ def forderungen_dashboard(request: Request, db: Session = Depends(get_session)):
|
|
|
return templates.TemplateResponse(request, "forderungen/dashboard/dashboard.html", context)
|
|
|
|
|
|
|
|
|
-@router.get("/export/csv")
|
|
|
-def export_csv(request: Request, db: Session = Depends(get_session)):
|
|
|
- rows = _export_rows(db, current_principal(request))
|
|
|
- df = pd.DataFrame(rows)
|
|
|
- buf = io.StringIO()
|
|
|
- df.to_csv(buf, index=False)
|
|
|
- buf.seek(0)
|
|
|
- return Response(
|
|
|
- content=buf.getvalue(),
|
|
|
- media_type="text/csv",
|
|
|
- headers={"Content-Disposition": "attachment; filename=forderungen.csv"},
|
|
|
- )
|
|
|
-
|
|
|
-
|
|
|
-@router.get("/export/xlsx")
|
|
|
-def export_xlsx(request: Request, db: Session = Depends(get_session)):
|
|
|
- rows = _export_rows(db, current_principal(request))
|
|
|
- df = pd.DataFrame(rows)
|
|
|
- buf = io.BytesIO()
|
|
|
- with pd.ExcelWriter(buf, engine="openpyxl") as writer:
|
|
|
- df.to_excel(writer, index=False, sheet_name="Forderungen")
|
|
|
- buf.seek(0)
|
|
|
- return StreamingResponse(
|
|
|
- buf,
|
|
|
- media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
|
- headers={"Content-Disposition": "attachment; filename=forderungen.xlsx"},
|
|
|
- )
|
|
|
-
|
|
|
-
|
|
|
-def _export_rows(db: Session, principal: Principal) -> list[dict]:
|
|
|
- query = text(
|
|
|
- """
|
|
|
- SELECT F.Client_DB,
|
|
|
- F.Beleg_Nr AS Rechnungsnummer,
|
|
|
- F.Kunde_Text AS Kunde,
|
|
|
- F.Beleg_offen AS Betrag_offen,
|
|
|
- F.Beleg_Faellig_Datum AS Faelligkeit,
|
|
|
- F.Hauptbetrieb_ID,
|
|
|
- F.Standort_ID,
|
|
|
- F.Abteilung
|
|
|
- FROM dbo.Forderungen_Kopf AS F
|
|
|
- WHERE EXISTS (
|
|
|
- SELECT 1
|
|
|
- FROM dbo.Benutzer_Rechte AS BR
|
|
|
- INNER JOIN dbo.Rollen AS R ON R.Rolle = BR.Rolle AND R.Aktiv = 1
|
|
|
- WHERE LOWER(LTRIM(RTRIM(BR.Benutzer_ID))) = :auth_username
|
|
|
- AND NULLIF(LTRIM(RTRIM(BR.Benutzer_ID)), '') IS NOT NULL
|
|
|
- AND BR.Hauptbetrieb_ID = F.Hauptbetrieb_ID
|
|
|
- AND BR.Standort_ID = F.Standort_ID
|
|
|
- AND (BR.Abteilung IS NULL OR BR.Abteilung = F.Abteilung)
|
|
|
- )
|
|
|
- ORDER BY F.Beleg_Faellig_Datum, F.Beleg_Nr
|
|
|
- """
|
|
|
- )
|
|
|
- return [dict(row) for row in db.execute(query, {"auth_username": principal.username}).mappings().all()]
|
|
|
-
|
|
|
-
|
|
|
-@router.get("/detail/{id}/export/docx")
|
|
|
-def export_docx_legacy(id: int):
|
|
|
- raise HTTPException(status_code=410, detail="Veralteter Export; bitte die Forderungsdetailseite verwenden")
|
|
|
-
|
|
|
-
|
|
|
# TODO
|
|
|
@router.get("/app/forderungen/export/{client_db}_{document_no}/")
|
|
|
def export_docx2(request: Request, client_db: str, document_no: str, db: Session = Depends(get_session)):
|
|
|
principal = current_principal(request)
|
|
|
ensure_forderung_access(db, principal, client_db, document_no)
|
|
|
- q = db.execute(
|
|
|
- text(
|
|
|
- """SELECT TOP 1 F.*, K.Kunde_Name, K.Kunde_Adresse, K.Kunde_PLZ, K.Kunde_Ort
|
|
|
+ q = (
|
|
|
+ db.execute(
|
|
|
+ text("""SELECT TOP 1 F.*, K.Kunde_Name, K.Kunde_Adresse, K.Kunde_PLZ, K.Kunde_Ort
|
|
|
FROM dbo.Forderungen_Kopf AS F
|
|
|
LEFT JOIN dbo.Kunden AS K ON K.Client_DB = F.Client_DB AND K.Kunde_Nr = F.Kunde_Nr
|
|
|
- WHERE F.Client_DB = :client_db AND F.Beleg_Nr = :beleg_nr"""
|
|
|
- ),
|
|
|
- {"client_db": client_db, "beleg_nr": document_no},
|
|
|
- ).mappings().first()
|
|
|
+ WHERE F.Client_DB = :client_db AND F.Beleg_Nr = :beleg_nr"""),
|
|
|
+ {"client_db": client_db, "beleg_nr": document_no},
|
|
|
+ )
|
|
|
+ .mappings()
|
|
|
+ .first()
|
|
|
+ )
|
|
|
if not q or not settings.LOCAL_STORAGE:
|
|
|
raise HTTPException(status_code=503, detail="Dokumentablage ist nicht konfiguriert")
|
|
|
|