Skip to content

fix(bug 12): P&L report honours from_date/to_date across multiple periods#9

Merged
roger296 merged 2 commits intomainfrom
fix/bug-12-pnl-date-range
Apr 22, 2026
Merged

fix(bug 12): P&L report honours from_date/to_date across multiple periods#9
roger296 merged 2 commits intomainfrom
fix/bug-12-pnl-date-range

Conversation

@roger296
Copy link
Copy Markdown
Owner

Summary

getProfitAndLoss() in `src/engine/reports.ts` was applying a hard single-period filter:

```typescript
.where('transaction_lines.period_id', opts.period_id)
```

which silently neutralised the `from_date`/`to_date` filters further down. Any caller passing a multi-period date range got back only the single period named in `period_id` — the date parameters were accepted but had no effect.

Fix

Make the `period_id` filter conditional — apply it only when no date range is supplied. When `from_date` or `to_date` is present, the existing date filters select across periods as the caller expects. This mirrors the pattern already used by `getBalanceSheet()` in the same file.

Tests

Added 4 new integration tests covering:

  • default single-period behaviour still works (regression guard)
  • date range spanning two periods aggregates revenue from both
  • date range confined to a different period than `period_id` returns that period's data (confirms `period_id` is ignored once dates are set)
  • aggregate across range equals sum of individual monthly reports

Test plan

  • `npx tsc --noEmit` clean
  • Unit tests: 205/205 passing
  • Integration tests: 363/363 passing (4 new)
  • Post-deploy: call `gl_get_profit_and_loss` with `from_date`/`to_date` spanning multiple periods (e.g. Feb 2025 → Nov 2025) and confirm aggregated totals

🤖 Generated with Claude Code

Roger and others added 2 commits April 21, 2026 12:22
…iods

getProfitAndLoss() was applying a hard single-period filter:

    .where('transaction_lines.period_id', opts.period_id)

which silently neutralised the from_date/to_date filters below it. Any
caller passing a multi-period date range got back only the single period
named in period_id.

Fix: make the period filter conditional — apply it only when no date
range is supplied. When from_date or to_date is present, the date
filters select across periods as the caller expects.

This mirrors the pattern already used by getBalanceSheet() in the same
file.

New tests cover:
- default single-period behaviour still works (regression guard)
- date range spanning two periods aggregates both
- date range confined to a different period than period_id returns that
  period's data (confirms period_id is ignored once dates are set)
- aggregate across range equals sum of individual monthly reports

Verified: 205 unit tests pass, 363 integration tests pass (was 359 — 4
new).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@roger296 roger296 merged commit dc26461 into main Apr 22, 2026
1 check passed
@roger296 roger296 deleted the fix/bug-12-pnl-date-range branch April 22, 2026 05:44
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