import test from 'node:test'; import assert from 'node:assert/strict'; import { forecastBusinessPlan, serializeBusinessPlanSnapshot, restoreBusinessPlanSnapshot, exportBusinessPlanCsv, } from '../../resources/js/lib/business-financial-plan.ts'; import { buildIntegralFinancialTables } from '../../resources/js/lib/integral-financial-report.ts'; import { advanceFixture, event } from './fixtures/supplier-advances.mjs'; import { buildFinancialFiscalBridge } from '../../resources/js/lib/financial-fiscal-bridge.ts'; test('F20-B refund reconciles in the editor fiscal bridge without becoming taxable revenue or other collections', () => { const p = advanceFixture({ kind: 'services', amount: '12000', recognition: '2027-02-05', invoice: '2027-02-08', }); p.supplierAdvances.operations[0].events = [ event('A', 'advance', '2027-01-10', '20000'), event('B', 'apply', '2027-02-08', '12000'), event('C', 'refund', '2027-03-01', '8000'), ]; const bridge = buildFinancialFiscalBridge(forecastBusinessPlan(p)); assert.equal(bridge.cashSchedule[2].supplierAdvanceRefunds, '8000.00'); assert.equal(bridge.cashSchedule[2].otherCollections, '0.00'); assert.equal( bridge.accountingLines.find((l) => l.code === 'revenue').total, '0.00', ); }); test('F20-B accepts a prior documentary invoice without inferring an opening balance; horizon 60 retains future events', () => { const p = advanceFixture({ recognition: '2031-12-20', invoice: '2026-12-01', periods: 60, }); p.supplierAdvances.operations[0].events = [ event('A', 'advance', '2027-01-01', '30000'), event('B', 'apply', '2031-12-20', '30000'), event('C', 'settle', '2032-01-01', '70000'), ]; const r = forecastBusinessPlan(p); assert.equal(r.supplierAdvanceLedger.monthly[0].advance, '30000.00'); assert.equal(r.supplierAdvanceLedger.monthly[59].obligation, '70000.00'); assert.equal(r.rows[59].variableCostPayments, '0.00'); assert.equal( r.supplierAdvanceLedger.operations[0].events[2].outsideHorizon, true, ); }); const ledger = (p) => forecastBusinessPlan(p).supplierAdvanceLedger; function reconciled(p) { const tables = buildIntegralFinancialTables(p); assert.ok( tables.every((t) => t.issues.length === 0), JSON.stringify(tables.map((t) => [t.id, t.issues])), ); const row = tables .find((t) => t.id === 'balance') .rows.find((r) => r[0] === 'Diferença do balanço'); assert.ok( row.slice(1).every((v) => v === '0.00'), JSON.stringify(row), ); return tables; } test('F20-B reception before invoice: gross obligation and asset separated, no second payment on application', () => { const p = advanceFixture(); p.supplierAdvances.operations[0].events = [ event('A', 'advance', '2027-01-10', '30000'), event('B', 'apply', '2027-03-01', '30000'), event('C', 'settle', '2027-04-01', '70000'), ]; const before = structuredClone(p), r = forecastBusinessPlan(p), l = r.supplierAdvanceLedger; assert.deepEqual(p, before); assert.deepEqual( l.monthly.map((m) => [m.obligation, m.advance, m.netDue]), [ ['0.00', '30000.00', '0.00'], ['100000.00', '30000.00', '70000.00'], ['70000.00', '0.00', '70000.00'], ['0.00', '0.00', '0.00'], ], ); assert.deepEqual( r.rows.map((m) => m.variableCostPayments), ['30000.00', '0.00', '0.00', '70000.00'], ); assert.equal(r.totalPayments, '100000.00'); const tables = reconciled(p), b = tables.find((t) => t.id === 'balance'); assert.equal( b.rows.find((r) => r[0] === 'Obrigações operacionais brutas')[2], '100000.00', ); assert.equal( b.rows.find( (r) => r[0] === 'Adiantamentos identificados a fornecedores', )[2], '30000.00', ); assert.equal(exportBusinessPlanCsv(p).includes('OP-1'), true); }); test('F20-B invoice before reception does not recognize goods or expenses early', () => { const p = advanceFixture({ recognition: '2027-03-10', invoice: '2027-01-10', }); p.supplierAdvances.operations[0].events = [ event('A', 'advance', '2027-02-01', '30000'), event('B', 'apply', '2027-03-10', '30000'), event('C', 'settle', '2027-04-01', '70000'), ]; const r = forecastBusinessPlan(p); assert.deepEqual( r.rows.map((m) => m.inventoryReceipts), ['0.00', '0.00', '100000.00', '0.00'], ); assert.deepEqual( ledger(p).monthly.map((m) => m.obligation), ['0.00', '0.00', '70000.00', '0.00'], ); reconciled(p); }); test('F20-B service partial use and refund: no new income or expenditure', () => { const p = advanceFixture({ kind: 'services', amount: '12000', recognition: '2027-02-05', invoice: '2027-02-08', }); p.supplierAdvances.operations[0].events = [ event('A', 'advance', '2027-01-10', '20000'), event('B', 'apply', '2027-02-08', '12000'), event('C', 'refund', '2027-03-01', '8000'), ]; const r = forecastBusinessPlan(p); assert.deepEqual( ledger(p).monthly.map((m) => m.advance), ['20000.00', '8000.00', '0.00', '0.00'], ); assert.equal(r.rows[2].cashCollections, '8000.00'); assert.equal(r.rows[2].revenue, '0.00'); assert.equal(r.totalProfitBeforeTax, '-12040.00'); // accepted fixture depreciation: 10/month const tables = reconciled(p); assert.ok( tables .find((t) => t.id === 'profit-loss') .rows.find((r) => r[0] === 'Outras receitas de caixa declaradas') .slice(1) .every((v) => v === '0.00'), ); assert.equal( tables .find((t) => t.id === 'balance') .rows.find((r) => r[0] === 'Recebíveis líquidos')[3], '0.00', ); }); test('F20-B different operations of same supplier never offset each other', () => { const p = advanceFixture({ amount: '20000', recognition: '2027-03-01', invoice: '2027-03-01', }); const a = p.supplierAdvances.operations[0]; a.events = [event('A', 'advance', '2027-01-01', '20000')]; p.supplierAdvances.operations.push({ ...structuredClone(a), originId: 'OP-2', amount: '12000', recognitionDate: '2027-02-01', invoiceDate: '2027-02-01', events: [], }); p.inventoryPurchaseSchedule[1].amount = '12000'; const l = ledger(p); assert.equal(l.monthly[1].advance, '20000.00'); assert.equal(l.monthly[1].obligation, '12000.00'); assert.equal(l.monthly[1].netDue, '12000.00'); reconciled(p); p.supplierAdvances.operations[1].events = [ event('B', 'apply', '2027-02-01', '12000'), ]; assert.throws(() => forecastBusinessPlan(p), /excede o saldo/); }); test('F20-B application, refunds and remaining payments are bounded by same-parcel balances', () => { const p = advanceFixture(), o = p.supplierAdvances.operations[0]; o.events = [ event('A', 'advance', '2027-01-10', '30000'), event('B', 'apply', '2027-02-15', '30000'), ]; assert.throws(() => forecastBusinessPlan(p), /reconhecimento e factura/); o.events[1].date = '2027-03-01'; o.events[1].amount = '30000.01'; assert.throws(() => forecastBusinessPlan(p), /excede o saldo/); o.events[1] = event('B', 'refund', '2027-01-11', '30000.01'); assert.throws(() => forecastBusinessPlan(p), /devolução excede/); o.events[1] = event('B', 'settle', '2027-03-01', '100000'); assert.throws(() => forecastBusinessPlan(p), /líquido por liquidar/); o.events[1] = event('B', 'advance', '2027-03-01', '1'); assert.throws(() => forecastBusinessPlan(p), /após reconhecimento/); }); test('F20-B rejects special contracts, missing regime, invalid dates, duplicate parcels and inconsistent allocations', () => { const p = advanceFixture(), o = p.supplierAdvances.operations[0]; o.ordinaryContract = false; assert.throws(() => forecastBusinessPlan(p), /contratos especiais/); o.ordinaryContract = true; p.supplierAdvances.regime = ''; assert.throws(() => forecastBusinessPlan(p), /seleccione explicitamente/); p.supplierAdvances.regime = 'PGC-PE'; o.recognitionDate = '2027-02-30'; assert.throws(() => forecastBusinessPlan(p), /reconhecimento inválida/); o.recognitionDate = '2027-02-15'; o.events = [event('A', 'advance', '', '1')]; assert.throws(() => forecastBusinessPlan(p), /data de movimento/); o.events = []; p.supplierAdvances.operations.push(structuredClone(o)); assert.throws(() => forecastBusinessPlan(p), /duplicada/); p.supplierAdvances.operations.pop(); o.amount = '99999'; assert.throws( () => forecastBusinessPlan(p), /exactamente o reconhecimento/, ); }); test('F20-B missing or future recognition preserves advances, no inferred opening allocation or last-month cash', () => { for (const recognition of ['', '2028-02-01']) { const p = advanceFixture({ recognition, invoice: '', periods: 1 }); p.supplierAdvances.operations[0].events = [ event('A', 'advance', '2027-01-01', '20000'), event('B', 'refund', '2027-02-01', '8000'), ]; assert.equal(ledger(p).monthly[0].advance, '20000.00'); assert.equal(ledger(p).operations[0].events[1].outsideHorizon, true); assert.equal(forecastBusinessPlan(p).rows[0].cashCollections, '0.00'); reconciled(p); } const p = advanceFixture(); p.supplierAdvances.operations[0].events = [ event('A', 'advance', '2026-12-31', '1'), ]; assert.throws(() => forecastBusinessPlan(p), /anterior ao plano/); }); test('F20-B snapshot 21 round-trips inactive agendas, rejects foreign versions and invalid active ledgers', () => { const p = advanceFixture(); p.supplierAdvances.operations[0].events = [ event('A', 'advance', '2027-01-01', '30000'), ]; const s = serializeBusinessPlanSnapshot(p); assert.equal(JSON.parse(s).version, 21); const r = restoreBusinessPlanSnapshot(s); assert.ok(r); assert.equal(r.monthlyVariableCostPayments, '999'); assert.deepEqual(r.supplierAdvances, p.supplierAdvances); assert.deepEqual(forecastBusinessPlan(r), forecastBusinessPlan(p)); assert.equal( restoreBusinessPlanSnapshot(s.replace('"version":21', '"version":20')), null, ); p.supplierAdvances.operations[0].events.push( event('A', 'refund', '2027-01-02', '1'), ); assert.equal( restoreBusinessPlanSnapshot(serializeBusinessPlanSnapshot(p)), null, ); assert.ok( restoreBusinessPlanSnapshot(serializeBusinessPlanSnapshot(p), { editable: true, }), ); }); test('F20-B inactive mode preserves every old value and F20-A can coexist only on unselected categories', () => { const p = advanceFixture(), legacy = { ...p, supplierAdvances: undefined }; p.supplierAdvances.purchases = p.supplierAdvances.services = false; assert.deepEqual(forecastBusinessPlan(p), forecastBusinessPlan(legacy)); p.supplierAdvances.purchases = true; p.dayTerms = { sales: true, purchases: true, services: false, operations: [], }; assert.doesNotThrow(() => forecastBusinessPlan(p)); // inactive purchase day agenda isn't added or validated const l = ledger(p); assert.ok(l); assert.equal(forecastBusinessPlan(p).rows[0].variableCostPayments, '0.00'); });