From 1d33b7a9b8f7df102fe053e82b02bb11485768df Mon Sep 17 00:00:00 2001 From: Walid Date: Tue, 5 Sep 2023 13:16:22 +0200 Subject: [PATCH] [FIX] stock: SN sequence Steps to reproduce: - Create a serial tracked product - Update quantity add random SN - Edit "ir.sequence" for serial numbers (search sequence with dev mode enabled) - Add a prefix for exemple "xx%(doy)sxx" - Manufacture the product and generate new serial Bug: if a serial already exist next number in the sequence will be generated instead of using the sequence Fix: apply same logic than for lots i.e. first generate from sequence then take next number if it already exists opw-3291532 closes odoo/odoo#134879 X-original-commit: df96d9ca10c576981f6af79e330b5b46cdeea377 Signed-off-by: William Henrotin (whe) Signed-off-by: Walid Hanniche (waha) --- addons/mrp/models/mrp_production.py | 19 ++++++++----------- addons/mrp/tests/test_traceability.py | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index b3b936fe7b3..3de5f3d6ca3 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -1306,17 +1306,14 @@ class MrpProduction(models.Model): def _prepare_stock_lot_values(self): self.ensure_one() - if self.product_id.tracking == 'lot': - name = self.env['ir.sequence'].next_by_code('stock.lot.serial') - exist_lot = self.env['stock.lot'].search([ - ('product_id', '=', self.product_id.id), - ('company_id', '=', self.company_id.id), - ('name', '=', name), - ], limit=1) - if exist_lot: - name = self.env['stock.lot']._get_next_serial(self.company_id, self.product_id) - else: - name = self.env['stock.lot']._get_next_serial(self.company_id, self.product_id) or self.env['ir.sequence'].next_by_code('stock.lot.serial') + name = self.env['ir.sequence'].next_by_code('stock.lot.serial') + exist_lot = self.env['stock.lot'].search([ + ('product_id', '=', self.product_id.id), + ('company_id', '=', self.company_id.id), + ('name', '=', name), + ], limit=1) + if exist_lot: + name = self.env['stock.lot']._get_next_serial(self.company_id, self.product_id) return { 'product_id': self.product_id.id, 'company_id': self.company_id.id, diff --git a/addons/mrp/tests/test_traceability.py b/addons/mrp/tests/test_traceability.py index 9bfec2a001e..09c4bbb29d5 100644 --- a/addons/mrp/tests/test_traceability.py +++ b/addons/mrp/tests/test_traceability.py @@ -3,6 +3,8 @@ from odoo.tests import Form from odoo.addons.mrp.tests.common import TestMrpCommon + +from datetime import datetime import logging _logger = logging.getLogger(__name__) @@ -631,3 +633,21 @@ class TestTraceability(TestMrpCommon): mo.action_generate_serial() lot_2 = mo.lot_producing_id.name self.assertEqual(lot_2, str(int(lot_1) + 1).zfill(7)) + + def test_generate_serial_button_sequence(self): + """Test if serial in form "00000dd" is manually created, the generate serial + correctly create new serial from sequence. + """ + seq = self.env['ir.sequence'].search([('code', '=', 'stock.lot.serial')]) + seq.prefix = 'xx%(doy)sxx' + mo, _bom, p_final, _p1, _p2 = self.generate_mo(qty_base_1=1, qty_base_2=1, qty_final=1, tracking_final='serial') + + # manually create lot_1 + self.env['stock.lot'].create({ + 'name': "test_000", + 'product_id': p_final.id, + 'company_id': self.env.company.id, + }) + # generate serial lot_2 from the MO + mo.action_generate_serial() + self.assertIn(datetime.now().strftime('%j'), mo.lot_producing_id.name)