changes
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
from . import fusion_report
|
||||
from . import fusion_report_engine
|
||||
from . import fusion_report_commentary
|
||||
from . import fusion_report_anomaly
|
||||
from . import fusion_account_balance_mv
|
||||
from . import fusion_reports_cron
|
||||
from . import fusion_migration_wizard
|
||||
@@ -0,0 +1,80 @@
|
||||
"""Materialized view of per-account-per-month balances.
|
||||
|
||||
Created lazily by init() (called by Odoo on install/upgrade). Refresh
|
||||
via the model's _refresh() method or via cron (Task 25)."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FusionAccountBalanceMV(models.Model):
|
||||
_name = "fusion.account.balance.mv"
|
||||
_description = "MV of per-account per-month aggregated balances"
|
||||
_auto = False
|
||||
_table = "fusion_account_balance_mv"
|
||||
_order = "period_month desc, account_id"
|
||||
|
||||
account_id = fields.Many2one('account.account', readonly=True)
|
||||
company_id = fields.Many2one('res.company', readonly=True)
|
||||
period_month = fields.Date(readonly=True)
|
||||
debit = fields.Float(readonly=True)
|
||||
credit = fields.Float(readonly=True)
|
||||
balance = fields.Float(readonly=True)
|
||||
line_count = fields.Integer(readonly=True)
|
||||
|
||||
def init(self):
|
||||
# If the MV exists but is missing the synthetic `id` column (e.g. from
|
||||
# an earlier dev install), drop it so the new schema applies cleanly.
|
||||
self.env.cr.execute(
|
||||
"""
|
||||
SELECT 1
|
||||
FROM pg_matviews mv
|
||||
JOIN pg_attribute a
|
||||
ON a.attrelid = (mv.schemaname || '.' || mv.matviewname)::regclass
|
||||
AND a.attname = 'id'
|
||||
WHERE mv.matviewname = 'fusion_account_balance_mv'
|
||||
"""
|
||||
)
|
||||
if not self.env.cr.fetchone():
|
||||
self.env.cr.execute(
|
||||
"DROP MATERIALIZED VIEW IF EXISTS fusion_account_balance_mv"
|
||||
)
|
||||
sql_path = os.path.join(
|
||||
os.path.dirname(__file__), '..', 'data', 'sql',
|
||||
'create_mv_account_balance.sql',
|
||||
)
|
||||
with open(sql_path, 'r') as f:
|
||||
self.env.cr.execute(f.read())
|
||||
_logger.info(
|
||||
"fusion_account_balance_mv: created/verified MV + indexes")
|
||||
|
||||
@api.model
|
||||
def _refresh(self, *, concurrently=True):
|
||||
"""Refresh the MV. Falls back to non-concurrent if CONCURRENTLY fails.
|
||||
|
||||
REFRESH MATERIALIZED VIEW CONCURRENTLY requires the MV to be already
|
||||
populated and an autocommit-capable cursor; the cron path in Task 25
|
||||
opens a dedicated cursor for that. This helper keeps callers safe by
|
||||
retrying without CONCURRENTLY on failure."""
|
||||
keyword = "CONCURRENTLY" if concurrently else ""
|
||||
try:
|
||||
self.env.cr.execute(
|
||||
f"REFRESH MATERIALIZED VIEW {keyword} fusion_account_balance_mv"
|
||||
)
|
||||
_logger.debug(
|
||||
"fusion_account_balance_mv refreshed (%s)",
|
||||
'concurrent' if concurrently else 'blocking',
|
||||
)
|
||||
except Exception as e:
|
||||
if concurrently:
|
||||
_logger.warning(
|
||||
"Concurrent MV refresh failed (%s); falling back", e)
|
||||
self.env.cr.execute(
|
||||
"REFRESH MATERIALIZED VIEW fusion_account_balance_mv"
|
||||
)
|
||||
else:
|
||||
raise
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Reports-specific migration step.
|
||||
|
||||
Ensures the 4 CORE report definitions are present after migration."""
|
||||
|
||||
import logging
|
||||
|
||||
from odoo import models
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FusionMigrationWizard(models.TransientModel):
|
||||
_inherit = "fusion.migration.wizard"
|
||||
|
||||
def _reports_bootstrap_step(self):
|
||||
"""Verify all 4 CORE report definitions exist."""
|
||||
Report = self.env['fusion.report'].sudo()
|
||||
expected = ['pnl', 'balance_sheet', 'trial_balance', 'general_ledger']
|
||||
present = Report.search([('report_type', 'in', expected)]).mapped('report_type')
|
||||
missing = set(expected) - set(present)
|
||||
return {
|
||||
'step': 'reports_bootstrap',
|
||||
'expected_reports': expected,
|
||||
'present_reports': list(present),
|
||||
'missing_reports': list(missing),
|
||||
}
|
||||
|
||||
def action_run_migration(self):
|
||||
"""Override to add reports-bootstrap step at the end of the chain."""
|
||||
result = super().action_run_migration() if hasattr(super(), 'action_run_migration') else None
|
||||
try:
|
||||
self._reports_bootstrap_step()
|
||||
except Exception as e:
|
||||
_logger.warning("reports_bootstrap_step failed: %s", e)
|
||||
return result
|
||||
@@ -0,0 +1,66 @@
|
||||
"""Persistent definition of a Fusion financial report.
|
||||
|
||||
Each report (P&L, balance sheet, trial balance, GL) has ONE row in
|
||||
fusion.report describing its metadata + line specs. The line specs
|
||||
are stored as a JSON-typed field for flexibility (each line spec
|
||||
includes account_type filter, sub-totaling rules, sign convention)."""
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
|
||||
|
||||
REPORT_TYPES = [
|
||||
('pnl', 'Income Statement (P&L)'),
|
||||
('balance_sheet', 'Balance Sheet'),
|
||||
('trial_balance', 'Trial Balance'),
|
||||
('general_ledger', 'General Ledger'),
|
||||
('aged_receivable', 'Aged Receivable'),
|
||||
('aged_payable', 'Aged Payable'),
|
||||
('partner_ledger', 'Partner Ledger'),
|
||||
]
|
||||
|
||||
|
||||
class FusionReport(models.Model):
|
||||
_name = "fusion.report"
|
||||
_description = "Fusion Financial Report Definition"
|
||||
_order = "sequence, id"
|
||||
|
||||
name = fields.Char(required=True, translate=True)
|
||||
code = fields.Char(
|
||||
required=True,
|
||||
help="Unique technical code (e.g. 'pnl', 'balance_sheet').",
|
||||
)
|
||||
report_type = fields.Selection(REPORT_TYPES, required=True)
|
||||
sequence = fields.Integer(default=10)
|
||||
description = fields.Text()
|
||||
active = fields.Boolean(default=True)
|
||||
|
||||
# Layout config - stored as JSON for flexibility per report type.
|
||||
# Example for P&L:
|
||||
# [
|
||||
# {"label": "Revenue", "account_type_prefix": "income_", "sign": 1},
|
||||
# {"label": "Cost of Goods Sold", "account_type_prefix": "expense_direct_", "sign": -1},
|
||||
# {"label": "Gross Profit", "compute": "subtotal", "above": 2},
|
||||
# ...
|
||||
# ]
|
||||
line_specs = fields.Json(string="Line Specs")
|
||||
|
||||
show_zero_balances = fields.Boolean(default=False)
|
||||
show_unposted = fields.Boolean(default=False)
|
||||
default_comparison_mode = fields.Selection(
|
||||
[
|
||||
('none', 'No comparison'),
|
||||
('previous_period', 'Previous Period'),
|
||||
('previous_year', 'Previous Year'),
|
||||
],
|
||||
default='none',
|
||||
)
|
||||
|
||||
company_id = fields.Many2one(
|
||||
'res.company',
|
||||
default=lambda self: self.env.company,
|
||||
)
|
||||
|
||||
_unique_company_code = models.Constraint(
|
||||
'UNIQUE(company_id, code)',
|
||||
'Report code must be unique per company.',
|
||||
)
|
||||
@@ -0,0 +1,56 @@
|
||||
"""Persisted anomaly flags from the engine's variance detection.
|
||||
|
||||
Each row captures one flagged report row variance. Used by the OWL
|
||||
anomaly_strip + the audit trail."""
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
|
||||
|
||||
SEVERITY = [('low', 'Low'), ('medium', 'Medium'), ('high', 'High')]
|
||||
DIRECTION = [('increase', 'Increase'), ('decrease', 'Decrease')]
|
||||
|
||||
|
||||
class FusionReportAnomaly(models.Model):
|
||||
_name = "fusion.report.anomaly"
|
||||
_description = "Flagged Report Variance"
|
||||
_order = "detected_at desc, severity desc"
|
||||
|
||||
report_id = fields.Many2one('fusion.report', required=True, ondelete='cascade')
|
||||
company_id = fields.Many2one('res.company', required=True,
|
||||
default=lambda self: self.env.company)
|
||||
period_from = fields.Date(required=True)
|
||||
period_to = fields.Date(required=True)
|
||||
|
||||
row_id = fields.Char(required=True, help="Engine-generated row id (e.g. 'line_3').")
|
||||
label = fields.Char(required=True)
|
||||
current_amount = fields.Float()
|
||||
comparison_amount = fields.Float()
|
||||
variance_amount = fields.Float()
|
||||
variance_pct = fields.Float()
|
||||
severity = fields.Selection(SEVERITY, required=True)
|
||||
direction = fields.Selection(DIRECTION, required=True)
|
||||
|
||||
detected_at = fields.Datetime(default=fields.Datetime.now, required=True)
|
||||
state = fields.Selection([
|
||||
('new', 'New'),
|
||||
('acknowledged', 'Acknowledged'),
|
||||
('investigating', 'Investigating'),
|
||||
('resolved', 'Resolved'),
|
||||
('dismissed', 'Dismissed'),
|
||||
], default='new', required=True)
|
||||
notes = fields.Text()
|
||||
acknowledged_by = fields.Many2one('res.users')
|
||||
acknowledged_at = fields.Datetime()
|
||||
|
||||
def action_acknowledge(self):
|
||||
self.write({
|
||||
'state': 'acknowledged',
|
||||
'acknowledged_by': self.env.uid,
|
||||
'acknowledged_at': fields.Datetime.now(),
|
||||
})
|
||||
|
||||
def action_dismiss(self):
|
||||
self.write({'state': 'dismissed'})
|
||||
|
||||
def action_resolve(self):
|
||||
self.write({'state': 'resolved'})
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Cached AI-generated commentary for a report run.
|
||||
|
||||
One row per (report, period_from, period_to, comparison_mode, company).
|
||||
Refreshed on demand or via cron when the underlying data has changed."""
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
|
||||
|
||||
class FusionReportCommentary(models.Model):
|
||||
_name = "fusion.report.commentary"
|
||||
_description = "AI-Generated Report Commentary Cache"
|
||||
_order = "generated_at desc"
|
||||
|
||||
report_id = fields.Many2one('fusion.report', required=True, ondelete='cascade')
|
||||
company_id = fields.Many2one('res.company', required=True,
|
||||
default=lambda self: self.env.company)
|
||||
period_from = fields.Date(required=True)
|
||||
period_to = fields.Date(required=True)
|
||||
comparison_mode = fields.Selection([
|
||||
('none', 'None'),
|
||||
('previous_period', 'Previous Period'),
|
||||
('previous_year', 'Previous Year'),
|
||||
], default='none', required=True)
|
||||
|
||||
summary = fields.Text()
|
||||
highlights = fields.Json() # list of strings
|
||||
concerns = fields.Json() # list of strings
|
||||
next_actions = fields.Json() # list of strings
|
||||
|
||||
generated_at = fields.Datetime(default=fields.Datetime.now, required=True)
|
||||
generated_by = fields.Selection([
|
||||
('on_demand', 'On Demand'),
|
||||
('cron', 'Cron'),
|
||||
('templated', 'Templated Fallback'),
|
||||
], default='on_demand', required=True)
|
||||
|
||||
provider = fields.Char(help="LLM provider used (e.g. 'openai', 'claude', 'local'). "
|
||||
"Empty for templated fallback.")
|
||||
|
||||
_unique_period = models.Constraint(
|
||||
'UNIQUE(report_id, company_id, period_from, period_to, comparison_mode)',
|
||||
'Only one commentary cache row per report+period+mode.',
|
||||
)
|
||||
@@ -0,0 +1,424 @@
|
||||
"""The reports engine - orchestrator for all report computation.
|
||||
|
||||
5-method public API. All controllers, AI tools, wizards, exports must
|
||||
go through these methods; no direct ORM aggregation queries from
|
||||
anywhere else.
|
||||
|
||||
Internal pipeline (per report run):
|
||||
1. Validate (period valid, company allowed, report exists)
|
||||
2. Fetch account hierarchy (cached per (company, fiscal_year))
|
||||
3. Aggregate move lines per account (the SQL workhorse)
|
||||
4. Resolve line_specs into report rows
|
||||
5. (Optional) Compute comparison-period rows
|
||||
6. (Optional) Detect anomalies (deferred to later tasks)
|
||||
"""
|
||||
|
||||
import logging
|
||||
from datetime import date, timedelta
|
||||
|
||||
from odoo import _, api, models
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
from ..services.account_hierarchy import build_tree
|
||||
from ..services.date_periods import Period, comparison_period as _comp_period
|
||||
from ..services.drill_down_resolver import fetch_drill_down
|
||||
from ..services.line_resolver import resolve as _resolve_lines
|
||||
from ..services.totaling import TotalLine
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FusionReportEngine(models.AbstractModel):
|
||||
_name = "fusion.report.engine"
|
||||
_description = "Fusion Financial Reports Engine"
|
||||
|
||||
# ============================================================
|
||||
# PUBLIC API (5 methods)
|
||||
# ============================================================
|
||||
|
||||
@api.model
|
||||
def compute_pnl(
|
||||
self, period: Period, *, comparison: str = 'none',
|
||||
company_id: int | None = None, report_code: str | None = None,
|
||||
) -> dict:
|
||||
"""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,
|
||||
)
|
||||
|
||||
@api.model
|
||||
def compute_balance_sheet(
|
||||
self, date_to: date, *, comparison: str = '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, code=report_code,
|
||||
)
|
||||
period = Period(
|
||||
date_from=date(1970, 1, 1),
|
||||
date_to=date_to,
|
||||
label=f"As of {date_to}",
|
||||
)
|
||||
return self._compute(
|
||||
report, period, comparison=comparison, company_id=company_id,
|
||||
)
|
||||
|
||||
@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_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,
|
||||
)
|
||||
|
||||
@api.model
|
||||
def compute_gl(
|
||||
self, period: Period, *, account_ids: list | 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, code=report_code,
|
||||
)
|
||||
company_id = company_id or self.env.company.id
|
||||
result = self._compute(
|
||||
report, period, comparison='none', company_id=company_id,
|
||||
)
|
||||
gl_by_account = {}
|
||||
target_ids = account_ids or list(result.get('account_totals', {}).keys())
|
||||
for acct_id in target_ids:
|
||||
gl_by_account[acct_id] = fetch_drill_down(
|
||||
self.env,
|
||||
account_id=acct_id,
|
||||
date_from=period.date_from,
|
||||
date_to=period.date_to,
|
||||
company_id=company_id,
|
||||
limit=200,
|
||||
)
|
||||
result['gl_by_account'] = gl_by_account
|
||||
return result
|
||||
|
||||
@api.model
|
||||
def drill_down(
|
||||
self, *, account_id: int, period: Period,
|
||||
company_id: int | None = None,
|
||||
) -> list:
|
||||
"""Drill into a report line: list the journal items behind it."""
|
||||
company_id = company_id or self.env.company.id
|
||||
return fetch_drill_down(
|
||||
self.env,
|
||||
account_id=account_id,
|
||||
date_from=period.date_from,
|
||||
date_to=period.date_to,
|
||||
company_id=company_id,
|
||||
limit=500,
|
||||
)
|
||||
|
||||
@api.model
|
||||
def compute_partner_grouped(
|
||||
self, period: Period, *, account_type: str = 'asset_receivable',
|
||||
comparison: str = 'none', company_id: int | None = None,
|
||||
) -> dict:
|
||||
"""Per-partner aggregation report (Aged Receivable, Aged Payable,
|
||||
Partner Ledger).
|
||||
|
||||
Returns a dict with ``rows`` = list of partner-level aggregates.
|
||||
Each row has the partner_id, partner_name, total residual, and
|
||||
aging buckets: current / 1-30 / 31-60 / 61-90 / 90+ days past
|
||||
``period.date_to``.
|
||||
|
||||
SQL-direct for performance: a single GROUP BY query with conditional
|
||||
sum per bucket. Only un-reconciled, posted lines with non-zero
|
||||
residual at the as-of date are included.
|
||||
"""
|
||||
company_id = company_id or self.env.company.id
|
||||
|
||||
accounts = self.env['account.account'].sudo().search([
|
||||
('account_type', '=', account_type),
|
||||
('company_ids', 'in', company_id),
|
||||
])
|
||||
if not accounts:
|
||||
return {
|
||||
'report_type': 'partner_grouped',
|
||||
'account_type': account_type,
|
||||
'period': {
|
||||
'date_from': str(period.date_from),
|
||||
'date_to': str(period.date_to),
|
||||
'label': period.label,
|
||||
},
|
||||
'rows': [],
|
||||
'total': 0.0,
|
||||
'partner_count': 0,
|
||||
}
|
||||
|
||||
as_of = period.date_to
|
||||
d30 = as_of - timedelta(days=30)
|
||||
d60 = as_of - timedelta(days=60)
|
||||
d90 = as_of - timedelta(days=90)
|
||||
|
||||
self.env.cr.execute(
|
||||
"""
|
||||
SELECT
|
||||
COALESCE(p.id, 0) AS partner_id,
|
||||
COALESCE(p.name, '(no partner)') AS partner_name,
|
||||
SUM(aml.amount_residual) AS total_residual,
|
||||
SUM(CASE
|
||||
WHEN aml.date_maturity >= %s
|
||||
OR aml.date_maturity IS NULL
|
||||
THEN aml.amount_residual ELSE 0
|
||||
END) AS bucket_current,
|
||||
SUM(CASE
|
||||
WHEN aml.date_maturity < %s
|
||||
AND aml.date_maturity >= %s
|
||||
THEN aml.amount_residual ELSE 0
|
||||
END) AS bucket_1_30,
|
||||
SUM(CASE
|
||||
WHEN aml.date_maturity < %s
|
||||
AND aml.date_maturity >= %s
|
||||
THEN aml.amount_residual ELSE 0
|
||||
END) AS bucket_31_60,
|
||||
SUM(CASE
|
||||
WHEN aml.date_maturity < %s
|
||||
AND aml.date_maturity >= %s
|
||||
THEN aml.amount_residual ELSE 0
|
||||
END) AS bucket_61_90,
|
||||
SUM(CASE
|
||||
WHEN aml.date_maturity < %s
|
||||
THEN aml.amount_residual ELSE 0
|
||||
END) AS bucket_90_plus,
|
||||
COUNT(*) AS line_count
|
||||
FROM account_move_line aml
|
||||
LEFT JOIN res_partner p ON p.id = aml.partner_id
|
||||
WHERE aml.account_id = ANY(%s)
|
||||
AND aml.parent_state = 'posted'
|
||||
AND aml.reconciled = false
|
||||
AND aml.amount_residual != 0
|
||||
AND aml.company_id = %s
|
||||
AND aml.date <= %s
|
||||
GROUP BY p.id, p.name
|
||||
HAVING SUM(aml.amount_residual) != 0
|
||||
ORDER BY total_residual DESC
|
||||
""",
|
||||
(
|
||||
as_of,
|
||||
as_of, d30,
|
||||
d30, d60,
|
||||
d60, d90,
|
||||
d90,
|
||||
list(accounts.ids), company_id, as_of,
|
||||
),
|
||||
)
|
||||
|
||||
rows = []
|
||||
for r in self.env.cr.dictfetchall():
|
||||
rows.append({
|
||||
'partner_id': r['partner_id'] or False,
|
||||
'partner_name': r['partner_name'] or '(no partner)',
|
||||
'total': float(r['total_residual'] or 0),
|
||||
'bucket_current': float(r['bucket_current'] or 0),
|
||||
'bucket_1_30': float(r['bucket_1_30'] or 0),
|
||||
'bucket_31_60': float(r['bucket_31_60'] or 0),
|
||||
'bucket_61_90': float(r['bucket_61_90'] or 0),
|
||||
'bucket_90_plus': float(r['bucket_90_plus'] or 0),
|
||||
'line_count': r['line_count'],
|
||||
})
|
||||
|
||||
total = sum(r['total'] for r in rows)
|
||||
return {
|
||||
'report_type': 'partner_grouped',
|
||||
'account_type': account_type,
|
||||
'period': {
|
||||
'date_from': str(period.date_from),
|
||||
'date_to': str(period.date_to),
|
||||
'label': period.label,
|
||||
},
|
||||
'company_id': company_id,
|
||||
'rows': rows,
|
||||
'total': total,
|
||||
'partner_count': len(rows),
|
||||
}
|
||||
|
||||
# ============================================================
|
||||
# PRIVATE HELPERS
|
||||
# ============================================================
|
||||
|
||||
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(
|
||||
[('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
|
||||
)
|
||||
return report
|
||||
|
||||
def _fetch_accounts(self, company_id):
|
||||
"""Fetch all accounts for a company, return flat dict + tree."""
|
||||
Account = self.env['account.account'].sudo()
|
||||
records = Account.search([('company_ids', 'in', company_id)])
|
||||
# account.account doesn't carry a parent_id in V19 - we use
|
||||
# account_type prefixes instead, so parent_id is always None here.
|
||||
flat = [
|
||||
{
|
||||
'id': a.id,
|
||||
'code': a.code,
|
||||
'name': a.name,
|
||||
'account_type': a.account_type or '',
|
||||
'parent_id': None,
|
||||
}
|
||||
for a in records
|
||||
]
|
||||
accounts_by_id = {a['id']: a for a in flat}
|
||||
tree = build_tree(flat)
|
||||
return accounts_by_id, tree
|
||||
|
||||
def _aggregate_period(self, period: Period, company_id: int) -> dict:
|
||||
"""SQL aggregate per account_id for a period.
|
||||
|
||||
Raw SQL for performance; this is the perf-critical step."""
|
||||
self.env.cr.execute(
|
||||
"""
|
||||
SELECT account_id,
|
||||
COALESCE(SUM(debit), 0) AS d,
|
||||
COALESCE(SUM(credit), 0) AS c,
|
||||
COALESCE(SUM(balance), 0) AS b
|
||||
FROM account_move_line
|
||||
WHERE parent_state = 'posted'
|
||||
AND company_id = %s
|
||||
AND date >= %s
|
||||
AND date <= %s
|
||||
GROUP BY account_id
|
||||
""",
|
||||
(company_id, period.date_from, period.date_to),
|
||||
)
|
||||
out = {}
|
||||
for row in self.env.cr.fetchall():
|
||||
out[row[0]] = TotalLine(
|
||||
debit=float(row[1] or 0),
|
||||
credit=float(row[2] or 0),
|
||||
balance=float(row[3] or 0),
|
||||
)
|
||||
return out
|
||||
|
||||
def _compute(
|
||||
self, report, period: Period, *, comparison: str,
|
||||
company_id: int | None = None,
|
||||
) -> dict:
|
||||
"""Shared computation pipeline. Returns dict with rows, totals,
|
||||
metadata."""
|
||||
company_id = company_id or self.env.company.id
|
||||
|
||||
accounts_by_id, _tree = self._fetch_accounts(company_id)
|
||||
|
||||
account_totals = self._aggregate_period(period, company_id)
|
||||
|
||||
comp_totals = None
|
||||
comp_period = None
|
||||
if comparison and comparison != 'none':
|
||||
comp_period = _comp_period(period, comparison)
|
||||
if comp_period:
|
||||
comp_totals = self._aggregate_period(comp_period, company_id)
|
||||
|
||||
rows = _resolve_lines(
|
||||
report.line_specs or [],
|
||||
account_totals=account_totals,
|
||||
accounts_by_id=accounts_by_id,
|
||||
comparison_totals=comp_totals,
|
||||
)
|
||||
|
||||
return {
|
||||
'report_id': report.id,
|
||||
'report_name': report.name,
|
||||
'report_type': report.report_type,
|
||||
'period': {
|
||||
'date_from': str(period.date_from),
|
||||
'date_to': str(period.date_to),
|
||||
'label': period.label,
|
||||
},
|
||||
'comparison_period': (
|
||||
{
|
||||
'date_from': str(comp_period.date_from),
|
||||
'date_to': str(comp_period.date_to),
|
||||
'label': comp_period.label,
|
||||
}
|
||||
if comp_period
|
||||
else None
|
||||
),
|
||||
'company_id': company_id,
|
||||
'rows': rows,
|
||||
'account_totals': {
|
||||
aid: tl.balance for aid, tl in account_totals.items()
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
"""Cron handlers for fusion_accounting_reports.
|
||||
|
||||
Two scheduled jobs:
|
||||
- _cron_anomaly_scan: daily P&L variance scan -> persist anomalies
|
||||
- _cron_mv_refresh: every 15 min CONCURRENTLY refresh the MV"""
|
||||
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
|
||||
import odoo
|
||||
from odoo import api, fields, models
|
||||
|
||||
from ..services.anomaly_detection import detect
|
||||
from ..services.date_periods import month_bounds
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FusionReportsCron(models.AbstractModel):
|
||||
_name = "fusion.reports.cron"
|
||||
_description = "Fusion Reports Cron Handlers"
|
||||
|
||||
@api.model
|
||||
def _cron_anomaly_scan(self):
|
||||
"""Run last-month P&L vs prior-year-same-month and persist anomalies."""
|
||||
today = fields.Date.today()
|
||||
# Walk back into the previous full calendar month.
|
||||
last_month = today.replace(day=1) - timedelta(days=1)
|
||||
period = month_bounds(last_month)
|
||||
|
||||
Report = self.env['fusion.report'].sudo()
|
||||
Anomaly = self.env['fusion.report.anomaly'].sudo()
|
||||
engine = self.env['fusion.report.engine']
|
||||
|
||||
for company in self.env['res.company'].search([]):
|
||||
try:
|
||||
pnl_def = Report.search(
|
||||
[
|
||||
('report_type', '=', 'pnl'),
|
||||
'|', ('company_id', '=', company.id),
|
||||
('company_id', '=', False),
|
||||
],
|
||||
limit=1,
|
||||
)
|
||||
if not pnl_def:
|
||||
continue
|
||||
result = engine.compute_pnl(
|
||||
period,
|
||||
comparison='previous_year',
|
||||
company_id=company.id,
|
||||
)
|
||||
anomalies = detect(result)
|
||||
for a in anomalies:
|
||||
existing = Anomaly.search(
|
||||
[
|
||||
('report_id', '=', pnl_def.id),
|
||||
('company_id', '=', company.id),
|
||||
('period_from', '=', period.date_from),
|
||||
('period_to', '=', period.date_to),
|
||||
('row_id', '=', a['row_id']),
|
||||
],
|
||||
limit=1,
|
||||
)
|
||||
vals = {
|
||||
'report_id': pnl_def.id,
|
||||
'company_id': company.id,
|
||||
'period_from': period.date_from,
|
||||
'period_to': period.date_to,
|
||||
'row_id': a['row_id'],
|
||||
'label': a['label'],
|
||||
'current_amount': a['current_amount'],
|
||||
'comparison_amount': a['comparison_amount'],
|
||||
'variance_amount': a['variance_amount'],
|
||||
'variance_pct': a['variance_pct'],
|
||||
'severity': a['severity'],
|
||||
'direction': a['direction'],
|
||||
}
|
||||
if existing:
|
||||
existing.write(vals)
|
||||
else:
|
||||
Anomaly.create(vals)
|
||||
_logger.info(
|
||||
"Anomaly scan for company %s: %d flagged",
|
||||
company.id, len(anomalies),
|
||||
)
|
||||
except Exception as e:
|
||||
_logger.exception(
|
||||
"Anomaly scan failed for company %s: %s", company.id, e,
|
||||
)
|
||||
|
||||
@api.model
|
||||
def _cron_mv_refresh(self):
|
||||
"""REFRESH CONCURRENTLY via dedicated autocommit cursor.
|
||||
|
||||
REFRESH MATERIALIZED VIEW CONCURRENTLY cannot run inside a
|
||||
transaction block, so we open a separate connection with autocommit
|
||||
enabled. The blocking REFRESH is used as a fallback if the
|
||||
concurrent path fails (e.g. on a cold MV with no rows yet)."""
|
||||
try:
|
||||
db_name = self.env.cr.dbname
|
||||
db = odoo.sql_db.db_connect(db_name)
|
||||
with db.cursor() as cron_cr:
|
||||
cron_cr._cnx.set_session(autocommit=True)
|
||||
cron_cr.execute(
|
||||
"REFRESH MATERIALIZED VIEW CONCURRENTLY "
|
||||
"fusion_account_balance_mv"
|
||||
)
|
||||
_logger.debug("MV refresh CONCURRENTLY succeeded")
|
||||
except Exception as e:
|
||||
_logger.warning(
|
||||
"CONCURRENTLY refresh failed (%s); blocking fallback", e)
|
||||
try:
|
||||
self.env['fusion.account.balance.mv']._refresh(
|
||||
concurrently=False)
|
||||
except Exception as e2:
|
||||
_logger.exception(
|
||||
"Blocking MV refresh also failed: %s", e2)
|
||||
Reference in New Issue
Block a user