feat(finance): add explicit plan regime and PGC-NIRF advance basis in QA
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
buildSupplierAdvances,
|
||||
isSupplierAdvances,
|
||||
type SupplierAdvances,
|
||||
type AccountingRegime,
|
||||
} from './supplier-advances.ts';
|
||||
import {
|
||||
buildDayTermSchedule,
|
||||
@@ -97,6 +98,7 @@ export type BusinessFinancialPlanInput = {
|
||||
/** Explicit opt-in. Missing keeps all historical calculations unchanged. */
|
||||
dayTerms?: DayTerms;
|
||||
supplierAdvances?: SupplierAdvances;
|
||||
accountingRegime?: AccountingRegime;
|
||||
economicClassification?: {
|
||||
code: string;
|
||||
revision: string;
|
||||
@@ -217,27 +219,29 @@ export function serializeBusinessPlanSnapshot(
|
||||
): string {
|
||||
const snapshot = JSON.stringify({
|
||||
version:
|
||||
plan.supplierAdvances !== undefined
|
||||
? 21
|
||||
: plan.dayTerms !== undefined
|
||||
? 20
|
||||
: plan.startupAssetRequirements
|
||||
? 19
|
||||
: [
|
||||
'applyAnnualGrowth',
|
||||
'creditTermDays',
|
||||
'dividendsByPeriod',
|
||||
'operatingExpenseLines',
|
||||
'fundingBreakdown',
|
||||
'reportSource',
|
||||
].some(
|
||||
(key) =>
|
||||
plan[
|
||||
key as keyof BusinessFinancialPlanInput
|
||||
] !== undefined,
|
||||
)
|
||||
? 18
|
||||
: 17,
|
||||
plan.accountingRegime !== undefined
|
||||
? 22
|
||||
: plan.supplierAdvances !== undefined
|
||||
? 21
|
||||
: plan.dayTerms !== undefined
|
||||
? 20
|
||||
: plan.startupAssetRequirements
|
||||
? 19
|
||||
: [
|
||||
'applyAnnualGrowth',
|
||||
'creditTermDays',
|
||||
'dividendsByPeriod',
|
||||
'operatingExpenseLines',
|
||||
'fundingBreakdown',
|
||||
'reportSource',
|
||||
].some(
|
||||
(key) =>
|
||||
plan[
|
||||
key as keyof BusinessFinancialPlanInput
|
||||
] !== undefined,
|
||||
)
|
||||
? 18
|
||||
: 17,
|
||||
plan: {
|
||||
...plan,
|
||||
...(plan.useSupplierPaymentTerms === true
|
||||
@@ -323,7 +327,8 @@ export function restoreBusinessPlanSnapshot(
|
||||
decoded.version !== 18 &&
|
||||
decoded.version !== 19 &&
|
||||
decoded.version !== 20 &&
|
||||
decoded.version !== 21) ||
|
||||
decoded.version !== 21 &&
|
||||
decoded.version !== 22) ||
|
||||
!decoded.plan ||
|
||||
typeof decoded.plan !== 'object'
|
||||
)
|
||||
@@ -331,12 +336,13 @@ export function restoreBusinessPlanSnapshot(
|
||||
const plan = decoded.plan as Record<string, unknown>;
|
||||
if (
|
||||
plan.dayTerms !== undefined &&
|
||||
(![20, 21].includes(decoded.version) || !isDayTerms(plan.dayTerms))
|
||||
(![20, 21, 22].includes(decoded.version) ||
|
||||
!isDayTerms(plan.dayTerms))
|
||||
)
|
||||
return null;
|
||||
if (
|
||||
plan.supplierAdvances !== undefined &&
|
||||
(decoded.version !== 21 ||
|
||||
(![21, 22].includes(decoded.version) ||
|
||||
!isSupplierAdvances(plan.supplierAdvances))
|
||||
)
|
||||
return null;
|
||||
@@ -352,6 +358,31 @@ export function restoreBusinessPlanSnapshot(
|
||||
].some((key) => plan[key] !== undefined)
|
||||
)
|
||||
return null;
|
||||
if (
|
||||
plan.accountingRegime !== undefined &&
|
||||
(decoded.version !== 22 ||
|
||||
typeof plan.accountingRegime !== 'string' ||
|
||||
!['PGC-PE', 'PGC-NIRF'].includes(plan.accountingRegime))
|
||||
)
|
||||
return null;
|
||||
if (
|
||||
decoded.version === 22 &&
|
||||
(typeof plan.accountingRegime !== 'string' ||
|
||||
!['PGC-PE', 'PGC-NIRF'].includes(plan.accountingRegime))
|
||||
)
|
||||
return null;
|
||||
const advances = plan.supplierAdvances as SupplierAdvances | undefined;
|
||||
if (
|
||||
advances?.regime === 'PGC-NIRF' &&
|
||||
(decoded.version !== 22 || plan.accountingRegime !== 'PGC-NIRF')
|
||||
)
|
||||
return null;
|
||||
if (
|
||||
plan.accountingRegime &&
|
||||
advances?.regime &&
|
||||
plan.accountingRegime !== advances.regime
|
||||
)
|
||||
return null;
|
||||
const shortText = (v: unknown) =>
|
||||
typeof v === 'string' && v.length <= 160;
|
||||
if (plan.startupAssetRequirements !== undefined) {
|
||||
@@ -472,7 +503,7 @@ export function restoreBusinessPlanSnapshot(
|
||||
)
|
||||
return null;
|
||||
if (
|
||||
[12, 13, 14, 15, 16, 17, 20, 21].includes(
|
||||
[12, 13, 14, 15, 16, 17, 20, 21, 22].includes(
|
||||
Number(decoded.version),
|
||||
) &&
|
||||
(!Object.hasOwn(plan, 'inventoryPurchaseSchedule') ||
|
||||
@@ -485,7 +516,7 @@ export function restoreBusinessPlanSnapshot(
|
||||
plan.inventoryPurchaseSchedule !== null
|
||||
) {
|
||||
if (
|
||||
![12, 13, 14, 15, 16, 17, 20, 21].includes(
|
||||
![12, 13, 14, 15, 16, 17, 20, 21, 22].includes(
|
||||
Number(decoded.version),
|
||||
) ||
|
||||
!Array.isArray(plan.inventoryPurchaseSchedule) ||
|
||||
@@ -510,14 +541,18 @@ export function restoreBusinessPlanSnapshot(
|
||||
)
|
||||
return null;
|
||||
if (
|
||||
[13, 14, 15, 16, 17, 20, 21].includes(Number(decoded.version)) &&
|
||||
[13, 14, 15, 16, 17, 20, 21, 22].includes(
|
||||
Number(decoded.version),
|
||||
) &&
|
||||
!Object.hasOwn(plan, 'monthlyVariableCostPaymentsByPeriod')
|
||||
)
|
||||
return null;
|
||||
if (
|
||||
plan.monthlyVariableCostPaymentsByPeriod !== undefined &&
|
||||
plan.monthlyVariableCostPaymentsByPeriod !== null &&
|
||||
(![13, 14, 15, 16, 17, 20, 21].includes(Number(decoded.version)) ||
|
||||
(![13, 14, 15, 16, 17, 20, 21, 22].includes(
|
||||
Number(decoded.version),
|
||||
) ||
|
||||
!Array.isArray(plan.monthlyVariableCostPaymentsByPeriod) ||
|
||||
plan.monthlyVariableCostPaymentsByPeriod.length <
|
||||
Number(plan.periods) ||
|
||||
@@ -582,7 +617,7 @@ export function restoreBusinessPlanSnapshot(
|
||||
if (openingCollectionRate < 0n || openingCollectionRate > 100_000_000n)
|
||||
return null;
|
||||
if (
|
||||
[7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21].includes(
|
||||
[7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21, 22].includes(
|
||||
Number(decoded.version),
|
||||
)
|
||||
) {
|
||||
@@ -655,7 +690,7 @@ export function restoreBusinessPlanSnapshot(
|
||||
)
|
||||
return null;
|
||||
if (
|
||||
[8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21].includes(
|
||||
[8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21, 22].includes(
|
||||
Number(decoded.version),
|
||||
)
|
||||
) {
|
||||
@@ -694,7 +729,7 @@ export function restoreBusinessPlanSnapshot(
|
||||
const schedule = plan[field];
|
||||
if (
|
||||
schedule !== undefined &&
|
||||
(![11, 12, 13, 14, 15, 16, 17, 20, 21].includes(
|
||||
(![11, 12, 13, 14, 15, 16, 17, 20, 21, 22].includes(
|
||||
Number(decoded.version),
|
||||
) ||
|
||||
!Array.isArray(schedule) ||
|
||||
@@ -784,7 +819,7 @@ export function restoreBusinessPlanSnapshot(
|
||||
const costSchedule = productData.unitVariableCostByPeriod;
|
||||
const validMoneySchedule = (values: unknown) =>
|
||||
values === undefined ||
|
||||
([10, 11, 12, 13, 14, 15, 16, 17, 20, 21].includes(
|
||||
([10, 11, 12, 13, 14, 15, 16, 17, 20, 21, 22].includes(
|
||||
Number(decoded.version),
|
||||
) &&
|
||||
Array.isArray(values) &&
|
||||
@@ -793,9 +828,9 @@ export function restoreBusinessPlanSnapshot(
|
||||
values.every((value) => typeof value === 'string'));
|
||||
return (
|
||||
(schedule !== undefined &&
|
||||
(![9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21].includes(
|
||||
Number(decoded.version),
|
||||
) ||
|
||||
(![
|
||||
9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21, 22,
|
||||
].includes(Number(decoded.version)) ||
|
||||
!Array.isArray(schedule) ||
|
||||
schedule.length < Number(plan.periods) ||
|
||||
schedule.length > 60 ||
|
||||
@@ -832,7 +867,7 @@ export function restoreBusinessPlanSnapshot(
|
||||
entry.paymentLagMonths < 0 ||
|
||||
entry.paymentLagMonths > 12)) ||
|
||||
(entry.paymentInstallments !== undefined &&
|
||||
(![16, 17, 20, 21].includes(
|
||||
(![16, 17, 20, 21, 22].includes(
|
||||
Number(decoded.version),
|
||||
) ||
|
||||
!Array.isArray(
|
||||
@@ -1489,6 +1524,27 @@ type ActiveDayTerms = ReturnType<typeof buildDayTermSchedule> & {
|
||||
export function forecastBusinessPlan(input: BusinessFinancialPlanInput) {
|
||||
const config = input.dayTerms;
|
||||
const advances = input.supplierAdvances;
|
||||
if (
|
||||
input.accountingRegime !== undefined &&
|
||||
!['PGC-PE', 'PGC-NIRF'].includes(input.accountingRegime)
|
||||
)
|
||||
throw Error('Seleccione PGC-PE ou PGC-NIRF no plano.');
|
||||
if (
|
||||
advances?.regime === 'PGC-NIRF' &&
|
||||
input.accountingRegime !== 'PGC-NIRF'
|
||||
)
|
||||
throw Error(
|
||||
'Adiantamentos PGC-NIRF exigem identificação explícita do regime no plano.',
|
||||
);
|
||||
if (
|
||||
input.accountingRegime &&
|
||||
advances?.regime &&
|
||||
input.accountingRegime !== advances.regime
|
||||
)
|
||||
throw Error(
|
||||
'O regime do plano diverge da agenda de adiantamentos. Confirme a mudança de regime antes de recalcular.',
|
||||
);
|
||||
|
||||
if (config !== undefined && !isDayTerms(config))
|
||||
throw new Error('Prazos em dias: estrutura inválida.');
|
||||
if (advances !== undefined && !isSupplierAdvances(advances))
|
||||
@@ -3710,11 +3766,19 @@ export function exportBusinessPlanCsv(
|
||||
]),
|
||||
);
|
||||
}
|
||||
if (input.accountingRegime)
|
||||
rows.push(
|
||||
[],
|
||||
[
|
||||
'Regime contabilístico declarado no plano',
|
||||
input.accountingRegime,
|
||||
],
|
||||
);
|
||||
if (result.supplierAdvanceLedger) {
|
||||
rows.push(
|
||||
[],
|
||||
[
|
||||
'Adiantamentos por operação — PGC-PE; projecções sem IVA, câmbio, perdas ou penalizações',
|
||||
`Adiantamentos por operação — ${result.supplierAdvanceLedger.regime}; projecções sem IVA, câmbio, perdas ou penalizações`,
|
||||
],
|
||||
[
|
||||
'Operação',
|
||||
@@ -3760,6 +3824,34 @@ export function exportBusinessPlanCsv(
|
||||
]),
|
||||
),
|
||||
);
|
||||
if (result.supplierAdvanceLedger.regime === 'PGC-NIRF')
|
||||
rows.push(
|
||||
[],
|
||||
[
|
||||
'Operação',
|
||||
'Parcela',
|
||||
'Acordo formal',
|
||||
'Data do acordo',
|
||||
'Aplicação',
|
||||
'Fundamento',
|
||||
'Referência',
|
||||
],
|
||||
...result.supplierAdvanceLedger.operations.flatMap((o) =>
|
||||
o.events
|
||||
.filter((e) => e.kind === 'apply')
|
||||
.map((e) => [
|
||||
o.originId,
|
||||
o.parcelId,
|
||||
o.formalAgreement?.reference ?? '',
|
||||
o.formalAgreement?.date ?? '',
|
||||
e.date,
|
||||
e.applicationBasis?.kind === 'formal_agreement'
|
||||
? 'Acordo formal'
|
||||
: 'Factura',
|
||||
e.applicationBasis?.reference ?? '',
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
return `\uFEFF${rows
|
||||
.map((row) => row.map(csvCell).join(','))
|
||||
|
||||
Reference in New Issue
Block a user