This commit is contained in:
2026-11-03 11:51:44 +02:00
parent badaada043
commit e753f7a0a4
309 changed files with 14537 additions and 529 deletions
+39 -1
View File
@@ -22,6 +22,41 @@ use Illuminate\Validation\ValidationException;
class SupplyCoordination
{
public function canManage(User $actor, SupplyRecord $record, string $action): bool
{
if ($actor->tenant_id !== $record->tenant_id || ! in_array($record->kind, ['reservation', 'delivery'], true)
|| ! app(SupplyPlanning::class)->allows($actor, $record->kind === 'reservation' ? 'supply_reserve' : 'supply_delivery')) {
return false;
}
$latest = app(SupplyPlanning::class)->latest($record->kind)->where('record_key', $record->record_key)->first();
if ($latest?->id !== $record->id) {
return false;
}
$open = $record->state === 'active' || ($record->kind === 'reservation' && $record->state === 'fulfilled' && $this->remainingReservation($record) > 0);
return match ($action) {
'edit', 'cancel' => $open,
'fulfil' => $record->kind === 'reservation' && in_array($record->state, ['active', 'fulfilled'], true) && $this->remainingReservation($record) > 0,
default => false,
};
}
private function independentTechnicalReviewer(User $actor, SupplyRecord $record): bool
{
return $actor->tenant_id === $record->tenant_id && app(SupplyPlanning::class)->allows($actor, 'supply_technical_review')
&& ! in_array($actor->id, $record->snapshot['contributors'], true);
}
public function canReviewAlternative(User $actor, SupplyRecord $record, string $action = 'approve'): bool
{
if ($record->kind !== 'alternative' || ! in_array($action, ['approve', 'return', 'suspend'], true) || ! $this->independentTechnicalReviewer($actor, $record)) {
return false;
}
$latest = app(SupplyPlanning::class)->latest('alternative')->where('record_key', $record->record_key)->first();
return $latest?->id === $record->id && ($action === 'suspend' ? $record->state === 'approved' : in_array($record->state, ['draft', 'returned'], true));
}
/** @param array<string,mixed> $input */
public function save(User $actor, string $kind, array $input): SupplyRecord
{
@@ -47,6 +82,9 @@ class SupplyCoordination
}
$old = isset($v['record_key']) ? app(SupplyPlanning::class)->latest($kind)->where('record_key', $v['record_key'])->firstOrFail() : null;
$this->require((int) $v['version'] === (int) ($old->version ?? 0), 'version', 'O registo mudou. Actualize antes de continuar.');
if ($old !== null && in_array($kind, ['reservation', 'delivery'], true)) {
$this->require($this->canManage($actor, $old, $v['action'] === 'save' ? 'edit' : $v['action']), 'action', 'O registo não admite esta intervenção no estado actual.');
}
$data = $old->data ?? [];
$snapshot = $old->snapshot ?? [];
$state = $old->state ?? 'draft';
@@ -72,7 +110,7 @@ class SupplyCoordination
$state = $kind === 'alternative' ? 'draft' : 'active';
} elseif ($kind === 'alternative') {
$this->require($old !== null && in_array($v['action'], ['approve', 'return', 'suspend'], true), 'action', 'Acção técnica inválida.');
$this->require(! in_array($actor->id, $snapshot['contributors'], true), 'action', 'A validação técnica exige independência dos preparadores.');
$this->require($this->independentTechnicalReviewer($actor, $old), 'action', 'A validação técnica exige independência dos preparadores.');
$this->require($v['action'] === 'suspend' ? $state === 'approved' : in_array($state, ['draft', 'returned'], true), 'action', 'Estado incompatível.');
if ($v['action'] === 'approve') {
$this->require($data['compatibility'] !== 'pending', 'action', 'Conclua a análise técnica antes de aprovar.');