Skip to content

Commit 1a12a0e

Browse files
committed
[IMP] stock_available_location_orderpoint: Don't compute to replenish with other fields
As for performances improvements, we don't need always the replenishement quantities in the recordset with the other quantity fields
1 parent 35e8fcd commit 1a12a0e

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

stock_available_location_orderpoint/models/product_product.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33
from collections import defaultdict
44

5-
from odoo import fields, models
5+
from odoo import api, fields, models
66
from odoo.tools import float_compare
77
from odoo.tools.safe_eval import safe_eval
88

@@ -14,36 +14,38 @@ class ProductProduct(models.Model):
1414
_inherit = "product.product"
1515

1616
quantity_to_replenish = fields.Float(
17-
compute="_compute_available_quantities",
17+
compute="_compute_replenishment_quantities",
1818
search="_search_quantity_to_replenish",
19+
prefetch=False,
1920
help="This is the quantity to replenish following the location orderpoints.",
2021
)
2122
quantity_in_replenishments = fields.Float(
22-
compute="_compute_available_quantities",
23+
compute="_compute_replenishment_quantities",
2324
search="_search_quantity_in_replenishments",
25+
prefetch=False,
2426
help="This is the quantity currently in replenishments following the "
2527
"location orderpoints.",
2628
)
2729

28-
def _compute_available_quantities_dict(self):
29-
"""
30-
Retrieve all replenishment quantities for the selected products
31-
and locations.
32-
"""
33-
res, stock_dict = super()._compute_available_quantities_dict()
34-
location_domain = self._get_domain_location_for_locations()
35-
locations = self.env["stock.location"].search(location_domain)
30+
@api.depends_context("location")
31+
def _compute_replenishment_quantities(self):
3632
orderpoint_obj = self.env["stock.location.orderpoint"]
3733
if orderpoint_obj.check_access_rights("read", raise_exception=False):
34+
orderpoint_obj = self.env["stock.location.orderpoint"]
35+
location_domain = self._get_domain_location_for_locations()
36+
locations = self.env["stock.location"].search(location_domain)
3837
orderpoint_domain = orderpoint_obj._prepare_orderpoint_domain_location(
3938
locations.ids
4039
)
4140
orderpoints = orderpoint_obj.search(orderpoint_domain)
4241
else:
43-
for product in self:
44-
res[product.id]["quantity_to_replenish"] = 0
45-
res[product.id]["quantity_in_replenishments"] = 0
46-
return res, stock_dict
42+
self.update(
43+
{
44+
"quantity_to_replenish": 0.0,
45+
"quantity_in_replenishments": 0.0,
46+
}
47+
)
48+
return
4749

4850
# Merge both source locations and destination locations
4951
location_ids = set(
@@ -84,14 +86,18 @@ def _compute_available_quantities_dict(self):
8486
)
8587
> 0
8688
):
89+
# We take the maximum value from all the concerned orderpoints
90+
# for the product
8791
qties_replenished_for_location[product] += qty_to_replenish
88-
res[product.id][
89-
"quantity_in_replenishments"
90-
] = quantities_in_replenishments[product.id]
91-
res[product.id]["quantity_to_replenish"] = qties_replenished_for_location[
92-
product
93-
]
94-
return res, stock_dict
92+
product.update(
93+
{
94+
"quantity_in_replenishments": quantities_in_replenishments[
95+
product.id
96+
],
97+
"quantity_to_replenish": qties_replenished_for_location[product],
98+
}
99+
)
100+
return
95101

96102
def _get_search_quantity_to_replenish_domain(self):
97103
return [("type", "=", "product")]

stock_available_location_orderpoint/tests/test_available_location_orderpoint_template.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ def test_available_on_different_sublocation(self):
456456
self.assertEqual(move.state, "confirmed")
457457

458458
self.template.invalidate_recordset()
459+
self.template.product_variant_ids.invalidate_recordset()
459460
self.assertEqual(12.0, self.template.quantity_to_replenish)
460461

461462
self.template.invalidate_recordset()
@@ -468,6 +469,7 @@ def test_available_on_different_sublocation(self):
468469
self.orderpoint_shelf_1.run_replenishment()
469470
# Test all variables in different contexts
470471
self.template.invalidate_recordset()
472+
self.template.product_variant_ids.invalidate_recordset()
471473
self.assertEqual(5.0, self.template.quantity_to_replenish)
472474
self.assertEqual(7.0, self.template.quantity_in_replenishments)
473475
self.template.invalidate_recordset()

0 commit comments

Comments
 (0)