From f737aff212c8a8eed36f6d2da75fba0ed6a715dc Mon Sep 17 00:00:00 2001 From: fds2610-bot Date: Fri, 10 Apr 2026 16:36:29 +0200 Subject: [PATCH] Feature: Extrahierten Belegtext in beleg_text speichern (Migration 021) --- Changes.md | 10 ++++++++ migrations/021_beleg_text.sql | 4 +++ src/Models/Beleg.php | 8 ++++++ src/Services/KontierungsvorschlagService.php | 27 +++++++++++++++----- 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 migrations/021_beleg_text.sql diff --git a/Changes.md b/Changes.md index 18e826c..5a04d0e 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,16 @@ --- +## 2026-04-10 – Belegtext-Speicherung für spätere Volltextsuche + +### Neu +- Migration 021: Spalte `beleg_text MEDIUMTEXT` in `belege` – speichert den per `pdftotext` oder Tesseract-OCR extrahierten Volltext +- `KontierungsvorschlagService::extractText()`: neue public Methode zur Textextraktion (PDF + Bild), unabhängig vom KI-Vorschlag nutzbar +- Beim Generieren des KI-Vorschlags wird `beleg_text` automatisch befüllt (einmalig, nur wenn noch leer) +- `Beleg::saveBelegText()`: neue Methode im Model + +--- + ## 2026-04-10 – OCR-Schalter unabhängig vom KI-Schalter ### Neu diff --git a/migrations/021_beleg_text.sql b/migrations/021_beleg_text.sql new file mode 100644 index 0000000..87b7681 --- /dev/null +++ b/migrations/021_beleg_text.sql @@ -0,0 +1,4 @@ +-- Migration 021: Extrahierter Belegtext als durchsuchbares Metafeld +ALTER TABLE belege + ADD COLUMN IF NOT EXISTS beleg_text MEDIUMTEXT NULL + COMMENT 'Per pdftotext oder OCR extrahierter Volltext des Belegs (für Suche)'; diff --git a/src/Models/Beleg.php b/src/Models/Beleg.php index 6de166f..32a5f6f 100644 --- a/src/Models/Beleg.php +++ b/src/Models/Beleg.php @@ -218,6 +218,14 @@ class Beleg ); } + public static function saveBelegText(int $id, string $text): void + { + Database::execute( + 'UPDATE belege SET beleg_text = ? WHERE id = ?', + [$text !== '' ? $text : null, $id] + ); + } + public static function verifyIntegrity(int $id): bool { $beleg = self::find($id); diff --git a/src/Services/KontierungsvorschlagService.php b/src/Services/KontierungsvorschlagService.php index fe0288d..944c132 100644 --- a/src/Services/KontierungsvorschlagService.php +++ b/src/Services/KontierungsvorschlagService.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Steuerkram\Services; use Steuerkram\Models\Account; +use Steuerkram\Models\Beleg; use Steuerkram\Models\Setting; use Steuerkram\Models\Supplier; @@ -29,19 +30,33 @@ class KontierungsvorschlagService return (bool)($s['ocr_aktiv'] ?? 1); } - public function generateForBeleg(array $beleg): ?array + public function extractText(array $beleg): string { $absPath = dirname(__DIR__, 2) . '/public/' . $beleg['storage_path']; - if (!file_exists($absPath)) return null; + if (!file_exists($absPath)) return ''; - // Inhalt extrahieren $mime = strtolower($beleg['mime_type'] ?? ''); if ($mime === 'application/pdf') { - $text = $this->extractPdfText($absPath); + return $this->extractPdfText($absPath); } elseif (in_array($mime, ['image/jpeg', 'image/png', 'image/tiff'], true)) { - $text = $this->isOcrEnabled() ? $this->extractImageText($absPath) : ''; - } else { + return $this->isOcrEnabled() ? $this->extractImageText($absPath) : ''; + } + + return ''; + } + + public function generateForBeleg(array $beleg): ?array + { + $text = $this->extractText($beleg); + + // Extrahierten Text dauerhaft am Beleg speichern (für spätere Suche) + if ($text !== '' && $beleg['beleg_text'] === null) { + Beleg::saveBelegText((int)$beleg['id'], $text); + } + + $mime = strtolower($beleg['mime_type'] ?? ''); + if (!in_array($mime, ['application/pdf', 'image/jpeg', 'image/png', 'image/tiff'], true)) { return null; }