diff --git a/stock_storage_type/models/stock_storage_category_capacity.py b/stock_storage_type/models/stock_storage_category_capacity.py index e007d398bf..c090f60ce8 100644 --- a/stock_storage_type/models/stock_storage_category_capacity.py +++ b/stock_storage_type/models/stock_storage_category_capacity.py @@ -1,6 +1,7 @@ # Copyright 2022 ACSONE SA # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) from odoo import _, api, fields, models +from odoo.osv.expression import AND, OR class StorageCategoryProductCapacity(models.Model): @@ -95,7 +96,34 @@ def _domain_location_storage_type(self, candidate_locations, quants, products): ] # Build the domain using the 'allow_new_product' field if self.allow_new_product == "empty": - location_domain.append(("location_is_empty", "=", True)) + # We should include the destination location of the current + # stock move line to avoid excluding it if already selected + # Indeed, if the current move line point to the last void location, + # calling the putaway apply will recompute the destination location + # to the related stock.move destination as the rules consider + # there is no more room available (which is not true). + exclude_sml_ids = self.env.context.get("exclude_sml_ids") + if exclude_sml_ids: + lines_locations = ( + self.env["stock.move.line"].browse(exclude_sml_ids).location_dest_id + ) + if lines_locations: + location_domain = AND( + [ + location_domain, + OR( + [ + [ + ("location_is_empty", "=", False), + ("id", "in", lines_locations.ids), + ], + [("location_is_empty", "=", True)], + ] + ), + ] + ) + else: + location_domain.append(("location_is_empty", "=", True)) elif self.allow_new_product == "same": location_domain += self._get_product_location_domain(products) elif self.allow_new_product == "same_lot": diff --git a/stock_storage_type/tests/test_storage_type_putaway_strategy.py b/stock_storage_type/tests/test_storage_type_putaway_strategy.py index 3f2956576c..907751e01d 100644 --- a/stock_storage_type/tests/test_storage_type_putaway_strategy.py +++ b/stock_storage_type/tests/test_storage_type_putaway_strategy.py @@ -141,6 +141,13 @@ def test_storage_strategy_only_empty_ordered_locations_pallets(self): self.pallets_bin_1_location | self.pallets_bin_3_location, ) + # Try to re-apply the putaways to check the same destinations are selected + int_picking.move_line_ids._apply_putaway_strategy() + self.assertEqual( + int_picking.move_line_ids.mapped("location_dest_id"), + self.pallets_bin_1_location | self.pallets_bin_3_location, + ) + def test_storage_strategy_max_weight_ordered_locations_pallets(self): # Add a category for max_weight 50 category_50 = self.env["stock.storage.category"].create(