Files
compras/app/Http/Controllers/SupplierBankController.php
T
nala 12dd6df28a
tests / ci (push) Waiting to run
commit melhorias compras1
2026-11-21 21:04:40 +02:00

72 lines
4.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Enums\PurchasingRole;
use App\Models\Supplier;
use App\Models\SupplierBankDetail;
use App\Models\SupplierBankReview;
use App\Services\RequisitionWorkflow;
use App\Services\SupplierBankWorkflow;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Inertia\Inertia;
use Symfony\Component\HttpFoundation\Response;
class SupplierBankController extends Controller
{
public function show(Request $request, Supplier $supplier, SupplierBankWorkflow $workflow): Response
{
abort_unless($this->staffIdentity($request)->hasFunctions([PurchasingRole::Admin, PurchasingRole::Procurement, PurchasingRole::Finance]), 403);
$filters = $request->validate(['account' => 'nullable|uuid']);
$key = isset($filters['account']) ? strtolower($filters['account']) : null;
$details = $workflow->account($supplier, $key)->latest('revision')->get();
$current = $details->first();
abort_unless($key === null || $current !== null, 404);
$canRead = $this->staffIdentity($request)->hasFunctions([PurchasingRole::Procurement, PurchasingRole::Finance]);
$data = null;
if ($current && $canRead) {
app(RequisitionWorkflow::class)->audit($this->staffIdentity($request), 'supplier.bank_viewed', null, null, ['supplier_id' => $supplier->id, 'bank_detail_id' => $current->id]);
$data = [...$current->toArray(), ...$current->only(['account_identifier', 'beneficiary', 'evidence_reference', 'reason'])];
}
Inertia::encryptHistory();
$response = Inertia::render('suppliers/bank', ['supplier' => $supplier->only(['id', 'name']), 'current' => $data, 'currentId' => $current->id ?? 0,
'accountKey' => $key, 'actorId' => $this->staffIdentity($request)->id, 'multiAccountsAvailable' => $workflow->multipleReady(),
'accounts' => $workflow->currentAccounts($supplier)->map(fn (SupplierBankDetail $detail) => [
'id' => $detail->id, 'key' => $detail->account_key, 'revision' => $detail->revision,
'label' => $canRead ? $detail->bank_name.' · '.$detail->currency.' · v'.$detail->revision : 'Conta #'.$detail->id.' · v'.$detail->revision,
'state' => $workflow->state($supplier, $detail->account_key),
]),
'state' => $workflow->state($supplier, $key), 'history' => $details->map(fn ($detail) => $detail->only(['id', 'revision', 'created_by', 'created_at'])),
'reviews' => SupplierBankReview::query()->whereIn('supplier_bank_detail_id', $details->pluck('id'))->orderBy('id')->get(),
'canPropose' => $this->staffIdentity($request)->hasFunctions([PurchasingRole::Procurement]),
'canReview' => $current !== null && $workflow->canReview($this->staffIdentity($request), $current)])->toResponse($request);
$response->headers->set('Cache-Control', 'private, no-store, max-age=0');
return $response;
}
public function store(Request $request, Supplier $supplier, SupplierBankWorkflow $workflow): RedirectResponse
{
$request->merge(['country' => Str::upper(trim($request->string('country')->toString())), 'currency' => Str::upper(trim($request->string('currency')->toString())), 'account_identifier' => Str::upper(preg_replace('/\s+/', '', $request->string('account_identifier')->toString()) ?? '')]);
$data = $request->validate(['previous_id' => 'required|integer|min:0', 'account_key' => 'nullable|uuid', 'bank_name' => 'required|string|max:200', 'country' => ['required', 'regex:/^[A-Z]{2}$/'], 'currency' => ['required', 'regex:/^[A-Z]{3}$/'], 'beneficiary' => 'required|string|max:200', 'account_identifier' => ['required', 'regex:/^[A-Z0-9.\/-]{4,100}$/'], 'evidence_reference' => 'required|string|max:1000', 'reason' => 'required|string|max:2000']);
$detail = $workflow->propose($this->staffIdentity($request), $supplier, $data);
if ($detail->account_key !== null) {
return redirect('/fornecedores/'.$supplier->id.'/dados-bancarios?account='.rawurlencode($detail->account_key));
}
return back();
}
public function review(Request $request, Supplier $supplier, SupplierBankDetail $detail, SupplierBankWorkflow $workflow): RedirectResponse
{
abort_unless($detail->supplier_id === $supplier->id, 404);
$data = $request->validate(['decision' => 'required|in:approved,rejected', 'reason' => 'required|string|max:2000', 'independently_verified' => 'accepted']);
$workflow->review($this->staffIdentity($request), $detail, $data['decision'], $data['reason']);
return back();
}
}