# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.osv import osv,fields
from openerp.tools.translate import _
import re

class product_template(osv.osv):
    _inherit = "product.template"
    
    def create(self, cr, uid, vals, context=None):
        product_template_id = super(product_template, self).create(cr, uid, vals, context=context)

        related_vals = {}       
        if vals.get('part_number'):
            related_vals['part_number'] = vals['part_number']
        if vals.get('manufacturer'):
            related_vals['manufacturer'] = vals['manufacturer']
        if related_vals:
             self.write(cr, uid, product_template_id, related_vals, context=context)

        return product_template_id

    
    _columns = {
        'part_number': fields.related('product_variant_ids', 'part_number', type='char', string='Part Number'),
        'manufacturer':fields.related('product_variant_ids', 'manufacturer', relation='res.partner', type='many2one', string='Manufacturer')
        }

class product_product(osv.osv):
    _inherit = "product.product"
    
    def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
        res = {}
        new_args = []
        cont=0

        if not args:
            args = []
        number_params = len(args)

        while cont<number_params:
            param=args[cont]
            if ('part_number' in param) or ('default_code' in param) or ('name' in param):
                search_vals = param[2].split()
                for val in search_vals:
                    new_args = new_args + [[param[0],'ilike', val]]
            cont = cont +1
        if len(new_args) > 0:
            args = new_args
        res = super(product_product, self).search(cr, uid, args, offset, limit, order, context, count)
        return res
        
    _columns = {
        'part_number': fields.char('Part Number', size=90),
        'manufacturer':fields.many2one('res.partner',string="Manufacturer")
    }
    _sql_constraints = [
        ('part_manufacturer_unique','UNIQUE(part_number,manufacturer)','The number part already exist associated to this manufacturer')
        
    ]
    def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
        if not args:
            args = []
        if name:
            ids = self.search(cr, user, [['default_code','ilike',name]]+ args, limit=limit, context=context)
            if not ids:
                ids = self.search(cr, user, [['part_number','ilike',name]]+ args, limit=limit, context=context)
            if not ids:
                ids = set()
                ids.update(self.search(cr, user, args + [['default_code',operator,name]], limit=limit, context=context))
                if len(ids) < limit:
                    ids.update(self.search(cr, user, args + [['name',operator,name]], limit=(limit-len(ids)), context=context))
                ids = list(ids)
            if not ids:
                ptrn = re.compile('(\[(.*?)\])')
                res = ptrn.search(name)
                if res:
                    ids = self.search(cr, user, [('default_code','=', res.group(2))] + args, limit=limit, context=context)
        else:
            ids = self.search(cr, user, args, limit=limit, context=context)
        result = self.name_get(cr, user, ids, context=context)
        return result

product_product()

class PurchaseOrderLine(osv.osv):
    _inherit = 'purchase.order.line'
    
    def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
            partner_id, date_order, fiscal_position_id, date_planned,
            name, price_unit, state, context=None):
        user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
        product=self.pool.get('product.product').browse(cr,uid,product_id)
        result = super(PurchaseOrderLine, self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id,
            partner_id, date_order, fiscal_position_id, date_planned, name, price_unit, state, context)
        product=self.pool.get('product.product').browse(cr,uid,product_id)
        
        if user.company_id.part_number_purchase_order==True:
            if product.part_number:
                result['value']['name']='['+product.part_number+'] '+ product.name
        return result
@@ -2017,764 +2017,8 @@
    %0A
-    def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):%0A        res = %7B%7D%0A        new_args = %5B%5D%0A        cont=0%0A%0A        if not args:%0A            args = %5B%5D%0A        number_params = len(args)%0A%0A        while cont%3Cnumber_params:%0A            param=args%5Bcont%5D%0A            if ('part_number' in param) or ('default_code' in param) or ('name' in param):%0A                search_vals = param%5B2%5D.split()%0A                for val in search_vals:%0A                    new_args = new_args + %5B%5Bparam%5B0%5D,'ilike', val%5D%5D%0A            cont = cont +1%0A        if len(new_args) %3E 0:%0A            args = new_args%0A        res = super(product_product, self).search(cr, uid, args, offset, limit, order, context, count)%0A        return res%0A        %0A
     

Delete search