Annual audit: tests for the loss-year tax charge, and detect step 4 charges by tax category

Aperta
#5,103 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
postgresql, typescript

Direzione di ricerca

Start with financial-charges.resolver.ts and charges.provider.ts to trace how annual charges are queried and matched, then check each generator’s tax category, especially the bank-deposits revaluation charge. Add the generator, regeneration, SHAAM 6111, and annual charge integration tests described in the issue; run the listed Vitest commands with a local Postgres database. Done means all six charges are found by category without a ledger, edited descriptions still match, the fallback is removed, and the tests pass in CI.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

annual-audit-workflow

Follow-up to #5086, fixed in #5096 (epic #5085). Links point to main at the #5096 merge commit (4d5ba86).

Background

#5096 made current tax never negative. In a loss year, the tax-expense charge now gets no tax ledger entry. Two things were left out to keep that PR small:

  1. tests for the generator, the step 4 lookup and 6111 (they were in the #5086 acceptance criteria);
  2. a reliable way for step 4 to find annual financial charges.

Part 1: missing tests

Today only the pure computeCurrentTax is tested (tax.helper.test.ts).

1a. Tax-expense generator (unit)
  • Code: generateLedgerRecordsForTaxExpenses
    • It calls calculateTaxAmounts and pushes the tax entry only when currentTaxExpense > 0 (L127).
    • It then appends the misc-expense entries.
  • Pattern to copy: monthly-vat-ledger-generation.resolver.test.ts. It mocks helper modules with vi.mock and fakes the injector.
  • What to mock:
    • calculateTaxAmounts and calculateCumulativeRnDExpenses (reports/helpers/tax.helper.js);
    • generateMiscExpensesLedger and storeInitialGeneratedRecords;
    • the LedgerProvider, FinancialEntitiesProvider and AdminContextProvider injector lookups.
  • Cases:
    • profit year: one entry, Dr taxExpensesTaxCategoryId / Cr taxBusinessId for currentTaxExpense, dated 31 Dec;
    • loss year (currentTaxExpense = 0): no tax entry, misc-expense entries still returned;
    • no description, or no year in it: CommonError.
1b. Regenerating removes the old wrong entry (unit)
  • Code: regeneration matches stored records against new ones with ledgerRecordsGenerationPartialMatchComparison. Every stored record left unmatched goes to toRemove, which the regenerate flow deletes.
  • Test: call it with one stored tax-expense record and newRecords = [], and assert that the record ends up in toRemove. That is the "existing data" acceptance criterion of #5086.
1c. 6111 in a loss year (integration)
  • Pattern to copy: shaam6111.integration.test.ts. It seeds books through the real app and checks balanceSheet / profitAndLoss codes, for example amountOf(data.balanceSheet, 9980) at L443.
  • New case: books with a pre-tax loss and no tax-expense entry. Assert:
    • 5600 is 0;
    • 9980 equals the accumulated result including the loss (see retainedProfitFromProfitAndLoss, shaam6111-computation.helper.ts);
    • 500 equals the (negative) taxable income.
1d. Step 4 finds a ledger-less tax charge (integration)

Covered by Part 2 below.

Part 2: detect step 4 charges by tax category

Current behaviour

annualFinancialCharges loads the year's FINANCIAL charges with fromAnyDate / toAnyDate = year. It then finds each of the six charges by checking whether its ledger's business_array contains a tax category or business.

That breaks for a charge with no ledger:

  • No dates: the date filter in getChargesByFilters is GREATEST/LEAST over document, transaction, ledger and securities dates. A charge with none of them has NULL dates and is filtered out.
  • No businesses: business_array is empty as well.
Stop-gap from #5096 (tax charge only, L63-L78)

If the normal lookup misses, a second query without dates uses freeText: "Tax expenses charge for YYYY" and picks the charge whose tax_category_id is taxExpensesTaxCategoryId. Problems:

  • Fragile: it depends on the exact description written by generateTaxExpensesCharge. Rename the charge and step 4 shows "Missing" again. freeText also matches transaction and document text, which is harmless here but imprecise.
  • Partial: the other five charges (revaluation, depreciation, recovery reserve, vacation reserve, bank-deposits revaluation) still need a ledger to be found.
Proposed fix
  1. Find each charge by its own tax category. generateAndTagCharge stores charges.tax_category_id on every generated financial charge, and the provider already returns it (charges.provider.ts L751).
    • Watch out: the bank-deposits charge is matched today by bankDepositBusinessId, not by a tax category. Check which category generateBankDepositsRevaluationCharge passes before switching it.
  2. Make the year lookup independent of ledger dates. Options, simplest first:
    • (a) Filter in SQL by type = 'FINANCIAL', owner_id and tax_category_id IN (...), without the date filter, then pick the charge whose year matches. Every generator puts the year or date in user_description, and the ledger generators parse it the same way (see the year regex in the tax generator, L52-L58).
    • (b) Add a dedicated column or filter (for example a period date on the charge). This needs a migration.
  3. Remove the description-based fallback added in #5096.
  4. No client change. Step 4 UI only reads which charges are present.
Tests
  • Integration (packages/server/src/modules/charges/**/*.integration.test.ts or packages/server/src/__tests__/): seed a FINANCIAL charge with the tax-expense category, the description Tax expenses charge for 2025 and no ledger, documents or transactions. Assert that annualFinancialCharges(year: "2025-01-01") returns it as taxExpensesCharge.
  • Repeat with an edited description once Part 2 lands.

How to run

yarn generate
yarn vitest run --project unit packages/server/src/modules/ledger packages/server/src/modules/reports/helpers
yarn vitest run --project integration packages/server/src/__tests__/shaam6111.integration.test.ts

Integration tests need Postgres and migrations (see the README, Testing). Make sure POSTGRES_* points at a local DB, not the one in the root .env.

Done when

  • The tests in 1a, 1b, 1c and 1d exist and pass in CI.
  • All six annual financial charges are found by tax category, including a charge with no ledger.
  • Editing a generated charge's description doesn't make step 4 report it as missing.
  • The freeText fallback is gone.
Lingua principale
TypeScript
Stelle
30
Fork
10
Merge medio
2g 12h
PR unite (30g)
276

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Urigo/accounter-fullstack

Tutte le issue di Urigo/accounter-fullstack

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.