If you want to generate the barcode for product in openERP .Then you will need the following kind of python code declaration in your .py file and run in Odoo server.
from openerp.osv import fields, orm
from openerp.tools.translate import _
def isodd(x):
return bool(x % 2)
#class product_template(orm.Model):
# _inherit = 'product.template'
#
# _columns = {
# 'variants': fields.one2many('product.product', 'product_tmpl_id', 'Variants'),
# }
#
# def generate_ean13(self, cr, uid, ids, context=None):
# if context is None: context = {}
# product_obj = self.pool.get('product.product')
# templates = self.browse(cr, uid, ids, context=context)
# for template in templates:
# variant_ids = [x.id for x in template.variant_ids]
# product_obj.generate_ean13(cr, uid, variant_ids, context=context)
# return True
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)
After that we have to go in .XML file :
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="view_product_category_simple_ean_form" model="ir.ui.view">
<field name="name">product.category.form</field>
<field name="model">product.category</field>
<field name="inherit_id" ref="product.product_category_form_view"/>
<field name="arch" type="xml">
<field name="type" position="after">
<field name="ean_sequence_id" domain="[('barcode_sequence', '=', 1)]"
context="{'default_name': name and 'EAN ' + name or 'EAN', 'default_barcode_sequence': 1}"/>
<newline/>
</field>
</field>
</record>
<record id="view_product_simple_ean_form" model="ir.ui.view">
<field name="name">product.product.form</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view"/>
<field name="arch" type="xml">
<field name="ean13" position="after">
<field name="ean_sequence_id" domain="[('barcode_sequence', '=', 1)]"
context="{'default_name': name and 'EAN ' + name or 'EAN', 'default_barcode_sequence': 1}"
class="oe_edit_only"/>
<button
string="Generate the EAN13"
name="generate_ean13"
attrs="{'invisible': [('ean13', '!=', False)]}"
type="object" colspan="2"/>
</field>
</field>
</record>
</data>
</openerp>
Note-> You can start printing bar-code from any row and column on PDF in reporting tools.
0 Comment(s)