invoiceModel->update($invoiceId, ['status' => 'extracting']); try { $extractedData = $this->aiExtraction->extractInvoiceData($filePath, $mimeType); // Map AI data to schema columns if needed, or just store in ai_raw_response $this->invoiceModel->update($invoiceId, [ 'status' => 'extracted', 'invoice_number' => $extractedData['invoice_number'] ?? null, 'invoice_date' => $extractedData['invoice_date'] ?? null, 'grand_total' => $extractedData['total_amount'] ?? 0, 'tax_amount' => $extractedData['tax_amount'] ?? 0, 'supplier_name' => $extractedData['vendor_name'] ?? null, 'supplier_tin' => $extractedData['vendor_tax_number'] ?? null, 'ai_raw_response' => json_encode($extractedData, JSON_UNESCAPED_UNICODE) ]); } catch (Throwable $e) { $this->invoiceModel->update($invoiceId, [ 'status' => 'validation_failed' ]); throw $e; } } }