import { financialReportFixture as plan } from './fixtures/integral-financial-plan.mjs'; import assert from 'node:assert/strict'; import test from 'node:test'; import { buildIntegralFinancialTables } from '../../resources/js/lib/integral-financial-report.ts'; import { forecastBusinessPlan } from '../../resources/js/lib/business-financial-plan.ts'; const row = (tables, id, label) => tables.find((t) => t.id === id).rows.find((r) => r[0] === label); const money = (value) => BigInt(value.replace('.', '')); test('IVA enters the treasury: received with the sales collections, delivered the following month, balance still reconciled (decisão 04/12)', () => { const input = plan(13); const periods = 13; const forecast = forecastBusinessPlan(input); const billed = forecast.rows.map((r) => (money(r.revenue) * 16n) / 100n); const vat = { ratePercent: '16', billedByMonth: billed, source: 'Operação interna geral, CIVA art. 17', }; const without = buildIntegralFinancialTables(input); const withVat = buildIntegralFinancialTables(input, vat); const received = row(withVat, 'cashflow', 'IVA recebido dos clientes'); const delivered = row(withVat, 'cashflow', 'IVA entregue ao Estado'); assert.ok(received && delivered); assert.equal( delivered[1], '0.00', 'nothing is delivered in the first month', ); assert.equal( money(delivered[2]), billed[0], 'month 1 IVA delivered in month 2', ); // Received follows the sales actually collected, never the billing alone. forecast.rows.forEach((r, i) => { const salesCash = money(r.cashCollections) - money(r.openingReceivablesCollections); assert.equal(money(received[i + 1]), (salesCash * 16n + 50n) / 100n); }); const cashWithout = row(without, 'cashflow', 'Caixa final'); const cashWith = row(withVat, 'cashflow', 'Caixa final'); let effect = 0n; for (let i = 1; i <= periods; i++) { effect += money(received[i]) - money(delivered[i]); assert.equal( money(cashWith[i]), money(cashWithout[i]) + effect, `month ${i}`, ); } const due = row(withVat, 'balance', 'IVA a entregar ao Estado'); assert.equal( money(due[periods]), billed[periods - 1], 'the last month stays to deliver', ); assert.deepEqual( row(withVat, 'balance', 'Diferença do balanço'), row(without, 'balance', 'Diferença do balanço'), 'the balance reconciles exactly as before', ); assert.deepEqual( row(withVat, 'profit-loss', 'Lucro líquido'), row(without, 'profit-loss', 'Lucro líquido'), 'IVA is neither revenue nor cost', ); assert.match( withVat.find((t) => t.id === 'cashflow').notes.join(' '), /32\(1\)\(b\)/, ); assert.equal( row(without, 'cashflow', 'IVA recebido dos clientes'), undefined, ); }); test('the treasury uses only a completed IVA result that still matches the sales; a stale or partial one is left out', async () => { const { serializeBusinessPlanSnapshot } = await import('../../resources/js/lib/business-financial-plan.ts'); const { monthlyIvaInput, integralVatFromDraft } = await import('../../resources/js/lib/monthly-iva.ts'); const settings = { start: '2026-01', basis: 'net', declared: true, auto: true, }; const value = plan(3); const draft = { currency: 'MZN', business_plan_snapshot: serializeBusinessPlanSnapshot(value), monthly_iva_settings: JSON.stringify(settings), }; const input = monthlyIvaInput(draft, settings); const record = (rows) => JSON.stringify({ input, execution: { id: '61000100-0000-4000-8000-000000000078', status: 'completed', version_no: 1, result_snapshot: { contract_version: 1, scenario_kind: 'fiscal_rule_published', base: null, currency: 'MZN', indicative_total: null, legal_interpretation: false, formula_version: 'investmoz.tax.v1', context: { reference_date: '2026-01-01', currency: 'MZN', currency_fraction_digits: 2, scenario_id: '61000100-0000-4000-8000-000000000099', scenario_version: 1, engine_version: 'fiscal-published-v1', ruleset_version: 'IVA-GERAL-BASE-LIQUIDA:1', }, result: { status: 'indeterminate', value: null, unit: 'MZN', explanation: 'IVA mensal; não é imposto a pagar.', missing_inputs: ['monthly_sales'], source_ids: ['qa-source'], }, presentation: { value: null, rounding_applied: false }, fiscal_provenance: { rule: { rule_code: 'IVA-GERAL-BASE-LIQUIDA', rule_version: 1, valid_from: '2023-01-01', valid_until: null, source: { diploma: 'Fonte sintética QA', article: 'Teste', }, }, scope_status: 'user_declared', }, monthly_projection: { input, rows }, }, }, }); const rows = input.monthly_sales.map((sale) => ({ date: sale.date, sales_declared: sale.amount, net_sales: sale.amount, rate: '16', vat: '160.0048', gross_sales: null, status: 'calculated', })); const vat = integralVatFromDraft({ ...draft, monthly_iva_snapshot: record(rows), }); assert.equal(vat.ratePercent, '16'); assert.deepEqual( vat.billedByMonth, [16000n, 16000n, 16000n], 'rounded half up to the cent', ); assert.equal( integralVatFromDraft({ ...draft, monthly_iva_snapshot: record([ ...rows.slice(0, 2), { ...rows[2], vat: null, rate: null }, ]), }), null, ); const changed = plan(3); changed.products[0].unitPrice = '120'; assert.equal( integralVatFromDraft({ ...draft, business_plan_snapshot: serializeBusinessPlanSnapshot(changed), monthly_iva_snapshot: record(rows), }), null, 'a result for other sales is not used', ); });