feat(finance): add explicit plan regime and PGC-NIRF advance basis in QA
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
export type AdvanceKind = 'purchases' | 'services';
|
||||
export type AdvanceEventKind = 'advance' | 'apply' | 'refund' | 'settle';
|
||||
export type AccountingRegime = 'PGC-PE' | 'PGC-NIRF';
|
||||
export type SupplierAdvanceOperation = {
|
||||
originId: string;
|
||||
parcelId: string;
|
||||
@@ -8,6 +9,7 @@ export type SupplierAdvanceOperation = {
|
||||
amount: string;
|
||||
recognitionDate: string;
|
||||
invoiceDate: string;
|
||||
formalAgreement?: { date: string; reference: string };
|
||||
ordinaryContract: boolean;
|
||||
events: {
|
||||
id: string;
|
||||
@@ -17,7 +19,7 @@ export type SupplierAdvanceOperation = {
|
||||
}[];
|
||||
};
|
||||
export type SupplierAdvances = {
|
||||
regime: '' | 'PGC-PE';
|
||||
regime: '' | AccountingRegime;
|
||||
purchases: boolean;
|
||||
services: boolean;
|
||||
operations: SupplierAdvanceOperation[];
|
||||
@@ -32,12 +34,22 @@ export const advanceEventLabels = {
|
||||
refund: 'Devolução do adiantamento',
|
||||
settle: 'Pagamento do remanescente',
|
||||
};
|
||||
export function advanceDocumentLabel(status: string) {
|
||||
const labels: Record<string, string> = {
|
||||
not_recognized: 'Ainda não reconhecido',
|
||||
invoice: 'Fornecimento facturado',
|
||||
formal_agreement: 'Fornecimento formalmente acordado',
|
||||
pending_documentation:
|
||||
'Fornecimento reconhecido, documentação por confirmar',
|
||||
};
|
||||
return labels[status] ?? 'Por confirmar';
|
||||
}
|
||||
const text = (v: unknown) => typeof v === 'string' && v.length <= 160;
|
||||
export function isSupplierAdvances(v: unknown): v is SupplierAdvances {
|
||||
if (!v || typeof v !== 'object') return false;
|
||||
const c = v as SupplierAdvances;
|
||||
return (
|
||||
['', 'PGC-PE'].includes(c.regime) &&
|
||||
['', 'PGC-PE', 'PGC-NIRF'].includes(c.regime) &&
|
||||
typeof c.purchases === 'boolean' &&
|
||||
typeof c.services === 'boolean' &&
|
||||
Array.isArray(c.operations) &&
|
||||
@@ -47,6 +59,10 @@ export function isSupplierAdvances(v: unknown): v is SupplierAdvances {
|
||||
o &&
|
||||
['purchases', 'services'].includes(o.kind) &&
|
||||
typeof o.ordinaryContract === 'boolean' &&
|
||||
(o.formalAgreement === undefined ||
|
||||
(o.formalAgreement &&
|
||||
text(o.formalAgreement.date) &&
|
||||
text(o.formalAgreement.reference))) &&
|
||||
[
|
||||
'originId',
|
||||
'parcelId',
|
||||
@@ -89,9 +105,9 @@ export function buildSupplierAdvances(
|
||||
) {
|
||||
if (!isSupplierAdvances(config))
|
||||
throw Error('Adiantamentos: estrutura inválida.');
|
||||
if (config.regime !== 'PGC-PE')
|
||||
if (!['PGC-PE', 'PGC-NIRF'].includes(config.regime))
|
||||
throw Error(
|
||||
'Adiantamentos: seleccione explicitamente PGC-PE. Outros regimes não estão automatizados.',
|
||||
'Adiantamentos: seleccione explicitamente PGC-PE ou PGC-NIRF.',
|
||||
);
|
||||
if (!Number.isSafeInteger(year) || year < 1900 || year > 9993)
|
||||
throw Error(
|
||||
@@ -145,6 +161,12 @@ export function buildSupplierAdvances(
|
||||
o.supplier.trim(),
|
||||
o.recognitionDate,
|
||||
o.invoiceDate,
|
||||
...(config.regime === 'PGC-NIRF'
|
||||
? [
|
||||
o.formalAgreement?.date ?? '',
|
||||
o.formalAgreement?.reference.trim() ?? '',
|
||||
]
|
||||
: []),
|
||||
]);
|
||||
if (origins.has(originKey) && origins.get(originKey) !== identity)
|
||||
fail(
|
||||
@@ -158,6 +180,18 @@ export function buildSupplierAdvances(
|
||||
? date(o.recognitionDate)
|
||||
: null;
|
||||
const invoice = o.invoiceDate ? date(o.invoiceDate) : null;
|
||||
const agreement =
|
||||
config.regime === 'PGC-NIRF' && o.formalAgreement
|
||||
? date(o.formalAgreement.date)
|
||||
: null;
|
||||
if (
|
||||
config.regime === 'PGC-NIRF' &&
|
||||
o.formalAgreement &&
|
||||
(!agreement || !o.formalAgreement.reference.trim())
|
||||
)
|
||||
fail(
|
||||
'acordo formal exige data válida e referência identificada da mesma operação; não basta orçamento ou encomenda.',
|
||||
);
|
||||
if (o.recognitionDate && !recognition)
|
||||
fail('data de reconhecimento inválida.');
|
||||
if (o.invoiceDate && !invoice) fail('data de factura inválida.');
|
||||
@@ -204,6 +238,11 @@ export function buildSupplierAdvances(
|
||||
advance: string;
|
||||
obligation: string;
|
||||
netDue: string;
|
||||
applicationBasis?: {
|
||||
kind: 'invoice' | 'formal_agreement';
|
||||
date: string;
|
||||
reference: string;
|
||||
};
|
||||
}[] = [];
|
||||
const process = (e: (typeof events)[number]) => {
|
||||
if (!isRecognized && recognition && recognition <= e.when) {
|
||||
@@ -212,6 +251,13 @@ export function buildSupplierAdvances(
|
||||
}
|
||||
const m = index(e.when);
|
||||
const inside = m >= 0 && m < periods;
|
||||
let applicationBasis:
|
||||
| {
|
||||
kind: 'invoice' | 'formal_agreement';
|
||||
date: string;
|
||||
reference: string;
|
||||
}
|
||||
| undefined;
|
||||
if (e.kind === 'advance') {
|
||||
if (isRecognized)
|
||||
fail(
|
||||
@@ -223,10 +269,26 @@ export function buildSupplierAdvances(
|
||||
paid[m] += e.value;
|
||||
}
|
||||
} else if (e.kind === 'apply') {
|
||||
if (!isRecognized || !invoice || invoice > e.when)
|
||||
const hasInvoice = !!invoice && invoice <= e.when;
|
||||
const hasAgreement = !!agreement && agreement <= e.when;
|
||||
if (!isRecognized || (!hasInvoice && !hasAgreement))
|
||||
fail(
|
||||
'aplicação exige reconhecimento e factura correspondente, sem antecipar datas.',
|
||||
config.regime === 'PGC-NIRF'
|
||||
? 'aplicação exige reconhecimento e factura ou acordo formal correspondente já datado e identificado, sem antecipar datas.'
|
||||
: 'aplicação exige reconhecimento e factura correspondente, sem antecipar datas.',
|
||||
);
|
||||
if (config.regime === 'PGC-NIRF')
|
||||
applicationBasis = hasInvoice
|
||||
? {
|
||||
kind: 'invoice',
|
||||
date: o.invoiceDate,
|
||||
reference: o.originId + '/' + o.parcelId,
|
||||
}
|
||||
: {
|
||||
kind: 'formal_agreement',
|
||||
date: o.formalAgreement!.date,
|
||||
reference: o.formalAgreement!.reference,
|
||||
};
|
||||
if (e.value > advance || e.value > obligation)
|
||||
fail(
|
||||
'aplicação excede o saldo da mesma parcela ou a obrigação reconhecida.',
|
||||
@@ -269,6 +331,7 @@ export function buildSupplierAdvances(
|
||||
netDue: money(
|
||||
obligation > advance ? obligation - advance : 0n,
|
||||
),
|
||||
...(applicationBasis ? { applicationBasis } : {}),
|
||||
});
|
||||
};
|
||||
const monthly = [];
|
||||
@@ -293,6 +356,17 @@ export function buildSupplierAdvances(
|
||||
obligation: money(obligation),
|
||||
netDue: money(netDue),
|
||||
recognized: isRecognized,
|
||||
...(config.regime === 'PGC-NIRF'
|
||||
? {
|
||||
documentaryStatus: !isRecognized
|
||||
? 'not_recognized'
|
||||
: invoice && invoice < boundary
|
||||
? 'invoice'
|
||||
: agreement && agreement < boundary
|
||||
? 'formal_agreement'
|
||||
: 'pending_documentation',
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
}
|
||||
// Validate future events as well; they never leak into the last month of cash.
|
||||
@@ -305,6 +379,9 @@ export function buildSupplierAdvances(
|
||||
amount: money(amount),
|
||||
recognitionDate: o.recognitionDate,
|
||||
invoiceDate: o.invoiceDate,
|
||||
...(config.regime === 'PGC-NIRF' && o.formalAgreement
|
||||
? { formalAgreement: o.formalAgreement }
|
||||
: {}),
|
||||
recognitionOutsideHorizon: !recognition || recognition >= end,
|
||||
monthly,
|
||||
events: audit,
|
||||
|
||||
Reference in New Issue
Block a user