[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) <whe@odoo.com>
Signed-off-by: Mahdi Cheikh Rouhou (macr) <macr@odoo.com>
This commit is contained in:
Mahdi Cheikh Rouhou (macr)
2023-08-24 20:04:25 +02:00
parent 5e5178eb66
commit 0630d76488
4 changed files with 44 additions and 0 deletions
+7
View File
@@ -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"
+12
View File
@@ -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"
+1
View File
@@ -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
+24
View File
@@ -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.')