Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to generate the barcode for product in openerp (Odoo)?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.35k
    Comment on it

    You will need the following kind of python code declaration in your .py file:

     class product_category(orm.Model):
        _inherit = 'product.category'
    
        _columns = {
            'ean_sequence_id': fields.many2one('ir.sequence', 'Ean Sequence'),
        }
    
    class product_product(orm.Model):
        _inherit = 'product.product'
    
        _columns = {
            'ean_sequence_id': fields.many2one('ir.sequence', 'Ean Sequence'),
        }
    
        def _get_ean_next_code(self, cr, uid, product, context=None):
            if context is None: context = {}
            sequence_obj = self.pool.get('ir.sequence')
            ean = ''
            if product.ean_sequence_id:
                ean = sequence_obj.next_by_id(cr, uid, product.ean_sequence_id.id, context=context)
            elif product.categ_id.ean_sequence_id:
                ean = sequence_obj.next_by_id(cr, uid, product.categ_id.ean_sequence_id.id, context=context)
            elif product.company_id and product.company_id.ean_sequence_id:
                ean = sequence_obj.next_by_id(cr, uid, product.company_id.ean_sequence_id.id, context=context)
            else:
                ean = sequence_obj.next_by_code(cr, uid, 'product.ean13.code', context=context)
            if len(ean) > 12:
                raise orm.except_orm(_("Configuration Error!"),
                     _("There next sequence is upper than 12 characters. This can't work."
                       "You will have to redefine the sequence or create a new one"))
            else:
                ean = (len(ean[0:6]) == 6 and ean[0:6] or ean[0:6].ljust(6,'0')) + ean[6:].rjust(6,'0')
            return ean
    
        def _get_ean_key(self, code):
            sum = 0
            for i in range(12):
                if isodd(i):
                    sum += 3 * int(code[i])
                else:
                    sum += int(code[i])
            key = (10 - sum % 10) % 10
            return str(key)
    
        def _generate_ean13_value(self, cr, uid, product, context=None):
            ean13 = False
            if context is None: context = {}
            ean = self._get_ean_next_code(cr, uid, product, context=context)
            if len(ean) != 12:
                raise orm.except_orm(_("Configuration Error!"),
                     _("This sequence is different than 12 characters. This can't work."
                       "You will have to redefine the sequence or create a new one"))
            key = self._get_ean_key(ean)
            ean13 = ean + key
            return ean13
    
        def generate_ean13(self, cr, uid, ids, context=None):
            if context is None: context = {}
            product_ids = self.browse(cr, uid, ids, context=context)
            for product in product_ids:
                if product.ean13:
                    continue
                ean13 = self._generate_ean13_value(cr, uid, product, context=context)
                self.write(cr, uid, [product.id], {
                            'ean13': ean13
                        }, context=context)
            return True
    
        def create(self, cr, uid, vals, context=None):
            if context is None: context = {}
            id = super(product_product, self).create(cr, uid, vals, context=context)
            if not vals.get('ean13'):
                ean13 = self.generate_ean13(cr, uid, [id], context=context)
            return id
    
        def copy(self, cr, uid, id, default=None, context=None):
            if default is None: default = {}
            if context is None: context = {}
            default['ean13'] = False
            return super(product_product, self).copy(cr, uid, id, default=default, context=context)
    

    You can start printing barcode from any row and column on PDF.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: