Compare commits

...

1 Commits

Author SHA1 Message Date
robin.keunen aba6202662 [ADD] b_stock_coverage: add effective sale price 4 years ago
  1. 2
      .isort.cfg
  2. 2
      beesdoo_product_usability/views/beesdoo_product.xml
  3. 34
      beesdoo_stock_coverage/models/product_template.py
  4. 2
      beesdoo_stock_coverage/views/product_template_view.xml

2
.isort.cfg

@ -9,4 +9,4 @@ line_length = 79
known_odoo = odoo known_odoo = odoo
known_odoo_addons = odoo.addons known_odoo_addons = odoo.addons
sections = FUTURE,STDLIB,THIRDPARTY,ODOO,ODOO_ADDONS,FIRSTPARTY,LOCALFOLDER sections = FUTURE,STDLIB,THIRDPARTY,ODOO,ODOO_ADDONS,FIRSTPARTY,LOCALFOLDER
known_third_party = pytz
known_third_party = pytz,setuptools

2
beesdoo_product_usability/views/beesdoo_product.xml

@ -20,6 +20,8 @@
<field name="stock_coverage"/> <field name="stock_coverage"/>
<field name="daily_sales"/> <field name="daily_sales"/>
<field name="range_sales"/> <field name="range_sales"/>
<field name="list_price"/>
<field name="effective_sale_price"/>
</tree> </tree>
</field> </field>
</record> </record>

34
beesdoo_stock_coverage/models/product_template.py

@ -32,25 +32,37 @@ class ProductTemplate(models.Model):
compute="_compute_stock_coverage", compute="_compute_stock_coverage",
store=True, 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.multi
@api.depends("computation_range", "virtual_available", "active") @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,
sum(pol.qty) as total_sales,
sum(pol.qty) / template.computation_range as daily_sales
select template.id as product_template_id,
sum(pol.qty) as total_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 from pos_order_line pol
join pos_order po ON pol.order_id = po.id join pos_order po ON pol.order_id = po.id
join product_product product ON pol.product_id = product.id join product_product product ON pol.product_id = product.id
join product_template template join product_template template
ON product.product_tmpl_id = template.id
ON product.product_tmpl_id = template.id
where po.state in ('done', 'invoiced', 'paid') where po.state in ('done', 'invoiced', 'paid')
and template.active and template.active
and po.date_order and po.date_order
BETWEEN now() - template.computation_range * interval '1 days' BETWEEN now() - template.computation_range * interval '1 days'
and now() and now()
and template.id in %(template_ids)s
and pol.qty != 0
and template.id in %(template_ids)s
group by product_template_id group by product_template_id
""" """
@ -62,11 +74,19 @@ class ProductTemplate(models.Model):
return True return True
self.env.cr.execute(query, {"template_ids": template_ids}) 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: 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.range_sales = qty
template.daily_sales = avg 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: if avg != 0:
template.stock_coverage = template.virtual_available / avg template.stock_coverage = template.virtual_available / avg
else: else:

2
beesdoo_stock_coverage/views/product_template_view.xml

@ -12,6 +12,7 @@
<field name="daily_sales"/> <field name="daily_sales"/>
<field name="range_sales"/> <field name="range_sales"/>
<field name="stock_coverage"/> <field name="stock_coverage"/>
<field name="effective_sale_price"/>
</group> </group>
</group> </group>
</field> </field>
@ -26,6 +27,7 @@
<field name="range_sales"/> <field name="range_sales"/>
<field name="daily_sales"/> <field name="daily_sales"/>
<field name="stock_coverage"/> <field name="stock_coverage"/>
<field name="effective_sale_price"/>
</field> </field>
</field> </field>
</record> </record>

Loading…
Cancel
Save