From 768f40936af82bdefe2abbe55c1b88229c677e84 Mon Sep 17 00:00:00 2001 From: sergio-teruel Date: Mon, 23 Sep 2024 10:58:31 +0200 Subject: [PATCH] [FIX] stock_owner_restriction: Wrong qty available computation for owner and product at the same time. --- stock_owner_restriction/README.rst | 6 ++++++ stock_owner_restriction/models/product.py | 10 +++++++++- stock_owner_restriction/readme/CONFIGURE.rst | 6 ++++++ stock_owner_restriction/static/description/index.html | 4 ++++ .../tests/test_stock_owner_restriction.py | 10 ++++++++-- 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/stock_owner_restriction/README.rst b/stock_owner_restriction/README.rst index 4fe5a18131e5..90ac9e4260e2 100644 --- a/stock_owner_restriction/README.rst +++ b/stock_owner_restriction/README.rst @@ -49,6 +49,12 @@ To configure this module you need to: #. Select an operation type, or create a new one, and set *Owner Restriction* field to the desired value. +Developers notes + +This module update the context dependency of product quantity available to be computed +correctly. If you need get product quantity available for an owner yo need set the +context key "force_restricted_owner_id". + Usage ===== diff --git a/stock_owner_restriction/models/product.py b/stock_owner_restriction/models/product.py index 3115ae45640d..d5b612a20215 100644 --- a/stock_owner_restriction/models/product.py +++ b/stock_owner_restriction/models/product.py @@ -1,7 +1,7 @@ # Copyright 2020 Carlos Dauden - Tecnativa # Copyright 2020 Sergio Teruel - Tecnativa # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import models +from odoo import api, models class ProductProduct(models.Model): @@ -34,3 +34,11 @@ def _search_qty_available_new( return super(ProductProduct, new_self)._search_qty_available_new( operator, value, lot_id=lot_id, owner_id=owner_id, package_id=package_id ) + + @api.depends_context("force_restricted_owner_id") + def _compute_quantities(self): + # Add force_restricted_owner_id to depends_context to compute qty when this + # key change. + # For example, in a report you want to print all stock available from a product + # and stock available from one owner. + return super()._compute_quantities() diff --git a/stock_owner_restriction/readme/CONFIGURE.rst b/stock_owner_restriction/readme/CONFIGURE.rst index f8874be86c2e..c1d25fb0c1f0 100644 --- a/stock_owner_restriction/readme/CONFIGURE.rst +++ b/stock_owner_restriction/readme/CONFIGURE.rst @@ -6,3 +6,9 @@ To configure this module you need to: #. Go to *Inventory > Configuration > Operation Types*. #. Select an operation type, or create a new one, and set *Owner Restriction* field to the desired value. + +Developers notes + +This module update the context dependency of product quantity available to be computed +correctly. If you need get product quantity available for an owner yo need set the +context key "force_restricted_owner_id". diff --git a/stock_owner_restriction/static/description/index.html b/stock_owner_restriction/static/description/index.html index 360eeefe06c4..6567dc2ba1b7 100644 --- a/stock_owner_restriction/static/description/index.html +++ b/stock_owner_restriction/static/description/index.html @@ -397,6 +397,10 @@

Configuration

  • Select an operation type, or create a new one, and set Owner Restriction field to the desired value.
  • +

    Developers notes

    +

    This module update the context dependency of product quantity available to be computed +correctly. If you need get product quantity available for an owner yo need set the +context key “force_restricted_owner_id”.

    Usage

    diff --git a/stock_owner_restriction/tests/test_stock_owner_restriction.py b/stock_owner_restriction/tests/test_stock_owner_restriction.py index af3824353225..a084c21d8710 100644 --- a/stock_owner_restriction/tests/test_stock_owner_restriction.py +++ b/stock_owner_restriction/tests/test_stock_owner_restriction.py @@ -68,8 +68,14 @@ def setUpClass(cls): def test_product_qty_available(self): # Quants with owner assigned are not available - self.assertEqual(self.product.qty_available, 500.00) - self.product.invalidate_cache() + # No need invalidate the cache, force_restricted_owner_id key is added to + # context depends of product qty_available + self.assertEqual( + self.product.with_context( + force_restricted_owner_id=self.owner.id + ).qty_available, + 500.00, + ) self.assertEqual( self.product.with_context(skip_restricted_owner=True).qty_available, 1000.00 )