🚀 مُصادَق: تحديث برمجي جديد 2026-05-03 02:38
This commit is contained in:
@@ -6,6 +6,7 @@ namespace App\Modules\Invoices;
|
||||
|
||||
use App\Core\{Request, Response};
|
||||
use App\Services\FileStorageService;
|
||||
use App\Services\AiExtractionService;
|
||||
use App\Modules\Invoices\InvoiceModel;
|
||||
use Throwable;
|
||||
|
||||
@@ -13,7 +14,8 @@ final class InvoiceController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly InvoiceModel $invoiceModel,
|
||||
private readonly FileStorageService $storage
|
||||
private readonly FileStorageService $storage,
|
||||
private readonly AiExtractionService $aiExtraction
|
||||
) {}
|
||||
|
||||
public function list(Request $request): void
|
||||
@@ -45,24 +47,52 @@ final class InvoiceController
|
||||
$fileHash = $this->storage->getHash($filePath);
|
||||
|
||||
// Create invoice record
|
||||
$invoiceId = $this->invoiceModel->create([
|
||||
$invoiceId = \Ramsey\Uuid\Uuid::uuid4()->toString();
|
||||
$this->invoiceModel->create([
|
||||
'id' => $invoiceId,
|
||||
'invoice_uuid' => \Ramsey\Uuid\Uuid::uuid4()->toString(),
|
||||
'tenant_id' => $tenantId,
|
||||
'company_id' => $companyId,
|
||||
'uploaded_by' => $request->user->user_id,
|
||||
'status' => 'uploaded',
|
||||
'status' => 'PROCESSING',
|
||||
'original_file_path' => $filePath,
|
||||
'original_file_hash' => $fileHash,
|
||||
'idempotency_key' => bin2hex(random_bytes(16))
|
||||
]);
|
||||
|
||||
// TODO: Push to queue for AI extraction
|
||||
// QueueService::push('extract_invoice', ['invoice_id' => $invoiceId]);
|
||||
// Attempt AI Extraction
|
||||
try {
|
||||
$mimeType = mime_content_type($filePath);
|
||||
$extractedData = $this->aiExtraction->extractInvoiceData($filePath, $mimeType);
|
||||
|
||||
// Update Invoice with extracted data
|
||||
$this->invoiceModel->update($invoiceId, [
|
||||
'status' => 'EXTRACTED',
|
||||
'extracted_data' => json_encode($extractedData, JSON_UNESCAPED_UNICODE)
|
||||
]);
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'invoice_id' => $invoiceId,
|
||||
'extracted_data' => $extractedData
|
||||
],
|
||||
'message' => 'تم رفع الفاتورة واستخراج البيانات بنجاح بالذكاء الاصطناعي'
|
||||
]);
|
||||
|
||||
} catch (Throwable $aiError) {
|
||||
// Keep it uploaded, maybe manual retry later
|
||||
$this->invoiceModel->update($invoiceId, [
|
||||
'status' => 'AI_FAILED'
|
||||
]);
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => ['invoice_id' => $invoiceId],
|
||||
'message' => 'تم الرفع ولكن فشل استخراج البيانات. ' . $aiError->getMessage()
|
||||
]);
|
||||
}
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => ['invoice_id' => $invoiceId],
|
||||
'message' => 'تم رفع الفاتورة بنجاح وبدء المعالجة'
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
Response::error($e->getMessage(), 'UPLOAD_FAILED', 500);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user