From 4953c82b9327e70e63b3ff670b92330c78e0681a Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 2 Mar 2026 13:12:01 +0100 Subject: [PATCH] fix: link-vendor opretter minimal extraction hvis ingen findes v2.2.32 --- app/billing/backend/supplier_invoices.py | 35 ++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/app/billing/backend/supplier_invoices.py b/app/billing/backend/supplier_invoices.py index d8157b5..d595d1d 100644 --- a/app/billing/backend/supplier_invoices.py +++ b/app/billing/backend/supplier_invoices.py @@ -757,17 +757,36 @@ async def link_vendor_to_extraction(file_id: int, data: dict): (file_id,)) if not extraction: - raise HTTPException(status_code=404, detail="Ingen extraction fundet for denne fil") - - # Update extraction with vendor match + # No extraction exists (e.g. custom template match or not yet processed) + # Create a minimal placeholder extraction so vendor can be linked + logger.info(f"⚠️ No extraction for file {file_id} — creating minimal extraction for vendor link") + extraction_id = execute_insert( + """INSERT INTO extractions + (file_id, vendor_matched_id, vendor_name, vendor_cvr, + document_id, document_type, document_type_detected, + currency, confidence, status) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) + RETURNING extraction_id""", + (file_id, vendor_id, + vendor['name'], None, + None, 'invoice', 'invoice', + 'DKK', 1.0, 'manual') + ) + else: + extraction_id = extraction['extraction_id'] + # Update extraction with vendor match + execute_update( + "UPDATE extractions SET vendor_matched_id = %s WHERE extraction_id = %s", + (vendor_id, extraction_id) + ) + + # Also update incoming_files so table shows vendor immediately execute_update( - """UPDATE extractions - SET vendor_matched_id = %s - WHERE extraction_id = %s""", - (vendor_id, extraction['extraction_id']) + "UPDATE incoming_files SET detected_vendor_id = %s, status = 'processed' WHERE file_id = %s", + (vendor_id, file_id) ) - logger.info(f"✅ Linked vendor {vendor['name']} (ID: {vendor_id}) to extraction for file {file_id}") + logger.info(f"✅ Linked vendor {vendor['name']} (ID: {vendor_id}) to file {file_id}") return { "status": "success",