Browse Source

store and compute total_consumption

pull/139/head
robinkeunen 7 years ago
committed by robin.keunen
parent
commit
9fede5a91c
  1. 1
      stock_coverage/README.md
  2. 15
      stock_coverage/models/product_template.py
  3. 4
      stock_coverage/tests/test_stock_coverage.py

1
stock_coverage/README.md

@ -1,3 +1,4 @@
# todo
- initializing the computed fields - initializing the computed fields
- daily cron - daily cron
- manual trigger - manual trigger

15
stock_coverage/models/product_template.py

@ -26,6 +26,8 @@ class ProductTemplate(models.Model):
total_consumption = fields.Float( total_consumption = fields.Float(
string='Total Consumption', string='Total Consumption',
default=0, default=0,
compute='_compute_total_consumption',
store=True,
readonly=True, readonly=True,
digits=(100, 2), digits=(100, 2),
) )
@ -39,7 +41,7 @@ class ProductTemplate(models.Model):
) )
@api.multi @api.multi
@api.depends('calculation_range')
@api.depends('total_consumption')
def _compute_average_daily_consumption(self): def _compute_average_daily_consumption(self):
for template in self: for template in self:
if template.calculation_range > 0: if template.calculation_range > 0:
@ -51,7 +53,7 @@ class ProductTemplate(models.Model):
return True return True
@api.multi @api.multi
@api.onchange('calculation_range')
@api.depends('calculation_range')
def _compute_total_consumption(self): def _compute_total_consumption(self):
for template in self: for template in self:
products = ( products = (
@ -73,15 +75,14 @@ class ProductTemplate(models.Model):
if order_lines: if order_lines:
order_lines = order_lines.filtered( order_lines = order_lines.filtered(
lambda oi: oi.order_id.state in ['done', 'invoiced', 'paid']) # noqa
res = sum(order_lines.mapped('qty'))
lambda ol: ol.order_id.state in ['done', 'invoiced', 'paid']) # noqa
template.total_consumption = sum(order_lines.mapped('qty'))
else: else:
res = 0
template.total_consumption = res
template.total_consumption = 0
return True return True
@api.multi @api.multi
@api.depends('calculation_range')
@api.depends('total_consumption')
def _compute_estimated_stock_coverage(self): def _compute_estimated_stock_coverage(self):
for product_template in self: for product_template in self:
qty = product_template.qty_available qty = product_template.qty_available

4
stock_coverage/tests/test_stock_coverage.py

@ -58,7 +58,7 @@ class TestProductTemplate(TransactionCase):
} }
return {pid: mock_data for pid in products.ids} return {pid: mock_data for pid in products.ids}
# mock area
# mock area fixme
# ProductTemplate._product_available = _product_available # ProductTemplate._product_available = _product_available
# ProductProduct._product_available = _product_available # ProductProduct._product_available = _product_available
# Order = namedtuple('Order', ['id', 'state']) # Order = namedtuple('Order', ['id', 'state'])
@ -92,7 +92,7 @@ class TestProductTemplate(TransactionCase):
expected_value = 57.11 expected_value = 57.11
self.assertAlmostEqual(computed_value, expected_value) self.assertAlmostEqual(computed_value, expected_value)
# def test_compute_estimated_stock_coverage(self):
# def test_compute_estimated_stock_coverage(self): fixme
# """Test computed field estimated_stock_coverage""" # """Test computed field estimated_stock_coverage"""
# ProductTemplate = self.env['product.template'] # ProductTemplate = self.env['product.template']
# product_template = ProductTemplate.browse(self.product_template_id) # product_template = ProductTemplate.browse(self.product_template_id)

Loading…
Cancel
Save