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

Open
#5,103 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
postgresql, typescript

Research direction

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.

Written by the indexing model from the issue text.

Description

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.
Dominant language
TypeScript
Stars
30
Forks
10
Avg merge
2d 12h
Merged PRs (30d)
276

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Urigo/accounter-fullstack

All issues in Urigo/accounter-fullstack

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.