feat: support public-payer treaty exemption and exact economic adjustments
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-10 13:54:06 +02:00
parent 60b52fc8c7
commit b10b35f308
16 changed files with 472 additions and 78 deletions
+20 -9
View File
@@ -1,3 +1,4 @@
import { creditLimit, creditCompare } from './irpc-international-credit.ts';
import type { NormalDepreciationModel } from './irpc-normal-depreciation.ts';
import type { NormalCostsModel } from './irpc-normal-costs.ts';
import type { SchedulesModel } from './irpc-normal-schedules.ts';
@@ -126,7 +127,13 @@ export type NormalProfitModel = {
source: string;
};
const zero = () => fraction(0n, 1n);
function money(s: string) {
function money(s: string, exact = false) {
if (exact) {
const v = creditLimit(s.startsWith('-') ? s.slice(1) : s);
return s.startsWith('-')
? fraction(-BigInt(v.numerator), BigInt(v.denominator))
: v;
}
if (!/^-?\d+(?:\.\d{1,2})?$/.test(s)) throw Error('Montante inválido');
const v = decimalFraction(s.startsWith('-') ? s.slice(1) : s);
return s.startsWith('-')
@@ -288,8 +295,11 @@ export function closeNormalProfit(
}
} else {
try {
value = money(t.amount);
if (units(value) !== 0n)
value = money(
t.amount,
key === 'incomeCorrections' && !!economic,
);
if (BigInt(value.numerator) !== 0n)
reason =
'Montante conhecido,mas tratamento específico ainda por corresponder;conservado sem aplicação presumida.';
else
@@ -304,23 +314,24 @@ export function closeNormalProfit(
...(costs?.effects ?? []),
...(economic?.effects ?? []),
].filter((e) => e.topic === key);
const mappedAmount = addFractions(
mapped.flatMap((e) => (e.amount ? [e.amount] : [])),
);
const covered =
(schedules || costs || economic) &&
mapped.length > 0 &&
mapped.every((e) => e.effect !== null) &&
value !== null &&
t.state === 'known' &&
units(
addFractions(
mapped.flatMap((e) => (e.amount ? [e.amount] : [])),
),
) === units(value);
(economic && key === 'incomeCorrections'
? creditCompare(mappedAmount, value) === 0n
: units(mappedAmount) === units(value));
if (covered)
reason =
'Montante reconciliado com as regras do mapa complementar; efeito fiscal discriminado por linha.';
if (
value === null ||
(units(value) !== 0n && !covered) ||
(BigInt(value.numerator) !== 0n && !covered) ||
(t.state === 'absent' && !!t.amount.trim()) ||
(mapped.length > 0 && !covered)
)