feat: calculate qualified investment credits in annual settlement
tests / ci (push) Canceled after 0s
tests / ci (push) Canceled after 0s
This commit is contained in:
@@ -1,3 +1,13 @@
|
||||
import {
|
||||
newInvestmentCredit,
|
||||
validateInvestmentCredit,
|
||||
calculateInvestmentCredit,
|
||||
deductInvestmentCredit,
|
||||
investmentCreditSource,
|
||||
investmentAssetIdentity,
|
||||
investmentAssetClaimed,
|
||||
type InvestmentCredit,
|
||||
} from './irpc-investment-credit.ts';
|
||||
import { inputText } from './input-text.ts';
|
||||
import {
|
||||
carryPlan,
|
||||
@@ -23,6 +33,7 @@ import {
|
||||
requireProofParcel,
|
||||
parcelProjection,
|
||||
overlappingProofParcels,
|
||||
canonicalParcel,
|
||||
type FiscalProofParcel,
|
||||
} from './fiscal-proof-parcel.ts';
|
||||
import {
|
||||
@@ -32,6 +43,8 @@ import {
|
||||
internationalPosition,
|
||||
internationalDeduction,
|
||||
internationalCreditSource,
|
||||
creditMoney,
|
||||
creditCompare,
|
||||
newInternationalCredits,
|
||||
type InternationalCredits,
|
||||
type InternationalTaxProof,
|
||||
@@ -93,7 +106,7 @@ type AnnualRecord = {
|
||||
model: AnnualModel;
|
||||
};
|
||||
export type AnnualBook = {
|
||||
version: 1;
|
||||
version: 1 | 2;
|
||||
subjectNuit: string;
|
||||
year: string;
|
||||
reference: string;
|
||||
@@ -108,6 +121,7 @@ export type AnnualBook = {
|
||||
internationalCredits?: InternationalCredits;
|
||||
creditCarry?: CreditCarry;
|
||||
economicCredits?: EconomicCredits;
|
||||
investmentCredit?: InvestmentCredit;
|
||||
result?: AnnualRecord;
|
||||
history: AnnualRecord[];
|
||||
};
|
||||
@@ -131,9 +145,25 @@ export const annualSource = (b: AnnualBook) => {
|
||||
(b.pecPayments ? ' ' + pecSource : '') +
|
||||
(b.internationalCredits ? ' ' + internationalCreditSource : '') +
|
||||
(b.creditCarry ? ' ' + carrySource : '') +
|
||||
(b.economicCredits ? ' ' + economicSource : '')
|
||||
(b.economicCredits ? ' ' + economicSource : '') +
|
||||
(b.investmentCredit ? ' ' + investmentCreditSource : '')
|
||||
);
|
||||
};
|
||||
export function prepareInvestmentCredit(
|
||||
d: Record<string, unknown>,
|
||||
b: AnnualBook,
|
||||
): AnnualBook {
|
||||
if (b.investmentCredit) return b;
|
||||
const next = prepareInternationalBook(b),
|
||||
c = newInvestmentCredit(),
|
||||
a = annualAssessment(d);
|
||||
c.subjectNuit = b.subjectNuit;
|
||||
c.year = b.year;
|
||||
c.assessmentKey = b.assessmentKey;
|
||||
c.qualification.from = a.period?.from ?? '';
|
||||
c.qualification.to = a.period?.to ?? '';
|
||||
return { ...next, version: 2, investmentCredit: c };
|
||||
}
|
||||
export function prepareEconomicBook(b: AnnualBook): AnnualBook {
|
||||
if (b.economicCredits) return b;
|
||||
const next = prepareInternationalBook(b),
|
||||
@@ -240,6 +270,8 @@ function books(d: Record<string, unknown>): Record<string, AnnualBook> {
|
||||
return m;
|
||||
}
|
||||
export function validateAnnual(b: AnnualBook) {
|
||||
if (b?.investmentCredit !== undefined)
|
||||
validateInvestmentCredit(b.investmentCredit);
|
||||
if (b?.economicCredits !== undefined) validateEconomic(b.economicCredits);
|
||||
if (b?.creditCarry !== undefined) validateCreditCarry(b.creditCarry);
|
||||
if (b?.pecPayments !== undefined) validatePec(b.pecPayments);
|
||||
@@ -247,7 +279,9 @@ export function validateAnnual(b: AnnualBook) {
|
||||
validateInternational(b.internationalCredits);
|
||||
if (
|
||||
!b ||
|
||||
b.version !== 1 ||
|
||||
![1, 2].includes(b.version) ||
|
||||
(b.version === 2 && b.investmentCredit === undefined) ||
|
||||
(b.investmentCredit !== undefined && b.version !== 2) ||
|
||||
[
|
||||
'subjectNuit',
|
||||
'year',
|
||||
@@ -467,7 +501,7 @@ export function reuseAnnualWithholding(
|
||||
},
|
||||
};
|
||||
}
|
||||
function dependencies(d: Record<string, unknown>) {
|
||||
function dependencies(d: Record<string, unknown>, includeInvestment = false) {
|
||||
let normal: unknown, L5: unknown;
|
||||
try {
|
||||
const a = annualAssessment(d);
|
||||
@@ -496,6 +530,9 @@ function dependencies(d: Record<string, unknown>) {
|
||||
...(b.internationalCredits ? [b.internationalCredits] : []),
|
||||
...(b.creditCarry ? [b.creditCarry] : []),
|
||||
...(b.economicCredits ? [b.economicCredits] : []),
|
||||
...(includeInvestment && b.investmentCredit
|
||||
? [b.investmentCredit]
|
||||
: []),
|
||||
]),
|
||||
];
|
||||
}
|
||||
@@ -505,7 +542,7 @@ function key(d: Record<string, unknown>, b: AnnualBook) {
|
||||
annualSettlementVersion,
|
||||
acquisitionScope(d),
|
||||
input,
|
||||
dependencies(d),
|
||||
dependencies(d, !!b.investmentCredit),
|
||||
]);
|
||||
}
|
||||
export function annualCurrent(d: Record<string, unknown>, b: AnnualBook) {
|
||||
@@ -809,6 +846,7 @@ export function calculateAnnual(d: Record<string, unknown>, b: AnnualBook) {
|
||||
...internationalGroupPending(
|
||||
b.internationalCredits,
|
||||
!!b.economicCredits,
|
||||
!!b.investmentCredit,
|
||||
),
|
||||
);
|
||||
const countries = b.internationalCredits.positions.map(
|
||||
@@ -1230,6 +1268,122 @@ export function calculateAnnual(d: Record<string, unknown>, b: AnnualBook) {
|
||||
'Crédito económico delimitado à colecta normal corrente;efeitos L7 exigem correspondência própria.',
|
||||
);
|
||||
}
|
||||
const investmentPreview =
|
||||
b.investmentCredit && assessment?.record
|
||||
? calculateInvestmentCredit(
|
||||
b.investmentCredit,
|
||||
assessment.book,
|
||||
assessment.record.model,
|
||||
assessment.key,
|
||||
assessment.current,
|
||||
)
|
||||
: null;
|
||||
if (b.investmentCredit && !investmentPreview)
|
||||
pending.push('CréditoCBF15 exige colecta L6 recuperável.');
|
||||
if (investmentPreview) pending.push(...investmentPreview.pending);
|
||||
if (
|
||||
investmentPreview?.available &&
|
||||
cmp(investmentPreview.available, zero()) > 0n &&
|
||||
(!investmentPreview.projectCeiling ||
|
||||
cmp(investmentPreview.projectCeiling, zero()) > 0n) &&
|
||||
b.creditCarry?.lots.length
|
||||
)
|
||||
pending.push(
|
||||
'CombinaçãoCBF15 com saldos anteriores exige correspondência própria; transportes anteriores preservados.',
|
||||
);
|
||||
if (
|
||||
b.investmentCredit?.qualification.state === 'known' &&
|
||||
b.investmentCredit.qualification.eligible === 'yes' &&
|
||||
investmentPreview?.nominal &&
|
||||
cmp(investmentPreview.nominal, zero()) > 0n
|
||||
) {
|
||||
for (const [scope, other] of Object.entries(books(d))) {
|
||||
if (
|
||||
scope === acquisitionScope(d) ||
|
||||
other.subjectNuit !== b.subjectNuit ||
|
||||
other.year !== b.year ||
|
||||
!other.investmentCredit
|
||||
)
|
||||
continue;
|
||||
try {
|
||||
validateInvestmentCredit(other.investmentCredit);
|
||||
const c = other.investmentCredit;
|
||||
if (
|
||||
c.qualification.state !== 'known' ||
|
||||
c.qualification.eligible !== 'yes'
|
||||
)
|
||||
continue;
|
||||
if (!c.assets.some(investmentAssetClaimed)) continue;
|
||||
for (const r of b.investmentCredit.assets.filter(
|
||||
investmentAssetClaimed,
|
||||
)) {
|
||||
const id = investmentAssetIdentity(r),
|
||||
others = c.assets.filter(
|
||||
(x) =>
|
||||
investmentAssetClaimed(x) &&
|
||||
investmentAssetIdentity(x) === id,
|
||||
);
|
||||
if (!others.length) continue;
|
||||
const proof = creditMoney(r.proofAmount);
|
||||
if (
|
||||
others.some(
|
||||
(x) =>
|
||||
creditCompare(
|
||||
creditMoney(x.proofAmount),
|
||||
proof,
|
||||
) !== 0n,
|
||||
)
|
||||
)
|
||||
pending.push(
|
||||
'Montante documentalCBF15 contraditório entre mapas; reconcilie a prova.',
|
||||
);
|
||||
const own = b.investmentCredit.assets.filter(
|
||||
(x) =>
|
||||
investmentAssetClaimed(x) &&
|
||||
investmentAssetIdentity(x) === id,
|
||||
);
|
||||
if (
|
||||
creditCompare(
|
||||
addFractions(
|
||||
[...own, ...others].map((x) =>
|
||||
creditMoney(x.amount),
|
||||
),
|
||||
),
|
||||
proof,
|
||||
) > 0n
|
||||
)
|
||||
pending.push(
|
||||
'InvestimentoCBF15 atribuído entre mapas ultrapassa o documento efectivo; não duplicar capacidade.',
|
||||
);
|
||||
}
|
||||
const sameProject =
|
||||
canonicalParcel(c.projectReference) ===
|
||||
canonicalParcel(b.investmentCredit.projectReference);
|
||||
const sharedAsset = c.assets
|
||||
.filter(investmentAssetClaimed)
|
||||
.some((r) =>
|
||||
b.investmentCredit!.assets.some((p) =>
|
||||
overlappingProofParcels(
|
||||
{ ...r, identity: investmentAssetIdentity(r) },
|
||||
{ ...p, identity: investmentAssetIdentity(p) },
|
||||
),
|
||||
),
|
||||
);
|
||||
if (sameProject || sharedAsset)
|
||||
pending.push(
|
||||
'CBF15 já preparado noutro mapa da entidade; consolide projecto/documentos sem duplicar: ' +
|
||||
scope,
|
||||
);
|
||||
} catch (e) {
|
||||
pending.push(
|
||||
'Outro mapaCBF15 não recuperável para conferir duplicação: ' +
|
||||
scope +
|
||||
'; ' +
|
||||
String(e),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
const economicPreview = b.economicCredits
|
||||
? economicDeduction(economicRows, colecta, true)
|
||||
: null;
|
||||
@@ -1286,6 +1440,27 @@ export function calculateAnnual(d: Record<string, unknown>, b: AnnualBook) {
|
||||
)
|
||||
: null;
|
||||
if (carry) pending.push(...carry.pending);
|
||||
if (investmentPreview?.projectCeiling && !pending.length) {
|
||||
const beforeInternational = economicPreview?.afterCredit ?? colecta;
|
||||
const preview = b.internationalCredits
|
||||
? internationalDeduction(
|
||||
positions,
|
||||
beforeInternational,
|
||||
true,
|
||||
carry?.international ?? undefined,
|
||||
)
|
||||
: null;
|
||||
const remaining = carry
|
||||
? subtractFractions(
|
||||
preview?.afterCredit ?? beforeInternational!,
|
||||
carry.priorInternational,
|
||||
)
|
||||
: (preview?.afterCredit ?? beforeInternational);
|
||||
if (!remaining || cmp(investmentPreview.projectCeiling, remaining) > 0n)
|
||||
pending.push(
|
||||
'Quota residualCBF15 do projecto excede a colecta após créditos económicos/internacionais; reconcilie a atribuição antes de deduzir.',
|
||||
);
|
||||
}
|
||||
const ready = pending.length === 0 && colecta !== null,
|
||||
economic = b.economicCredits
|
||||
? economicDeduction(economicRows, colecta, ready)
|
||||
@@ -1306,10 +1481,14 @@ export function calculateAnnual(d: Record<string, unknown>, b: AnnualBook) {
|
||||
carry.priorInternational,
|
||||
)
|
||||
: (international?.afterCredit ?? afterEconomic);
|
||||
const investment = investmentPreview
|
||||
? deductInvestmentCredit(investmentPreview, creditCollecta, ready)
|
||||
: null;
|
||||
const afterInvestment = investment?.afterCredit ?? creditCollecta;
|
||||
const pec = b.pecPayments
|
||||
? pecDeduction(
|
||||
pecAvailable,
|
||||
creditCollecta,
|
||||
afterInvestment,
|
||||
ready,
|
||||
carry?.pec ?? undefined,
|
||||
)
|
||||
@@ -1317,10 +1496,10 @@ export function calculateAnnual(d: Record<string, unknown>, b: AnnualBook) {
|
||||
const afterPec =
|
||||
ready && carry
|
||||
? subtractFractions(
|
||||
pec?.afterPec ?? creditCollecta!,
|
||||
pec?.afterPec ?? afterInvestment!,
|
||||
carry.priorPec,
|
||||
)
|
||||
: (pec?.afterPec ?? creditCollecta);
|
||||
: (pec?.afterPec ?? afterInvestment);
|
||||
const arithmeticBalance = ready
|
||||
? subtractFractions(
|
||||
subtractFractions(afterPec!, knownWithholdings),
|
||||
@@ -1387,6 +1566,7 @@ export function calculateAnnual(d: Record<string, unknown>, b: AnnualBook) {
|
||||
claims,
|
||||
...(economic ? { economicCredit: economic } : {}),
|
||||
internationalCredit: international,
|
||||
...(investment ? { investmentCredit: investment } : {}),
|
||||
pecDeduction: pec,
|
||||
...(carry
|
||||
? {
|
||||
@@ -1427,8 +1607,12 @@ export function calculateAnnual(d: Record<string, unknown>, b: AnnualBook) {
|
||||
? 'Crédito internacional do próprio exercício:fracção nacional derivada no caso L6 ordinário com parcela líquida correspondida;CDT e montantes MZN documentados;sem repartição presumida de custos/perdas,transporte ou consumo oficial. '
|
||||
: 'Crédito internacional do próprio exercício:fracção nacional,convenção e montantes MZN são documentados;sem cálculo presumido,transporte ou consumo oficial. ') +
|
||||
(b.economicCredits
|
||||
? 'Benefícios positivos ficam separados;crédito económico no detalhe próprio.'
|
||||
: 'Créditos económicos/benefícios positivos ficam separados.'),
|
||||
? b.investmentCredit
|
||||
? 'CBF15 do primeiro exercício no detalhe próprio;restantes benefícios positivos separados;crédito económico no detalhe próprio.'
|
||||
: 'Benefícios positivos ficam separados;crédito económico no detalhe próprio.'
|
||||
: b.investmentCredit
|
||||
? 'CBF15 do primeiro exercício no detalhe próprio;restantes créditos económicos/benefícios positivos separados.'
|
||||
: 'Créditos económicos/benefícios positivos ficam separados.'),
|
||||
]
|
||||
: []),
|
||||
b.economicCredits
|
||||
@@ -1815,7 +1999,9 @@ export function annualReportRows(
|
||||
m.input.internationalCredits?.benefits
|
||||
.state ?? '',
|
||||
) +
|
||||
'.',
|
||||
(m.investmentCredit
|
||||
? '. Resumo anterior dos benefícios conservado, não reaplicado; CBF15 no detalhe próprio.'
|
||||
: '.'),
|
||||
m.internationalCredit.source +
|
||||
'; ' +
|
||||
(m.input.internationalCredits?.reference ?? '') +
|
||||
@@ -1904,6 +2090,99 @@ export function annualReportRows(
|
||||
] as [string, string, string, string],
|
||||
]
|
||||
: []),
|
||||
...(m.investmentCredit
|
||||
? [
|
||||
...m.investmentCredit.rows.map(
|
||||
(r) =>
|
||||
[
|
||||
'IRPC — L8',
|
||||
'CBF15 — ' +
|
||||
r.input.issuer +
|
||||
' / ' +
|
||||
r.input.document +
|
||||
' / ' +
|
||||
r.input.parcel +
|
||||
'; data ' +
|
||||
r.input.date,
|
||||
'Valor declarado: ' +
|
||||
r.input.amount +
|
||||
' ' +
|
||||
r.input.currency +
|
||||
'; investimento elegível: ' +
|
||||
show(r.value),
|
||||
r.input.reference +
|
||||
'; ' +
|
||||
r.reason +
|
||||
'; parcela: ' +
|
||||
r.input.parcelReference +
|
||||
'; ' +
|
||||
r.pending.join(' | '),
|
||||
] as [string, string, string, string],
|
||||
),
|
||||
[
|
||||
'IRPC — L8',
|
||||
'CBF15 — crédito fiscal por investimento',
|
||||
'Investimento: ' +
|
||||
show(m.investmentCredit.investment) +
|
||||
'; taxa ' +
|
||||
(m.investmentCredit.rate === null
|
||||
? 'não aplicável'
|
||||
: m.investmentCredit.rate + '%') +
|
||||
'; nominal ' +
|
||||
show(m.investmentCredit.nominal) +
|
||||
'; uso anterior ' +
|
||||
show(m.investmentCredit.priorUse) +
|
||||
'; disponível ' +
|
||||
show(m.investmentCredit.available) +
|
||||
'; dedução ' +
|
||||
show(m.investmentCredit.deducted) +
|
||||
'; sem colecta neste exercício ' +
|
||||
show(m.investmentCredit.unused) +
|
||||
(m.investmentCredit.projectCeiling
|
||||
? '; quota documental do projecto ' +
|
||||
show(m.investmentCredit.projectCeiling)
|
||||
: '') +
|
||||
'; colecta após benefício ' +
|
||||
show(m.investmentCredit.afterCredit),
|
||||
m.investmentCredit.version +
|
||||
'; ' +
|
||||
m.investmentCredit.source +
|
||||
'; projecto ' +
|
||||
m.investmentCredit.input.projectReference +
|
||||
'; ' +
|
||||
m.investmentCredit.input.qualification.reference +
|
||||
'; período ' +
|
||||
m.investmentCredit.input.qualification.from +
|
||||
' a ' +
|
||||
m.investmentCredit.input.qualification.to +
|
||||
'; ' +
|
||||
m.investmentCredit.input.qualification
|
||||
.periodReference +
|
||||
'; ' +
|
||||
m.investmentCredit.input.locationReference +
|
||||
'; ' +
|
||||
'início ' +
|
||||
m.investmentCredit.input.startDate +
|
||||
'; ' +
|
||||
m.investmentCredit.input.startReference +
|
||||
'; reconhecimento ' +
|
||||
m.investmentCredit.input.recognitionReference +
|
||||
'; exclusividade ' +
|
||||
(m.investmentCredit.input.soleProject === 'no'
|
||||
? m.investmentCredit.input.projectQuota
|
||||
?.reference
|
||||
: m.investmentCredit.input
|
||||
.soleProjectReference) +
|
||||
'; outros benefícios: ' +
|
||||
m.investmentCredit.input
|
||||
.noOtherBenefitsReference +
|
||||
'; uso anterior: ' +
|
||||
m.investmentCredit.input.previousUse.reference +
|
||||
'; ' +
|
||||
m.investmentCredit.pending.join(' | '),
|
||||
] as [string, string, string, string],
|
||||
]
|
||||
: []),
|
||||
...(m.pecDeduction
|
||||
? [
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user