markTestSkipped('Select the authorized unpublished common source for isolated QA.'); } }); /** Explicit synthetic values, not legal rates or real documents. */ function landedCostMoney(int $base, int $tax): array { return ['subtotal_minor' => $base, 'tax_minor' => $tax, 'total_minor' => $base + $tax, 'reference_subtotal_minor' => $base, 'reference_tax_minor' => $tax, 'reference_total_minor' => $base + $tax]; } function landedCostExample(): array { $documents = $components = []; foreach ([['goods', 'supplier_invoice', 10000, 1600], ['freight', 'carrier_invoice', 1000, 160], ['duty', 'customs_assessment', 2000, 0]] as $index => [$kind, $document, $base, $tax]) { $key = 'source-'.($index + 1); $documents[$key] = ['kind' => $document, 'id' => $index + 1, 'creditor_key' => 'creditor-'.($index + 1), 'source_key' => 'native:'.($index + 1), 'source_version' => 1, 'currency' => 'MZN', 'revision' => hash('sha256', $key), ...landedCostMoney($base, $tax)]; $components[] = ['component_key' => $kind, 'kind' => $kind, 'origin' => $key, ...landedCostMoney($base, $tax)]; } return [$components, $documents]; } test('landed cost reconciles each creditor document and both existing budget tax bases without financial effects', function () { [$components, $documents] = landedCostExample(); $withTax = BudgetLandedCostPlan::reconcile($components, $documents, true); $withoutTax = BudgetLandedCostPlan::reconcile($components, $documents, false); expect($withTax['total_minor'])->toBe(14760)->and($withTax['subtotal_minor'])->toBe(13000) ->and($withTax['budget_amount_minor'])->toBe(14760)->and($withoutTax['budget_amount_minor'])->toBe(13000) ->and($withTax['documents']['source-1']['total_minor'])->toBe(11600) ->and($withTax['documents']['source-2']['total_minor'])->toBe(1160) ->and($withTax['documents']['source-3']['total_minor'])->toBe(2000) ->and($withTax['financial_effects'])->toBeFalse(); expect(BudgetLandedCostPlan::reconcile(array_reverse($components), array_reverse($documents, true), true)['fingerprint'])->toBe($withTax['fingerprint']); }); test('missing tax and reference values remain unknown instead of becoming zero', function (string $field) { [$components, $documents] = landedCostExample(); unset($components[2][$field]); expect(fn () => BudgetLandedCostPlan::reconcile($components, $documents, true))->toThrow(BudgetFailure::class); })->with(['tax_minor', 'reference_tax_minor', 'reference_total_minor']); test('an invoice cannot be disguised as a second origin by changing its component role', function () { [$components, $documents] = landedCostExample(); $documents['source-2']['id'] = $documents['source-1']['id']; expect(fn () => BudgetLandedCostPlan::reconcile($components, $documents, true))->toThrow(BudgetFailure::class); }); test('a balanced grand total cannot compensate amounts between different creditors', function () { [$components, $documents] = landedCostExample(); $components[0] = [...$components[0], ...landedCostMoney(10001, 1600)]; $components[1] = [...$components[1], ...landedCostMoney(999, 160)]; expect(fn () => BudgetLandedCostPlan::reconcile($components, $documents, true))->toThrow(BudgetFailure::class); }); test('multiple customs components reconcile once to one customs principal', function () { [$components, $documents] = landedCostExample(); $components[2] = [...$components[2], ...landedCostMoney(1200, 0)]; $components[] = ['component_key' => 'ice', 'kind' => 'ice', 'origin' => 'source-3', ...landedCostMoney(800, 0)]; $plan = BudgetLandedCostPlan::reconcile($components, $documents, true); expect($plan['total_minor'])->toBe(14760)->and($plan['documents'])->toHaveCount(3)->and($plan['components'])->toHaveCount(4); }); test('customs impositions require a customs origin and MZN cannot acquire a fictitious conversion', function () { [$components, $documents] = landedCostExample(); $documents['source-3']['kind'] = 'supplier_invoice'; expect(fn () => BudgetLandedCostPlan::reconcile($components, $documents, true))->toThrow(BudgetFailure::class); [$components, $documents] = landedCostExample(); $documents['source-1']['reference_total_minor']++; $documents['source-1']['reference_tax_minor']++; expect(fn () => BudgetLandedCostPlan::reconcile($components, $documents, true))->toThrow(BudgetFailure::class); }); test('aggregate values refuse overflow even when every document is individually representable', function () { [$components, $documents] = landedCostExample(); foreach ($documents as $key => &$document) { $document = [...$document, ...landedCostMoney(100000000000000, 0)]; } unset($document); foreach ($components as &$component) { $component = [...$component, ...landedCostMoney(100000000000000, 0)]; } unset($component); expect(fn () => BudgetLandedCostPlan::reconcile($components, $documents, true))->toThrow(BudgetFailure::class); });