categories.py 856 Bytes
# -*- coding: utf-8 -*-

from openerp import _, api, models, fields

class LaCagetteCategories(models.Model):
    _name = "lacagette.categories"



    @api.model
    def get_all_with_products_count(self):
        res = {}
        try:
            sql = """
            SELECT
            categ_id, count(*) as nb
            FROM product_template
            WHERE
            active=true AND sale_ok=true
            GROUP BY categ_id;
            """
            self.env.cr.execute(sql)
            res['list'] = {}
            req_res = self.env.cr.dictfetchall()
            for u in req_res:
                for attr in u.keys():
                    if u[attr] is None:
                        u[attr] = ''
                res['list'][str(u['categ_id'])] = u['nb']
        except Exception as e:
            res['error'] = str(e)
        return res