Browse Source

[FIX] b_stock_coverage: store computed fields

pull/166/head
robin.keunen 4 years ago
parent
commit
66a90cb970
  1. 14
      beesdoo_stock_coverage/models/product_template.py

14
beesdoo_stock_coverage/models/product_template.py

@ -20,16 +20,21 @@ class ProductTemplate(models.Model):
computation_range = fields.Integer("Computation range (days)", default=14) computation_range = fields.Integer("Computation range (days)", default=14)
range_sales = fields.Float( range_sales = fields.Float(
string="Sales over Range", compute="_compute_stock_coverage"
string="Sales over Range",
compute="_compute_stock_coverage",
store=True,
) )
daily_sales = fields.Float( daily_sales = fields.Float(
string="Daily Sales", compute="_compute_stock_coverage"
string="Daily Sales", compute="_compute_stock_coverage", store=True,
) )
stock_coverage = fields.Float( stock_coverage = fields.Float(
string="Stock Coverage (days)", compute="_compute_stock_coverage"
string="Stock Coverage (days)",
compute="_compute_stock_coverage",
store=True,
) )
@api.multi @api.multi
@api.depends("computation_range", "virtual_available", "active")
def _compute_stock_coverage(self): def _compute_stock_coverage(self):
query = """ query = """
select template.id as product_template_id, select template.id as product_template_id,
@ -48,7 +53,8 @@ class ProductTemplate(models.Model):
and template.id in %(template_ids)s and template.id in %(template_ids)s
group by product_template_id group by product_template_id
""" """
self.env.cr.execute(query, {"template_ids": tuple(self.ids)})
template_ids = tuple(self.ids) if self.ids else (self._origin.id,)
self.env.cr.execute(query, {"template_ids": template_ids})
results = {pid: (qty, avg) for pid, qty, avg in self.env.cr.fetchall()} results = {pid: (qty, avg) for pid, qty, avg in self.env.cr.fetchall()}
for template in self: for template in self:
qty, avg = results.get(template.id, (0, 0)) qty, avg = results.get(template.id, (0, 0))

Loading…
Cancel
Save