This commit is contained in:
Mazen Abdo 2025-08-24 12:49:01 +03:00
parent d2e2c74e68
commit 91a9faf2fd
1 changed files with 15 additions and 51 deletions

View File

@ -84,7 +84,7 @@ class HrEmployee(models.Model):
r_name = fields.Char("Name")
# fields of page work information in employees view
emp_no = fields.Char(string="Employee number", tracking=True)
emp_no = fields.Char(string="Employee number", tracking=True, default=lambda self: self._default_emp_code())
english_name = fields.Char(string="English Name")
home_no = fields.Char()
present_address = fields.Char()
@ -251,55 +251,23 @@ class HrEmployee(models.Model):
help='GOSI Years According To The New activation Date Until Today')
@api.model
def _generate_emp_code(self):
"""Generate employee code safely"""
try:
# Try sequence first
seq = self.env['ir.sequence'].next_by_code('hr.employee')
if seq and seq != '/' and str(seq).isdigit():
def _default_emp_code(self):
seq = self.env['ir.sequence'].next_by_code('hr.employee') or '/'
emp_seq = self.env['hr.employee'].search([('active', 'in', [False, True])])
# Check if sequence needs adjustment
employees = self.env['hr.employee'].search([('active', 'in', [False, True])])
max_number = 0
# Get the maximum current employee number
max_number = 0
if emp_seq:
max_number = max(int(emp.emp_no) for emp in emp_seq if emp.emp_no and emp.emp_no.isdigit())
for emp in employees:
if emp.emp_no and str(emp.emp_no).isdigit():
try:
emp_num = int(emp.emp_no)
if emp_num > max_number:
max_number = emp_num
except (ValueError, TypeError):
continue
# Ensure the sequence matches the max number + 1
if int(seq) != (max_number + 1):
currnt_sequence = self.env['ir.sequence'].search([('code', '=', 'hr.employee')], limit=1)
currnt_sequence.write({'number_next_actual': max_number + 1})
seq = self.env['ir.sequence'].next_by_code('hr.employee') or '/'
seq_int = int(seq)
if seq_int != (max_number + 1):
current_sequence = self.env['ir.sequence'].search([('code', '=', 'hr.employee')], limit=1)
if current_sequence:
current_sequence.write({'number_next_actual': max_number + 1})
seq = self.env['ir.sequence'].next_by_code('hr.employee') or str(max_number + 1)
return str(seq)
return str(seq)
# Fallback if sequence fails or returns non-digit
employees = self.env['hr.employee'].search([('active', 'in', [False, True])])
max_number = 0
for emp in employees:
if emp.emp_no and str(emp.emp_no).isdigit():
try:
emp_num = int(emp.emp_no)
if emp_num > max_number:
max_number = emp_num
except (ValueError, TypeError):
continue
return str(max_number + 1)
except Exception:
# Ultimate fallback
count = self.env['hr.employee'].search_count([]) + 1
return str(count)
@api.depends('new_gosi')
def _compute_gosi_years(self):
for emp in self:
@ -634,10 +602,6 @@ class HrEmployee(models.Model):
@api.model
def create(self, vals):
# Generate employee number if not provided
if not vals.get('emp_no'):
vals['emp_no'] = self._generate_emp_code()
new_record = super(HrEmployee, self).create(vals)
update_list = []
if vals.get("passport_id", False):
@ -661,7 +625,7 @@ class HrEmployee(models.Model):
if "context" in dir(self.env) and new_record.name:
if new_record.english_name:
new_record.translate_employee_name()
# seq = self.env['ir.sequence'].next_by_code('hr.employee') or '/'
return new_record
@api.constrains("emp_no", "birthday", 'attachment_ids')