[REV] stock: prevent creating sequences with same code

This reverts commit 9034602a028fb22fbd139cf41a2f5c71d9d8d032. Orig PR
for reference/auto-linking tracking: odoo/odoo#129394

Commit was intended to prevent having picking type sequences created
with the same code, but set up too strict a condition. This caused
issues with the following use cases:

- upgrades: see odoo/odoo#139277
- uninstalling and reinstalling modules that auto-create picking types
  see:
   - POS = odoo/odoo#138189
   - stock = odoo/odoo#134032
   - mrp = odoo/odoo#136755
   - repair = odoo/odoo#139841 saas-16.4 onwards

For uninstall + reinstall cases, we should indeed do something better to
clean up these sequences so they do not appear to be duplicates of the
re-added operation types (i.e. unlinking them is not a good solution,
especially since the picking type still exists afterwards). That will
be part of a separate task though and low priority since we already warn
users that uninstalling and reinstalling modules can result in
unexpected behavior.

Since the original issue was only that there was no way to distinguish
between the sequences, a "_copy" will be added to duplicated picking
type records + we will add a warning if users tries to use an already
used sequence prefix (since the name is based off of this) but we won't
block them from doing it.

Also, we move the test that was added after the commit that is being
reverted to a better location.

Part-of: odoo/odoo#146362
This commit is contained in:
Tiffany Chang (tic)
2023-12-18 23:31:58 +00:00
parent 8725127d45
commit 5c25669ed3
5 changed files with 10 additions and 52 deletions
-7
View File
@@ -7651,13 +7651,6 @@ msgstr ""
msgid "Sequence picking"
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"
-10
View File
@@ -343,16 +343,6 @@ 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])
duplicate_records = self.env['ir.sequence']._read_group(
domain, ['company_id', 'name'], having=[('__count', '>', 1)])
if duplicate_records:
duplicate_names = [name for __, name in duplicate_records]
raise UserError(_("Sequences %s already exist.",
', '.join(duplicate_names)))
class Picking(models.Model):
_name = "stock.picking"
-1
View File
@@ -22,6 +22,5 @@ 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
from . import test_picking_tours
from . import test_replenish
-34
View File
@@ -1,34 +0,0 @@
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.')
def test_empty_picking_draft(self):
""" test an empty still can be reset to draft """
picking = self.env['stock.picking'].create({
'picking_type_id': self.env.ref('stock.picking_type_in').id,
'location_id': self.env.ref('stock.stock_location_stock').id,
'location_dest_id': self.env.ref('stock.stock_location_stock').id,
})
self.assertFalse(picking.move_ids)
self.assertEqual(picking.state, 'draft')
+10
View File
@@ -2507,6 +2507,16 @@ class TestStockFlow(TestStockCommon):
self.PickingObj.flush_model()
self.assertEqual(self.PickingObj.search_count([('name', '=', picking_2.name)]), 1)
def test_empty_picking_draft(self):
""" test an empty still can be reset to draft """
picking = self.PickingObj.create({
'picking_type_id': self.picking_type_in,
'location_id': self.stock_location,
'location_dest_id': self.stock_location,
})
self.assertFalse(picking.move_ids)
self.assertEqual(picking.state, 'draft')
@tagged('-at_install', 'post_install')
class TestStockFlowPostInstall(TestStockCommon):