49 lines
1.6 KiB
PHP
49 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Queue\Jobs;
|
|
|
|
use App\Modules\Invoices\InvoiceModel;
|
|
use App\Services\AiExtractionService;
|
|
use Throwable;
|
|
|
|
final class ExtractInvoiceJob
|
|
{
|
|
public function __construct(
|
|
private readonly InvoiceModel $invoiceModel,
|
|
private readonly AiExtractionService $aiExtraction
|
|
) {}
|
|
|
|
public function handle(array $payload): void
|
|
{
|
|
$invoiceId = $payload['invoice_id'];
|
|
$filePath = $payload['file_path'];
|
|
$mimeType = $payload['mime_type'];
|
|
|
|
// Update status to extracting
|
|
$this->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;
|
|
}
|
|
}
|
|
}
|