fix(fusion_accounting_reports): engine accepts report_code to disambiguate

When multiple fusion.report rows share a report_type (e.g. 4 PnL-typed
reports: pnl, cash_flow, executive_summary, annual_statements), the
engine's _get_report previously returned whichever matched the type
filter first \u2014 so all four reports rendered the canonical P&L
line_specs regardless of which report the user selected.

Adds report_code kwarg to compute_pnl, compute_balance_sheet,
compute_trial_balance, compute_gl. Controller /fusion/reports/run now
accepts and forwards report_code. _get_report has a 3-tier resolution:
1. Exact code match (validates type)
2. Canonical (code == report_type)
3. First by sequence

Two new tests assert distinct line_specs render for distinct codes and
that wrong-type code raises ValidationError.

Verified live on westin-v19: pnl/cash_flow/executive_summary/
annual_statements now return 3/9/7/5 rows respectively (was all
3 before).

Made-with: Cursor
This commit is contained in:
gsinghpal
2026-04-19 23:58:29 -04:00
parent 1ebff01d35
commit 1817f63c67
3 changed files with 136 additions and 23 deletions

View File

@@ -39,10 +39,17 @@ class FusionReportEngine(models.AbstractModel):
@api.model
def compute_pnl(
self, period: Period, *, comparison: str = 'none',
company_id: int | None = None,
company_id: int | None = None, report_code: str | None = None,
) -> dict:
"""Income statement (P&L) for the given period."""
report = self._get_report('pnl', company_id=company_id)
"""Income statement (P&L) for the given period.
``report_code`` selects between multiple PnL-typed report definitions
(``pnl``, ``cash_flow``, ``executive_summary``, ``annual_statements``).
When omitted, falls back to the canonical ``pnl`` definition.
"""
report = self._get_report(
'pnl', company_id=company_id, code=report_code,
)
return self._compute(
report, period, comparison=comparison, company_id=company_id,
)
@@ -50,11 +57,13 @@ class FusionReportEngine(models.AbstractModel):
@api.model
def compute_balance_sheet(
self, date_to: date, *, comparison: str = 'none',
company_id: int | None = None,
company_id: int | None = None, report_code: str | None = None,
) -> dict:
"""Balance sheet AS OF date_to. Period.date_from is set to a
far-past date so balances are cumulative-since-inception."""
report = self._get_report('balance_sheet', company_id=company_id)
report = self._get_report(
'balance_sheet', company_id=company_id, code=report_code,
)
period = Period(
date_from=date(1970, 1, 1),
date_to=date_to,
@@ -67,10 +76,17 @@ class FusionReportEngine(models.AbstractModel):
@api.model
def compute_trial_balance(
self, period: Period, *, company_id: int | None = None,
report_code: str | None = None,
) -> dict:
"""Trial balance for the given period - every account with
non-zero balance."""
report = self._get_report('trial_balance', company_id=company_id)
non-zero balance.
``report_code`` selects between multiple TB-typed reports (e.g.
``trial_balance``, ``tax_summary``).
"""
report = self._get_report(
'trial_balance', company_id=company_id, code=report_code,
)
return self._compute(
report, period, comparison='none', company_id=company_id,
)
@@ -78,12 +94,14 @@ class FusionReportEngine(models.AbstractModel):
@api.model
def compute_gl(
self, period: Period, *, account_ids: list | None = None,
company_id: int | None = None,
company_id: int | None = None, report_code: str | None = None,
) -> dict:
"""General ledger for the given period.
Returns per-account move-line listings rather than aggregated rows."""
report = self._get_report('general_ledger', company_id=company_id)
report = self._get_report(
'general_ledger', company_id=company_id, code=report_code,
)
company_id = company_id or self.env.company.id
result = self._compute(
report, period, comparison='none', company_id=company_id,
@@ -246,23 +264,60 @@ class FusionReportEngine(models.AbstractModel):
# PRIVATE HELPERS
# ============================================================
def _get_report(self, report_type: str, *, company_id: int | None = None):
"""Look up the active fusion.report definition for a given
type+company. If no per-company override, falls back to global
(company_id=False)."""
def _get_report(
self, report_type: str, *, company_id: int | None = None,
code: str | None = None,
):
"""Look up the active fusion.report definition.
When ``code`` is provided, prefer the report with that exact code
(validating its ``report_type`` matches). Otherwise fall back to
the canonical-by-type lookup: prefer code == report_type, then any
report of that type. Per-company overrides win over global.
"""
Report = self.env['fusion.report'].sudo()
company_id = company_id or self.env.company.id
company_domain = [
('active', '=', True),
'|',
('company_id', '=', company_id),
('company_id', '=', False),
]
if code:
report = Report.search(
[('code', '=', code)] + company_domain,
order='company_id desc nulls last',
limit=1,
)
if not report:
raise ValidationError(
_("No active fusion.report definition with code '%s'") % code
)
if report.report_type != report_type:
raise ValidationError(
_("Report '%(code)s' has type '%(actual)s' but '%(expected)s' was expected.")
% {
'code': code,
'actual': report.report_type,
'expected': report_type,
}
)
return report
# No code: prefer the canonical (code == report_type), then any
# other report of that type.
report = Report.search(
[
('report_type', '=', report_type),
('active', '=', True),
'|',
('company_id', '=', company_id),
('company_id', '=', False),
],
[('code', '=', report_type), ('report_type', '=', report_type)] + company_domain,
order='company_id desc nulls last',
limit=1,
)
if report:
return report
report = Report.search(
[('report_type', '=', report_type)] + company_domain,
order='company_id desc nulls last, sequence',
limit=1,
)
if not report:
raise ValidationError(
_("No active fusion.report definition for type '%s'") % report_type