@@ -10,6 +10,7 @@ use Illuminate\Support\Str;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Tempo\Finance\Budget\BudgetControl;
|
||||
use Tempo\Finance\Budget\BudgetFailure;
|
||||
use Tempo\Finance\Budget\BudgetImportEstimate;
|
||||
|
||||
require_once __DIR__.'/../Support/ReceiptFixture.php';
|
||||
|
||||
@@ -58,6 +59,75 @@ test('landed reserve commit and consume affect only the additional native origin
|
||||
->and(DB::table('invoices')->count())->toBe(0)->and(DB::table('budget_consumptions')->count())->toBe(0);
|
||||
});
|
||||
|
||||
test('pre-document assumptions reserve only extras and reconcile atomically into actual origins without an invented principal', function () {
|
||||
$components = array_map(fn ($kind) => ['key' => $kind, 'kind' => $kind, 'status' => 'not_applicable', 'basis' => 'Pressuposto sintético explícito',
|
||||
'subtotal_minor' => null, 'tax_minor' => null, 'total_minor' => null], BudgetImportEstimate::KINDS);
|
||||
$components[0] = [...$components[0], 'status' => 'known', 'subtotal_minor' => 1000, 'tax_minor' => 160, 'total_minor' => 1160];
|
||||
$goods = ['subtotal_minor' => 10000, 'tax_minor' => 1600, 'total_minor' => 11600];
|
||||
$operation = (string) Str::uuid();
|
||||
$beforePrincipal = DB::table('invoices')->count();
|
||||
$reserved = $this->engine->reserveImportEstimate($this->key, 1, $this->budget, $this->f['r']->id, $goods, $components, $operation);
|
||||
expect($reserved['additional_amount_minor'])->toBe(1160)->and($reserved['total_reference_minor'])->toBe(12760)
|
||||
->and($this->engine->balance($this->budget)['reserved_minor'])->toBe(1160)
|
||||
->and($this->engine->reserveImportEstimate($this->key, 1, $this->budget, $this->f['r']->id, $goods, $components, $operation)['replayed'])->toBeTrue();
|
||||
expect(fn () => $this->engine->reserveImportEstimate((string) Str::uuid(), 1, $this->budget, $this->f['r']->id, $goods, $components, (string) Str::uuid()))->toThrow(BudgetFailure::class);
|
||||
expect(fn () => $this->engine->reconcileImportEstimate($this->key, 1, $reserved['plan_hash'], 1, 1000,
|
||||
(string) Str::uuid(), 1, $this->budget, $this->components, $this->documents, (string) Str::uuid(), $operation))->toThrow(BudgetFailure::class);
|
||||
expect($this->engine->balance($this->budget)['reserved_minor'])->toBe(1160);
|
||||
$this->engine->transitionLandedCosts($this->key, 1, $reserved['plan_hash'], 'commit', (string) Str::uuid());
|
||||
expect(fn () => $this->engine->transitionLandedCosts($this->key, 1, $reserved['plan_hash'], 'consume', (string) Str::uuid()))->toThrow(BudgetFailure::class);
|
||||
$actual = $this->engine->amendLandedCosts($this->key, 1, $reserved['plan_hash'], 2, $this->components, $this->documents, (string) Str::uuid());
|
||||
expect($actual['additional_amount_minor'])->toBe(2000)->and($this->engine->balance($this->budget)['reserved_minor'])->toBe(2000)
|
||||
->and($this->engine->balance($this->budget)['committed_minor'])->toBe(0)
|
||||
->and(DB::table('invoices')->count())->toBe($beforePrincipal);
|
||||
});
|
||||
|
||||
test('partial estimate reconciliation preserves its unallocated remainder and actual costs once with atomic failure', function () {
|
||||
$components = array_map(fn ($kind) => ['key' => $kind, 'kind' => $kind, 'status' => 'not_applicable', 'basis' => 'Pressuposto sintético explícito',
|
||||
'subtotal_minor' => null, 'tax_minor' => null, 'total_minor' => null], BudgetImportEstimate::KINDS);
|
||||
$components[0] = [...$components[0], 'status' => 'known', 'subtotal_minor' => 3000, 'tax_minor' => 0, 'total_minor' => 3000];
|
||||
$reserved = $this->engine->reserveImportEstimate($this->key, 1, $this->budget, $this->f['r']->id,
|
||||
['subtotal_minor' => 10000, 'tax_minor' => 0, 'total_minor' => 10000], $components, (string) Str::uuid());
|
||||
$actualKey = (string) Str::uuid();
|
||||
$operation = (string) Str::uuid();
|
||||
$actualOperation = (string) Str::uuid();
|
||||
$apply = fn () => $this->engine->reconcileImportEstimate($this->key, 1, $reserved['plan_hash'], 1, 1000,
|
||||
$actualKey, 1, $this->budget, $this->components, $this->documents, $operation, $actualOperation);
|
||||
$result = $apply();
|
||||
expect($result['additional_amount_minor'])->toBe(2000)->and($result['estimate_reconciliation']['reserved_minor'])->toBe(2000)
|
||||
->and($this->engine->balance($this->budget)['reserved_minor'])->toBe(4000)
|
||||
->and($apply()['replayed'])->toBeTrue()->and(DB::table('budget_landed_commands')->count())->toBe(3);
|
||||
$before = DB::table('budget_landed_entries')->orderBy('id')->get()->toJson();
|
||||
expect(fn () => $this->engine->reconcileImportEstimate($this->key, 1, $reserved['plan_hash'], 2, 1000,
|
||||
(string) Str::uuid(), 1, $this->budget, $this->components, $this->documents, (string) Str::uuid(), (string) Str::uuid()))->toThrow(BudgetFailure::class);
|
||||
expect(DB::table('budget_landed_entries')->orderBy('id')->get()->toJson())->toBe($before)
|
||||
->and(DB::table('budget_landed_commands')->count())->toBe(3);
|
||||
});
|
||||
|
||||
test('multiple approved-origin estimates reconcile to the same native invoice plan without another principal or repeated actual cost', function () {
|
||||
$components = array_map(fn ($kind) => ['key' => $kind, 'kind' => $kind, 'status' => 'not_applicable', 'basis' => 'Pressuposto sintético explícito',
|
||||
'subtotal_minor' => null, 'tax_minor' => null, 'total_minor' => null], BudgetImportEstimate::KINDS);
|
||||
$components[0] = [...$components[0], 'status' => 'known', 'subtotal_minor' => 3000, 'tax_minor' => 0, 'total_minor' => 3000];
|
||||
$goods = ['subtotal_minor' => 10000, 'tax_minor' => 0, 'total_minor' => 10000];
|
||||
$first = $this->engine->reserveImportEstimate($this->key, 1, $this->budget, $this->f['r']->id, $goods, $components, (string) Str::uuid());
|
||||
$request = $this->f['r']->replicate();
|
||||
$request->number = 'SECOND-ESTIMATE-'.Str::random(10);
|
||||
$request->save();
|
||||
$secondKey = (string) Str::uuid();
|
||||
$second = $this->engine->reserveImportEstimate($secondKey, 1, $this->budget, $request->id, $goods, $components, (string) Str::uuid());
|
||||
$documents = array_diff_key($this->documents, ['source-2' => true]);
|
||||
$actualComponents = array_values(array_filter($this->components, fn ($row) => $row['kind'] !== 'duty'));
|
||||
$actualKey = (string) Str::uuid();
|
||||
$this->engine->reconcileImportEstimate($this->key, 1, $first['plan_hash'], 1, 1000, $actualKey, 1, $this->budget,
|
||||
$actualComponents, $documents, (string) Str::uuid(), (string) Str::uuid());
|
||||
$result = $this->engine->reconcileImportEstimate($secondKey, 1, $second['plan_hash'], 1, 2000, $actualKey, 1, $this->budget,
|
||||
$actualComponents, $documents, (string) Str::uuid(), (string) Str::uuid());
|
||||
expect($result['additional_amount_minor'])->toBe(0)->and($result['origins'])->toBe([])
|
||||
->and($this->engine->balance($this->budget)['reserved_minor'])->toBe(3000)
|
||||
->and($this->engine->landedCostState($actualKey)['result']['revision'])->toBe(2)
|
||||
->and(DB::table('invoices')->count())->toBe(0)->and(DB::table('budget_consumptions')->count())->toBe(0);
|
||||
});
|
||||
|
||||
test('landed operation mismatch stale versions and repeated transition fail without changing the balance', function () {
|
||||
$operation = (string) Str::uuid();
|
||||
$reserved = $this->engine->reserveLandedCosts($this->key, 2, $this->budget, $this->components, $this->documents, $operation);
|
||||
|
||||
Reference in New Issue
Block a user