from odoo import models, fields, api, _ from odoo.exceptions import ValidationError from datetime import date class HRDisciplinaryAction(models.Model): _name = 'hr.employee.disciplinary' _inherit = ['mail.thread', 'mail.activity.mixin'] _description = 'Employee Disciplinary Management' active = fields.Boolean(default=True) name = fields.Char('Reference', copy=False, readonly=True, default=lambda x: _('New')) employee_id = fields.Many2one('hr.employee', string="Employee", required=True) company_id = fields.Many2one('res.company', string="Company", required=True, default=lambda self: self.env.company) employee_code = fields.Char(string='Employee Code', related='employee_id.employee_id',tracking=True,required=True) # unit_id = fields.Many2one('unit.master', string="Unit",tracking=True) department_id = fields.Many2one('hr.department', string="Department",tracking=True) designation_id = fields.Many2one('hr.job', string="Designation",tracking=True) doj = fields.Date(string="Date of Joining",tracking=True) referred_by_id = fields.Many2one('res.users', string="Referred By",tracking=True) loss_of_cost = fields.Float(string="Loss of Cost") # employee_section_id = fields.Many2one('section.master',string='Section') disciplinary_complaint_line_ids = fields.One2many('hr.disciplinary.complaint.line','disciplinary_id',string = 'Complaint Lines') disciplinary_action_line_ids = fields.One2many('hr.disciplinary.action.line','disciplinary_id',string = 'Action Lines') state = fields.Selection([ ('new', 'New'), ('submitted', 'Submitted'), ('pending', 'Pending'), ('closed', 'Closed'), ('cancel', 'Cancel') ], default='new',tracking=True,string='State') complaint_name = fields.Text('Complaint', compute='_compute_complaint_name', store=True) name_1 = fields.Char('Name') disciplinary_id = fields.Many2one('hr.employee.disciplinary', string="Disciplinary") complaint_date = fields.Date('Complaint Date') language_id = fields.Many2one('res.lang', 'Language') complaint_type_id = fields.Many2one('disciplinary.complaint.type', string="Complaint Type") mistake_type_id = fields.Many2one('disciplinary.mistake.type', string="Mistake Type", required=True) complaint = fields.Char(string='Complaints') employee_id_2 = fields.Many2one('hr.employee', string='Employee') related_record_count = fields.Integer(string="Disciplinary Action Records Count", compute="_compute_related_record_count") # general_cat = fields.Many2one('general.category', string="General Category", tracking=True) # cat_id = fields.Many2one('hr.category','Category') occurrences = fields.Integer('Occurrences', store=True) severe = fields.Char('Severe') major = fields.Char('Major') less_major = fields.Char('Less Major') negligible = fields.Char('Negligible') normal = fields.Char('Normal') total_mistakes = fields.Char('Total Mistakes') memo = fields.Char('Memo') explanation = fields.Char('Explanation') show_cause = fields.Char('Show Cause') charge_sheet = fields.Char('Charge Sheet') warning = fields.Char('Warning') enquiry_notice = fields.Char('Enquiry Notice') recovery_order = fields.Char('Recovery_ Order') stoppage_of_increment = fields.Char('Stoppage Of Increment') demotion = fields.Char('Demotion') total_actions = fields.Char('Total Actions') normal_action = fields.Char('Normal Actions') suspension = fields.Char('Suspension') total_cost = fields.Float('Total Cost') @api.depends('employee_id') def _compute_related_record_count(self): for record in self: record.related_record_count = self.env['hr.employee.disciplinary'].search_count([('employee_id', '=', record.employee_id.id)]) def action_open_related_records(self): return { 'name': 'Disciplinary Action Records', 'type': 'ir.actions.act_window', 'res_model': 'hr.employee.disciplinary', 'view_mode': 'list', 'domain': [('employee_id', '=', self.employee_id.id)], 'context': {'default_employee_id': self.employee_id.id}, } @api.depends('disciplinary_complaint_line_ids.complaint') def _compute_complaint_name(self): for record in self: complaints = record.disciplinary_complaint_line_ids.mapped('complaint') record.complaint_name = "\n".join(filter(None, complaints)) def action_set_submitted(self): self.state = 'submitted' def action_set_pending(self): self.state = 'pending' def action_set_closed(self): self.state = 'closed' def action_set_cancel(self): self.state = 'cancel' def action_reset_to_new(self): self.state = 'new' @api.model_create_multi def create(self, vals_list): for vals in vals_list: if not vals.get('name') or vals['name'] == _('New'): vals['name'] = self.env['ir.sequence'].next_by_code('hr.employee.sequence') or _('New') return super().create(vals_list) @api.onchange('employee_id') def _onchange_employee_id(self): for rec in self: if rec.employee_id: rec.employee_code = rec.employee_id.employee_id or '' rec.department_id = rec.employee_id.department_id.id rec.designation_id = rec.employee_id.job_id.id rec.doj = rec.employee_id.doj rec.company_id = rec.employee_id.company_id.id # rec.unit_id = rec.employee_id.unit_name_hr.id if rec.employee_id.unit_name_hr else False # rec.employee_section_id = rec.employee_id.section_name_hr.id if rec.employee_id.section_name_hr else False else: rec.employee_code = False rec.department_id = False rec.designation_id = False rec.doj = False class DisciplinaryComplaintLine(models.Model): _name = 'hr.disciplinary.complaint.line' _description = 'Disciplinary Complaint Line' name = fields.Char('Name') disciplinary_id = fields.Many2one('hr.employee.disciplinary',string="Disciplinary") complaint_date = fields.Date('Complaint Date') language_id = fields.Many2one('res.lang','Language') complaint_type_id = fields.Many2one('disciplinary.complaint.type',string="Complaint Type") mistake_type_id = fields.Many2one('disciplinary.mistake.type',string="Mistake Type") complaint = fields.Char(string='Complaints') employee_id = fields.Many2one('hr.employee', string='Employee') class DisciplinaryActionLine(models.Model): _name = 'hr.disciplinary.action.line' _description = 'Disciplinary Action Line' name = fields.Char('Name') disciplinary_id = fields.Many2one('hr.employee.disciplinary',string="Disciplinary") action_taken_date = fields.Date('Action On') action_type_id = fields.Many2one('disciplinary.action.type',string="Action Type") action = fields.Char(string='Description') action_name = fields.Char('ActionName') related_complaint_id = fields.Many2one('hr.disciplinary.complaint.line', string="Related Complaint", domain="[('disciplinary_id', '=', disciplinary_id)]") employee_id = fields.Many2one('hr.employee', string='Employee') @api.constrains('action_taken_date') def _check_action_taken_date(self): for record in self: if record.action_taken_date and record.action_taken_date > date.today(): raise ValidationError("The Action On date cannot be in the future.") class DisciplinaryActionType(models.Model): _name = 'disciplinary.action.type' _description = 'Action Type' name = fields.Char('Name', required=True) class DisciplinaryComplaintType(models.Model): _name = 'disciplinary.complaint.type' _description = 'Complaint Type' name = fields.Char('Name', required=True) class DisciplinaryMistakeType(models.Model): _name = 'disciplinary.mistake.type' _description = 'Mistake Type' name = fields.Char('Name', required=True)