c-stone-invoice-check/backend/routers/invoices.py
Markus Kruse d2c3d29ad2 feat(invoice-check): Nextcloud-Commits squashed
Enthält ~18 Commits vom Nextcloud-Mount (FUSE-Blocker verhinderte
direkten Push): Excel-Export-Fix, IMAP-Verbesserungen, Scan-Fixes,
UI-Updates. Aus Forgejo-Migration squashed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-10-01 00:48:20 +02:00

400 lines
13 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
C-Stone Invoice Check — Invoice Router
Rechnungs-Verarbeitung und Speicherung
"""
import io
import os
import fitz # PyMuPDF
import re
import json
import urllib.request
from datetime import datetime
from typing import List, Optional, Dict, Any
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
from fastapi.responses import StreamingResponse
from sqlalchemy.orm import Session
from openpyxl import Workbook
from openpyxl.styles import Font
from backend.database import get_db
from backend.models import Invoice, InvoiceDetail, EmailLog
router = APIRouter()
# Ablagepfad (Evo-X2)
INVOICE_STORAGE_PATH = os.getenv("INVOICE_STORAGE_PATH", "/Volumes/Evo-X2/Rechnungen")
# Steuer-Schlüsselwörter
TAX_KEYWORDS = {
"mit_mwst": ["mwst", "mehrwertsteuer", "steuer", "incl. mwst", "zzgl. mwst"],
"ohne_mwst": ["zzgl. steuer", "exkl. steuer", "steuerfrei", "steuerbefreit"],
"umsatzsteuer": ["umsatzsteuer", "ust", "ust-id"],
"vat": ["vat", "v.a.t.", "value added tax"],
}
def detect_tax_type(text: str) -> Dict[str, Any]:
"""Erkennt Steuer-Art aus PDF-Text"""
text_lower = text.lower()
# Prüfen auf "mit MwSt"
if any(kw in text_lower for kw in TAX_KEYWORDS["mit_mwst"]):
# Prüfen auf Satz: z.B. "inkl. 19 % MwSt"
match = re.search(r'(?:inkl|zzgl)[\s\-]*(?:der)?[\s\-]*(\d+)[\s\-]*%[\s\-]*(?:mwst|steuer)', text_lower)
if match:
tax_rate = float(match.group(1)) / 100
return {
"tax_type": "mit MwSt",
"tax_rate": tax_rate,
"confidence": 0.9,
"found_keyword": match.group(0)
}
# Prüfen auf "ohne MwSt"
if any(kw in text_lower for kw in TAX_KEYWORDS["ohne_mwst"]):
return {
"tax_type": "ohne MwSt",
"tax_rate": 0.0,
"confidence": 0.85,
"found_keyword": next(kw for kw in TAX_KEYWORDS["ohne_mwst"] if kw in text_lower)
}
# Prüfen auf Umsatzsteuer
if any(kw in text_lower for kw in TAX_KEYWORDS["umsatzsteuer"]):
return {
"tax_type": "Umsatzsteuer",
"tax_rate": 0.19, # Standard
"confidence": 0.7,
"found_keyword": next(kw for kw in TAX_KEYWORDS["umsatzsteuer"] if kw in text_lower)
}
# Prüfen auf VAT (europäische Steuer)
if any(kw in text_lower for kw in TAX_KEYWORDS["vat"]):
return {
"tax_type": "VAT",
"tax_rate": 0.19, # Standard EU
"confidence": 0.7,
"found_keyword": next(kw for kw in TAX_KEYWORDS["vat"] if kw in text_lower)
}
# Standard: Prüfen auf Steuerangabe im Text
tax_matches = re.findall(r'(\d+)[\s\-]*%[\s\-]*(?:steuer|mwst|ust|vat)', text_lower)
if tax_matches:
tax_rate = float(tax_matches[0]) / 100
return {
"tax_type": "mit Steuer",
"tax_rate": tax_rate,
"confidence": 0.6,
"found_keyword": f"{tax_matches[0]}%"
}
# Keine Steuer erkannt
return {
"tax_type": "unbekannt",
"tax_rate": None,
"confidence": 0.3,
"found_keyword": None
}
def extract_amount(text: str) -> Optional[float]:
"""Extrahiert Betrag aus PDF-Text"""
# Muster: "Summe: 123,45 €" oder "Total: 123.45 EUR"
patterns = [
r'(?:summe|total|betrag)[\s:]*([\d\.]+[\s\,]\d{2})\s*€?',
r'([\d\.]+[\s\,]\d{2})\s*(?:€|EUR|EUR\*)',
r'(?:zu\s+bezahlen|gesamt)[\s:]*([\d\.]+[\s\,]\d{2})',
]
for pattern in patterns:
match = re.search(pattern, text.lower())
if match:
amount_str = match.group(1)
# Komma zu Punkt für float
amount_str = amount_str.replace('.', '').replace(',', '.')
try:
return float(amount_str)
except ValueError:
continue
return None
def extract_invoice_number(text: str) -> Optional[str]:
"""Extrahiert Rechnungsnummer aus PDF-Text"""
patterns = [
r'(?:rechnungs)?nr\.?\s*[:\s]*([A-Za-z0-9\-/]+)',
r'(?:invoice|rechnung)?[\s_]*(?:no\.?|#)?\s*[:\s]*([A-Za-z0-9\-/]+)',
r'(?:rechnung|invoice)\s*(\d{6,})',
]
for pattern in patterns:
match = re.search(pattern, text.lower())
if match:
return match.group(1).strip()
return None
def extract_issuer(text: str) -> Optional[str]:
"""Extrahiert Rechnungsausteller aus PDF-Text"""
patterns = [
r'(?:von|von\s+haus|ausgestellt\s+vom|issuer)[:\s]*\n?([A-Z][A-Za-z\s]+(?:GmbH|AG|UG|KG|e\.K\.|GmbH&Co\.KG))',
r'(?:seit|seit\s+dem|seit\s+vom)[\s\S]{0,100}?(?:rechnung|invoice)',
r'^(.+?)[\n\r]{1,2}(?:rechnung|invoice|rechnungsnummer)',
]
for pattern in patterns:
match = re.search(pattern, text, re.IGNORECASE)
if match:
return match.group(1).strip()
return None
AI_URL = os.getenv("AI_MODEL_URL", "http://192.168.50.124:8085")
AI_PROMPT = """Du bist ein Buchhalter. Analysiere den folgenden Dokumenttext.
Ist das Dokument eine Rechnung/Invoice? Wenn NEIN (z.B. AGB, Kontoauszug, Mahnung, Vertrag), antworte exakt:
{"is_invoice": false}
Wenn JA, extrahiere die Daten und antworte exakt mit diesem JSON (kein Markdown, keine Erklärung):
{
"is_invoice": true,
"invoice_number": "Rechnungsnummer oder null",
"invoice_date": "YYYY-MM-DD oder null",
"amount_gross": 0.00,
"issuer": "Firmenname des Ausstellers (nicht Empfänger) oder null",
"tax_type": "mit MwSt|ohne MwSt|Umsatzsteuer|VAT|unbekannt",
"tax_rate": 0.19
}
Dokumenttext:
"""
def clean_pdf_text(text: str) -> str:
# Bidi-Kontrollzeichen und Zero-Width-Chars entfernen
return re.sub(r'[​-‏‪-‮⁦-⁩]', '', text)
def _call_ai(content: str) -> Optional[str]:
"""HTTP-Call an lokales Gemma-Modell."""
try:
payload = json.dumps({
"model": "gemma-4-26b",
"messages": [{"role": "user", "content": content}],
"max_tokens": 2000,
"temperature": 0.0,
}).encode()
req = urllib.request.Request(
f"{AI_URL}/v1/chat/completions",
data=payload,
headers={"Content-Type": "application/json"},
)
with urllib.request.urlopen(req, timeout=90) as resp:
result = json.loads(resp.read())
return result["choices"][0]["message"]["content"].strip()
except Exception:
return None
def analyze_with_ai(text: str) -> Optional[Dict[str, Any]]:
"""Analysiert Rechnungstext mit lokalem Gemma-Modell. Gibt None zurück wenn kein Invoice."""
raw = _call_ai(AI_PROMPT + text[:4000])
if not raw:
return None
start, end = raw.find("{"), raw.rfind("}") + 1
if start < 0 or end <= start:
return None
try:
data = json.loads(raw[start:end])
if not data.get("is_invoice"):
return {"is_invoice": False}
return data
except Exception:
return None
def extract_pdf_data(file_path: str) -> Dict[str, Any]:
"""Extrahiert alle relevanten Daten aus PDF-Datei"""
try:
doc = fitz.open(file_path)
text = ""
pages = 0
for page in doc:
text += page.get_text()
pages += 1
doc.close()
text = clean_pdf_text(text)
# AI-Analyse (primär), Regex als Fallback
ai = analyze_with_ai(text)
# Kein Invoice laut AI → als nicht-Rechnung markieren
if ai and not ai.get("is_invoice", True):
return {"not_invoice": True, "text": text, "pages": pages}
if ai and ai.get("is_invoice"):
tax_type = ai.get("tax_type", "unbekannt")
return {
"text": text,
"invoice_number": ai.get("invoice_number") or extract_invoice_number(text),
"amount": ai.get("amount_gross") or extract_amount(text),
"issuer": ai.get("issuer") or extract_issuer(text),
"tax_info": {
"tax_type": tax_type,
"tax_rate": ai.get("tax_rate", 0.19 if "mit" in tax_type.lower() else 0.0),
"confidence": 0.95,
},
"pages": pages,
"ai_analyzed": True,
}
# Fallback: Regex
return {
"text": text,
"invoice_number": extract_invoice_number(text),
"amount": extract_amount(text),
"issuer": extract_issuer(text),
"tax_info": detect_tax_type(text),
"pages": pages,
"ai_analyzed": False,
}
except Exception as e:
return {
"error": str(e),
"pages": 0
}
def get_storage_path(year: int, tax_type: str) -> str:
"""Erstellt Ablagepfad auf Evo-X2"""
base_path = INVOICE_STORAGE_PATH
# Steuer-Verzeichnis
if tax_type and "mit" in tax_type.lower():
tax_dir = "mit MwSt"
elif tax_type and "ohne" in tax_type.lower():
tax_dir = "ohne MwSt"
else:
tax_dir = "ohne MwSt" # Default
return os.path.join(base_path, str(year), tax_dir)
def build_excel(invoices: List[Invoice]) -> "io.BytesIO":
import io
wb = Workbook()
ws = wb.active
ws.title = "Rechnungen"
headers = ["Datum", "Rechnungsausteller", "Rechnungsnummer", "Betrag (brutto)", "Steuerart", "Steuersatz"]
for col, header in enumerate(headers, 1):
cell = ws.cell(row=1, column=col, value=header)
cell.font = Font(bold=True)
for row, inv in enumerate(invoices, 2):
ws.cell(row=row, column=1, value=inv.invoice_date.strftime("%Y-%m-%d") if inv.invoice_date else "")
ws.cell(row=row, column=2, value=inv.issuer_name or "")
ws.cell(row=row, column=3, value=inv.invoice_number or "")
ws.cell(row=row, column=4, value=inv.invoice_amount or 0)
ws.cell(row=row, column=5, value=inv.invoice_tax_type or "")
ws.cell(row=row, column=6, value=f"{int((inv.invoice_tax_rate or 0) * 100)} %" if inv.invoice_tax_rate else "")
for col in range(1, 7):
ws.column_dimensions[chr(64 + col)].width = 22
buf = io.BytesIO()
wb.save(buf)
buf.seek(0)
return buf
@router.get("/scan-pdf")
async def scan_pdf_file(file_path: str, db: Session = Depends(get_db)):
"""Scannt einzelne PDF-Datei"""
if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="Datei nicht gefunden")
data = extract_pdf_data(file_path)
if "error" in data:
return {"error": data["error"]}
# Speichern in DB
invoice = Invoice(
file_path=file_path,
file_name=os.path.basename(file_path),
invoice_number=data.get("invoice_number"),
invoice_amount=data.get("amount"),
issuer_name=data.get("issuer"),
invoice_tax_type=data.get("tax_info", {}).get("tax_type"),
invoice_tax_rate=data.get("tax_info", {}).get("tax_rate"),
confidence_score=data.get("tax_info", {}).get("confidence", 0),
processed=True
)
db.add(invoice)
db.commit()
# Details speichern
detail = InvoiceDetail(
invoice_id=invoice.id,
raw_text=data.get("text", ""),
parsed_data={
"invoice_number": data.get("invoice_number"),
"amount": data.get("amount"),
"issuer": data.get("issuer"),
"tax_info": data.get("tax_info")
}
)
db.add(detail)
db.commit()
return {
"id": invoice.id,
"invoice_number": data.get("invoice_number"),
"amount": data.get("amount"),
"issuer": data.get("issuer"),
"tax_type": data.get("tax_info", {}).get("tax_type"),
"tax_rate": data.get("tax_info", {}).get("tax_rate"),
"pages": data.get("pages", 0)
}
@router.get("/export-excel")
async def export_to_excel(db: Session = Depends(get_db)):
"""Liefert Excel-Datei als direkten Browser-Download."""
from fastapi.responses import StreamingResponse
invoices = db.query(Invoice).order_by(Invoice.invoice_date.desc()).all()
buf = build_excel(invoices)
filename = f"rechnungen_{datetime.utcnow().strftime('%Y%m%d')}.xlsx"
return StreamingResponse(
buf,
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={"Content-Disposition": f"attachment; filename={filename}"},
)
@router.get("/list")
async def list_invoices(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
"""Listet alle Rechnungen"""
invoices = db.query(Invoice).offset(skip).limit(limit).all()
return {
"invoices": [
{
"id": inv.id,
"invoice_number": inv.invoice_number,
"invoice_date": inv.invoice_date.isoformat() if inv.invoice_date else None,
"issuer_name": inv.issuer_name,
"invoice_amount": inv.invoice_amount,
"invoice_tax_type": inv.invoice_tax_type,
"file_path": inv.file_path,
"processed": inv.processed
}
for inv in invoices
],
"total": db.query(Invoice).count()
}