feat(iva): add G4C linked export services with explicit operation reference

This commit is contained in:
2026-10-24 17:53:35 +02:00
parent 117f2a70a8
commit b1c8e0e343
6 changed files with 123 additions and 2 deletions
@@ -0,0 +1,47 @@
<?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 IvaExportServiceDraftTest extends TestCase
{
use DatabaseTransactions;
public function test_linked_export_services_recover_with_the_selected_opportunity_and_previous_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-g4-services-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:325', $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['salesOperations']['opportunity:325'];
$this->assertSame('opportunity:325', $book['result']['opportunity']);
$this->assertSame('MZN', $book['result']['currency']);
$this->assertSame('2026-12', $book['result']['periods'][0]['period']);
$this->assertSame('1300.00', $book['result']['periods'][0]['fields']['05']);
$this->assertCount(4, $book['rows']);
$this->assertSame('g4c-goods', $book['rows'][2]['relatedExport']['id']);
$this->assertSame('100', $book['rows'][2]['amount']);
$this->assertSame('1000', $book['rows'][0]['amount']);
foreach ($book['result']['rows'] as $row) {
$this->assertSame('05', $row['field']);
$this->assertSame('0.00', $row['vat']);
}
$frozen = json_decode(file_get_contents(base_path('tests/frontend/fixtures/iva-g4b-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-g4-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
@@ -0,0 +1,36 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { exportServiceTreatments } from '../../resources/js/lib/iva-export-services.ts';
import { newSalesOperation,operationTreatments,evaluateSalesOperation,referenceExportOperation,recordSales,saveSalesBook,salesBook,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-g4b-inputs.json',import.meta.url),'utf8'));
const row=(rule,i=0)=>({...newSalesOperation(`g4c-${i}`,'2026-12','MZN'),description:`SYNTHETIC QA — ${rule.label}`,establishment:'SYNTHETIC QA estabelecimento',amount:'100',basis:i%2?'included':'net',treatment:rule.id,confirmed:rule.conditions.map((_,n)=>n),evidence:'SYNTHETIC QA — documento/pressuposto externo identifica bens,regime/localização,mandante e ligação do serviço;operação do intermediário localizada emMoçambique;sem certificação'});
function fixture(){
const draft={...structuredClone(frozen),context_opportunity:'opportunity:325',project_title:'SYNTHETIC QA — G4C / Serviços ligados'};
const goods={...row(operationTreatments.find(r=>r.id==='export_goods'),9),id:'g4c-goods',description:'SYNTHETIC QA — mercadorias expedidas pelo vendedor',amount:'1000'};
const services=exportServiceTreatments.map(row);services[1]=referenceExportOperation(services[1],goods);services[1].confirmed=exportServiceTreatments[1].conditions.map((_,i)=>i);
const book=recordSales(draft,{currency:'MZN',rows:[goods,...services],periods:[{period:'2026-12',complete:true}]});draft.modelo_a_snapshot=saveSalesBook(draft,book);return{draft,book};
}
test('three services require each legal condition,local service scope and evidence;no automatic classification from client or sector',()=>{
assert.equal(exportServiceTreatments.length,3);assert.match(exportServiceTreatments[0].conditions[2],/admissão temporária/);assert.match(exportServiceTreatments[0].conditions[3],/artigo9/);assert.match(exportServiceTreatments[1].conditions[2],/artigo9/);assert.match(exportServiceTreatments[2].conditions[1],/não actuação em nome próprio/);
for(const [i,rule]of exportServiceTreatments.entries()){const input=row(rule,i);assert.equal(evaluateSalesOperation(input,'MZN').field,'05');assert.equal(evaluateSalesOperation(input,'MZN').vat,'0.00');for(const missing of input.confirmed)assert.equal(evaluateSalesOperation({...input,confirmed:input.confirmed.filter(n=>n!==missing)},'MZN').vat,null);for(const patch of [{amount:''},{evidence:''},{sourceCurrency:'USD'},{period:'2027-01'},{basis:''}])assert.equal(evaluateSalesOperation({...input,...patch},'MZN').vat,null);}
});
test('reference reuse keeps service price/identity/evidence and requires deliberate confirmation of new relationship',()=>{
const service=row(exportServiceTreatments[1]),related={...row(operationTreatments.find(r=>r.id==='export_goods'),8),amount:'1000'},linked=referenceExportOperation(service,related);
assert.deepEqual(linked.relatedExport,{id:related.id,description:related.description,treatment:related.treatment});assert.deepEqual(linked.confirmed,[]);
for(const field of ['id','amount','basis','period','description','evidence','establishment','sourceAmount','sourcePeriod','sourceCurrency','source'])assert.deepEqual(linked[field],service[field],field);
assert.equal(linked.amount,'100');assert.equal(evaluateSalesOperation(linked,'MZN').vat,null);assert.throws(()=>referenceExportOperation(service,service));assert.throws(()=>referenceExportOperation(related,service));
});
test('deleted/changed linked operation blocks current calculation,keeps history and requires explicit reference update',()=>{
const{draft,book}=fixture();assert.equal(book.result.periods[0].fields['05'],'1300.00');
for(const rows of [book.rows.filter(r=>r.id!=='g4c-goods'),book.rows.map(r=>r.id==='g4c-goods'?{...r,description:'Changed goods'}:r)]){const changed=recordSales(draft,{...book,rows});assert.equal(changed.result.rows.find(r=>r.id==='g4c-1').vat,null);assert.match(changed.result.rows.find(r=>r.id==='g4c-1').reason,/removida ou alterada/);assert.equal(changed.result.periods[0].fields['05'],null);assert.deepEqual(changed.history.at(-1),book.result);assert.equal(changed.rows.find(r=>r.id==='g4c-1').amount,'100');}
const changedGoods={...book.rows[0],description:'Reviewed goods'};const linked=referenceExportOperation(book.rows[2],changedGoods);linked.confirmed=exportServiceTreatments[1].conditions.map((_,i)=>i);const next=recordSales(draft,{...book,rows:[changedGoods,book.rows[1],linked,book.rows[3]]});assert.equal(next.result.periods[0].fields['05'],'1300.00');
const invalid=recordSales(draft,{...book,rows:book.rows.map(r=>r.id==='g4c-1'?{...r,relatedExport:{id:r.id,description:r.description,treatment:r.treatment}}:r)});assert.equal(invalid.result.periods[0].fields['05'],null);
});
test('stored service reference survives report/recovery;all16 prior books remain unchanged/current without recalculation',()=>{
const{draft,book}=fixture();assert.ok(salesCurrent(draft,book));assert.deepEqual(salesBook(JSON.parse(JSON.stringify(draft))),JSON.parse(JSON.stringify(book)));
const before=JSON.parse(frozen.modelo_a_snapshot).salesOperations,after=JSON.parse(draft.modelo_a_snapshot).salesOperations;assert.equal(Object.keys(before).length,16);for(const[context,original]of Object.entries(before)){assert.deepEqual(after[context],original);assert.ok(salesCurrent({...draft,context_opportunity:context},after[context]));}
const report=JSON.stringify(createProjectDossierReport(draft,{selectedSections:new Set(['finance']),version:'QA G4C'}));for(const token of ['opportunity:325','Manica','2026-12','1300.00','13(1)(m)','13(1)(n)','13(1)(s)','g4c-goods','Operação relacionada'])assert.ok(report.includes(token),token);
if(process.env.IVA_G4C_CALCULATED)fs.writeFileSync(process.env.IVA_G4C_CALCULATED,JSON.stringify(draft));
});