🚀 مُصادَق: تحديث برمجي جديد 2026-05-03 15:51

This commit is contained in:
Hamza-Ayed
2026-05-03 15:51:53 +03:00
parent e182faad1d
commit 81a3e5188e
12 changed files with 415 additions and 6060 deletions

View File

@@ -63,24 +63,38 @@ final class InvoiceController
public function upload(Request $request): void
{
$db = Database::getInstance();
try {
$files = $request->getFiles();
if (empty($files['file'])) {
throw new \Exception('يرجى اختيار ملف للفاتورة');
throw new \App\Core\Exceptions\HttpException('يرجى اختيار ملف للفاتورة', 'VALIDATION_ERROR', 422);
}
$file = $files['file'];
if ($file['size'] > 20 * 1024 * 1024) { // 20 MB limit
throw new \App\Core\Exceptions\HttpException('حجم الملف يتجاوز الحد المسموح به (20 ميجابايت)', 'VALIDATION_ERROR', 422);
}
$companyId = (string)$request->input('company_id');
if (empty($companyId)) {
throw new \Exception('يرجى اختيار الشركة');
throw new \App\Core\Exceptions\HttpException('يرجى اختيار الشركة', 'VALIDATION_ERROR', 422);
}
// Verify company belongs to tenant
$stmt = $db->prepare("SELECT id FROM companies WHERE id = ? AND tenant_id = ?");
$stmt->execute([$companyId, $request->tenantId]);
if (!$stmt->fetchColumn()) {
throw new \App\Core\Exceptions\HttpException('الشركة غير موجودة أو لا تملك صلاحية الوصول', 'FORBIDDEN', 403);
}
$file = $files['file'];
$invoiceId = \Ramsey\Uuid\Uuid::uuid4()->toString();
// Store file
$path = $this->storage->store($file, $request->tenantId, $companyId);
// Create record
// Transaction for consistency
$db->beginTransaction();
$this->invoiceModel->create([
'id' => $invoiceId,
'tenant_id' => $request->tenantId,
@@ -89,16 +103,22 @@ final class InvoiceController
'status' => 'uploaded'
]);
// Queue extraction and risk analysis
\App\Services\QueueService::push(\queue\Jobs\ExtractInvoiceJob::class, ['invoice_id' => $invoiceId]);
$db->commit();
Response::json([
'success' => true,
'data' => ['invoice_id' => $invoiceId],
'message' => 'تم رفع الفاتورة بنجاح وجاري استخراج البيانات بالذكاء الاصطناعي'
], 202);
} catch (\App\Core\Exceptions\HttpException $e) {
throw $e; // Let global handler catch it
} catch (Throwable $e) {
Response::error($e->getMessage(), 'UPLOAD_ERROR', (int)($e->getCode() ?: 500));
if ($db->inTransaction()) {
$db->rollBack();
}
throw $e;
}
}
@@ -211,7 +231,13 @@ final class InvoiceController
public function update(Request $request, string $id): void
{
// Implementation for PUT /api/v1/invoices/{id}
$db = Database::getInstance();
$stmt = $db->prepare("SELECT id FROM invoices WHERE id = ? AND tenant_id = ?");
$stmt->execute([$id, $request->tenantId]);
if (!$stmt->fetchColumn()) {
throw new \App\Core\Exceptions\HttpException('الفاتورة غير موجودة', 'NOT_FOUND', 404);
}
$data = $request->getBody();
$this->invoiceModel->update($id, $data);
Response::json(['success' => true, 'message' => 'تم تحديث الفاتورة بنجاح']);
@@ -219,6 +245,13 @@ final class InvoiceController
public function destroy(Request $request, string $id): void
{
$db = Database::getInstance();
$stmt = $db->prepare("SELECT id FROM invoices WHERE id = ? AND tenant_id = ?");
$stmt->execute([$id, $request->tenantId]);
if (!$stmt->fetchColumn()) {
throw new \App\Core\Exceptions\HttpException('الفاتورة غير موجودة', 'NOT_FOUND', 404);
}
$this->invoiceModel->delete($id);
Response::json(['success' => true, 'message' => 'تم حذف الفاتورة بنجاح']);
}