Skip to content

feat: close financial year wizard - #50

Merged
jonymaster merged 4 commits into
mainfrom
feat/close-fiscal-year
Nov 6, 2026
Merged

jonymaster merged 4 commits into
mainfrom
feat/close-fiscal-year

Conversation

@jonymaster

Copy link
Copy Markdown
Owner

Summary

Adds a guided "Close financial year" action for service costs, as described in docs/superpowers/specs/2026-11-06-close-fiscal-year-design.md.

Closing year X:

  1. Converts the year's estimated service cost records to actual in place.
  2. Creates one estimated record per service in X+1. Its amount is the sum of the year's actuals, with an optional uplift.

Archived services, budget records, and hardware costs are left out. Admins review and edit every row in a wizard before confirming. Each close is stored as a batch and can be reverted.

Changes

  • Backend
    • Migration 050: fiscal_year_close_batches table, plus rollover_batch_id, rollover_action and rollover_created_batch_id on cost_records.
    • Service module app/services/fiscal_year_close.py. The plan logic is pure functions, so it can be tested without a DB; async functions handle execute and revert.
    • Router /api/cost-records/close-year: POST /preview, POST /execute, POST /revert, GET /batches. Every endpoint requires admin plus financial visibility.
    • Execute locks every service that has service cost records in X or X+1, then the cost records, both in id order. It then recomputes the plan hash. A hash mismatch, deadlock or serialization failure returns 409, and nothing is written.
    • Money uses Decimal with ROUND_HALF_UP. Validation errors return 400.
    • The batch table is audited. Every converted, created, or deleted record gets a history row attributed to the admin.
  • Frontend
    • /costs/close-year wizard with four steps: pick year, review and edit, confirm, done.
    • Live totals use the same math as the server.
    • The review step has an "Only rows with flags" filter and a Skipped section.
    • The Confirm button stays disabled until the user types the year. A 409 reloads the preview.
    • A "Previous closes" panel lists past batches. Each can be reverted, and the confirmation shows how many records changed since the close.
    • Admin-only entry button on the Costs report. The Costs report now accepts year and mode query params.
  • MCP: read-only preview_close_fiscal_year tool. There are no execute or revert tools.
  • Docs: README section, MCP tool list (the README tool list was also missing the earlier cost tools), release notes, and spec updates.

Decisions beyond the original spec

  • Chained closes. Closing X+1 converts the estimates that the close of X created, which overwrites their single batch link. Reverting both closes used to leave an orphan X+1 estimate. The new rollover_created_batch_id column fixes this:
    • Reverting the close of X+1 links those records back to the close of X.
    • Reverting the close of X while the X+1 close is still active returns 409 Revert these later closes first: FY....
  • Execute locks all services with service cost records in X or X+1, archived ones included, not only active services in X. This avoids deadlocks with cascades on archived services and keeps the is_active part of the hash stable until commit.
  • The preview adds uplift_pct, conversion notes and payment_method_id, and existing_actual_payment_method_ids. The wizard uses the last one to update the mixed_payment_methods flag live.
  • GET /batches timestamps carry a UTC offset. User names fall back to the user's email.
  • The admin export leaves out the new table and columns. The export is a hand-picked column set and nothing imports it back, so only the batch link is lost on export.

Known limitations

  • changed_since_close can miss an edit whose transaction started before the close and committed after it.
  • One race remains, and the spec accepts it. Execute only locks records that exist when it runs. A record that another user adds or moves into X or X+1 during a close is not seen. The worst case is one extra next-year record, which the user can delete.
  • The PostgreSQL tests (backend/tests/test_fiscal_year_close_db.py) are opt-in, and CI skips them. They need a disposable *_test database and CATALOGIT_DB_TESTS=1.

Test plan

  • Backend CI suite: uv run --locked --with pytest pytest -q: 262 passed, 1 skipped (the opt-in DB module)
  • Backend DB tests against a disposable PostgreSQL 16 test database: 13 passed. They cover execute end to end, the hash-mismatch 409, running the close twice, revert (including chained closes and archived services), GET /batches with changed_since_close, history rows, a concurrent execute, and a cost record PUT that waits for the close's locks.
  • Migration 050 upgrade, downgrade to 049, and upgrade again on the test database
  • Frontend: npm run lint, npm test (53 tests), npm run build
  • MCP: uv run --locked --with pytest pytest -q: 53 passed
  • Manual check in a browser against a running instance: close a year, edit rows, confirm, revert

Add migration 050 with the fiscal_year_close_batches table and rollover
columns on cost_records, a service module that plans, executes, and reverts
a close, and admin-only endpoints under /api/cost-records/close-year.

Execute locks services and cost records, rechecks the plan hash, converts
estimated records to actual in place, and creates one summed next-year
estimate per service. Revert restores chained closes in order. Includes CI
unit tests and opt-in PostgreSQL tests (CATALOGIT_DB_TESTS=1).
Add the /costs/close-year wizard (pick year, review and edit, confirm by
typing the year, done) with previous closes and revert, plus an admin-only
entry button on the Costs report. The Costs report accepts year and mode
query params.
Cover the close rules, flags, revert, and yearly cost effects in the README
and release notes, list the new MCP tool, and update the spec with the
decisions made during implementation.
@jonymaster
jonymaster merged commit 311ef03 into main Nov 6, 2026
7 checks passed
@jonymaster
jonymaster deleted the feat/close-fiscal-year branch November 6, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant