You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
2.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2014 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. import barcode
  6. from openerp.osv import fields
  7. from openerp.osv.orm import AbstractModel
  8. class barcode_generate_mixin(AbstractModel):
  9. _name = 'barcode.generate.mixin'
  10. _columns = {
  11. 'barcode_rule_id': fields.many2one(
  12. 'barcode.rule', 'Barcode Rule'),
  13. 'barcode_base': fields.integer('Barcode Base'),
  14. }
  15. def generate_barcode(self, cr, uid, ids, context=None):
  16. vals = self._compute_custom_barcode(cr, uid, ids, context=context)
  17. for id in vals.keys():
  18. self.write(cr, uid, id, {'ean13': vals[id]}, context=context)
  19. return True
  20. def _compute_custom_barcode(self, cr, uid, ids, context=None):
  21. res = {}
  22. for item in self.browse(cr, uid, ids, context=context):
  23. if item.barcode_rule_id and item.barcode_base:
  24. pass
  25. barcode_class = barcode.get_barcode_class(
  26. item.barcode_rule_id.encoding)
  27. padding = item.barcode_rule_id.pattern.count('.')
  28. full_base = str(item.barcode_base).rjust(padding, '0')
  29. # Define barcode
  30. custom_code = item.barcode_rule_id.pattern
  31. custom_code = custom_code.replace('{', '').replace('}', '')
  32. custom_code = custom_code.replace(
  33. 'D', self._get_barcode_replacement_char(
  34. cr, uid, 'D', context=context))
  35. custom_code = custom_code.replace(
  36. 'N', self._get_barcode_replacement_char(
  37. cr, uid, 'N', context=context))
  38. custom_code = custom_code.replace('.' * padding, full_base)
  39. res[item.id] = barcode_class(custom_code)
  40. return res
  41. def _get_barcode_replacement_char(self, cr, uid, char, context=None):
  42. """
  43. Can be overload by inheritance
  44. Define wich character will be used instead of the 'N' or the 'D'
  45. char, present in the pattern of the barcode_rule_id
  46. """
  47. return '0'