This commit is contained in:
Mazen Abdo 2025-08-24 11:57:04 +03:00
parent c30ff7831e
commit 75a2837a12
1 changed files with 11 additions and 36 deletions

View File

@ -6,8 +6,6 @@ from num2words import num2words
from odoo import api, exceptions, fields, models, _
from odoo.exceptions import ValidationError, Warning
from odoo.tools.translate import _
from datetime import date
class HrEmployeePublic(models.Model):
_inherit = 'hr.employee.public'
@ -254,42 +252,19 @@ class HrEmployee(models.Model):
@api.model
def _default_emp_code(self):
try:
seq = self.env['ir.sequence'].next_by_code('hr.employee') or '/'
emp_seq = self.env['hr.employee'].search([('active', 'in', [False, True])])
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
# 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 '/'
# Get all employee numbers that are digits
emp_seq = self.env['hr.employee'].search([('active', 'in', [False, True])])
numbers = []
for emp in emp_seq:
if emp.emp_no and emp.emp_no.isdigit():
numbers.append(int(emp.emp_no))
# Safely get the maximum number
if numbers:
max_number = max(numbers)
else:
max_number = 0
# Check if sequence needs to be updated
if seq and seq != '/' and seq.isdigit():
seq_number = int(seq)
if seq_number != (max_number + 1):
current_sequence = self.env['ir.sequence'].search([('code', '=', 'hr.employee')], limit=1)
if current_sequence:
current_sequence.sudo().write({'number_next_actual': max_number + 1})
seq = self.env['ir.sequence'].next_by_code('hr.employee') or str(max_number + 1)
# Ensure we always return a valid string
if not seq or seq == '/':
seq = str(max_number + 1)
return str(seq)
except Exception as e:
# Log the error and return a fallback value
# _logger.error("Error in _default_emp_code: %s", str(e))
# Return a timestamp-based fallback or simple increment
return str(int(time.time()) % 1000000) # Last 6 digits of timestamp
return str(seq)
@api.depends('new_gosi')
def _compute_gosi_years(self):