feat(iva): classify G4D onboard supplies with explicit beverage exceptions
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?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 IvaOnboardSupplyDraftTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function test_onboard_supplies_recover_with_the_selected_opportunity_and_previous_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('outputs/iva-l1-g4-supplies-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']);
|
||||
$this->assertSame('opportunity:326', $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['salesOperations']['opportunity:326'];
|
||||
$this->assertSame('opportunity:326', $book['result']['opportunity']);
|
||||
$this->assertSame('MZN', $book['result']['currency']);
|
||||
$this->assertSame('2026-12', $book['result']['periods'][0]['period']);
|
||||
$this->assertSame('400.00', $book['result']['periods'][0]['fields']['05']);
|
||||
$this->assertCount(4, $book['rows']);
|
||||
$this->assertSame(['provisions','fuel','beverages','cargo_products'], array_column($book['rows'], 'supplyCategory'));
|
||||
foreach ($book['result']['rows'] as $row) {
|
||||
$this->assertSame('05', $row['field']);
|
||||
$this->assertSame('0.00', $row['vat']);
|
||||
}
|
||||
$frozen = json_decode(file_get_contents(base_path('tests/frontend/fixtures/iva-g4c-inputs.json')), true, flags: JSON_THROW_ON_ERROR);
|
||||
foreach (json_decode($frozen['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR)['salesOperations'] as $context => $original) {
|
||||
$this->assertSame($original, $store['salesOperations'][$context]);
|
||||
}
|
||||
file_put_contents(base_path('outputs/iva-l1-g4-supplies-20261024/recovered.json'), json_encode($recovered, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,49 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import { onboardSupplyTreatments, supplyCategories, onboardSupplyIssue } from '../../resources/js/lib/iva-onboard-supplies.ts';
|
||||
import { newSalesOperation, evaluateSalesOperation, recordSales, saveSalesBook, salesBook, salesCurrent } from '../../resources/js/lib/iva-operations.ts';
|
||||
import { createProjectDossierReport } from '../../resources/js/lib/project-dossier-report.ts';
|
||||
const frozen=JSON.parse(fs.readFileSync(new URL('./fixtures/iva-g4c-inputs.json',import.meta.url),'utf8'));
|
||||
const positive=['provisions','fuel','beverages','cargo_products'];
|
||||
const row=(rule,i)=>({...newSalesOperation(`g4d-${i}`,'2026-12','MZN'),description:`SYNTHETIC QA — ${rule.label}`,establishment:'SYNTHETIC QA estabelecimento',amount:'100',basis:i%2?'included':'net',treatment:rule.id,supplyCategory:positive[i],confirmed:rule.conditions.map((_,n)=>n),evidence:'SYNTHETIC QA — documento/pressuposto identifica bens postos a bordo, destino, embarcação/avião e actividade do operador; projecção, sem certificar despacho nem restituição'});
|
||||
function fixture(){
|
||||
const draft={...structuredClone(frozen),context_opportunity:'opportunity:326',project_title:'SYNTHETIC QA — G4D / Abastecimentos a bordo'};
|
||||
const book=recordSales(draft,{currency:'MZN',rows:onboardSupplyTreatments.map(row),periods:[{period:'2026-12',complete:true}]});draft.modelo_a_snapshot=saveSalesBook(draft,book);return{draft,book};
|
||||
}
|
||||
test('four supply branches require each legal condition and identification; values with/without included VAT use existing motor',()=>{
|
||||
assert.equal(onboardSupplyTreatments.length,4);
|
||||
for(const[i,rule]of onboardSupplyTreatments.entries()){
|
||||
const input=row(rule,i),result=evaluateSalesOperation(input,'MZN');assert.equal(result.field,'05');assert.equal(result.vat,'0.00');assert.equal(result.net,'100.00');assert.equal(result.gross,'100.00');
|
||||
for(const missing of input.confirmed)assert.equal(evaluateSalesOperation({...input,confirmed:input.confirmed.filter(n=>n!==missing)},'MZN').vat,null);
|
||||
for(const patch of [{amount:''},{evidence:''},{sourceCurrency:'USD'},{period:'2027-01'},{basis:''},{description:''},{establishment:''}])assert.equal(evaluateSalesOperation({...input,...patch},'MZN').vat,null);
|
||||
}
|
||||
});
|
||||
test('article13 categories and exclusions are distinct: beverages do not cause automatic exemption or deduction',()=>{
|
||||
for(const[i,rule]of onboardSupplyTreatments.entries())for(const[category]of supplyCategories){
|
||||
const result=evaluateSalesOperation({...row(rule,i),supplyCategory:category},'MZN');
|
||||
const excluded=i===1&&(category==='provisions'||category==='beverages'),refund=category==='beverages'&&(i===0||i===3);
|
||||
if(excluded||refund){assert.equal(result.vat,null);assert.equal(result.net,null);assert.equal(result.field,null);assert.equal(result.rate,null);assert.match(result.reason,excluded?/excluídas/:/dedução ou restituição/);}
|
||||
else assert.equal(result.vat,'0.00');
|
||||
}
|
||||
assert.match(onboardSupplyIssue('onboard_international_aircraft','beverages'),/20\(1\)\(d\)/);
|
||||
assert.equal(onboardSupplyIssue('onboard_departing_warship','beverages'),null); // 13(2) lists c/d/h, not e.
|
||||
for(const category of [undefined,'',null,'unknown'])for(const rule of onboardSupplyTreatments){const result=evaluateSalesOperation({...row(rule,0),supplyCategory:category},'MZN');assert.equal(result.vat,null);assert.match(result.reason,/Identifique a natureza/);}
|
||||
});
|
||||
test('changing category preserves price and historical result; unresolved beverage/provisions never turn into zero totals',()=>{
|
||||
const{draft,book}=fixture();assert.equal(book.result.periods[0].fields['05'],'400.00');
|
||||
for(const[index,category]of [[0,'beverages'],[1,'provisions'],[3,'']]){
|
||||
const changed=recordSales(draft,{...book,rows:book.rows.map((r,i)=>i===index?{...r,supplyCategory:category,confirmed:[]}:r)});
|
||||
assert.equal(changed.rows[index].amount,'100');assert.equal(changed.rows[index].sourceCurrency,'MZN');assert.equal(changed.rows[index].period,'2026-12');
|
||||
assert.equal(changed.result.periods[0].fields['05'],null);assert.deepEqual(changed.history.at(-1),book.result);
|
||||
const edited={...draft,modelo_a_snapshot:saveSalesBook(draft,{...book,rows:changed.rows})};assert.equal(salesCurrent(edited,salesBook(edited)),false);
|
||||
}
|
||||
});
|
||||
test('QA snapshot and report retain category/context/source; all17 prior books stay identical/current without recalculation',()=>{
|
||||
const{draft,book}=fixture();assert.ok(salesCurrent(draft,book));assert.deepEqual(salesBook(JSON.parse(JSON.stringify(draft))),JSON.parse(JSON.stringify(book)));
|
||||
const before=JSON.parse(frozen.modelo_a_snapshot).salesOperations,after=JSON.parse(draft.modelo_a_snapshot).salesOperations;assert.equal(Object.keys(before).length,17);
|
||||
for(const[context,original]of Object.entries(before)){assert.deepEqual(after[context],original);assert.ok(salesCurrent({...draft,context_opportunity:context},after[context]));}
|
||||
const report=JSON.stringify(createProjectDossierReport(draft,{selectedSections:new Set(['finance']),version:'QA G4D'}));
|
||||
for(const token of ['opportunity:326','Manica','2026-12','400.00','13(1)(c)','13(1)(d)','13(1)(e)','13(1)(h)','Natureza do abastecimento','Bebidas como provisões','QA G4D'])assert.ok(report.includes(token),token);
|
||||
if(process.env.IVA_G4D_CALCULATED)fs.writeFileSync(process.env.IVA_G4D_CALCULATED,JSON.stringify(draft));
|
||||
});
|
||||
Reference in New Issue
Block a user