Skip to content

Commit

Permalink
[FIX] stock_owner_restriction: Wrong qty available computation for ow…
Browse files Browse the repository at this point in the history
…ner and product at the same time.
  • Loading branch information
sergio-teruel committed Sep 23, 2024
1 parent b75f12d commit 768f409
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
6 changes: 6 additions & 0 deletions stock_owner_restriction/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====

Expand Down
10 changes: 9 additions & 1 deletion stock_owner_restriction/models/product.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()
6 changes: 6 additions & 0 deletions stock_owner_restriction/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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".
4 changes: 4 additions & 0 deletions stock_owner_restriction/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ <h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<li>Select an operation type, or create a new one, and set <em>Owner Restriction</em>
field to the desired value.</li>
</ol>
<p>Developers notes</p>
<p>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”.</p>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
Expand Down
10 changes: 8 additions & 2 deletions stock_owner_restriction/tests/test_stock_owner_restriction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit 768f409

Please sign in to comment.