如何在 OpenERP 7 中的 MRP(物料清单)中增加自定义字段

110 阅读1分钟

我们需要在 OpenERP 7 中的 MRP 中增加几个自定义字段,以便更好地管理物料清单。这些自定义字段包括产品单位成本、产品总成本和总成本,它们都应该显示在 BOM 行树字段中。

huake_00063_.jpg

解决方案

为了解决这个问题,我们需要创建一个新的 Python 类 mrp_extend 来扩展现有的 mrp.bom 模型,并添加所需的字段和函数。我们还需要创建一个新的 XML 文件 mrp_extend.xml 来注册这些更改,并将其添加到 OpenERP 的视图中。

具体步骤如下:

  1. 创建一个新的 Python 类 mrp_extend,它继承自 mrp.bom 模型:
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="Product Unit Cost", digits_compute=dp.get_precision('Product Price')),
        'total_cost' : fields.function(get_total_cost, string="Total Cost"),
        'mrp_bom_ids' : fields.one2many('mrp.extend.bom', 'mrp_extend_id', 'Stock Extend Log', states={'done': [('readonly', False)]})
    }
  1. 创建一个新的 XML 文件 mrp_extend.xml,并将其添加到 OpenERP 的视图中:
<?xml version="1.0" encoding="utf-8"?>

<openerp>
    <data>
        <!-- Add new field in MRP (By Henry on 07/Nov/2013) -->
        <record id="stock_ext_form" model="ir.ui.view">
            <field name="name">mrp.ext.form</field>
            <field name="model">mrp.bom</field>
            <field name="type">tree</field>
            <field name="inherit_id" ref="mrp.mrp_bom_form_view" />
            <field name="arch" type="xml">
                <field name="bom_lines" position="after">
                    <field name="product_unit_cost"/>
                    <field name="product_total_cost" sum="Total Product Unit Cost"/> 
                </field>                                     
            </field>
        </record>   
    </data>
</openerp>
  1. 重新启动 OpenERP 服务,以便应用这些更改。

现在,您应该就可以在 MRP 的 BOM 行树字段中看到这些新的自定义字段了。