Add Product ean13 barcode in Quotation line item search (Odoo 8)

376 Views Asked by At

I want to know how to proceed for adding in the search a product by barcode (ean 13) in sale quotations. Like the image here, I have only the name of the product and product internal reference.

screenshot

I try to override the model product.product like this :

# -*- coding: utf-8 -*-

from openerp import models, api

class product_product(models.model):

_inherit = "product.product"

def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):

res = super(product_product, self).name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100)

if operator in ('ilike', 'like', '=', '=like', '=ilike'):

domain = [('ean13', operator, name)]

ids = self.search(cr, user, domain, limit=limit, context=context)

res += self.name_get(cr, user, ids, context=context)

return res

self.search([('ean13', 'ilike', name)])
2

There are 2 best solutions below

1
On

The name_get method changes the default name that appears on the drop_down list.

Override the name_search method instead, something like this:

@api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
    # Make a search with default criteria
    temp = super(models.Model, self).name_search(
        name=name, args=args, operator=operator, limit=limit)
    # Make the other search
    temp += super(ProductProduct, self).name_search(
        name=name, args=args, operator=operator, limit=limit)
    # Merge both results
    res = []
    keys = []
    for val in temp:
        if val[0] not in keys:
            res.append(val)
            keys.append(val[0])
            if len(res) >= limit:
                break
    return res

You just need to add the results of the ean13 as well to the method:

self.search([('ean13', 'ilike', name)])
0
On

you can also write this way

def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):

    res = super(product_product, self).name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100)

    if operator in ('ilike', 'like', '=', '=like', '=ilike'):

        domain = [('ean13', operator, name)]

        ids = self.search(cr, user, domain, limit=limit, context=context)
        res+=self.browse(ids).name_get()
        return res