mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
[CHG] improve code regarding code review [ADD] add test [CHG] optimize stock computation by avoiding to call useless compute
58 lines
2.2 KiB
Python
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.")
|