From 6dba6b5c65c44672eedff4879aed98a153526f5e Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Thu, 19 Dec 2024 15:15:12 +0100 Subject: [PATCH] [IMP] sale_order_line_cancel: Add hook method The new hook method allows you to filter out move that should no be taken into account to trigger the update of the canceled qty on the sale order line --- sale_order_line_cancel/models/sale_order.py | 4 +--- .../models/sale_order_line.py | 3 ++- sale_order_line_cancel/models/stock_move.py | 23 +++++++++++++++---- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/sale_order_line_cancel/models/sale_order.py b/sale_order_line_cancel/models/sale_order.py index 870d5136b38a..560ab58e1b74 100644 --- a/sale_order_line_cancel/models/sale_order.py +++ b/sale_order_line_cancel/models/sale_order.py @@ -16,7 +16,5 @@ def action_draft(self): def _action_cancel(self): res = super()._action_cancel() - orders = self.filtered(lambda s: s.state == "cancel") - for line in orders.order_line: - line.product_qty_canceled = line.product_uom_qty - line.qty_delivered + self.order_line._update_qty_canceled() return res diff --git a/sale_order_line_cancel/models/sale_order_line.py b/sale_order_line_cancel/models/sale_order_line.py index 119d6ab8cd50..8d1a978c20f9 100644 --- a/sale_order_line_cancel/models/sale_order_line.py +++ b/sale_order_line_cancel/models/sale_order_line.py @@ -49,7 +49,8 @@ def _compute_product_qty_remains_to_deliver(self): line.product_qty_remains_to_deliver = qty_remaining def _get_moves_to_cancel(self): - return self.move_ids.filtered(lambda m: m.state not in ("done", "cancel")) + lines = self.filtered(lambda l: l.qty_delivered_method == "stock_move") + return lines.move_ids.filtered(lambda m: m.state not in ("done", "cancel")) def _check_moves_to_cancel(self, moves): """Override this method to add checks before cancel""" diff --git a/sale_order_line_cancel/models/stock_move.py b/sale_order_line_cancel/models/stock_move.py index 461150927f38..ced76a9afa58 100644 --- a/sale_order_line_cancel/models/stock_move.py +++ b/sale_order_line_cancel/models/stock_move.py @@ -10,11 +10,8 @@ class StockMove(models.Model): _inherit = "stock.move" def _action_cancel(self): - sale_moves = self.filtered( - lambda m: m.sale_line_id and m.state not in ("done", "cancel") - ) res = super()._action_cancel() - sale_lines = sale_moves.filtered(lambda m: m.state == "cancel").sale_line_id + sale_lines = self._get_sale_lines_to_update_qty_canceled() sale_lines._update_qty_canceled() return res @@ -25,3 +22,21 @@ def _action_done(self, cancel_backorder=False): # _action_cancel will not be triggered. Call it now self.sale_line_id._update_qty_canceled() return moves_todo + + def _get_sale_lines_to_update_qty_canceled(self): + sale_lines = self.env["sale.order.line"] + for move in self: + if ( + move.sale_line_id + and move._is_move_to_take_into_account_for_qty_canceled() + ): + sale_lines |= move.sale_line_id + return sale_lines + + def _is_move_to_take_into_account_for_qty_canceled(self): + self.ensure_one() + return ( + self.state == "cancel" + and self.sale_line_id + and self.picking_type_id.code == "outgoing" + )