feat(iva): explain eliminated P086 exemption and preserve acquisition continuity

This commit is contained in:
2026-10-24 19:03:00 +02:00
parent d3e9f3732c
commit 689a4c93c3
6 changed files with 143 additions and 8 deletions
@@ -0,0 +1,53 @@
<?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 IvaAcquisitionContinuityDraftTest extends TestCase
{
use DatabaseTransactions;
public function test_acquisition_continuity_recovers_with_document_history_and_previous_sales_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('outputs/iva-l1-g5b-acquisitions-20261024/calculated.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:329', $recovered['context_opportunity']);
$this->assertSame('Manica', $recovered['selection_labels']['province_id']);
$this->assertArrayNotHasKey('monthly_iva_settings', $recovered);
$store = json_decode($recovered['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR);
$book = $store['books']['opportunity:329'];
$this->assertCount(1, $book['rows']);
$this->assertSame('normal', $book['rows'][0]['acquisitionTreatment']);
$this->assertSame('former_exploration_exemption', $book['rows'][0]['acquisitionTreatmentOrigin']['id']);
$this->assertSame('100', $book['rows'][0]['net']);
$this->assertSame('16', $book['rows'][0]['vat']);
$this->assertSame('116', $book['rows'][0]['gross']);
$this->assertSame('SYNTHETIC QA G5B 001', $book['rows'][0]['documentRef']);
$this->assertSame('2026-12', $book['rows'][0]['period']);
$this->assertSame('MZN', $book['currency']);
$this->assertCount(1, $book['history']);
$this->assertNull($book['history'][0]['rows'][0]['deductible']);
$this->assertSame('former_exploration_exemption', $book['history'][0]['inputs'][0]['acquisitionTreatment']);
$this->assertSame('16.00', $book['result']['rows'][0]['deductible']);
$this->assertSame('16.00', $book['result']['periods'][0]['fields']['10']);
$this->assertNull($book['result']['periods'][0]['fields']['17']);
$this->assertNull($book['result']['periods'][0]['fields']['18']);
$this->assertStringContainsString('BR89', $book['result']['sources']);
$frozen = json_decode(file_get_contents(base_path('tests/frontend/fixtures/iva-g5a-inputs.json')), true, flags: JSON_THROW_ON_ERROR);
foreach (json_decode($frozen['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR)['salesOperations'] as $context => $original) {
$this->assertSame($original, $store['salesOperations'][$context]);
}
file_put_contents(base_path('outputs/iva-l1-g5b-acquisitions-20261024/recovered.json'), json_encode($recovered, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
}
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,41 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { acquisitionTreatmentIssue,continueWithoutFormerExploration,formerExplorationSource } from '../../resources/js/lib/iva-acquisition-continuity.ts';
import { newAcquisition,evaluateAcquisition,recordAcquisitions,acquisitionBook,acquisitionCurrent,saveAcquisitionBook,acquisitionReportRows } from '../../resources/js/lib/iva-acquisitions.ts';
import { salesCurrent } from '../../resources/js/lib/iva-operations.ts';
import { createProjectDossierReport } from '../../resources/js/lib/project-dossier-report.ts';
const frozen=JSON.parse(fs.readFileSync(new URL('./fixtures/iva-g5a-inputs.json',import.meta.url),'utf8'));
const row=()=>({...newAcquisition('g5b-0','2026-12','MZN'),field:'10',net:'100',vat:'16',gross:'116',documentType:'invoice',documentRef:'SYNTHETIC QA G5B 001',documentDate:'2026-12-10',supplier:'SYNTHETIC QA fornecedor',supplierRegistered:'yes',legalDocument:'yes',held:'yes',domestic:'yes',allocation:'taxable_only',exclusion:'none',integrity:'yes',description:'SYNTHETIC QA — serviço de pesquisa; antiga referência14(2)(c)',acquisitionTreatment:'former_exploration_exemption'});
function fixture(){
const draft={...structuredClone(frozen),context_opportunity:'opportunity:329',project_title:'SYNTHETIC QA — G5B / Continuidade da aquisição'};
const prior=recordAcquisitions(draft,{currency:'MZN',taxpayerRegistered:'yes',rows:[row()],periods:[{period:'2026-12',complete:true,absent:[]}]});draft.modelo_a_snapshot=saveAcquisitionBook(draft,prior);return{draft,prior};
}
test('2017 rectification prevents former14(2)(c) exemption in2026,including explicit zero;unknown is not a normal treatment',()=>{
assert.match(formerExplorationSource,/BR89/);assert.match(acquisitionTreatmentIssue('former_exploration_exemption'),/eliminada/);
for(const vat of ['16','0','']){const input={...row(),vat,gross:vat==='0'?'100':'116'},result=evaluateAcquisition(input,'MZN','yes');assert.equal(result.deductible,null);assert.match(result.reason,/Não se aplica nesta cobertura2026/);assert.equal(input.net,'100');}
assert.equal(evaluateAcquisition({...row(),acquisitionTreatment:'unknown'},'MZN','yes').deductible,null);
assert.match(evaluateAcquisition({...row(),period:'2016-12'},'MZN','yes').reason,/fora do âmbito/);
});
test('explicit continuation keeps values,document,answers and source;normal deduction requires existing conditions',()=>{
const input=row(),next=continueWithoutFormerExploration(input);for(const key of Object.keys(input).filter(k=>k!=='acquisitionTreatment'))assert.deepEqual(next[key],input[key],key);
assert.equal(input.acquisitionTreatment,'former_exploration_exemption');assert.equal(next.acquisitionTreatment,'normal');assert.equal(next.acquisitionTreatmentOrigin.id,'former_exploration_exemption');
assert.equal(evaluateAcquisition(next,'MZN','yes').deductible,'16.00');
for(const patch of [{held:''},{allocation:'mixed'},{legalDocument:''},{sourceCurrency:'USD'},{vat:'',gross:''},{domestic:'no'},{documentDate:'2026-11-10'}])assert.equal(evaluateAcquisition({...next,...patch},'MZN','yes').deductible,null);
assert.throws(()=>continueWithoutFormerExploration({...input,acquisitionTreatment:'normal'}),/antiga referência/);
});
test('manual purchase month does not require sales;previous classification remains in history and incomplete totals remain unknown',()=>{
const{draft,prior}=fixture();assert.equal(draft.monthly_iva_settings,undefined);assert.equal(prior.result.rows[0].deductible,null);assert.equal(prior.result.periods[0].fields['10'],null);
const next=recordAcquisitions(draft,{...prior,rows:[continueWithoutFormerExploration(prior.rows[0])]});assert.deepEqual(next.history,[prior.result]);assert.equal(next.result.rows[0].deductible,'16.00');assert.equal(next.result.periods[0].fields['10'],'16.00');
for(const f of ['01','02','11','14','15','16','17','18'])assert.equal(next.result.periods[0].fields[f],null,f);
assert.throws(()=>recordAcquisitions(draft,{...next,periods:[]}),/meses aplicáveis/);assert.throws(()=>recordAcquisitions(draft,{...next,periods:[...next.periods,...next.periods]}),/meses aplicáveis/);
assert.throws(()=>recordAcquisitions(draft,{...next,rows:[{...next.rows[0],sourceMonth:1,sourceAmount:'100',sourceBasis:'net'}]}),/orçamentos do plano mudaram/);
});
test('save/recovery/report retain rectification,continuation and history;all20 prior sales books stay identical/current',()=>{
const{draft,prior}=fixture();const book=recordAcquisitions(draft,{...prior,rows:[continueWithoutFormerExploration(prior.rows[0])]});draft.modelo_a_snapshot=saveAcquisitionBook(draft,book);assert.ok(acquisitionCurrent(draft,book));assert.deepEqual(acquisitionBook(JSON.parse(JSON.stringify(draft))),JSON.parse(JSON.stringify(book)));
const before=JSON.parse(frozen.modelo_a_snapshot),after=JSON.parse(draft.modelo_a_snapshot);assert.equal(Object.keys(before.salesOperations).length,20);assert.deepEqual(after.salesOperations,before.salesOperations);assert.deepEqual(Object.keys(after.books),['opportunity:329']);
for(const[context,original]of Object.entries(before.salesOperations))assert.ok(salesCurrent({...draft,context_opportunity:context},original));
const report=JSON.stringify(createProjectDossierReport(draft,{selectedSections:new Set(['finance']),version:'QA G5B'}));for(const token of ['opportunity:329','Manica','2026-12','16.00','BR89','Continuidade14(2)(c)','eliminado','expressamente escolhida'])assert.ok(report.includes(token),token);
assert.match(JSON.stringify(acquisitionReportRows(draft)),/antiga isenção/);
if(process.env.IVA_G5B_CALCULATED)fs.writeFileSync(process.env.IVA_G5B_CALCULATED,JSON.stringify(draft));
});