markTestSkipped('Select the authorized unpublished common source for isolated QA.'); } $this->f = receiptFixture(); (require __DIR__.'/../../database/qa-migrations/2026_11_22_000009_create_budget_landed_costs.php')->up(); $this->finance = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'purchasing_role' => PurchasingRole::Finance]); $this->reviewer = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'purchasing_role' => PurchasingRole::Finance]); $this->actingAs($this->finance); $this->budget = app(CommonBudget::class)->for($this->finance)->create(['cost_center_id' => $this->f['r']->cost_center_id, 'year' => (int) now()->year, 'kind' => 'operating', 'includes_tax' => true, 'amount_minor' => 20000, 'reason' => 'Explicit synthetic landed-cost budget']); $this->actingAs($this->reviewer); app(CommonBudget::class)->for($this->reviewer)->approve($this->budget); $this->actingAs($this->finance); $this->engine = app(CommonBudget::class)->for($this->finance); $this->key = (string) Str::uuid(); $this->documents = $this->components = []; foreach ([['supplier_invoice', 'goods', 10000, 1600], ['carrier_invoice', 'freight', 1000, 160], ['customs_assessment', 'duty', 2000, 0]] as $index => [$kind, $component, $base, $tax]) { $money = ['subtotal_minor' => $base, 'tax_minor' => $tax, 'total_minor' => $base + $tax, 'reference_subtotal_minor' => $base, 'reference_tax_minor' => $tax, 'reference_total_minor' => $base + $tax]; // Contract-only native source declarations; native invoice integration is tested separately. $this->documents['source-'.$index] = ['kind' => $kind, 'id' => $index + 1, 'source_key' => 'native:'.$index, 'source_version' => 1, 'creditor_key' => 'creditor:'.$index, 'currency' => 'MZN', 'revision' => hash('sha256', (string) $index), ...$money]; $this->components[] = ['component_key' => 'component-'.$index, 'kind' => $component, 'origin' => 'source-'.$index, ...$money]; } }); test('landed reserve commit and consume affect only the additional native origin exactly once', function () { $operation = (string) Str::uuid(); $reserved = $this->engine->reserveLandedCosts($this->key, 2, $this->budget, $this->components, $this->documents, $operation); expect($reserved['total_reference_minor'])->toBe(14760)->and($reserved['additional_amount_minor'])->toBe(2000) ->and($this->engine->balance($this->budget)['reserved_minor'])->toBe(2000); expect($this->engine->reserveLandedCosts($this->key, 2, $this->budget, $this->components, $this->documents, $operation)['replayed'])->toBeTrue(); $committed = $this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], 'commit', (string) Str::uuid()); expect($committed['phase'])->toBe('committed')->and($this->engine->balance($this->budget)['reserved_minor'])->toBe(0) ->and($this->engine->balance($this->budget)['committed_minor'])->toBe(2000); $consume = (string) Str::uuid(); $this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], 'consume', $consume); expect($this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], 'consume', $consume)['replayed'])->toBeTrue(); expect($this->engine->balance($this->budget))->toMatchArray(['reserved_minor' => 0, 'committed_minor' => 0, 'consumed_minor' => 2000, 'available_minor' => 18000]) ->and(DB::table('budget_landed_commands')->count())->toBe(3)->and(DB::table('budget_landed_entries')->count())->toBe(3) ->and(DB::table('budget_landed_entries')->where('source_kind', '!=', 'customs_assessment')->count())->toBe(0) ->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); expect(fn () => $this->engine->reserveLandedCosts($this->key, 3, $this->budget, $this->components, $this->documents, $operation))->toThrow(BudgetFailure::class); expect(fn () => $this->engine->transitionLandedCosts($this->key, 1, $reserved['plan_hash'], 'commit', (string) Str::uuid()))->toThrow(BudgetFailure::class); $this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], 'commit', (string) Str::uuid()); expect(fn () => $this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], 'commit', (string) Str::uuid()))->toThrow(BudgetFailure::class); expect(DB::table('budget_landed_commands')->count())->toBe(2)->and($this->engine->balance($this->budget)['committed_minor'])->toBe(2000); }); test('a native origin cannot be reserved under another plan even after its budget cancellation', function () { $reserved = $this->engine->reserveLandedCosts($this->key, 1, $this->budget, $this->components, $this->documents, (string) Str::uuid()); $this->engine->transitionLandedCosts($this->key, 1, $reserved['plan_hash'], 'cancel', (string) Str::uuid()); expect($this->engine->balance($this->budget)['available_minor'])->toBe(20000); expect(fn () => $this->engine->reserveLandedCosts((string) Str::uuid(), 1, $this->budget, $this->components, $this->documents, (string) Str::uuid()))->toThrow(BudgetFailure::class); expect(DB::table('budget_landed_commands')->count())->toBe(2); }); test('consumed customs costs cannot be cancelled or consumed a second time', function () { $reserved = $this->engine->reserveLandedCosts($this->key, 1, $this->budget, $this->components, $this->documents, (string) Str::uuid()); foreach (['commit', 'consume'] as $kind) { $this->engine->transitionLandedCosts($this->key, 1, $reserved['plan_hash'], $kind, (string) Str::uuid()); } foreach (['cancel', 'consume'] as $kind) { expect(fn () => $this->engine->transitionLandedCosts($this->key, 1, $reserved['plan_hash'], $kind, (string) Str::uuid()))->toThrow(BudgetFailure::class); } expect($this->engine->balance($this->budget)['consumed_minor'])->toBe(2000); }); test('failure of the host audit rolls back the entire additional-origin financial command', function () { $this->mock(RequisitionWorkflow::class, function ($mock) { $mock->shouldReceive('audit')->once()->andThrow(new RuntimeException('Synthetic landed-cost audit failure')); }); expect(fn () => $this->engine->reserveLandedCosts($this->key, 1, $this->budget, $this->components, $this->documents, (string) Str::uuid()))->toThrow(RuntimeException::class); expect(DB::table('budget_landed_commands')->count())->toBe(0)->and(DB::table('budget_landed_entries')->count())->toBe(0) ->and($this->engine->balance($this->budget)['available_minor'])->toBe(20000); }); test('only current Finance can reserve and another company cannot inspect the native plan', function () { $reserved = $this->engine->reserveLandedCosts($this->key, 1, $this->budget, $this->components, $this->documents, (string) Str::uuid()); $this->actingAs($this->f['issuer']); expect(fn () => app(CommonBudget::class)->for($this->f['issuer'])->reserveLandedCosts((string) Str::uuid(), 1, $this->budget, $this->components, $this->documents, (string) Str::uuid()))->toThrow(HttpException::class); $other = receiptFixture(); $foreign = User::factory()->create(['tenant_id' => $other['tenant']->id, 'purchasing_role' => PurchasingRole::Finance]); $this->actingAs($foreign); expect(app(CommonBudget::class)->for($foreign)->landedCostState($this->key))->toBeNull(); expect(fn () => app(CommonBudget::class)->for($foreign)->transitionLandedCosts($this->key, 1, $reserved['plan_hash'], 'commit', (string) Str::uuid()))->toThrow(BudgetFailure::class); }); test('the landed history cannot be mutated deleted or silently reinstalled', function () { $this->engine->reserveLandedCosts($this->key, 1, $this->budget, $this->components, $this->documents, (string) Str::uuid()); foreach (['budget_landed_commands', 'budget_landed_entries'] as $table) { $first = DB::table($table)->first(); expect(fn () => DB::transaction(fn () => DB::table($table)->where('id', $first->id)->update(['tenant_id' => $first->tenant_id])))->toThrow(QueryException::class); expect(fn () => DB::transaction(fn () => DB::table($table)->where('id', $first->id)->delete()))->toThrow(QueryException::class); } expect(fn () => (require __DIR__.'/../../database/qa-migrations/2026_11_22_000009_create_budget_landed_costs.php')->up())->toThrow(LogicException::class); }); test('reviewed actual-cost amendments preserve the original commitment and require new documentary versions', function () { $reserved = $this->engine->reserveLandedCosts($this->key, 2, $this->budget, $this->components, $this->documents, (string) Str::uuid()); $this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], 'commit', (string) Str::uuid()); $before = DB::table('budget_landed_entries')->get()->toArray(); $this->documents['source-2']['subtotal_minor'] = $this->documents['source-2']['total_minor'] = 2100; $this->documents['source-2']['reference_subtotal_minor'] = $this->documents['source-2']['reference_total_minor'] = 2100; $this->components[2]['subtotal_minor'] = $this->components[2]['total_minor'] = 2100; $this->components[2]['reference_subtotal_minor'] = $this->components[2]['reference_total_minor'] = 2100; expect(fn () => $this->engine->amendLandedCosts($this->key, 2, $reserved['plan_hash'], 4, $this->components, $this->documents, (string) Str::uuid()))->toThrow(BudgetFailure::class); $this->documents['source-2']['id'] = 4; $this->documents['source-2']['source_version'] = 2; $this->documents['source-2']['revision'] = hash('sha256', 'Explicit synthetic corrected customs document'); $operation = (string) Str::uuid(); $amended = $this->engine->amendLandedCosts($this->key, 2, $reserved['plan_hash'], 4, $this->components, $this->documents, $operation); expect($amended['additional_amount_minor'])->toBe(2100)->and($amended['phase'])->toBe('committed') ->and($amended['committed_delta_minor'])->toBe(100)->and($amended['consumed_delta_minor'])->toBe(0) ->and($this->engine->balance($this->budget)['committed_minor'])->toBe(2100) ->and(DB::table('budget_landed_entries')->limit(2)->get()->toArray())->toEqual($before); expect($this->engine->amendLandedCosts($this->key, 2, $reserved['plan_hash'], 4, $this->components, $this->documents, $operation)['replayed'])->toBeTrue(); $this->engine->transitionLandedCosts($this->key, 4, $amended['plan_hash'], 'consume', (string) Str::uuid()); expect($this->engine->balance($this->budget)['consumed_minor'])->toBe(2100) ->and(DB::table('budget_landed_entries')->where('source_version', 1)->count())->toBe(2); }); test('additional origins reduce funds available to the preserved legacy budget writer', function () { $this->engine->reserveLandedCosts($this->key, 1, $this->budget, $this->components, $this->documents, (string) Str::uuid()); expect(fn () => $this->engine->record($this->f['r']->id, $this->f['r']->cost_center_id, 'operating', 'reserve', 19000, 19000))->toThrow(BudgetFailure::class); expect(DB::table('budget_entries')->count())->toBe(0)->and($this->engine->balance($this->budget)['available_minor'])->toBe(18000); }); function addLandedContractSource(object $test, string $key = 'native:late', int $amount = 500): void { $money = ['subtotal_minor' => $amount, 'tax_minor' => 0, 'total_minor' => $amount, 'reference_subtotal_minor' => $amount, 'reference_tax_minor' => 0, 'reference_total_minor' => $amount]; $test->documents['late'] = ['kind' => 'other_charge', 'id' => 4, 'source_key' => $key, 'source_version' => 1, 'creditor_key' => 'synthetic-other-authority', 'currency' => 'MZN', 'revision' => hash('sha256', 'Explicit synthetic additional document'), ...$money]; $test->components[] = ['component_key' => 'late', 'kind' => 'other', 'origin' => 'late', ...$money]; } test('per-origin consumption and release preserve the consumed source and reject stale financial revisions', function () { addLandedContractSource($this); $reserved = $this->engine->reserveLandedCosts($this->key, 2, $this->budget, $this->components, $this->documents, (string) Str::uuid()); $this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], 'commit', (string) Str::uuid(), ['native:2'], 1); $consume = (string) Str::uuid(); $partial = $this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], 'consume', $consume, ['native:2'], 2); expect($partial)->toMatchArray(['phase' => 'mixed', 'revision' => 3, 'reserved_minor' => 500, 'committed_minor' => 0, 'consumed_minor' => 2000]); expect(fn () => $this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], 'cancel', (string) Str::uuid(), ['native:late'], 2))->toThrow(BudgetFailure::class); $released = $this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], 'cancel', (string) Str::uuid(), ['native:late'], 3); expect($released['reserved_delta_minor'])->toBe(-500)->and($released['consumed_minor'])->toBe(2000) ->and($this->engine->balance($this->budget))->toMatchArray(['reserved_minor' => 0, 'committed_minor' => 0, 'consumed_minor' => 2000, 'available_minor' => 18000]); expect(fn () => $this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], 'cancel', (string) Str::uuid(), ['native:2'], 4))->toThrow(BudgetFailure::class); expect($this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], 'consume', $consume, ['native:2'], 2)['replayed'])->toBeTrue(); expect(DB::table('budget_landed_commands')->count())->toBe(4); }); test('a late documentary origin is reserved and consumed without repeating an earlier consumption', function () { $reserved = $this->engine->reserveLandedCosts($this->key, 2, $this->budget, $this->components, $this->documents, (string) Str::uuid()); foreach (['commit', 'consume'] as $kind) { $this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], $kind, (string) Str::uuid()); } addLandedContractSource($this); $amended = $this->engine->amendLandedCosts($this->key, 2, $reserved['plan_hash'], 4, $this->components, $this->documents, (string) Str::uuid(), 3); expect($amended)->toMatchArray(['phase' => 'mixed', 'reserved_minor' => 500, 'committed_minor' => 0, 'consumed_minor' => 2000]); $this->engine->transitionLandedCosts($this->key, 4, $amended['plan_hash'], 'commit', (string) Str::uuid()); $final = $this->engine->transitionLandedCosts($this->key, 4, $amended['plan_hash'], 'consume', (string) Str::uuid()); expect($final)->toMatchArray(['phase' => 'consumed', 'consumed_delta_minor' => 500, 'consumed_minor' => 2500]) ->and($this->engine->balance($this->budget)['consumed_minor'])->toBe(2500) ->and(DB::table('budget_landed_entries')->where('source_key', 'native:2')->where('consumed_minor', '>', 0)->count())->toBe(1); }); test('a corrected consumed source commits only its explicit increment and never reduces the consumed floor', function () { $reserved = $this->engine->reserveLandedCosts($this->key, 2, $this->budget, $this->components, $this->documents, (string) Str::uuid()); foreach (['commit', 'consume'] as $kind) { $this->engine->transitionLandedCosts($this->key, 2, $reserved['plan_hash'], $kind, (string) Str::uuid()); } foreach (['subtotal_minor', 'total_minor', 'reference_subtotal_minor', 'reference_total_minor'] as $field) { $this->documents['source-2'][$field] = $this->components[2][$field] = 2100; } $this->documents['source-2']['id'] = 4; $this->documents['source-2']['source_version'] = 2; $this->documents['source-2']['revision'] = hash('sha256', 'Explicit synthetic correction from 2000 to 2100'); $amended = $this->engine->amendLandedCosts($this->key, 2, $reserved['plan_hash'], 4, $this->components, $this->documents, (string) Str::uuid()); expect($amended)->toMatchArray(['reserved_delta_minor' => 100, 'consumed_delta_minor' => 0, 'consumed_minor' => 2000]); $this->engine->transitionLandedCosts($this->key, 4, $amended['plan_hash'], 'commit', (string) Str::uuid()); $final = $this->engine->transitionLandedCosts($this->key, 4, $amended['plan_hash'], 'consume', (string) Str::uuid()); expect($final)->toMatchArray(['consumed_delta_minor' => 100, 'consumed_minor' => 2100]); $before = DB::table('budget_landed_commands')->count(); foreach (['subtotal_minor', 'total_minor', 'reference_subtotal_minor', 'reference_total_minor'] as $field) { $this->documents['source-2'][$field] = $this->components[2][$field] = 1900; } $this->documents['source-2']['id'] = 5; $this->documents['source-2']['source_version'] = 3; $this->documents['source-2']['revision'] = hash('sha256', 'Synthetic lower correction needs separate consumed-cost reconciliation'); expect(fn () => $this->engine->amendLandedCosts($this->key, 4, $amended['plan_hash'], 6, $this->components, $this->documents, (string) Str::uuid()))->toThrow(BudgetFailure::class); expect(DB::table('budget_landed_commands')->count())->toBe($before)->and($this->engine->balance($this->budget)['consumed_minor'])->toBe(2100); });