odoo18/addons_extensions/hr_employee_extended/models/hr_employee.py

159 lines
6.9 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
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 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')