summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/promotion/promotion_monitoring.py
blob: 9df0825d9f83378fad369ff714c10d54c7ea5abd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from odoo import fields, models, tools


class PromotionMonitoring(models.Model):
    _name = "promotion.monitoring"
    _auto = False
    _rec_name = "product_id"

    product_id = fields.Many2one(comodel_name="product.product", string="Product")
    manufacture_id = fields.Many2one(comodel_name="x_manufactures", string="Manufacture", related="product_id.x_manufacture")
    price = fields.Float(string="Price", help="Computed Price di Product Pricelist (Tier 1 New)")
    has_promo = fields.Boolean(string="Has Promo")
    count_active = fields.Integer(string="Count Active")
    count_inactive = fields.Integer(string="Count Inactive")

    def init(self):
        tools.drop_view_if_exists(self.env.cr, self._table)
        sql = {
            'count_active': "COUNT(CASE WHEN ppl.active = True THEN ppl.id ELSE NULL END)", 
            'count_inactive': "COUNT(CASE WHEN ppl.active = False THEN ppl.id ELSE NULL END)"
        }
        self.env.cr.execute("""
            CREATE OR REPLACE VIEW {table} AS (
                SELECT 
                    p.id as id,
                    p.id as product_id,
                    ppi.computed_price as price,
                    ({count_active} > 0) as has_promo,
                    {count_active} as count_active,
                    {count_inactive} as count_inactive
                FROM product_product p
                LEFT JOIN product_template pt ON pt.id = p.product_tmpl_id
                LEFT JOIN promotion_product pp ON pp.product_id = p.id 
                LEFT JOIN promotion_program_line ppl ON ppl.id = pp.program_line_id
                LEFT JOIN product_pricelist_item ppi ON ppi.product_id = p.id
                WHERE p.active = True
                AND pt.sale_ok = True
                AND ppi.pricelist_id = 17023
                GROUP BY p.id, ppi.id
            )
        """.format(
            table=self._table, 
            count_active=sql['count_active'],
            count_inactive=sql['count_inactive']
        ))