From 14ccd5accf2814e831a3515fd682715a717b9bdc Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 2 Mar 2026 09:22:40 +0100 Subject: [PATCH] fix: llm_response_json already parsed object i split-view modal v2.2.27 --- app/billing/frontend/supplier_invoices.html | 50 +++++++++++++++++---- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/app/billing/frontend/supplier_invoices.html b/app/billing/frontend/supplier_invoices.html index a89fcd2..96aa56d 100644 --- a/app/billing/frontend/supplier_invoices.html +++ b/app/billing/frontend/supplier_invoices.html @@ -2120,17 +2120,36 @@ async function openQuickVendorCreate(fileId, filename) { if (resp.ok) { const data = await resp.json(); const ext = data.extraction || {}; + + // llm_response_json kan være allerede-parsed objekt (FastAPI/JSONB) eller string let ai = {}; - try { ai = JSON.parse(ext.llm_response_json || '{}'); } catch(e) {} + const rawLlm = ext.llm_response_json; + if (rawLlm) { + if (typeof rawLlm === 'string') { + try { ai = JSON.parse(rawLlm); } catch(e) { console.warn('llm_response_json parse error:', e); } + } else if (typeof rawLlm === 'object') { + ai = rawLlm; // allerede parsed af browser + } + } - const name = ext.vendor_name || ai.vendor_name || ''; - const cvr = (ext.vendor_cvr || ai.vendor_cvr || '').replace(/^DK/i, '').trim(); - const addr = ai.vendor_address || ''; + // Vendor navn: prøv extraction tabel først, derefter LLM JSON + const name = ext.vendor_name || ai.vendor_name || ai.supplier_name || ''; + // Vendor CVR: strip "DK" prefix + const cvr = (ext.vendor_cvr || ai.vendor_cvr || ai.supplier_cvr || '').toString().replace(/^DK/i, '').trim(); + // Vendor email + const email = ai.vendor_email || ai.supplier_email || ''; + // Vendor adresse: prøv samlet adresse eller separate felter + const addr = ai.vendor_address || ai.supplier_address || + [ai.vendor_street || ai.supplier_street, + (ai.vendor_postal_code || ai.postal_code || '') + ' ' + (ai.vendor_city || ai.city || '') + ].filter(Boolean).join(', ') || ''; - if (name) document.getElementById('qvName').value = name; - if (cvr) document.getElementById('qvCVR').value = cvr; - if (addr) { - // Try to split address into street / postal / city + if (name) document.getElementById('qvName').value = name; + if (cvr) document.getElementById('qvCVR').value = cvr; + if (email) document.getElementById('qvEmail').value = email; + + if (addr && addr.trim()) { + // Forsøg at splitte adresse i gade / postnr / by const parts = addr.split(/,|\n/).map(s => s.trim()).filter(Boolean); if (parts.length >= 1) document.getElementById('qvAddress').value = parts[0]; if (parts.length >= 2) { @@ -2143,6 +2162,21 @@ async function openQuickVendorCreate(fileId, filename) { document.getElementById('qvCity').value = postalCity; } } + } else { + // Separate felter fra AI hvis ingen samlet adresse + if (ai.vendor_street || ai.supplier_street) + document.getElementById('qvAddress').value = ai.vendor_street || ai.supplier_street || ''; + if (ai.vendor_postal_code || ai.postal_code) + document.getElementById('qvPostal').value = ai.vendor_postal_code || ai.postal_code || ''; + if (ai.vendor_city || ai.city) + document.getElementById('qvCity').value = ai.vendor_city || ai.city || ''; + } + + // Vis status hvis ingen relevante data fundet + if (!name && !cvr) { + const statusEl = document.getElementById('qvStatusAlert'); + statusEl.className = 'alert alert-warning py-2 small'; + statusEl.textContent = 'Ingen AI-data fundet for denne fil endnu. Kør fakturaen igennem igen (↺) og prøv derefter.'; } } } catch(e) {