feat: reimport pdf

This commit is contained in:
2026-01-22 21:23:46 +01:00
parent 981fabcc27
commit 4f0ca67dc4
13 changed files with 1068 additions and 14 deletions

View File

@@ -9,6 +9,7 @@ from sqlalchemy.orm import Session
from sqlalchemy.exc import IntegrityError
from .models import Document, Immeuble, Lot, Locataire, Revenu, Depense, Tag
from . import storage
class DuplicateDocumentError(Exception):
@@ -98,6 +99,7 @@ class DatabaseService:
self,
data: dict[str, Any],
source_file: str = None,
pdf_content: bytes = None,
depenses_tags: list[dict] = None,
overwrite: bool = False,
) -> Document:
@@ -107,6 +109,7 @@ class DatabaseService:
data: The 'data' portion of the extracted JSON (contains metadata,
situation_locataires, recapitulatif_operations)
source_file: Original PDF filename
pdf_content: Binary content of the PDF file (optional, for storage)
depenses_tags: List of tags for expenses
overwrite: If True, delete existing document and recreate it
@@ -129,11 +132,17 @@ class DatabaseService:
if not reference or not doc_date:
raise ValueError("Document must have reference and date")
# Check for duplicates
# Check for duplicates and preserve existing file paths if overwriting
existing_pdf_path = None
existing_json_path = None
existing = self.check_duplicate(reference, doc_date)
if existing:
if overwrite:
# Preserve existing file paths for reuse
existing_pdf_path = existing.pdf_path
existing_json_path = existing.json_path
# Delete existing document (cascade will delete related data)
# But DON'T delete files - we'll reuse or update them
self.session.delete(existing)
self.session.flush()
else:
@@ -147,6 +156,26 @@ class DatabaseService:
code_postal=immeuble_info.get("code_postal"),
)
# Compute storage paths for PDF and JSON
pdf_path = None
json_path = None
if pdf_content is not None:
# New PDF provided - compute new paths
pdf_path, json_path = storage.compute_document_paths(
reference=reference,
doc_date=doc_date,
immeuble_adresse=immeuble.adresse,
)
# Delete old files if paths are different
if existing_pdf_path and existing_pdf_path != pdf_path:
storage.delete_document_files(existing_pdf_path, None)
if existing_json_path and existing_json_path != json_path:
storage.delete_document_files(None, existing_json_path)
elif existing_json_path:
# No new PDF but we have existing paths - preserve them
pdf_path = existing_pdf_path
json_path = existing_json_path
# Create document
document = Document(
reference=reference,
@@ -160,10 +189,21 @@ class DatabaseService:
solde_montant=solde_info.get("montant"),
solde_type=solde_info.get("type"),
solde_date_arrete=self._parse_date(solde_info.get("date_arrete")),
pdf_path=pdf_path,
json_path=json_path,
)
self.session.add(document)
self.session.flush()
# Save files to storage
if pdf_content is not None and pdf_path and json_path:
# New PDF provided - save both files
storage.save_pdf(pdf_content, pdf_path)
storage.save_json(data, json_path)
elif json_path:
# No new PDF but we have a json_path - update the JSON file
storage.save_json(data, json_path)
# Process situation_locataires (revenus)
for situation in data.get("situation_locataires", []):
self._save_situation_locataire(document, immeuble, situation)