Files
Odoo-Modules/fusion_accounting_assets/wizards/depreciation_run_wizard.py
2026-04-19 20:06:25 -04:00

73 lines
2.4 KiB
Python

"""Manual depreciation run wizard.
Operator picks a period_date and the wizard posts all running assets'
unposted lines whose scheduled_date <= period_date."""
from odoo import fields, models
class FusionDepreciationRunWizard(models.TransientModel):
_name = "fusion.depreciation.run.wizard"
_description = "Manual Depreciation Run Wizard"
period_date = fields.Date(
required=True, default=fields.Date.today,
help="Post all unposted lines whose scheduled_date is on or before this date.",
)
state_filter = fields.Selection([
('all_running', 'All Running Assets'),
('selected', 'Selected Asset(s) Only'),
], default='all_running', required=True)
asset_ids = fields.Many2many(
'fusion.asset', domain=[('state', '=', 'running')],
)
state = fields.Selection(
[('draft', 'Draft'), ('done', 'Done')], default='draft',
)
posted_count = fields.Integer(readonly=True)
skipped_count = fields.Integer(readonly=True)
error_count = fields.Integer(readonly=True)
summary = fields.Text(readonly=True)
def action_run(self):
self.ensure_one()
if self.state_filter == 'all_running':
assets = self.env['fusion.asset'].search([
('state', '=', 'running'),
('company_id', '=', self.env.company.id),
])
else:
assets = self.asset_ids
engine = self.env['fusion.asset.engine']
posted = 0
skipped = 0
errors = []
for asset in assets:
try:
with self.env.cr.savepoint():
result = engine.post_depreciation_entry(
asset, period_date=self.period_date,
)
posted += result.get('posted_count', 0)
if result.get('posted_count', 0) == 0:
skipped += 1
except Exception as e: # noqa: BLE001
errors.append(f"{asset.name}: {e}")
self.write({
'state': 'done',
'posted_count': posted,
'skipped_count': skipped,
'error_count': len(errors),
'summary': '\n'.join(errors[:20]) if errors else False,
})
return {
'type': 'ir.actions.act_window',
'res_model': self._name,
'res_id': self.id,
'view_mode': 'form',
'target': 'new',
}