diff --git a/addons/mrp/i18n/mrp.pot b/addons/mrp/i18n/mrp.pot index f52c2404070..e178cb1b6c9 100644 --- a/addons/mrp/i18n/mrp.pot +++ b/addons/mrp/i18n/mrp.pot @@ -3581,6 +3581,13 @@ msgstr "" msgid "Plastic Laminate" msgstr "" +#. module: mrp +#. odoo-python +#: code:addons/mrp/models/mrp_production.py:0 +#, python-format +msgid "Please set the first Serial Number or a default sequence" +msgstr "" + #. module: mrp #: model:product.template,name:mrp.product_product_wood_ply_product_template msgid "Ply Layer" diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index a4096756d8d..837d8682d66 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -1351,13 +1351,15 @@ class MrpProduction(models.Model): def _prepare_stock_lot_values(self): self.ensure_one() name = self.env['ir.sequence'].next_by_code('stock.lot.serial') - exist_lot = self.env['stock.lot'].search([ + exist_lot = not name or 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) + if not name: + raise UserError(_("Please set the first Serial Number or a default sequence")) 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 09c4bbb29d5..8c4526df261 100644 --- a/addons/mrp/tests/test_traceability.py +++ b/addons/mrp/tests/test_traceability.py @@ -3,6 +3,7 @@ from odoo.tests import Form from odoo.addons.mrp.tests.common import TestMrpCommon +from odoo.exceptions import UserError from datetime import datetime import logging @@ -639,15 +640,26 @@ class TestTraceability(TestMrpCommon): 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') + seq.active = False + # No initial serial and No default sequence + with self.assertRaises(UserError): + mo.action_generate_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 + + # generate serial lot_2 from the MO (next_serial) + mo.action_generate_serial() + self.assertEqual(mo.lot_producing_id.name, "test_001") + + seq.active = True + seq.prefix = 'xx%(doy)sxx' + # generate serial lot_3 from the MO (next from sequence) mo.action_generate_serial() self.assertIn(datetime.now().strftime('%j'), mo.lot_producing_id.name)