feat: apply investment credit five-exercise window
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-10 15:35:24 +02:00
parent 89e6d72c1e
commit d5cb41b111
12 changed files with 274 additions and 20 deletions
@@ -0,0 +1,50 @@
<?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 IrpcInvestmentWindowDraftTest extends TestCase
{
use DatabaseTransactions;
public function test_derived_national_fraction_round_trips_through_the_existing_private_draft_writer(): 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 (['fifth', 'expired'] as $name) {
$snapshot = json_decode(file_get_contents(base_path('outputs/irpc-cbf15-window-20261110/'.$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']);
$this->assertSame($snapshot['business_plan_snapshot'], $back['business_plan_snapshot']);
$book = json_decode($back['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR)['irpcAnnual'][$snapshot['context_opportunity']];
$this->assertSame(2, $book['version']);
$this->assertCount(array_search($name, ['fifth', 'expired'], true) + 5, $book['history']);
$model = $book['result']['model'];
$this->assertSame($name === 'fifth' ? 'active' : 'expired', $model['investmentCredit']['window']['status']);
$this->assertSame('IRPC-CBF15/2', $model['investmentCredit']['version']);
$this->assertSame(['numerator' => $name === 'fifth' ? '200' : '0', 'denominator' => '1'], $model['investmentCredit']['deducted']);
$this->assertSame(['numerator' => $name === 'fifth' ? '500' : '700', 'denominator' => '1'], $model['arithmeticBalance']);
file_put_contents(base_path('outputs/irpc-cbf15-window-20261110/recovered-'.$name.'.json'), json_encode($back, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
}
}
}
@@ -84,7 +84,7 @@ void test('CBF15 requires actual project,source and first-exercise conditions wi
{ soleProject: '' },
{ noOtherBenefits: 'no' },
{ assessmentKey: 'stale' },
{ startDate: '2025-01-01' },
{ startDate: '2027-01-01' },
]) {
const c = investmentCase();
Object.assign(c.b.investmentCredit, patch);
@@ -188,7 +188,7 @@ void test('CBF15 calculation-save-recovery-HTML and dossier reports retain sourc
numerator: '200',
denominator: '1',
});
assert.match(annualHtml(d), /IRPC-CBF15\/1/);
assert.match(annualHtml(d), /IRPC-CBF15\/2/);
assert.match(
JSON.stringify(
createProjectDossierReport(d, {
@@ -196,7 +196,7 @@ void test('CBF15 calculation-save-recovery-HTML and dossier reports retain sourc
generatedAt: '2026-11-10T19:00:00+02:00',
}),
),
/IRPC-CBF15\/1/,
/IRPC-CBF15\/2/,
);
const old = internationalCase();
assert.equal(calculateAnnual(old.d, old.b).investmentCredit, undefined);
@@ -0,0 +1,106 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { investmentCase } from './helpers/irpc-investment-case.mjs';
import {
calculateAnnual,
recordAnnual,
saveAnnualBook,
annualBook,
annualCurrent,
annualHtml,
} from '../../resources/js/lib/irpc-annual-settlement.ts';
import { createProjectDossierReport } from '../../resources/js/lib/project-dossier-report.ts';
void test('CBF15 includes first through fifth civil fiscal exercises, including a late-year start', () => {
for (const [start, exercise] of [
['2026-06-01', 1],
['2025-12-31', 2],
['2024-12-31', 3],
['2023-12-31', 4],
['2022-12-31', 5],
]) {
const c = investmentCase();
c.b.investmentCredit.startDate = start;
const m = calculateAnnual(c.d, c.b);
assert.deepEqual(m.pending, []);
assert.equal(m.investmentCredit.window.exercise, exercise);
assert.equal(m.investmentCredit.window.status, 'active');
assert.equal(
m.investmentCredit.window.lastYear,
Number(start.slice(0, 4)) + 4,
);
assert.deepEqual(m.investmentCredit.deducted, {
numerator: '500',
denominator: '1',
});
}
});
void test('CBF15 sixth exercise expires without erasing actual investment or creating a carry/refund', () => {
const c = investmentCase();
c.b.investmentCredit.startDate = '2021-12-31';
const m = calculateAnnual(c.d, c.b);
assert.deepEqual(m.pending, []);
assert.equal(m.investmentCredit.window.exercise, 6);
assert.equal(m.investmentCredit.window.status, 'expired');
assert.deepEqual(m.investmentCredit.deducted, {
numerator: '0',
denominator: '1',
});
assert.deepEqual(m.arithmeticBalance, {
numerator: '700',
denominator: '1',
});
assert.equal(m.investmentCredit.rows[0].input.amount, '10000');
assert.match(m.investmentCredit.rows[0].reason, /quinto/);
assert.equal(m.investmentCredit.carryCreated, false);
});
void test('CBF15 does not admit future/invalid start dates or import older assets as current-year investments', () => {
for (const start of ['2027-01-01', '2025-02-29', '0000-01-01', '']) {
const c = investmentCase();
c.b.investmentCredit.startDate = start;
assert.equal(calculateAnnual(c.d, c.b).arithmeticBalance, null);
}
const c = investmentCase();
c.b.investmentCredit.startDate = '2022-01-01';
c.b.investmentCredit.assets[0].date = '2025-12-31';
assert.equal(calculateAnnual(c.d, c.b).arithmeticBalance, null);
});
void test('CBF15 existing project uses procedural start; new project can invest before operations within the same window', () => {
const c = investmentCase();
c.b.investmentCredit.startKind = 'existing';
c.b.investmentCredit.startDate = '2022-12-31';
assert.deepEqual(calculateAnnual(c.d, c.b).pending, []);
c.b.investmentCredit.startDate = '2026-06-01';
assert.equal(calculateAnnual(c.d, c.b).arithmeticBalance, null);
c.b.investmentCredit.startKind = 'new';
assert.deepEqual(calculateAnnual(c.d, c.b).pending, []);
});
void test('CBF15 fifth-year then expiry recovers and reports window while preserving earlier result', () => {
const c = investmentCase();
c.b.investmentCredit.startDate = '2022-12-31';
let b = recordAnnual(c.d, c.b, '2026-11-10T14:00:00Z');
b.investmentCredit.startDate = '2021-12-31';
b = recordAnnual(c.d, b, '2026-11-10T14:01:00Z');
const d = { ...c.d, modelo_a_snapshot: saveAnnualBook(c.d, b) },
back = annualBook(d);
assert.equal(annualCurrent(d, back), true);
assert.equal(back.history.length, 1);
assert.deepEqual(back.history[0].model.investmentCredit.deducted, {
numerator: '500',
denominator: '1',
});
assert.deepEqual(
calculateAnnual(d, back).investmentCredit,
back.result.model.investmentCredit,
);
assert.match(annualHtml(d), /JanelaCBF15\/1/);
assert.match(annualHtml(d), /prazo expirado/);
assert.match(
JSON.stringify(
createProjectDossierReport(d, {
selectedSections: new Set(['finance']),
generatedAt: '2026-11-10T14:01:00Z',
}),
),
/JanelaCBF15/,
);
});