以下是我创建的编码:
class mrp_extend(osv.Model):
_inherit = 'mrp.bom'
def _get_unit_cost(self, cr, uid, ids, field_name, arg, context):
result = {}
for bom_line_obj in self.browse(cr, uid, ids, context=context):
result[bom_line_obj.id] = bom_line_obj.product_id.product_tmpl_id.standard_price or 0.00
return result
def _get_product_total_cost(self, cr, uid, ids, field_name, arg, context):
result = {}
for bom_line_obj in self.browse(cr, uid, ids, context=context):
result[bom_line_obj.id] = (bom_line_obj.product_id.product_tmpl_id.standard_price or 0.00) * (bom_line_obj.product_qty or 0.00)
return result
def get_total_cost(self, cr, uid, ids, name, args, context=None):
res = {}
for rec in self.browse(cr, uid, ids, context=context):
total_cost = 0.0
for line_rec in rec.bom_lines:
total_cost += line_rec.product_total_cost or 0.0
res.update({rec.id : total_cost})
return res
_columns = {
'product_unit_cost' : fields.function(_get_unit_cost, string="Product Unit Cost", digits_compute=dp.get_precision('Product Price')),
'product_total_cost' : fields.function(_get_product_total_cost, string="Total Product Unit Cost", digits_compute=dp.get_precision('Product Price')),
'total_cost' : fields.function(get_total_cost, string="Total Cost", digits_compute=dp.get_precision('Product Price')),
'mrp_bom_ids' : fields.one2many('mrp.extend.bom', 'mrp_extend_id', 'MRP Extend', states={'done': [('readonly', False)]})
}
_defaults = {
'total_cost': lambda *a: 0.0,
}
def onchange_product_id(self, cr, uid, ids, product_id, name, context=None):
""" Changes UoM and name if product_id changes.
@param name: Name of the field
@param product_id: Changed product_id
@return: Dictionary of changed values
"""
if product_id:
prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
return {'value': {'name': prod.name, 'product_uom': prod.uom_id.id, 'product_unit_cost': prod.standard_price}}
return {}
def onchange_uom(self, cr, uid, ids, product_id, product_uom, context=None):
res = {'value':{}}
if not product_uom or not product_id:
return res
product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
uom = self.pool.get('product.uom').browse(cr, uid, product_uom, context=context)
if uom.category_id.id != product.uom_id.category_id.id:
res['warning'] = {'title': _('Warning'), 'message': _('The Product Unit of Measure you chose has a different category than in the product form.')}
res['value'].update({'product_uom': product.uom_id.id})
return res
class mrp_production_extend(osv.Model):
_inherit = 'mrp.production'
def _get_total_cost(self, cr, uid, ids, field_name, arg, context):
bom_point = self.pool.get('mrp.bom').browse(cr, uid, ids, context=context)
total_cost = bom_point.total_cost or 0.00
product_qty = bom_point.product_qty or 0.00
if product_qty > 1:
total_cost = total_cost / product_qty
return total_cost
_columns = {
#Add by Henry on 07Nov2013
#'total_cost' : fields.float('Total Cost', digits_compute=dp.get_precision('Product Price')),
'total_cost' : fields.function(_get_total_cost, string="Total Cost", digits_compute=dp.get_precision('Product Price')),
'pre_set_qty' : fields.float('Pre Set Quantity', digits_compute=dp.get_precision('Product Unit of Measure')),
'total_final_cost' : fields.float('Total Final Cost', digits_compute=dp.get_precision('Product Price')),
'mrp_production_ids' : fields.one2many('mrp.production.extend.bom', 'mrp_production_extend_id', 'MRP Production Extend', states={'done': [('readonly', False)]})
#-----------------------------
}
我有新的创建 2 对象。一个是 mrp_extend,这个对象是从 mrp.bom 继承的。
然后我创建了新的对象名称 mrp_production_extend,这个对象是从 mrp.production 继承的。
现在我的问题是,我在 mrp_production_extend 中创建了一个函数:
def _get_total_cost(self, cr, uid, ids, field_name, arg, context):
bom_point = self.pool.get('mrp.bom').browse(cr, uid, ids, context=context)
total_cost = bom_point.total_cost or 0.00
product_qty = bom_point.product_qty or 0.00
if product_qty > 1:
total_cost = total_cost / product_qty
return total_cost
但这个功能不起作用,并告诉我这个错误:
AttributeError: 'browse_record_list' object has no attribute 'total_cost'
为什么?因为在我的 mrp_extend 对象中,我继承了 mrp.bom 并创建了新的 total_cost。我做错什么了吗?请帮忙。