commit
6bb7cfd8ce
|
|
@ -251,25 +251,55 @@ class HrEmployee(models.Model):
|
|||
help='GOSI Years According To The New activation Date Until Today')
|
||||
|
||||
@api.model
|
||||
def _default_emp_code(self):
|
||||
# حاول تجيب الرقم من sequence
|
||||
seq = self.env['ir.sequence'].next_by_code('hr.employee')
|
||||
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():
|
||||
|
||||
if not seq or not str(seq).isdigit():
|
||||
return "1"
|
||||
# Check if sequence needs adjustment
|
||||
employees = self.env['hr.employee'].search([('active', 'in', [False, True])])
|
||||
max_number = 0
|
||||
|
||||
emp_seq = self.env['hr.employee'].search([('active', 'in', [False, True])])
|
||||
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
|
||||
|
||||
numbers = [int(emp.emp_no) for emp in emp_seq if emp.emp_no and emp.emp_no.isdigit()]
|
||||
max_number = max(numbers) if numbers else 0
|
||||
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)
|
||||
|
||||
if int(seq) != (max_number + 1):
|
||||
currnt_sequence = self.env['ir.sequence'].search([('code', '=', 'hr.employee')], limit=1)
|
||||
if currnt_sequence:
|
||||
currnt_sequence.write({'number_next_actual': max_number + 1})
|
||||
seq = self.env['ir.sequence'].next_by_code('hr.employee') or (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:
|
||||
|
|
@ -604,6 +634,10 @@ 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):
|
||||
|
|
|
|||
Loading…
Reference in New Issue