# -*- encoding: utf-8 -*- import logging from openerp import models, fields, api, _ from openerp.exceptions import Warning _logger = logging.getLogger(__name__) class StockInventory(models.Model): _inherit = "stock.inventory" category_id = fields.Many2one( comodel_name='product.category', string='Categoría de Producto', readonly=True, states={'draft': [('readonly', False)]}, ) filter = fields.Selection( selection='_get_available_filters' ) @api.model def _get_available_filters(self): res_filter = super(StockInventory, self)._get_available_filters() res_filter.append(('category2', _('Inventario inicial por categoría'))) return res_filter @api.model def _get_inventory_lines(self, inventory): if inventory.category_id: product_obj = self.env['product.product'] categ_obj = self.env['product.category'] categories = categ_obj.search([('id', 'child_of', [inventory.category_id.id])]) sql = "SELECT pp.id as product_id, pc.id as category_id FROM product_product as pp INNER JOIN product_template as pt ON pp.product_tmpl_id = pt.id INNER JOIN product_category as pc ON pt.categ_id = pc.id WHERE pp.active = true and pt.type = 'product' and pc.id IN %s" self.env.cr.execute(sql, (tuple(categories.ids),)) vals = [] for product_line in self.env.cr.dictfetchall(): #replace the None the dictionary by False, because falsy values are tested later on for key, value in product_line.items(): if not value: product_line[key] = False product_line['theoretical_qty'] = 0 product_line['product_id'] = product_line['product_id'] product_line['location_id'] = inventory.location_id.id product_line['prod_lot_id'] = False product_line['category_id'] = product_line['category_id'] product_line['inventory_id'] = inventory.id product_line['package_id'] = False product_line['product_qty'] = 0 product_line['partner_id'] = False if product_line['product_id']: product = product_obj.browse(product_line['product_id']) product_line['product_uom_id'] = product.uom_id.id vals.append(product_line) else: vals = super(StockInventory, self)._get_inventory_lines(inventory) return vals