Skip to content

Commit

Permalink
[CHG] optimize stock computation by avoiding to call useless compute
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedric-Pigeon authored and sbejaoui committed Jun 3, 2021
1 parent b9403b3 commit 3c896fc
Showing 1 changed file with 10 additions and 34 deletions.
44 changes: 10 additions & 34 deletions stock_available_unreserved/models/product.py
Expand Up @@ -23,37 +23,15 @@ class ProductTemplate(models.Model):
string='Unreserved stock quantity')

@api.multi
@api.depends('product_variant_ids.qty_available_not_res')
def _compute_product_available_not_res(self):
no_new = self.filtered(
lambda x: not isinstance(x.id, models.NewId))
res = no_new._product_available()
for tmpl in no_new:
qty = res[tmpl.id]['qty_available_not_res']
tmpl.qty_available_not_res = qty
text = res[tmpl.id]['qty_available_stock_text']
tmpl.qty_available_stock_text = text

def _compute_quantities_dict(self):
prod_available = super(ProductTemplate, self).\
_compute_quantities_dict()

variants = self.mapped('product_variant_ids')
variant_available = variants._product_available()

for product in self:
if isinstance(product.id, models.NewId):
continue
qty_available_not_res = 0.0
text = ''
for p in product.product_variant_ids:
qty = variant_available[p.id]["qty_available_not_res"]
qty_available_not_res += qty
text = variant_available[p.id]["qty_available_stock_text"]
prod_available[product.id].update({
"qty_available_not_res": qty_available_not_res,
"qty_available_stock_text": text,
})
return prod_available
tmpl.qty_available_not_res = sum(tmpl.mapped(
'product_variant_ids.qty_available_not_res'))
tmpl.qty_available_stock_text = "/".join(tmpl.mapped(
'product_variant_ids.qty_available_stock_text'))

@api.multi
def action_open_quants_unreserved(self):
Expand All @@ -79,7 +57,7 @@ class ProductProduct(models.Model):

@api.multi
def _compute_qty_available_not_res(self):
res = self._product_available()
res = self._compute_product_available_not_res_dict()
for prod in self:
qty = res[prod.id]['qty_available_not_res']
text = res[prod.id]['qty_available_stock_text']
Expand All @@ -106,12 +84,9 @@ def _product_available_not_res_hook(self, quants):
return False

@api.multi
def _compute_quantities_dict(self, lot_id, owner_id, package_id,
from_date=False, to_date=False):
res = super(ProductProduct, self).\
_compute_quantities_dict(
lot_id=lot_id, owner_id=owner_id, package_id=package_id,
from_date=from_date, to_date=to_date)
def _compute_product_available_not_res_dict(self):

res = {}

domain_quant = self._prepare_domain_available_not_res(self)

Expand All @@ -126,6 +101,7 @@ def _compute_quantities_dict(self, lot_id, owner_id, package_id,
values_prod.setdefault(quant['product_id'][0], 0)
values_prod[quant['product_id'][0]] += quant['qty']
for product in self:
res[product.id] = {}
# get total qty for the product
qty = float_round(values_prod.get(product.id, 0.0),
precision_rounding=product.uom_id.rounding)
Expand Down

0 comments on commit 3c896fc

Please sign in to comment.