Untitled

 avatar
unknown
python
a month ago
2.0 kB
2
Indexable
def _select(self):
        return """
            MIN(pp.id) AS id, 
            pp.id AS product_id,
            pt.name AS product_name,
            SUM(sq.quantity) AS item_quantity,
            pt.uom_id AS uom_id,
            pt.list_price AS avg_unit_cost,
        """

    def _group_by(self):
        return """
            pp.id, 
            pt.name, 
            pt.uom_id,
            pt.list_price,
        """

    def _from(self):
        return f"""
            product_product pp
            JOIN 
                product_template pt ON pp.product_tmpl_id = pt.id
            JOIN 
                stock_quant sq ON sq.product_id = pp.id
            JOIN 
                stock_location sl ON sq.location_id = sl.id
            LEFT JOIN 
                max_pc ON max_pc.product_template_id = pt.id
        """

    def _where(self):
        return """
            sl.usage = 'internal'
        """

    def _order_by(self):
        return """
            pp.id
        """

    def _table_tmp(self):
        return """
            WITH max_pc AS (
                SELECT 
                    product_template_id,
                    id AS pc_id,
                    ask,
                    ssp_times,
                    ssp_add
                FROM 
                    price_calculation pc
                WHERE 
                    id = (SELECT MAX(id) 
                        FROM price_calculation 
                        WHERE product_template_id = pc.product_template_id)
            )
        """

    def init(self):
        tools.drop_view_if_exists(self.env.cr, self._table)
        self.env.cr.execute("""CREATE or REPLACE VIEW %s as (
            %s
            SELECT %s
            FROM %s
            WHERE %s
            GROUP BY %s
            ORDER BY %s
            )""" % (self._table, self._table_tmp(), self._select(), self._from(),
                    self._where(), self._group_by(), self._order_by())
        )
Leave a Comment