diff --git a/addons/stock/models/product.py b/addons/stock/models/product.py index 632983fab30..5e5c6f1d7fb 100644 --- a/addons/stock/models/product.py +++ b/addons/stock/models/product.py @@ -591,15 +591,19 @@ class ProductUoM(models.Model): def write(self, values): # Users can not update the factor if open stock moves are based on it if 'factor' in values or 'factor_inv' in values or 'category_id' in values: - stock_move_lines = self.env['stock.move.line'].search_count([ - ('product_uom_id.category_id', '=', self.category_id.id), - ('state', '!=', 'cancel'), - ]) + changed = self.filtered( + lambda u: any(u[f] != values[f] if f in values else False + for f in {'factor', 'factor_inv', 'category_id'})) + if changed: + stock_move_lines = self.env['stock.move.line'].search_count([ + ('product_uom_id.category_id', 'in', changed.mapped('category_id.id')), + ('state', '!=', 'cancel'), + ]) - if stock_move_lines: - raise UserError(_( - "You cannot change the ratio of this unit of mesure as some" - " products with this UoM have already been moved or are " - "currently reserved." - )) + if stock_move_lines: + raise UserError(_( + "You cannot change the ratio of this unit of mesure as some" + " products with this UoM have already been moved or are " + "currently reserved." + )) return super(ProductUoM, self).write(values) diff --git a/addons/stock/models/stock_location.py b/addons/stock/models/stock_location.py index 5a06cc2425e..bce2de0ecd2 100644 --- a/addons/stock/models/stock_location.py +++ b/addons/stock/models/stock_location.py @@ -84,10 +84,10 @@ class Location(models.Model): if self.mapped('quant_ids'): raise UserError(_("This location's usage cannot be changed to view as it contains products.")) if 'usage' in values or 'scrap_location' in values: + modified_locations = self.filtered( - lambda l: l.usage != values.get('usage') or - l.scrap_location != values.get('scrap_location') - ) + lambda l: any(l[f] != values[f] if f in values else False + for f in {'usage', 'scrap_location'})) reserved_quantities = self.env['stock.move.line'].search_count([ ('location_id', 'in', modified_locations.ids), ('product_qty', '>', 0),