96 lines
3.2 KiB
PHP
96 lines
3.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 App\Modules\Invoices\Actions\{
|
|
ListInvoicesAction,
|
|
UploadInvoiceAction,
|
|
GetInvoiceDetailAction,
|
|
SubmitInvoiceAction,
|
|
DownloadInvoiceFileAction
|
|
};
|
|
use Throwable;
|
|
|
|
final class InvoiceController
|
|
{
|
|
public function __construct(
|
|
private readonly InvoiceModel $invoiceModel,
|
|
private readonly FileStorageService $storage
|
|
) {}
|
|
|
|
public function list(Request $request): void
|
|
{
|
|
try {
|
|
$action = new ListInvoicesAction();
|
|
$invoices = $action->execute($request->tenantId, $request->user);
|
|
Response::json(['success' => true, 'data' => $invoices]);
|
|
} catch (Throwable $e) {
|
|
Response::error($e->getMessage(), 'LIST_ERROR', (int)($e->getCode() ?: 500));
|
|
}
|
|
}
|
|
|
|
public function upload(Request $request): void
|
|
{
|
|
try {
|
|
$action = new UploadInvoiceAction($this->storage, $this->invoiceModel);
|
|
$invoiceId = $action->execute(
|
|
$request->getFiles(),
|
|
(string)$request->input('company_id'),
|
|
$request->tenantId,
|
|
$request->user
|
|
);
|
|
|
|
Response::json([
|
|
'success' => true,
|
|
'data' => ['invoice_id' => $invoiceId],
|
|
'message' => 'تم رفع الفاتورة بنجاح وجاري استخراج البيانات بالذكاء الاصطناعي'
|
|
], 202);
|
|
} catch (Throwable $e) {
|
|
Response::error($e->getMessage(), 'UPLOAD_ERROR', (int)($e->getCode() ?: 500));
|
|
}
|
|
}
|
|
|
|
public function detail(Request $request, string $id): void
|
|
{
|
|
try {
|
|
$action = new GetInvoiceDetailAction();
|
|
$invoice = $action->execute($id, $request->tenantId, $request->user);
|
|
Response::json(['success' => true, 'data' => $invoice]);
|
|
} catch (Throwable $e) {
|
|
Response::error($e->getMessage(), 'DETAIL_ERROR', (int)($e->getCode() ?: 500));
|
|
}
|
|
}
|
|
|
|
public function submit(Request $request, string $id): void
|
|
{
|
|
try {
|
|
$action = new SubmitInvoiceAction();
|
|
$action->execute($id, $request->tenantId);
|
|
Response::json(['success' => true, 'message' => 'Invoice submission queued.']);
|
|
} catch (Throwable $e) {
|
|
Response::error($e->getMessage(), 'SUBMIT_ERROR', (int)($e->getCode() ?: 500));
|
|
}
|
|
}
|
|
|
|
public function downloadFile(Request $request, string $id): void
|
|
{
|
|
try {
|
|
$action = new DownloadInvoiceFileAction();
|
|
$file = $action->execute($id, $request->tenantId, $request->user);
|
|
|
|
header("Content-Type: {$file['mime']}");
|
|
header("Content-Disposition: inline; filename=\"{$file['name']}\"");
|
|
header("Content-Length: " . filesize($file['path']));
|
|
readfile($file['path']);
|
|
exit;
|
|
} catch (Throwable $e) {
|
|
Response::error($e->getMessage(), 'DOWNLOAD_ERROR', (int)($e->getCode() ?: 500));
|
|
}
|
|
}
|
|
}
|