diff --git a/addons/stock/models/stock_quant.py b/addons/stock/models/stock_quant.py index 67a51966e2f..f1c696426f8 100644 --- a/addons/stock/models/stock_quant.py +++ b/addons/stock/models/stock_quant.py @@ -335,6 +335,15 @@ class StockQuant(models.Model): self = self.sudo() return super(StockQuant, self).write(vals) + @api.ondelete(at_uninstall=False) + def _unlink_except_wrong_permission(self): + if not self.env.is_superuser(): + if not self.user_has_groups('stock.group_stock_manager'): + raise UserError(_("Quants are auto-deleted when appropriate. If you must manually delete them, please ask a stock manager to do it.")) + self = self.with_context(inventory_mode=True) + self.inventory_quantity = 0 + self._apply_inventory() + def action_view_stock_moves(self): self.ensure_one() action = self.env["ir.actions.actions"]._for_xml_id("stock.stock_move_line_action") diff --git a/addons/stock/tests/test_quant.py b/addons/stock/tests/test_quant.py index c70f746a1c6..71cebe479b9 100644 --- a/addons/stock/tests/test_quant.py +++ b/addons/stock/tests/test_quant.py @@ -509,7 +509,7 @@ class StockQuant(TransactionCase): }) with self.assertRaises(AccessError): quant.with_user(self.demo_user).write({'quantity': 2.0}) - with self.assertRaises(AccessError): + with self.assertRaises(UserError): quant.with_user(self.demo_user).unlink() self.env = self.env(user=self.stock_user)