feat(iva): implement G2K pharmaceutical manufacturing inputs in QA

This commit is contained in:
2026-10-24 16:05:55 +02:00
parent eb9e0dbc60
commit 8882dc54d0
10 changed files with 340 additions and 10 deletions
@@ -0,0 +1,43 @@
<?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 IvaPharmaInputsDraftTest extends TestCase
{
use DatabaseTransactions;
public function test_selected_annex_rows_round_trip_with_their_own_opportunity(): 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(getenv('IVA_G2K_INPUT') ?: base_path('outputs/iva-l1-g2-pharma-inputs-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']);
$store = json_decode($recovered['modelo_a_snapshot'], true);
$book = $store['salesOperations']['opportunity:320'];
$this->assertSame('opportunity:320', $book['result']['opportunity']);
$this->assertSame('MZN', $book['result']['currency']);
$this->assertSame('2026-12', $book['result']['periods'][0]['period']);
$this->assertCount(5, $book['result']['rows']);
$this->assertSame('500.00', $book['result']['periods'][0]['fields']['07']);
$this->assertSame('0.00', $book['result']['periods'][0]['fields']['05']);
$this->assertNotSame($book['rows'][1]['pharmaAnnexItem'], $book['rows'][2]['pharmaAnnexItem']);
$this->assertStringContainsString('2912.19.00', $book['result']['rows'][1]['source']);
$this->assertStringContainsString('2912.19.00', $book['result']['rows'][2]['source']);
$this->assertStringContainsString('2935.', $book['result']['rows'][3]['source']);
$this->assertStringContainsString('PDF16', $book['result']['rows'][4]['source']);
$original = json_decode(file_get_contents(base_path('tests/frontend/fixtures/iva-fundraising.json')), true, flags: JSON_THROW_ON_ERROR);
foreach (json_decode($original['modelo_a_snapshot'], true)['salesOperations'] as $key => $value) {
$this->assertSame($value, $store['salesOperations'][$key]);
}
if (getenv('IVA_G2K_RECOVERED')) file_put_contents(getenv('IVA_G2K_RECOVERED'), json_encode($recovered, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
}
}
File diff suppressed because one or more lines are too long
+51
View File
@@ -0,0 +1,51 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import fs from 'node:fs';
import {pharmaAnnexEntries,pharmaAnnexEntry,pharmaAnnexVersion} from '../../resources/js/lib/iva-pharma-annex.ts';
import {operationTreatments,newSalesOperation,evaluateSalesOperation,recordSales,saveSalesBook,salesBook,salesCurrent} from '../../resources/js/lib/iva-operations.ts';
import {createProjectDossierReport} from '../../resources/js/lib/project-dossier-report.ts';
const rule=operationTreatments.find(t=>t.id==='pharma_manufacturing_inputs');
const seed=()=>JSON.parse(fs.readFileSync(new URL('./fixtures/iva-fundraising.json',import.meta.url),'utf8'));
const operation=(entry,i=0)=>({...newSalesOperation(`g2k-${i}`,'2026-12','MZN'),description:`SYNTHETIC QA ${entry.code} ${entry.id}`,establishment:'SYNTHETIC QA estabelecimento transmitente integrado no SNS — Manica',amount:'100',basis:i%2?'included':'net',treatment:rule.id,pharmaAnnexItem:entry.id,confirmed:rule.conditions.map((_,n)=>n),evidence:'SYNTHETIC QA: integração do próprio transmitente no SNS, utilização no fabrico e correspondência código/designação expressamente pressupostas; não é certificação'});
function fixture(){const draft=seed();draft.context_opportunity='opportunity:320';draft.project_title='SYNTHETIC QA — G2K Anexo III';
const entries=[pharmaAnnexEntries[0],...pharmaAnnexEntries.filter(e=>e.code==='2912.19.00'||e.code==='2935.'),pharmaAnnexEntries.at(-1)];
const book=recordSales(draft,{currency:'MZN',rows:entries.map(operation),periods:[{period:'2026-12',complete:true}]});
draft.modelo_a_snapshot=saveSalesBook(draft,book);return {draft,book};}
test('AnexoIII has 170 documentary rows, including two distinct 2912.19.00 entries and printed 2935. position',()=>{
assert.equal(pharmaAnnexEntries.length,170);assert.equal(new Set(pharmaAnnexEntries.map(e=>e.id)).size,170);
assert.equal(new Set(pharmaAnnexEntries.map(e=>e.code)).size,169);
const duplicates=pharmaAnnexEntries.filter(e=>e.code==='2912.19.00');assert.equal(duplicates.length,2);assert.notEqual(duplicates[0].label,duplicates[1].label);
assert.equal(pharmaAnnexEntries.filter(e=>e.code==='2935.').length,1);assert.equal(pharmaAnnexEntry('2935.00.00'),undefined);
assert.equal(pharmaAnnexEntries.find(e=>e.code==='2827.39.00').lastPdfPage,13);assert.equal(pharmaAnnexEntries.find(e=>e.code==='2922.49.00').lastPdfPage,15);
for(const [i,entry] of pharmaAnnexEntries.entries()){
const r=evaluateSalesOperation(operation(entry,i),'MZN');assert.equal(r.status,'exempt');assert.equal(r.field,'07');assert.equal(r.vat,'0.00');assert.equal(r.gross,'100.00');
for(const token of [entry.id,entry.code,entry.label,`PDF${entry.pdfPage}`,pharmaAnnexVersion])assert.ok(r.source.includes(token),token);
}
});
test('P008 requires the transmitting establishment, manufacturing use, exact annex identity and evidence; unknown is not zero',()=>{
const {draft,book}=fixture(),row=book.rows[0];
assert.match(rule.conditions[1],/pelo próprio estabelecimento/);assert.match(rule.conditions[1],/não basta.*adquirente/);
for(const missing of row.confirmed)assert.equal(evaluateSalesOperation({...row,confirmed:row.confirmed.filter(n=>n!==missing)},'MZN').vat,null);
for(const patch of [{pharmaAnnexItem:undefined},{pharmaAnnexItem:''},{pharmaAnnexItem:'1108.11.00'},{pharmaAnnexItem:'annexI-2022-001'},{pharmaAnnexItem:'annexIII-2017-001'},{evidence:''},{establishment:''},{amount:''},{period:'2025-12'},{sourceCurrency:'USD'}])assert.equal(evaluateSalesOperation({...row,...patch},'MZN').vat,null);
assert.equal(evaluateSalesOperation(row,'USD').vat,null);
const unknown=recordSales(draft,{...book,rows:[{...row,pharmaAnnexItem:''}]});assert.equal(unknown.result.periods[0].fields['07'],null);
assert.deepEqual(unknown.history.at(-1),book.result);
});
test('P008 keeps its row identity, report and selected opportunity through recovery; eleven delivered books remain current without recalculation',()=>{
const {draft,book}=fixture(),original=seed(),old=JSON.parse(original.modelo_a_snapshot).salesOperations;
const recovered=JSON.parse(JSON.stringify(draft)),store=JSON.parse(recovered.modelo_a_snapshot).salesOperations;
assert.equal(Object.keys(old).length,11);
for(const [id,value] of Object.entries(old)){assert.deepEqual(store[id],value);assert.equal(salesCurrent({...recovered,context_opportunity:id},value),true,id);}
assert.equal(salesCurrent(recovered,salesBook(recovered)),true);
const report=JSON.stringify(createProjectDossierReport(recovered,{selectedSections:new Set(['finance'])}));
for(const token of ['500.00','opportunity:320','Manica','2026-12','MZN','AnexoIII/2022',pharmaAnnexVersion,...book.rows.map(r=>r.pharmaAnnexItem)])assert.ok(report.includes(token),token);
assert.equal(report.includes('fundraising_public'),false);assert.equal(report.includes('production_forestry_opted'),false);
assert.deepEqual(recovered.selection_labels,original.selection_labels);assert.equal(recovered.currency,original.currency);
const duplicates=pharmaAnnexEntries.filter(e=>e.code==='2912.19.00');
const changed={...book,rows:book.rows.map(r=>r.pharmaAnnexItem===duplicates[0].id?{...r,pharmaAnnexItem:duplicates[1].id}:r)};
assert.equal(salesCurrent(draft,changed),false);assert.deepEqual(changed.result,book.result);
});
if(process.env.IVA_G2K_OUT)fs.writeFileSync(process.env.IVA_G2K_OUT,JSON.stringify(fixture().draft,null,2));