odoo18/addons_extensions/hr_employee_extended/models/hr_employee.py

126 lines
5.3 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)
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 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"
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')