From ef592ffa3d1bbeab910cc66fca4028c978d9446e Mon Sep 17 00:00:00 2001 From: Wolfgang Taferner Date: Tue, 17 Oct 2017 11:50:22 +0200 Subject: [PATCH] [FIX] stock: make it possible to inherit easier in _merge_moves --- addons/stock/models/stock_move.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py index 47f3a99637a..f37554cf583 100644 --- a/addons/stock/models/stock_move.py +++ b/addons/stock/models/stock_move.py @@ -514,25 +514,35 @@ class StockMove(models.Model): 'origin': origin, } + @api.model + def _prepare_merge_moves_distinct_fields(self): + return [ + 'product_id', 'price_unit', 'product_packaging', + 'product_uom', 'restrict_partner_id', 'scrapped', 'origin_returned_move_id' + ] + + @api.model + def _prepare_merge_move_sort_method(self, move): + move.ensure_one() + return [ + move.product_id.id, move.price_unit, move.product_packaging.id, move.product_uom.id, + move.restrict_partner_id.id, move.scrapped, move.origin_returned_move_id.id + ] + def _merge_moves(self): """ This method will, for each move in `self`, go up in their linked picking and try to find in their existing moves a candidate into which we can merge the move. :return: Recordset of moves passed to this method. If some of the passed moves were merged into another existing one, return this one and not the (now unlinked) original. """ - distinct_fields = ['product_id', 'price_unit', 'product_packaging', - 'product_uom', 'restrict_partner_id', 'scrapped', 'origin_returned_move_id'] - - def _keys_sorted(move): - return move.product_id.id, move.price_unit, move.product_packaging.id, move.product_uom.id,\ - move.restrict_partner_id.id, move.scrapped, move.origin_returned_move_id.id + distinct_fields = self._prepare_merge_moves_distinct_fields() # Move removed after merge moves_to_unlink = self.env['stock.move'] moves_to_merge = [] for picking in self.mapped('picking_id'): # First step find move to merge. - for k, g in groupby(sorted(picking.move_lines, key=_keys_sorted), key=itemgetter(*distinct_fields)): + for k, g in groupby(sorted(picking.move_lines, key=self._prepare_merge_move_sort_method), key=itemgetter(*distinct_fields)): moves = self.env['stock.move'].concat(*g).filtered(lambda m: m.state not in ('done', 'cancel', 'draft')) # If we have multiple records we will merge then in a single one. if len(moves) > 1: