71 lines
2.2 KiB
PHP
71 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Invoices;
|
|
|
|
use App\Core\{Request, Response};
|
|
use App\Services\FileStorageService;
|
|
use App\Modules\Invoices\InvoiceModel;
|
|
use Throwable;
|
|
|
|
final class InvoiceController
|
|
{
|
|
public function __construct(
|
|
private readonly InvoiceModel $invoiceModel,
|
|
private readonly FileStorageService $storage
|
|
) {}
|
|
|
|
public function list(Request $request): void
|
|
{
|
|
$invoices = $this->invoiceModel->findByTenant($request->tenantId);
|
|
Response::json([
|
|
'success' => true,
|
|
'data' => $invoices
|
|
]);
|
|
}
|
|
|
|
public function upload(Request $request): void
|
|
{
|
|
$files = $request->getFiles();
|
|
if (empty($files['invoice'])) {
|
|
Response::error('يرجى اختيار ملف الفاتورة', 'MISSING_FILE', 422);
|
|
return;
|
|
}
|
|
|
|
$companyId = $request->input('company_id');
|
|
if (!$companyId) {
|
|
Response::error('يرجى تحديد الشركة', 'MISSING_COMPANY', 422);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$tenantId = $request->tenantId;
|
|
$filePath = $this->storage->store($files['invoice'], $tenantId, $companyId);
|
|
$fileHash = $this->storage->getHash($filePath);
|
|
|
|
// Create invoice record
|
|
$invoiceId = $this->invoiceModel->create([
|
|
'tenant_id' => $tenantId,
|
|
'company_id' => $companyId,
|
|
'uploaded_by' => $request->user->user_id,
|
|
'status' => 'uploaded',
|
|
'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]);
|
|
|
|
Response::json([
|
|
'success' => true,
|
|
'data' => ['invoice_id' => $invoiceId],
|
|
'message' => 'تم رفع الفاتورة بنجاح وبدء المعالجة'
|
|
]);
|
|
} catch (Throwable $e) {
|
|
Response::error($e->getMessage(), 'UPLOAD_FAILED', 500);
|
|
}
|
|
}
|
|
}
|