from odoo import models, api from odoo.exceptions import UserError from datetime import date, timedelta from calendar import month_name from collections import defaultdict class BRM_WeekSummaryReport(models.AbstractModel): _name = 'report.sos_brm.report_brm_action_plan_summary' _description = 'BRM Action Plan Summary Report' @api.model def _get_report_values(self, docids, data=None): if not data: raise UserError("No filter data provided for the report.") done_by = data.get('done_by') status = data.get('status') department = data.get('department') department_name = data.get('department_name') cross_dept_action = data.get('cross_dept_action') domain = [] if done_by: domain.append(('responsible_person', '=', int(done_by))) if department: domain += [ '|', ('department', '=', department), ('assigned_to_dept', '=', department) ] if status and status != 'all': domain.append(('status', '=', status)) if cross_dept_action != "all": domain.append(('cross_dept_action', '=', cross_dept_action)) # Main actions docs = self.env['sos_brm_action'].search(domain, order='target_date asc, id asc') # Subsets cross = docs.filtered(lambda r: r.cross_dept_action == 'cross_dept') local = docs.filtered(lambda r: r.cross_dept_action == 'inter_dept') # Prefetch all child lines once and group by parent id ActionLines = self.env['sos_brm_action_lines'] lines_map = defaultdict(list) # action_id -> list of line records if docs: all_lines = ActionLines.search([ ('ref_id', 'in', docs.ids) ], order='ref_id, start_date, id') for ln in all_lines: lines_map[ln.ref_id.id].append(ln) return { 'doc_ids': docs.ids, 'doc_model': 'sos_brm_action', 'done_by': self.env['res.users'].browse(int(done_by)) if done_by else self.env['res.users'], 'status': status or '', 'department': department_name or '', 'docs': docs, 'cross': cross, 'local': local, 'count_cross': len(cross), 'count_local': len(local), 'cross_dept_action':cross_dept_action, 'lines_map': lines_map, }