Files
compras/tests/Support/IssuedOrderCancellationFixture.php
T
nala 96b30368bc
tests / ci (push) Canceled after 0s
commit compras5 2911
2026-11-29 19:49:19 +02:00

57 lines
3.2 KiB
PHP

<?php
use App\Enums\PurchasingRole;
use App\Models\Budget;
use App\Models\User;
use App\Services\BudgetWorkflow;
use App\Services\CommonBudget;
use App\Services\OrderCancellationPolicyWorkflow;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
require_once __DIR__.'/../Support/ReceiptFixture.php';
function issuedOrderCancellationFixture(object $test, int $cycle = 1): void
{
config(['order_cancellation.enabled' => true]);
$test->f = receiptFixture();
$base = ['tenant_id' => $test->f['tenant']->id, 'department_id' => $test->f['department']->id];
$test->buyer = User::factory()->create([...$base, 'purchasing_role' => PurchasingRole::Procurement]);
$test->finance = User::factory()->create([...$base, 'purchasing_role' => PurchasingRole::Finance]);
$test->admins = [];
foreach (range(1, 3) as $index) {
$test->admins[] = User::factory()->create([...$base, 'purchasing_role' => PurchasingRole::Admin]);
}
$test->actingAs($test->admins[0]);
$flow = app(OrderCancellationPolicyWorkflow::class);
$test->policy = $flow->propose($test->admins[0], ['previous_id' => 0, 'mode' => 'issued_order', 'enabled' => true,
'valid_from' => today()->toDateString(), 'valid_until' => today()->addWeek()->toDateString(), 'reason' => 'Política exclusivamente fictícia para o circuito de testes.']);
foreach ([$test->admins[1], $test->admins[2]] as $admin) {
$test->actingAs($admin);
$flow->review($admin, $test->policy, 'approved', 'Conferência independente da política fictícia.');
}
$test->actingAs($test->finance);
$test->budget = Budget::query()->create(['tenant_id' => $test->f['tenant']->id, 'cost_center_id' => $test->f['r']->cost_center_id, 'year' => now()->year,
'amount_minor' => 20000, 'includes_tax' => true, 'reason' => 'Dotação fictícia para ensaio da reversão.', 'created_by' => $test->finance->id,
'approved_by' => $test->admins[0]->id, 'approved_at' => now()]);
DB::transaction(function () use ($test, $cycle) {
if ($cycle === 2) {
$origin = $test->f['r']->id;
$adapter = app(CommonBudget::class);
$first = $adapter->for($test->finance, [$origin => 1]);
$first->beginRequisitionCycle($origin, 1, (string) Str::uuid());
$first->record($origin, $test->f['r']->cost_center_id, 'operating', 'reserve', 10000, 10000);
$first->record($origin, $test->f['r']->cost_center_id, 'operating', 'release');
$first->closeRequisitionCycle($origin, 1, (string) Str::uuid());
$second = $adapter->for($test->finance, [$origin => 2]);
$second->beginRequisitionCycle($origin, 2, (string) Str::uuid());
$test->f['r']->forceFill(['approval_cycle' => 2])->save();
}
foreach (['reserve', 'award', 'commit'] as $kind) {
app(BudgetWorkflow::class)->record($test->finance, $test->f['r'], $kind, 10000, 10000);
}
});
$test->data = ['operation_key' => (string) Str::uuid(), 'reason' => 'Cancelamento integral fictício antes de qualquer execução.', 'evidence' => 'Referência documental fictícia do fundamento do cancelamento.'];
$test->actingAs($test->buyer);
}