Skip to content

Commit

Permalink
[9.0][REF]stock_auto_move: fix action_assign() (OCA#373)
Browse files Browse the repository at this point in the history
* The `_apply` method should not be overridden. In case of three step
operation with the last step set to auto move. The override of the
`_apply` method was causing the second move to be automatic as well.

**Use case**: doing a reception operation in two steps where the moves on
the second step are set to be automatic (using the push rule in this
case), and the reception is done partially.
**Expected Result**: When doing a first reception step partially a back
order is created, and we expect the second step to have a back order for
the remaining qty as well.
**Current behavior**: The correct move is processed automatically but no
backorder is created. The reason for that behavior is we processing the
move directly (i.e calling `action_done` of the `stock.move`) without
going through the normal process (i.e processing the second step
picking).
**The solution to the problem**: The processing the of the automatic
stock move should follow the same behavior as done from the user interface.
I've added a new function in `stock.picking` model called
`_transfer_pickings_with_auto_move` to simulate the same behavior this
function is called inside the `action_assign` of the stock move instead
of calling the `action_done` of the move.
  • Loading branch information
zakiuu authored and ThomasBinsfeld committed Mar 23, 2021
1 parent bbbfa8d commit f18e53a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
42 changes: 35 additions & 7 deletions stock_auto_move/models/stock_auto_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,43 @@ class StockMove(models.Model):
help="If this option is selected, the move will be automatically "
"processed as soon as the products are available.")

@api.model
def _get_auto_moves_by_pickings(self, auto_moves):
""" Group moves by picking.
@param auto_moves: stock.move data set
@return dict dict of moves grouped by pickings
{stock.picking(id): stock.move(id1, id2, id3 ...), ...}
"""
auto_moves_by_pickings = dict()
for move in auto_moves:
if move.picking_id in auto_moves_by_pickings:
auto_moves_by_pickings[move.picking_id] |= move
else:
auto_moves_by_pickings.update({move.picking_id: move})
return auto_moves_by_pickings

@api.multi
def action_assign(self, no_prepare=False):
super(StockMove, self).action_assign(no_prepare=no_prepare)
# Transfer all pickings which have an auto move assigned
moves = self.filtered(lambda m: m.state == 'assigned' and m.auto_move)
todo_pickings = moves.mapped('picking_id')
# We create packing operations to keep packing if any
todo_pickings.do_prepare_partial()
moves.action_done()

already_assigned_moves = self.filtered(
lambda m: m.state == 'assigned')

not_assigned_auto_move = self - already_assigned_moves

res = super(StockMove, self).action_assign(
no_prepare=no_prepare)

# Process only moves that have been processed recently
auto_moves = not_assigned_auto_move.filtered(
lambda m: m.state == 'assigned' and m.auto_move)

# group the moves by pickings
auto_moves_by_pickings = self._get_auto_moves_by_pickings(auto_moves)

# process the moves by creating backorders
self.env['stock.picking']._transfer_pickings_with_auto_move(
auto_moves_by_pickings)
return res

@api.multi
def _change_procurement_group(self):
Expand Down
34 changes: 34 additions & 0 deletions stock_auto_move/models/stock_picking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
# Copyright 2017 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models


class StockPicking(models.Model):

_inherit = 'stock.picking'

@api.model
def _transfer_pickings_with_auto_move(self, auto_moves_by_pickings):
"""This function is meant to simulate what a user would normally
transfer a picking from the user interface either partial processing
or full processing.
@params auto_moves_by_pickings: dict of moves grouped by pickings
{stock.picking(id): stock.move(id1, id2, id3 ...), ...}
"""
for picking in auto_moves_by_pickings:
if len(picking.move_lines) != len(auto_moves_by_pickings[picking]):
# Create a back order for remaning moves
backorder_moves = \
picking.move_lines - auto_moves_by_pickings[picking]
picking._create_backorder(backorder_moves=backorder_moves)

# Create immediate transfer wizard so it will fill the qty_done
# on the auto move linked operation
picking.do_prepare_partial()
wizard = self.env['stock.immediate.transfer'].create(
{'pick_id': picking.id})
wizard.process()

return

0 comments on commit f18e53a

Please sign in to comment.