From ec3cd2b659c0279340891e6b7f3e16df13010d20 Mon Sep 17 00:00:00 2001 From: Andreas Knuth Date: Tue, 24 Feb 2026 16:46:54 -0600 Subject: [PATCH] fix --- server.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/server.js b/server.js index 770a28b..3dd3f70 100644 --- a/server.js +++ b/server.js @@ -292,6 +292,7 @@ async function syncInvoiceToQbo(invoiceId, client) { } + // Logo endpoints app.get('/api/logo-info', async (req, res) => { try { @@ -958,18 +959,27 @@ app.put('/api/invoices/:id', async (req, res) => { await client.query('COMMIT'); - // Auto QBO Sync (falls bereits in QBO) + // Auto QBO: Export if not yet in QBO, Sync if already in QBO let qboResult = null; try { - qboResult = await syncInvoiceToQbo(id, client); + // Prüfe ob Invoice schon in QBO + const checkRes = await client.query('SELECT qbo_id FROM invoices WHERE id = $1', [id]); + const hasQboId = !!checkRes.rows[0]?.qbo_id; + + if (hasQboId) { + qboResult = await syncInvoiceToQbo(id, client); + } else { + qboResult = await exportInvoiceToQbo(id, client); + } + if (qboResult.skipped) { - console.log(`ℹ️ Invoice ${id} not synced to QBO: ${qboResult.reason}`); + console.log(`ℹ️ Invoice ${id}: ${qboResult.reason}`); } } catch (qboErr) { - console.error(`⚠️ Auto QBO sync failed for Invoice ${id}:`, qboErr.message); + console.error(`⚠️ Auto QBO failed for Invoice ${id}:`, qboErr.message); } - res.json({ success: true, qbo_synced: !!qboResult?.success }); + res.json({ success: true, qbo_synced: !!qboResult?.success, qbo_id: qboResult?.qbo_id || null, qbo_doc_number: qboResult?.qbo_doc_number || null }); } catch (error) { await client.query('ROLLBACK');