Implement P092 P093 P125 individual secondhand bases in QA

This commit is contained in:
2026-10-25 00:27:37 +02:00
parent ec786f4ae2
commit b0818c0163
8 changed files with 221 additions and 0 deletions
+49
View File
@@ -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 IvaSecondhandDraftTest extends TestCase
{
use DatabaseTransactions;
public function test_secondhand_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-secondhand.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:354', $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['regulatedSecondhands']['opportunity:354'];
$this->assertCount(8, $book['rows']);
$this->assertSame(array_merge(array_fill(0,4,'reseller_margin'),array_fill(0,4,'auction_margin')),array_column($book['rows'],'treatment'));
$this->assertSame(array_merge(array_fill(0,4,'138.00'),array_fill(0,4,'344.83')),array_column($book['result']['rows'],'baseAmount'));
$this->assertSame(array_fill(0,8,'qualified_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('138.00',$book['history'][0]['rows'][0]['baseAmount']);
$frozen = json_decode(file_get_contents(base_path('tests/frontend/fixtures/iva-secondhand-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, ['regulatedSecondhands' => 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-secondhand-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
+27
View File
@@ -0,0 +1,27 @@
import test from 'node:test';import assert from 'node:assert/strict';import fs from 'node:fs';
import {newSecondhand,switchSecondhandTreatment,switchSecondhandCategory,secondhandConditions,secondhandCategoryConditions,secondhandTreatments,secondhandCategories,evaluateSecondhand,recordSecondhands,saveSecondhandBook,secondhandBook,secondhandCurrent,secondhandReportRows} from '../../resources/js/lib/iva-secondhand.ts';
const frozen=JSON.parse(fs.readFileSync('tests/frontend/fixtures/iva-secondhand-preserved.json','utf8'));const draft={...frozen,currency:'MZN',context_opportunity:'opportunity:354',project_title:'SYNTHETIC QA — Segunda mão / Manica'};
function row(t,c){return{...newSecondhand(t+'-'+c,'MZN'),treatment:t,category:c,description:'SYNTHETIC QA '+t+'/'+c,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 bem individual '+t+c,operatorRef:'SYNTHETIC QA estatuto operador',categoryRef:'SYNTHETIC QA características/definição3(6)',artSubtype:'print',editionCount:'200',ceramicUniqueSigned:'yes',collectionSubtype:'postal',postalNotCirculating:'yes',ageYears:'101',sourceType:'private',sourceRef:'SYNTHETIC QA origem particular nacional',domesticPurchase:'yes',regimeChoice:'margin',domesticTaxable:'yes',grossSale:'1160',purchasePrice:'1000',purchaseRef:'SYNTHETIC QA preço compra justificado',grossInvoice:'1300',hammerPrice:'1000',principalCommission:'100',auctionRef:'SYNTHETIC QA organizador emnome próprio/contrato comissão/leilão',principalRef:'SYNTHETIC QA comitente/líquido',ownVat:'55.17',ownVatRef:'SYNTHETIC QA IVApróprio transmissão identificado',scope:'SYNTHETIC QA bem/partes/operação/período',evidence:'SYNTHETIC QA pressupostos expressos',conditions:{...Object.fromEntries([...secondhandConditions,...secondhandCategoryConditions(c)].map(([k])=>[k,'yes'])),regimeConfirmed:'yes',compositionConfirmed:'yes'}};}
for(const t of secondhandTreatments)for(const c of secondhandCategories)test(t.branch+'/P125 '+c.id+' ownfacts,eligibleorigin and specificformula',()=>{
const r=row(t.id,c.id),result=evaluateSecondhand(r,'MZN');assert.equal(result.status,'determined');assert.equal(result.classification,'qualified_goods');assert.equal(result.baseAmount,t.id==='reseller_margin'?'138.00':'344.83');assert.equal(result.vat,null);assert.equal(result.rate,null);
for(const [k] of [...secondhandConditions,...secondhandCategoryConditions(c.id)]){const missing=structuredClone(r);delete missing.conditions[k];assert.equal(evaluateSecondhand(missing,'MZN').classification,'indeterminate');missing.conditions[k]='no';assert.equal(evaluateSecondhand(missing,'MZN').classification,'not_applicable');assert.equal(evaluateSecondhand(missing,'MZN').baseAmount,null);}
for(const field of ['description','operationRef','documentRef','goodsRef','operatorRef','categoryRef','scope','evidence'])assert.equal(evaluateSecondhand({...r,[field]:''},'MZN').classification,'indeterminate',field);
for(const patch of [{period:'2025-12'},{operationDate:'2026-11-12'},{documentDate:'2026-02-30'},{currency:'USD'},{sourceType:'legacy_35_42'},{sourceType:'other'},{sourceRef:''},{domesticPurchase:''},{domesticTaxable:''}])assert.equal(evaluateSecondhand({...r,...patch},'MZN').baseAmount,null);
assert.equal(evaluateSecondhand({...r,sourceType:'margin_reseller'},'MZN').status,'determined');assert.equal(evaluateSecondhand({...r,sourceType:'exempt_9_12_e'},'MZN').status,'determined');
if(t.id==='reseller_margin'){assert.equal(evaluateSecondhand({...r,regimeChoice:'general'},'MZN').baseAmount,null);for(const field of ['grossSale','purchasePrice','purchaseRef'])assert.equal(evaluateSecondhand({...r,[field]:''},'MZN').baseAmount,null);}
else{for(const field of ['grossInvoice','hammerPrice','principalCommission','ownVat','ownVatRef','auctionRef','principalRef'])assert.equal(evaluateSecondhand({...r,[field]:''},'MZN').baseAmount,null);assert.equal(evaluateSecondhand({...r,principalCommission:'1001'},'MZN').baseAmount,null);assert.equal(evaluateSecondhand({...r,ownVat:'401'},'MZN').baseAmount,null);}
});
test('P092 rounding towholeunit,zero vsunknown and negative individualmargin no compensation',()=>{
const r=row('reseller_margin','used_goods');assert.equal(evaluateSecondhand({...r,grossSale:'1000.58'},'MZN').baseAmount,'1.00');assert.equal(evaluateSecondhand({...r,grossSale:'1000.579999'},'MZN').baseAmount,'0.00');assert.equal(evaluateSecondhand({...r,grossSale:'1000'},'MZN').baseAmount,'0.00');
const loss=evaluateSecondhand({...r,grossSale:'900'},'MZN');assert.equal(loss.grossMargin,'-100.00');assert.equal(loss.baseAmount,'0.00');assert.match(loss.reason,/sem compensar/);assert.equal(evaluateSecondhand({...r,purchasePrice:''},'MZN').baseAmount,null);
const positive=evaluateSecondhand(r,'MZN');assert.equal(positive.baseAmount,'138.00');assert.equal(positive.baseDivisorRate,'16%');assert.equal(positive.vat,null);
});
test('P125 boundaries and both switches preserve hidden values withoutautomaticregime',()=>{
const art=row('reseller_margin','art');assert.equal(evaluateSecondhand({...art,editionCount:'201'},'MZN').classification,'not_applicable');assert.equal(evaluateSecondhand({...art,editionCount:''},'MZN').classification,'indeterminate');assert.equal(evaluateSecondhand({...art,artSubtype:'ceramic',ceramicUniqueSigned:''},'MZN').baseAmount,null);
const antique=row('reseller_margin','antique');assert.equal(evaluateSecondhand({...antique,ageYears:'100'},'MZN').classification,'not_applicable');assert.equal(evaluateSecondhand({...antique,ageYears:'100.1'},'MZN').classification,'qualified_goods');
const changed=switchSecondhandTreatment(art,'auction_margin');assert.equal(changed.grossSale,art.grossSale);assert.equal(changed.ownVat,art.ownVat);assert.equal(changed.conditions.compositionConfirmed,'');assert.equal(evaluateSecondhand(changed,'MZN').baseAmount,null);const cat=switchSecondhandCategory(art,'collection');assert.equal(cat.editionCount,'200');assert.equal(cat.conditions.collectionNature,undefined);
});
test('All8individualgoods persistwithhistory/report;all priorbooks preserved,noaggregateoffset',()=>{
const rows=secondhandTreatments.flatMap(t=>secondhandCategories.map(c=>row(t.id,c.id)));let book=recordSecondhands(draft,{rows});book=recordSecondhands(draft,book);assert.equal(book.history.length,1);const next={...draft,modelo_a_snapshot:saveSecondhandBook(draft,book)};assert.deepEqual(secondhandBook(next),book);assert.ok(secondhandCurrent(next,book));assert.equal(secondhandCurrent({...next,currency:'USD'},book),false);assert.equal(secondhandReportRows(next).length,8);
const store=JSON.parse(next.modelo_a_snapshot);delete store.regulatedSecondhands;assert.deepEqual(store,JSON.parse(frozen.modelo_a_snapshot));assert.throws(()=>recordSecondhands(next,{rows:[rows[0],rows[0]]}));assert.throws(()=>secondhandBook({...next,modelo_a_snapshot:JSON.stringify({...JSON.parse(next.modelo_a_snapshot),regulatedSecondhands:[]})}));if(process.env.IVA_SECONDHAND_FIXTURE)fs.writeFileSync(process.env.IVA_SECONDHAND_FIXTURE,JSON.stringify(next,null,2)+'\n');
});