diff --git a/addons/purchase_stock/models/stock.py b/addons/purchase_stock/models/stock.py
index 81536d55169..385899aa194 100644
--- a/addons/purchase_stock/models/stock.py
+++ b/addons/purchase_stock/models/stock.py
@@ -82,8 +82,8 @@ class StockMove(models.Model):
super(StockMove, self)._clean_merged()
self.write({'created_purchase_line_id': False})
- def _action_done(self):
- res = super(StockMove, self)._action_done()
+ def _action_done(self, cancel_backorder=False):
+ res = super(StockMove, self)._action_done(cancel_backorder=cancel_backorder)
self.mapped('purchase_line_id').sudo()._update_received_qty()
return res
diff --git a/addons/stock/models/stock_inventory.py b/addons/stock/models/stock_inventory.py
index 1e66ccffe20..a763adee13c 100644
--- a/addons/stock/models/stock_inventory.py
+++ b/addons/stock/models/stock_inventory.py
@@ -180,7 +180,7 @@ class Inventory(models.Model):
else:
self._action_done()
- def _action_done(self):
+ def _action_done(self, cancel_backorder=False):
negative = next((line for line in self.mapped('line_ids') if line.product_qty < 0 and line.product_qty != line.theoretical_qty), False)
if negative:
raise UserError(_('You cannot set a negative product quantity in an inventory line:\n\t%s - qty: %s') % (negative.product_id.name, negative.product_qty))
diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py
index 29433c13091..9fc83d6b7d0 100644
--- a/addons/stock/models/stock_move.py
+++ b/addons/stock/models/stock_move.py
@@ -1084,7 +1084,7 @@ class StockMove(models.Model):
def _unreserve_initial_demand(self, new_move):
pass
- def _action_done(self):
+ def _action_done(self, cancel_backorder=False):
self.filtered(lambda move: move.state == 'draft')._action_confirm() # MRP allows scrapping draft moves
moves = self.exists().filtered(lambda x: x.state not in ('done', 'cancel'))
moves_todo = self.env['stock.move']
@@ -1124,6 +1124,8 @@ class StockMove(models.Model):
except UserError:
pass
move._unreserve_initial_demand(new_move)
+ if cancel_backorder:
+ self.env['stock.move'].browse(new_move)._action_cancel()
moves_todo.mapped('move_line_ids')._action_done()
# Check the consistency of the result packages; there should be an unique location across
# the contained quants.
@@ -1140,7 +1142,7 @@ class StockMove(models.Model):
if all(move_todo.scrapped for move_todo in moves_todo):
return moves_todo
- if picking:
+ if picking and not cancel_backorder:
picking._create_backorder()
return moves_todo
diff --git a/addons/stock/models/stock_move_line.py b/addons/stock/models/stock_move_line.py
index ccd2aea5a85..711a921dee9 100644
--- a/addons/stock/models/stock_move_line.py
+++ b/addons/stock/models/stock_move_line.py
@@ -351,7 +351,7 @@ class StockMoveLine(models.Model):
moves._recompute_state()
return res
- def _action_done(self):
+ def _action_done(self, cancel_backorder=False):
""" This method is called during a move's `action_done`. It'll actually move a quant from
the source location to the destination location, and unreserve if needed in the source
location.
diff --git a/addons/stock/models/stock_picking.py b/addons/stock/models/stock_picking.py
index 85d15f0d1c6..93187e523bc 100644
--- a/addons/stock/models/stock_picking.py
+++ b/addons/stock/models/stock_picking.py
@@ -622,7 +622,7 @@ class Picking(models.Model):
new_move._action_confirm()
todo_moves |= new_move
#'qty_done': ops.qty_done})
- todo_moves._action_done()
+ todo_moves._action_done(cancel_backorder=self.env.context.get('cancel_backorder'))
self.write({'date_done': fields.Datetime.now()})
return True
diff --git a/addons/stock/wizard/stock_backorder_confirmation.py b/addons/stock/wizard/stock_backorder_confirmation.py
index d0c30a2ffc0..8b0b70ccea3 100644
--- a/addons/stock/wizard/stock_backorder_confirmation.py
+++ b/addons/stock/wizard/stock_backorder_confirmation.py
@@ -20,12 +20,7 @@ class StockBackorderConfirmation(models.TransientModel):
if float_compare(move.product_uom_qty, move.quantity_done, move.product_uom.rounding) > 0:
moves_to_log[move] = (move.quantity_done, move.product_uom_qty)
pick_id._log_less_quantities_than_expected(moves_to_log)
- self.pick_ids.action_done()
- if cancel_backorder:
- for pick_id in self.pick_ids:
- backorder_pick = self.env['stock.picking'].search([('backorder_id', '=', pick_id.id)])
- backorder_pick.action_cancel()
- pick_id.message_post(body=_("Back order %s cancelled.") % (backorder_pick.name))
+ self.pick_ids.with_context({'cancel_backorder': cancel_backorder}).action_done()
def process(self):
self._process()
diff --git a/addons/stock_account/models/stock.py b/addons/stock_account/models/stock.py
index f13f8316977..e8a645aeca1 100644
--- a/addons/stock_account/models/stock.py
+++ b/addons/stock_account/models/stock.py
@@ -348,9 +348,9 @@ class StockMove(models.Model):
'price_unit': price_unit if self._is_dropshipped() else -price_unit,
})
- def _action_done(self):
+ def _action_done(self, cancel_backorder=False):
self.product_price_update_before_done()
- res = super(StockMove, self)._action_done()
+ res = super(StockMove, self)._action_done(cancel_backorder=cancel_backorder)
for move in res:
# Apply restrictions on the stock move to be able to make
# consistent accounting entries.