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

32 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Invoices\Actions;
use App\Core\Database;
use Exception;
final class GetInvoiceDetailAction {
public function execute(string $invoiceId, string $tenantId, $user): array {
$db = Database::getInstance();
$stmt = $db->prepare("SELECT * FROM invoices WHERE id = ? AND tenant_id = ? AND deleted_at IS NULL LIMIT 1");
$stmt->execute([$invoiceId, $tenantId]);
$invoice = $stmt->fetch();
if (!$invoice) {
throw new Exception('الفاتورة غير موجودة أو تم حذفها', 404);
}
$role = $user->role ?? 'viewer';
if ($role !== 'super_admin' && $invoice['company_id'] !== ($user->assigned_company_id ?? null)) {
throw new Exception('غير مصرح لك بالوصول لهذه الفاتورة', 403);
}
$stmt = $db->prepare("SELECT * FROM invoice_lines WHERE invoice_id = ? ORDER BY line_number ASC");
$stmt->execute([$invoiceId]);
$invoice['lines'] = $stmt->fetchAll() ?: [];
return $invoice;
}
}