diff --git a/stock_auto_move/models/stock_auto_move.py b/stock_auto_move/models/stock_auto_move.py index 729ff2b15951..3ec9aebc27ed 100644 --- a/stock_auto_move/models/stock_auto_move.py +++ b/stock_auto_move/models/stock_auto_move.py @@ -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): diff --git a/stock_auto_move/models/stock_picking.py b/stock_auto_move/models/stock_picking.py new file mode 100644 index 000000000000..d25bb035280b --- /dev/null +++ b/stock_auto_move/models/stock_picking.py @@ -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