@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\PurchasingRole;
|
||||
use App\Models\CostCenter;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Models\Requisition;
|
||||
@@ -76,6 +77,132 @@ function orderPartsBalance(object $test): array
|
||||
return $balance;
|
||||
}
|
||||
|
||||
function amendableOrderPartsSource(object $test): array
|
||||
{
|
||||
$centre = CostCenter::query()->create(['tenant_id' => $test->finance->tenant_id, 'department_id' => $test->f['department']->id,
|
||||
'code' => 'PART-AMEND-'.Str::random(8), 'name' => 'Synthetic amendment centre']);
|
||||
$test->actingAs($test->finance);
|
||||
$test->secondBudget = app(CommonBudget::class)->for($test->finance)->create(['cost_center_id' => $centre->id,
|
||||
'year' => (int) now()->year, 'kind' => 'operating', 'includes_tax' => true, 'amount_minor' => 100000,
|
||||
'reason' => 'Synthetic second-centre amendment capacity.']);
|
||||
$test->actingAs($test->reviewer);
|
||||
app(CommonBudget::class)->for($test->reviewer)->approve($test->secondBudget);
|
||||
$test->actingAs($test->f['issuer']);
|
||||
$request = unissuedOrderPartsSource($test->f, 10000);
|
||||
$line = $request->items()->sole();
|
||||
$lines = [$line->id => ['subtotal_minor' => 10000, 'total_minor' => 10000, 'required_kind' => 'operating']];
|
||||
$allocations = [
|
||||
['allocation_key' => 'a', 'line_id' => $line->id, 'centre_id' => $request->cost_center_id, 'subtotal_minor' => 5000, 'total_minor' => 5000],
|
||||
['allocation_key' => 'b', 'line_id' => $line->id, 'centre_id' => $centre->id, 'subtotal_minor' => 5000, 'total_minor' => 5000],
|
||||
];
|
||||
$engine = app(CommonBudget::class)->for($test->f['issuer']);
|
||||
$engine->recordDistribution($request->id, 'reserve', $allocations, $lines, str_repeat('a', 64), (string) Str::uuid());
|
||||
$result = $engine->recordDistribution($request->id, 'award', $allocations, $lines, str_repeat('b', 64), (string) Str::uuid());
|
||||
|
||||
return ['origin' => $request->id, 'version' => 0, 'plan_hash' => $result['plan_hash'],
|
||||
'allocations' => [['allocation_key' => 'a', 'subtotal_minor' => 2000, 'total_minor' => 2000],
|
||||
['allocation_key' => 'b', 'subtotal_minor' => 2000, 'total_minor' => 2000]]];
|
||||
}
|
||||
|
||||
test('explicit Finance order amendment preserves unissued reserve original documents and replay', function () {
|
||||
$source = amendableOrderPartsSource($this);
|
||||
$order = (string) Str::uuid();
|
||||
app(CommonBudget::class)->for($this->f['issuer'])->commitOrderParts($order, [$source], (string) Str::uuid());
|
||||
$this->actingAs($this->finance);
|
||||
$engine = app(CommonBudget::class)->for($this->finance);
|
||||
$original = DB::table('budget_order_commands')->where('kind', 'commit')->first()->snapshot;
|
||||
$source['version'] = 1;
|
||||
$source['allocations'][0]['subtotal_minor'] = $source['allocations'][0]['total_minor'] = 1000;
|
||||
$source['allocations'][1]['subtotal_minor'] = $source['allocations'][1]['total_minor'] = 3000;
|
||||
$operation = (string) Str::uuid();
|
||||
expect($engine->amendOrderParts($order, [$source], 'Explicit synthetic Finance reallocation.', $operation)['replayed'])->toBeFalse()
|
||||
->and($engine->amendOrderParts($order, [$source], 'Explicit synthetic Finance reallocation.', $operation)['replayed'])->toBeTrue()
|
||||
->and(DB::table('budget_order_commands')->where('kind', 'commit')->first()->snapshot)->toBe($original)
|
||||
->and((int) DB::table('budget_distribution_entries')->sum('reserved_minor'))->toBe(6000)
|
||||
->and((int) DB::table('budget_distribution_entries')->sum('committed_minor'))->toBe(4000)
|
||||
->and($engine->balance($this->budget)['committed_minor'])->toBe(1000)
|
||||
->and($engine->balance($this->secondBudget)['committed_minor'])->toBe(3000);
|
||||
expect(fn () => $engine->amendOrderParts($order, [$source], 'Stale independent synthetic amendment.', (string) Str::uuid()))->toThrow(BudgetFailure::class);
|
||||
$distribution = DB::table('budget_distributions')->where('requisition_id', $source['origin'])->first();
|
||||
$source['version'] = 2;
|
||||
$source['plan_hash'] = json_decode($distribution->snapshot, true, 64, JSON_THROW_ON_ERROR)['plan_hash'];
|
||||
$engine->cancelOrderParts($order, [$source], (string) Str::uuid());
|
||||
expect((int) DB::table('budget_distribution_entries')->sum('committed_minor'))->toBe(0)
|
||||
->and((int) DB::table('budget_distribution_entries')->sum('reserved_minor'))->toBe(6000);
|
||||
});
|
||||
|
||||
test('Finance amendment respects consumed floors authorisation atomicity and revised invoice capacity', function () {
|
||||
$source = amendableOrderPartsSource($this);
|
||||
$order = (string) Str::uuid();
|
||||
$issuer = app(CommonBudget::class)->for($this->f['issuer']);
|
||||
$issuer->commitOrderParts($order, [$source], (string) Str::uuid());
|
||||
$source['version'] = 1;
|
||||
$amended = $source;
|
||||
$amended['allocations'][0]['subtotal_minor'] = $amended['allocations'][0]['total_minor'] = 1000;
|
||||
$amended['allocations'][1]['subtotal_minor'] = $amended['allocations'][1]['total_minor'] = 3000;
|
||||
expect(fn () => $issuer->amendOrderParts($order, [$amended], 'Procurement cannot amend Finance allocations.', (string) Str::uuid()))->toThrow(HttpException::class);
|
||||
$this->actingAs($this->finance);
|
||||
$engine = app(CommonBudget::class)->for($this->finance);
|
||||
$invoice = orderPartsInvoice($this, 1500);
|
||||
$consumption = $source;
|
||||
$consumption['allocations'] = [['order_key' => $order, 'allocation_key' => 'a', 'subtotal_minor' => 1500, 'total_minor' => 1500]];
|
||||
$engine->consumeOrderParts($invoice->id, [$consumption], (string) Str::uuid());
|
||||
$amended['version'] = 2;
|
||||
$before = [DB::table('budget_distribution_entries')->get()->toArray(), DB::table('budget_order_commands')->count()];
|
||||
expect(fn () => $engine->amendOrderParts($order, [$amended], 'Cannot reduce the amount already consumed.', (string) Str::uuid()))->toThrow(BudgetFailure::class);
|
||||
expect([DB::table('budget_distribution_entries')->get()->toArray(), DB::table('budget_order_commands')->count()])->toEqual($before);
|
||||
$amended['allocations'][0]['subtotal_minor'] = $amended['allocations'][0]['total_minor'] = 1500;
|
||||
$amended['allocations'][1]['subtotal_minor'] = $amended['allocations'][1]['total_minor'] = 2500;
|
||||
$engine->amendOrderParts($order, [$amended], 'Explicit amendment preserves the consumed floor.', (string) Str::uuid());
|
||||
$distribution = DB::table('budget_distributions')->where('requisition_id', $source['origin'])->first();
|
||||
$consumption = ['origin' => $source['origin'], 'version' => 3,
|
||||
'plan_hash' => json_decode($distribution->snapshot, true, 64, JSON_THROW_ON_ERROR)['plan_hash'],
|
||||
'allocations' => [['order_key' => $order, 'allocation_key' => 'b', 'subtotal_minor' => 2500, 'total_minor' => 2500]]];
|
||||
$engine->consumeOrderParts(orderPartsInvoice($this, 2500)->id, [$consumption], (string) Str::uuid());
|
||||
expect((int) DB::table('budget_order_consumptions')->sum('amount_minor'))->toBe(4000)
|
||||
->and((int) DB::table('budget_distribution_entries')->sum('committed_minor'))->toBe(0)
|
||||
->and((int) DB::table('budget_distribution_entries')->sum('reserved_minor'))->toBe(6000);
|
||||
});
|
||||
|
||||
test('one invalid origin rejects an entire consolidated Finance amendment without partial effects', function () {
|
||||
$a = amendableOrderPartsSource($this);
|
||||
$b = amendableOrderPartsSource($this);
|
||||
$order = (string) Str::uuid();
|
||||
app(CommonBudget::class)->for($this->f['issuer'])->commitOrderParts($order, [$a, $b], (string) Str::uuid());
|
||||
$this->actingAs($this->finance);
|
||||
$a['version'] = $b['version'] = 1;
|
||||
$a['allocations'][0]['subtotal_minor'] = $a['allocations'][0]['total_minor'] = 1000;
|
||||
$a['allocations'][1]['subtotal_minor'] = $a['allocations'][1]['total_minor'] = 3000;
|
||||
$b['allocations'][1]['subtotal_minor'] = $b['allocations'][1]['total_minor'] = 3000;
|
||||
$before = [DB::table('budget_distributions')->get()->toArray(), DB::table('budget_order_allocations')->get()->toArray(),
|
||||
DB::table('budget_order_commands')->count(), DB::table('budget_distribution_entries')->get()->toArray()];
|
||||
expect(fn () => app(CommonBudget::class)->for($this->finance)->amendOrderParts($order, [$a, $b],
|
||||
'Second origin cannot change its original line totals.', (string) Str::uuid()))->toThrow(BudgetFailure::class);
|
||||
expect([DB::table('budget_distributions')->get()->toArray(), DB::table('budget_order_allocations')->get()->toArray(),
|
||||
DB::table('budget_order_commands')->count(), DB::table('budget_distribution_entries')->get()->toArray()])->toEqual($before);
|
||||
});
|
||||
|
||||
test('Finance amendment cannot transfer commitment into a centre with no available budget', function () {
|
||||
$source = amendableOrderPartsSource($this);
|
||||
$centre = DB::table('budgets')->where('id', $this->secondBudget)->value('cost_center_id');
|
||||
$other = unissuedOrderPartsSource($this->f, 95000);
|
||||
$line = $other->items()->sole();
|
||||
$engine = app(CommonBudget::class)->for($this->f['issuer']);
|
||||
$engine->recordDistribution($other->id, 'reserve', [['allocation_key' => 'other', 'line_id' => $line->id, 'centre_id' => $centre,
|
||||
'subtotal_minor' => 95000, 'total_minor' => 95000]], [$line->id => ['subtotal_minor' => 95000, 'total_minor' => 95000, 'required_kind' => 'operating']],
|
||||
str_repeat('c', 64), (string) Str::uuid());
|
||||
$order = (string) Str::uuid();
|
||||
$engine->commitOrderParts($order, [$source], (string) Str::uuid());
|
||||
$this->actingAs($this->finance);
|
||||
$source['version'] = 1;
|
||||
$source['allocations'][0]['subtotal_minor'] = $source['allocations'][0]['total_minor'] = 1000;
|
||||
$source['allocations'][1]['subtotal_minor'] = $source['allocations'][1]['total_minor'] = 3000;
|
||||
expect(fn () => app(CommonBudget::class)->for($this->finance)->amendOrderParts($order, [$source],
|
||||
'Insufficient destination capacity must reject this amendment.', (string) Str::uuid()))->toThrow(BudgetFailure::class);
|
||||
expect(DB::table('budget_order_commands')->where('kind', 'amend')->count())->toBe(0)
|
||||
->and((int) DB::table('budget_distribution_entries')->where('kind', 'like', 'order-amend-%')->count())->toBe(0);
|
||||
});
|
||||
|
||||
test('one approved synthetic source commits forty then sixty without committing the unissued portion', function () {
|
||||
$source = orderPartsSource($this, 10000);
|
||||
$order = (string) Str::uuid();
|
||||
|
||||
Reference in New Issue
Block a user