79 lines
3.4 KiB
PHP
79 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\User;
|
|
use App\Services\GeneralLedger;
|
|
use App\Services\StandaloneRuntime;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Tempo\Finance\GeneralLedger\CycleWorkspace;
|
|
use Tempo\Finance\GeneralLedger\ReportsWorkspace;
|
|
|
|
final class GeneralLedgerController extends Controller
|
|
{
|
|
/** @return array<string,mixed> */
|
|
private function presentation(): array
|
|
{
|
|
return app(StandaloneRuntime::class)->enabled() ? ['host_journal_policy' => true,
|
|
'synthetic_only' => app(StandaloneRuntime::class)->syntheticOnly(),
|
|
'reports_from' => today()->startOfYear()->toDateString(), 'reports_to' => today()->toDateString()] : [];
|
|
}
|
|
|
|
public function report(Request $r, string $kind, GeneralLedger $ledger): Response|JsonResponse
|
|
{
|
|
$d = $r->validate(['from_on' => 'required|date_format:Y-m-d', 'to_on' => 'required|date_format:Y-m-d|after_or_equal:from_on', 'account_id' => 'sometimes|required|integer|min:1', 'page' => 'sometimes|integer|min:1|max:100000', 'per_page' => 'sometimes|integer|in:25,100']);
|
|
abort_if(array_diff(array_keys($r->query()), array_keys($d)) !== [], 422);
|
|
try {
|
|
$data = $ledger->report($this->actor($r), $kind, $d);
|
|
} catch (\InvalidArgumentException $e) {
|
|
abort(422, $e->getMessage());
|
|
} catch (\DomainException $e) {
|
|
abort(403, $e->getMessage());
|
|
}
|
|
if ($r->is('api/*')) {
|
|
return response()->json($data)->header('Cache-Control', 'no-store');
|
|
}
|
|
|
|
return response(ReportsWorkspace::render($data, ['host' => 'SGC autónomo', 'return_url' => '/general-ledger/cycle'] + $this->presentation()))->header('Cache-Control', 'no-store');
|
|
}
|
|
|
|
private function actor(Request $r): User
|
|
{
|
|
$user = $r->user();
|
|
abort_unless($user instanceof User, 403);
|
|
|
|
return $user;
|
|
}
|
|
|
|
public function page(Request $r, GeneralLedger $ledger): Response
|
|
{
|
|
$d = $r->validate(['page' => 'sometimes|integer|min:1|max:100000']);
|
|
abort_if(array_diff(array_keys($r->query()), ['page']) !== [], 422);
|
|
$data = $ledger->workspace($this->actor($r), (int) ($d['page'] ?? 1));
|
|
|
|
return response(CycleWorkspace::render($data, ['host' => 'SGC autónomo', 'csrf' => csrf_token(), 'financial_url' => '/supplier-payments', 'return_url' => '/obrigacoes-fornecedores', 'page_url' => '/general-ledger/cycle'] + $this->presentation()))->header('Cache-Control', 'no-store');
|
|
}
|
|
|
|
public function command(Request $r, string $action, GeneralLedger $ledger): JsonResponse
|
|
{
|
|
abort_unless($ledger->cycleEnabled(), 403);
|
|
try {
|
|
return response()->json($ledger->engine($this->actor($r))->command($action, $r->all(), (string) $r->header('Idempotency-Key')))->header('Cache-Control', 'no-store');
|
|
} catch (\InvalidArgumentException $e) {
|
|
abort(422, $e->getMessage());
|
|
} catch (\DomainException $e) {
|
|
abort(409, $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function index(Request $r, GeneralLedger $ledger): JsonResponse
|
|
{
|
|
$d = $r->validate(['page' => 'sometimes|integer|min:1|max:100000']);
|
|
abort_if(array_diff(array_keys($r->query()), ['page']) !== [], 422);
|
|
|
|
return response()->json($ledger->workspace($this->actor($r), (int) ($d['page'] ?? 1)))->header('Cache-Control', 'no-store');
|
|
}
|
|
}
|