Implementa revisao de adjudicacoes e navegacao partilhada do SGC

This commit is contained in:
2026-10-28 17:46:59 +02:00
parent 5ff8d78803
commit ff403ea927
28 changed files with 1767 additions and 131 deletions
+109
View File
@@ -2,7 +2,9 @@
use App\Enums\PurchasingRole;
use App\Models\AuditLog;
use App\Models\AwardCancellation;
use App\Models\Budget;
use App\Models\BudgetEntry;
use App\Models\CostCenter;
use App\Models\CriticalityLevel;
use App\Models\Department;
@@ -10,7 +12,9 @@ use App\Models\Equipment;
use App\Models\EquipmentSparePart;
use App\Models\InvestmentPolicy;
use App\Models\PurchaseAward;
use App\Models\PurchaseControlPolicy;
use App\Models\PurchaseOrder;
use App\Models\PurchaseRevision;
use App\Models\Quotation;
use App\Models\Requisition;
use App\Models\SparePart;
@@ -23,6 +27,8 @@ use App\Models\Warehouse;
use App\Services\BudgetWorkflow;
use App\Services\ConflictGuard;
use App\Services\EquipmentWorkflow;
use App\Services\PurchaseControlPolicyWorkflow;
use App\Services\PurchaseRevisionWorkflow;
use App\Services\RequisitionWorkflow;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
@@ -504,3 +510,106 @@ test('final approval rechecks a department head who moved departments', function
$this->post('/adjudicacoes/'.$award->id.'/decidir', ['decision' => 'approved'])->assertForbidden();
expect($award->approvals()->where('decision', 'approved')->count())->toBe(0);
});
function enablePurchaseCancellation(object $test): PurchaseControlPolicy
{
$admins = [$test->admin, User::factory()->create(['tenant_id' => $test->tenant->id, 'purchasing_role' => PurchasingRole::Admin]), User::factory()->create(['tenant_id' => $test->tenant->id, 'purchasing_role' => PurchasingRole::Admin])];
$test->actingAs($admins[0]);
$workflow = app(PurchaseControlPolicyWorkflow::class);
$policy = $workflow->propose($admins[0], ['previous_id' => 0, 'mode' => 'unissued_award', 'enabled' => true, 'valid_from' => today()->format('Y-m-d'), 'valid_until' => today()->addMonth()->format('Y-m-d'), 'reason' => 'Regra fictícia para teste isolado']);
foreach ([$admins[1], $admins[2]] as $admin) {
$test->actingAs($admin);
$workflow->review($admin, $policy, 'approved', 'Validação independente fictícia');
}
return $policy->refresh();
}
test('cancellation requires an explicitly published company policy', function () {
$award = selectPurchaseAward($this);
$this->actingAs($this->buyer)->post('/adjudicacoes/'.$award->id.'/cancelamento', ['reason' => 'Necessidade alterada para teste'])->assertSessionHasErrors('cancellation');
expect(AwardCancellation::query()->count())->toBe(0);
});
test('pending cancellation freezes final approval and rejection resumes the original process', function () {
enablePurchaseCancellation($this);
$award = selectPurchaseAward($this);
$this->actingAs($this->buyer)->post('/adjudicacoes/'.$award->id.'/cancelamento', ['reason' => 'Necessidade alterada para teste'])->assertSessionHasNoErrors();
$this->actingAs($this->head)->post('/adjudicacoes/'.$award->id.'/decidir', ['decision' => 'approved'])->assertSessionHasErrors('cancellation');
$cancel = AwardCancellation::query()->firstOrFail();
$this->actingAs($this->finance)->post('/cancelamentos/'.$cancel->id.'/decidir', ['decision' => 'rejected', 'reason' => 'Manter o processo original fictício'])->assertSessionHasNoErrors();
issuePurchaseOrder($this, $award);
expect(PurchaseOrder::query()->count())->toBe(1);
});
test('approved cancellation preserves history and creates exactly one fresh requester draft', function () {
enablePurchaseCancellation($this);
$award = selectPurchaseAward($this);
approvePurchaseAward($this, $award);
$steps = $award->approvals()->count();
$this->actingAs($this->buyer)->post('/adjudicacoes/'.$award->id.'/cancelamento', ['reason' => 'Necessidade alterada para teste'])->assertSessionHasNoErrors();
$this->post('/adjudicacoes/'.$award->id.'/emitir')->assertSessionHasErrors('cancellation');
$cancel = AwardCancellation::query()->firstOrFail();
$this->actingAs($this->finance)->post('/cancelamentos/'.$cancel->id.'/decidir', ['decision' => 'approved', 'reason' => 'Cancelamento independente fictício'])->assertSessionHasNoErrors();
expect($award->refresh()->status)->toBe('cancelled')->and($this->r->refresh()->status)->toBe('cancelled')->and($award->approvals()->count())->toBe($steps)->and(Quotation::query()->count())->toBe(3);
$this->actingAs($this->owner);
$data = ['needed_by' => today()->addWeek()->format('Y-m-d'), 'justification' => 'Nova necessidade fictícia actualizada'];
$this->post('/cancelamentos/'.$cancel->id.'/revisao', $data)->assertSessionHasNoErrors();
$this->post('/cancelamentos/'.$cancel->id.'/revisao', $data)->assertSessionHasNoErrors();
$link = PurchaseRevision::query()->sole();
$draft = Requisition::query()->findOrFail($link->requisition_id);
expect($draft->status)->toBe('draft')->and($draft->steps()->count())->toBe(0)->and($draft->items()->count())->toBe(1)->and($draft->total_minor)->toBe($this->r->total_minor)->and(PurchaseOrder::query()->count())->toBe(0);
});
test('issued orders cannot enter the cancellation circuit', function () {
enablePurchaseCancellation($this);
$award = selectPurchaseAward($this);
issuePurchaseOrder($this, $award);
$this->actingAs($this->buyer)->post('/adjudicacoes/'.$award->id.'/cancelamento', ['reason' => 'Tentativa fictícia após emissão'])->assertSessionHasErrors('cancellation');
expect(AwardCancellation::query()->count())->toBe(0)->and(PurchaseOrder::query()->count())->toBe(1);
});
test('cancellation cannot be self reviewed after a role change', function () {
enablePurchaseCancellation($this);
$award = selectPurchaseAward($this);
$this->actingAs($this->buyer)->post('/adjudicacoes/'.$award->id.'/cancelamento', ['reason' => 'Necessidade alterada para teste'])->assertSessionHasNoErrors();
$cancel = AwardCancellation::query()->firstOrFail();
DB::table('users')->where('id', $this->buyer->id)->update(['purchasing_role' => 'finance']);
$this->post('/cancelamentos/'.$cancel->id.'/decidir', ['decision' => 'approved', 'reason' => 'Tentativa de auto aprovação fictícia'])->assertForbidden();
expect($cancel->refresh()->status)->toBe('pending');
});
test('cancellation releases the unused budget reservation exactly once', function () {
prepareEquipmentInvestment($this);
enablePurchaseCancellation($this);
$award = selectPurchaseAward($this);
$this->actingAs($this->buyer)->post('/adjudicacoes/'.$award->id.'/cancelamento', ['reason' => 'Necessidade alterada para teste'])->assertSessionHasNoErrors();
$cancel = AwardCancellation::query()->firstOrFail();
$data = ['decision' => 'approved', 'reason' => 'Libertação independente fictícia'];
$this->actingAs($this->finance)->post('/cancelamentos/'.$cancel->id.'/decidir', $data)->assertSessionHasNoErrors();
$this->post('/cancelamentos/'.$cancel->id.'/decidir', $data)->assertSessionHasNoErrors();
expect(BudgetEntry::query()->where('requisition_id', $this->r->id)->sum('reserved_minor'))->toBe(0)
->and(BudgetEntry::query()->where('requisition_id', $this->r->id)->where('kind', 'release')->count())->toBe(1);
});
test('cancellation and reservation release roll back together if audit fails', function () {
prepareEquipmentInvestment($this);
enablePurchaseCancellation($this);
$award = selectPurchaseAward($this);
$this->actingAs($this->buyer)->post('/adjudicacoes/'.$award->id.'/cancelamento', ['reason' => 'Necessidade alterada para teste'])->assertSessionHasNoErrors();
$cancel = AwardCancellation::query()->firstOrFail();
$this->actingAs($this->finance);
AuditLog::creating(function (AuditLog $entry) {
if ($entry->action === 'cancellation.approved') {
throw new RuntimeException('Falha fictícia de auditoria');
}
});
try {
expect(fn () => app(PurchaseRevisionWorkflow::class)->review($this->finance, $cancel, 'approved', 'Teste de atomicidade fictício'))->toThrow(RuntimeException::class);
} finally {
AuditLog::flushEventListeners();
}
expect($cancel->refresh()->status)->toBe('pending')->and($award->refresh()->status)->toBe('pending')
->and(BudgetEntry::query()->where('kind', 'release')->count())->toBe(0)
->and((int) BudgetEntry::query()->sum('reserved_minor'))->toBeGreaterThan(0);
});