From 2abf2f08d3955e59dc76154f84b85e22b80651a4 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Fri, 24 Jun 2022 12:07:31 +0200 Subject: [PATCH] [FIX] rma: improve check on rule selection during procurement Fixes #274 Thanks to @florian-dacosta for suggesting this approach. --- rma/models/procurement_group.py | 16 ++++++++-------- rma/wizards/rma_make_picking.py | 10 +++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/rma/models/procurement_group.py b/rma/models/procurement_group.py index bd8fe4ba..8fe4b1a5 100644 --- a/rma/models/procurement_group.py +++ b/rma/models/procurement_group.py @@ -17,21 +17,21 @@ class ProcurementGroup(models.Model): @api.model def _get_rule(self, product_id, location_id, values): - """Ensure that the selected rule is from the configured route""" + """Ensure that the selected rule is valid for RMAs""" res = super()._get_rule(product_id, location_id, values) - force_rule_ids = self.env.context.get("rma_force_rule_ids") - if force_rule_ids: - if res and res.id not in force_rule_ids: + rma_route_check = self.env.context.get("rma_route_check") + if rma_route_check: + if res and not res.route_id.rma_selectable: raise ValidationError( _( - "No rule found in this RMA's configured route for product " - "%(product)s and location %(location)s" + "No rule found for this product %(product)s and " + "location %(location)s that is valid for RMA operations." ) % { "product": product_id.default_code or product_id.name, "location": location_id.complete_name, } ) - # Don't enforce rules on any chained moves - force_rule_ids.clear() + # Don't enforce check on any chained moves + rma_route_check.clear() return res diff --git a/rma/wizards/rma_make_picking.py b/rma/wizards/rma_make_picking.py index 360984a2..960e3361 100644 --- a/rma/wizards/rma_make_picking.py +++ b/rma/wizards/rma_make_picking.py @@ -144,10 +144,8 @@ class RmaMakePicking(models.TransientModel): group = self.env["procurement.group"].create(pg_data) if picking_type == "incoming": qty = item.qty_to_receive - force_rule_ids = item.line_id.in_route_id.rule_ids.ids else: qty = item.qty_to_deliver - force_rule_ids = item.line_id.out_route_id.rule_ids.ids values = self._get_procurement_data(item, group, qty, picking_type) values = dict(values, rma_line_id=item.line_id, rma_id=item.line_id.rma_id) # create picking @@ -165,9 +163,11 @@ class RmaMakePicking(models.TransientModel): ) procurements.append(procurement) - self.env["procurement.group"].with_context( - rma_force_rule_ids=force_rule_ids - ).run(procurements) + # Trigger a route check with a mutable in the context that can be + # cleared after the first rule selection + self.env["procurement.group"].with_context(rma_route_check=[True]).run( + procurements + ) except UserError as error: errors.append(error.name) if errors: