|
|
@ -32,6 +32,13 @@ class ProductTemplate(models.Model): |
|
|
|
compute="_compute_stock_coverage", |
|
|
|
store=True, |
|
|
|
) |
|
|
|
effective_sale_price = fields.Float( |
|
|
|
string="Effective Sale Price", |
|
|
|
compute="_compute_stock_coverage", |
|
|
|
store=True, |
|
|
|
help="SUM (total sale price without vat / sold quantity ) " |
|
|
|
"/ count (pos order line)", |
|
|
|
) |
|
|
|
|
|
|
|
@api.multi |
|
|
|
@api.depends("computation_range", "virtual_available", "active") |
|
|
@ -39,7 +46,11 @@ class ProductTemplate(models.Model): |
|
|
|
query = """ |
|
|
|
select template.id as product_template_id, |
|
|
|
sum(pol.qty) as total_sales, |
|
|
|
sum(pol.qty) / template.computation_range as daily_sales |
|
|
|
sum(pol.qty) / template.computation_range as daily_sales, |
|
|
|
sum(pol.price_subtotal / pol.qty) / |
|
|
|
count(pol.id) as effective_sale_price, |
|
|
|
sum(pol.price_subtotal_incl / pol.qty) / |
|
|
|
count(pol.id) as effective_sale_price_incl |
|
|
|
from pos_order_line pol |
|
|
|
join pos_order po ON pol.order_id = po.id |
|
|
|
join product_product product ON pol.product_id = product.id |
|
|
@ -50,6 +61,7 @@ class ProductTemplate(models.Model): |
|
|
|
and po.date_order |
|
|
|
BETWEEN now() - template.computation_range * interval '1 days' |
|
|
|
and now() |
|
|
|
and pol.qty != 0 |
|
|
|
and template.id in %(template_ids)s |
|
|
|
group by product_template_id |
|
|
|
""" |
|
|
@ -62,11 +74,19 @@ class ProductTemplate(models.Model): |
|
|
|
return True |
|
|
|
|
|
|
|
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, esp, espi) |
|
|
|
for pid, qty, avg, esp, espi in self.env.cr.fetchall() |
|
|
|
} |
|
|
|
for template in self: |
|
|
|
qty, avg = results.get(template.id, (0, 0)) |
|
|
|
qty, avg, esp, espi = results.get(template.id, (0, 0, 0, 0)) |
|
|
|
template.range_sales = qty |
|
|
|
template.daily_sales = avg |
|
|
|
if any(template.taxes_id.mapped("price_include")): |
|
|
|
template.effective_sale_price = espi |
|
|
|
else: |
|
|
|
template.effective_sale_price = esp |
|
|
|
|
|
|
|
if avg != 0: |
|
|
|
template.stock_coverage = template.virtual_available / avg |
|
|
|
else: |
|
|
|