From e8a9b59a46784a604f917218b2146a129659b9c3 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Tue, 12 May 2026 01:35:12 +0300 Subject: [PATCH] Update: 2026-05-12 01:35:12 --- app/Core/AI.php | 5 +++++ app/Services/InvoiceProcessor.php | 7 +------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Core/AI.php b/app/Core/AI.php index 2ed96d9..63f1794 100644 --- a/app/Core/AI.php +++ b/app/Core/AI.php @@ -106,6 +106,11 @@ class AI return null; } + // If the AI returns an array of invoices, extract the first one + if (isset($data['invoices']) && is_array($data['invoices']) && count($data['invoices']) > 0) { + $data = $data['invoices'][0]; + } + // Track token usage from Gemini response $usage = $result['usageMetadata'] ?? []; if (!empty($usage)) { diff --git a/app/Services/InvoiceProcessor.php b/app/Services/InvoiceProcessor.php index 74c4961..f90fb5e 100644 --- a/app/Services/InvoiceProcessor.php +++ b/app/Services/InvoiceProcessor.php @@ -102,26 +102,21 @@ class InvoiceProcessor supplier_tin, supplier_name, supplier_address, buyer_tin, buyer_name, buyer_national_id, subtotal, tax_amount, discount_total, grand_total, currency_code, - ai_provider, created_at ) VALUES ( ?, ?, ?, ?, ?, 'extracted', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, - ?, NOW() ) "); - $rawJsonSnippet = substr(json_encode($extracted, JSON_UNESCAPED_UNICODE), 0, 500); - $stmt->execute([ $invoiceId, $tenantId, $companyId, $userId, $imagePath, $invoiceNum, $validDate, $extracted['invoice_type'] ?? 'cash', $extracted['invoice_category'] ?? 'simplified', Encryption::encrypt($supplierTin), Encryption::encrypt($extracted['supplier']['name'] ?? ''), Encryption::encrypt($extracted['supplier']['address'] ?? ''), Encryption::encrypt($extracted['buyer']['tin'] ?? ''), Encryption::encrypt($extracted['buyer']['name'] ?? ''), Encryption::encrypt($extracted['buyer']['national_id'] ?? ''), - $extracted['subtotal'] ?? 0, $extracted['tax_amount'] ?? 0, $extracted['discount_total'] ?? 0, $extracted['grand_total'] ?? 0, $extracted['currency_code'] ?? 'JOD', - $rawJsonSnippet + $extracted['subtotal'] ?? 0, $extracted['tax_amount'] ?? 0, $extracted['discount_total'] ?? 0, $extracted['grand_total'] ?? 0, $extracted['currency_code'] ?? 'JOD' ]); // Save invoice line items