Files
Investidor-V2/tools/irpc-ppc-obligation-proof.mjs
2026-11-11 10:37:33 +02:00

98 lines
3.6 KiB
JavaScript

import fs from 'node:fs';
import assert from 'node:assert/strict';
import { combinedCase } from '../tests/frontend/helpers/irpc-investment-combination-case.mjs';
import { plan } from '../tests/frontend/helpers/irpc-ppc-obligation-case.mjs';
import { calculatePpcObligation } from '../resources/js/lib/irpc-ppc-obligation.ts';
import {
recordAnnual,
saveAnnualBook,
annualBook,
annualCurrent,
annualHtml,
} from '../resources/js/lib/irpc-annual-settlement.ts';
import { createProjectDossierReport } from '../resources/js/lib/project-dossier-report.ts';
const out = new URL(
'../outputs/irpc-ppc-obligation-20261111/',
import.meta.url,
);
fs.mkdirSync(out, { recursive: true });
const scenarios = {
ordinary: {},
rounded: { previousTax: '100001' },
zero: { previousTax: '10000' },
unknown: { previousTax: '' },
other_period: { previousFullCalendarYear: 'no' },
};
for (const [name, changes] of Object.entries(scenarios)) {
if (process.argv[2] === 'generate') {
const { d, b } = combinedCase();
const prior = recordAnnual(d, b, '2026-11-11T10:00:00Z');
prior.ppcObligation = {
...plan(),
subjectNuit: b.subjectNuit,
year: b.year,
...changes,
};
const next = recordAnnual(d, prior, '2026-11-11T10:01:00Z');
d.modelo_a_snapshot = saveAnnualBook(d, next);
fs.writeFileSync(new URL(name + '.json', out), JSON.stringify(d));
} else {
const d = JSON.parse(
fs.readFileSync(
new URL('recovered-' + name + '.json', out),
'utf8',
),
),
b = annualBook(d),
m = calculatePpcObligation(
b.ppcObligation,
b.subjectNuit,
b.year,
b.assessment,
);
assert.equal(annualCurrent(d, b), true);
assert.equal(b.history.length, 1);
assert.deepEqual(b.result.model.arithmeticBalance, {
numerator: '138',
denominator: '1',
});
const html = annualHtml(d),
report = createProjectDossierReport(d, {
selectedSections: new Set(['finance']),
generatedAt: '2026-11-11T10:02:00Z',
});
assert.match(html, /PPC — estimativa fiscal/);
assert.match(JSON.stringify(report), /PPC — estimativa fiscal/);
assert.equal(m.paymentExecuted, false);
assert.equal(m.annualDeductionCreated, false);
if (name === 'ordinary') assert.equal(m.total.numerator, '72000');
if (name === 'rounded') assert.equal(m.total.numerator, '75000');
if (name === 'zero') assert.equal(m.total.numerator, '0');
if (['unknown', 'other_period'].includes(name))
assert.equal(m.total, null);
fs.writeFileSync(new URL(name + '-report.html', out), html);
fs.writeFileSync(
new URL(name + '-verification.json', out),
JSON.stringify(
{
synthetic: true,
apiRecovered: true,
current: true,
historyPreserved: true,
annualBalanceUnchanged: true,
report: true,
visualReview: false,
model: m,
},
null,
2,
),
);
}
}
console.log(
process.argv[2] === 'generate'
? 'Five synthetic PPC scenarios prepared.'
: 'Five native recovered PPC scenarios recalculated and reported without creating deductions.',
);