"""Period selection + comparison wizard. Pre-fills date ranges for common report periods (current month, YTD, etc.).""" from datetime import timedelta from odoo import api, fields, models from ..services.date_periods import ( fiscal_year_bounds, month_bounds, quarter_bounds, ) class FusionPeriodPickerWizard(models.TransientModel): _name = "fusion.period.picker.wizard" _description = "Period Selection Wizard" report_type = fields.Selection([ ('pnl', 'P&L'), ('balance_sheet', 'Balance Sheet'), ('trial_balance', 'Trial Balance'), ('general_ledger', 'General Ledger'), ], required=True, default='pnl') period_preset = fields.Selection([ ('this_month', 'This Month'), ('last_month', 'Last Month'), ('this_quarter', 'This Quarter'), ('last_quarter', 'Last Quarter'), ('this_year', 'This Year (YTD)'), ('last_year', 'Last Year'), ('custom', 'Custom Range'), ], default='this_month', required=True) date_from = fields.Date() date_to = fields.Date() comparison = fields.Selection([ ('none', 'No Comparison'), ('previous_period', 'Previous Period'), ('previous_year', 'Previous Year'), ], default='none') @api.onchange('period_preset') def _onchange_period_preset(self): today = fields.Date.today() if self.period_preset == 'this_month': p = month_bounds(today) self.date_from, self.date_to = p.date_from, p.date_to elif self.period_preset == 'last_month': p = month_bounds(today.replace(day=1) - timedelta(days=1)) self.date_from, self.date_to = p.date_from, p.date_to elif self.period_preset == 'this_quarter': p = quarter_bounds(today) self.date_from, self.date_to = p.date_from, p.date_to elif self.period_preset == 'last_quarter': this_q = quarter_bounds(today) p = quarter_bounds(this_q.date_from - timedelta(days=1)) self.date_from, self.date_to = p.date_from, p.date_to elif self.period_preset == 'this_year': p = fiscal_year_bounds(today) self.date_from, self.date_to = p.date_from, today elif self.period_preset == 'last_year': last_year = today.replace(year=today.year - 1) p = fiscal_year_bounds(last_year) self.date_from, self.date_to = p.date_from, p.date_to def action_open_report(self): """Open the fusion reports viewer pre-filled with selected period.""" self.ensure_one() return { 'type': 'ir.actions.client', 'tag': 'fusion_reports', 'context': { 'default_report_type': self.report_type, 'default_date_from': str(self.date_from), 'default_date_to': str(self.date_to), 'default_comparison': self.comparison, }, }