product.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # -*- coding: utf-8 -*-
  2. #################################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2013 Julius Network Solutions SARL <contact@julius.fr>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. #################################################################################
  21. from openerp.osv import fields, orm
  22. from openerp.tools.translate import _
  23. def isodd(x):
  24. return bool(x % 2)
  25. # class product_template(orm.Model):
  26. # _inherit = 'product.template'
  27. #
  28. # _columns = {
  29. # 'variants': fields.one2many('product.product', 'product_tmpl_id', 'Variants'),
  30. # }
  31. #
  32. # def generate_ean13(self, cr, uid, ids, context=None):
  33. # if context is None: context = {}
  34. # product_obj = self.pool.get('product.product')
  35. # templates = self.browse(cr, uid, ids, context=context)
  36. # for template in templates:
  37. # variant_ids = [x.id for x in template.variant_ids]
  38. # product_obj.generate_ean13(cr, uid, variant_ids, context=context)
  39. # return True
  40. class product_category(orm.Model):
  41. _inherit = 'product.category'
  42. _columns = {
  43. 'ean_sequence_id': fields.many2one('ir.sequence', 'Ean Sequence'),
  44. }
  45. class product_product(orm.Model):
  46. _inherit = 'product.product'
  47. _columns = {
  48. 'ean_sequence_id': fields.many2one('ir.sequence', 'Ean Sequence'),
  49. }
  50. def _get_ean_next_code(self, cr, uid, product, context=None):
  51. if context is None:
  52. context = {}
  53. sequence_obj = self.pool.get('ir.sequence')
  54. ean = ''
  55. if product.ean_sequence_id:
  56. ean = sequence_obj.next_by_id(cr, uid, product.ean_sequence_id.id, context=context)
  57. elif product.categ_id.ean_sequence_id:
  58. ean = sequence_obj.next_by_id(cr, uid, product.categ_id.ean_sequence_id.id, context=context)
  59. elif product.company_id and product.company_id.ean_sequence_id:
  60. ean = sequence_obj.next_by_id(cr, uid, product.company_id.ean_sequence_id.id, context=context)
  61. elif context.get('sequence_id'):
  62. ean = sequence_obj.next_by_id(cr, uid, context.get('sequence_id'), context=context)
  63. else:
  64. return None
  65. if len(ean) > 12:
  66. raise orm.except_orm(_("Configuration Error!"),
  67. _("There next sequence is upper than 12 characters. This can't work."
  68. "You will have to redefine the sequence or create a new one"))
  69. else:
  70. ean = (len(ean[0:6]) == 6 and ean[0:6] or ean[0:6].ljust(6, '0')) + ean[6:].rjust(6, '0')
  71. return ean
  72. def _get_ean_key(self, code):
  73. sum = 0
  74. for i in range(12):
  75. if isodd(i):
  76. sum += 3 * int(code[i])
  77. else:
  78. sum += int(code[i])
  79. key = (10 - sum % 10) % 10
  80. return str(key)
  81. def _generate_ean13_value(self, cr, uid, product, context=None):
  82. ean13 = False
  83. if context is None:
  84. context = {}
  85. ean = self._get_ean_next_code(cr, uid, product, context=context)
  86. if not ean:
  87. return None
  88. key = self._get_ean_key(ean)
  89. ean13 = ean + key
  90. return ean13
  91. def generate_ean13(self, cr, uid, ids, context=None):
  92. if context is None:
  93. context = {}
  94. product_ids = self.browse(cr, uid, ids, context=context)
  95. for product in product_ids:
  96. if product.ean13:
  97. continue
  98. ean13 = self._generate_ean13_value(cr, uid, product, context=context)
  99. if not ean13:
  100. continue
  101. self.write(cr, uid, [product.id], {
  102. 'ean13': ean13
  103. }, context=context)
  104. return True
  105. def copy(self, cr, uid, id, default=None, context=None):
  106. if default is None:
  107. default = {}
  108. if context is None:
  109. context = {}
  110. default['ean13'] = False
  111. return super(product_product, self).copy(cr, uid, id, default=default, context=context)
  112. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: