Files
compras/tests/Feature/ImportLegalEstimatesTest.php
T
nala 1ddabbbdcc
tests / ci (push) Waiting to run
commit compras3 2811
2026-11-28 18:16:04 +02:00

129 lines
10 KiB
PHP

<?php
use App\Services\ImportLegalAmounts;
use App\Services\ImportLegalEstimates;
use App\Services\ImportLegalRules;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\HttpException;
require_once __DIR__.'/../Support/ReceiptFixture.php';
require_once __DIR__.'/../Support/SupplyFixture.php';
require_once __DIR__.'/../Support/ImportLegalQaFixture.php';
function legalCharge(array $overrides = []): array
{
return [...['key' => 'synthetic-duty', 'kind' => 'duty', 'label' => 'Synthetic duty', 'method' => 'ad_valorem',
'rate_percent' => '7.5', 'specific_minor' => null, 'quantity_unit' => null,
'basis_reference' => 'Synthetic customs-value basis; not an approved real legal rule'], ...$overrides];
}
beforeEach(function () {
config(['import-legal.qa_enabled' => true, 'import-tariffs.qa_enabled' => true]);
Storage::fake('local');
$this->withoutVite();
$this->f = receiptFixture();
supplyGrant($this->f['issuer'], ['procurement', 'import_customs']);
$this->actingAs($this->f['issuer']);
});
test('ad valorem specific and the greater-value method use exact decimals and documented units', function () {
$amounts = app(ImportLegalAmounts::class);
$base = ['value_minor' => 10001, 'quantity' => null, 'quantity_unit' => null, 'source' => 'Synthetic explicit customs valuation'];
$result = $amounts->calculate([legalCharge()], ['synthetic-duty' => $base]);
expect($result['total_minor'])->toBe(750)->and($result['financial_effects'])->toBeFalse();
$specific = legalCharge(['method' => 'specific', 'rate_percent' => null, 'specific_minor' => 125, 'quantity_unit' => 'KG']);
expect($amounts->calculate([$specific], ['synthetic-duty' => [...$base, 'value_minor' => null, 'quantity' => '1.500', 'quantity_unit' => 'KG']])['total_minor'])->toBe(188);
$maximum = legalCharge(['method' => 'maximum', 'specific_minor' => 1000, 'quantity_unit' => 'KG']);
expect($amounts->calculate([$maximum], ['synthetic-duty' => [...$base, 'quantity' => '1.500', 'quantity_unit' => 'KG']])['total_minor'])->toBe(1500);
expect($amounts->calculate([$maximum], ['synthetic-duty' => [...$base, 'quantity' => '0.100', 'quantity_unit' => 'KG']])['total_minor'])->toBe(750);
expect(fn () => $amounts->calculate([$maximum], ['synthetic-duty' => [...$base, 'quantity' => '1.5', 'quantity_unit' => 'BOX']]))->toThrow(ValidationException::class);
});
test('unknown rates and missing bases never become zero while explicit non-application and zero rates remain distinct', function () {
$amounts = app(ImportLegalAmounts::class);
$unknown = legalCharge(['key' => 'unknown', 'method' => 'unknown', 'rate_percent' => null]);
$zero = legalCharge(['key' => 'zero', 'rate_percent' => '0']);
$notApplicable = legalCharge(['key' => 'not-applicable', 'method' => 'not_applicable', 'rate_percent' => null]);
$result = $amounts->calculate([$unknown, $zero, $notApplicable], ['zero' => ['value_minor' => 10000, 'quantity' => null, 'quantity_unit' => null, 'source' => 'Synthetic declared base'],
'not-applicable' => ['value_minor' => null, 'quantity' => null, 'quantity_unit' => null, 'source' => 'Synthetic documentary proof that all non-application prerequisites are met']]);
expect($result['complete'])->toBeFalse()->and($result['total_minor'])->toBeNull()->and($result['known_subtotal_minor'])->toBe(0)
->and(array_column($result['lines'], 'amount_minor'))->toBe([null, 0, 0]);
expect($amounts->calculate([], [])['total_minor'])->toBeNull();
expect($amounts->calculate([$notApplicable], [])['total_minor'])->toBeNull();
expect($amounts->calculate([legalCharge()], [])['total_minor'])->toBeNull();
expect(fn () => $amounts->definitions([legalCharge(['method' => 'unknown'])]))->toThrow(ValidationException::class);
expect(fn () => $amounts->calculate([legalCharge()], ['synthetic-duty' => ['value_minor' => 10000, 'quantity' => null, 'quantity_unit' => null, 'source' => null]]))->toThrow(ValidationException::class);
expect(fn () => $amounts->calculate([legalCharge(['rate_percent' => '10000'])], ['synthetic-duty' => ['value_minor' => 100000000000000, 'quantity' => null, 'quantity_unit' => null, 'source' => 'Synthetic overflow boundary']]))->toThrow(ValidationException::class);
});
test('specific coefficients preserve fractional cents and round only the final exact charge', function () {
$amounts = app(ImportLegalAmounts::class);
$charge = legalCharge(['method' => 'specific', 'rate_percent' => null, 'specific_minor' => '1.33', 'quantity_unit' => 'g']);
// Synthetic quantity: 600 L * 10.6 g/100 ml * 10 = 63600 g.
// Coefficient0.0133 MZN/g corresponds exactly to1.33 cents/g.
$base = ['value_minor' => null, 'quantity' => '63600.000000', 'quantity_unit' => 'g', 'source' => 'Synthetic documented quantity and concentration; no implicit conversion'];
expect($amounts->definitions([$charge])[0]['specific_minor'])->toBe('1.33')
->and($amounts->calculate([$charge], ['synthetic-duty' => $base])['total_minor'])->toBe(84588)
->and($amounts->calculate([[...$charge, 'specific_minor' => '0.99']], ['synthetic-duty' => $base])['total_minor'])->toBe(62964);
foreach (['0.499999' => 0, '0.500000' => 1] as $coefficient => $expected) {
expect($amounts->calculate([[...$charge, 'specific_minor' => $coefficient]], ['synthetic-duty' => [...$base, 'quantity' => '1']])['total_minor'])->toBe($expected);
}
expect($amounts->definitions([[...$charge, 'specific_minor' => '125.000000']])[0]['specific_minor'])->toBe(125);
foreach (['0.0000001', '1e2', '-0.1', '100000000000000.000001', 0.99] as $invalid) {
expect(fn () => $amounts->definitions([[...$charge, 'specific_minor' => $invalid]]))->toThrow(ValidationException::class);
}
$maximum = [...$charge, 'method' => 'maximum', 'rate_percent' => '7.5', 'specific_minor' => '0.99'];
expect($amounts->calculate([$maximum], ['synthetic-duty' => [...$base, 'value_minor' => 10001, 'quantity' => '1000']])['total_minor'])->toBe(990);
});
test('native fractional specific estimates preserve the coefficient source and replay without financial effects', function () {
$rules = app(ImportLegalRules::class);
$charge = legalCharge(['method' => 'specific', 'rate_percent' => null, 'specific_minor' => '0.99', 'quantity_unit' => 'g']);
$draft = $rules->save($this->f['issuer'], importLegalQaInput(['charges' => [$charge]]), importLegalQaFile());
$rule = $rules->save($this->f['issuer'], ['operation_key' => (string) Str::uuid(), 'record_key' => $draft->record_key, 'version' => 1,
'action' => 'publish', 'reason' => 'Synthetic fractional coefficient verification, no real tax activation']);
$before = [];
foreach (['budget_entries', 'invoices', 'stock_movements'] as $table) {
$before[$table] = DB::table($table)->orderBy('id')->get()->toJson();
}
$input = ['operation_key' => (string) Str::uuid(), 'version' => 0, 'requisition_item_id' => $this->f['r']->items()->sole()->id,
'rule_id' => $rule->id, 'match_code' => '1234', 'origin' => 'Synthetic A', 'sector' => 'Synthetic sector', 'date' => '2026-11-27',
'bases' => ['synthetic-duty' => ['value_minor' => null, 'quantity' => '63600', 'quantity_unit' => 'g', 'source' => 'Synthetic explicit sugar mass']],
'reason' => 'Synthetic exact coefficient estimate without a budget or stock effect'];
$service = app(ImportLegalEstimates::class);
$record = $service->save($this->f['issuer'], $this->f['r'], $input);
expect($rule->data['charges'][0]['specific_minor'])->toBe('0.99')
->and($record->snapshot['result']['total_minor'])->toBe(62964)
->and($service->save($this->f['issuer'], $this->f['r'], $input)->id)->toBe($record->id);
foreach ($before as $table => $rows) {
expect(DB::table($table)->orderBy('id')->get()->toJson())->toBe($rows);
}
});
test('native estimates freeze source bases and calculated results without reserving funds and reject stale versions', function () {
$rules = app(ImportLegalRules::class);
$draft = $rules->save($this->f['issuer'], importLegalQaInput(['charges' => [legalCharge()]]), importLegalQaFile());
$rule = $rules->save($this->f['issuer'], ['operation_key' => (string) Str::uuid(), 'record_key' => $draft->record_key, 'version' => 1,
'action' => 'publish', 'reason' => 'Synthetic explicit customs rate review for estimates']);
$input = ['operation_key' => (string) Str::uuid(), 'version' => 0, 'requisition_item_id' => $this->f['r']->items()->sole()->id,
'rule_id' => $rule->id, 'match_code' => '1234', 'origin' => 'Synthetic A', 'sector' => 'Synthetic sector', 'date' => '2026-11-27',
'bases' => ['synthetic-duty' => ['value_minor' => 10001, 'quantity' => null, 'quantity_unit' => null, 'source' => 'Synthetic exact customs valuation']],
'reason' => 'Synthetic estimate with explicit source and no financial effects'];
$before = [DB::table('budget_entries')->get()->toJson(), DB::table('invoices')->get()->toJson(), DB::table('stock_movements')->get()->toJson()];
$url = '/requisicoes/'.$this->f['r']->id.'/base-legal/estimativas';
$this->post($url, $input)->assertRedirect();
$service = app(ImportLegalEstimates::class);
$record = $service->save($this->f['issuer'], $this->f['r'], $input);
expect($record->snapshot['result']['total_minor'])->toBe(750)->and($record->snapshot['legal_rule']['id'])->toBe($rule->id)
->and($service->save($this->f['issuer'], $this->f['r'], $input)->id)->toBe($record->id);
expect(fn () => $service->save($this->f['issuer'], $this->f['r'], [...$input, 'operation_key' => (string) Str::uuid()]))->toThrow(HttpException::class);
$second = $service->save($this->f['issuer'], $this->f['r'], [...$input, 'operation_key' => (string) Str::uuid(), 'version' => 1, 'bases' => []]);
expect($second->state)->toBe('indeterminate')->and($second->snapshot['result']['total_minor'])->toBeNull()
->and($record->fresh()->snapshot['result']['total_minor'])->toBe(750)
->and($service->forPlan($this->f['r'], $record->id)->id)->toBe($record->id)
->and([DB::table('budget_entries')->get()->toJson(), DB::table('invoices')->get()->toJson(), DB::table('stock_movements')->get()->toJson()])->toBe($before);
});