Skip to Content
Menu
This question has been flagged
1 Reply
7855 Views

i'm using  odoo 9 and i have created a custom module for product barcode generation. Everytime when i press the button 'generate the ean13 ' this error shows and i don't know what to do .

Any help please this is my code 

_______________________________________________________________________________________________

product_view.xml 

<?xml version="1.0" encoding="utf-8"?>

<odoo>


        <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.template.product.form.barcode</field>

            <field name="model">product.template</field>

            <field name="inherit_id" ref="product.product_template_only_form_view" />

            <field name="arch" type="xml">

              

                <field name="barcode" position="after">

                  

                    <button string="Generate the EAN13" name="generate_ean13"

                        attrs="{'invisible': [('barcode', '!=', False)]}"

                        type="object" colspan="2" />

                </field>

            </field>

        </record>

</odoo>

__________________________________________________________________________________________

product.py 

<?xml version="1.0" encoding="utf-8"?>

<odoo>


        <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.template.product.form.barcode</field>

            <field name="model">product.template</field>

            <field name="inherit_id" ref="product.product_template_only_form_view" />

            <field name="arch" type="xml">

              

                <field name="barcode" position="after">

                  

                    <button string="Generate the EAN13" name="generate_ean13"

                        attrs="{'invisible': [('barcode', '!=', False)]}"

                        type="object" colspan="2" />

                </field>

            </field>

        </record>

</odoo>



Avatar
Discard
Best Answer

Hi,

Going through the given code along with the question, it seems you have added a new button to the product.template model.


You have to add the function generate_ean13 to the same model.  Right now as there is no such a function in the same model you are getting this error when you click the newly added button.

class ProductTemplate(models.Model):
_inherit = "product.template"

@api.multi
def generate_ean13(self):
# code here


Also, you can have a look at the Product Barcode Generator module available in the v10 : https://www.odoo.com/apps/modules/10.0/product_barcode/


Thanks

Avatar
Discard
Author

199/5000

I did not notice that it pasted the product_view.xml code into product.py. my problem that I already wrote the function generate_ean13 but he did not take it into consideration here is my code

from openerp import api, models, fields, _

from openerp import exceptions

def isodd(x):

return bool(x % 2)

class ProductCategory(models.Model):

_inherit = 'product.category'

ean_sequence_id = fields.Many2one('ir.sequence', string='Ean sequence')

class ProductTemplate(models.Model):

_inherit = 'product.template'

barcode = fields.Char(copy=False)

ean= fields.Char(copy=False)

_columns = {

ean_sequence_id : fields.Many2one('ir.sequence', string='Ean sequence')

}

@api.model

def _get_ean_next_code(self, product):

sequence_obj = self.env['ir.sequence']

if product.ean_sequence_id:

ean = sequence_obj.next_by_id(product.ean_sequence_id.id)

elif product.categ_id.ean_sequence_id:

ean = sequence_obj.next_by_id(product.categ_id.ean_sequence_id.id)

elif product.company_id and product.company_id.ean_sequence_id:

ean = sequence_obj.next_by_id(

product.company_id.ean_sequence_id.id)

elif self.env.context.get('sequence_id', False):

ean = sequence_obj.next_by_id(self.env.context.get('sequence_id'))

else:

return None

ean = (len(ean[0:6]) == 6 and ean[0:6] or

ean[0:6].ljust(6, '0')) + ean[6:].rjust(6, '0')

if len(ean) > 12:

raise exceptions.Warning(

_("Configuration Error!"

"The next sequence is longer than 12 characters. "

"It is not valid for an EAN13 needing 12 characters, "

"the 13 being used as a control digit"

"You will have to redefine the sequence or create a new one")

)

return ean

def _get_ean_control_digit(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 '%d' % key

@api.model

def _generate_ean13_value(self, product):

ean = self._get_ean_next_code(product)

if not ean:

return None

key = self._get_ean_control_digit(ean)

barcode = ean + key

return barcode

@api.one

def generate_ean13(self):

if not self.barcode:

barcode = self._generate_ean13_value(self)

if barcode:

self.write({'barcode': barcode})

return True

Then check whether this py file is called in the init file

Author

he is called in the init file "from . import product" that's why i'm i'm desprerate because i have tried every thing but i still don't know why this error shows up

Related Posts Replies Views Activity
2
Jul 22
4923
2
Jul 21
5444
0
Jul 18
2270
1
Jul 17
9625
1
Jul 17
2310