Files
stock-logistics-warehouse/stock_available/models/product_template.py
Cédric Pigeon 4988d74218 [10.0]stock_available: improve computation performance
[CHG] improve code regarding code review

[ADD] add test

[CHG] optimize stock computation by avoiding to call useless compute
2020-06-16 11:18:12 +07:00

58 lines
2.2 KiB
Python

# -*- coding: utf-8 -*-
# Copyright 2014 Numérigraphe
# Copyright 2016 Sodexis
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import models, fields, api
from odoo.addons import decimal_precision as dp
class ProductTemplate(models.Model):
_inherit = 'product.template'
@api.multi
@api.depends('product_variant_ids.immediately_usable_qty',
'product_variant_ids.potential_qty')
def _compute_available_quantities(self):
res = self._compute_available_quantities_dict()
for product in self:
data = res[product.id]
for key, value in data.iteritems():
if key in product._fields:
product[key] = value
@api.multi
def _compute_available_quantities_dict(self):
variants_dict = self.mapped(
'product_variant_ids')._compute_available_quantities_dict()
res = {}
for template in self:
immediately_usable_qty = sum(
[variants_dict[p.id]["immediately_usable_qty"] for p in
template.product_variant_ids])
potential_qty = max(
[variants_dict[p.id]["potential_qty"] for p in
template.product_variant_ids] or [0.0])
res[template.id] = {
"immediately_usable_qty": immediately_usable_qty,
"potential_qty": potential_qty,
}
return res
immediately_usable_qty = fields.Float(
digits=dp.get_precision('Product Unit of Measure'),
compute='_compute_available_quantities',
string='Available to promise',
help="Stock for this Product that can be safely proposed "
"for sale to Customers.\n"
"The definition of this value can be configured to suit "
"your needs")
potential_qty = fields.Float(
compute='_compute_available_quantities',
digits=dp.get_precision('Product Unit of Measure'),
string='Potential',
help="Quantity of this Product that could be produced using "
"the materials already at hand. "
"If the product has several variants, this will be the biggest "
"quantity that can be made for a any single variant.")