stock_inventory.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (C) 2016 ICTSTUDIO (<http://www.ictstudio.eu>).
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ##############################################################################
  20. import logging
  21. from openerp import models, fields, api, _
  22. _logger = logging.getLogger(__name__)
  23. class StockInventory(models.Model):
  24. _inherit = "stock.inventory"
  25. categ_id = fields.Many2one(
  26. comodel_name='product.category',
  27. string='Categoría de producto',
  28. readonly=True,
  29. states={'draft': [('readonly', False)]},
  30. help="Specify Product Category to focus your inventory on a particular Product Category."
  31. )
  32. filter = fields.Selection(
  33. selection='_get_available_filters'
  34. )
  35. @api.model
  36. def _get_available_filters(self):
  37. res_filter = super(StockInventory, self)._get_available_filters()
  38. res_filter.append(('category', _('Por categoría de producto')))
  39. return res_filter
  40. @api.model
  41. def _get_inventory_lines(self, inventory):
  42. if inventory.categ_id:
  43. location_obj = self.env['stock.location']
  44. product_obj = self.env['product.product']
  45. categ_obj = self.env['product.category']
  46. locations = location_obj.search(
  47. [
  48. ('id', 'child_of', [inventory.location_id.id])
  49. ]
  50. )
  51. categories = categ_obj.search(
  52. [
  53. ('id', 'child_of', [inventory.categ_id.id])
  54. ]
  55. )
  56. domain = ' sq.location_id in %s'
  57. args = (tuple(locations.ids),)
  58. domain += ' and pc.id in %s'
  59. args += (tuple(categories.ids),)
  60. if inventory.partner_id:
  61. domain += ' and sq.owner_id = %s'
  62. args += (inventory.partner_id.id,)
  63. if inventory.lot_id:
  64. domain += ' and sq.lot_id = %s'
  65. args += (inventory.lot_id.id,)
  66. if inventory.product_id:
  67. domain += ' and sq.product_id = %s'
  68. args += (inventory.product_id.id,)
  69. if inventory.package_id:
  70. domain += ' and sq.package_id = %s'
  71. args += (inventory.package_id.id,)
  72. self.env.cr.execute('''
  73. SELECT sq.product_id as product_id, sum(sq.qty) as product_qty, sq.location_id as location_id,
  74. sq.lot_id as prod_lot_id, sq.package_id as package_id, sq.owner_id as partner_id, pc.id as categ_id
  75. FROM stock_quant as sq
  76. INNER JOIN product_product as pp on pp.id = sq.product_id
  77. INNER JOIN product_template as pt on pp.product_tmpl_id = pt.id
  78. INNER JOIN product_category as pc on pt.categ_id = pc.id
  79. WHERE''' + domain + '''
  80. GROUP BY sq.product_id, sq.location_id, sq.lot_id, sq.package_id, sq.owner_id, pc.id
  81. ''', args)
  82. vals = []
  83. for product_line in self.env.cr.dictfetchall():
  84. #replace the None the dictionary by False, because falsy values are tested later on
  85. for key, value in product_line.items():
  86. if not value:
  87. product_line[key] = False
  88. product_line['inventory_id'] = inventory.id
  89. product_line['theoretical_qty'] = product_line['product_qty']
  90. if product_line['product_id']:
  91. product = product_obj.browse(product_line['product_id'])
  92. product_line['product_uom_id'] = product.uom_id.id
  93. vals.append(product_line)
  94. else:
  95. vals = super(StockInventory, self)._get_inventory_lines(inventory)
  96. return vals