From e4050a6e317247fc2dd6429d32acb40e70fc8021 Mon Sep 17 00:00:00 2001 From: luis Date: Wed, 1 Oct 2025 13:26:25 +0200 Subject: [PATCH] add stock_inventory_category_concurrency: allow several inventory adjustments for diffent categories --- .../__init__.py | 1 + .../__manifest__.py | 12 ++++ .../models/__init__.py | 1 + .../models/stock_inventory.py | 70 +++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 stock_inventory_category_concurrency/__init__.py create mode 100644 stock_inventory_category_concurrency/__manifest__.py create mode 100644 stock_inventory_category_concurrency/models/__init__.py create mode 100644 stock_inventory_category_concurrency/models/stock_inventory.py diff --git a/stock_inventory_category_concurrency/__init__.py b/stock_inventory_category_concurrency/__init__.py new file mode 100644 index 0000000..0650744 --- /dev/null +++ b/stock_inventory_category_concurrency/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/stock_inventory_category_concurrency/__manifest__.py b/stock_inventory_category_concurrency/__manifest__.py new file mode 100644 index 0000000..9f77b9f --- /dev/null +++ b/stock_inventory_category_concurrency/__manifest__.py @@ -0,0 +1,12 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +{ + "name": "Stock Inventory Category Concurrency", + "version": "16.0.1.0.0", + "summary": "Permite abrir simultáneamente ajustes de inventario en categorías distintas", + "category": "Inventory", + "license": "AGPL-3", + "author": "Your Company", + "depends": ["stock_inventory"], + "data": [], + "installable": True, +} diff --git a/stock_inventory_category_concurrency/models/__init__.py b/stock_inventory_category_concurrency/models/__init__.py new file mode 100644 index 0000000..3553681 --- /dev/null +++ b/stock_inventory_category_concurrency/models/__init__.py @@ -0,0 +1 @@ +from . import stock_inventory diff --git a/stock_inventory_category_concurrency/models/stock_inventory.py b/stock_inventory_category_concurrency/models/stock_inventory.py new file mode 100644 index 0000000..93f8f33 --- /dev/null +++ b/stock_inventory_category_concurrency/models/stock_inventory.py @@ -0,0 +1,70 @@ +from odoo import _, models +from odoo.exceptions import ValidationError + + +class InventoryAdjustmentsGroup(models.Model): + _inherit = "stock.inventory" + + def action_state_to_in_progress(self): + """ + Relaja el bloqueo por categoría: si el ajuste actual es por categoría, + solo bloquea otros ajustes en progreso que afecten a la MISMA categoría exacta, + no a categorías hermanas distintas ni a todas sus hijas. + """ + self.ensure_one() + # Reusar la lógica original, pero parchear el caso de category + search_filter = [ + ( + "location_id", + "child_of" if not self.exclude_sublocation else "in", + self.location_ids.ids, + ), + ("to_do", "=", True), + ] + + error_field = "location_id" + error_message = _( + "There's already an Adjustment in Process " + "using one requested Location: %(names)s. " + "Blocking adjustments: %(blocking_names)s" + ) + + if self.product_ids: + search_filter.append(("product_id", "in", self.product_ids.ids)) + error_field = "product_id" + error_message = _( + "There are active adjustments for the requested products: %(names)s. " + "Blocking adjustments: %(blocking_names)s" + ) + elif self.category_id: + # Solo misma categoría exacta, sin OR hijos + search_filter.append(("product_id.categ_id", "=", self.category_id.id)) + error_field = "category_id" + error_message = _( + "There are active adjustments for the requested category: %(names)s. " + "Blocking adjustments: %(blocking_names)s" + ) + + quants = self.env["stock.quant"].search(search_filter) + if quants: + inventory_ids = self.env["stock.inventory"].search( + [("stock_quant_ids", "in", quants.ids), ("state", "=", "in_progress")] + ) + if inventory_ids: + blocking_names = ", ".join(inventory_ids.mapped("name")) + names = self._get_quant_joined_names(quants, error_field) + raise ValidationError( + error_message % {"names": names, "blocking_names": blocking_names} + ) + + quants = self._get_quants(self.location_ids) + self.write({"state": "in_progress", "stock_quant_ids": [(6, 0, quants.ids)]}) + quants.write( + { + "to_do": True, + "user_id": self.responsible_id, + "inventory_date": self.date, + "current_inventory_id": self.id, + } + ) + return