feat(iva): classify P050 services included in import taxable base

This commit is contained in:
2026-10-24 20:39:23 +02:00
parent 77d1290258
commit f9fb04f947
8 changed files with 126 additions and 18 deletions
@@ -0,0 +1,56 @@
<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Tests\TestCase;
final class IvaImportServicesDraftTest extends TestCase
{
use DatabaseTransactions;
public function test_import_classification_recovers_privately_without_changing_delivered_books(): void
{
$target = DB::selectOne('select current_database() as name,inet_server_port() as port');
$this->assertSame('simulator_fiscal_delta_20261021_test', $target->name);
$this->assertSame(55441, (int) $target->port);
config(['investor-path.enabled' => true, 'suitecrm.enabled' => false]);
$this->actingAs(User::factory()->create(['email_verified_at' => now()]));
$draft = json_decode(file_get_contents(base_path('tests/frontend/fixtures/iva-g6f-imports.json')), true, flags: JSON_THROW_ON_ERROR);
$id = $this->postJson('/api/v1/simulation-drafts', ['personal_snapshot' => [], 'economic_snapshot' => $draft], ['Idempotency-Key' => (string) Str::uuid()])->assertCreated()->json('data.id');
$recovered = $this->getJson('/api/v1/simulation-drafts/'.$id)->assertOk()->json('data.economic_snapshot');
$this->assertSame($draft['modelo_a_snapshot'], $recovered['modelo_a_snapshot']);
$this->assertSame('opportunity:336', $recovered['context_opportunity']);
$this->assertSame('Manica', $recovered['selection_labels']['province_id']);
$store = json_decode($recovered['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR);
$book = $store['importExemptions']['opportunity:336'];
$this->assertCount(2, $book['rows']);
$this->assertSame(['service_in_customs_value','accessory_service_first_destination'], array_column($book['rows'], 'treatment'));
$this->assertSame(array_fill(0, 2, '0.00'), array_column($book['result']['rows'], 'serviceVat'));
$this->assertSame([null,null], array_column($book['result']['rows'], 'importVat'));
$this->assertSame('SYNTHETIC QA importação DU-A,2026-12', $book['rows'][0]['serviceImportRef']);
$this->assertSame('SYNTHETIC QA base DU-A,linha serviço5000MZN', $book['rows'][0]['serviceBaseRef']);
$this->assertSame(array_fill(0, 2, 'exempt'), array_column($book['result']['rows'], 'status'));
$this->assertSame('2026-12', $book['rows'][0]['period']);
$this->assertSame('5000', $book['rows'][0]['declaredValue']);
$this->assertSame('SYNTHETIC QA G6F service_in_customs_value', $book['rows'][0]['documentRef']);
$this->assertStringContainsString('12(1)(e)', $book['result']['source']);
$frozen = json_decode(file_get_contents(base_path('tests/frontend/fixtures/iva-offer-paths-inputs.json')), true, flags: JSON_THROW_ON_ERROR);
$prior = json_decode($frozen['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR);
$this->assertSame($prior['salesOperations'], $store['salesOperations']);
$this->assertSame($prior['books'], $store['books']);
$this->assertSame($prior['importExemptions']['opportunity:330'], $store['importExemptions']['opportunity:330']);
$this->assertSame($prior['importExemptions']['opportunity:331'], $store['importExemptions']['opportunity:331']);
$this->assertSame($prior['importExemptions']['opportunity:332'], $store['importExemptions']['opportunity:332']);
$this->assertCount(2, $book['history']);
$this->assertSame($prior['importExemptions'], array_diff_key($store['importExemptions'], ['opportunity:336' => true]));
$this->assertSame($prior['offerPreparations'], $store['offerPreparations']);
$this->assertNull($book['history'][0]['rows'][0]['importVat']);
$this->assertNull($store['books']['opportunity:329']['result']['periods'][0]['fields']['11']);
$this->assertNull($store['books']['opportunity:329']['result']['periods'][0]['fields']['17']);
$this->assertStringContainsString('BR89', $store['books']['opportunity:329']['result']['sources']);
file_put_contents(base_path('outputs/iva-l1-g6f-services-20261024/recovered.json'), json_encode($recovered, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
}
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,28 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { newImportExemption,evaluateImportExemption,recordImportExemptions,saveImportExemptionBook,importExemptionBook,importExemptionCurrent,importExemptionRules,importServiceSource } from '../../resources/js/lib/iva-import-exemptions.ts';
import { createProjectDossierReport } from '../../resources/js/lib/project-dossier-report.ts';
const frozen=JSON.parse(fs.readFileSync(new URL('./fixtures/iva-offer-paths-inputs.json',import.meta.url),'utf8'));
const rules=importExemptionRules.filter(r=>r.branch==='P050');
const row=rule=>({...newImportExemption('g6f-'+rule.id,'MZN'),treatment:rule.id,description:'SYNTHETIC QA '+rule.label,period:'2026-12',declaredValue:'5000',importer:'SYNTHETIC QA importador destinatário',documentRef:'SYNTHETIC QA G6F '+rule.id,documentDate:'2026-11-14',evidence:'SYNTHETIC QA serviço e montante integral incluídos na base identificada, conforme artigo16; pressuposto expresso, não documento real',serviceImportRef:'SYNTHETIC QA importação DU-A,2026-12',serviceBaseRef:'SYNTHETIC QA base DU-A,linha serviço5000MZN',serviceProvider:'SYNTHETIC QA prestador',...(rule.id==='accessory_service_first_destination'?{serviceFirstDestination:'SYNTHETIC QA Manica,primeiro destino no documento de transporte'}:{}),conditions:Object.fromEntries(rule.conditions.map(([key])=>[key,'yes']))});
const draft=()=>({...structuredClone(frozen),context_opportunity:'opportunity:336',project_title:'SYNTHETIC QA — G6F / Serviços na base da importação'});
test('P050 two treatments exempt only service VAT;every missing/failed condition leaves service and imported-goods tax indeterminate',()=>{
assert.equal(rules.length,2);assert.match(importServiceSource,/16\(2\)/);
for(const rule of rules){const input=row(rule),ok=evaluateImportExemption(input,'MZN');assert.equal(ok.serviceVat,'0.00');assert.equal(ok.importVat,null);assert.match(ok.reason,/IVA do serviço/);
for(const [key] of rule.conditions)for(const value of ['','unknown','no']){const r=evaluateImportExemption({...input,conditions:{...input.conditions,[key]:value}},'MZN');assert.equal(r.status,value==='no'?'not_applicable':'indeterminate');assert.equal(r.serviceVat,null);assert.equal(r.importVat,null);}
for(const patch of [{serviceImportRef:''},{serviceBaseRef:''},{declaredValue:''},{documentRef:''},{period:'2025-12'},{currency:'USD'},{documentDate:'2026-02-30'}])assert.equal(evaluateImportExemption({...input,...patch},'MZN').serviceVat,null);
}
});
test('first-destination reference is required only for added accessory service;switching keeps facts but cannot inherit own eligibility',()=>{
const inValue=row(rules[0]),accessory=row(rules[1]);assert.equal(evaluateImportExemption({...accessory,serviceFirstDestination:''},'MZN').status,'indeterminate');assert.equal(evaluateImportExemption(inValue,'MZN').status,'exempt');const changed={...inValue,treatment:rules[1].id};assert.equal(evaluateImportExemption(changed,'MZN').status,'indeterminate');assert.equal(changed.documentRef,inValue.documentRef);assert.ok(!rules[0].conditions.some(([key])=>key==='serviceToFirstDestination'));
const beyond={...accessory,conditions:{...accessory.conditions,serviceToFirstDestination:'no'}};assert.equal(evaluateImportExemption(beyond,'MZN').status,'not_applicable');assert.equal(evaluateImportExemption({...inValue,conditions:{...inValue.conditions,serviceFullValueIncluded:'no'}},'MZN').serviceVat,null);
});
test('save/recovery/report preserve27fiscalbooks and946fc089scenarios/requests/history exactly;service is not added to base or deduction',()=>{
const d=draft(),prior=JSON.parse(frozen.modelo_a_snapshot);let book=recordImportExemptions(d,{rows:rules.map(rule=>({...row(rule),conditions:{}}))});book=recordImportExemptions(d,{...book,rows:rules.map(rule=>({...row(rule),conditions:{...row(rule).conditions,serviceFullValueIncluded:'no'}}))});book=recordImportExemptions(d,{...book,rows:rules.map(row)});assert.equal(book.history.length,2);assert.ok(book.history.every(h=>h.rows.every(r=>r.serviceVat===null&&r.importVat===null)));d.modelo_a_snapshot=saveImportExemptionBook(d,book);assert.deepEqual(importExemptionBook(JSON.parse(JSON.stringify(d))),book);assert.match(book.result.sourceKey,/import-included-services\/1/);const next=JSON.parse(d.modelo_a_snapshot);for(const key of ['books','salesOperations','offerPreparations'])assert.deepEqual(next[key],prior[key]);for(const [scope,previous] of Object.entries(prior.importExemptions)){assert.deepEqual(next.importExemptions[scope],previous);assert.ok(importExemptionCurrent({...d,context_opportunity:scope},previous));}assert.equal(next.books['opportunity:329'].result.periods[0].fields['11'],null);
const report=JSON.stringify(createProjectDossierReport(d,{selectedSections:new Set(['finance']),version:'QA G6F'}));for(const token of ['Serviço associado à importação P050','IVA do serviço: 0.00','opportunity:336','Manica','2026-12','SYNTHETIC QA G6F','12(1)(e)','primeiro destino','campo11'])assert.ok(report.includes(token),token);assert.ok(!report.includes('IVA na importação: 0.00'));if(process.env.IVA_G6F_CALCULATED)fs.writeFileSync(process.env.IVA_G6F_CALCULATED,JSON.stringify(d));
});
test('old goods rows keep own tax representation and malformed optional service references cannot overwrite saved originals',()=>{
const d=draft(),prior=JSON.parse(d.modelo_a_snapshot);for(const book of Object.values(prior.importExemptions))assert.ok(book.result.rows.every(row=>!Object.hasOwn(row,'serviceVat')));
assert.throws(()=>saveImportExemptionBook(d,{rows:[{...row(rules[0]),serviceBaseRef:42}]}),/Referências do serviço/);
});