Implement P123 consignment and P126 rebuttable goods presumptions in QA
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?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 IvaGoodsPresumptionDraftTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function test_goodspresumptions_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-presumptions.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:355', $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['goodsPresumptions']['opportunity:355'];
|
||||
$this->assertCount(4, $book['rows']);
|
||||
$this->assertSame(['consignment_180','missing_goods','excess_consumption','found_goods'],array_column($book['rows'],'treatment'));
|
||||
$this->assertSame(['consignment_transmission','presumed_transmission','presumed_transmission','presumed_acquisition'],array_column($book['result']['rows'],'classification'));
|
||||
foreach ($book['result']['rows'] as $row) {
|
||||
$this->assertNull($row['baseAmount']);$this->assertNull($row['vat']);$this->assertNull($row['rate']);
|
||||
}
|
||||
$this->assertSame($book['rows'], $book['result']['inputs']);
|
||||
$this->assertCount(1, $book['history']);
|
||||
$this->assertSame('2026-11-28',$book['history'][0]['rows'][0]['deadline']);
|
||||
$frozen = json_decode(file_get_contents(base_path('tests/frontend/fixtures/iva-goods-presumptions-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, ['goodsPresumptions' => 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-presumptions-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,31 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import {newGoodsPresumption,presumptionConditions,goodsPresumptionTreatments,evaluateGoodsPresumption,recordGoodsPresumptions,saveGoodsPresumptionBook,goodsPresumptionBook,goodsPresumptionCurrent,goodsPresumptionReportRows,switchPresumptionTreatment} from '../../resources/js/lib/iva-goods-presumptions.ts';
|
||||
const frozen=JSON.parse(fs.readFileSync('tests/frontend/fixtures/iva-goods-presumptions-preserved.json','utf8'));
|
||||
const draft={...frozen,context_opportunity:'opportunity:355',project_title:'SYNTHETIC QA — Consignação e presunções / Manica'};
|
||||
function row(t){return{...newGoodsPresumption(t),treatment:t,description:'SYNTHETIC QA '+t,period:'2026-12',origin:'projected',operationDate:'2026-12-12',operationRef:'SYNTHETIC QA operação/conferência',documentRef:'SYNTHETIC QA documento/pressuposto',documentDate:'2026-12-12',goodsRef:'SYNTHETIC QA conjunto individual',scope:'SYNTHETIC QA todos os estabelecimentos/período/conjunto',evidence:'SYNTHETIC QA factos/pressupostos expressos',deliveryDate:'2026-06-01',recipientRef:'SYNTHETIC QA destinatário/entrega',inventoryRef:'SYNTHETIC QA levantamento todos estabelecimentos',productionRef:'SYNTHETIC QA volume de produção/comparação',quantity:'125.123456',quantityUnit:'kg',contraryProof:'no',contraryProofRef:'',conditions:Object.fromEntries(presumptionConditions(t).map(([k])=>[k,'yes']))};}
|
||||
for(const rule of goodsPresumptionTreatments)test(rule.branch+' '+rule.id+' own facts/references and no fiscal amount',()=>{
|
||||
const r=row(rule.id),result=evaluateGoodsPresumption(r);assert.equal(result.status,'determined');assert.equal(result.classification,rule.id==='consignment_180'?'consignment_transmission':rule.id==='found_goods'?'presumed_acquisition':'presumed_transmission');assert.equal(result.baseAmount,null);assert.equal(result.vat,null);assert.equal(result.rate,null);
|
||||
for(const[k]of presumptionConditions(rule.id)){const missing=structuredClone(r);delete missing.conditions[k];assert.equal(evaluateGoodsPresumption(missing).status,'indeterminate');missing.conditions[k]='no';assert.equal(evaluateGoodsPresumption(missing).status,'not_applicable');}
|
||||
for(const field of ['description','operationRef','documentRef','goodsRef','scope','evidence',...(rule.id==='consignment_180'?['deliveryDate','recipientRef']:['inventoryRef']),...(rule.id==='excess_consumption'?['productionRef','quantity','quantityUnit']:[])])assert.equal(evaluateGoodsPresumption({...r,[field]:''}).status,'indeterminate',field);
|
||||
for(const patch of [{operationDate:'2026-02-30'},{documentDate:'2026-02-30'},{period:'2025-12'},{period:'2026-11'}])assert.equal(evaluateGoodsPresumption({...r,...patch}).status,'indeterminate');
|
||||
});
|
||||
test('P123 exact180-day boundary,yearcrossing and chronology',()=>{
|
||||
const r=row('consignment_180');const day=n=>new Date(Date.parse(r.deliveryDate+'T00:00:00Z')+n*86400000).toISOString().slice(0,10);
|
||||
for(const n of [179,180,181]){const d=day(n),v=evaluateGoodsPresumption({...r,operationDate:d,period:d.slice(0,7)});assert.equal(v.elapsedDays,n);assert.equal(v.deadline,day(180));assert.equal(v.status,n>180?'determined':'not_applicable');}
|
||||
assert.equal(evaluateGoodsPresumption({...r,deliveryDate:'2025-12-31',operationDate:'2026-06-30',period:'2026-06'}).elapsedDays,181);
|
||||
assert.equal(evaluateGoodsPresumption({...r,deliveryDate:'2026-12-13'}).status,'indeterminate');
|
||||
const returned=structuredClone(r);returned.conditions.notReturned='no';assert.equal(evaluateGoodsPresumption(returned).status,'not_applicable');
|
||||
});
|
||||
test('P126 rebuttable presumption,unknown proof and scope retained',()=>{
|
||||
for(const t of ['missing_goods','excess_consumption','found_goods']){const r=row(t);assert.equal(evaluateGoodsPresumption({...r,contraryProof:''}).status,'indeterminate');assert.equal(evaluateGoodsPresumption({...r,contraryProof:'yes'}).status,'indeterminate');const v=evaluateGoodsPresumption({...r,contraryProof:'yes',contraryProofRef:'SYNTHETIC QA prova/conjunto/período'});assert.equal(v.classification,'rebutted');assert.equal(v.baseAmount,null);assert.equal(v.vat,null);}
|
||||
for(const quantity of ['','0','-1','1e3','1.1234567'])assert.equal(evaluateGoodsPresumption({...row('excess_consumption'),quantity}).status,'indeterminate');
|
||||
const switched=switchPresumptionTreatment({...row('excess_consumption'),contraryProof:'yes',contraryProofRef:'retained'},'consignment_180');assert.equal(switched.productionRef,row('excess_consumption').productionRef);assert.equal(switched.contraryProofRef,'retained');assert.equal(switched.contraryProof,'');assert.deepEqual(switched.conditions,{});
|
||||
});
|
||||
test('private draft/report/history preserve allpreviousbooks and opportunity',()=>{
|
||||
const rows=goodsPresumptionTreatments.map(t=>row(t.id)),first=recordGoodsPresumptions(draft,{rows});const next={...draft,modelo_a_snapshot:saveGoodsPresumptionBook(draft,first)};const second=recordGoodsPresumptions(next,first);next.modelo_a_snapshot=saveGoodsPresumptionBook(next,second);
|
||||
assert.equal(second.history.length,1);assert.deepEqual(second.history[0].inputs,rows);assert.equal(goodsPresumptionCurrent(next,second),true);assert.equal(goodsPresumptionCurrent(next,{...second,rows:rows.map((r,i)=>i? r:{...r,scope:'changed'})}),false);assert.equal(goodsPresumptionReportRows(next).length,4);assert.deepEqual(goodsPresumptionBook({...next,context_opportunity:'opportunity:356'}),{rows:[]});
|
||||
const store=JSON.parse(next.modelo_a_snapshot);delete store.goodsPresumptions;assert.deepEqual(store,JSON.parse(frozen.modelo_a_snapshot));assert.throws(()=>recordGoodsPresumptions(next,{rows:[rows[0],rows[0]]}));assert.throws(()=>goodsPresumptionBook({...next,modelo_a_snapshot:JSON.stringify({...store,goodsPresumptions:[]})}));
|
||||
if(process.env.IVA_GOODS_PRESUMPTIONS_FIXTURE)fs.writeFileSync(process.env.IVA_GOODS_PRESUMPTIONS_FIXTURE,JSON.stringify(next,null,2)+'\n');
|
||||
});
|
||||
Reference in New Issue
Block a user