Merge pull request #4302 from expsa/maz_ba_ox

Maz ba ox
This commit is contained in:
mazenmuhamad 2025-08-24 12:39:17 +03:00 committed by GitHub
commit 6bb7cfd8ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 48 additions and 14 deletions

View File

@ -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):