Skip to content

Commit

Permalink
[IMP] sale_order_line_cancel: Add hook method
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lmignon committed Dec 19, 2024
1 parent 736014a commit 544e946
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
4 changes: 1 addition & 3 deletions sale_order_line_cancel/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion sale_order_line_cancel/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
23 changes: 19 additions & 4 deletions sale_order_line_cancel/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
)
4 changes: 2 additions & 2 deletions sale_order_line_cancel/wizards/sale_order_line_cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class SaleOrderLineCancel(models.TransientModel):
_description = "Cancel Remaining Wizard"

def _get_sale_order_line(self):
active_id = self._context.get("active_id")
active_model = self._context.get("active_model")
active_id = self.env.context.get("active_id")
active_model = self.env.context.get("active_model")
if not active_id or active_model != "sale.order.line":
raise UserError(_("No sale order line ID found"))

Check warning on line 17 in sale_order_line_cancel/wizards/sale_order_line_cancel.py

View check run for this annotation

Codecov / codecov/patch

sale_order_line_cancel/wizards/sale_order_line_cancel.py#L17

Added line #L17 was not covered by tests
return self.env[active_model].browse(active_id)
Expand Down

0 comments on commit 544e946

Please sign in to comment.