Modified employee creation to assign a valid emp_no if missing or '/'

This commit is contained in:
Mazen Abdo 2025-11-13 14:12:41 +02:00
parent b3a92a1543
commit 8a7a3755cd
2 changed files with 0 additions and 201 deletions

View File

@ -1,195 +0,0 @@
# HR Multi-Company Employee Number
## Overview
This module fixes the employee number generation issue in multi-company Odoo 14 environments. It ensures that all employees across all companies get unique, sequential employee numbers without conflicts.
## Problem Statement
In the original `hr_base` module, when creating employees from `res.users` in a multi-company setup:
- All companies shared a single employee number sequence (`hr.employee`)
- This caused validation errors: "You cannot create Employee with the same employee number"
- New employees were assigned "/" instead of proper sequential numbers
## Solution
This module implements a **global employee number sequence**:
- All companies share a single global sequence: `hr.employee.global`
- Employee numbers are globally unique across all companies
- Sequential numbering continues across companies
- Maintains compatibility with existing `hr_base` module
## Features
1. **Global Sequential Numbering**: All employees get sequential numbers regardless of company
2. **Automatic Sequence Creation**: The sequence is automatically created if it doesn't exist
3. **Smart Number Generation**: Analyzes existing employee numbers to start from the correct next number
4. **Multi-Company Support**: Works seamlessly across all companies
5. **No Duplicate Numbers**: Employee numbers are completely unique across all companies
## Numbering Format
- **All Companies**: EMP-0001, EMP-0002, EMP-0003, EMP-0004, ...
- **Example**:
- Company 1, Employee 1: EMP-0001
- Company 2, Employee 1: EMP-0002
- Company 1, Employee 2: EMP-0003
- Company 2, Employee 2: EMP-0004
This ensures that no two employees in the entire system have the same employee number, and numbering is continuous across all companies.
## Installation
1. **Copy the module** to your Odoo addons directory:
```
/odoo14/custom/STANDARD_MODULES/test/odex25_hr/odex25_hr/hr_multicompany_employee_number/
```
2. **Update the apps list**:
- Go to Apps menu
- Click "Update Apps List"
- Search for "HR Multi-Company Employee Number"
3. **Install the module**:
- Click Install on the module
## Configuration
### Automatic Setup
The module works automatically after installation. No additional configuration is required.
### Manual Sequence Configuration (Optional)
If you want to customize the global sequence:
1. Go to **Settings > Technical > Sequences & Identifiers > Sequences**
2. Search for sequence with code: `hr.employee.global`
3. You can modify:
- Prefix (default: "EMP-", you can change to "E-", "EMPLOYEE-", etc.)
- Padding (default: 4 digits, you can change to 5, 6, etc.)
- Next Number (starting point)
### Example Sequence Configuration
**Global Employee Sequence:**
- Sequence Code: `hr.employee.global`
- Name: `Global Employee Number`
- Prefix: `EMP-`
- Padding: 4 (produces 0001, 0002, 0003, etc.)
- Next Number: Based on existing employees + 1
- Company: None (global across all companies)
## Usage
### Creating Employees from Users
1. **Create a new user**:
```
Users & Companies > Users > Create
```
2. **Set the company**:
- In the user form, set the company field
- The system automatically creates an employee with a unique number for that company
3. **Result**:
- First employee (any company): EMP-0001
- Second employee (any company): EMP-0002
- Third employee (any company): EMP-0003
- And so on...
### Creating Employees Directly
1. **Create a new employee**:
```
Employees > Employees > Create
```
2. **Set the company**:
- Set the company field before saving
- The system generates the next available number for that company
## Technical Details
### Key Files
- `models/hr_employee.py`: Main logic for company-specific employee numbering
- `data/ir_sequence_data.xml`: Sequence configuration template
- `__manifest__.py`: Module definition and dependencies
### Key Methods
#### `_default_emp_code()`
Generates the next employee number for the company:
```python
@api.model
def _default_emp_code(self):
company_id = self.env.context.get('default_company_id') or self.env.company.id
sequence_code = f'hr.employee.company.{company_id}'
seq = self.env['ir.sequence'].next_by_code(sequence_code)
...
```
#### `_create_company_sequence()`
Creates a new sequence for a company if it doesn't exist:
```python
@api.model
def _create_company_sequence(self, company_id):
company = self.env['res.company'].browse(company_id)
sequence_code = f'hr.employee.company.{company_id}'
...
```
#### `_check_unique_emp_no_per_company()`
Validates uniqueness per company (not globally):
```python
@api.constrains("emp_no", "company_id")
def _check_unique_emp_no_per_company(self):
items = self.search([
("emp_no", "=", item.emp_no),
("company_id", "=", item.company_id.id)
])
...
```
## Troubleshooting
### Issue: Still getting "/" as employee number
**Solution**:
1. Check that the module is properly installed
2. Verify that sequences are created: Settings > Technical > Sequences
3. Try creating an employee manually first to trigger sequence creation
### Issue: Validation error about duplicate employee numbers
**Solution**:
1. This should not happen with the module installed
2. Check that `hr_multicompany_employee_number` is loaded AFTER `hr_base`
3. Verify in the module dependencies that `hr_base` is listed
### Issue: Existing employees have conflicts
**Solution**:
Run this SQL to update existing employees (backup first!):
```sql
-- This is just for reference, contact your DB admin
UPDATE hr_employee
SET emp_no = CONCAT(company_id, '-', emp_no)
WHERE emp_no IN (
SELECT emp_no FROM hr_employee
GROUP BY emp_no HAVING COUNT(*) > 1
);
```
## Dependencies
- `hr`: Base HR module
- `hr_base`: Custom HR base module
## Version History
- **14.0.1.0.0**: Initial release
- Company-specific employee number sequences
- Automatic sequence creation
- Per-company uniqueness constraint
## Support
For issues or questions, contact your Odoo administrator.
## License
Proprietary
## Author
Custom Development Team

View File

@ -7,12 +7,6 @@
'description': """ 'description': """
This module fixes the employee number generation issue when creating employees This module fixes the employee number generation issue when creating employees
from res.users in a multi-company environment. from res.users in a multi-company environment.
Features:
- Company-specific employee number sequences
- Automatic sequence creation for each company
- Prevents duplicate employee numbers across companies
- Seamless integration with existing hr_base module
""", """,
'author': 'Custom Development', 'author': 'Custom Development',
'depends': ['hr_base', 'hr'], 'depends': ['hr_base', 'hr'],