Completar combinação CBF15 com saldos anteriores e conferir PEC residual
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-11 07:35:04 +02:00
parent 67cd583b94
commit 9c067d6ae8
10 changed files with 357 additions and 18 deletions
@@ -0,0 +1,46 @@
<?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 IrpcInvestmentCombinationDraftTest extends TestCase
{
use DatabaseTransactions;
public function test_combination_and_explicit_pe_c_reconciliation_survive_private_draft_recovery(): void
{
$target = DB::selectOne('select current_database() as name,inet_server_port() as port');
$this->assertSame('proactive_investment_20261027_test', $target->name);
$this->assertSame(55441, (int) $target->port);
config(['investor-path.enabled' => true, 'suitecrm.enabled' => false]);
$this->actingAs(User::findOrFail(1021));
$id = null;
$etag = null;
foreach (['combined' => 138, 'quota' => 438, 'overallocated' => null, 'reconciled' => -500, 'expired' => 538] as $name => $balance) {
$snapshot = json_decode(file_get_contents(base_path('outputs/irpc-cbf15-combination-20261111/'.$name.'.json')), true, flags: JSON_THROW_ON_ERROR);
$personal = ['person_type' => $snapshot['person_type']];
unset($snapshot['person_type']);
$payload = ['personal_snapshot' => $personal, 'economic_snapshot' => $snapshot];
if ($id === null) {
$id = $this->postJson('/api/v1/simulation-drafts', $payload, ['Idempotency-Key' => (string) Str::uuid()])->assertCreated()->json('data.id');
} else {
$this->putJson('/api/v1/simulation-drafts/'.$id, $payload, ['If-Match' => $etag])->assertOk();
}
$response = $this->getJson('/api/v1/simulation-drafts/'.$id)->assertOk();
$etag = $response->headers->get('ETag');
$back = $response->json('data.economic_snapshot');
$this->assertSame($snapshot['modelo_a_snapshot'], $back['modelo_a_snapshot']);
$book = json_decode($back['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR)['irpcAnnual'][$snapshot['context_opportunity']];
$this->assertCount(1, $book['history']);
$this->assertSame(['numerator' => '138', 'denominator' => '1'], $book['history'][0]['model']['arithmeticBalance']);
$this->assertSame($balance === null ? null : ['numerator' => (string) $balance, 'denominator' => '1'], $book['result']['model']['arithmeticBalance']);
$this->assertFalse($book['result']['model']['investmentCredit']['officialBalancesConsumed']);
file_put_contents(base_path('outputs/irpc-cbf15-combination-20261111/recovered-'.$name.'.json'), json_encode($back, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
}
}
}
@@ -0,0 +1,14 @@
import { economicCase } from "./irpc-economic-case.mjs";
import { investmentCarryCase } from "./irpc-investment-carry-case.mjs";
import {
prepareInvestmentCarry,
} from "../../../resources/js/lib/irpc-annual-settlement.ts";
export function combinedCase() {
const c = economicCase(true);
c.b = prepareInvestmentCarry(c.d, c.b);
c.b.investmentCredit = structuredClone(
investmentCarryCase().b.investmentCredit,
);
c.b.investmentCredit.assessmentKey = c.b.assessmentKey;
return c;
}
@@ -0,0 +1,101 @@
import test from "node:test";
import assert from "node:assert/strict";
import { investmentCase } from "./helpers/irpc-investment-case.mjs";
import { combinedCase } from "./helpers/irpc-investment-combination-case.mjs";
import {
calculateAnnual,
recordAnnual,
annualCurrent,
annualBook,
saveAnnualBook,
annualHtml,
} from "../../resources/js/lib/irpc-annual-settlement.ts";
const f = (n) => ({ numerator: String(n), denominator: "1" });
test("CBF15 carry combines with economic/current and prior international/PEC once in statutory order", () => {
const c = combinedCase(),
m = calculateAnnual(c.d, c.b);
assert.deepEqual(m.pending, []);
assert.deepEqual(m.investmentCredit.deducted, f(400));
assert.deepEqual(m.arithmeticBalance, f(138));
});
test("explicit current and prior PEC allocation cannot exceed collecta left after CBF15", () => {
const c = combinedCase();
Object.assign(c.b.investmentCredit.assets[0], {
amount: "40000",
proofAmount: "40000",
});
const m = calculateAnnual(c.d, c.b);
assert.equal(
m.arithmeticBalance,
null,
"An excessive PEC allocation must not manufacture a recoverable balance",
);
assert.match(m.pending.join(" "), /PEC.*CBF15/);
});
test("documented residual quota and expired original window retain distinct unused credits", () => {
const c = combinedCase();
c.b.investmentCredit.soleProject = "no";
c.b.investmentCredit.projectQuota = {
amount: "100",
reference:
"SYNTHETIC residual collecta allocated to this project after prior credits",
};
let m = calculateAnnual(c.d, c.b);
assert.deepEqual(m.pending, []);
assert.deepEqual(m.arithmeticBalance, f(438));
assert.deepEqual(m.investmentCredit.unused, f(300));
c.b.investmentCredit.projectQuota.amount = "1339";
assert.match(
calculateAnnual(c.d, c.b).pending.join(" "),
/Quota residualCBF15/,
);
c.b.investmentCredit.soleProject = "yes";
c.b.investmentCredit.startDate = "2021-01-01";
m = calculateAnnual(c.d, c.b);
assert.deepEqual(m.pending, []);
assert.deepEqual(m.investmentCredit.expired, f(400));
assert.deepEqual(m.arithmeticBalance, f(538));
});
test("explicit zero PEC allocation after exhausted collecta preserves prior PEC and recovers only PPC", () => {
const c = combinedCase();
Object.assign(c.b.investmentCredit.assets[0], {
amount: "40000",
proofAmount: "40000",
});
c.b.creditCarry.currentPec = "0";
c.b.creditCarry.lots[1].selected = "0";
const m = calculateAnnual(c.d, c.b);
assert.deepEqual(m.pending, []);
assert.deepEqual(m.investmentCredit.deducted, f(1338));
assert.deepEqual(m.pecDeduction.deducted, f(0));
assert.deepEqual(m.creditCarry.priorPec, f(0));
assert.deepEqual(m.arithmeticBalance, f(-500));
assert.equal(m.creditCarry.officialBalancesConsumed, false);
const b = recordAnnual(c.d, c.b, "2026-11-11T09:00:00+02:00"),
d = { ...c.d, modelo_a_snapshot: saveAnnualBook(c.d, b) },
back = annualBook(d);
assert.equal(annualCurrent(d, back), true);
assert.deepEqual(
calculateAnnual(d, back).investmentCredit,
m.investmentCredit,
);
assert.match(annualHtml(d), /CBF15/);
});
test("current CBF15 and prior international/PEC obey the same residual collecta guard", () => {
const c = combinedCase();
c.b.version = 2;
c.b.investmentCredit = structuredClone(investmentCase().b.investmentCredit);
c.b.investmentCredit.assessmentKey = c.b.assessmentKey;
let m = calculateAnnual(c.d, c.b);
assert.deepEqual(m.pending, []);
assert.deepEqual(m.arithmeticBalance, f(38));
Object.assign(c.b.investmentCredit.assets[0], {
amount: "40000",
proofAmount: "40000",
});
m = calculateAnnual(c.d, c.b);
assert.equal(m.arithmeticBalance, null);
assert.match(m.pending.join(" "), /PEC.*CBF15/);
});