Files
musadaq-saas/app/Modules/Invoices/Actions/UploadInvoiceAction.php

50 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Invoices\Actions;
use App\Services\FileStorageService;
use App\Modules\Invoices\InvoiceModel;
use App\Services\QueueService;
use Exception;
use Ramsey\Uuid\Uuid;
final class UploadInvoiceAction {
public function __construct(
private readonly FileStorageService $storage,
private readonly InvoiceModel $invoiceModel
) {}
public function execute(array $files, string $companyId, string $tenantId, $user): string {
if (empty($files['invoice'])) {
throw new Exception('يرجى اختيار ملف الفاتورة', 422);
}
if (!$companyId) {
throw new Exception('يرجى تحديد الشركة', 422);
}
$filePath = $this->storage->store($files['invoice'], $tenantId, $companyId);
$fileHash = $this->storage->getHash($filePath);
$invoiceId = Uuid::uuid4()->toString();
$this->invoiceModel->create([
'id' => $invoiceId,
'tenant_id' => $tenantId,
'company_id' => $companyId,
'uploaded_by' => $user->user_id ?? null,
'status' => 'uploaded',
'original_file_path' => $filePath,
'original_file_hash' => $fileHash,
'idempotency_key' => bin2hex(random_bytes(16))
]);
QueueService::push('invoice_extraction', [
'invoice_id' => $invoiceId,
'file_path' => $filePath,
'mime_type' => mime_content_type($filePath)
]);
return $invoiceId;
}
}