[IMP]improved test case in compute_price_margin.
[odoo/odoo.git] / addons / product / product.py
index 259afd6..63e914c 100644 (file)
@@ -23,7 +23,10 @@ import math
 import re
 
 from _common import rounding
+from lxml import etree
 
+from openerp.osv.orm import setup_modifiers
+from openerp import SUPERUSER_ID
 from openerp import tools
 from openerp.osv import osv, fields
 from openerp.tools.translate import _
@@ -135,7 +138,7 @@ class product_uom(osv.osv):
                     '1 * (reference unit) = ratio * (this unit)'),
         'factor_inv': fields.function(_factor_inv, digits=(12,12),
             fnct_inv=_factor_inv_write,
-            string='Ratio',
+            string='Bigger Ratio',
             help='How many times this Unit of Measure is bigger than the reference Unit of Measure in this category:\n'\
                     '1 * (this unit) = ratio * (reference unit)', required=True),
         'rounding': fields.float('Rounding Precision', digits_compute=dp.get_precision('Product Unit of Measure'), required=True,
@@ -281,12 +284,87 @@ class product_category(osv.osv):
         return [ids]
 
 
+class product_public_category(osv.osv):
+    _name = "product.public.category"
+    _description = "Public Category"
+    _order = "sequence, name"
+    def _check_recursion(self, cr, uid, ids, context=None):
+        level = 100
+        while len(ids):
+            cr.execute('select distinct parent_id from product_public_category where id IN %s',(tuple(ids),))
+            ids = filter(None, map(lambda x:x[0], cr.fetchall()))
+            if not level:
+                return False
+            level -= 1
+        return True
+
+    _constraints = [
+        (_check_recursion, 'Error ! You cannot create recursive categories.', ['parent_id'])
+    ]
+
+    def name_get(self, cr, uid, ids, context=None):
+        if not len(ids):
+            return []
+        reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
+        res = []
+        for record in reads:
+            name = record['name']
+            if record['parent_id']:
+                name = record['parent_id'][1]+' / '+name
+            res.append((record['id'], name))
+        return res
+
+    def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
+        res = self.name_get(cr, uid, ids, context=context)
+        return dict(res)
+
+    def _get_image(self, cr, uid, ids, name, args, context=None):
+        result = dict.fromkeys(ids, False)
+        for obj in self.browse(cr, uid, ids, context=context):
+            result[obj.id] = tools.image_get_resized_images(obj.image)
+        return result
+    
+    def _set_image(self, cr, uid, id, name, value, args, context=None):
+        return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
+
+    _columns = {
+        'name': fields.char('Name', size=64, required=True, translate=True),
+        'complete_name': fields.function(_name_get_fnc, type="char", string='Name'),
+        'parent_id': fields.many2one('product.public.category','Parent Category', select=True),
+        'child_id': fields.one2many('product.public.category', 'parent_id', string='Children Categories'),
+        'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of product categories."),
+        
+        # NOTE: there is no 'default image', because by default we don't show thumbnails for categories. However if we have a thumbnail
+        # for at least one category, then we display a default image on the other, so that the buttons have consistent styling.
+        # In this case, the default image is set by the js code.
+        # NOTE2: image: all image fields are base64 encoded and PIL-supported
+        'image': fields.binary("Image",
+            help="This field holds the image used as image for the cateogry, limited to 1024x1024px."),
+        'image_medium': fields.function(_get_image, fnct_inv=_set_image,
+            string="Medium-sized image", type="binary", multi="_get_image",
+            store={
+                'product.public.category': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
+            },
+            help="Medium-sized image of the category. It is automatically "\
+                 "resized as a 128x128px image, with aspect ratio preserved. "\
+                 "Use this field in form views or some kanban views."),
+        'image_small': fields.function(_get_image, fnct_inv=_set_image,
+            string="Smal-sized image", type="binary", multi="_get_image",
+            store={
+                'product.public.category': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
+            },
+            help="Small-sized image of the category. It is automatically "\
+                 "resized as a 64x64px image, with aspect ratio preserved. "\
+                 "Use this field anywhere a small image is required."),
+    }
+
 
 #----------------------------------------------------------
 # Products
 #----------------------------------------------------------
 class product_template(osv.osv):
     _name = "product.template"
+    _inherit = ['mail.thread']
     _description = "Product Template"
 
     def _get_image(self, cr, uid, ids, name, args, context=None):
@@ -313,8 +391,9 @@ class product_template(osv.osv):
         'produce_delay': fields.float('Manufacturing Lead Time', help="Average delay in days to produce this product. In the case of multi-level BOM, the manufacturing lead times of the components will be added."),
         'rental': fields.boolean('Can be Rent'),
         'categ_id': fields.many2one('product.category','Category', required=True, change_default=True, domain="[('type','=','normal')]" ,help="Select category for the current product"),
+        'public_categ_id': fields.many2one('product.public.category','Public Category', help="Those categories are used to group similar products for public sales (eg.: point of sale, e-commerce)."),
         'list_price': fields.float('Sale Price', digits_compute=dp.get_precision('Product Price'), help="Base price to compute the customer price. Sometimes called the catalog price."),
-        'standard_price': fields.float('Cost', digits_compute=dp.get_precision('Product Price'), help="Cost price of the product used for standard stock valuation in accounting and used as a base price on purchase orders.", groups="base.group_user"),
+        'standard_price': fields.float('Cost Price', digits_compute=dp.get_precision('Product Price'), help="Cost price of the product template used for standard stock valuation in accounting and used as a base price on purchase orders.", groups="base.group_user"),
         'volume': fields.float('Volume', help="The volume in m3."),
         'weight': fields.float('Gross Weight', digits_compute=dp.get_precision('Stock Weight'), help="The gross weight in Kg."),
         'weight_net': fields.float('Net Weight', digits_compute=dp.get_precision('Stock Weight'), help="The net weight in Kg."),
@@ -335,7 +414,7 @@ class product_template(osv.osv):
             help='Coefficient to convert default Unit of Measure to Unit of Sale\n'
             ' uos = uom * coeff'),
         'mes_type': fields.selection((('fixed', 'Fixed'), ('variable', 'Variable')), 'Measure Type'),
-        'seller_ids': fields.one2many('product.supplierinfo', 'product_id', 'Supplier'),
+        'seller_ids': fields.one2many('product.supplierinfo', 'product_tmpl_id', 'Supplier'),
         'company_id': fields.many2one('res.company', 'Company', select=1),
         # image: all image fields are base64 encoded and PIL-supported
         'image': fields.binary("Image",
@@ -343,7 +422,7 @@ class product_template(osv.osv):
         'image_medium': fields.function(_get_image, fnct_inv=_set_image,
             string="Medium-sized image", type="binary", multi="_get_image",
             store={
-                'product.product': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
+                'product.template': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
             },
             help="Medium-sized image of the product. It is automatically "\
                  "resized as a 128x128px image, with aspect ratio preserved, "\
@@ -351,12 +430,12 @@ class product_template(osv.osv):
         'image_small': fields.function(_get_image, fnct_inv=_set_image,
             string="Small-sized image", type="binary", multi="_get_image",
             store={
-                'product.product': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
+                'product.template': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
             },
             help="Small-sized image of the product. It is automatically "\
                  "resized as a 64x64px image, with aspect ratio preserved. "\
                  "Use this field anywhere a small image is required."),
-        'product_variant_ids': fields.one2many('product.product', 'product_tmpl_id', 'Product Variants'),
+        'product_variant_ids': fields.one2many('product.product', 'product_tmpl_id', 'Product Variants', required=True),
     }
 
     def _get_uom_id(self, cr, uid, *args):
@@ -391,6 +470,15 @@ class product_template(osv.osv):
                     raise osv.except_osv(_('Unit of Measure categories Mismatch!'), _("New Unit of Measure '%s' must belong to same Unit of Measure category '%s' as of old Unit of Measure '%s'. If you need to change the unit of measure, you may deactivate this product from the 'Procurements' tab and create a new one.") % (new_uom.name, old_uom.category_id.name, old_uom.name,))
         return super(product_template, self).write(cr, uid, ids, vals, context=context)
 
+    def copy(self, cr, uid, id, default=None, context=None):
+        if default is None:
+            default = {}
+        #TOFIX: it should be pass default={'name': _("%s (copy)") % (template['name'])}.
+        template = self.read(cr, uid, id, ['name'], context=context)
+        res = super(product_template, self).copy(cr, uid, id, default=default, context=context)
+        self.write(cr, uid, res, {'name': _("%s (copy)") % (template['name'])}, context=context)
+        return res
+
     _defaults = {
         'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'product.template', context=c),
         'list_price': 1,
@@ -442,6 +530,7 @@ class product_product(osv.osv):
         return res
 
     def _product_price(self, cr, uid, ids, name, arg, context=None):
+        plobj = self.pool.get('product.pricelist')
         res = {}
         if context is None:
             context = {}
@@ -451,15 +540,16 @@ class product_product(osv.osv):
         if pricelist:
             # Support context pricelists specified as display_name or ID for compatibility
             if isinstance(pricelist, basestring):
-                pricelist_ids = self.pool.get('product.pricelist').name_search(
+                pricelist_ids = plobj.name_search(
                     cr, uid, pricelist, operator='=', context=context, limit=1)
                 pricelist = pricelist_ids[0][0] if pricelist_ids else pricelist
+
+            products = self.browse(cr, uid, ids, context=context)
+            qtys = map(lambda x: (x, quantity, partner), products)
+            pl = plobj.browse(cr, uid, pricelist, context=context)
+            price = plobj._price_get_multi(cr,uid, pl, qtys, context=context)
             for id in ids:
-                try:
-                    price = self.pool.get('product.pricelist').price_get(cr,uid,[pricelist], id, quantity, partner=partner, context=context)[pricelist]
-                except:
-                    price = 0.0
-                res[id] = price
+                res[id] = price.get(id, 0.0)
         for id in ids:
             res.setdefault(id, 0.0)
         return res
@@ -486,9 +576,22 @@ class product_product(osv.osv):
                         uom.id, product.list_price, context['uom'])
             else:
                 res[product.id] = product.list_price
-            res[product.id] =  (res[product.id] or 0.0) * (product.price_margin or 1.0) + product.price_extra
+            res[product.id] =  (res[product.id] + ((res[product.id] * (product.price_margin)) / 100)) + product.price_extra
         return res
 
+    def _save_product_lst_price(self, cr, uid, product_id, field_name, field_value, arg, context=None):
+        field_value = field_value or 0.0
+        product = self.browse(cr, uid, product_id, context=context)
+        price = product.list_price
+        if product.price_margin:
+            price = (product.list_price + (product.list_price * (product.price_margin / 100)))
+        price = (field_value - price)
+        price_field = 'price_extra'
+        if product.standard_variants:
+            price_field = 'list_price'
+            price = field_value - product.price_extra
+        return product.write({price_field: price}, context=context)
+
     def _get_partner_code_name(self, cr, uid, ids, product, partner_id, context=None):
         for supinfo in product.seller_ids:
             if supinfo.name.id == partner_id:
@@ -548,6 +651,16 @@ class product_product(osv.osv):
         return result
 
 
+    def _get_image(self, cr, uid, ids, name, args, context=None):
+        result = dict.fromkeys(ids, False)
+        for obj in self.browse(cr, uid, ids, context=context):
+            img = obj.image or obj.product_tmpl_id.image or False
+            result[obj.id] = tools.image_get_resized_images(img, avoid_resize_medium=True)
+        return result
+
+    def _set_image(self, cr, uid, id, name, value, args, context=None):
+        return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
+
     def _get_name_template_ids(self, cr, uid, ids, context=None):
         result = set()
         template_ids = self.pool.get('product.product').search(cr, uid, [('product_tmpl_id', 'in', ids)])
@@ -555,10 +668,17 @@ class product_product(osv.osv):
             result.add(el)
         return list(result)
 
+    def _check_variants(self, cr, uid, ids, fields, args, context=None):
+        res = dict.fromkeys(ids, False)
+        for product in self.browse(cr, uid, ids, context=context):
+            no_varaints = [x for x in product.product_tmpl_id.product_variant_ids if x.variants == False]
+            res[product.id] = len(no_varaints) and True or False
+        return res
+    
     _defaults = {
         'active': lambda *a: 1,
         'price_extra': lambda *a: 0.0,
-        'price_margin': lambda *a: 1.0,
+        'price_margin': lambda *a: 0.0,
         'color': 0,
     }
 
@@ -573,18 +693,18 @@ class product_product(osv.osv):
         'virtual_available': fields.function(_product_virtual_available, type='float', string='Quantity Available'),
         'incoming_qty': fields.function(_product_incoming_qty, type='float', string='Incoming'),
         'outgoing_qty': fields.function(_product_outgoing_qty, type='float', string='Outgoing'),
-        'price': fields.function(_product_price, type='float', string='Price', digits_compute=dp.get_precision('Product Price')),
-        'lst_price' : fields.function(_product_lst_price, type='float', string='Public Price', digits_compute=dp.get_precision('Product Price')),
+        'price': fields.function(_product_price, fnct_inv=_save_product_lst_price, type='float', string='Price', digits_compute=dp.get_precision('Product Price')),
+        'lst_price' : fields.function(_product_lst_price, fnct_inv=_save_product_lst_price, type='float', string='Public Price', digits_compute=dp.get_precision('Product Price')),
         'code': fields.function(_product_code, type='char', string='Internal Reference'),
         'partner_ref' : fields.function(_product_partner_ref, type='char', string='Customer ref'),
         'default_code' : fields.char('Internal Reference', size=64, select=True),
         'active': fields.boolean('Active', help="If unchecked, it will allow you to hide the product without removing it."),
-        'variants': fields.char('Variants', size=64),
+        'variants': fields.char('Variants', size=64, translate=True),
         'product_tmpl_id': fields.many2one('product.template', 'Product Template', required=True, ondelete="cascade", select=True),
         'ean13': fields.char('EAN13 Barcode', size=13, help="International Article Number used for product identification."),
         'packaging' : fields.one2many('product.packaging', 'product_id', 'Logistical Units', help="Gives the different ways to package the same product. This has no impact on the picking order and is mainly used if you use the EDI module."),
-        'price_extra': fields.float('Variant Price Extra', digits_compute=dp.get_precision('Product Price')),
-        'price_margin': fields.float('Variant Price Margin', digits_compute=dp.get_precision('Product Price')),
+        'price_extra': fields.float('Variant Price Extra', digits_compute=dp.get_precision('Product Price'), help="Price Extra: Extra price for the variant on sale price. eg. 200 price extra, 1000 + 200 = 1200."),
+        'price_margin': fields.float('Variant Price Margin', digits_compute=dp.get_precision('Product Price'), help="Price Margin: Margin in percentage on sale price for the variant. eg. 10% price margin, 1000 + 10% = 1100."),
         'pricelist_id': fields.dummy(string='Pricelist', relation='product.pricelist', type='many2one'),
         'name_template': fields.related('product_tmpl_id', 'name', string="Template Name", type='char', size=128, store={
             'product.template': (_get_name_template_ids, ['name'], 10),
@@ -592,25 +712,57 @@ class product_product(osv.osv):
 
             }, select=True),
         'color': fields.integer('Color Index'),
+        # image: all image fields are base64 encoded and PIL-supported
+        'image': fields.binary("Image",
+            help="This field holds the image used as image for the product, limited to 1024x1024px."),
+        'image_medium': fields.function(_get_image, fnct_inv=_set_image,
+            string="Medium-sized image", type="binary", multi="_get_image",
+            store={
+                'product.product': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
+                'product.template': (_get_name_template_ids, ['image'], 10),
+            },
+            help="Medium-sized image of the product. It is automatically "\
+                 "resized as a 128x128px image, with aspect ratio preserved, "\
+                 "only when the image exceeds one of those sizes. Use this field in form views or some kanban views."),
+        'image_small': fields.function(_get_image, fnct_inv=_set_image,
+            string="Small-sized image", type="binary", multi="_get_image",
+            store={
+                'product.product': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
+                'product.template': (_get_name_template_ids, ['image'], 10),
+            },
+            help="Small-sized image of the product. It is automatically "\
+                 "resized as a 64x64px image, with aspect ratio preserved. "\
+                 "Use this field anywhere a small image is required."),
         'seller_info_id': fields.function(_calc_seller, type='many2one', relation="product.supplierinfo", string="Supplier Info", multi="seller_info"),
         'seller_delay': fields.function(_calc_seller, type='integer', string='Supplier Lead Time', multi="seller_info", help="This is the average delay in days between the purchase order confirmation and the reception of goods for this product and for the default supplier. It is used by the scheduler to order requests based on reordering delays."),
         'seller_qty': fields.function(_calc_seller, type='float', string='Supplier Quantity', multi="seller_info", help="This is minimum quantity to purchase from Main Supplier."),
         'seller_id': fields.function(_calc_seller, type='many2one', relation="res.partner", string='Main Supplier', help="Main Supplier who has highest priority in Supplier List.", multi="seller_info"),
+        'description': fields.text('Description',translate=True,
+            help="A precise description of the Product, used only for internal information purposes."),
+        'standard_variants': fields.function(_check_variants, type='boolean', string='Standard Variants', store=False),
     }
-    def unlink(self, cr, uid, ids, context=None):
-        unlink_ids = []
-        unlink_product_tmpl_ids = []
-        for product in self.browse(cr, uid, ids, context=context):
-            tmpl_id = product.product_tmpl_id.id
-            # Check if the product is last product of this template
-            other_product_ids = self.search(cr, uid, [('product_tmpl_id', '=', tmpl_id), ('id', '!=', product.id)], context=context)
-            if not other_product_ids:
-                unlink_product_tmpl_ids.append(tmpl_id)
-            unlink_ids.append(product.id)
-        res = super(product_product, self).unlink(cr, uid, unlink_ids, context=context)
-        # delete templates after calling super, as deleting template could lead to deleting
-        # products due to ondelete='cascade'
-        self.pool.get('product.template').unlink(cr, uid, unlink_product_tmpl_ids, context=context)
+
+
+    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
+        #override of fields_view_get in order to replace the name field to product template
+        if context is None:
+            context = {}
+        res = super(product_product, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
+        #check the current user in group_product_variant
+        if view_type == 'form':
+            doc = etree.XML(res['arch'])
+            if self.pool['res.users'].has_group(cr, uid, 'product.group_product_variant'):
+                for node in doc.xpath("//field[@name='name']"):
+                    node.set('invisible', '1')
+                    node.set('required', '0')
+                    setup_modifiers(node, res['fields']['name'])
+                for node in doc.xpath("//label[@name='label_name']"):
+                    node.set('string','Product Template')
+            else:
+                for node in doc.xpath("//field[@name='product_tmpl_id']"):
+                    node.set('required', '0')
+                    setup_modifiers(node, res['fields']['name'])
+            res['arch'] = etree.tostring(doc)
         return res
 
     def onchange_uom(self, cursor, user, ids, uom_id, uom_po_id):
@@ -622,6 +774,20 @@ class product_product(osv.osv):
                 return {'value': {'uom_po_id': uom_id}}
         return False
 
+    def onchange_product_tmpl_id(self, cr, uid, ids, template_id, lst_price, price_margin, price_extra, context=None):
+        res = {}
+        if template_id:
+            template = self.pool.get('product.template').browse(cr, uid, template_id, context=context)
+            no_varaints = [x for x in template.product_variant_ids if x.variants == False]
+            if not lst_price:
+                lst_price = (template.list_price + ((template.list_price * (price_margin)) / 100)) + price_extra
+            res['value'] = {
+                'standard_variants': len(no_varaints) and True or False, 
+                'name': template.name,
+                'lst_price': lst_price,
+            }
+        return res
+
     def _check_ean_key(self, cr, uid, ids, context=None):
         for product in self.read(cr, uid, ids, ['ean13'], context=context):
             res = check_ean(product['ean13'])
@@ -651,8 +817,13 @@ class product_product(osv.osv):
 
         partner_id = context.get('partner_id', False)
 
+        # all user don't have access to seller and partner
+        # check access and use superuser
+        self.check_access_rights(cr, user, "read")
+        self.check_access_rule(cr, user, ids, "read", context=context)
+
         result = []
-        for product in self.browse(cr, user, ids, context=context):
+        for product in self.browse(cr, SUPERUSER_ID, ids, context=context):
             sellers = filter(lambda x: x.name.id == partner_id, product.seller_ids)
             if sellers:
                 for s in sellers:
@@ -686,7 +857,7 @@ class product_product(osv.osv):
                 # OR operator (and given the fact that the 'name' lookup results come from the ir.translation table
                 # Performing a quick memory merge of ids in Python will give much better performance
                 ids = set()
-                ids.update(self.search(cr, user, args + [('default_code',operator,name)], limit=limit, context=context))
+                ids.update(self.search(cr, user, args + ['|',('default_code',operator,name),('variants',operator,name)], limit=limit, context=context))
                 if not limit or len(ids) < limit:
                     # we may underrun the limit because of dupes in the results, that's fine
                     ids.update(self.search(cr, user, args + [('name',operator,name)], limit=(limit and (limit-len(ids)) or False) , context=context))
@@ -705,6 +876,10 @@ class product_product(osv.osv):
     # Could be overrided for variants matrices prices
     #
     def price_get(self, cr, uid, ids, ptype='list_price', context=None):
+        products = self.browse(cr, uid, ids, context=context)
+        return self._price_get(cr, uid, products, ptype=ptype, context=context)
+
+    def _price_get(self, cr, uid, products, ptype='list_price', context=None):
         if context is None:
             context = {}
 
@@ -715,10 +890,10 @@ class product_product(osv.osv):
 
         res = {}
         product_uom_obj = self.pool.get('product.uom')
-        for product in self.browse(cr, uid, ids, context=context):
+        for product in products:
             res[product.id] = product[ptype] or 0.0
             if ptype == 'list_price':
-                res[product.id] = (res[product.id] * (product.price_margin or 1.0)) + \
+                res[product.id] = (res[product.id] + ((res[product.id] * (product.price_margin)) / 100)) + \
                         product.price_extra
             if 'uom' in context:
                 uom = product.uom_id or product.uos_id
@@ -744,9 +919,12 @@ class product_product(osv.osv):
         # will do the other languages).
         context_wo_lang = context.copy()
         context_wo_lang.pop('lang', None)
-        product = self.read(cr, uid, id, ['name'], context=context_wo_lang)
+        product = self.read(cr, uid, id, ['name', 'list_price', 'standard_price', 'categ_id', 'variants', 'product_tmpl_id'], context=context_wo_lang)
         default = default.copy()
-        default.update(name=_("%s (copy)") % (product['name']))
+        if product['variants']:
+            default.update(variants=_("%s (copy)") % (product['variants']), product_tmpl_id=product['product_tmpl_id'][0])
+        else:
+            default.update(name=_("%s (copy)") % (product['name']), list_price=product['list_price'], standard_price=product['standard_price'], categ_id=product['categ_id'][0], product_tmpl_id=None)
 
         if context.get('variant',False):
             fields = ['product_tmpl_id', 'active', 'variants', 'default_code',
@@ -766,8 +944,10 @@ class product_product(osv.osv):
     def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
         if context is None:
             context = {}
-        if context and context.get('search_default_categ_id', False):
+        if context.get('search_default_categ_id'):
             args.append((('categ_id', 'child_of', context['search_default_categ_id'])))
+        if context.get('search_variants'):
+            args.append(('variants', '!=', ''))
         return super(product_product, self).search(cr, uid, args, offset=offset, limit=limit, order=order, context=context, count=count)
 
 
@@ -842,7 +1022,6 @@ class product_supplierinfo(osv.osv):
     _description = "Information about a product supplier"
     def _calc_qty(self, cr, uid, ids, fields, arg, context=None):
         result = {}
-        product_uom_pool = self.pool.get('product.uom')
         for supplier_info in self.browse(cr, uid, ids, context=context):
             for field in fields:
                 result[supplier_info.id] = {field:False}
@@ -855,10 +1034,10 @@ class product_supplierinfo(osv.osv):
         'product_name': fields.char('Supplier Product Name', size=128, help="This supplier's product name will be used when printing a request for quotation. Keep empty to use the internal one."),
         'product_code': fields.char('Supplier Product Code', size=64, help="This supplier's product code will be used when printing a request for quotation. Keep empty to use the internal one."),
         'sequence' : fields.integer('Sequence', help="Assigns the priority to the list of product supplier."),
-        'product_uom': fields.related('product_id', 'uom_po_id', type='many2one', relation='product.uom', string="Supplier Unit of Measure", readonly="1", help="This comes from the product form."),
+        'product_uom': fields.related('product_tmpl_id', 'uom_po_id', type='many2one', relation='product.uom', string="Supplier Unit of Measure", readonly="1", help="This comes from the product form."),
         'min_qty': fields.float('Minimal Quantity', required=True, help="The minimal quantity to purchase to this supplier, expressed in the supplier Product Unit of Measure if not empty, in the default unit of measure of the product otherwise."),
         'qty': fields.function(_calc_qty, store=True, type='float', string='Quantity', multi="qty", help="This is a quantity which is converted into Default Unit of Measure."),
-        'product_id' : fields.many2one('product.template', 'Product', required=True, ondelete='cascade', select=True),
+        'product_tmpl_id' : fields.many2one('product.template', 'Product Template', required=True, ondelete='cascade', select=True),
         'delay' : fields.integer('Delivery Lead Time', required=True, help="Lead time in days between the confirmation of the purchase order and the reception of the products in your warehouse. Used by the scheduler for automatic computation of the purchase order planning."),
         'pricelist_ids': fields.one2many('pricelist.partnerinfo', 'suppinfo_id', 'Supplier Pricelist'),
         'company_id':fields.many2one('res.company','Company',select=1),
@@ -884,10 +1063,11 @@ class product_supplierinfo(osv.osv):
         pricelist_pool = self.pool.get('product.pricelist')
         currency_pool = self.pool.get('res.currency')
         currency_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
+        # Compute price from standard price of product
+        product_price = product_pool.price_get(cr, uid, [product_id], 'standard_price', context=context)[product_id]
+        product = product_pool.browse(cr, uid, product_id, context=context)
         for supplier in partner_pool.browse(cr, uid, supplier_ids, context=context):
-            # Compute price from standard price of product
-            price = product_pool.price_get(cr, uid, [product_id], 'standard_price', context=context)[product_id]
-
+            price = product_price
             # Compute price from Purchase pricelist of supplier
             pricelist_id = supplier.property_product_pricelist_purchase.id
             if pricelist_id:
@@ -895,7 +1075,7 @@ class product_supplierinfo(osv.osv):
                 price = currency_pool.compute(cr, uid, pricelist_pool.browse(cr, uid, pricelist_id).currency_id.id, currency_id, price)
 
             # Compute price from supplier pricelist which are in Supplier Information
-            supplier_info_ids = self.search(cr, uid, [('name','=',supplier.id),('product_id','=',product_id)])
+            supplier_info_ids = self.search(cr, uid, [('name','=',supplier.id),('product_tmpl_id','=',product.product_tmpl_id.id)])
             if supplier_info_ids:
                 cr.execute('SELECT * ' \
                     'FROM pricelist_partnerinfo ' \