initial_inventory.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- encoding: utf-8 -*-
  2. import logging
  3. from openerp import models, fields, api, _
  4. from openerp.exceptions import Warning
  5. _logger = logging.getLogger(__name__)
  6. class StockInventory(models.Model):
  7. _inherit = "stock.inventory"
  8. category_id = fields.Many2one(
  9. comodel_name='product.category',
  10. string='Categoría de Producto',
  11. readonly=True,
  12. states={'draft': [('readonly', False)]},
  13. )
  14. filter = fields.Selection(
  15. selection='_get_available_filters'
  16. )
  17. @api.model
  18. def _get_available_filters(self):
  19. res_filter = super(StockInventory, self)._get_available_filters()
  20. res_filter.append(('category2', _('Inventario inicial por categoría')))
  21. return res_filter
  22. @api.model
  23. def _get_inventory_lines(self, inventory):
  24. if inventory.category_id:
  25. product_obj = self.env['product.product']
  26. categ_obj = self.env['product.category']
  27. categories = categ_obj.search([('id', 'child_of', [inventory.category_id.id])])
  28. 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"
  29. self.env.cr.execute(sql, (tuple(categories.ids),))
  30. vals = []
  31. for product_line in self.env.cr.dictfetchall():
  32. #replace the None the dictionary by False, because falsy values are tested later on
  33. for key, value in product_line.items():
  34. if not value:
  35. product_line[key] = False
  36. product_line['theoretical_qty'] = 0
  37. product_line['product_id'] = product_line['product_id']
  38. product_line['location_id'] = inventory.location_id.id
  39. product_line['prod_lot_id'] = False
  40. product_line['category_id'] = product_line['category_id']
  41. product_line['inventory_id'] = inventory.id
  42. product_line['package_id'] = False
  43. product_line['product_qty'] = 0
  44. product_line['partner_id'] = False
  45. if product_line['product_id']:
  46. product = product_obj.browse(product_line['product_id'])
  47. product_line['product_uom_id'] = product.uom_id.id
  48. vals.append(product_line)
  49. else:
  50. vals = super(StockInventory, self)._get_inventory_lines(inventory)
  51. return vals