@@ -7,14 +7,54 @@ use App\Models\Supplier;
|
||||
use App\Models\SupplierBankDetail;
|
||||
use App\Models\SupplierBankReview;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class SupplierBankWorkflow
|
||||
{
|
||||
public function multipleReady(): bool
|
||||
{
|
||||
return Schema::hasColumn('supplier_bank_details', 'account_key');
|
||||
}
|
||||
|
||||
/** @return Builder<SupplierBankDetail> */
|
||||
public function account(Supplier $supplier, ?string $key = null): Builder
|
||||
{
|
||||
$query = SupplierBankDetail::query()->where('tenant_id', $supplier->tenant_id)->where('supplier_id', $supplier->id);
|
||||
if ($this->multipleReady()) {
|
||||
$query->where('account_key', $key);
|
||||
} else {
|
||||
abort_unless($key === null, 503, 'A estrutura de contas independentes ainda não está instalada.');
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function latest(Supplier $supplier, ?string $key = null): ?SupplierBankDetail
|
||||
{
|
||||
return $this->account($supplier, $key)->latest('revision')->first();
|
||||
}
|
||||
|
||||
/** @return Collection<int,SupplierBankDetail> */
|
||||
public function currentAccounts(Supplier $supplier): Collection
|
||||
{
|
||||
$query = SupplierBankDetail::query()->where('tenant_id', $supplier->tenant_id)->where('supplier_id', $supplier->id);
|
||||
if (! $this->multipleReady()) {
|
||||
return $query->latest('revision')->limit(1)->get();
|
||||
}
|
||||
|
||||
return $query->whereIn('revision', fn ($q) => $q->from('supplier_bank_details')->where('tenant_id', $supplier->tenant_id)
|
||||
->where('supplier_id', $supplier->id)->groupBy('account_key')->selectRaw('MAX(revision)'))
|
||||
->orderBy('revision')->get();
|
||||
}
|
||||
|
||||
private function actor(User $user, Supplier $supplier): void
|
||||
{
|
||||
abort_unless(auth()->id() === $user->id && $user->tenant_id !== null && $supplier->tenant_id === $user->tenant_id, 403);
|
||||
abort_if(app(OrganizationAccess::class)->maintenanceOnly($user), 403);
|
||||
if (app(ConflictGuard::class)->hasConflict($user->id, $supplier->id, null)) {
|
||||
throw ValidationException::withMessages(['bank' => 'Conflito de interesses com este fornecedor.']);
|
||||
}
|
||||
@@ -31,11 +71,24 @@ class SupplierBankWorkflow
|
||||
$user = app(CurrentActor::class)->lock($user, [PurchasingRole::Procurement]);
|
||||
$supplier = Supplier::query()->lockForUpdate()->findOrFail($supplier->id);
|
||||
$this->actor($user, $supplier);
|
||||
$latest = SupplierBankDetail::query()->where('supplier_id', $supplier->id)->latest('revision')->first();
|
||||
$key = validator($data, ['account_key' => 'nullable|uuid'])->validate()['account_key'] ?? null;
|
||||
$key = $key === null ? null : strtolower($key);
|
||||
$latest = $this->latest($supplier, $key);
|
||||
$fields = ['bank_name', 'country', 'currency', 'beneficiary', 'account_identifier', 'evidence_reference', 'reason'];
|
||||
if ($key !== null && (int) $data['previous_id'] === 0 && $latest !== null) {
|
||||
$original = $this->account($supplier, $key)->oldest('revision')->firstOrFail();
|
||||
if ($original->created_by === $user->id
|
||||
&& array_map(fn ($field) => $original->$field, $fields) === array_map(fn ($field) => $data[$field] ?? null, $fields)) {
|
||||
return $original;
|
||||
}
|
||||
throw ValidationException::withMessages(['account_key' => 'A identidade desta conta já foi utilizada com outros dados.']);
|
||||
}
|
||||
if ((int) $data['previous_id'] !== ($latest->id ?? 0)) {
|
||||
throw ValidationException::withMessages(['previous_id' => 'Dados bancários alterados. Reabra a ficha antes de propor outra versão.']);
|
||||
}
|
||||
$detail = SupplierBankDetail::query()->create(['tenant_id' => $user->tenant_id, 'supplier_id' => $supplier->id, 'created_by' => $user->id, 'revision' => ($latest->revision ?? 0) + 1, ...array_intersect_key($data, array_flip(['bank_name', 'country', 'currency', 'beneficiary', 'account_identifier', 'evidence_reference', 'reason'])), 'created_at' => now()]);
|
||||
$revision = (int) SupplierBankDetail::query()->where('supplier_id', $supplier->id)->max('revision') + 1;
|
||||
$detail = SupplierBankDetail::query()->create(['tenant_id' => $user->tenant_id, 'supplier_id' => $supplier->id, 'created_by' => $user->id, 'revision' => $revision,
|
||||
...($key === null ? [] : ['account_key' => $key]), ...array_intersect_key($data, array_flip($fields)), 'created_at' => now()]);
|
||||
app(RequisitionWorkflow::class)->audit($user, 'supplier.bank_proposed', null, null, ['supplier_id' => $supplier->id, 'bank_detail_id' => $detail->id, 'revision' => $detail->revision]);
|
||||
|
||||
return $detail;
|
||||
@@ -53,8 +106,8 @@ class SupplierBankWorkflow
|
||||
$user = app(CurrentActor::class)->lock($user, [PurchasingRole::Finance]);
|
||||
$supplier = Supplier::query()->lockForUpdate()->findOrFail($detail->supplier_id);
|
||||
$this->actor($user, $supplier);
|
||||
$latest = SupplierBankDetail::query()->where('supplier_id', $supplier->id)->latest('revision')->firstOrFail();
|
||||
if ($latest->id !== $detail->id) {
|
||||
$latest = $this->latest($supplier, $detail->account_key);
|
||||
if ($latest === null || $latest->id !== $detail->id) {
|
||||
throw ValidationException::withMessages(['bank' => 'Versão substituída. Reveja os dados bancários actuais.']);
|
||||
}
|
||||
$existing = SupplierBankReview::query()->where('supplier_bank_detail_id', $detail->id)->where('reviewed_by', $user->id)->first();
|
||||
@@ -74,9 +127,9 @@ class SupplierBankWorkflow
|
||||
}
|
||||
|
||||
/** Re-evaluate inside the payment transaction in the future; UI readiness is not authorisation. */
|
||||
public function state(Supplier $supplier): string
|
||||
public function state(Supplier $supplier, ?string $key = null): string
|
||||
{
|
||||
$detail = SupplierBankDetail::query()->where('supplier_id', $supplier->id)->latest('revision')->first();
|
||||
$detail = $this->latest($supplier, $key);
|
||||
if (! $detail) {
|
||||
return 'missing';
|
||||
}
|
||||
@@ -99,4 +152,18 @@ class SupplierBankWorkflow
|
||||
|
||||
return 'approved';
|
||||
}
|
||||
|
||||
public function canReview(User $actor, SupplierBankDetail $detail): bool
|
||||
{
|
||||
if ($actor->tenant_id !== $detail->tenant_id || $actor->id === $detail->created_by || ! $actor->hasFunctions([PurchasingRole::Finance])
|
||||
|| app(OrganizationAccess::class)->maintenanceOnly($actor)
|
||||
|| SupplierBankReview::query()->where('supplier_bank_detail_id', $detail->id)->where('reviewed_by', $actor->id)->exists()) {
|
||||
return false;
|
||||
}
|
||||
$supplier = Supplier::query()->find($detail->supplier_id);
|
||||
|
||||
return $supplier !== null && $supplier->eligible() && $this->latest($supplier, $detail->account_key)?->id === $detail->id
|
||||
&& ! app(ConflictGuard::class)->hasConflict($actor->id, $supplier->id, null)
|
||||
&& $this->state($supplier, $detail->account_key) === 'pending';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user