from odoo import models, fields, api class sos_brm_action(models.Model): _name = 'sos_brm_action' _description = 'Business Review' todo_id = fields.Char(string="To-Do ID", readonly= True) name = fields.Char(string="Action Point") priority = fields.Selection([ ('0', '🟢 Low'), ('1', '🟡 Medium'), ('2', '🔴 High'), ('3', '🚨 Urgent') ], string='Priority', default='0') start_date = fields.Date(string="Start Date",default=fields.Date.today) target_date = fields.Date(string="Target Date") end_date = fields.Date(string="Actual end Date") status = fields.Selection([ ('open', 'Open'),('close', 'Closed'),('hold', 'Hold')], default='open' , string="Status") line_ids = fields.One2many('sos_brm_action_lines', 'ref_id', string="Action Details",copy=True) target_date_line_ids = fields.One2many('sos_brm_action_revised_targets', 'ref_id', string="Revised Target Details",copy=True) result = fields.Html(string="Remarks") cross_dept_action = fields.Selection( selection=[ ('cross_dept', 'Cross-Dept'), ('inter_dept', 'Intra-Dept') ], string='Type', default='inter_dept', required=True ) department = fields.Many2one('sos_departments', string='Self Department',required=True, default=lambda self: self._default_department()) responsible_person = fields.Many2one( 'res.users', string='Assigned To',required=True, domain="[('id', 'in', allowed_user_ids)]" ) assigned_by = fields.Many2one( 'res.users', string='Assigned By', default=lambda self: self.env.user ) assigned_from_dept = fields.Many2one('sos_departments', string='Assigned By Dept', default=lambda self: self._default_department()) assigned_to_dept = fields.Many2one('sos_departments', string='Assigned To Dept') latest_target_date = fields.Date( string="Latest Revised Target", compute='_compute_latest_target', store=True, compute_sudo=True, readonly=True, ) latest_target_line_id = fields.Many2one( 'sos_brm_action_lines', string="Latest Target Line", compute='_compute_latest_target', store=True, compute_sudo=True, readonly=True, ) is_sales_user_created = fields.Boolean( compute='_compute_is_sales_user_created', store=True, string='Created by Sales User' ) allowed_user_ids = fields.Many2many('res.users', compute='_compute_allowed_users') reporting_to = fields.Many2one('res.users',related="responsible_person.reporting_to", string='Reporting To') @api.model def _default_department(self): dept = self.env['sos_departments'].search( [('users_line_ids.users', '=', self.env.user.id)], limit=1 ) if dept: return dept.id return False @api.onchange('department') def _onchange_department(self): if self.department: if self.cross_dept_action == "cross_dept": self.assigned_from_dept = self.department.id @api.depends('department','cross_dept_action','assigned_to_dept') def _compute_allowed_users(self): for rec in self: if rec.cross_dept_action == "inter_dept": rec.allowed_user_ids = rec.department.users_line_ids.mapped('users') else: rec.allowed_user_ids = rec.assigned_to_dept.users_line_ids.mapped('users') @api.depends('create_uid') def _compute_is_sales_user_created(self): sales_groups = [ self.env.ref('sos_inventory.sos_sales_user').id, self.env.ref('sos_inventory.sos_inside_sales_user').id ] for record in self: record.is_sales_user_created = any( gid in record.create_uid.groups_id.ids for gid in sales_groups ) def _generate_id(self): sequence_util = self.env['sos_common_scripts'] scope = 'Cross' if self.cross_dept_action == 'cross_dept' else 'Intra' dept_label = ( getattr(self.department, 'short_form', False) or getattr(self.department, 'name', 'NoDept') ) type_id = f"{scope}/{dept_label}" return sequence_util.generate_sequence('sos_brm_action', type_id, 'todo_id') @api.depends('line_ids.target_date', 'line_ids.create_date') def _compute_latest_target(self): Line = self.env['sos_brm_action_lines'] for rec in self: latest = Line.search([ ('ref_id', '=', rec.id), ('target_date', '!=', False), ], order='target_date desc, id desc', limit=1) if not latest: latest = Line.search([('ref_id', '=', rec.id)], order='create_date desc, id desc', limit=1) rec.latest_target_line_id = latest.id or False rec.latest_target_date = latest.target_date or False def action_revise_target(self): print("bye") @api.model def create(self, vals): record = super().create(vals) if record.priority == 0 : priority_emo='🟢 Low' elif record.priority == 1 : priority_emo='🟡 Medium' elif record.priority == 2 : priority_emo='🔴 High' else: priority_emo='🚨 Urgent' # Set assigned_by record.assigned_by = self.env.user.id # Send email if responsible person exists and is different responsible_id = record.responsible_person process_incharge = record.reporting_to if record.cross_dept_action == "cross_dept": if process_incharge.id != self.env.user.id: cc_mail_id = process_incharge.login else: cc_mail_id = "" if responsible_id and responsible_id.id != self.env.user.id: body_html = f"""

Below Action Plan is assigned to your Department

Action Plan : {record.name}

Priority : {priority_emo}

Assigned By Dept : {record.assigned_from_dept.name}

Assigned By : {self.env.user.name}

""" subject = "Action Plan - Notification" self.env['sos_common_scripts'].send_direct_email( self.env, "sos_brm_action", record.id, responsible_id.login, subject, body_html, cc_mail_id ) else: if responsible_id and responsible_id.id != self.env.user.id: body_html = f"""

Below Action Plan is assigned to you

Action Plan : {record.name}

Priority : {priority_emo}

Assigned By : {self.env.user.name}

""" subject = "Action Plan - Notification" self.env['sos_common_scripts'].send_direct_email( self.env, "sos_brm_action", record.id, responsible_id.login, subject, body_html ) # Now department is guaranteed → Generate ID dept_label = record.department.short_form or record.department.name or 'NoDept' scope = 'Cross' if record.cross_dept_action == 'cross_dept' else 'Intra' type_id = f"{scope}/{dept_label}" record.todo_id = self.env['sos_common_scripts'].generate_sequence('sos_brm_action', type_id, 'todo_id') return record def action_assign_action_btn(self): return { 'name': "Assign Action to Other Department", 'type': 'ir.actions.act_window', 'res_model': 'sos_brm_action', 'view_mode': 'form', 'view_id': self.env.ref('sos_brm.view_form_sos_brm_action_cross_dept').id, 'target': 'new', 'context': { 'from_wizard': True, 'default_cross_dept_action': 'cross_dept' } } class sos_brm_action_lines(models.Model): _name = 'sos_brm_action_lines' _description = 'Business Review' ref_id = fields.Many2one('sos_brm_action', string="BRM action lines", ondelete="cascade") name = fields.Char(string="Action Plan") start_date = fields.Date(string="Entry Date") target_date = fields.Date(string="Revised Target Date") result = fields.Text(string="Result") status = fields.Selection([ ('open', 'Open'),('close', 'Closed'),('hold', 'Hold')], default='open' , string="Status") class sos_revised_targets(models.Model): _name = 'sos_brm_action_revised_targets' _description = 'Business Review' ref_id = fields.Many2one('sos_brm_action', string="BRM action lines", ondelete="cascade") revised_date = fields.Date(string="Revised Date")