diff --git a/fusion_accounting_assets/__manifest__.py b/fusion_accounting_assets/__manifest__.py index 47ced6ac..f13587c1 100644 --- a/fusion_accounting_assets/__manifest__.py +++ b/fusion_accounting_assets/__manifest__.py @@ -1,6 +1,6 @@ { 'name': 'Fusion Accounting Assets', - 'version': '19.0.1.0.16', + 'version': '19.0.1.0.17', 'category': 'Accounting/Accounting', 'summary': 'AI-augmented asset management with depreciation schedules.', 'description': """ @@ -33,6 +33,7 @@ menu hides; the engine + AI tools remain available for the chat. ], 'data': [ 'security/ir.model.access.csv', + 'data/cron.xml', ], 'assets': { 'web.assets_backend': [ diff --git a/fusion_accounting_assets/data/cron.xml b/fusion_accounting_assets/data/cron.xml new file mode 100644 index 00000000..7363bd6e --- /dev/null +++ b/fusion_accounting_assets/data/cron.xml @@ -0,0 +1,24 @@ + + + + + Fusion Assets — Post Due Depreciation + + code + model._cron_post_due_depreciation() + 1 + days + + + + + Fusion Assets — Monthly Anomaly Scan + + code + model._cron_anomaly_scan() + 30 + days + + + + diff --git a/fusion_accounting_assets/models/__init__.py b/fusion_accounting_assets/models/__init__.py index 5112fbcf..ef38f3a4 100644 --- a/fusion_accounting_assets/models/__init__.py +++ b/fusion_accounting_assets/models/__init__.py @@ -5,3 +5,4 @@ from . import fusion_asset_disposal from . import fusion_asset_anomaly from . import account_move from . import fusion_asset_engine +from . import fusion_assets_cron diff --git a/fusion_accounting_assets/models/fusion_assets_cron.py b/fusion_accounting_assets/models/fusion_assets_cron.py new file mode 100644 index 00000000..4bf16429 --- /dev/null +++ b/fusion_accounting_assets/models/fusion_assets_cron.py @@ -0,0 +1,85 @@ +"""Cron handlers for fusion_accounting_assets. + +- _cron_post_due_depreciation: daily, post due depreciation lines for running assets +- _cron_anomaly_scan: monthly, scan for schedule variance and create anomaly records +""" + +import logging + +from odoo import api, fields, models + +from ..services.anomaly_detection import detect_schedule_variance + +_logger = logging.getLogger(__name__) + + +class FusionAssetsCron(models.AbstractModel): + _name = "fusion.assets.cron" + _description = "Fusion Assets Cron Handlers" + + @api.model + def _cron_post_due_depreciation(self): + """For each running asset, post any due un-posted depreciation lines.""" + today = fields.Date.today() + engine = self.env['fusion.asset.engine'] + Asset = self.env['fusion.asset'] + running_assets = Asset.search([('state', '=', 'running')]) + posted_total = 0 + for asset in running_assets: + try: + with self.env.cr.savepoint(): + result = engine.post_depreciation_entry(asset, period_date=today) + posted_total += result.get('posted_count', 0) + except Exception as e: # noqa: BLE001 + _logger.warning("Cron post failed for asset %s: %s", asset.id, e) + _logger.info( + "Cron: posted depreciation on %d lines across %d running assets", + posted_total, len(running_assets), + ) + + @api.model + def _cron_anomaly_scan(self): + """For each running asset, compare expected accumulated depreciation + vs posted, and persist any variance flags.""" + Asset = self.env['fusion.asset'] + Anomaly = self.env['fusion.asset.anomaly'] + running_assets = Asset.search([('state', '=', 'running')]) + flagged = 0 + today = fields.Date.today() + for asset in running_assets: + try: + expected = sum( + l.amount for l in asset.depreciation_line_ids + if l.scheduled_date and l.scheduled_date <= today + ) + actual = asset.total_depreciated + anomaly = detect_schedule_variance( + asset_id=asset.id, asset_name=asset.name, + expected_accumulated=expected, actual_accumulated=actual, + ) + if anomaly is None: + continue + anomaly_dict = anomaly.to_dict() + existing = Anomaly.search([ + ('asset_id', '=', asset.id), + ('anomaly_type', '=', anomaly_dict['anomaly_type']), + ('state', 'in', ('new', 'acknowledged')), + ], limit=1) + if existing: + continue + Anomaly.create({ + 'asset_id': asset.id, + 'anomaly_type': anomaly_dict['anomaly_type'], + 'severity': anomaly_dict['severity'], + 'expected': anomaly_dict['expected'], + 'actual': anomaly_dict['actual'], + 'variance_pct': anomaly_dict['variance_pct'], + 'detail': anomaly_dict['detail'], + }) + flagged += 1 + except Exception as e: # noqa: BLE001 + _logger.warning("Cron anomaly scan failed for asset %s: %s", asset.id, e) + _logger.info( + "Cron: scanned %d assets, flagged %d anomalies", + len(running_assets), flagged, + ) diff --git a/fusion_accounting_assets/tests/__init__.py b/fusion_accounting_assets/tests/__init__.py index 343b0c49..9a0e65ee 100644 --- a/fusion_accounting_assets/tests/__init__.py +++ b/fusion_accounting_assets/tests/__init__.py @@ -14,3 +14,4 @@ from . import test_engine_integration from . import test_assets_controller from . import test_assets_adapter from . import test_asset_tools +from . import test_assets_cron diff --git a/fusion_accounting_assets/tests/test_assets_cron.py b/fusion_accounting_assets/tests/test_assets_cron.py new file mode 100644 index 00000000..fafaa804 --- /dev/null +++ b/fusion_accounting_assets/tests/test_assets_cron.py @@ -0,0 +1,28 @@ +"""Cron handler smoke tests.""" + +from datetime import date + +from odoo.tests import tagged +from odoo.tests.common import TransactionCase + + +@tagged('post_install', '-at_install') +class TestFusionAssetsCron(TransactionCase): + + def setUp(self): + super().setUp() + self.cron = self.env['fusion.assets.cron'] + self.asset = self.env['fusion.asset'].create({ + 'name': 'Cron Test', 'cost': 4000, + 'acquisition_date': date(2026, 1, 1), + 'in_service_date': date(2026, 1, 1), + 'method': 'straight_line', 'useful_life_years': 4, + }) + self.env['fusion.asset.engine'].compute_depreciation_schedule(self.asset) + self.asset.action_set_running() + + def test_cron_post_due_depreciation_runs(self): + self.cron._cron_post_due_depreciation() + + def test_cron_anomaly_scan_runs(self): + self.cron._cron_anomaly_scan()