@@ -34,6 +34,7 @@ final class RequisitionExpiry
|
||||
&& constant(BudgetControl::class.'::REQUISITION_CYCLES_VERSION') === CommonBudget::REQUISITION_CYCLES_CONTRACT
|
||||
&& Schema::hasTable('budget_requisition_cycles') && Schema::hasColumn('requisitions', 'approval_cycle')
|
||||
&& Schema::hasTable('workflow_notices') && Schema::hasTable('requisition_commands') && Schema::hasColumn('requisitions', 'process_version')
|
||||
&& Schema::hasColumn('workflow_notices', 'event_kind')
|
||||
&& Schema::hasColumn('approval_steps', 'approval_cycle') && Schema::hasColumn('budget_entries', 'requisition_cycle');
|
||||
}
|
||||
|
||||
@@ -114,6 +115,7 @@ final class RequisitionExpiry
|
||||
return ['requisition' => [...$request->only(['id', 'number', 'status', 'process_version', 'requester_id']),
|
||||
'approval_cycle' => $request->getAttribute('approval_cycle') ?? 1], 'deadline' => $deadline, 'history' => $history,
|
||||
'canExpire' => $deadline['state'] === 'expired' && ! app(ConflictGuard::class)->participationBlocked($actor, $request),
|
||||
'canCheck' => in_array($deadline['state'], ['running', 'expired'], true) && ! app(ConflictGuard::class)->participationBlocked($actor, $request),
|
||||
'canEdit' => $request->status === 'draft' && (int) ($request->getAttribute('approval_cycle') ?? 1) > 1
|
||||
&& $request->requester_id === $actor->id && app(OrganizationAccess::class)->canRequest($actor),
|
||||
'actorId' => $actor->id, 'tenantId' => $actor->tenant_id];
|
||||
@@ -312,4 +314,123 @@ final class RequisitionExpiry
|
||||
app(RequisitionWorkflow::class)->audit($actor, 'requisition.expired_returned', $request, $before,
|
||||
['status' => 'draft', 'cycle' => $cycle + 1, 'expiration_record_id' => $record->id, 'reason' => $proof['reason']]);
|
||||
}
|
||||
|
||||
/** Escalation is a recorded alert to configured, currently authorised viewers; it grants no decision authority.
|
||||
* @param list<int> $profileIds
|
||||
* @return list<User>
|
||||
*/
|
||||
public function escalationRecipients(Requisition $request, array $profileIds): array
|
||||
{
|
||||
if ($profileIds === [] || ! app(OrganizationAccess::class)->active($request->tenant_id)) {
|
||||
return [];
|
||||
}
|
||||
$members = DB::table('organization_memberships as m')->join('organization_profiles as p', 'p.id', '=', 'm.profile_id')
|
||||
->where('m.tenant_id', $request->tenant_id)->where('p.tenant_id', $request->tenant_id)->whereIn('p.id', $profileIds)
|
||||
->where('m.maintenance', false)->where(fn ($q) => $q->whereNull('m.expires_at')->orWhere('m.expires_at', '>', now()))->select('m.user_id');
|
||||
$result = [];
|
||||
foreach (User::query()->where('tenant_id', $request->tenant_id)->where('id', '!=', $request->requester_id)->whereIn('id', $members)->orderBy('id')->get() as $person) {
|
||||
if (app(WorkContext::class)->enforced($person) && ! DB::table('organization_memberships as m')->join('organization_profiles as p', 'p.id', '=', 'm.profile_id')
|
||||
->where('m.tenant_id', $request->tenant_id)->where('p.tenant_id', $request->tenant_id)->where('m.user_id', $person->id)
|
||||
->whereIn('p.id', $profileIds)->where('m.maintenance', false)->where(fn ($q) => $q->whereNull('m.expires_at')->orWhere('m.expires_at', '>', now()))
|
||||
->whereJsonContains('p.work_contexts', 'procurement.internal')->exists()) {
|
||||
continue;
|
||||
}
|
||||
if (! app(OrganizationAccess::class)->maintenanceOnly($person) && app(OrganizationAccess::class)->permissions($person) !== []
|
||||
&& Requisition::query()->visibleTo($person)->whereKey($request->id)->exists()) {
|
||||
$result[] = $person;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $input
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function check(User $actor, Requisition $request, array $input): array
|
||||
{
|
||||
abort_unless($this->ready(), 503);
|
||||
$data = Validator::make($input, ['operation_key' => 'required|uuid', 'process_version' => 'required|integer|min:1'])->validate();
|
||||
$data = ['operation_key' => strtolower($data['operation_key']), 'process_version' => (int) $data['process_version']];
|
||||
$hash = hash('sha256', json_encode([$request->id, $data], JSON_THROW_ON_ERROR));
|
||||
|
||||
return DB::transaction(function () use ($actor, $request, $data, $hash) {
|
||||
app(ConflictGuard::class)->lockCompany($actor);
|
||||
$actor = app(CurrentActor::class)->lock($actor);
|
||||
abort_if(app(OrganizationAccess::class)->maintenanceOnly($actor), 403);
|
||||
abort_unless(app(OrganizationAccess::class)->permissions($actor) !== [], 403);
|
||||
$request = Requisition::query()->visibleTo($actor)->lockForUpdate()->whereKey($request->id)->firstOrFail();
|
||||
app(ConflictGuard::class)->assertParticipation($actor, $request);
|
||||
$retry = SupplyRecord::query()->where('operation_key', $data['operation_key'])->first();
|
||||
if ($retry !== null) {
|
||||
abort_unless($retry->kind === 'req_timeout_check' && $retry->created_by === $actor->id && hash_equals($retry->input_hash, $hash), 409);
|
||||
|
||||
return $retry->snapshot['result'];
|
||||
}
|
||||
abort_unless((int) $request->getAttribute('process_version') === $data['process_version'], 409, 'O pedido mudou. Reveja a etapa actual.');
|
||||
$deadline = $this->deadline($request);
|
||||
$result = ['action' => 'no_pending_deadline', 'notices_created' => 0];
|
||||
if ($deadline['state'] === 'expired') {
|
||||
$childOperation = (string) Str::uuid();
|
||||
app(RequisitionWorkflow::class)->expire($actor, $request, $deadline['basis_hash'], $childOperation, $data['process_version']);
|
||||
$result = ['action' => 'returned', 'notices_created' => 0, 'expiry_operation' => $childOperation];
|
||||
} elseif ($deadline['state'] === 'running') {
|
||||
$step = $request->steps()->whereKey($deadline['step_id'])->firstOrFail();
|
||||
$created = 0;
|
||||
if ($deadline['reminder_at'] !== null && now()->greaterThanOrEqualTo(CarbonImmutable::parse($deadline['reminder_at']))) {
|
||||
$routing = app(ApprovalRouting::class)->current($request, $actor);
|
||||
foreach ($routing['people'] as $person) {
|
||||
$created += DB::table('workflow_notices')->insertOrIgnore(['tenant_id' => $request->tenant_id, 'requisition_id' => $request->id,
|
||||
'approval_step_id' => $step->id, 'recipient_id' => $person['id'], 'reference' => $request->number,
|
||||
'role' => $step->role, 'event_kind' => 'reminder', 'created_at' => now()]);
|
||||
}
|
||||
}
|
||||
$escalationEligible = null;
|
||||
if ($deadline['escalation_at'] !== null && now()->greaterThanOrEqualTo(CarbonImmutable::parse($deadline['escalation_at']))) {
|
||||
$people = $this->escalationRecipients($request, $deadline['profile_ids']);
|
||||
$escalationEligible = count($people);
|
||||
foreach ($people as $person) {
|
||||
$created += DB::table('workflow_notices')->insertOrIgnore(['tenant_id' => $request->tenant_id, 'requisition_id' => $request->id,
|
||||
'approval_step_id' => $step->id, 'recipient_id' => $person->id, 'reference' => $request->number,
|
||||
'role' => $step->role, 'event_kind' => 'escalation', 'created_at' => now()]);
|
||||
}
|
||||
}
|
||||
$result = ['action' => 'deadline_checked', 'notices_created' => $created, 'escalation_eligible' => $escalationEligible];
|
||||
}
|
||||
$receipt = SupplyRecord::query()->create(['tenant_id' => $actor->tenant_id, 'kind' => 'req_timeout_check', 'record_key' => (string) Str::uuid(),
|
||||
'version' => 1, 'state' => 'checked', 'created_by' => $actor->id,
|
||||
'data' => ['requisition_id' => $request->id, 'process_version' => $data['process_version']],
|
||||
'snapshot' => ['result' => $result, 'deadline' => $deadline], 'reason' => 'Verificação dos prazos preservados na submissão.',
|
||||
'operation_key' => $data['operation_key'], 'input_hash' => $hash, 'created_at' => now()]);
|
||||
app(RequisitionWorkflow::class)->audit($actor, 'requisition.timeout_checked', $request, null, ['receipt_id' => $receipt->id, 'result' => $result]);
|
||||
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
/** Execute only due work, so periodic checks do not accumulate empty receipts.
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function scheduled(int $id): array
|
||||
{
|
||||
return app(RequisitionExpiryRun::class)->execute($id, function () use ($id) {
|
||||
$request = Requisition::query()->whereKey($id)->firstOrFail();
|
||||
$deadline = $this->deadline($request);
|
||||
$due = $deadline['state'] === 'expired';
|
||||
if ($deadline['state'] === 'running') {
|
||||
foreach (['reminder' => 'reminder_at', 'escalation' => 'escalation_at'] as $kind => $field) {
|
||||
if ($deadline[$field] !== null && now()->greaterThanOrEqualTo(CarbonImmutable::parse($deadline[$field]))
|
||||
&& ! DB::table('workflow_notices')->where('tenant_id', $request->tenant_id)->where('approval_step_id', $deadline['step_id'])->where('event_kind', $kind)->exists()) {
|
||||
$due = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (! $due) {
|
||||
return ['action' => 'nothing_due'];
|
||||
}
|
||||
$actor = User::query()->whereKey($request->requester_id)->firstOrFail();
|
||||
|
||||
return $this->check($actor, $request, ['operation_key' => (string) Str::uuid(), 'process_version' => $request->getAttribute('process_version')]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user