From ff342d9e9314d4fba1da35499cfa1261619c34bd Mon Sep 17 00:00:00 2001 From: yhu-odoo Date: Thu, 30 Mar 2023 11:20:22 +0000 Subject: [PATCH] [FIX] mrp: duplicate lot when auto-generate serial. To reproduce: 1. Manually create lot "0000001" for a lot product 2. Create a MO for this product and click generate-serial button. Validation error raised since we are trying to generate lot/sn "0000001" again. When useing action_generate_serial, ir.sequence always try create a lot/sn in form "00000dd". If user already created the same one, the generation will fail. We already tried to avoid this issue for sn in _get_next_serial() by finding the latest sn and create new one base on it. To fix, we also allow _get_next_serial to be applied to lot. Part of Tast-3187003 closes odoo/odoo#118481 X-original-commit: dc69748ea2698077782d839cf7da48e987f17ffc Signed-off-by: William Henrotin (whe) Signed-off-by: Yuchen Huang (yhu) --- addons/mrp/tests/test_traceability.py | 22 ++++++++++++++++++++++ addons/stock/models/stock_lot.py | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/addons/mrp/tests/test_traceability.py b/addons/mrp/tests/test_traceability.py index 946d4ef98b3..221c31b8aa8 100644 --- a/addons/mrp/tests/test_traceability.py +++ b/addons/mrp/tests/test_traceability.py @@ -613,3 +613,25 @@ class TestTraceability(TestMrpCommon): {'product_id': self.bom_4.product_id.id, 'lot_id': False, 'qty_done': 1}, {'product_id': component.id, 'lot_id': serial_number.id, 'qty_done': 1}, ]) + + def test_generate_serial_button(self): + """Test if lot in form "00000dd" is manually created, the generate serial + button can skip it and create the next one. + """ + mo, _bom, p_final, _p1, _p2 = self.generate_mo(qty_base_1=1, qty_base_2=1, qty_final=1, tracking_final='lot') + + # generate lot lot_0 on MO + mo.action_generate_serial() + lot_0 = mo.lot_producing_id.name + # manually create lot_1 (lot_0 + 1) + lot_1 = self.env['stock.lot'].create({ + 'name': str(int(lot_0) + 1).zfill(7), + 'product_id': p_final.id, + 'company_id': self.env.company.id, + }).name + # generate lot lot_2 on a new MO + mo = mo.copy() + mo.action_confirm() + mo.action_generate_serial() + lot_2 = mo.lot_producing_id.name + self.assertEqual(lot_2, str(int(lot_1) + 1).zfill(7)) diff --git a/addons/stock/models/stock_lot.py b/addons/stock/models/stock_lot.py index 0a7edbba7e5..7edcb7e15cd 100644 --- a/addons/stock/models/stock_lot.py +++ b/addons/stock/models/stock_lot.py @@ -63,7 +63,7 @@ class StockLot(models.Model): @api.model def _get_next_serial(self, company, product): """Return the next serial number to be attributed to the product.""" - if product.tracking == "serial": + if product.tracking != "none": last_serial = self.env['stock.lot'].search( [('company_id', '=', company.id), ('product_id', '=', product.id)], limit=1, order='id DESC')