Merge pull request #5406 from expsa/maz_hr_multicompany_employee

Maz hr multicompany employee
This commit is contained in:
mazenmuhamad 2025-11-13 14:13:15 +02:00 committed by GitHub
commit 1ff8aca934
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import models

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
{
'name': 'HR Multi-Company Employee Number',
'version': '14.0.1.0.0',
'category': 'Human Resources',
'summary': 'Fix employee number generation for multi-company setup',
'description': """
This module fixes the employee number generation issue when creating employees
from res.users in a multi-company environment.
""",
'author': 'Custom Development',
'depends': ['hr_base', 'hr'],
'data': [
'security/ir.model.access.csv',
'data/ir_sequence_data.xml',
],
'installable': True,
'application': False,
'auto_install': False,
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<record id="seq_hr_employee_global" model="ir.sequence">
<field name="name">Global Employee Number</field>
<field name="code">hr.employee.global</field>
<field name="prefix">EMP-</field>
<field name="padding">4</field>
<field name="number_next">1</field>
<field name="number_increment">1</field>
<field name="company_id" eval="False"/>
</record>
</data>
</odoo>

View File

@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import hr_employee

View File

@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-
from datetime import date
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError
class HrEmployee(models.Model):
_inherit = "hr.employee"
@api.model
def _default_emp_code(self):
seq = self.env['ir.sequence'].next_by_code('hr.employee.global')
if not seq:
# If sequence doesn't exist, create it
self._create_global_sequence()
seq = self.env['ir.sequence'].next_by_code('hr.employee.global')
return seq or '/'
@api.model
def _create_global_sequence(self):
existing_sequence = self.env['ir.sequence'].search([
('code', '=', 'hr.employee.global')
], limit=1)
if not existing_sequence:
# Find the maximum employee number in the system
all_employees = self.env['hr.employee'].search([
('active', 'in', [False, True])
])
max_number = 0
for emp in all_employees:
if emp.emp_no and emp.emp_no.startswith('EMP-'):
try:
number_part = emp.emp_no.replace('EMP-', '')
if number_part.isdigit():
max_number = max(max_number, int(number_part))
except (ValueError, AttributeError):
continue
self.env['ir.sequence'].sudo().create({
'name': 'Global Employee Number',
'code': 'hr.employee.global',
'implementation': 'standard',
'prefix': 'EMP-',
'padding': 4,
'number_increment': 1,
'number_next_actual': max_number + 1,
'company_id': False,
})
@api.model
def create(self, vals):
if not vals.get('emp_no'):
vals['emp_no'] = self._default_emp_code()
if 'company_id' not in vals:
vals['company_id'] = self.env.context.get('default_company_id') or self.env.company.id
return super(HrEmployee, self).create(vals)
@api.constrains("emp_no", "birthday", "attachment_ids")
def e_unique_field_name_constrains(self):
for rec in self:
# Check employee number uniqueness globally
if rec.emp_no and rec.emp_no != '/':
duplicate = self.search([
("emp_no", "=", rec.emp_no),
("id", "!=", rec.id)
], limit=1)
if duplicate:
raise ValidationError(
_("You cannot create Employee with the same employee number")
)
if rec.birthday and isinstance(rec.birthday, date) and rec.birthday >= date.today():
raise ValidationError(_("Sorry, The Birthday Must Be Less than Date Today"))
if rec.attachment_ids:
for att in rec.attachment_ids:
if not att.doc_name:
raise ValidationError(_('Attach the attachment to the Document %s') % att.name)

View File

@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_hr_employee_multicompany,hr.employee.multicompany,hr.model_hr_employee,base.group_user,1,1,1,1
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_hr_employee_multicompany hr.employee.multicompany hr.model_hr_employee base.group_user 1 1 1 1