@@ -0,0 +1,84 @@
|
||||
<?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('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);
|
||||
});
|
||||
Reference in New Issue
Block a user