[FIX] stock: make it possible to inherit easier in _merge_moves

This commit is contained in:
Wolfgang Taferner
2017-10-18 17:52:19 +02:00
committed by Simon Lejeune
parent 47b8887ff5
commit ef592ffa3d
+17 -7
View File
@@ -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: