Skip to content

Commit

Permalink
[IMP] hr_employee_id: Add support forbatch creation
Browse files Browse the repository at this point in the history
- Updated the create method in models/hr_employee.py to not break batch mode create
- Add test : test_generate_identification_id_exception
  • Loading branch information
maisim committed Aug 16, 2024
1 parent 778ceef commit b17964b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
12 changes: 8 additions & 4 deletions hr_employee_id/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ def _generate_identification_id(self):
)

@api.model
def create(self, vals):
if not vals.get("identification_id"):
vals["identification_id"] = self._generate_identification_id()
return super(HrEmployee, self).create(vals)
def create(self, vals_list):
records = super(HrEmployee, self).create(vals_list)

for record in records:
if not record.identification_id:
record.identification_id = record._generate_identification_id()

return records
23 changes: 23 additions & 0 deletions hr_employee_id/tests/test_employee_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# Copyright 2018 Brainbean Apps (https://brainbeanapps.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from unittest.mock import patch

from odoo.exceptions import UserError
from odoo.tests import common


Expand Down Expand Up @@ -145,3 +148,23 @@ def test_multi_company_res_config(self):
self.assertEqual(res2["employee_id_random_digits"], DIGITS2)
self.assertEqual(res2["employee_id_gen_method"], METHOD2)
self.assertEqual(res2["employee_id_sequence"], sequence2.id)

def test_generate_identification_id_exception(self):
sequence = self.sequence
company = self.company
company.employee_id_gen_method = "sequence"
company.employee_id_sequence = sequence.id

# Create an user with 00001 identification_id
self.employee_model.create(
{"name": "First Employee", "identification_id": "00001"}
)

# Override the company employee_id_sequence sequence next_by_id
# method to always return 00001
with patch(
"odoo.addons.base.models.ir_sequence.IrSequence.next_by_id",
return_value="00001",
):
with self.assertRaises(UserError):
self.employee_model.create({"name": "Second Employee"})

0 comments on commit b17964b

Please sign in to comment.