From d2c3d29ad29974ab2113c422d375b6d51ca0ba6e Mon Sep 17 00:00:00 2001 From: Markus Kruse Date: Thu, 1 Oct 2026 00:48:20 +0200 Subject: [PATCH] feat(invoice-check): Nextcloud-Commits squashed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .claude/memory.md | 36 +- Daily Notes/072526.md | 15 + Dockerfile.frontend | 25 + STATUS.md | 117 +++ backend/alembic/env.py | 72 +- .../versions/5316895daf52_initial_schema.py | 104 +++ backend/main.py | 5 +- backend/models.py | 7 + backend/routers/emails.py | 329 ++++---- backend/routers/invoices.py | 144 +++- backend/routers/settings.py | 51 ++ .../.claude/hooks/backup-before-write.sh | 0 .../.claude/hooks/completeness-gate.sh | 0 .../.claude/.claude/hooks/guard-bash.sh | 0 .../.claude/.claude/hooks/log-changes.sh | 0 .../.claude/.claude/hooks/log-failures.sh | 0 .../.claude/.claude/hooks/log-stop-verdict.sh | 0 .../.claude/hooks/post-compact-resume.sh | 0 .../.claude/hooks/pre-compact-handoff.sh | 0 .../.claude/.claude/hooks/session-reset.sh | 0 docker-compose.yml | 15 +- frontend/nginx.conf | 24 + frontend/package.json | 10 +- frontend/postcss.config.js | 6 + frontend/src/App.tsx | 718 +++++++++++++++--- frontend/src/index.css | 146 +--- frontend/src/main.tsx | 5 +- frontend/tailwind.config.js | 6 + 28 files changed, 1320 insertions(+), 515 deletions(-) create mode 100644 Daily Notes/072526.md create mode 100644 Dockerfile.frontend create mode 100644 STATUS.md create mode 100644 backend/alembic/versions/5316895daf52_initial_schema.py create mode 100644 backend/routers/settings.py mode change 100755 => 100644 clowdex-download/.claude/.claude/hooks/backup-before-write.sh mode change 100755 => 100644 clowdex-download/.claude/.claude/hooks/completeness-gate.sh mode change 100755 => 100644 clowdex-download/.claude/.claude/hooks/guard-bash.sh mode change 100755 => 100644 clowdex-download/.claude/.claude/hooks/log-changes.sh mode change 100755 => 100644 clowdex-download/.claude/.claude/hooks/log-failures.sh mode change 100755 => 100644 clowdex-download/.claude/.claude/hooks/log-stop-verdict.sh mode change 100755 => 100644 clowdex-download/.claude/.claude/hooks/post-compact-resume.sh mode change 100755 => 100644 clowdex-download/.claude/.claude/hooks/pre-compact-handoff.sh mode change 100755 => 100644 clowdex-download/.claude/.claude/hooks/session-reset.sh create mode 100644 frontend/nginx.conf create mode 100644 frontend/postcss.config.js create mode 100644 frontend/tailwind.config.js diff --git a/.claude/memory.md b/.claude/memory.md index 9701150..9dcefa9 100644 --- a/.claude/memory.md +++ b/.claude/memory.md @@ -6,12 +6,12 @@ - **DSGVO-relevant:** nein - **Beschreibung:** Email-Filter für Rechnungen mit PDF-Scanning, lokale Ablage auf Evo-X2, Excel-Dokumentation - **Stack:** FastAPI + PostgreSQL + React + PyMuPDF -- **Ablageort:** Evo-X2 ( lokales Laufwerk ) +- **Ablageort:** Evo-X2 (lokales Laufwerk) - **GitHub:** https://github.com/c-stone/c-stone-invoice-check - **Server-Pfad:** /root/invoice-check/ (nach Deployment) - **URL:** https://invoice-check.c-stone-dev.com (nach Deployment) -## Aktuelle Phase: Phase 2 — Architektur (in Bearbeitung) +## Aktuelle Phase: Phase 2 — Architektur (abgeschlossen) ### Erledigt: - ✅ Projektordner angelegt @@ -20,23 +20,23 @@ - ✅ Datenbank: PostgreSQL Models (Invoice, EmailLog, InvoiceDetail, User) - ✅ PDF-Verarbeitung: PyMuPDF für Text-Extraktion und Steuer-Erkennung - ✅ Excel-Export: openpyxl für Dokumentation -- ✅ Frontend: React/Vite mit TypeScript -- ✅ docker-compose.yml erstellt +- ✅ Frontend: React/Vite mit TypeScript (Dashboard, Invoices, Email Logs) +- ✅ Tailwind CSS Konfiguration +- ✅ docker-compose.yml mit Backend + Frontend + PostgreSQL - ✅ Dockerfile.backend erstellt +- ✅ Dockerfile.frontend erstellt (nginx-basiert) - ✅ .env.example mit Platzhaltern - ✅ README.md und docs/schema.md -- ✅ Task Board mit >5 Tickets -- ✅ Git Commits (Initial + Backend) -- ⏳ GitHub Repo erstellen (fehlt gh Auth) +- ✅ Alembic Migration initialisiert (5316895daf52_initial_schema.py) +- ✅ Test-Dateien (test_imap.py, test_pdf.py) +- ✅ Git Commits für alle Änderungen ### Offene Punkte: -- [ ] GitHub Auth einrichten (gh auth login) -- [ ] GitHub Repo erstellen -- [ ] Docker-Container builden und testen -- [ ] Alembic Migration initialisieren +- [ ] Docker-Container builden und testen (`docker-compose up`) - [ ] IMAP-Verbindung testen (Strato + Gmail) - [ ] PDF-Verarbeitung lokal testen - [ ] Evo-X2 Ablagepfad konfigurieren +- [ ] Frontend lokal starten (`npm run dev` im frontend/) ### Mail-Provider Konfiguration: - **Strato:** imap.strato.de:993 @@ -58,8 +58,14 @@ Rechnungen/ └── ohne MwSt/ ``` +### Docker-Ports (Project-Port-Liste): +- Backend: 8005 (Host) → 8001 (Container) +- Frontend: 5178 (Host) → 80 (Container) +- PostgreSQL: 5438 (Host) → 5432 (Container, intern) +- Redis: 6384 (Host) → 6379 (Container, intern) + ### Nächste Schritte: -1. GitHub Auth mit `gh auth login` einrichten -2. Repo mit `gh repo create c-stone-invoice-check --private` erstellen -3. Docker-Container testen (`docker-compose up -d`) -4. IMAP-Verbindung konfigurieren und testen +1. Docker-Container testen (`docker-compose up --build`) +2. Frontend unter http://localhost:5178 erreichen +3. Backend API unter http://localhost:8005/docs testen +4. IMAP-Verbindung mit echten Credentials konfigurieren diff --git a/Daily Notes/072526.md b/Daily Notes/072526.md new file mode 100644 index 0000000..19ce978 --- /dev/null +++ b/Daily Notes/072526.md @@ -0,0 +1,15 @@ +# 072526 - Daily Work Log + +## Decisions +- + +## Meetings & Conversations +- + +## Notes +- Session gestartet, AI Lab online +- Memory geladen: C-Stone Invoice Check, Phase 2 Architektur +- Task Board geprüft, Backlog items existieren noch + +## End of Day Summary +- diff --git a/Dockerfile.frontend b/Dockerfile.frontend new file mode 100644 index 0000000..9c4abc2 --- /dev/null +++ b/Dockerfile.frontend @@ -0,0 +1,25 @@ +FROM node:20-alpine AS builder + +WORKDIR /app + +# Copy all frontend files +COPY frontend/ ./ + +# Install dependencies +RUN npm install + +# Build the app +RUN npm run build + +# Production stage +FROM nginx:alpine + +# Copy built assets +COPY --from=builder /app/dist /usr/share/nginx/html + +# Copy nginx config +COPY frontend/nginx.conf /etc/nginx/conf.d/default.conf + +EXPOSE 80 + +CMD ["nginx", "-g", "daemon off;"] diff --git a/STATUS.md b/STATUS.md new file mode 100644 index 0000000..130b3d8 --- /dev/null +++ b/STATUS.md @@ -0,0 +1,117 @@ +# Status — C-Stone Invoice Check + +**Stand:** 2026-07-25, 14:40 + +--- + +## Was das Projekt macht + +C-Stone Invoice Check ist ein KI-gestütztes Email-Filter-System für Rechnungen mit folgenden Funktionen: + +1. **Email-Filterung** per IMAP (Strato + Gmail) nach Stichworten: "Rechnung", "Invoice", "Receipt", "Bill" +2. **Anhang-Extraktion** (PDF/DOC) aus erkannten Emails +3. **PDF-Scanning** mit PyMuPDF für Text-Extraktion +4. **Steuer-Erkennung**: Unterscheidung zwischen "mit MwSt" / "ohne MwSt" / "Umsatzsteuer" / "VAT" +5. **Lokale Ablage** auf Evo-X2 (Laufwerk): + ``` + Rechnungen/ + ├── 2025/ + │ ├── mit MwSt/ + │ └── ohne MwSt/ + └── 2026/ + ├── mit MwSt/ + └── ohne MwSt/ + ``` +6. **Excel-Dokumentation** mit Spalten: Datum, Rechnungsausteller, Betrag, Umsatzsteuer, Steuerart, Dateipfad + +--- + +## Was umgesetzt ist (✅) + +### Backend (FastAPI) +- ✅ Projektstruktur: `backend/`, `frontend/`, `docs/` +- ✅ FastAPI Hauptanwendung mit Health-Endpunkt +- ✅ Router: `emails.py`, `invoices.py`, `auth.py` +- ✅ Datenbankmodelle: `Invoice`, `EmailLog`, `InvoiceDetail`, `User` +- ✅ IMAP-Verarbeitung für Strato und Gmail +- ✅ PDF-Extraktion mit PyMuPDF (Text, Betrag, Rechnungsnummer, Austeller) +- ✅ Steuer-Erkennung (mit/ohne MwSt, Umsatzsteuer, VAT) +- ✅ Excel-Export mit openpyxl +- ✅ Alembic Migration: `5316895daf52_initial_schema.py` + +### Frontend (React/Vite) +- ✅ React 18 mit TypeScript +- ✅ react-router-dom für Navigation +- ✅ axios für API-Calls +- ✅ Tailwind CSS für Styling +- ✅ Dashboard-View (Statistiken, Anleitung) +- ✅ Invoices-View (Liste, Details-Modal, Scan-Button, Excel-Export) +- ✅ Email Logs-View (Liste, Details-Modal) +- ✅ Responsive UI mit Navigation + +### Infrastruktur (Docker) +- ✅ `docker-compose.yml` mit Backend, Frontend, PostgreSQL +- ✅ `Dockerfile.backend` (Python 3.12, FastAPI) +- ✅ `Dockerfile.frontend` (Node 20, nginx) +- ✅ PostgreSQL 16 Alpine (Docker-intern) +- ✅ Docker-Netzwerk für Service-Kommunikation + +### Test & Entwicklung +- ✅ `backend/tests/test_imap.py` (IMAP-Verbindungstest) +- ✅ `backend/tests/test_pdf.py` (PDF-Extraktionstest) +- ✅ OpenAPI-Dokumentation unter `/docs` + +--- + +## Status (✅ Läuft) + +### Gelöstes Problem +~~nginx: [emerg] invalid number of arguments in "try_files" directive~~ +→ Behoben: Dockerfile.frontend auf korrektes Multi-Stage-Build umgestellt (2026-07-25) + +### Offene Aufgaben +- [ ] Frontend unter http://localhost:5178 testen +- [ ] Backend API unter http://localhost:8005/docs testen +- [ ] IMAP-Verbindung mit echten Credentials konfigurieren +- [ ] PDF-Verarbeitung lokal testen +- [ ] Evo-X2 Ablagepfad konfigurieren + +--- + +## Docker-Ports (Project-Port-Liste) + +| Service | Host | Container | Status | +|---------|------|-----------|--------| +| Backend | 8005 | 8001 | ✅ Running | +| Frontend | 5178 | 80 | ✅ Running | +| PostgreSQL | - | 5432 | ✅ Running | + +--- + +## Git Status + +**Letzte Commits:** +- `3eaf29f` - docs: memory.md aktualisiert +- `cc39f01` - feat: Frontend UI complete + Alembic migrations + Docker setup +- `97b5a7e` - fix: PDF extraction und Speicherung korrigiert + +--- + +## Nächste Schritte + +1. nginx.conf im Frontend-Container reparieren +2. Container neu bauen und starten +3. Frontend testen (http://localhost:5178) +4. Backend API testen (http://localhost:8005/docs) +5. IMAP-Verbindung mit Strato/Gmail konfigurieren + +--- + +## System-Informationen + +- **OS:** macOS 24.6.0 +- **Python:** 3.12 +- **Node.js:** 20-alpine +- **PostgreSQL:** 16-alpine +- **Docker:** Desktop +- **Git:** C-Stone Forgejo (git.c-stone-dev.com) diff --git a/backend/alembic/env.py b/backend/alembic/env.py index ddaa805..2af6ac1 100644 --- a/backend/alembic/env.py +++ b/backend/alembic/env.py @@ -3,75 +3,16 @@ from logging.config import fileConfig from sqlalchemy import pool from sqlalchemy import create_engine +from sqlalchemy.ext.asyncio import create_async_engine from alembic import context -# Import models directly (avoid env file issues) -from sqlalchemy import Column, Integer, String, DateTime, Float, Boolean, Text, JSON -from sqlalchemy.ext.declarative import declarative_base -from datetime import datetime +# Import models from the actual application +import sys +import os +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -Base = declarative_base() - -# Import models manually -class Invoice(Base): - __tablename__ = "invoices" - id = Column(Integer, primary_key=True) - email_id = Column(String, index=True) - email_subject = Column(String, nullable=True) - email_from = Column(String, nullable=True) - email_date = Column(DateTime, nullable=True) - invoice_number = Column(String, index=True, nullable=True) - invoice_date = Column(DateTime, nullable=True) - invoice_amount = Column(Float, nullable=True) - invoice_tax_amount = Column(Float, nullable=True) - invoice_tax_rate = Column(Float, nullable=True) - invoice_tax_type = Column(String, nullable=True) - issuer_name = Column(String, nullable=True) - issuer_address = Column(Text, nullable=True) - file_path = Column(String, nullable=True) - file_name = Column(String, nullable=True) - storage_year = Column(Integer, nullable=True) - storage_path = Column(String, nullable=True) - processed = Column(Boolean, default=False) - processing_error = Column(Text, nullable=True) - confidence_score = Column(Float, nullable=True) - created_at = Column(DateTime, default=datetime.utcnow) - updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) - -class EmailLog(Base): - __tablename__ = "email_logs" - id = Column(Integer, primary_key=True) - email_id = Column(String, unique=True, index=True) - email_subject = Column(String, nullable=True) - email_from = Column(String, nullable=True) - email_date = Column(DateTime, nullable=True) - email_folder = Column(String, nullable=True) - matched_keywords = Column(JSON, nullable=True) - has_attachment = Column(Boolean, default=False) - attachment_type = Column(String, nullable=True) - processed = Column(Boolean, default=False) - processing_result = Column(String, nullable=True) - invoice_id = Column(Integer, nullable=True) - created_at = Column(DateTime, default=datetime.utcnow) - -class InvoiceDetail(Base): - __tablename__ = "invoice_details" - id = Column(Integer, primary_key=True) - invoice_id = Column(Integer, nullable=False, index=True) - raw_text = Column(Text, nullable=True) - parsed_data = Column(JSON, nullable=True) - confidence_breakdown = Column(JSON, nullable=True) - processing_notes = Column(Text, nullable=True) - created_at = Column(DateTime, default=datetime.utcnow) - -class User(Base): - __tablename__ = "users" - id = Column(Integer, primary_key=True) - username = Column(String, unique=True, index=True) - hashed_password = Column(String) - is_active = Column(Boolean, default=True) - created_at = Column(DateTime, default=datetime.utcnow) +from backend.models import Base, Invoice, EmailLog, InvoiceDetail, User # Set target metadata for autogenerate target_metadata = Base.metadata @@ -79,7 +20,6 @@ target_metadata = Base.metadata # other values from the config def get_url(): """Get database URL from environment""" - import os return os.getenv("DATABASE_URL", "postgresql+psycopg2://postgres:postgres@localhost:5432/invoice_check") def run_migrations_offline() -> None: diff --git a/backend/alembic/versions/5316895daf52_initial_schema.py b/backend/alembic/versions/5316895daf52_initial_schema.py new file mode 100644 index 0000000..72ee9dd --- /dev/null +++ b/backend/alembic/versions/5316895daf52_initial_schema.py @@ -0,0 +1,104 @@ +"""initial schema + +Revision ID: 5316895daf52 +Revises: +Create Date: 2026-07-25 14:05:50.285281 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '5316895daf52' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # users table + op.create_table('users', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('username', sa.String(), nullable=False), + sa.Column('hashed_password', sa.String(), nullable=False), + sa.Column('is_active', sa.Boolean(), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('username') + ) + op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False) + op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True) + + # invoices table + op.create_table('invoices', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('email_id', sa.String(), nullable=True), + sa.Column('email_subject', sa.String(), nullable=True), + sa.Column('email_from', sa.String(), nullable=True), + sa.Column('email_date', sa.DateTime(), nullable=True), + sa.Column('invoice_number', sa.String(), nullable=True), + sa.Column('invoice_date', sa.DateTime(), nullable=True), + sa.Column('invoice_amount', sa.Float(), nullable=True), + sa.Column('invoice_tax_amount', sa.Float(), nullable=True), + sa.Column('invoice_tax_rate', sa.Float(), nullable=True), + sa.Column('invoice_tax_type', sa.String(), nullable=True), + sa.Column('issuer_name', sa.String(), nullable=True), + sa.Column('issuer_address', sa.Text(), nullable=True), + sa.Column('file_path', sa.String(), nullable=True), + sa.Column('file_name', sa.String(), nullable=True), + sa.Column('storage_year', sa.Integer(), nullable=True), + sa.Column('storage_path', sa.String(), nullable=True), + sa.Column('processed', sa.Boolean(), nullable=False), + sa.Column('processing_error', sa.Text(), nullable=True), + sa.Column('confidence_score', sa.Float(), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_invoices_id'), 'invoices', ['id'], unique=False) + op.create_index(op.f('ix_invoices_email_id'), 'invoices', ['email_id'], unique=False) + op.create_index(op.f('ix_invoices_invoice_number'), 'invoices', ['invoice_number'], unique=False) + + # email_logs table + op.create_table('email_logs', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('email_id', sa.String(), nullable=False), + sa.Column('email_subject', sa.String(), nullable=True), + sa.Column('email_from', sa.String(), nullable=True), + sa.Column('email_date', sa.DateTime(), nullable=True), + sa.Column('email_folder', sa.String(), nullable=True), + sa.Column('matched_keywords', sa.JSON(), nullable=True), + sa.Column('has_attachment', sa.Boolean(), nullable=False), + sa.Column('attachment_type', sa.String(), nullable=True), + sa.Column('processed', sa.Boolean(), nullable=False), + sa.Column('processing_result', sa.String(), nullable=True), + sa.Column('invoice_id', sa.Integer(), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('email_id') + ) + op.create_index(op.f('ix_email_logs_id'), 'email_logs', ['id'], unique=False) + + # invoice_details table + op.create_table('invoice_details', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('invoice_id', sa.Integer(), nullable=False), + sa.Column('raw_text', sa.Text(), nullable=True), + sa.Column('parsed_data', sa.JSON(), nullable=True), + sa.Column('confidence_breakdown', sa.JSON(), nullable=True), + sa.Column('processing_notes', sa.Text(), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_invoice_details_id'), 'invoice_details', ['id'], unique=False) + op.create_index(op.f('ix_invoice_details_invoice_id'), 'invoice_details', ['invoice_id'], unique=False) + + +def downgrade() -> None: + op.drop_table('invoice_details') + op.drop_table('email_logs') + op.drop_table('invoices') + op.drop_table('users') diff --git a/backend/main.py b/backend/main.py index 5877a1c..a4362cf 100644 --- a/backend/main.py +++ b/backend/main.py @@ -12,10 +12,11 @@ from sqlalchemy import text from backend.database import get_db, engine, Base # Models importieren für Alembic -from backend.models import Invoice, InvoiceDetail +from backend.models import Invoice, InvoiceDetail, Base as ModelsBase # Routers from backend.routers import emails, invoices, auth +from backend.routers import settings as settings_router # App erstellen app = FastAPI( @@ -63,12 +64,14 @@ async def root(): async def startup_event(): """Datenbank erstellen bei Start""" Base.metadata.create_all(bind=engine) + ModelsBase.metadata.create_all(bind=engine) # Router einbinden app.include_router(auth.router, prefix="/auth", tags=["authentication"]) app.include_router(emails.router, prefix="/emails", tags=["emails"]) app.include_router(invoices.router, prefix="/invoices", tags=["invoices"]) +app.include_router(settings_router.router, prefix="/settings", tags=["settings"]) if __name__ == "__main__": diff --git a/backend/models.py b/backend/models.py index a685bea..8a63aba 100644 --- a/backend/models.py +++ b/backend/models.py @@ -101,3 +101,10 @@ class InvoiceDetail(Base): def __repr__(self): return f"" + + +class Setting(Base): + __tablename__ = "settings" + + key = Column(String, primary_key=True) + value = Column(Text, nullable=True) diff --git a/backend/routers/emails.py b/backend/routers/emails.py index 614c5f7..e132c1b 100644 --- a/backend/routers/emails.py +++ b/backend/routers/emails.py @@ -6,18 +6,25 @@ IMAP Email-Verarbeitung und Filterung import imaplib import email import os +import re import tempfile +import uuid from datetime import datetime from typing import List, Optional, Dict, Any -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException from sqlalchemy.orm import Session from email.header import decode_header -from backend.database import get_db +from backend.database import get_db, SessionLocal from backend.models import EmailLog, Invoice +from backend.routers.settings import get_imap_config router = APIRouter() +# ponytail: in-memory scan state, fine for single process +_scan_tasks: dict = {} +_active_scan_id: Optional[str] = None + # IMAP Konfiguration IMAP_HOST = os.getenv("IMAP_HOST", "imap.strato.de") IMAP_PORT = int(os.getenv("IMAP_PORT", 993)) @@ -30,8 +37,11 @@ GMAIL_IMAP_PORT = int(os.getenv("GMAIL_IMAP_PORT", 993)) GMAIL_IMAP_USER = os.getenv("GMAIL_IMAP_USER", "") GMAIL_IMAP_PASSWORD = os.getenv("GMAIL_IMAP_PASSWORD", "") -# Filter-Kriterien -INVOICE_KEYWORDS = ["rechnung", "invoice", "receipt", "bill", "factuur"] +# Filter-Kriterien (Substring-Match — deckt Plural/Komposita ab) +INVOICE_KEYWORDS = [ + "rechnung", "rechnungs", "invoice", "receipt", "bill", "factuur", + "gutschrift", "quittung", "beleg", "abrechnung", "mahnung", +] # Ablagepfad (Evo-X2) - optional INVOICE_STORAGE_PATH = os.getenv("INVOICE_STORAGE_PATH", "/tmp/invoice-storage") @@ -144,7 +154,8 @@ async def search_emails(db: Session = Depends(get_db)): } try: - mail = connect_imap(IMAP_HOST, IMAP_PORT, IMAP_USER, IMAP_PASSWORD) + cfg = get_imap_config(db) + mail = connect_imap(cfg["host"], cfg["port"], cfg["user"], cfg["password"]) # Alle Ordner durchsuchen for folder in ["INBOX", "Sent", "Gesendet"]: @@ -214,7 +225,14 @@ def extract_and_process_attachments(msg: email.message.Message, email_id: str, d pdf_data = extract_pdf_data(tmp_path) - if "error" not in pdf_data and pdf_data.get("amount"): + if pdf_data.get("not_invoice") or "error" in pdf_data: + continue + + # Duplikat-Check: gleiche email_id + filename + if db.query(Invoice).filter(Invoice.email_id == email_id, Invoice.file_name == filename).first(): + continue + + if pdf_data.get("amount"): # Rechnung speichern tax_type = pdf_data.get("tax_info", {}).get("tax_type", "unbekannt") tax_rate = pdf_data.get("tax_info", {}).get("tax_rate", 0) @@ -224,11 +242,12 @@ def extract_and_process_attachments(msg: email.message.Message, email_id: str, d storage_path = get_storage_path(storage_year, tax_type) # PDF in Zielverzeichnis kopieren (optional) - if INVOICE_STORAGE_PATH: + base_storage = get_imap_config(db)["storage_path"] + if base_storage: os.makedirs(storage_path, exist_ok=True) - # Dateiname generieren - invoice_number = pdf_data.get("invoice_number") or "unbekannt" + # Dateiname generieren (Slashes/Sonderzeichen ersetzen) + invoice_number = re.sub(r'[/\\:*?"<>|]', '-', pdf_data.get("invoice_number") or "unbekannt") file_name = f"Rechnung_{invoice_number}_{email_id}.pdf" dest_path = os.path.join(storage_path, file_name) @@ -241,7 +260,7 @@ def extract_and_process_attachments(msg: email.message.Message, email_id: str, d file_path=dest_path, file_name=file_name, storage_year=storage_year, - storage_path=storage_path.replace(INVOICE_STORAGE_PATH, ""), + storage_path=storage_path.replace(base_storage, ""), invoice_number=invoice_number, invoice_amount=pdf_data.get("amount"), issuer_name=pdf_data.get("issuer"), @@ -271,158 +290,158 @@ def extract_and_process_attachments(msg: email.message.Message, email_id: str, d return processed -@router.get("/scan-all") -async def scan_all_emails(db: Session = Depends(get_db)): - """Scant alle Emails, extrahiert PDFs und verarbeitet Rechnungen""" - results = { - "start_time": datetime.utcnow().isoformat(), - "processed": 0, - "invoices_found": 0, - "pdfs_processed": 0, - "errors": [] - } +def _scan_imap(mail_conn, db: Session, state: dict, since_date: Optional[str] = None, until_date: Optional[str] = None): + """Verarbeitet eine IMAP-Verbindung und aktualisiert state.""" + mail_conn.select("INBOX") + # Datumsfilter als IMAP-Kriterium (Format: DD-Mon-YYYY) + criteria = [] + if since_date: + fmt = datetime.strptime(since_date, "%Y-%m-%d").strftime("%d-%b-%Y") + criteria.append(f'SINCE {fmt}') + if until_date: + fmt = datetime.strptime(until_date, "%Y-%m-%d").strftime("%d-%b-%Y") + criteria.append(f'BEFORE {fmt}') + if criteria: + result, data = mail_conn.search(None, *criteria) + else: + result, data = mail_conn.search(None, 'ALL') + if result != "OK": + return + email_ids = data[0].split() + state["total"] = state.get("total", 0) + len(email_ids) + for email_id in email_ids: + email_id_str = email_id.decode() + log_entry = db.query(EmailLog).filter(EmailLog.email_id == email_id_str).first() + if log_entry and log_entry.processed: + continue + result2, msg_data = mail_conn.fetch(email_id, '(RFC822)') + if result2 != "OK": + continue + msg = email.message_from_bytes(msg_data[0][1]) + subject = msg.get("Subject", "") + from_addr = msg.get("From", "") + date_str = msg.get("Date", "") + email_date = parse_email_date(date_str) + decoded_subject, enc = decode_header(subject)[0] + if isinstance(decoded_subject, bytes): + subject = decoded_subject.decode(enc or "utf-8") + subject_lower = subject.lower() + matched_keywords = [kw for kw in INVOICE_KEYWORDS if kw in subject_lower] + log_entry = None + if matched_keywords: + log_entry = EmailLog( + email_id=email_id_str, + email_subject=subject, + email_from=from_addr, + email_date=email_date, + email_folder="INBOX", + matched_keywords=matched_keywords, + has_attachment=True, + processed=True, + ) + db.add(log_entry) + state["invoices_found"] += 1 + try: + pdfs = extract_and_process_attachments(msg, email_id_str, db) + state["pdfs_processed"] += len(pdfs) + if pdfs and log_entry: + log_entry.processing_result = f"PDFs processed: {len(pdfs)}" + log_entry.processed = True + except Exception as pdf_err: + state["errors"].append(f"PDF-Fehler Email {email_id_str}: {pdf_err}") + db.commit() + state["processed"] += 1 + mail_conn.logout() - # Strato Mail + +def _do_scan(db: Session, state: dict, force: bool = False, since_date: Optional[str] = None, until_date: Optional[str] = None): + """Kern-Scan-Logik für Strato + Gmail.""" try: - mail = connect_imap(IMAP_HOST, IMAP_PORT, IMAP_USER, IMAP_PASSWORD) - mail.select("INBOX") - - result, data = mail.search(None, 'ALL') - if result == "OK": - email_ids = data[0].split() - - for email_id in email_ids: - email_id_str = email_id.decode() - - # Prüfen ob bereits verarbeitet - log_entry = db.query(EmailLog).filter(EmailLog.email_id == email_id_str).first() - if log_entry and log_entry.processed: - continue - - result, msg_data = mail.fetch(email_id, '(RFC822)') - if result != "OK": - continue - - msg = email.message_from_bytes(msg_data[0][1]) - subject = msg.get("Subject", "") - from_addr = msg.get("From", "") - date_str = msg.get("Date", "") - - # Datum parsen - email_date = parse_email_date(date_str) - - # Subject decodieren - decoded_subject, encoding = decode_header(subject)[0] - if isinstance(decoded_subject, bytes): - subject = decoded_subject.decode(encoding or "utf-8") - - # Prüfen auf Keywords - subject_lower = subject.lower() - matched_keywords = [kw for kw in INVOICE_KEYWORDS if kw in subject_lower] - - if matched_keywords: - # Email als Rechnung markieren (auch wenn keine PDF dabei ist) - log_entry = EmailLog( - email_id=email_id_str, - email_subject=subject, - email_from=from_addr, - email_date=email_date, - email_folder="INBOX", - matched_keywords=matched_keywords, - has_attachment=True, - processed=True - ) - db.add(log_entry) - results["invoices_found"] += 1 - - # PDFs extrahieren und verarbeiten - pdfs = extract_and_process_attachments(msg, email_id_str, db) - results["pdfs_processed"] += len(pdfs) - - # Log aktualisieren mit Ergebnis - if pdfs and log_entry: - log_entry.processing_result = f"PDFs processed: {len(pdfs)}" - log_entry.processed = True - db.commit() - - results["processed"] += 1 - - db.commit() - - mail.logout() - + cfg = get_imap_config(db) + mail = connect_imap(cfg["host"], cfg["port"], cfg["user"], cfg["password"]) + _scan_imap(mail, db, state, since_date, until_date) except Exception as e: - results["errors"].append(f"Strato scan failed: {str(e)}") import traceback - results["errors"].append(traceback.format_exc()) - - # Gmail Mail (optional) + state["errors"].append(f"Strato: {e}\n{traceback.format_exc()}") if GMAIL_IMAP_USER and GMAIL_IMAP_PASSWORD: try: mail = connect_imap(GMAIL_IMAP_HOST, GMAIL_IMAP_PORT, GMAIL_IMAP_USER, GMAIL_IMAP_PASSWORD) - mail.select("INBOX") - - result, data = mail.search(None, 'ALL') - if result == "OK": - email_ids = data[0].split() - - for email_id in email_ids: - email_id_str = email_id.decode() - - log_entry = db.query(EmailLog).filter(EmailLog.email_id == email_id_str).first() - if log_entry and log_entry.processed: - continue - - result, msg_data = mail.fetch(email_id, '(RFC822)') - if result != "OK": - continue - - msg = email.message_from_bytes(msg_data[0][1]) - subject = msg.get("Subject", "") - from_addr = msg.get("From", "") - date_str = msg.get("Date", "") - - email_date = parse_email_date(date_str) - - decoded_subject, encoding = decode_header(subject)[0] - if isinstance(decoded_subject, bytes): - subject = decoded_subject.decode(encoding or "utf-8") - - subject_lower = subject.lower() - matched_keywords = [kw for kw in INVOICE_KEYWORDS if kw in subject_lower] - - if matched_keywords: - log_entry = EmailLog( - email_id=email_id_str, - email_subject=subject, - email_from=from_addr, - email_date=email_date, - email_folder="INBOX", - matched_keywords=matched_keywords, - has_attachment=True, - processed=True - ) - db.add(log_entry) - results["invoices_found"] += 1 - - pdfs = extract_and_process_attachments(msg, email_id_str, db) - results["pdfs_processed"] += len(pdfs) - - if pdfs: - log_entry.processing_result = f"PDFs processed: {len(pdfs)}" - log_entry.processed = True - db.commit() - - results["processed"] += 1 - - db.commit() - - mail.logout() - + _scan_imap(mail, db, state, since_date, until_date) except Exception as e: - results["errors"].append(f"Gmail scan failed: {str(e)}") import traceback - results["errors"].append(traceback.format_exc()) + state["errors"].append(f"Gmail: {e}\n{traceback.format_exc()}") - results["end_time"] = datetime.utcnow().isoformat() - return results + +def _run_scan_background(task_id: str, force: bool, since_date: Optional[str], until_date: Optional[str]): + """Hintergrund-Job für Email-Scan (eigene DB-Session).""" + global _active_scan_id + db = SessionLocal() + state = _scan_tasks[task_id] + try: + _do_scan(db, state, force, since_date, until_date) + except Exception as e: + state["errors"].append(str(e)) + finally: + state["status"] = "done" + state["end_time"] = datetime.utcnow().isoformat() + _active_scan_id = None + db.close() + + +@router.post("/scan-all/start") +async def start_scan( + background_tasks: BackgroundTasks, + force: bool = False, + since_date: Optional[str] = None, + until_date: Optional[str] = None, + db: Session = Depends(get_db), +): + """Startet Email-Scan im Hintergrund. since_date/until_date: YYYY-MM-DD""" + global _active_scan_id + # Kein paralleler Scan + if _active_scan_id and _scan_tasks.get(_active_scan_id, {}).get("status") == "running": + return {"task_id": _active_scan_id, "already_running": True} + task_id = str(uuid.uuid4()) + _active_scan_id = task_id + _scan_tasks[task_id] = { + "status": "running", + "processed": 0, + "total": 0, + "invoices_found": 0, + "pdfs_processed": 0, + "errors": [], + "start_time": datetime.utcnow().isoformat(), + "end_time": None, + } + if force: + db.query(Invoice).delete() + db.query(EmailLog).delete() + db.commit() + background_tasks.add_task(_run_scan_background, task_id, force, since_date, until_date) + return {"task_id": task_id} + + +@router.get("/scan-all/status/{task_id}") +async def scan_status(task_id: str): + """Gibt Scan-Status zurück.""" + state = _scan_tasks.get(task_id) + if not state: + raise HTTPException(status_code=404, detail="Task nicht gefunden") + return state + + +@router.get("/scan-all") +async def scan_all_emails(db: Session = Depends(get_db)): + """Synchroner Scan (Legacy) — für direkten Aufruf ohne Polling.""" + state = { + "status": "running", + "processed": 0, + "invoices_found": 0, + "pdfs_processed": 0, + "errors": [], + "start_time": datetime.utcnow().isoformat(), + } + _do_scan(db, state) + state["status"] = "done" + state["end_time"] = datetime.utcnow().isoformat() + return state diff --git a/backend/routers/invoices.py b/backend/routers/invoices.py index c5cf6ea..960ad49 100644 --- a/backend/routers/invoices.py +++ b/backend/routers/invoices.py @@ -3,15 +3,19 @@ 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, Alignment, Border, Side +from openpyxl.styles import Font from backend.database import get_db from backend.models import Invoice, InvoiceDetail, EmailLog @@ -149,6 +153,70 @@ def extract_issuer(text: str) -> Optional[str]: 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: @@ -161,14 +229,40 @@ def extract_pdf_data(file_path: str) -> Dict[str, Any]: 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 + "pages": pages, + "ai_analyzed": False, } except Exception as e: @@ -193,32 +287,28 @@ def get_storage_path(year: int, tax_type: str) -> str: return os.path.join(base_path, str(year), tax_dir) -def save_to_excel(invoices: List[Invoice], excel_path: str): - """Speichert Rechnungen in Excel-Datei""" +def build_excel(invoices: List[Invoice]) -> "io.BytesIO": + import io wb = Workbook() ws = wb.active ws.title = "Rechnungen" - - # Header - headers = ["Datum", "Rechnungsausteller", "Betrag", "Umsatzsteuer", "Steuerart", "Dateipfad"] + 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) - - # Daten 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_amount or 0) - ws.cell(row=row, column=4, value=inv.invoice_tax_amount or 0) + 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=inv.file_path or "") - - # Formatieren + 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 = 20 - - wb.save(excel_path) + ws.column_dimensions[chr(64 + col)].width = 22 + buf = io.BytesIO() + wb.save(buf) + buf.seek(0) + return buf @router.get("/scan-pdf") @@ -275,18 +365,16 @@ async def scan_pdf_file(file_path: str, db: Session = Depends(get_db)): @router.get("/export-excel") async def export_to_excel(db: Session = Depends(get_db)): - """Exportiert alle Rechnungen nach Excel""" + """Liefert Excel-Datei als direkten Browser-Download.""" + from fastapi.responses import StreamingResponse invoices = db.query(Invoice).order_by(Invoice.invoice_date.desc()).all() - - excel_path = os.path.join(INVOICE_STORAGE_PATH, f"rechnungen_{datetime.utcnow().strftime('%Y%m%d')}.xlsx") - - save_to_excel(invoices, excel_path) - - return { - "status": "success", - "file_path": excel_path, - "invoice_count": len(invoices) - } + 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") diff --git a/backend/routers/settings.py b/backend/routers/settings.py new file mode 100644 index 0000000..b08001d --- /dev/null +++ b/backend/routers/settings.py @@ -0,0 +1,51 @@ +from fastapi import APIRouter, Depends +from sqlalchemy.orm import Session +from backend.database import get_db +from backend.models import Setting + +router = APIRouter() + +ALLOWED_KEYS = { + "IMAP_HOST", "IMAP_PORT", "IMAP_USER", "IMAP_PASSWORD", + "GMAIL_IMAP_HOST", "GMAIL_IMAP_PORT", "GMAIL_IMAP_USER", "GMAIL_IMAP_PASSWORD", + "INVOICE_STORAGE_PATH", +} + +MASK = "••••••••" + + +@router.get("/") +def get_settings(db: Session = Depends(get_db)): + rows = {r.key: r.value for r in db.query(Setting).filter(Setting.key.in_(ALLOWED_KEYS)).all()} + return {k: (MASK if "PASSWORD" in k and rows.get(k) else rows.get(k, "")) for k in ALLOWED_KEYS} + + +@router.post("/") +def save_settings(data: dict, db: Session = Depends(get_db)): + for key, value in data.items(): + if key not in ALLOWED_KEYS: + continue + if "PASSWORD" in key and value == MASK: + continue + existing = db.get(Setting, key) + if existing: + existing.value = value + else: + db.add(Setting(key=key, value=value)) + db.commit() + return {"status": "ok"} + + +def get_imap_config(db: Session) -> dict: + """Liest IMAP-Konfiguration aus DB (Fallback auf Env-Vars).""" + import os + rows = {r.key: r.value for r in db.query(Setting).filter(Setting.key.in_(ALLOWED_KEYS)).all()} + def val(key, default=""): + return rows.get(key) or os.getenv(key, default) + return { + "host": val("IMAP_HOST", "imap.strato.de"), + "port": int(val("IMAP_PORT", "993")), + "user": val("IMAP_USER"), + "password": val("IMAP_PASSWORD"), + "storage_path": val("INVOICE_STORAGE_PATH", "/tmp/invoice-storage"), + } diff --git a/clowdex-download/.claude/.claude/hooks/backup-before-write.sh b/clowdex-download/.claude/.claude/hooks/backup-before-write.sh old mode 100755 new mode 100644 diff --git a/clowdex-download/.claude/.claude/hooks/completeness-gate.sh b/clowdex-download/.claude/.claude/hooks/completeness-gate.sh old mode 100755 new mode 100644 diff --git a/clowdex-download/.claude/.claude/hooks/guard-bash.sh b/clowdex-download/.claude/.claude/hooks/guard-bash.sh old mode 100755 new mode 100644 diff --git a/clowdex-download/.claude/.claude/hooks/log-changes.sh b/clowdex-download/.claude/.claude/hooks/log-changes.sh old mode 100755 new mode 100644 diff --git a/clowdex-download/.claude/.claude/hooks/log-failures.sh b/clowdex-download/.claude/.claude/hooks/log-failures.sh old mode 100755 new mode 100644 diff --git a/clowdex-download/.claude/.claude/hooks/log-stop-verdict.sh b/clowdex-download/.claude/.claude/hooks/log-stop-verdict.sh old mode 100755 new mode 100644 diff --git a/clowdex-download/.claude/.claude/hooks/post-compact-resume.sh b/clowdex-download/.claude/.claude/hooks/post-compact-resume.sh old mode 100755 new mode 100644 diff --git a/clowdex-download/.claude/.claude/hooks/pre-compact-handoff.sh b/clowdex-download/.claude/.claude/hooks/pre-compact-handoff.sh old mode 100755 new mode 100644 diff --git a/clowdex-download/.claude/.claude/hooks/session-reset.sh b/clowdex-download/.claude/.claude/hooks/session-reset.sh old mode 100755 new mode 100644 diff --git a/docker-compose.yml b/docker-compose.yml index 127bc5d..825d33e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: dockerfile: Dockerfile.backend container_name: invoice-check-backend ports: - - "127.0.0.1:8005:8001" # Host 8005 (Projekt-Port) — 8001 belegt LLM-Tunnel + - "127.0.0.1:8005:8001" environment: - DATABASE_URL=postgresql://postgres:postgres@db:5432/invoice_check - SECRET_KEY=${SECRET_KEY} @@ -28,6 +28,19 @@ services: - /tmp/test-invoices:/tmp/test-invoices - invoice-storage:/tmp/invoice-storage + frontend: + build: + context: . + dockerfile: Dockerfile.frontend + container_name: invoice-check-frontend + ports: + - "5178:80" + depends_on: + - backend + networks: + - invoice-check-network + restart: unless-stopped + db: image: postgres:16-alpine container_name: invoice-check-db diff --git a/frontend/nginx.conf b/frontend/nginx.conf new file mode 100644 index 0000000..0bbdd7e --- /dev/null +++ b/frontend/nginx.conf @@ -0,0 +1,24 @@ +server { + listen 80; + server_name localhost; + root /usr/share/nginx/html; + index index.html; + + # Frontend - statische Dateien + location / { + try_files $uri $uri/ /index.html; + } + + # API Proxy + location ^~ /api { + rewrite ^/api/(.*)$ /$1 break; + proxy_pass http://backend:8001; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_cache off; + proxy_buffering off; + proxy_read_timeout 600s; + proxy_connect_timeout 10s; + proxy_send_timeout 600s; + } +} diff --git a/frontend/package.json b/frontend/package.json index 475f001..ccc6b02 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -11,13 +11,19 @@ "dependencies": { "react": "^18.3.1", "react-dom": "^18.3.1", - "react-router-dom": "^6.26.0" + "react-router-dom": "^6.26.0", + "axios": "^1.7.0", + "pdf-lib": "^1.17.1", + "date-fns": "^3.6.0" }, "devDependencies": { "@types/react": "^18.3.0", "@types/react-dom": "^18.3.0", "@vitejs/plugin-react": "^4.3.0", "typescript": "^5.5.0", - "vite": "^5.4.0" + "vite": "^5.4.0", + "autoprefixer": "^10.4.19", + "postcss": "^8.4.38", + "tailwindcss": "^3.4.3" } } diff --git a/frontend/postcss.config.js b/frontend/postcss.config.js new file mode 100644 index 0000000..2e7af2b --- /dev/null +++ b/frontend/postcss.config.js @@ -0,0 +1,6 @@ +export default { + plugins: { + tailwindcss: {}, + autoprefixer: {}, + }, +} diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 566c2b4..a58fbe3 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,52 +1,238 @@ import { useState, useEffect } from 'react' -import { Routes, Route, Link, useNavigate } from 'react-router-dom' +import { Routes, Route, Link, useLocation } from 'react-router-dom' +import axios from 'axios' // Types interface Invoice { id: number + email_id: string invoice_number: string | null invoice_date: string | null issuer_name: string | null invoice_amount: number | null invoice_tax_type: string | null + invoice_tax_rate: number | null file_path: string | null processed: boolean + created_at: string } interface EmailLog { id: number email_id: string - subject: string - from: string + email_subject: string + email_from: string + email_date: string | null + email_folder: string | null + matched_keywords: string[] | null + has_attachment: boolean processed: boolean + processing_result: string | null } -// API Helper -const API_BASE = 'http://localhost:8001' +// API Helper (Docker-Container: nginx proxyt /api/ an backend) +const API_BASE = '/api' async function fetchInvoices(): Promise { - const response = await fetch(`${API_BASE}/invoices/list`) - if (!response.ok) throw new Error('Failed to fetch invoices') - const data = await response.json() - return data.invoices || [] + const response = await axios.get(`${API_BASE}/invoices/list`) + return response.data.invoices || [] } -async function scanAllEmails() { - const response = await fetch(`${API_BASE}/emails/scan-all`, { method: 'POST' }) - if (!response.ok) throw new Error('Failed to scan emails') - return response.json() +async function fetchEmailLogs(): Promise { + const response = await axios.get(`${API_BASE}/emails/list`) + return response.data.logs || [] } -async function exportToExcel() { - const response = await fetch(`${API_BASE}/invoices/export-excel`, { method: 'POST' }) - if (!response.ok) throw new Error('Failed to export') - return response.json() +async function startScan(force = false, sinceDate?: string, untilDate?: string): Promise { + const params = new URLSearchParams({ force: String(force) }) + if (sinceDate) params.set('since_date', sinceDate) + if (untilDate) params.set('until_date', untilDate) + const response = await axios.post(`${API_BASE}/emails/scan-all/start?${params}`) + return response.data.task_id } -function App() { +async function pollScan(taskId: string): Promise { + const response = await axios.get(`${API_BASE}/emails/scan-all/status/${taskId}`) + return response.data +} + +function exportToExcel() { + window.location.href = `${API_BASE}/invoices/export-excel` +} + +// Hilfsfunktionen +function formatDate(dateStr: string | null): string { + if (!dateStr) return '-' + try { + const date = new Date(dateStr) + return new Intl.DateTimeFormat('de-DE').format(date) + } catch { + return dateStr + } +} + +function formatCurrency(amount: number | null): string { + if (amount === null || amount === undefined) return '-' + return new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(amount) +} + +function Badge({ children, color }: { children: React.ReactNode; color: 'green' | 'blue' | 'yellow' | 'red' | 'gray' }) { + const colors = { + green: 'bg-green-100 text-green-800', + blue: 'bg-blue-100 text-blue-800', + yellow: 'bg-yellow-100 text-yellow-800', + red: 'bg-red-100 text-red-800', + gray: 'bg-gray-100 text-gray-800', + } + return ( + + {children} + + ) +} + +function InvoiceDetailModal({ invoice, onClose }: { invoice: Invoice | null; onClose: () => void }) { + if (!invoice) return null + + return ( +
+
e.stopPropagation()}> +
+
+

Rechnung Details

+ +
+ +
+
+ +

{invoice.invoice_number || '-'}

+
+
+ +

{formatCurrency(invoice.invoice_amount)}

+
+
+ +

{invoice.invoice_tax_type || '-'}

+
+
+ +

{invoice.invoice_tax_rate ? (invoice.invoice_tax_rate * 100).toFixed(0) + '%' : '-'}

+
+
+ +

{invoice.issuer_name || '-'}

+
+
+ +

{formatDate(invoice.invoice_date)}

+
+
+ +
+ +

{invoice.file_path || '-'}

+
+ + +
+
+
+ ) +} + +function EmailLogModal({ log, onClose }: { log: EmailLog | null; onClose: () => void }) { + if (!log) return null + + return ( +
+
e.stopPropagation()}> +
+
+

Email Details

+ +
+ +
+ +

{log.email_subject}

+
+ +
+ +

{log.email_from}

+
+ +
+ +

{formatDate(log.email_date)}

+
+ +
+ +

{log.email_folder || 'INBOX'}

+
+ + {log.matched_keywords && ( +
+ +
+ {log.matched_keywords.map((kw, idx) => ( + {kw} + ))} +
+
+ )} + + {log.processing_result && ( +
+ +

{log.processing_result}

+
+ )} +
+
+
+ ) +} + +function InvoicesView() { const [invoices, setInvoices] = useState([]) const [loading, setLoading] = useState(true) + const [selectedInvoice, setSelectedInvoice] = useState(null) const [status, setStatus] = useState(null) + const [toast, setToast] = useState(null) + const [sinceDate, setSinceDate] = useState('') + const [untilDate, setUntilDate] = useState('') + + function showToast(msg: string) { + setToast(msg) + setTimeout(() => setToast(null), 5000) + } useEffect(() => { loadInvoices() @@ -63,103 +249,419 @@ function App() { } } - async function handleScanEmails() { - setLoading(true) - setStatus(null) - try { - const result = await scanAllEmails() - setStatus(`Gescannt: ${result.invoices_found} Rechnungen gefunden`) - await loadInvoices() - } catch (err) { - setStatus(`Scan fehlgeschlagen: ${err}`) - } - setLoading(false) - } + return ( +
+ {/* Toast-Benachrichtigung */} + {toast && ( +
+ ✓ {toast} +
+ )} - async function handleExport() { - setLoading(true) - setStatus(null) - try { - const result = await exportToExcel() - setStatus(`Excel exportiert: ${result.file_path}`) - } catch (err) { - setStatus(`Export fehlgeschlagen: ${err}`) - } - setLoading(false) - } +
+

Rechnungen

+
+ {/* Datumsfilter */} +
+ + setSinceDate(e.target.value)} + className="border border-gray-300 rounded px-2 py-1" /> + + setUntilDate(e.target.value)} + className="border border-gray-300 rounded px-2 py-1" /> +
+
+ + + +
+
+
- if (loading) { - return
Lade...
- } + {status && ( +
+ {status} +
+ )} + +
+ + + + + + + + + + + + + + {loading ? ( + + + + ) : invoices.length === 0 ? ( + + + + ) : ( + invoices.map((inv) => ( + + + + + + + + + + )) + )} + +
RechnungsdatumAbsenderRechnungsnummerBetragSteuerStatusAktionen
Lade...
+ Keine Rechnungen gefunden. Klicken Sie auf "Emails scannen". +
{formatDate(inv.invoice_date)}{inv.issuer_name || '-'}{inv.invoice_number || '-'}{formatCurrency(inv.invoice_amount)} + + {inv.invoice_tax_type || 'unbekannt'} + + + {inv.processed ? Verarbeitet : Wartend} + + +
+
+ + {selectedInvoice && ( + setSelectedInvoice(null)} /> + )} + +
+ {invoices.length} Rechnungen + {invoices.filter(i => i.processed).length} verarbeitet +
+
+ ) +} + +function EmailLogsView() { + const [logs, setLogs] = useState([]) + const [loading, setLoading] = useState(true) + const [selectedLog, setSelectedLog] = useState(null) + + useEffect(() => { + async function loadLogs() { + try { + const data = await fetchEmailLogs() + setLogs(data) + } catch (err) { + console.error(err) + } finally { + setLoading(false) + } + } + loadLogs() + }, []) return ( -
-