product_product.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # coding: utf-8
  2. # Copyright (C) 2014 - Today: GRAP (http://www.grap.coop)
  3. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp import _, api, fields, models
  6. from openerp.exceptions import ValidationError
  7. from openerp.exceptions import Warning as UserError
  8. class ProductProduct(models.Model):
  9. _inherit = 'product.product'
  10. # Column Section
  11. ean13 = fields.Char(copy=False)
  12. ean_duplicates_exist = fields.Boolean(
  13. compute='_compute_ean_duplicates',
  14. string='Has EAN Duplicates', multi='ean_duplicates',
  15. search='_search_ean_duplicates_exist')
  16. ean_duplicates_qty = fields.Integer(
  17. compute='_compute_ean_duplicates',
  18. string='EAN Duplicates Quantity', multi='ean_duplicates')
  19. # Compute Section
  20. @api.multi
  21. def _compute_ean_duplicates(self):
  22. res = self._get_ean_duplicates()
  23. for product in self:
  24. if product.id in res:
  25. product.ean_duplicates_qty = res[product.id]
  26. product.ean_duplicates_exist = True
  27. # Search Section
  28. def _search_ean_duplicates_exist(self, operator, operand):
  29. products = self.search([])
  30. res = products._get_ean_duplicates()
  31. if operator == '=' and operand is True:
  32. product_ids = res.keys()
  33. elif operator == '=' and operand is False:
  34. product_ids = list(set(products.ids) - set(res.keys()))
  35. else:
  36. raise UserError(_(
  37. "Operator '%s' not implemented.") % (operator))
  38. return [('id', 'in', product_ids)]
  39. # Constrains Section
  40. @api.constrains('company_id', 'ean13')
  41. def _check_ean13_company(self):
  42. for product in self.filtered(lambda x: x.ean13):
  43. duplicates = self.with_context(active_test=True).search([
  44. ('company_id', '=', product.company_id.id),
  45. ('ean13', '=', product.ean13),
  46. ('id', '!=', product.id)])
  47. if duplicates:
  48. raise ValidationError(_(
  49. "No puedes establecer el codigo ean13 '%s' para el producto %s"
  50. " debido a que hay otro producto con el mismo codido"
  51. " ean13 :\n - %s") % (
  52. product.ean13, product.name,
  53. '- %s\n'.join([x.name for x in duplicates])))
  54. # Private Section
  55. @api.multi
  56. def _get_ean_duplicates(self):
  57. self._cr.execute("""
  58. SELECT
  59. pp1.id,
  60. count(*) as qty
  61. FROM product_product pp1
  62. INNER JOIN product_template pt1
  63. ON pt1.id = pp1.product_tmpl_id
  64. INNER JOIN product_product pp2
  65. ON pp1.ean13 = pp2.ean13
  66. AND pp1.id != pp2.id
  67. AND pp2.active = True
  68. INNER JOIN product_template pt2
  69. ON pt2.id = pp2.product_tmpl_id
  70. AND pt1.company_id = pt2.company_id
  71. WHERE
  72. pp1.ean13 IS NOT NULL
  73. AND pp1.ean13 != ''
  74. AND pp1.id in %s
  75. GROUP BY pp1.id
  76. ORDER BY pp1.id""", (tuple(self.ids),))
  77. return {x[0]: x[1] for x in self._cr.fetchall()}