From 02bffa5f403031c51af2983dbf5239bbdc5a559b Mon Sep 17 00:00:00 2001 From: yhu-odoo Date: Mon, 1 Feb 2021 23:52:04 +0000 Subject: [PATCH] [IMP] stock: smart putaway for packaging Apply smart putaway rules on move with packaging. Task-2357259 PR #68654 --- addons/stock/models/stock_location.py | 9 +++++++-- addons/stock/models/stock_move.py | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/addons/stock/models/stock_location.py b/addons/stock/models/stock_location.py index 5c2fbc41ad0..144662f61f0 100644 --- a/addons/stock/models/stock_location.py +++ b/addons/stock/models/stock_location.py @@ -197,13 +197,18 @@ class Location(models.Model): domain = ['|', ('barcode', operator, name), ('complete_name', operator, name)] return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid) - def _get_putaway_strategy(self, product, quantity=0, package=None): + def _get_putaway_strategy(self, product, quantity=0, package=None, packaging=None): """Returns the location where the product has to be put, if any compliant putaway strategy is found. Otherwise returns self. The quantity should be in the default UOM of the product, it is used when no package is specified. """ - package_type = package and package.package_type_id or self.env['stock.package.type'] + # find package type on package or packaging + package_type = self.env['stock.package.type'] + if package: + package_type = package.package_type_id + elif packaging: + package_type = packaging.package_type_id putaway_rules = self.env['stock.putaway.rule'] putaway_rules |= self.putaway_rule_ids.filtered(lambda x: x.product_id == product and (package_type in x.package_type_ids or package_type == x.package_type_ids)) diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py index bd5028de1f7..44c0fceae8d 100644 --- a/addons/stock/models/stock_move.py +++ b/addons/stock/models/stock_move.py @@ -1056,7 +1056,7 @@ class StockMove(models.Model): if origin_move_line: location_dest = origin_move_line.location_dest_id else: - location_dest = self.location_dest_id._get_putaway_strategy(self.product_id, quantity=1) + location_dest = self.location_dest_id._get_putaway_strategy(self.product_id, quantity=1, packaging=self.product_packaging_id) move_line_vals = { 'picking_id': self.picking_id.id, 'location_dest_id': location_dest.id, @@ -1212,7 +1212,7 @@ class StockMove(models.Model): def _prepare_move_line_vals(self, quantity=None, reserved_quant=None): self.ensure_one() # apply putaway - location_dest_id = self.location_dest_id._get_putaway_strategy(self.product_id, quantity=quantity or 0).id + location_dest_id = self.location_dest_id._get_putaway_strategy(self.product_id, quantity=quantity or 0, packaging=self.product_packaging_id).id vals = { 'move_id': self.id, 'product_id': self.product_id.id,