[FIX] product_extended: price from bom is build for product templates

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
This commit is contained in:
Denis Ledoux 2014-11-24 17:24:49 +01:00
parent 1276d01f27
commit 6feefe4d10
4 changed files with 29 additions and 14 deletions

View File

@ -22,21 +22,30 @@ from openerp.osv import fields
from openerp.osv import osv from openerp.osv import osv
class product_product(osv.osv): class product_template(osv.osv):
_name = 'product.product' _name = 'product.template'
_inherit = 'product.product' _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 Will return test dict when the test = False
Multiple ids at once? Multiple ids at once?
testdict is used to inform the user about the changes to be made testdict is used to inform the user about the changes to be made
''' '''
testdict = {} testdict = {}
if product_ids:
ids = product_ids
model = 'product.product'
else:
ids = template_ids
model = 'product.template'
for prod_id in ids: for prod_id in ids:
bom_obj = self.pool.get('mrp.bom') 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: if bom_id:
# In recursive mode, it will first compute the prices of child boms # In recursive mode, it will first compute the prices of child boms
if recursive: if recursive:
@ -66,7 +75,9 @@ class product_product(osv.osv):
tmpl_obj = self.pool.get('product.template') tmpl_obj = self.pool.get('product.template')
for sbom in bom.bom_line_ids: for sbom in bom.bom_line_ids:
my_qty = sbom.product_qty 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: if bom.routing_id:
for wline in bom.routing_id.workcenter_lines: 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) wizard_obj.change_price(cr, uid, [wiz_id], context=ctx)
return price return price
product_product()
class product_bom(osv.osv): class product_bom(osv.osv):
_inherit = 'mrp.bom' _inherit = 'mrp.bom'

View File

@ -3,9 +3,9 @@
<data> <data>
<record model="ir.ui.view" id="product_product_ext_form_view2"> <record model="ir.ui.view" id="product_product_ext_form_view2">
<field name="name">product_extended.product.form.view</field> <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="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="arch" type="xml">
<field name="standard_price" position="after"> <field name="standard_price" position="after">
<button name="%(action_view_compute_price_wizard)d" string="Recompute price from BoM" <button name="%(action_view_compute_price_wizard)d" string="Recompute price from BoM"

View File

@ -9,6 +9,7 @@
<separator string="Change Price"/> <separator string="Change Price"/>
<group> <group>
<field name="info_field"/> <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="real_time_accounting"/>
<field name="recursive"/> <field name="recursive"/>
</group> </group>

View File

@ -21,8 +21,9 @@
# #
############################################################################## ##############################################################################
from openerp.exceptions import except_orm
from openerp.osv import fields, osv from openerp.osv import fields, osv
from openerp.tools.translate import _
class wizard_price(osv.osv): class wizard_price(osv.osv):
_name = "wizard.price" _name = "wizard.price"
@ -35,24 +36,27 @@ class wizard_price(osv.osv):
def default_get(self, cr, uid, fields, context=None): def default_get(self, cr, uid, fields, context=None):
res = super(wizard_price, self).default_get(cr, uid, fields, context=context) 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)) product_obj = product_pool.browse(cr, uid, context.get('active_id', False))
if context is None: if context is None:
context = {} context = {}
rec_id = context and context.get('active_id', False) rec_id = context and context.get('active_id', False)
assert rec_id, _('Active ID is not set in Context.') 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 return res
def compute_from_bom(self, cr, uid, ids, context=None): def compute_from_bom(self, cr, uid, ids, context=None):
assert len(ids) == 1 assert len(ids) == 1
if context is None: if context is None:
context = {} 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) rec_id = context and context.get('active_id', False)
assert rec_id, _('Active ID is not set in Context.') 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) res = self.browse(cr, uid, ids, context=context)
prod = prod_obj.browse(cr, uid, rec_id, 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: # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: