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>
This commit is contained in:
parent
97b5a7ec45
commit
d2c3d29ad2
28 changed files with 1320 additions and 515 deletions
|
|
@ -6,12 +6,12 @@
|
||||||
- **DSGVO-relevant:** nein
|
- **DSGVO-relevant:** nein
|
||||||
- **Beschreibung:** Email-Filter für Rechnungen mit PDF-Scanning, lokale Ablage auf Evo-X2, Excel-Dokumentation
|
- **Beschreibung:** Email-Filter für Rechnungen mit PDF-Scanning, lokale Ablage auf Evo-X2, Excel-Dokumentation
|
||||||
- **Stack:** FastAPI + PostgreSQL + React + PyMuPDF
|
- **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
|
- **GitHub:** https://github.com/c-stone/c-stone-invoice-check
|
||||||
- **Server-Pfad:** /root/invoice-check/ (nach Deployment)
|
- **Server-Pfad:** /root/invoice-check/ (nach Deployment)
|
||||||
- **URL:** https://invoice-check.c-stone-dev.com (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:
|
### Erledigt:
|
||||||
- ✅ Projektordner angelegt
|
- ✅ Projektordner angelegt
|
||||||
|
|
@ -20,23 +20,23 @@
|
||||||
- ✅ Datenbank: PostgreSQL Models (Invoice, EmailLog, InvoiceDetail, User)
|
- ✅ Datenbank: PostgreSQL Models (Invoice, EmailLog, InvoiceDetail, User)
|
||||||
- ✅ PDF-Verarbeitung: PyMuPDF für Text-Extraktion und Steuer-Erkennung
|
- ✅ PDF-Verarbeitung: PyMuPDF für Text-Extraktion und Steuer-Erkennung
|
||||||
- ✅ Excel-Export: openpyxl für Dokumentation
|
- ✅ Excel-Export: openpyxl für Dokumentation
|
||||||
- ✅ Frontend: React/Vite mit TypeScript
|
- ✅ Frontend: React/Vite mit TypeScript (Dashboard, Invoices, Email Logs)
|
||||||
- ✅ docker-compose.yml erstellt
|
- ✅ Tailwind CSS Konfiguration
|
||||||
|
- ✅ docker-compose.yml mit Backend + Frontend + PostgreSQL
|
||||||
- ✅ Dockerfile.backend erstellt
|
- ✅ Dockerfile.backend erstellt
|
||||||
|
- ✅ Dockerfile.frontend erstellt (nginx-basiert)
|
||||||
- ✅ .env.example mit Platzhaltern
|
- ✅ .env.example mit Platzhaltern
|
||||||
- ✅ README.md und docs/schema.md
|
- ✅ README.md und docs/schema.md
|
||||||
- ✅ Task Board mit >5 Tickets
|
- ✅ Alembic Migration initialisiert (5316895daf52_initial_schema.py)
|
||||||
- ✅ Git Commits (Initial + Backend)
|
- ✅ Test-Dateien (test_imap.py, test_pdf.py)
|
||||||
- ⏳ GitHub Repo erstellen (fehlt gh Auth)
|
- ✅ Git Commits für alle Änderungen
|
||||||
|
|
||||||
### Offene Punkte:
|
### Offene Punkte:
|
||||||
- [ ] GitHub Auth einrichten (gh auth login)
|
- [ ] Docker-Container builden und testen (`docker-compose up`)
|
||||||
- [ ] GitHub Repo erstellen
|
|
||||||
- [ ] Docker-Container builden und testen
|
|
||||||
- [ ] Alembic Migration initialisieren
|
|
||||||
- [ ] IMAP-Verbindung testen (Strato + Gmail)
|
- [ ] IMAP-Verbindung testen (Strato + Gmail)
|
||||||
- [ ] PDF-Verarbeitung lokal testen
|
- [ ] PDF-Verarbeitung lokal testen
|
||||||
- [ ] Evo-X2 Ablagepfad konfigurieren
|
- [ ] Evo-X2 Ablagepfad konfigurieren
|
||||||
|
- [ ] Frontend lokal starten (`npm run dev` im frontend/)
|
||||||
|
|
||||||
### Mail-Provider Konfiguration:
|
### Mail-Provider Konfiguration:
|
||||||
- **Strato:** imap.strato.de:993
|
- **Strato:** imap.strato.de:993
|
||||||
|
|
@ -58,8 +58,14 @@ Rechnungen/
|
||||||
└── ohne MwSt/
|
└── 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:
|
### Nächste Schritte:
|
||||||
1. GitHub Auth mit `gh auth login` einrichten
|
1. Docker-Container testen (`docker-compose up --build`)
|
||||||
2. Repo mit `gh repo create c-stone-invoice-check --private` erstellen
|
2. Frontend unter http://localhost:5178 erreichen
|
||||||
3. Docker-Container testen (`docker-compose up -d`)
|
3. Backend API unter http://localhost:8005/docs testen
|
||||||
4. IMAP-Verbindung konfigurieren und testen
|
4. IMAP-Verbindung mit echten Credentials konfigurieren
|
||||||
|
|
|
||||||
15
Daily Notes/072526.md
Normal file
15
Daily Notes/072526.md
Normal file
|
|
@ -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
|
||||||
|
-
|
||||||
25
Dockerfile.frontend
Normal file
25
Dockerfile.frontend
Normal file
|
|
@ -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;"]
|
||||||
117
STATUS.md
Normal file
117
STATUS.md
Normal file
|
|
@ -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)
|
||||||
|
|
@ -3,75 +3,16 @@ from logging.config import fileConfig
|
||||||
|
|
||||||
from sqlalchemy import pool
|
from sqlalchemy import pool
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.ext.asyncio import create_async_engine
|
||||||
|
|
||||||
from alembic import context
|
from alembic import context
|
||||||
|
|
||||||
# Import models directly (avoid env file issues)
|
# Import models from the actual application
|
||||||
from sqlalchemy import Column, Integer, String, DateTime, Float, Boolean, Text, JSON
|
import sys
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
import os
|
||||||
from datetime import datetime
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
|
||||||
Base = declarative_base()
|
from backend.models import Base, Invoice, EmailLog, InvoiceDetail, User
|
||||||
|
|
||||||
# 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)
|
|
||||||
|
|
||||||
# Set target metadata for autogenerate
|
# Set target metadata for autogenerate
|
||||||
target_metadata = Base.metadata
|
target_metadata = Base.metadata
|
||||||
|
|
@ -79,7 +20,6 @@ target_metadata = Base.metadata
|
||||||
# other values from the config
|
# other values from the config
|
||||||
def get_url():
|
def get_url():
|
||||||
"""Get database URL from environment"""
|
"""Get database URL from environment"""
|
||||||
import os
|
|
||||||
return os.getenv("DATABASE_URL", "postgresql+psycopg2://postgres:postgres@localhost:5432/invoice_check")
|
return os.getenv("DATABASE_URL", "postgresql+psycopg2://postgres:postgres@localhost:5432/invoice_check")
|
||||||
|
|
||||||
def run_migrations_offline() -> None:
|
def run_migrations_offline() -> None:
|
||||||
|
|
|
||||||
104
backend/alembic/versions/5316895daf52_initial_schema.py
Normal file
104
backend/alembic/versions/5316895daf52_initial_schema.py
Normal file
|
|
@ -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')
|
||||||
|
|
@ -12,10 +12,11 @@ from sqlalchemy import text
|
||||||
from backend.database import get_db, engine, Base
|
from backend.database import get_db, engine, Base
|
||||||
|
|
||||||
# Models importieren für Alembic
|
# Models importieren für Alembic
|
||||||
from backend.models import Invoice, InvoiceDetail
|
from backend.models import Invoice, InvoiceDetail, Base as ModelsBase
|
||||||
|
|
||||||
# Routers
|
# Routers
|
||||||
from backend.routers import emails, invoices, auth
|
from backend.routers import emails, invoices, auth
|
||||||
|
from backend.routers import settings as settings_router
|
||||||
|
|
||||||
# App erstellen
|
# App erstellen
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
|
|
@ -63,12 +64,14 @@ async def root():
|
||||||
async def startup_event():
|
async def startup_event():
|
||||||
"""Datenbank erstellen bei Start"""
|
"""Datenbank erstellen bei Start"""
|
||||||
Base.metadata.create_all(bind=engine)
|
Base.metadata.create_all(bind=engine)
|
||||||
|
ModelsBase.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
|
||||||
# Router einbinden
|
# Router einbinden
|
||||||
app.include_router(auth.router, prefix="/auth", tags=["authentication"])
|
app.include_router(auth.router, prefix="/auth", tags=["authentication"])
|
||||||
app.include_router(emails.router, prefix="/emails", tags=["emails"])
|
app.include_router(emails.router, prefix="/emails", tags=["emails"])
|
||||||
app.include_router(invoices.router, prefix="/invoices", tags=["invoices"])
|
app.include_router(invoices.router, prefix="/invoices", tags=["invoices"])
|
||||||
|
app.include_router(settings_router.router, prefix="/settings", tags=["settings"])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -101,3 +101,10 @@ class InvoiceDetail(Base):
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<InvoiceDetail {self.invoice_id}>"
|
return f"<InvoiceDetail {self.invoice_id}>"
|
||||||
|
|
||||||
|
|
||||||
|
class Setting(Base):
|
||||||
|
__tablename__ = "settings"
|
||||||
|
|
||||||
|
key = Column(String, primary_key=True)
|
||||||
|
value = Column(Text, nullable=True)
|
||||||
|
|
|
||||||
|
|
@ -6,18 +6,25 @@ IMAP Email-Verarbeitung und Filterung
|
||||||
import imaplib
|
import imaplib
|
||||||
import email
|
import email
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import uuid
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import List, Optional, Dict, Any
|
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 sqlalchemy.orm import Session
|
||||||
from email.header import decode_header
|
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.models import EmailLog, Invoice
|
||||||
|
from backend.routers.settings import get_imap_config
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
# ponytail: in-memory scan state, fine for single process
|
||||||
|
_scan_tasks: dict = {}
|
||||||
|
_active_scan_id: Optional[str] = None
|
||||||
|
|
||||||
# IMAP Konfiguration
|
# IMAP Konfiguration
|
||||||
IMAP_HOST = os.getenv("IMAP_HOST", "imap.strato.de")
|
IMAP_HOST = os.getenv("IMAP_HOST", "imap.strato.de")
|
||||||
IMAP_PORT = int(os.getenv("IMAP_PORT", 993))
|
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_USER = os.getenv("GMAIL_IMAP_USER", "")
|
||||||
GMAIL_IMAP_PASSWORD = os.getenv("GMAIL_IMAP_PASSWORD", "")
|
GMAIL_IMAP_PASSWORD = os.getenv("GMAIL_IMAP_PASSWORD", "")
|
||||||
|
|
||||||
# Filter-Kriterien
|
# Filter-Kriterien (Substring-Match — deckt Plural/Komposita ab)
|
||||||
INVOICE_KEYWORDS = ["rechnung", "invoice", "receipt", "bill", "factuur"]
|
INVOICE_KEYWORDS = [
|
||||||
|
"rechnung", "rechnungs", "invoice", "receipt", "bill", "factuur",
|
||||||
|
"gutschrift", "quittung", "beleg", "abrechnung", "mahnung",
|
||||||
|
]
|
||||||
|
|
||||||
# Ablagepfad (Evo-X2) - optional
|
# Ablagepfad (Evo-X2) - optional
|
||||||
INVOICE_STORAGE_PATH = os.getenv("INVOICE_STORAGE_PATH", "/tmp/invoice-storage")
|
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:
|
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
|
# Alle Ordner durchsuchen
|
||||||
for folder in ["INBOX", "Sent", "Gesendet"]:
|
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)
|
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
|
# Rechnung speichern
|
||||||
tax_type = pdf_data.get("tax_info", {}).get("tax_type", "unbekannt")
|
tax_type = pdf_data.get("tax_info", {}).get("tax_type", "unbekannt")
|
||||||
tax_rate = pdf_data.get("tax_info", {}).get("tax_rate", 0)
|
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)
|
storage_path = get_storage_path(storage_year, tax_type)
|
||||||
|
|
||||||
# PDF in Zielverzeichnis kopieren (optional)
|
# 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)
|
os.makedirs(storage_path, exist_ok=True)
|
||||||
|
|
||||||
# Dateiname generieren
|
# Dateiname generieren (Slashes/Sonderzeichen ersetzen)
|
||||||
invoice_number = pdf_data.get("invoice_number") or "unbekannt"
|
invoice_number = re.sub(r'[/\\:*?"<>|]', '-', pdf_data.get("invoice_number") or "unbekannt")
|
||||||
file_name = f"Rechnung_{invoice_number}_{email_id}.pdf"
|
file_name = f"Rechnung_{invoice_number}_{email_id}.pdf"
|
||||||
dest_path = os.path.join(storage_path, file_name)
|
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_path=dest_path,
|
||||||
file_name=file_name,
|
file_name=file_name,
|
||||||
storage_year=storage_year,
|
storage_year=storage_year,
|
||||||
storage_path=storage_path.replace(INVOICE_STORAGE_PATH, ""),
|
storage_path=storage_path.replace(base_storage, ""),
|
||||||
invoice_number=invoice_number,
|
invoice_number=invoice_number,
|
||||||
invoice_amount=pdf_data.get("amount"),
|
invoice_amount=pdf_data.get("amount"),
|
||||||
issuer_name=pdf_data.get("issuer"),
|
issuer_name=pdf_data.get("issuer"),
|
||||||
|
|
@ -271,57 +290,45 @@ def extract_and_process_attachments(msg: email.message.Message, email_id: str, d
|
||||||
return processed
|
return processed
|
||||||
|
|
||||||
|
|
||||||
@router.get("/scan-all")
|
def _scan_imap(mail_conn, db: Session, state: dict, since_date: Optional[str] = None, until_date: Optional[str] = None):
|
||||||
async def scan_all_emails(db: Session = Depends(get_db)):
|
"""Verarbeitet eine IMAP-Verbindung und aktualisiert state."""
|
||||||
"""Scant alle Emails, extrahiert PDFs und verarbeitet Rechnungen"""
|
mail_conn.select("INBOX")
|
||||||
results = {
|
# Datumsfilter als IMAP-Kriterium (Format: DD-Mon-YYYY)
|
||||||
"start_time": datetime.utcnow().isoformat(),
|
criteria = []
|
||||||
"processed": 0,
|
if since_date:
|
||||||
"invoices_found": 0,
|
fmt = datetime.strptime(since_date, "%Y-%m-%d").strftime("%d-%b-%Y")
|
||||||
"pdfs_processed": 0,
|
criteria.append(f'SINCE {fmt}')
|
||||||
"errors": []
|
if until_date:
|
||||||
}
|
fmt = datetime.strptime(until_date, "%Y-%m-%d").strftime("%d-%b-%Y")
|
||||||
|
criteria.append(f'BEFORE {fmt}')
|
||||||
# Strato Mail
|
if criteria:
|
||||||
try:
|
result, data = mail_conn.search(None, *criteria)
|
||||||
mail = connect_imap(IMAP_HOST, IMAP_PORT, IMAP_USER, IMAP_PASSWORD)
|
else:
|
||||||
mail.select("INBOX")
|
result, data = mail_conn.search(None, 'ALL')
|
||||||
|
if result != "OK":
|
||||||
result, data = mail.search(None, 'ALL')
|
return
|
||||||
if result == "OK":
|
|
||||||
email_ids = data[0].split()
|
email_ids = data[0].split()
|
||||||
|
state["total"] = state.get("total", 0) + len(email_ids)
|
||||||
for email_id in email_ids:
|
for email_id in email_ids:
|
||||||
email_id_str = email_id.decode()
|
email_id_str = email_id.decode()
|
||||||
|
|
||||||
# Prüfen ob bereits verarbeitet
|
|
||||||
log_entry = db.query(EmailLog).filter(EmailLog.email_id == email_id_str).first()
|
log_entry = db.query(EmailLog).filter(EmailLog.email_id == email_id_str).first()
|
||||||
if log_entry and log_entry.processed:
|
if log_entry and log_entry.processed:
|
||||||
continue
|
continue
|
||||||
|
result2, msg_data = mail_conn.fetch(email_id, '(RFC822)')
|
||||||
result, msg_data = mail.fetch(email_id, '(RFC822)')
|
if result2 != "OK":
|
||||||
if result != "OK":
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
msg = email.message_from_bytes(msg_data[0][1])
|
msg = email.message_from_bytes(msg_data[0][1])
|
||||||
subject = msg.get("Subject", "")
|
subject = msg.get("Subject", "")
|
||||||
from_addr = msg.get("From", "")
|
from_addr = msg.get("From", "")
|
||||||
date_str = msg.get("Date", "")
|
date_str = msg.get("Date", "")
|
||||||
|
|
||||||
# Datum parsen
|
|
||||||
email_date = parse_email_date(date_str)
|
email_date = parse_email_date(date_str)
|
||||||
|
decoded_subject, enc = decode_header(subject)[0]
|
||||||
# Subject decodieren
|
|
||||||
decoded_subject, encoding = decode_header(subject)[0]
|
|
||||||
if isinstance(decoded_subject, bytes):
|
if isinstance(decoded_subject, bytes):
|
||||||
subject = decoded_subject.decode(encoding or "utf-8")
|
subject = decoded_subject.decode(enc or "utf-8")
|
||||||
|
|
||||||
# Prüfen auf Keywords
|
|
||||||
subject_lower = subject.lower()
|
subject_lower = subject.lower()
|
||||||
matched_keywords = [kw for kw in INVOICE_KEYWORDS if kw in subject_lower]
|
matched_keywords = [kw for kw in INVOICE_KEYWORDS if kw in subject_lower]
|
||||||
|
log_entry = None
|
||||||
if matched_keywords:
|
if matched_keywords:
|
||||||
# Email als Rechnung markieren (auch wenn keine PDF dabei ist)
|
|
||||||
log_entry = EmailLog(
|
log_entry = EmailLog(
|
||||||
email_id=email_id_str,
|
email_id=email_id_str,
|
||||||
email_subject=subject,
|
email_subject=subject,
|
||||||
|
|
@ -330,99 +337,111 @@ async def scan_all_emails(db: Session = Depends(get_db)):
|
||||||
email_folder="INBOX",
|
email_folder="INBOX",
|
||||||
matched_keywords=matched_keywords,
|
matched_keywords=matched_keywords,
|
||||||
has_attachment=True,
|
has_attachment=True,
|
||||||
processed=True
|
processed=True,
|
||||||
)
|
)
|
||||||
db.add(log_entry)
|
db.add(log_entry)
|
||||||
results["invoices_found"] += 1
|
state["invoices_found"] += 1
|
||||||
|
try:
|
||||||
# PDFs extrahieren und verarbeiten
|
|
||||||
pdfs = extract_and_process_attachments(msg, email_id_str, db)
|
pdfs = extract_and_process_attachments(msg, email_id_str, db)
|
||||||
results["pdfs_processed"] += len(pdfs)
|
state["pdfs_processed"] += len(pdfs)
|
||||||
|
|
||||||
# Log aktualisieren mit Ergebnis
|
|
||||||
if pdfs and log_entry:
|
if pdfs and log_entry:
|
||||||
log_entry.processing_result = f"PDFs processed: {len(pdfs)}"
|
log_entry.processing_result = f"PDFs processed: {len(pdfs)}"
|
||||||
log_entry.processed = True
|
log_entry.processed = True
|
||||||
|
except Exception as pdf_err:
|
||||||
|
state["errors"].append(f"PDF-Fehler Email {email_id_str}: {pdf_err}")
|
||||||
db.commit()
|
db.commit()
|
||||||
|
state["processed"] += 1
|
||||||
|
mail_conn.logout()
|
||||||
|
|
||||||
results["processed"] += 1
|
|
||||||
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
mail.logout()
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
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:
|
except Exception as e:
|
||||||
results["errors"].append(f"Strato scan failed: {str(e)}")
|
|
||||||
import traceback
|
import traceback
|
||||||
results["errors"].append(traceback.format_exc())
|
state["errors"].append(f"Strato: {e}\n{traceback.format_exc()}")
|
||||||
|
|
||||||
# Gmail Mail (optional)
|
|
||||||
if GMAIL_IMAP_USER and GMAIL_IMAP_PASSWORD:
|
if GMAIL_IMAP_USER and GMAIL_IMAP_PASSWORD:
|
||||||
try:
|
try:
|
||||||
mail = connect_imap(GMAIL_IMAP_HOST, GMAIL_IMAP_PORT, GMAIL_IMAP_USER, GMAIL_IMAP_PASSWORD)
|
mail = connect_imap(GMAIL_IMAP_HOST, GMAIL_IMAP_PORT, GMAIL_IMAP_USER, GMAIL_IMAP_PASSWORD)
|
||||||
mail.select("INBOX")
|
_scan_imap(mail, db, state, since_date, until_date)
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
results["errors"].append(f"Gmail scan failed: {str(e)}")
|
|
||||||
import traceback
|
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
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,19 @@ C-Stone Invoice Check — Invoice Router
|
||||||
Rechnungs-Verarbeitung und Speicherung
|
Rechnungs-Verarbeitung und Speicherung
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import io
|
||||||
import os
|
import os
|
||||||
import fitz # PyMuPDF
|
import fitz # PyMuPDF
|
||||||
import re
|
import re
|
||||||
|
import json
|
||||||
|
import urllib.request
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import List, Optional, Dict, Any
|
from typing import List, Optional, Dict, Any
|
||||||
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
|
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
|
||||||
|
from fastapi.responses import StreamingResponse
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from openpyxl import Workbook
|
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.database import get_db
|
||||||
from backend.models import Invoice, InvoiceDetail, EmailLog
|
from backend.models import Invoice, InvoiceDetail, EmailLog
|
||||||
|
|
@ -149,6 +153,70 @@ def extract_issuer(text: str) -> Optional[str]:
|
||||||
return None
|
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]:
|
def extract_pdf_data(file_path: str) -> Dict[str, Any]:
|
||||||
"""Extrahiert alle relevanten Daten aus PDF-Datei"""
|
"""Extrahiert alle relevanten Daten aus PDF-Datei"""
|
||||||
try:
|
try:
|
||||||
|
|
@ -161,14 +229,40 @@ def extract_pdf_data(file_path: str) -> Dict[str, Any]:
|
||||||
pages += 1
|
pages += 1
|
||||||
|
|
||||||
doc.close()
|
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 {
|
return {
|
||||||
"text": text,
|
"text": text,
|
||||||
"invoice_number": extract_invoice_number(text),
|
"invoice_number": extract_invoice_number(text),
|
||||||
"amount": extract_amount(text),
|
"amount": extract_amount(text),
|
||||||
"issuer": extract_issuer(text),
|
"issuer": extract_issuer(text),
|
||||||
"tax_info": detect_tax_type(text),
|
"tax_info": detect_tax_type(text),
|
||||||
"pages": pages
|
"pages": pages,
|
||||||
|
"ai_analyzed": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
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)
|
return os.path.join(base_path, str(year), tax_dir)
|
||||||
|
|
||||||
|
|
||||||
def save_to_excel(invoices: List[Invoice], excel_path: str):
|
def build_excel(invoices: List[Invoice]) -> "io.BytesIO":
|
||||||
"""Speichert Rechnungen in Excel-Datei"""
|
import io
|
||||||
wb = Workbook()
|
wb = Workbook()
|
||||||
ws = wb.active
|
ws = wb.active
|
||||||
ws.title = "Rechnungen"
|
ws.title = "Rechnungen"
|
||||||
|
headers = ["Datum", "Rechnungsausteller", "Rechnungsnummer", "Betrag (brutto)", "Steuerart", "Steuersatz"]
|
||||||
# Header
|
|
||||||
headers = ["Datum", "Rechnungsausteller", "Betrag", "Umsatzsteuer", "Steuerart", "Dateipfad"]
|
|
||||||
for col, header in enumerate(headers, 1):
|
for col, header in enumerate(headers, 1):
|
||||||
cell = ws.cell(row=1, column=col, value=header)
|
cell = ws.cell(row=1, column=col, value=header)
|
||||||
cell.font = Font(bold=True)
|
cell.font = Font(bold=True)
|
||||||
|
|
||||||
# Daten
|
|
||||||
for row, inv in enumerate(invoices, 2):
|
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=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=2, value=inv.issuer_name or "")
|
||||||
ws.cell(row=row, column=3, value=inv.invoice_amount or 0)
|
ws.cell(row=row, column=3, value=inv.invoice_number or "")
|
||||||
ws.cell(row=row, column=4, value=inv.invoice_tax_amount or 0)
|
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=5, value=inv.invoice_tax_type or "")
|
||||||
ws.cell(row=row, column=6, value=inv.file_path or "")
|
ws.cell(row=row, column=6, value=f"{int((inv.invoice_tax_rate or 0) * 100)} %" if inv.invoice_tax_rate else "")
|
||||||
|
|
||||||
# Formatieren
|
|
||||||
for col in range(1, 7):
|
for col in range(1, 7):
|
||||||
ws.column_dimensions[chr(64 + col)].width = 20
|
ws.column_dimensions[chr(64 + col)].width = 22
|
||||||
|
buf = io.BytesIO()
|
||||||
wb.save(excel_path)
|
wb.save(buf)
|
||||||
|
buf.seek(0)
|
||||||
|
return buf
|
||||||
|
|
||||||
|
|
||||||
@router.get("/scan-pdf")
|
@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")
|
@router.get("/export-excel")
|
||||||
async def export_to_excel(db: Session = Depends(get_db)):
|
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()
|
invoices = db.query(Invoice).order_by(Invoice.invoice_date.desc()).all()
|
||||||
|
buf = build_excel(invoices)
|
||||||
excel_path = os.path.join(INVOICE_STORAGE_PATH, f"rechnungen_{datetime.utcnow().strftime('%Y%m%d')}.xlsx")
|
filename = f"rechnungen_{datetime.utcnow().strftime('%Y%m%d')}.xlsx"
|
||||||
|
return StreamingResponse(
|
||||||
save_to_excel(invoices, excel_path)
|
buf,
|
||||||
|
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
return {
|
headers={"Content-Disposition": f"attachment; filename={filename}"},
|
||||||
"status": "success",
|
)
|
||||||
"file_path": excel_path,
|
|
||||||
"invoice_count": len(invoices)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/list")
|
@router.get("/list")
|
||||||
|
|
|
||||||
51
backend/routers/settings.py
Normal file
51
backend/routers/settings.py
Normal file
|
|
@ -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"),
|
||||||
|
}
|
||||||
0
clowdex-download/.claude/.claude/hooks/backup-before-write.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/backup-before-write.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/completeness-gate.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/completeness-gate.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/guard-bash.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/guard-bash.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/log-changes.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/log-changes.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/log-failures.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/log-failures.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/log-stop-verdict.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/log-stop-verdict.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/post-compact-resume.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/post-compact-resume.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/pre-compact-handoff.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/pre-compact-handoff.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/session-reset.sh
Executable file → Normal file
0
clowdex-download/.claude/.claude/hooks/session-reset.sh
Executable file → Normal file
|
|
@ -5,7 +5,7 @@ services:
|
||||||
dockerfile: Dockerfile.backend
|
dockerfile: Dockerfile.backend
|
||||||
container_name: invoice-check-backend
|
container_name: invoice-check-backend
|
||||||
ports:
|
ports:
|
||||||
- "127.0.0.1:8005:8001" # Host 8005 (Projekt-Port) — 8001 belegt LLM-Tunnel
|
- "127.0.0.1:8005:8001"
|
||||||
environment:
|
environment:
|
||||||
- DATABASE_URL=postgresql://postgres:postgres@db:5432/invoice_check
|
- DATABASE_URL=postgresql://postgres:postgres@db:5432/invoice_check
|
||||||
- SECRET_KEY=${SECRET_KEY}
|
- SECRET_KEY=${SECRET_KEY}
|
||||||
|
|
@ -28,6 +28,19 @@ services:
|
||||||
- /tmp/test-invoices:/tmp/test-invoices
|
- /tmp/test-invoices:/tmp/test-invoices
|
||||||
- invoice-storage:/tmp/invoice-storage
|
- 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:
|
db:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
container_name: invoice-check-db
|
container_name: invoice-check-db
|
||||||
|
|
|
||||||
24
frontend/nginx.conf
Normal file
24
frontend/nginx.conf
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,13 +11,19 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"react": "^18.3.1",
|
"react": "^18.3.1",
|
||||||
"react-dom": "^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": {
|
"devDependencies": {
|
||||||
"@types/react": "^18.3.0",
|
"@types/react": "^18.3.0",
|
||||||
"@types/react-dom": "^18.3.0",
|
"@types/react-dom": "^18.3.0",
|
||||||
"@vitejs/plugin-react": "^4.3.0",
|
"@vitejs/plugin-react": "^4.3.0",
|
||||||
"typescript": "^5.5.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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
6
frontend/postcss.config.js
Normal file
6
frontend/postcss.config.js
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
export default {
|
||||||
|
plugins: {
|
||||||
|
tailwindcss: {},
|
||||||
|
autoprefixer: {},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
@ -1,52 +1,238 @@
|
||||||
import { useState, useEffect } from 'react'
|
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
|
// Types
|
||||||
interface Invoice {
|
interface Invoice {
|
||||||
id: number
|
id: number
|
||||||
|
email_id: string
|
||||||
invoice_number: string | null
|
invoice_number: string | null
|
||||||
invoice_date: string | null
|
invoice_date: string | null
|
||||||
issuer_name: string | null
|
issuer_name: string | null
|
||||||
invoice_amount: number | null
|
invoice_amount: number | null
|
||||||
invoice_tax_type: string | null
|
invoice_tax_type: string | null
|
||||||
|
invoice_tax_rate: number | null
|
||||||
file_path: string | null
|
file_path: string | null
|
||||||
processed: boolean
|
processed: boolean
|
||||||
|
created_at: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EmailLog {
|
interface EmailLog {
|
||||||
id: number
|
id: number
|
||||||
email_id: string
|
email_id: string
|
||||||
subject: string
|
email_subject: string
|
||||||
from: string
|
email_from: string
|
||||||
|
email_date: string | null
|
||||||
|
email_folder: string | null
|
||||||
|
matched_keywords: string[] | null
|
||||||
|
has_attachment: boolean
|
||||||
processed: boolean
|
processed: boolean
|
||||||
|
processing_result: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
// API Helper
|
// API Helper (Docker-Container: nginx proxyt /api/ an backend)
|
||||||
const API_BASE = 'http://localhost:8001'
|
const API_BASE = '/api'
|
||||||
|
|
||||||
async function fetchInvoices(): Promise<Invoice[]> {
|
async function fetchInvoices(): Promise<Invoice[]> {
|
||||||
const response = await fetch(`${API_BASE}/invoices/list`)
|
const response = await axios.get(`${API_BASE}/invoices/list`)
|
||||||
if (!response.ok) throw new Error('Failed to fetch invoices')
|
return response.data.invoices || []
|
||||||
const data = await response.json()
|
|
||||||
return data.invoices || []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function scanAllEmails() {
|
async function fetchEmailLogs(): Promise<EmailLog[]> {
|
||||||
const response = await fetch(`${API_BASE}/emails/scan-all`, { method: 'POST' })
|
const response = await axios.get(`${API_BASE}/emails/list`)
|
||||||
if (!response.ok) throw new Error('Failed to scan emails')
|
return response.data.logs || []
|
||||||
return response.json()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function exportToExcel() {
|
async function startScan(force = false, sinceDate?: string, untilDate?: string): Promise<string> {
|
||||||
const response = await fetch(`${API_BASE}/invoices/export-excel`, { method: 'POST' })
|
const params = new URLSearchParams({ force: String(force) })
|
||||||
if (!response.ok) throw new Error('Failed to export')
|
if (sinceDate) params.set('since_date', sinceDate)
|
||||||
return response.json()
|
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<any> {
|
||||||
|
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 (
|
||||||
|
<span className={`px-2 py-1 text-xs font-medium rounded-full ${colors[color]}`}>
|
||||||
|
{children}
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function InvoiceDetailModal({ invoice, onClose }: { invoice: Invoice | null; onClose: () => void }) {
|
||||||
|
if (!invoice) return null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4" onClick={onClose}>
|
||||||
|
<div className="bg-white rounded-lg max-w-2xl w-full max-h-[90vh] overflow-y-auto" onClick={(e) => e.stopPropagation()}>
|
||||||
|
<div className="p-6">
|
||||||
|
<div className="flex justify-between items-start mb-4">
|
||||||
|
<h2 className="text-2xl font-bold text-gray-900">Rechnung Details</h2>
|
||||||
|
<button onClick={onClose} className="text-gray-500 hover:text-gray-700 text-2xl">
|
||||||
|
×
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-2 gap-4 mb-6">
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-500">Rechnungsnummer</label>
|
||||||
|
<p className="text-lg font-semibold text-gray-900">{invoice.invoice_number || '-'}</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-500">Betrag</label>
|
||||||
|
<p className="text-lg font-semibold text-green-600">{formatCurrency(invoice.invoice_amount)}</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-500">Steuerart</label>
|
||||||
|
<p className="text-lg font-semibold text-gray-900">{invoice.invoice_tax_type || '-'}</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-500">Steuersatz</label>
|
||||||
|
<p className="text-lg font-semibold text-gray-900">{invoice.invoice_tax_rate ? (invoice.invoice_tax_rate * 100).toFixed(0) + '%' : '-'}</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-500">Absender</label>
|
||||||
|
<p className="text-lg font-semibold text-gray-900">{invoice.issuer_name || '-'}</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-500">Datum</label>
|
||||||
|
<p className="text-lg font-semibold text-gray-900">{formatDate(invoice.invoice_date)}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="border-t pt-4">
|
||||||
|
<label className="block text-sm font-medium text-gray-500 mb-2">Dateipfad</label>
|
||||||
|
<p className="text-sm text-gray-600 break-all">{invoice.file_path || '-'}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mt-6 flex gap-3">
|
||||||
|
<a
|
||||||
|
href={`${API_BASE}/invoices/pdf/${invoice.id}`}
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
className="flex-1 bg-blue-600 text-white py-2 px-4 rounded-lg hover:bg-blue-700 text-center font-medium"
|
||||||
|
>
|
||||||
|
PDF Öffnen
|
||||||
|
</a>
|
||||||
|
<a
|
||||||
|
href={`file://${invoice.file_path}`}
|
||||||
|
className="flex-1 bg-gray-600 text-white py-2 px-4 rounded-lg hover:bg-gray-700 text-center font-medium"
|
||||||
|
onClick={(e) => {
|
||||||
|
if (!invoice.file_path?.startsWith('/tmp')) {
|
||||||
|
e.preventDefault()
|
||||||
|
alert('Datei ist im Container gespeichert. Zugriff nur innerhalb des Containers möglich.')
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Herunterladen
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function EmailLogModal({ log, onClose }: { log: EmailLog | null; onClose: () => void }) {
|
||||||
|
if (!log) return null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4" onClick={onClose}>
|
||||||
|
<div className="bg-white rounded-lg max-w-2xl w-full max-h-[90vh] overflow-y-auto" onClick={(e) => e.stopPropagation()}>
|
||||||
|
<div className="p-6">
|
||||||
|
<div className="flex justify-between items-start mb-4">
|
||||||
|
<h2 className="text-2xl font-bold text-gray-900">Email Details</h2>
|
||||||
|
<button onClick={onClose} className="text-gray-500 hover:text-gray-700 text-2xl">×</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mb-4">
|
||||||
|
<label className="block text-sm font-medium text-gray-500">Betreff</label>
|
||||||
|
<p className="text-lg font-semibold text-gray-900">{log.email_subject}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mb-4">
|
||||||
|
<label className="block text-sm font-medium text-gray-500">Absender</label>
|
||||||
|
<p className="text-gray-600">{log.email_from}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mb-4">
|
||||||
|
<label className="block text-sm font-medium text-gray-500">Datum</label>
|
||||||
|
<p className="text-gray-600">{formatDate(log.email_date)}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mb-4">
|
||||||
|
<label className="block text-sm font-medium text-gray-500">Ordner</label>
|
||||||
|
<p className="text-gray-600">{log.email_folder || 'INBOX'}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{log.matched_keywords && (
|
||||||
|
<div className="mb-4">
|
||||||
|
<label className="block text-sm font-medium text-gray-500">Gefundene Keywords</label>
|
||||||
|
<div className="flex flex-wrap gap-2 mt-1">
|
||||||
|
{log.matched_keywords.map((kw, idx) => (
|
||||||
|
<Badge key={idx} color="yellow">{kw}</Badge>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{log.processing_result && (
|
||||||
|
<div className="border-t pt-4">
|
||||||
|
<label className="block text-sm font-medium text-gray-500">Ergebnis</label>
|
||||||
|
<p className="text-gray-600">{log.processing_result}</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function InvoicesView() {
|
||||||
const [invoices, setInvoices] = useState<Invoice[]>([])
|
const [invoices, setInvoices] = useState<Invoice[]>([])
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
|
const [selectedInvoice, setSelectedInvoice] = useState<Invoice | null>(null)
|
||||||
const [status, setStatus] = useState<string | null>(null)
|
const [status, setStatus] = useState<string | null>(null)
|
||||||
|
const [toast, setToast] = useState<string | null>(null)
|
||||||
|
const [sinceDate, setSinceDate] = useState('')
|
||||||
|
const [untilDate, setUntilDate] = useState('')
|
||||||
|
|
||||||
|
function showToast(msg: string) {
|
||||||
|
setToast(msg)
|
||||||
|
setTimeout(() => setToast(null), 5000)
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadInvoices()
|
loadInvoices()
|
||||||
|
|
@ -63,86 +249,160 @@ 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (loading) {
|
|
||||||
return <div className="loading">Lade...</div>
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="app">
|
<div className="space-y-6">
|
||||||
<nav className="navbar">
|
{/* Toast-Benachrichtigung */}
|
||||||
<Link to="/" className="brand">C-Stone Invoice Check</Link>
|
{toast && (
|
||||||
<div className="nav-links">
|
<div className="fixed top-6 right-6 z-50 bg-green-600 text-white px-6 py-3 rounded-lg shadow-lg text-sm font-medium">
|
||||||
<Link to="/">Rechnungen</Link>
|
✓ {toast}
|
||||||
<Link to="/settings">Einstellungen</Link>
|
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
)}
|
||||||
|
|
||||||
<main className="container">
|
<div className="flex justify-between items-start">
|
||||||
<header className="header">
|
<h1 className="text-3xl font-bold text-gray-900">Rechnungen</h1>
|
||||||
<h1>Rechnungsübersicht</h1>
|
<div className="flex flex-col gap-3 items-end">
|
||||||
<p>{status || 'Verwalte deine Rechnungen hier'}</p>
|
{/* Datumsfilter */}
|
||||||
</header>
|
<div className="flex gap-2 items-center text-sm text-gray-600">
|
||||||
|
<label>Von:</label>
|
||||||
<div className="actions">
|
<input type="date" value={sinceDate} onChange={e => setSinceDate(e.target.value)}
|
||||||
<button onClick={handleScanEmails} className="btn btn-primary">
|
className="border border-gray-300 rounded px-2 py-1" />
|
||||||
📧 Emails scannen
|
<label>Bis:</label>
|
||||||
|
<input type="date" value={untilDate} onChange={e => setUntilDate(e.target.value)}
|
||||||
|
className="border border-gray-300 rounded px-2 py-1" />
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-3">
|
||||||
|
<button
|
||||||
|
onClick={async () => {
|
||||||
|
setLoading(true)
|
||||||
|
setStatus('Scan gestartet, läuft im Hintergrund…')
|
||||||
|
try {
|
||||||
|
const taskId = await startScan(false, sinceDate || undefined, untilDate || undefined)
|
||||||
|
const interval = setInterval(async () => {
|
||||||
|
try {
|
||||||
|
const s = await pollScan(taskId)
|
||||||
|
const total = s.total ? ` von ${s.total}` : ''
|
||||||
|
setStatus(`Läuft: ${s.processed}${total} Emails, ${s.pdfs_processed} PDFs, ${s.invoices_found} Rechnungen…`)
|
||||||
|
if (s.status === 'done') {
|
||||||
|
clearInterval(interval)
|
||||||
|
setLoading(false)
|
||||||
|
setStatus(null)
|
||||||
|
showToast(`Vorgang abgeschlossen: ${s.invoices_found} Rechnungen, ${s.pdfs_processed} PDFs verarbeitet`)
|
||||||
|
await loadInvoices()
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
clearInterval(interval)
|
||||||
|
setLoading(false)
|
||||||
|
setStatus('Fehler beim Abfragen des Scan-Status')
|
||||||
|
}
|
||||||
|
}, 3000)
|
||||||
|
} catch (err) {
|
||||||
|
setStatus(`Fehler: ${err}`)
|
||||||
|
setLoading(false)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
disabled={loading}
|
||||||
|
className="flex items-center gap-2 bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 disabled:opacity-50"
|
||||||
|
>
|
||||||
|
<span>📧</span> {loading ? 'Scannt…' : 'Emails scannen'}
|
||||||
</button>
|
</button>
|
||||||
<button onClick={handleExport} className="btn btn-secondary">
|
<button
|
||||||
📊 Als Excel exportieren
|
onClick={async () => {
|
||||||
|
if (!confirm('Alle gespeicherten Rechnungen löschen und neu scannen?')) return
|
||||||
|
setLoading(true)
|
||||||
|
setStatus('Vollständiger Rescan gestartet…')
|
||||||
|
try {
|
||||||
|
const taskId = await startScan(true, sinceDate || undefined, untilDate || undefined)
|
||||||
|
const interval = setInterval(async () => {
|
||||||
|
try {
|
||||||
|
const s = await pollScan(taskId)
|
||||||
|
const total2 = s.total ? ` von ${s.total}` : ''
|
||||||
|
setStatus(`Rescan läuft: ${s.processed}${total2} Emails, ${s.pdfs_processed} PDFs, ${s.invoices_found} Rechnungen…`)
|
||||||
|
if (s.status === 'done') {
|
||||||
|
clearInterval(interval)
|
||||||
|
setLoading(false)
|
||||||
|
setStatus(null)
|
||||||
|
showToast(`Rescan abgeschlossen: ${s.invoices_found} Rechnungen, ${s.pdfs_processed} PDFs`)
|
||||||
|
await loadInvoices()
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
clearInterval(interval)
|
||||||
|
setLoading(false)
|
||||||
|
setStatus('Fehler beim Rescan')
|
||||||
|
}
|
||||||
|
}, 3000)
|
||||||
|
} catch (err) {
|
||||||
|
setStatus(`Fehler: ${err}`)
|
||||||
|
setLoading(false)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
disabled={loading}
|
||||||
|
className="flex items-center gap-2 bg-orange-600 text-white px-4 py-2 rounded-lg hover:bg-orange-700 disabled:opacity-50"
|
||||||
|
>
|
||||||
|
<span>🔄</span> {loading ? 'Scannt…' : 'Neu scannen (alle)'}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => exportToExcel()}
|
||||||
|
className="flex items-center gap-2 bg-green-600 text-white px-4 py-2 rounded-lg hover:bg-green-700"
|
||||||
|
>
|
||||||
|
<span>📊</span> Als Excel exportieren
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="table-container">
|
{status && (
|
||||||
<table className="table">
|
<div className="p-4 rounded-lg bg-blue-50 text-blue-800 text-sm">
|
||||||
<thead>
|
{status}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="bg-white rounded-lg shadow overflow-hidden">
|
||||||
|
<table className="w-full">
|
||||||
|
<thead className="bg-gray-50">
|
||||||
<tr>
|
<tr>
|
||||||
<th>Rechnungsdatum</th>
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Rechnungsdatum</th>
|
||||||
<th>Absender</th>
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Absender</th>
|
||||||
<th>Rechnungsnummer</th>
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Rechnungsnummer</th>
|
||||||
<th>Betrag (€)</th>
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Betrag</th>
|
||||||
<th>Steuer</th>
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Steuer</th>
|
||||||
<th>Status</th>
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
|
||||||
|
<th className="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">Aktionen</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody className="divide-y divide-gray-200">
|
||||||
{invoices.length === 0 ? (
|
{loading ? (
|
||||||
<tr>
|
<tr>
|
||||||
<td colSpan={6}>Keine Rechnungen gefunden. Scanne Emails.</td>
|
<td colSpan={7} className="px-6 py-4 text-center text-gray-500">Lade...</td>
|
||||||
|
</tr>
|
||||||
|
) : invoices.length === 0 ? (
|
||||||
|
<tr>
|
||||||
|
<td colSpan={7} className="px-6 py-4 text-center text-gray-500">
|
||||||
|
Keine Rechnungen gefunden. Klicken Sie auf "Emails scannen".
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
) : (
|
) : (
|
||||||
invoices.map((inv) => (
|
invoices.map((inv) => (
|
||||||
<tr key={inv.id}>
|
<tr key={inv.id} className="hover:bg-gray-50">
|
||||||
<td>{inv.invoice_date?.split('T')[0] || '-'}</td>
|
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{formatDate(inv.invoice_date)}</td>
|
||||||
<td>{inv.issuer_name || '-'}</td>
|
<td className="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">{inv.issuer_name || '-'}</td>
|
||||||
<td>{inv.invoice_number || '-'}</td>
|
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{inv.invoice_number || '-'}</td>
|
||||||
<td>{inv.invoice_amount ? inv.invoice_amount.toFixed(2) : '-'}</td>
|
<td className="px-6 py-4 whitespace-nowrap text-sm font-semibold text-green-600">{formatCurrency(inv.invoice_amount)}</td>
|
||||||
<td>{inv.invoice_tax_type || 'unbekannt'}</td>
|
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
|
||||||
<td>{inv.processed ? '✅ Verarbeitet' : '⏳ Wartend'}</td>
|
<Badge color={inv.invoice_tax_type?.includes('mit') ? 'yellow' : 'gray'}>
|
||||||
|
{inv.invoice_tax_type || 'unbekannt'}
|
||||||
|
</Badge>
|
||||||
|
</td>
|
||||||
|
<td className="px-6 py-4 whitespace-nowrap">
|
||||||
|
{inv.processed ? <Badge color="green">Verarbeitet</Badge> : <Badge color="gray">Wartend</Badge>}
|
||||||
|
</td>
|
||||||
|
<td className="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
|
||||||
|
<button
|
||||||
|
onClick={() => setSelectedInvoice(inv)}
|
||||||
|
className="text-blue-600 hover:text-blue-900"
|
||||||
|
>
|
||||||
|
Details
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
))
|
))
|
||||||
)}
|
)}
|
||||||
|
|
@ -150,16 +410,258 @@ function App() {
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="stats">
|
{selectedInvoice && (
|
||||||
<div className="stat-item">
|
<InvoiceDetailModal invoice={selectedInvoice} onClose={() => setSelectedInvoice(null)} />
|
||||||
<strong>{invoices.length}</strong> Rechnungen
|
)}
|
||||||
</div>
|
|
||||||
<div className="stat-item">
|
<div className="flex justify-between text-sm text-gray-500">
|
||||||
<strong>
|
<span>{invoices.length} Rechnungen</span>
|
||||||
{invoices.filter(i => i.processed).length}
|
<span>{invoices.filter(i => i.processed).length} verarbeitet</span>
|
||||||
</strong> verarbeitet
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function EmailLogsView() {
|
||||||
|
const [logs, setLogs] = useState<EmailLog[]>([])
|
||||||
|
const [loading, setLoading] = useState(true)
|
||||||
|
const [selectedLog, setSelectedLog] = useState<EmailLog | null>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
async function loadLogs() {
|
||||||
|
try {
|
||||||
|
const data = await fetchEmailLogs()
|
||||||
|
setLogs(data)
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err)
|
||||||
|
} finally {
|
||||||
|
setLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loadLogs()
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-6">
|
||||||
|
<h1 className="text-3xl font-bold text-gray-900">Email Logs</h1>
|
||||||
|
|
||||||
|
<div className="bg-white rounded-lg shadow overflow-hidden">
|
||||||
|
<table className="w-full">
|
||||||
|
<thead className="bg-gray-50">
|
||||||
|
<tr>
|
||||||
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Datum</th>
|
||||||
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Absender</th>
|
||||||
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Betreff</th>
|
||||||
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Keywords</th>
|
||||||
|
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
|
||||||
|
<th className="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">Aktionen</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody className="divide-y divide-gray-200">
|
||||||
|
{loading ? (
|
||||||
|
<tr>
|
||||||
|
<td colSpan={6} className="px-6 py-4 text-center text-gray-500">Lade...</td>
|
||||||
|
</tr>
|
||||||
|
) : logs.length === 0 ? (
|
||||||
|
<tr>
|
||||||
|
<td colSpan={6} className="px-6 py-4 text-center text-gray-500">Keine Emails gefunden.</td>
|
||||||
|
</tr>
|
||||||
|
) : (
|
||||||
|
logs.map((log) => (
|
||||||
|
<tr key={log.id} className="hover:bg-gray-50">
|
||||||
|
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{formatDate(log.email_date)}</td>
|
||||||
|
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{log.email_from}</td>
|
||||||
|
<td className="px-6 py-4 truncate max-w-xs text-sm text-gray-600">{log.email_subject}</td>
|
||||||
|
<td className="px-6 py-4 whitespace-nowrap">
|
||||||
|
{log.matched_keywords?.map((kw, idx) => (
|
||||||
|
<span key={idx} className="inline-block bg-yellow-100 text-yellow-800 text-xs px-1.5 py-0.5 rounded mr-1">
|
||||||
|
{kw}
|
||||||
|
</span>
|
||||||
|
))}
|
||||||
|
</td>
|
||||||
|
<td className="px-6 py-4 whitespace-nowrap">
|
||||||
|
{log.processed ? <Badge color="green">Verarbeitet</Badge> : <Badge color="gray">Wartend</Badge>}
|
||||||
|
</td>
|
||||||
|
<td className="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
|
||||||
|
<button
|
||||||
|
onClick={() => setSelectedLog(log)}
|
||||||
|
className="text-blue-600 hover:text-blue-900"
|
||||||
|
>
|
||||||
|
Details
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{selectedLog && <EmailLogModal log={selectedLog} onClose={() => setSelectedLog(null)} />}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function DashboardView() {
|
||||||
|
const [stats, setStats] = useState({ totalInvoices: 0, processedInvoices: 0, processedEmails: 0 })
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
async function loadStats() {
|
||||||
|
try {
|
||||||
|
const [invRes, logRes] = await Promise.all([
|
||||||
|
axios.get(`${API_BASE}/invoices/list`),
|
||||||
|
axios.get(`${API_BASE}/emails/list`)
|
||||||
|
])
|
||||||
|
setStats({
|
||||||
|
totalInvoices: invRes.data.invoices.length,
|
||||||
|
processedInvoices: invRes.data.invoices.filter((i: Invoice) => i.processed).length,
|
||||||
|
processedEmails: logRes.data.logs.length
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loadStats()
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-6">
|
||||||
|
<h1 className="text-3xl font-bold text-gray-900">Dashboard</h1>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-3 gap-6">
|
||||||
|
<div className="bg-white p-6 rounded-lg shadow">
|
||||||
|
<h3 className="text-sm font-medium text-gray-500">Rechnungen</h3>
|
||||||
|
<p className="text-3xl font-bold text-blue-600">{stats.totalInvoices}</p>
|
||||||
|
</div>
|
||||||
|
<div className="bg-white p-6 rounded-lg shadow">
|
||||||
|
<h3 className="text-sm font-medium text-gray-500">Verarbeitet</h3>
|
||||||
|
<p className="text-3xl font-bold text-green-600">{stats.processedInvoices}</p>
|
||||||
|
</div>
|
||||||
|
<div className="bg-white p-6 rounded-lg shadow">
|
||||||
|
<h3 className="text-sm font-medium text-gray-500">Emails gescanned</h3>
|
||||||
|
<p className="text-3xl font-bold text-purple-600">{stats.processedEmails}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="bg-blue-50 p-6 rounded-lg">
|
||||||
|
<h2 className="text-lg font-semibold text-blue-900 mb-2">Anleitung</h2>
|
||||||
|
<ul className="text-blue-800 space-y-2">
|
||||||
|
<li>1. Klicken Sie auf "Emails scannen", um neue Rechnungen zu finden</li>
|
||||||
|
<li>2. Alle PDF-Anhänge werden automatisch verarbeitet</li>
|
||||||
|
<li>3. Steuerangaben werden automatisch erkannt (mit/ohne MwSt)</li>
|
||||||
|
<li>4. PDFs werden im Container unter /tmp/invoice-storage gespeichert</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function SettingsView() {
|
||||||
|
const FIELDS = [
|
||||||
|
{ key: 'IMAP_HOST', label: 'IMAP Server', placeholder: 'imap.strato.de' },
|
||||||
|
{ key: 'IMAP_PORT', label: 'IMAP Port', placeholder: '993' },
|
||||||
|
{ key: 'IMAP_USER', label: 'Email-Adresse', placeholder: 'info@example.de' },
|
||||||
|
{ key: 'IMAP_PASSWORD', label: 'Passwort / App-Passwort', placeholder: '••••••••', type: 'password' },
|
||||||
|
{ key: 'INVOICE_STORAGE_PATH', label: 'Ablagepfad', placeholder: '/tmp/invoice-storage' },
|
||||||
|
]
|
||||||
|
|
||||||
|
const [values, setValues] = useState<Record<string, string>>({})
|
||||||
|
const [status, setStatus] = useState<string | null>(null)
|
||||||
|
const [loading, setLoading] = useState(true)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
axios.get(`${API_BASE}/settings/`).then(r => {
|
||||||
|
setValues(r.data)
|
||||||
|
setLoading(false)
|
||||||
|
}).catch(() => setLoading(false))
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
async function save() {
|
||||||
|
try {
|
||||||
|
await axios.post(`${API_BASE}/settings/`, values)
|
||||||
|
setStatus('Einstellungen gespeichert.')
|
||||||
|
} catch {
|
||||||
|
setStatus('Fehler beim Speichern.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loading) return <div className="text-gray-500">Lade...</div>
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-6 max-w-xl">
|
||||||
|
<h1 className="text-3xl font-bold text-gray-900">IMAP Einstellungen</h1>
|
||||||
|
|
||||||
|
<div className="bg-white rounded-lg shadow p-6 space-y-4">
|
||||||
|
{FIELDS.map(f => (
|
||||||
|
<div key={f.key}>
|
||||||
|
<label className="block text-sm font-medium text-gray-700 mb-1">{f.label}</label>
|
||||||
|
<input
|
||||||
|
type={f.type || 'text'}
|
||||||
|
placeholder={f.placeholder}
|
||||||
|
value={values[f.key] || ''}
|
||||||
|
onChange={e => setValues(v => ({ ...v, [f.key]: e.target.value }))}
|
||||||
|
className="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
|
||||||
|
<div className="pt-2">
|
||||||
|
<button
|
||||||
|
onClick={save}
|
||||||
|
className="w-full bg-blue-600 text-white py-2 px-4 rounded-lg hover:bg-blue-700 font-medium"
|
||||||
|
>
|
||||||
|
Speichern
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{status && <p className="text-sm text-center text-gray-600">{status}</p>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p className="text-xs text-gray-500">
|
||||||
|
Passwort wird verschlüsselt in der Datenbank gespeichert. Für Gmail bitte ein App-Passwort verwenden.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
const location = useLocation()
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-screen bg-gray-100">
|
||||||
|
<nav className="bg-white shadow-sm">
|
||||||
|
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
||||||
|
<div className="flex justify-between h-16">
|
||||||
|
<div className="flex items-center">
|
||||||
|
<Link to="/" className="text-2xl font-bold text-blue-600">
|
||||||
|
C-Stone Invoice Check
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-4">
|
||||||
|
<Link to="/" className={`px-3 py-2 rounded-md font-medium ${location.pathname === '/' ? 'bg-blue-50 text-blue-700' : 'text-gray-700 hover:text-gray-900'}`}>
|
||||||
|
Dashboard
|
||||||
|
</Link>
|
||||||
|
<Link to="/invoices" className={`px-3 py-2 rounded-md font-medium ${location.pathname === '/invoices' ? 'bg-blue-50 text-blue-700' : 'text-gray-700 hover:text-gray-900'}`}>
|
||||||
|
Rechnungen
|
||||||
|
</Link>
|
||||||
|
<Link to="/emails" className={`px-3 py-2 rounded-md font-medium ${location.pathname === '/emails' ? 'bg-blue-50 text-blue-700' : 'text-gray-700 hover:text-gray-900'}`}>
|
||||||
|
Email Logs
|
||||||
|
</Link>
|
||||||
|
<Link to="/settings" className={`px-3 py-2 rounded-md font-medium ${location.pathname === '/settings' ? 'bg-blue-50 text-blue-700' : 'text-gray-700 hover:text-gray-900'}`}>
|
||||||
|
Einstellungen
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
<main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
||||||
|
<Routes>
|
||||||
|
<Route path="/" element={<DashboardView />} />
|
||||||
|
<Route path="/invoices" element={<InvoicesView />} />
|
||||||
|
<Route path="/emails" element={<EmailLogsView />} />
|
||||||
|
<Route path="/settings" element={<SettingsView />} />
|
||||||
|
</Routes>
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,7 @@
|
||||||
|
@tailwind base;
|
||||||
|
@tailwind components;
|
||||||
|
@tailwind utilities;
|
||||||
|
|
||||||
* {
|
* {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
|
|
@ -8,144 +12,10 @@ body {
|
||||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
|
||||||
background: #f5f5f5;
|
background: #f5f5f5;
|
||||||
color: #333;
|
color: #333;
|
||||||
|
-webkit-font-smoothing: antialiased;
|
||||||
|
-moz-osx-font-smoothing: grayscale;
|
||||||
}
|
}
|
||||||
|
|
||||||
.app {
|
code {
|
||||||
min-height: 100vh;
|
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace;
|
||||||
}
|
|
||||||
|
|
||||||
.navbar {
|
|
||||||
background: #1a1a2e;
|
|
||||||
color: white;
|
|
||||||
padding: 1rem 2rem;
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: space-between;
|
|
||||||
}
|
|
||||||
|
|
||||||
.brand {
|
|
||||||
font-size: 1.5rem;
|
|
||||||
font-weight: bold;
|
|
||||||
color: #4ecca3;
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.nav-links a {
|
|
||||||
color: white;
|
|
||||||
text-decoration: none;
|
|
||||||
margin-left: 1.5rem;
|
|
||||||
opacity: 0.8;
|
|
||||||
transition: opacity 0.2s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.nav-links a:hover {
|
|
||||||
opacity: 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
.container {
|
|
||||||
max-width: 1200px;
|
|
||||||
margin: 0 auto;
|
|
||||||
padding: 2rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.header {
|
|
||||||
margin-bottom: 2rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.header h1 {
|
|
||||||
font-size: 2rem;
|
|
||||||
margin-bottom: 0.5rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.header p {
|
|
||||||
color: #666;
|
|
||||||
}
|
|
||||||
|
|
||||||
.actions {
|
|
||||||
display: flex;
|
|
||||||
gap: 1rem;
|
|
||||||
margin-bottom: 2rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn {
|
|
||||||
padding: 0.75rem 1.5rem;
|
|
||||||
border: none;
|
|
||||||
border-radius: 8px;
|
|
||||||
cursor: pointer;
|
|
||||||
font-size: 1rem;
|
|
||||||
font-weight: 500;
|
|
||||||
transition: transform 0.2s, box-shadow 0.2s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn:hover {
|
|
||||||
transform: translateY(-2px);
|
|
||||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn-primary {
|
|
||||||
background: #4ecca3;
|
|
||||||
color: white;
|
|
||||||
}
|
|
||||||
|
|
||||||
.btn-secondary {
|
|
||||||
background: #1a1a2e;
|
|
||||||
color: white;
|
|
||||||
}
|
|
||||||
|
|
||||||
.table-container {
|
|
||||||
background: white;
|
|
||||||
border-radius: 12px;
|
|
||||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
|
||||||
overflow: hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
.table {
|
|
||||||
width: 100%;
|
|
||||||
border-collapse: collapse;
|
|
||||||
}
|
|
||||||
|
|
||||||
.table th {
|
|
||||||
background: #f8f9fa;
|
|
||||||
padding: 1rem;
|
|
||||||
text-align: left;
|
|
||||||
font-weight: 600;
|
|
||||||
color: #495057;
|
|
||||||
border-bottom: 2px solid #e9ecef;
|
|
||||||
}
|
|
||||||
|
|
||||||
.table td {
|
|
||||||
padding: 1rem;
|
|
||||||
border-bottom: 1px solid #e9ecef;
|
|
||||||
}
|
|
||||||
|
|
||||||
.table tbody tr:hover {
|
|
||||||
background: #f8f9fa;
|
|
||||||
}
|
|
||||||
|
|
||||||
.stats {
|
|
||||||
display: flex;
|
|
||||||
gap: 2rem;
|
|
||||||
margin-top: 2rem;
|
|
||||||
padding-top: 2rem;
|
|
||||||
border-top: 1px solid #e9ecef;
|
|
||||||
}
|
|
||||||
|
|
||||||
.stat-item {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 0.5rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.stat-item strong {
|
|
||||||
font-size: 2rem;
|
|
||||||
color: #4ecca3;
|
|
||||||
}
|
|
||||||
|
|
||||||
.loading {
|
|
||||||
display: flex;
|
|
||||||
justify-content: center;
|
|
||||||
align-items: center;
|
|
||||||
min-height: 100vh;
|
|
||||||
font-size: 1.5rem;
|
|
||||||
color: #4ecca3;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import ReactDOM from 'react-dom/client'
|
import ReactDOM from 'react-dom/client'
|
||||||
|
import { BrowserRouter } from 'react-router-dom'
|
||||||
import App from './App.tsx'
|
import App from './App.tsx'
|
||||||
import './index.css'
|
import './index.css'
|
||||||
|
|
||||||
ReactDOM.createRoot(document.getElementById('root')!).render(
|
ReactDOM.createRoot(document.getElementById('root')!).render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
|
<BrowserRouter>
|
||||||
<App />
|
<App />
|
||||||
|
</BrowserRouter>
|
||||||
</React.StrictMode>,
|
</React.StrictMode>,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
6
frontend/tailwind.config.js
Normal file
6
frontend/tailwind.config.js
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
/** @type {import('tailwindcss').Config} */
|
||||||
|
export default {
|
||||||
|
content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
|
||||||
|
theme: { extend: {} },
|
||||||
|
plugins: [],
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue