From 0630d76488e37198cb8ac75e884f715dd4744705 Mon Sep 17 00:00:00 2001 From: "Mahdi Cheikh Rouhou (macr)" Date: Tue, 4 Jul 2023 08:37:16 +0000 Subject: [PATCH] [FIX] stock : prevent creating sequences with same code Issue: ====== You can create the same sequence with the same code Steps to reproduce the error: ============================= - Install inventory and activate storage locations - Go to inventory/configuration/Operations Types - Create 2 operation typs with the following values: name :any random name , type of Operations : internal transfer, sequence prefix : test , locations as WH/Stock - Go to sequences and search for test - You will have 2 duplicate sequences with the same values Origin of the problem : ======================= - Creating an operation type always creates atuomatically a sequences if the sequence_code is provided but the sequence_id isn't. Solution: ========= Display an error when the name already exist. opw-3238331 closes odoo/odoo#132982 X-original-commit: e1f9480c7aa2a982d828ffd0fbc032e4066089d4 Signed-off-by: William Henrotin (whe) Signed-off-by: Mahdi Cheikh Rouhou (macr) --- addons/stock/i18n/stock.pot | 7 +++++++ addons/stock/models/stock_picking.py | 12 ++++++++++++ addons/stock/tests/__init__.py | 1 + addons/stock/tests/test_picking.py | 24 ++++++++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 addons/stock/tests/test_picking.py diff --git a/addons/stock/i18n/stock.pot b/addons/stock/i18n/stock.pot index 2e243127aef..efed91a141d 100644 --- a/addons/stock/i18n/stock.pot +++ b/addons/stock/i18n/stock.pot @@ -7134,6 +7134,13 @@ msgstr "" msgid "Sequence return" msgstr "" +#. module: stock +#. odoo-python +#: code:addons/stock/models/stock_picking.py:0 +#, python-format +msgid "Sequences %s already exist." +msgstr "" + #. module: stock #: model:ir.model.fields,field_description:stock.field_stock_move__lot_ids msgid "Serial Numbers" diff --git a/addons/stock/models/stock_picking.py b/addons/stock/models/stock_picking.py index e9c20d541d8..e58d0dc7b38 100644 --- a/addons/stock/models/stock_picking.py +++ b/addons/stock/models/stock_picking.py @@ -273,6 +273,18 @@ class PickingType(models.Model): for record in self: record.show_picking_type = record.code in ['incoming', 'outgoing', 'internal'] + @api.constrains('sequence_id') + def _check_sequence_code(self): + domain = expression.OR([[('company_id', '=', record.company_id.id), ('name', '=', record.sequence_id.name)] + for record in self]) + record_counts = self.env['ir.sequence'].read_group( + domain, ['company_id', 'name'], ['company_id', 'name'], lazy=False) + duplicate_records = list(filter( + lambda r: r['__count'] > 1, record_counts)) + if duplicate_records: + duplicate_names = list(map(lambda r: r['name'], duplicate_records)) + raise UserError(_("Sequences %s already exist.", + ', '.join(duplicate_names))) class Picking(models.Model): _name = "stock.picking" diff --git a/addons/stock/tests/__init__.py b/addons/stock/tests/__init__.py index d1d65448aad..582aa0ea12d 100644 --- a/addons/stock/tests/__init__.py +++ b/addons/stock/tests/__init__.py @@ -23,3 +23,4 @@ from . import test_report_stock_quantity from . import test_report_tours from . import test_stock_return_picking from . import test_stock_lot +from . import test_picking diff --git a/addons/stock/tests/test_picking.py b/addons/stock/tests/test_picking.py new file mode 100644 index 00000000000..e40d26d1231 --- /dev/null +++ b/addons/stock/tests/test_picking.py @@ -0,0 +1,24 @@ +from odoo.tests import common +from odoo.exceptions import UserError + +class TestPicking(common.TransactionCase): + + def test_00_no_duplicate_sequence_code(self): + stock_location = self.env.ref('stock.stock_location_stock') + operation_type_1 = self.env['stock.picking.type'].create({ + 'name':'test 1', + 'code': 'internal', + 'default_location_src_id':stock_location.id, + 'default_location_dest_id':stock_location.id, + 'sequence_code':'testpicking' + }) + with self.assertRaises(UserError) as er: + self.env['stock.picking.type'].create({ + 'name':'test 2', + 'code': 'internal', + 'default_location_src_id':stock_location.id, + 'default_location_dest_id':stock_location.id, + 'sequence_code':'testpicking' + }) + self.assertEqual( + er.exception.args[0], f'Sequences {operation_type_1.sequence_id.name} already exist.')