feat(iva): classify UN development imports with proportional relief
This commit is contained in:
@@ -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 IvaImportDevelopmentDraftTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function test_development_scope_and_proportional_result_recover_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-g6h-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:338', $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:338'];
|
||||
$this->assertCount(2, $book['rows']);
|
||||
$this->assertSame(['un_development_exemption','un_development_reduction'], array_column($book['rows'], 'treatment'));
|
||||
$this->assertSame(['0.00','400.00'], array_column($book['result']['rows'], 'importVat'));
|
||||
$this->assertSame(['exempt','reduced'], array_column($book['result']['rows'], 'status'));
|
||||
$this->assertSame('2026-12', $book['rows'][0]['period']);
|
||||
$this->assertSame('5000', $book['rows'][0]['declaredValue']);
|
||||
foreach (['developmentProject','developmentAgency','developmentDecisionRef','developmentListRef','developmentPlanRef','developmentScope','developmentRightsRef'] as $field) {
|
||||
$this->assertStringContainsString('SYNTHETIC QA', $book['rows'][0][$field]);
|
||||
}
|
||||
$this->assertSame('50', $book['rows'][1]['developmentReductionPercent']);
|
||||
$this->assertSame('800', $book['rows'][1]['developmentNormalVat']);
|
||||
$this->assertStringContainsString('IVA sem benefício informado', $book['rows'][1]['developmentNormalVatRef']);
|
||||
$this->assertStringContainsString('12(4)', $book['result']['source']);
|
||||
$frozen = json_decode(file_get_contents(base_path('tests/frontend/fixtures/iva-g6h-preserved-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'], array_diff_key($store['importExemptions'], ['opportunity:338' => true]));
|
||||
$this->assertSame($prior['offerPreparations'], $store['offerPreparations']);
|
||||
$this->assertCount(2, $book['history']);
|
||||
$this->assertNull($book['history'][0]['rows'][0]['importVat']);
|
||||
$this->assertSame('0.00', $store['importExemptions']['opportunity:337']['result']['rows'][0]['importVat']);
|
||||
$this->assertSame('0.00', $store['importExemptions']['opportunity:336']['result']['rows'][0]['serviceVat']);
|
||||
$this->assertNull($store['importExemptions']['opportunity:336']['result']['rows'][0]['importVat']);
|
||||
$this->assertNull($store['books']['opportunity:329']['result']['periods'][0]['fields']['11']);
|
||||
file_put_contents(base_path('outputs/iva-l1-g6h-development-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,44 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import {newImportExemption,evaluateImportExemption,recordImportExemptions,saveImportExemptionBook,importExemptionBook,importExemptionCurrent,importExemptionRules,importDevelopmentSource,importDevelopmentFields} 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-g6h-preserved-inputs.json',import.meta.url),'utf8'));
|
||||
const rules=importExemptionRules.filter(r=>r.branch==='P060');
|
||||
const row=rule=>({...newImportExemption('g6h-'+rule.id,'MZN'),treatment:rule.id,description:'SYNTHETIC QA materiais/equipamentos projecto ONU',period:'2026-12',declaredValue:'5000',importer:'SYNTHETIC QA entidade promotora',documentRef:'SYNTHETIC QA G6H '+rule.id,documentDate:'2026-11-14',evidence:'SYNTHETIC QA pressupostos expressos; concessão aduaneira no plano e lista aprovada; não documento real',...Object.fromEntries(importDevelopmentFields.map(([key,label])=>[key,'SYNTHETIC QA '+label+';operação2026-12/bens A;pressuposto expresso'])),...(rule.id==='un_development_reduction'?{developmentReductionPercent:'50',developmentNormalVat:'800',developmentNormalVatRef:'SYNTHETIC QA IVA sem benefício informado, não calculado da base pelo portal'}:{}),conditions:Object.fromEntries(rule.conditions.map(([key])=>[key,'yes']))});
|
||||
const draft=()=>({...structuredClone(frozen),context_opportunity:'opportunity:338',project_title:'SYNTHETIC QA — G6H / Projecto de desenvolvimento'});
|
||||
|
||||
test('P060 own funding,accreditation,destination,decision,list/plan and customs conditions;unknown/no never produce exempt zero',()=>{
|
||||
assert.equal(rules.length,2);assert.match(importDevelopmentSource,/12\(4\)/);
|
||||
for(const rule of rules){const input=row(rule),ok=evaluateImportExemption(input,'MZN');assert.equal(ok.importVat,rule.id==='un_development_exemption'?'0.00':'400.00');assert.equal(ok.status,rule.id==='un_development_exemption'?'exempt':'reduced');assert.equal(ok.serviceVat,undefined);
|
||||
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.importVat,null);}
|
||||
for(const [key] of importDevelopmentFields)assert.equal(evaluateImportExemption({...input,[key]:''},'MZN').importVat,null);
|
||||
for(const patch of [{period:'2025-12'},{documentDate:'2026-02-30'},{currency:'USD'},{documentRef:''},{evidence:''}])assert.equal(evaluateImportExemption({...input,...patch},'MZN').importVat,null);
|
||||
}
|
||||
});
|
||||
|
||||
test('partial benefit is exact proportion of supplied VAT,not a VAT/customs rate or full exemption;missing is not zero',()=>{
|
||||
const input=row(rules[1]);
|
||||
for(const [normal,percent,expected] of [['800','25','600.00'],['800','75','200.00'],['1.234567','33.333333','0.82304467078189'],['999999999999999.999999','99.999999','9999999.99999999999999'],['0','50','0.00']]){
|
||||
const r=evaluateImportExemption({...input,developmentNormalVat:normal,developmentReductionPercent:percent},'MZN');assert.equal(r.status,'reduced');assert.equal(r.importVat,expected);assert.ok(!r.reason.includes('%/100'));
|
||||
}
|
||||
for(const patch of [{developmentReductionPercent:''},{developmentReductionPercent:'-1'},{developmentReductionPercent:'101'},{developmentReductionPercent:'100'},{developmentReductionPercent:'0.0000001'},{developmentNormalVat:''},{developmentNormalVat:'-1'},{developmentNormalVat:'NaN'},{developmentNormalVatRef:''}])assert.equal(evaluateImportExemption({...input,...patch},'MZN').importVat,null);
|
||||
assert.equal(evaluateImportExemption({...input,developmentReductionPercent:'0'},'MZN').status,'not_applicable');
|
||||
const full=row(rules[0]);assert.equal(evaluateImportExemption({...full,developmentNormalVat:'',developmentReductionPercent:'50'},'MZN').importVat,'0.00','hidden stale partial fields do not override full-rights condition');
|
||||
assert.equal(evaluateImportExemption({...full,treatment:rules[1].id},'MZN').status,'indeterminate','switch does not inherit own rights-reduction eligibility');
|
||||
});
|
||||
|
||||
test('save/recovery/report keep77 delivered branches and partial P084/P085 exact;new contexts/history and reference edits isolated',()=>{
|
||||
const d=draft(),prior=JSON.parse(d.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,developmentFavourableDecision:'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.importVat===null)));assert.ok(importExemptionCurrent(d,book));assert.match(book.result.sourceKey,/import-un-development\/1/);
|
||||
for(const field of ['developmentDecisionRef','developmentListRef','developmentPlanRef','developmentScope','developmentRightsRef','developmentReductionPercent','developmentNormalVat'])assert.ok(!importExemptionCurrent(d,{...book,rows:book.rows.map(r=>({...r,[field]:'changed'}))}));
|
||||
assert.ok(!importExemptionCurrent({...d,context_opportunity:'opportunity:339'},book));d.modelo_a_snapshot=saveImportExemptionBook(d,book);assert.deepEqual(importExemptionBook(JSON.parse(JSON.stringify(d))),book);const next=JSON.parse(d.modelo_a_snapshot);assert.deepEqual({...next,importExemptions:prior.importExemptions},prior);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.importExemptions['opportunity:337'].result.rows[0].importVat,'0.00');assert.equal(next.importExemptions['opportunity:336'].result.rows[0].serviceVat,'0.00');assert.equal(next.importExemptions['opportunity:336'].result.rows[0].importVat,null);assert.equal(next.books['opportunity:329'].result.periods[0].fields['11'],null);
|
||||
const report=JSON.stringify(createProjectDossierReport(d,{selectedSections:new Set(['finance']),version:'QA G6H'}));for(const token of ['P060','12(2)(c)','12(4)','400.00','opportunity:338','Manica','2026-12','Lista discriminada aprovada','Plano de importações','Redução proporcional','campo11'])assert.ok(report.includes(token),token);
|
||||
if(process.env.IVA_G6H_CALCULATED)fs.writeFileSync(process.env.IVA_G6H_CALCULATED,JSON.stringify(d));
|
||||
});
|
||||
|
||||
test('optional fields retain documents when switching,while malformed references cannot overwrite saved originals',()=>{
|
||||
const d=draft(),original=d.modelo_a_snapshot;
|
||||
for(const key of [...importDevelopmentFields.map(([key])=>key),'developmentReductionPercent','developmentNormalVat','developmentNormalVatRef'])assert.throws(()=>saveImportExemptionBook(d,{rows:[{...row(rules[1]),[key]:42}]}),/Referências do projecto de desenvolvimento/);
|
||||
assert.equal(d.modelo_a_snapshot,original);const changed={...row(rules[1]),treatment:'un_development_exemption'};d.modelo_a_snapshot=saveImportExemptionBook(d,{rows:[changed]});assert.equal(importExemptionBook(d).rows[0].developmentReductionPercent,'50');assert.equal(importExemptionBook(d).rows[0].developmentPlanRef,changed.developmentPlanRef);assert.equal(evaluateImportExemption(changed,'MZN').status,'indeterminate');
|
||||
});
|
||||
Reference in New Issue
Block a user