86 lines
4.9 KiB
PHP
86 lines
4.9 KiB
PHP
<?php
|
|
|
|
use App\Models\SupplyRecord;
|
|
use App\Services\ImportLegalAmounts;
|
|
use App\Services\ImportLegalReferenceDrafts;
|
|
use App\Services\ImportLegalRules;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
require_once __DIR__.'/../Support/ReceiptFixture.php';
|
|
require_once __DIR__.'/../Support/SupplyFixture.php';
|
|
|
|
beforeEach(function () {
|
|
config(['import-tariffs.qa_enabled' => true, 'import-legal.qa_enabled' => true]);
|
|
Storage::fake('local');
|
|
$this->f = receiptFixture();
|
|
supplyGrant($this->f['issuer'], ['procurement', 'import_customs']);
|
|
$this->actingAs($this->f['issuer']);
|
|
});
|
|
|
|
test('a supplied official edition offers exact documentary drafts without publishing rules or changing financial facts', function () {
|
|
$path = 'C:/Mac/Home/Documents/TEMPO/Portal Tempo/Legislacao/novas/BR_248_I_SERIE_SUPLEMENTO_2025.pdf';
|
|
if (! is_file($path)) {
|
|
test()->markTestSkipped('The owner supplied original is required for this real-source integration.');
|
|
}
|
|
expect(hash_file('sha256', $path))->toBe(ImportLegalReferenceDrafts::EDITION_SHA256);
|
|
$before = [];
|
|
foreach (['budget_entries', 'stock_movements', 'purchase_orders', 'invoices'] as $table) {
|
|
$before[$table] = DB::table($table)->orderBy('id')->get()->toJson();
|
|
}
|
|
$service = app(ImportLegalRules::class);
|
|
$source = $service->save($this->f['issuer'], ['operation_key' => (string) Str::uuid(), 'action' => 'source', 'version' => 0,
|
|
'official_reference' => 'BR I Série 248, suplemento, 29/12/2025; Lei 8/2025',
|
|
'reason' => 'Actual supplied official original copied only to synthetic isolated QA; no rule publication.'],
|
|
new UploadedFile($path, basename($path), 'application/pdf', UPLOAD_ERR_OK, true));
|
|
$workspace = $service->workspace($this->f['issuer'], $this->f['r']);
|
|
$drafts = $workspace['referenceDrafts'];
|
|
expect($drafts)->toHaveCount(48)
|
|
->and(array_column($drafts, 'code'))->toContain('3506.91.20', '7604.10.10')
|
|
->and($drafts[0]['rate_percent'])->toBe('20')
|
|
->and($drafts[2]['rate_percent'])->toBe('7.5')
|
|
->and($drafts[0]['valid_from'])->toBe('2026-01-01')
|
|
->and($drafts[0]['citation'])->toContain('2180-(4)')
|
|
->and($drafts[0]['source_id'])->toBe($source->id)
|
|
->and($drafts[0]['charges'][0]['kind'])->toBe('duty')
|
|
->and($drafts[0]['charges'])->toHaveCount(4)
|
|
->and($workspace['referenceDraftWarnings'])->toBe([])
|
|
->and(SupplyRecord::query()->where('kind', 'import_legal_rule')->count())->toBe(0)
|
|
->and(json_encode($workspace['sources'], JSON_THROW_ON_ERROR))->not->toContain('import-legal/');
|
|
expect(array_unique(array_column($drafts, 'code')))->toHaveCount(48);
|
|
$capital = collect($drafts)->firstWhere('code', '8451.80.10');
|
|
$vehicle = collect($drafts)->firstWhere('code', '8704.21.10');
|
|
$last = collect($drafts)->firstWhere('code', '9507.90.90');
|
|
expect($capital['rate_percent'])->toBe('5')->and($capital['documentary_class'])->toBe('K')
|
|
->and($capital['obligations'])->toContain('não comprova elegibilidade')
|
|
->and($capital['citation'])->toContain('2180-(5)')
|
|
->and($vehicle['documentary_class'])->toBeNull()
|
|
->and($vehicle['obligations'])->toContain('bens de capital para investimento')
|
|
->and($last['citation'])->toContain('2180-(7)')
|
|
->and($last['rate_percent'])->toBe('5');
|
|
// A purely illustrative 100 MZN base is not a real import, debt or complete tax assessment.
|
|
$estimate = app(ImportLegalAmounts::class)->calculate($drafts[0]['charges'], ['general_duty' => [
|
|
'value_minor' => 10000, 'quantity' => null, 'quantity_unit' => null, 'source' => 'Explicit synthetic 100 MZN unit-test base']]);
|
|
expect($estimate['known_subtotal_minor'])->toBe(2000)
|
|
->and($estimate['complete'])->toBeFalse()
|
|
->and($estimate['total_minor'])->toBeNull()
|
|
->and($estimate['financial_effects'])->toBeFalse();
|
|
foreach ($before as $table => $rows) {
|
|
expect(DB::table($table)->orderBy('id')->get()->toJson())->toBe($rows);
|
|
}
|
|
Storage::disk('local')->put($source->snapshot['evidence']['path'], 'ALTERED-SYNTHETIC-COPY');
|
|
$workspace = $service->workspace($this->f['issuer'], $this->f['r']);
|
|
expect($workspace['referenceDrafts'])->toBe([])
|
|
->and($workspace['referenceDraftWarnings'])->toHaveCount(1)
|
|
->and($workspace['referenceDraftWarnings'][0]['source_id'])->toBe($source->id)
|
|
->and(hash_file('sha256', $path))->toBe(ImportLegalReferenceDrafts::EDITION_SHA256);
|
|
});
|
|
|
|
test('an unknown edition never inherits parameters from its title or citation', function () {
|
|
$source = new SupplyRecord(['kind' => 'import_legal_source', 'data' => ['official_reference' => 'Lei 8/2025'],
|
|
'snapshot' => ['evidence' => ['sha256' => hash('sha256', 'different edition')]]]);
|
|
expect(app(ImportLegalReferenceDrafts::class)->forSource($source))->toBe([]);
|
|
});
|