odoo18/addons_extensions/hr_employee_extended/models/hr_employee.py

82 lines
3.5 KiB
Python

# 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
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)
@api.constrains('identification_id')
def _check_identification_id(self):
for record in self:
if 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
delta = relativedelta(current_date, record.doj)
# Format the experience as 'X years Y months Z days'
experience_str = f"{delta.years} years {delta.months} months {delta.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"