# Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo import _, api, fields, models from dateutil.relativedelta import relativedelta from odoo.exceptions import ValidationError from datetime import datetime, timedelta from calendar import monthrange class HrEmployeeBase(models.AbstractModel): _inherit = "hr.employee.base" employee_id = fields.Char(string='Employee Code') doj = fields.Date(string='Date of Joining') pan_no = fields.Char(string='PAN No') # Add computed field for experience previous_exp = fields.Integer(string='Previous Experience (Months)', default=0) current_company_exp = fields.Char(string='Current Experience', compute='_compute_experience', store=True) total_exp = fields.Char(string='Total Experience', compute='_compute_total_experience', store=True) emp_type = fields.Many2one('hr.contract.type', "Employee Type", tracking=True) blood_group = fields.Selection([ ('A+', 'A+'), ('A-', 'A-'), ('B+', 'B+'), ('B-', 'B-'), ('O+', 'O+'), ('O-', 'O-'), ('AB+', 'AB+'), ('AB-', 'AB-'), ], string="Blood Group") @api.constrains('identification_id') def _check_identification_id(self): for record in self: if record.identification_id and not record.identification_id.isdigit(): raise ValidationError("Identification ID should only contain numbers.") @api.depends('doj') def _compute_experience(self): for record in self: if record.doj: # Get the current date current_date = fields.Date.today() # Calculate the difference between current date and doj def calculate_experience(joined_date, current_date): # Start by calculating the difference in years and months delta_years = current_date.year - joined_date.year delta_months = current_date.month - joined_date.month delta_days = current_date.day - joined_date.day # Adjust months and years if necessary if delta_months < 0: delta_years -= 1 delta_months += 12 # Handle day adjustment if necessary (i.e., current day is less than the joined day) if delta_days < 0: # Subtract one month to adjust delta_months -= 1 # Get the number of days in the previous month to add to the days if current_date.month == 1: days_in_last_month = monthrange(current_date.year - 1, 12)[1] else: days_in_last_month = monthrange(current_date.year, current_date.month - 1)[1] delta_days += days_in_last_month # Final adjustment: if months become negative after adjusting days, fix that if delta_months < 0: delta_years -= 1 delta_months += 12 return delta_years, delta_months, delta_days # Calculate the experience years, months, days = calculate_experience(record.doj, current_date) # Format the experience as 'X years Y months Z days' experience_str = f"{years} years {months} months {days} days" record.current_company_exp = experience_str else: record.current_company_exp = '0 years 0 months 0 days' @api.depends('doj', 'previous_exp') def _compute_total_experience(self): for record in self: if record.doj: # Get the current date current_date = fields.Date.today() # Calculate the current experience in years, months, and days delta = relativedelta(current_date, record.doj) current_years = delta.years current_months = delta.months current_days = delta.days # Convert previous experience (in months) to years, months, and days previous_years = record.previous_exp // 12 previous_months = record.previous_exp % 12 previous_days = 0 # No days in previous_exp, it's already in months # Add the previous experience to the current experience total_years = current_years + previous_years total_months = current_months + previous_months total_days = current_days + previous_days # Adjust if months > 12 or days > 30 if total_months >= 12: total_years += total_months // 12 total_months = total_months % 12 if total_days >= 30: total_months += total_days // 30 total_days = total_days % 30 # Format the total experience as 'X years Y months Z days' total_experience_str = f"{total_years} years {total_months} months {total_days} days" record.total_exp = total_experience_str else: # If there's no DOJ, total experience is just the previous experience total_years = record.previous_exp // 12 total_months = record.previous_exp % 12 record.total_exp = f"{total_years} years {total_months} months 0 days" class HrEmployee(models.Model): _inherit = 'hr.employee' education_history = fields.One2many('education.history','employee_id', string='Education Details') employer_history = fields.One2many('employer.history','employee_id', string='Education Details') family_details = fields.One2many('family.details','employee_id',string='Family Details') permanent_street = fields.Char(string="permanent Street", groups="hr.group_hr_user") permanent_street2 = fields.Char(string="permanent Street2", groups="hr.group_hr_user") permanent_city = fields.Char(string="permanent City", groups="hr.group_hr_user") permanent_state_id = fields.Many2one( "res.country.state", string="permanent State", domain="[('country_id', '=?', private_country_id)]", groups="hr.group_hr_user") permanent_zip = fields.Char(string="permanent Zip", groups="hr.group_hr_user") permanent_country_id = fields.Many2one("res.country", string="permanent Country", groups="hr.group_hr_user") marriage_anniversary_date = fields.Date(string='Anniversary Date', tracking=True) passport_start_date = fields.Date(string='Passport Issued Date') passport_end_date = fields.Date(string='Passport End Date') passport_issued_location = fields.Char(string='Passport Issued Location') previous_company_pf_no = fields.Char(string='Previous Company PF No') previous_company_uan_no = fields.Char(string='Previous Company UAN No') def hr_employee_event_remainder(self): self.send_birthday_reminders() today = fields.Date.today() tomorrow = today + timedelta(days=1) day_after = today + timedelta(days=2) tomorrow_md = tomorrow.strftime('-%m-%d') day_after_md = day_after.strftime('-%m-%d') if today.weekday() == 4: # Friday birthday_domain = ['|', ('birthday', 'like', f'%{tomorrow_md}'), ('birthday', 'like', f'%{day_after_md}')] anniversary_domain = [('marital', '=', 'married'), '|', ('marriage_anniversary_date','like', f'%{tomorrow_md}'), ('marriage_anniversary_date', 'like', f'%{day_after_md}')] else: birthday_domain = [('birthday','like', f'%{tomorrow_md}')] anniversary_domain = [('marital', '=', 'married'), ('marriage_anniversary_date','like', f'%{tomorrow_md}')] birthday_emps = self.search(birthday_domain) anniversary_emps = self.search(anniversary_domain) birthday_list = [{'name': emp.name, 'birthday': emp.birthday} for emp in birthday_emps] anniversary_list = [{'name': emp.name, 'marriage_anniversary_date': emp.marriage_anniversary_date} for emp in anniversary_emps] context = { 'birthday_list': birthday_list, 'anniversary_list': anniversary_list, } email_values = {'auto_delete': True} template = self.env.ref('hr_employee_extended.hr_employee_event_reminder_template') if template and (birthday_list or anniversary_list): hr_id = self.env['ir.config_parameter'].sudo().get_param('hr_employee_extended.emp_hr_id') if hr_id: emp_hr = self.env['res.users'].sudo().browse(int(hr_id)) if hr_id else False email_values['email_to'] = emp_hr.email else: emp_hr = self.env['res.users'].sudo().search([('groups_id', 'in', self.env.ref('hr.group_hr_manager').id)],limit=1) # Use `self[0]` as dummy record for sending template.with_context(**context).send_mail( emp_hr.employee_id.id, force_send=True, email_values=email_values, ) @api.model def send_birthday_reminders(self): today = datetime.today().strftime('%m-%d') employees = self.search([('birthday', '!=', False)]) birthday_emps = employees.filtered( lambda emp: emp.birthday.strftime('%m-%d') == today ) if birthday_emps: channel = self.env['discuss.channel'].search([('name', 'ilike', 'General')], limit=1) if channel: for emp in birthday_emps: message = f"🎉 Happy Birthday {emp.name}! 🎂 Wishing you a fantastic day! 🥳" channel.message_post( body=message, message_type='comment', subtype_xmlid='mail.mt_comment' )