From a6f5c1df87d8ec7d61d83ab53f4b87b7efb3d9c8 Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 10 Jan 2025 14:49:59 +0100 Subject: [PATCH] [IMP] stock_inventory_discrepancy: better inheritance During the migration to 13.0 of this module it was decided to introduce a hook. Refactoring to a regular override plays better with inheritance. --- stock_inventory_discrepancy/__manifest__.py | 1 - stock_inventory_discrepancy/hooks.py | 76 ------------------- .../models/stock_quant.py | 36 +++++++++ 3 files changed, 36 insertions(+), 77 deletions(-) delete mode 100644 stock_inventory_discrepancy/hooks.py diff --git a/stock_inventory_discrepancy/__manifest__.py b/stock_inventory_discrepancy/__manifest__.py index f9269d8c44bb..99c9ef11ea64 100644 --- a/stock_inventory_discrepancy/__manifest__.py +++ b/stock_inventory_discrepancy/__manifest__.py @@ -21,7 +21,6 @@ "wizards/confirm_discrepancy_wiz.xml", ], "license": "AGPL-3", - "post_load": "post_load_hook", "installable": True, "application": False, } diff --git a/stock_inventory_discrepancy/hooks.py b/stock_inventory_discrepancy/hooks.py deleted file mode 100644 index 84a1de372699..000000000000 --- a/stock_inventory_discrepancy/hooks.py +++ /dev/null @@ -1,76 +0,0 @@ -# Copyright 2019 ForgeFlow S.L. -# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). - - -from odoo import _, fields -from odoo.exceptions import UserError -from odoo.tools.float_utils import float_compare - -from odoo.addons.stock.models.stock_quant import StockQuant - - -def post_load_hook(): - def _apply_inventory_discrepancy(self): - """Override method to avoid inline group validation""" - move_vals = [] - # START HOOK: - Allow specific group to validate inventory - # - Allow validate on pending status - if ( - not self.user_has_groups("stock.group_stock_manager") - and not self.user_has_groups( - "stock_inventory_discrepancy.group_stock_inventory_validation" - ) - and not self.user_has_groups( - "stock_inventory_discrepancy.group_stock_inventory_validation_always" - ) - ): - raise UserError( - _("Only a stock manager can validate an inventory adjustment.") - ) - # Allow to write last_inventory_date on stock.location - self = self.sudo() - # END HOOK - for quant in self: - # Create and validate a move so that the quant matches its `inventory_quantity`. - if ( - float_compare( - quant.inventory_diff_quantity, - 0, - precision_rounding=quant.product_uom_id.rounding, - ) - > 0 - ): - move_vals.append( - quant._get_inventory_move_values( - quant.inventory_diff_quantity, - quant.product_id.with_company( - quant.company_id - ).property_stock_inventory, - quant.location_id, - ) - ) - else: - move_vals.append( - quant._get_inventory_move_values( - -quant.inventory_diff_quantity, - quant.location_id, - quant.product_id.with_company( - quant.company_id - ).property_stock_inventory, - out=True, - ) - ) - moves = ( - self.env["stock.move"].with_context(inventory_mode=False).create(move_vals) - ) - moves._action_done() - self.location_id.write({"last_inventory_date": fields.Date.today()}) - date_by_location = { - loc: loc._get_next_inventory_date() for loc in self.mapped("location_id") - } - for quant in self: - quant.inventory_date = date_by_location[quant.location_id] - self.write({"inventory_quantity": 0, "user_id": False}) - self.write({"inventory_diff_quantity": 0}) - - StockQuant._patch_method("_apply_inventory", _apply_inventory_discrepancy) diff --git a/stock_inventory_discrepancy/models/stock_quant.py b/stock_inventory_discrepancy/models/stock_quant.py index e656955fb5ca..365e85c415d6 100644 --- a/stock_inventory_discrepancy/models/stock_quant.py +++ b/stock_inventory_discrepancy/models/stock_quant.py @@ -90,3 +90,39 @@ def action_apply_inventory(self): ) return action return super().action_apply_inventory() + + def user_has_groups(self, groups): + # - Allow specific group to validate inventory + # - Allow validate on pending status + if ( + self.env.context.get("from_apply_inventory") + and groups == "stock.group_stock_manager" + and not self.user_has_groups("stock.group_stock_manager") + and not self.user_has_groups( + "stock_inventory_discrepancy.group_stock_inventory_validation" + ) + and not self.user_has_groups( + "stock_inventory_discrepancy.group_stock_inventory_validation_always" + ) + ): + raise UserError( + _("Only a stock manager can validate an inventory adjustment.") + ) + return super().user_has_groups(groups) + + def _apply_inventory(self): + if ( + not self.user_has_groups("stock.group_stock_manager") + and not self.user_has_groups( + "stock_inventory_discrepancy.group_stock_inventory_validation" + ) + and not self.user_has_groups( + "stock_inventory_discrepancy.group_stock_inventory_validation_always" + ) + ): + raise UserError( + _("Only a stock manager can validate an inventory adjustment.") + ) + # Allow to write last_inventory_date on stock.location + self = self.sudo().with_context(from_apply_inventory=True) + return super(StockQuant, self)._apply_inventory()