feat(iva): prepare goods allocation classification and current acquisition-cost bases in QA
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?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 IvaGoodsAllocationDraftTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function test_goods_allocation_recover_privately_preserving_all_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-goods-allocation.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:353', $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['regulatedGoodsAllocations']['opportunity:353'];
|
||||
$this->assertCount(4, $book['rows']);
|
||||
$this->assertSame(['permanent_private_goods','free_goods_transfer','exempt_sector_goods','fixed_asset_20a'],array_column($book['rows'],'treatment'));
|
||||
$this->assertSame(['1000.25','800.15','1000.25','800.15'],array_column($book['result']['rows'],'baseAmount'));
|
||||
$this->assertSame(array_fill(0,4,'assimilated_goods'),array_column($book['result']['rows'],'classification'));
|
||||
foreach ($book['result']['rows'] as $row) {
|
||||
$this->assertNull($row['vat']);$this->assertNull($row['rate']);
|
||||
}
|
||||
$this->assertSame($book['rows'], $book['result']['inputs']);
|
||||
$this->assertCount(1, $book['history']);
|
||||
$this->assertSame('1000.25',$book['history'][0]['rows'][0]['baseAmount']);
|
||||
$frozen = json_decode(file_get_contents(base_path('tests/frontend/fixtures/iva-goods-allocation-preserved.json')), true, flags: JSON_THROW_ON_ERROR);
|
||||
$prior = json_decode($frozen['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR);
|
||||
$this->assertSame($prior, array_diff_key($store, ['regulatedGoodsAllocations' => true]));
|
||||
$this->assertSame('1250.30', $store['publicAuthorityBases']['opportunity:339']['result']['rows'][1]['baseAmount']);
|
||||
$this->assertNull($store['publicAuthorityBases']['opportunity:339']['result']['rows'][1]['vat']);
|
||||
$this->assertSame('400.00', $store['importExemptions']['opportunity:338']['result']['rows'][1]['importVat']);
|
||||
$this->assertNull($store['books']['opportunity:329']['result']['periods'][0]['fields']['11']);
|
||||
file_put_contents(base_path('outputs/iva-l1-goods-allocation-20261025/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,29 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import {newGoodsAllocation,switchAllocationTreatment,switchAllocationValuation,goodsAllocationConditions,allocationSpecificConditions,goodsAllocationTreatments,evaluateGoodsAllocation,recordGoodsAllocations,saveGoodsAllocationBook,goodsAllocationBook,goodsAllocationCurrent,goodsAllocationReportRows} from '../../resources/js/lib/iva-goods-allocation.ts';
|
||||
const frozen=JSON.parse(fs.readFileSync('tests/frontend/fixtures/iva-goods-allocation-preserved.json','utf8'));const draft={...frozen,currency:'MZN',context_opportunity:'opportunity:353',project_title:'SYNTHETIC QA — Afectação de bens / Manica'};
|
||||
function row(t){const cost=['free_goods_transfer','fixed_asset_20a'].includes(t);return{...newGoodsAllocation(t,'MZN'),treatment:t,description:'SYNTHETIC QA '+t,period:'2026-12',origin:'projected',operationDate:'2026-12-12',operationRef:'SYNTHETIC QA operação',documentRef:'SYNTHETIC QA documento/pressuposto',documentDate:'2026-12-12',goodsRef:'SYNTHETIC QA conjunto de bens/elementos',deductionRef:'SYNTHETIC QA dedução histórica parcial,bens/elementos identificados',destinationRef:'SYNTHETIC QA destino/finalidade/sector/activo eâmbito',exceptionRef:'SYNTHETIC QA factos fora da excepção de amostras/ofertas,não limiar inventado',asset20aRef:'SYNTHETIC QA bens20(1)a/activoimobilizado',valuationMode:cost?'cost':'acquisition',acquisitionPrice:'1000.25',acquisitionRef:'SYNTHETIC QA preço aquisição reportado ao momento',currentCost:'800.15',costRef:'SYNTHETIC QA custo reportado ao momento',valuationDate:'2026-12-12',valuationScope:'SYNTHETIC QA conjunto identificado/preço ao momento,semIVA',evidence:'SYNTHETIC QA pressupostos expressos',conditions:{...Object.fromEntries([...goodsAllocationConditions,...allocationSpecificConditions(t)].map(([key])=>[key,'yes'])),valuationConfirmed:'yes',acquisitionUnavailable:'yes'}};}
|
||||
for(const rule of goodsAllocationTreatments)test('P124/P088 '+rule.id+' ownfacts,historicaldeduction and base',()=>{
|
||||
const r=row(rule.id),result=evaluateGoodsAllocation(r,'MZN');assert.equal(result.status,'determined');assert.equal(result.classification,'assimilated_goods');assert.equal(result.baseAmount,r.valuationMode==='cost'?'800.15':'1000.25');assert.equal(result.vat,null);assert.equal(result.rate,null);
|
||||
for(const [key] of [...goodsAllocationConditions,...allocationSpecificConditions(rule.id)]){const missing=structuredClone(r);delete missing.conditions[key];assert.equal(evaluateGoodsAllocation(missing,'MZN').classification,'indeterminate');missing.conditions[key]='no';assert.equal(evaluateGoodsAllocation(missing,'MZN').status,'not_applicable');assert.equal(evaluateGoodsAllocation(missing,'MZN').baseAmount,null);}
|
||||
for(const field of ['description','operationRef','documentRef','goodsRef','deductionRef','destinationRef','evidence',...(rule.clause==='e'?['exceptionRef']:[]),...(rule.id==='fixed_asset_20a'?['asset20aRef']:[])])assert.equal(evaluateGoodsAllocation({...r,[field]:''},'MZN').classification,'indeterminate',field);
|
||||
for(const patch of [{operationDate:'2026-11-12'},{documentDate:'2026-02-30'},{period:'2025-12'}])assert.equal(evaluateGoodsAllocation({...r,...patch},'MZN').status,'indeterminate');
|
||||
});
|
||||
test('P124classification independentof missingP088base;no newdeduction and no temporaryservice substitution',()=>{
|
||||
const r=row('permanent_private_goods');for(const patch of [{valuationMode:''},{acquisitionPrice:''},{valuationDate:'2026-12-11'},{valuationScope:''},{currency:'USD'},{acquisitionRef:''}]){const result=evaluateGoodsAllocation({...r,...patch},'MZN');assert.equal(result.status,'indeterminate');assert.equal(result.classification,'assimilated_goods');assert.equal(result.baseAmount,null);}
|
||||
assert.match(evaluateGoodsAllocation(r,'MZN').reason,/não nova dedução/);assert.match(evaluateGoodsAllocation(r,'MZN').reason,/P089/);
|
||||
const noPrior=structuredClone(r);noPrior.conditions.priorDeduction='no';assert.equal(evaluateGoodsAllocation(noPrior,'MZN').classification,'not_applicable');assert.equal(evaluateGoodsAllocation(noPrior,'MZN').baseAmount,null);
|
||||
});
|
||||
test('P088costfallback explicit,currentmoment,hidden retention and exact amounts',()=>{
|
||||
const r=row('free_goods_transfer');for(const flag of ['','no']){const result=evaluateGoodsAllocation({...r,conditions:{...r.conditions,acquisitionUnavailable:flag}},'MZN');assert.equal(result.classification,'assimilated_goods');assert.equal(result.baseAmount,null);}
|
||||
assert.equal(evaluateGoodsAllocation({...r,acquisitionPrice:'999999'},'MZN').baseAmount,'800.15');assert.equal(evaluateGoodsAllocation({...r,currentCost:'0'},'MZN').baseAmount,'0.00');assert.equal(evaluateGoodsAllocation({...r,currentCost:'0.000001'},'MZN').baseAmount,'0.000001');for(const v of ['','-1','1e3','1,5'])assert.equal(evaluateGoodsAllocation({...r,currentCost:v},'MZN').baseAmount,null);
|
||||
const acquisition=switchAllocationValuation(r,'acquisition');assert.equal(acquisition.currentCost,r.currentCost);assert.equal(acquisition.acquisitionPrice,r.acquisitionPrice);assert.equal(acquisition.conditions.valuationConfirmed,'');assert.equal(evaluateGoodsAllocation(acquisition,'MZN').baseAmount,null);acquisition.conditions.valuationConfirmed='yes';assert.equal(evaluateGoodsAllocation(acquisition,'MZN').baseAmount,'1000.25');
|
||||
const changed=switchAllocationTreatment(r,'fixed_asset_20a');assert.equal(changed.exceptionRef,r.exceptionRef);assert.equal(changed.asset20aRef,r.asset20aRef);assert.equal(changed.conditions.fixedAsset,undefined);assert.equal(evaluateGoodsAllocation(changed,'MZN').classification,'indeterminate');
|
||||
});
|
||||
test('Sharedoperation under2requirements persists once;all previous books/history/report preserved',()=>{
|
||||
const rows=goodsAllocationTreatments.map(t=>row(t.id));let book=recordGoodsAllocations(draft,{rows});book=recordGoodsAllocations(draft,book);assert.equal(book.history.length,1);assert.notEqual(book.result.inputs,book.rows);
|
||||
const next={...draft,modelo_a_snapshot:saveGoodsAllocationBook(draft,book)};assert.deepEqual(goodsAllocationBook(next),book);assert.ok(goodsAllocationCurrent(next,book));assert.equal(goodsAllocationCurrent({...next,currency:'USD'},book),false);assert.equal(goodsAllocationCurrent({...next,context_opportunity:'other'},book),false);assert.equal(goodsAllocationReportRows(next).length,4);assert.ok(goodsAllocationReportRows(next).every(r=>r[1].includes('P124/baseP088')));
|
||||
const store=JSON.parse(next.modelo_a_snapshot);delete store.regulatedGoodsAllocations;assert.deepEqual(store,JSON.parse(frozen.modelo_a_snapshot));assert.throws(()=>recordGoodsAllocations(next,{rows:[rows[0],rows[0]]}));assert.throws(()=>goodsAllocationBook({...next,modelo_a_snapshot:JSON.stringify({...JSON.parse(next.modelo_a_snapshot),regulatedGoodsAllocations:[]})}));
|
||||
if(process.env.IVA_ALLOCATION_FIXTURE)fs.writeFileSync(process.env.IVA_ALLOCATION_FIXTURE,JSON.stringify(next,null,2)+'\n');
|
||||
});
|
||||
Reference in New Issue
Block a user