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.')