From 3fbfbf02cebf54e617ccd4afcf671fd5f9edfc02 Mon Sep 17 00:00:00 2001 From: JF Aubert Date: Thu, 4 Mar 2021 11:56:36 +0000 Subject: [PATCH] [FIX] uom: allow ratio change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allow unit of measure ratio change when there are no product currently reserved or in stock using this unit of measure. opw-2455914 closes odoo/odoo#67827 X-original-commit: d78238843ea8933b62d340d1b231a12e94bbf957 Signed-off-by: Arnold Moyaux Signed-off-by: Jean-François Aubert --- addons/stock/models/product.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/addons/stock/models/product.py b/addons/stock/models/product.py index 173c2959790..a96cab737d6 100644 --- a/addons/stock/models/product.py +++ b/addons/stock/models/product.py @@ -930,17 +930,26 @@ class UoM(models.Model): lambda u: any(u[f].id != int(values[f]) if f in values else False for f in {'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 measure as some" - " products with this UoM have already been moved or are " - "currently reserved." - )) + error_msg = _( + "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 self.env['stock.move'].sudo().search_count([ + ('product_uom', 'in', changed.ids), + ('state', 'not in', ('cancel', 'done')) + ]): + raise UserError(error_msg) + if self.env['stock.move.line'].sudo().search_count([ + ('product_uom_id', 'in', changed.ids), + ('state', 'not in', ('cancel', 'done')), + ]): + raise UserError(error_msg) + if self.env['stock.quant'].sudo().search_count([ + ('product_id.product_tmpl_id.uom_id', 'in', changed.ids), + ('quantity', '!=', 0), + ]): + raise UserError(error_msg) return super(UoM, self).write(values) def _adjust_uom_quantities(self, qty, quant_uom):