🚀 مُصادَق: تحديث برمجي جديد 2026-05-03 13:19
This commit is contained in:
48
queue/Jobs/ExtractInvoiceJob.php
Normal file
48
queue/Jobs/ExtractInvoiceJob.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,16 +26,24 @@ while ($keepRunning) {
|
||||
if ($job) {
|
||||
echo "[+] Processing job: {$job['type']} ({$job['id']})\n";
|
||||
try {
|
||||
// Process based on type
|
||||
// match($job['type']) { ... }
|
||||
$container = $app->getContainer();
|
||||
|
||||
switch($job['type']) {
|
||||
case 'invoice_extraction':
|
||||
$handler = $container->get(\Queue\Jobs\ExtractInvoiceJob::class);
|
||||
$handler->handle($job['payload']);
|
||||
break;
|
||||
|
||||
default:
|
||||
echo "[!] Unknown job type: {$job['type']}\n";
|
||||
}
|
||||
|
||||
echo "[✓] Job completed: {$job['id']}\n";
|
||||
} catch (\Throwable $e) {
|
||||
echo "[✗] Job failed: {$job['id']} - {$e->getMessage()}\n";
|
||||
// Handle retries or DLQ
|
||||
// In a real app, you'd handle retries or move to a failed_jobs table
|
||||
}
|
||||
} else {
|
||||
// Sleep if no jobs
|
||||
usleep(500000); // 0.5s
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user