From 3e7e6d849713330979bca7652cde902bde0e6de9 Mon Sep 17 00:00:00 2001 From: Adrien Widart Date: Mon, 16 Aug 2021 08:38:33 +0000 Subject: [PATCH] [FIX] mrp: set backorder name with custom sequence name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the prefix of the production sequence contains some dashes, it can lead to undesirable behaviors To reproduce the error: (Enable debug mode) 1. Settings > Technical > Sequences & Identifiers > Sequences, edit "San Francisco Sequence production": - Prefix: "WH-MO-" 2. Create a MO - Quantity: 2 3. Confirm, Edit: - Quantity: 1/2 4. (Keep MO's name in mind and) Validate + Create backorder Error: Backorder's name is "WH-MO-002" instead of "WH-MO-0000X-002". Going back to the initial MO, its name became "WH-MO-001" (was "WH-MO-0000X") OPW-2618971 closes odoo/odoo#75270 X-original-commit: 076fe045f219dcb018b3b18215bfc3a0cfb3aa46 Signed-off-by: Rémy Voet Signed-off-by: Adrien Widart --- addons/mrp/models/mrp_production.py | 5 ++--- addons/mrp/tests/test_backorder.py | 32 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/addons/mrp/models/mrp_production.py b/addons/mrp/models/mrp_production.py index 160fc8ae7f0..a70db0196c5 100644 --- a/addons/mrp/models/mrp_production.py +++ b/addons/mrp/models/mrp_production.py @@ -1395,7 +1395,7 @@ class MrpProduction(models.Model): return name seq_back = "-" + "0" * (SIZE_BACK_ORDER_NUMERING - 1 - int(math.log10(sequence))) + str(sequence) regex = re.compile(r"-\d+$") - if regex.search(name): + if regex.search(name) and sequence > 1: return regex.sub(seq_back, name) return name + seq_back @@ -1418,6 +1418,7 @@ class MrpProduction(models.Model): for production in self: if production.backorder_sequence == 0: # Activate backorder naming production.backorder_sequence = 1 + production.name = self._get_name_backorder(production.name, production.backorder_sequence) backorder_mo = production.copy(default=production._get_backorder_mo_vals()) if close_mo: production.move_raw_ids.filtered(lambda m: m.state not in ('done', 'cancel')).write({ @@ -1449,8 +1450,6 @@ class MrpProduction(models.Model): else: wo.qty_producing = wo.qty_remaining - production.name = self._get_name_backorder(production.name, production.backorder_sequence) - # We need to adapt `duration_expected` on both the original workorders and their # backordered workorders. To do that, we use the original `duration_expected` and the # ratio of the quantity really produced and the quantity to produce. diff --git a/addons/mrp/tests/test_backorder.py b/addons/mrp/tests/test_backorder.py index 1d1d5f1040f..aed6fa0af14 100644 --- a/addons/mrp/tests/test_backorder.py +++ b/addons/mrp/tests/test_backorder.py @@ -280,6 +280,38 @@ class TestMrpProductionBackorder(TestMrpCommon): self.assertEqual(self.env['stock.quant']._get_available_quantity(p_final, self.stock_location), nb_product_todo, f'You should have the {nb_product_todo} final product in stock') self.assertEqual(len(production.procurement_group_id.mrp_production_ids), nb_product_todo) + def test_backorder_name(self): + def produce_one(mo): + mo_form = Form(mo) + mo_form.qty_producing = 1 + mo = mo_form.save() + action = mo.button_mark_done() + backorder = Form(self.env['mrp.production.backorder'].with_context(**action['context'])) + backorder.save().action_backorder() + return mo.procurement_group_id.mrp_production_ids[-1] + + default_picking_type_id = self.env['mrp.production']._get_default_picking_type() + default_picking_type = self.env['stock.picking.type'].browse(default_picking_type_id) + mo_sequence = default_picking_type.sequence_id + + mo_sequence.prefix = "WH-MO-" + initial_mo_name = mo_sequence.prefix + str(mo_sequence.number_next_actual).zfill(mo_sequence.padding) + + production = self.generate_mo(qty_final=5)[0] + self.assertEqual(production.name, initial_mo_name) + + backorder = produce_one(production) + self.assertEqual(production.name, initial_mo_name + "-001") + self.assertEqual(backorder.name, initial_mo_name + "-002") + + backorder.backorder_sequence = 998 + + for seq in [998, 999, 1000]: + new_backorder = produce_one(backorder) + self.assertEqual(backorder.name, initial_mo_name + "-" + str(seq)) + self.assertEqual(new_backorder.name, initial_mo_name + "-" + str(seq + 1)) + backorder = new_backorder + class TestMrpWorkorderBackorder(TransactionCase): @classmethod