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

This commit is contained in:
Hamza-Ayed
2026-05-03 02:38:54 +03:00
parent 2579998cc7
commit ce9f14c7a3
7 changed files with 384 additions and 42 deletions

View File

@@ -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);
}

View File

@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace App\Modules\Users;
use App\Core\{Request, Response, Database};
use Throwable;
final class UsersController
{
public function __construct(private readonly UserModel $userModel) {}
public function list(Request $request): void
{
try {
$tenantId = $request->tenantId;
$db = Database::getInstance();
$stmt = $db->prepare("SELECT id, name, email, role, is_active, created_at FROM users WHERE tenant_id = ? AND deleted_at IS NULL ORDER BY created_at DESC");
$stmt->execute([$tenantId]);
$users = $stmt->fetchAll();
Response::json([
'success' => true,
'data' => $users
]);
} catch (Throwable $e) {
Response::error('Failed to load users: ' . $e->getMessage(), 'USERS_FETCH_ERROR', 500);
}
}
public function create(Request $request): void
{
$name = $request->input('name');
$email = $request->input('email');
$password = $request->input('password');
$role = $request->input('role', 'accountant');
if (!$name || !$email || !$password) {
Response::error('Name, email, and password are required', 'VALIDATION_ERROR', 422);
return;
}
try {
// Check if email exists
if ($this->userModel->findByEmail($email)) {
Response::error('Email already in use', 'EMAIL_EXISTS', 409);
return;
}
$userId = \Ramsey\Uuid\Uuid::uuid4()->toString();
$this->userModel->create([
'id' => $userId,
'tenant_id' => $request->tenantId,
'name' => $name,
'email' => $email,
'password_hash' => password_hash($password, PASSWORD_BCRYPT),
'role' => $role,
'is_active' => 1
]);
Response::json([
'success' => true,
'message' => 'User created successfully',
'data' => ['id' => $userId]
]);
} catch (Throwable $e) {
Response::error($e->getMessage(), 'USER_CREATE_ERROR', 500);
}
}
}

View File

@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
namespace App\Services;
use Exception;
final class AiExtractionService
{
private string $apiKey;
private string $model;
public function __construct()
{
$this->apiKey = $_ENV['GEMINI_API_KEY'] ?? '';
$this->model = $_ENV['GEMINI_MODEL'] ?? 'gemini-2.0-flash';
}
public function extractInvoiceData(string $filePath, string $mimeType): array
{
if (empty($this->apiKey)) {
throw new Exception("Gemini API Key is missing. Please configure it in .env");
}
$fileContent = file_get_contents($filePath);
if ($fileContent === false) {
throw new Exception("Could not read uploaded invoice file.");
}
$base64Data = base64_encode($fileContent);
$prompt = "Please extract the following information from this invoice and return it strictly as JSON without markdown blocks or backticks:\n"
. "- invoice_number\n"
. "- invoice_date (YYYY-MM-DD)\n"
. "- total_amount\n"
. "- tax_amount\n"
. "- vendor_name\n"
. "- vendor_tax_number";
$payload = [
'contents' => [
[
'parts' => [
['text' => $prompt],
[
'inline_data' => [
'mime_type' => $mimeType,
'data' => $base64Data
]
]
]
]
],
'generationConfig' => [
'temperature' => 0.1,
'response_mime_type' => 'application/json'
]
];
$url = "https://generativelanguage.googleapis.com/v1beta/models/{$this->model}:generateContent?key={$this->apiKey}";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
throw new Exception("AI Extraction failed. HTTP Code: {$httpCode}. Response: {$response}");
}
$result = json_decode($response, true);
$text = $result['candidates'][0]['content']['parts'][0]['text'] ?? '{}';
$data = json_decode($text, true);
if (!is_array($data)) {
throw new Exception("Failed to parse AI output as JSON: {$text}");
}
return $data;
}
}

View File

@@ -22,9 +22,9 @@ final class FileStorageService
$mime = finfo_file($finfo, $file['tmp_name']);
finfo_close($finfo);
$allowedMimes = ['application/pdf', 'image/jpeg', 'image/png', 'image/webp'];
$allowedMimes = ['application/pdf', 'image/jpeg', 'image/png', 'image/webp', 'application/json', 'text/plain', 'text/xml', 'application/xml'];
if (!in_array($mime, $allowedMimes)) {
throw new Exception("نوع الملف غير مسموح به");
throw new Exception("نوع الملف غير مسموح به ({$mime})");
}
// 2. Generate path
@@ -37,8 +37,15 @@ final class FileStorageService
$filename = hash('sha256', $file['name'] . time() . uniqid()) . '.' . $extension;
$targetPath = "{$dir}/{$filename}";
if (isset($file['error']) && $file['error'] !== UPLOAD_ERR_OK) {
throw new Exception("حدث خطأ أثناء رفع الملف من المتصفح. كود الخطأ: " . $file['error']);
}
if (!move_uploaded_file($file['tmp_name'], $targetPath)) {
throw new Exception("فشل رفع الملف");
// Fallback for some non-standard PHP environments
if (!copy($file['tmp_name'], $targetPath)) {
throw new Exception("فشل نقل الملف إلى المسار النهائي: " . $targetPath);
}
}
return $targetPath;