[FIX] product_extended: price from bom is build for product templates
authorDenis Ledoux <dle@odoo.com>
Mon, 24 Nov 2014 16:24:49 +0000 (17:24 +0100)
committerDenis Ledoux <dle@odoo.com>
Mon, 24 Nov 2014 17:01:20 +0000 (18:01 +0100)
As it sets the standard_price field, which is defined on product templates only: setting the standard price of a product variant will lead to set the standard price to all the variants of the product template.

There is therefore no meaning to compute and set the standard price of product variants according to the boms

addons/product_extended/product_extended.py
addons/product_extended/product_extended_view.xml
addons/product_extended/product_extended_wizard.xml
addons/product_extended/wizard/wizard_price.py

index 4aecc29..a8bcea6 100644 (file)
@@ -22,21 +22,30 @@ from openerp.osv import fields
 from openerp.osv import osv
 
 
-class product_product(osv.osv):
-    _name = 'product.product'
-    _inherit = 'product.product'
+class product_template(osv.osv):
+    _name = 'product.template'
+    _inherit = 'product.template'
 
 
-    def compute_price(self, cr, uid, ids, recursive=False, test=False, real_time_accounting = False, context=None):
+    def compute_price(self, cr, uid, product_ids, template_ids=False, recursive=False, test=False, real_time_accounting = False, context=None):
         '''
         Will return test dict when the test = False
         Multiple ids at once?
         testdict is used to inform the user about the changes to be made
         '''
         testdict = {}
+        if product_ids:
+            ids = product_ids
+            model = 'product.product'
+        else:
+            ids = template_ids
+            model = 'product.template'
         for prod_id in ids:
             bom_obj = self.pool.get('mrp.bom')
-            bom_id = bom_obj._bom_find(cr, uid, product_id = prod_id, context=context)
+            if model == 'product.product':
+                bom_id = bom_obj._bom_find(cr, uid, product_id=prod_id, context=context)
+            else:
+                bom_id = bom_obj._bom_find(cr, uid, product_tmpl_id=prod_id, context=context)
             if bom_id:
                 # In recursive mode, it will first compute the prices of child boms
                 if recursive:
@@ -66,7 +75,9 @@ class product_product(osv.osv):
         tmpl_obj = self.pool.get('product.template')
         for sbom in bom.bom_line_ids:
             my_qty = sbom.product_qty
-            price += uom_obj._compute_price(cr, uid, sbom.product_id.uom_id.id, sbom.product_id.standard_price, sbom.product_uom.id) * my_qty
+            if not sbom.attribute_value_ids:
+                # No attribute_value_ids means the bom line is not variant specific
+                price += uom_obj._compute_price(cr, uid, sbom.product_id.uom_id.id, sbom.product_id.standard_price, sbom.product_uom.id) * my_qty
 
         if bom.routing_id:
             for wline in bom.routing_id.workcenter_lines:
@@ -93,7 +104,6 @@ class product_product(osv.osv):
                 wizard_obj.change_price(cr, uid, [wiz_id], context=ctx)
         return price
 
-product_product()
 
 class product_bom(osv.osv):
     _inherit = 'mrp.bom'
index 01432a9..c6f143d 100644 (file)
@@ -3,9 +3,9 @@
     <data>
         <record model="ir.ui.view" id="product_product_ext_form_view2">
             <field name="name">product_extended.product.form.view</field>
-            <field name="model">product.product</field>
+            <field name="model">product.template</field>
             <field name="priority">3</field>
-            <field name="inherit_id" ref="product.product_normal_form_view" />
+            <field name="inherit_id" ref="product.product_template_only_form_view" />
             <field name="arch" type="xml">
                 <field name="standard_price" position="after">
                     <button name="%(action_view_compute_price_wizard)d" string="Recompute price from BoM"
index cfab062..ace66fa 100644 (file)
@@ -9,6 +9,7 @@
                   <separator string="Change Price"/>
                   <group>
                       <field name="info_field"/>
+                      <div colspan="2">The price is computed from the bill of material lines which are not variant specific</div>
                       <field name="real_time_accounting"/>
                       <field name="recursive"/>
                   </group>
index ddbeff9..688baff 100644 (file)
@@ -21,8 +21,9 @@
 #
 ##############################################################################
 
+from openerp.exceptions import except_orm
 from openerp.osv import fields, osv
-
+from openerp.tools.translate import _
 
 class wizard_price(osv.osv):
     _name = "wizard.price"
@@ -35,24 +36,27 @@ class wizard_price(osv.osv):
 
     def default_get(self, cr, uid, fields, context=None):
         res = super(wizard_price, self).default_get(cr, uid, fields, context=context)
-        product_pool = self.pool.get('product.product')
+        product_pool = self.pool.get('product.template')
         product_obj = product_pool.browse(cr, uid, context.get('active_id', False))
         if context is None:
             context = {}
         rec_id = context and context.get('active_id', False)
         assert rec_id, _('Active ID is not set in Context.')
-        res['info_field'] = str(product_pool.compute_price(cr, uid, [product_obj.id], test=True, context=context))
+        res['info_field'] = str(product_pool.compute_price(cr, uid, [], template_ids=[product_obj.id], test=True, context=context))
         return res
 
     def compute_from_bom(self, cr, uid, ids, context=None):
         assert len(ids) == 1
         if context is None:
             context = {}
+        model = context.get('active_model')
+        if model != 'product.template':
+            raise except_orm(_('Wrong model!'), _('This wizard is build for product templates, while you are currently running it from a product variant.'))
         rec_id = context and context.get('active_id', False)
         assert rec_id, _('Active ID is not set in Context.')
-        prod_obj = self.pool.get('product.product')
+        prod_obj = self.pool.get('product.template')
         res = self.browse(cr, uid, ids, context=context)
         prod = prod_obj.browse(cr, uid, rec_id, context=context)
-        prod_obj.compute_price(cr, uid, [prod.id], real_time_accounting=res[0].real_time_accounting, recursive=res[0].recursive, test=False, context=context)
+        prod_obj.compute_price(cr, uid, [], template_ids=[prod.id], real_time_accounting=res[0].real_time_accounting, recursive=res[0].recursive, test=False, context=context)
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: