44 lines
2.0 KiB
PHP
44 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Tenant;
|
|
use App\Services\SupplierPayables;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Tempo\Finance\Payables\Workspace;
|
|
|
|
final class SupplierPayablesController extends Controller
|
|
{
|
|
public function index(Request $r, SupplierPayables $s): JsonResponse
|
|
{
|
|
$filter = $r->validate(['invoice' => 'sometimes|required|integer|min:1']);
|
|
|
|
return response()->json($s->listing($r->user(), isset($filter['invoice']) ? (int) $filter['invoice'] : null))->header('Cache-Control', 'no-store');
|
|
}
|
|
|
|
public function page(Request $r, SupplierPayables $s): Response
|
|
{
|
|
$filter = $r->validate(['invoice' => 'sometimes|required|integer|min:1']);
|
|
$data = $s->listing($r->user(), isset($filter['invoice']) ? (int) $filter['invoice'] : null);
|
|
$company = Tenant::query()->findOrFail($r->user()->tenant_id)->name;
|
|
|
|
return response(Workspace::render($data, ['host' => 'Compras autónomo', 'actor' => $r->user()->name, 'organisation' => $company, 'api' => '/api/supplier-payables', 'csrf' => csrf_token(), 'return_url' => '/facturas', 'return_label' => 'Voltar às facturas']))->header('Cache-Control', 'no-store');
|
|
}
|
|
|
|
public function command(Request $r, string $action, SupplierPayables $s): JsonResponse
|
|
{
|
|
abort_if(str_ends_with($action, '_history') && ! SupplierPayables::historyEnabled(), 403, 'Passagem de histórico não activada nesta instalação.');
|
|
$d = $r->validate(['id' => 'required|integer|min:1', 'reason' => 'required|string|min:3|max:2000']);
|
|
abort_if(array_diff(array_keys($r->all()), ['id', 'reason']) !== [], 422);
|
|
try {
|
|
return response()->json($s->engine($r->user())->command($action, $d['id'], $d['reason'], (string) $r->header('Idempotency-Key')));
|
|
} catch (\DomainException $e) {
|
|
abort(409, $e->getMessage());
|
|
} catch (\InvalidArgumentException $e) {
|
|
abort(422, $e->getMessage());
|
|
}
|
|
}
|
|
}
|