openerp学习笔记 视图中字段只变化(on_change)联动其他字段值、选择和过滤
1.修改产品数量时,自动计算产品销售金额、销售成本和销售利润
<field name="num" on_change="on_change_product(product, num, price)"/>
2.选择历史工单时,按销售单中当前选择的客户自动过滤,打开选择历史工单窗口时,过滤条件默认为销售单中当前选择的客户
<field name="work_order" domain="[('customer','=',customer)]" context="{'search_default_customer':customer}"/>
代码示例:
<!-- views dispatch_sale_form -->
<record model="ir.ui.view"
id="dispatch_sale_form">
<field
name="name">dispatch.sale.form</field>
<field
name="model">dispatch.sale</field>
<field name="type">form</field>
<field name="priority">2</field>
<field name="arch" type="xml">
<form
string="销售单明细"
version="7.0">
<header>
<button string="确认" name="set_to_confirmed" states="draft" type="object"
icon="gtk-yes"/>
<button string="还原为草稿" name="set_to_draft" states="confirmed" type="object"
icon="gtk-convert"
groups="kl_dispatch.group_dispatch_manager"/>
<field name="state" widget="statusbar" statusbar_visible="draft,confirmed"
statusbar_colors='{"draft":"blue"}'/>
</header>
<sheet>
<group>
<group>
<field
name="date"/>
<field
name="name"/>
<field
name="customer"/>
<field name="product" on_change="on_change_product(product, num,
price)"/>
<field name="num" on_change="on_change_product(product, num,
price)"/>
<field name="price" on_change="on_change_product(product, num,
price)"/>
<field name="cost" on_change="on_change_product(product, num, price)"
invisible="1"/>
<field name="cost_view"
invisible="0"/>
</group>
<group>
<field name="employee_id"
on_change="onchange_employee_id(employee_id)"/>
<field
name="department_id"/>
<field name="work_order" domain="[('customer','=',customer)]"
context="{'search_default_customer':customer}"/>
<field
name="klbh"/>
<field
name="sum_sale"/>
<field
name="sum_cost"/>
<field
name="sum_profit"/>
</group>
</group>
<group>
<field name="note"
colspan="4"/>
</group>
</sheet>
</form>
</field>
</record>
#职员修改自动更改部门
def onchange_employee_id(self, cr, uid, ids,
employee_id,
context=None):
department_id = False
if
employee_id:
emp_obj =
self.pool.get('hr.employee')
employee = emp_obj.browse(cr, uid, employee_id,
context=context)
department_id =
employee.department_id.id
return
{'value': {'department_id': department_id}}
#产品、数量、单价、成本更改,自动计算销售金额、成本金额、利润金额
def
on_change_product(self,cr,uid,ids,product_id, num,
price):
v={}
if
product_id:
product=self.pool.get("dispatch.product").browse(cr,uid,product_id)
v["cost"]=product.cost
v["cost_view"]=product.cost
else:
v["cost"]=0
v["cost_view"]=0
v['sum_sale']=num*price
v['sum_cost']=num*v["cost"]
v['sum_profit']=v['sum_sale']-v['sum_cost']
return {"value":v}
#按销售单中对应的客户,自动过滤选择工单
domain="[('customer','=',customer)]" ,打开选择窗口时过滤条件默认客户
context="{'search_default_customer':customer}"
<group>
<field
name="date"/>
<field
name="name"/>
<field
name="customer"/>
<field name="product" on_change="on_change_product(product, num,
price)"/>
<field name="num" on_change="on_change_product(product, num,
price)"/>
<field name="price" on_change="on_change_product(product, num,
price)"/>
<field name="cost" on_change="on_change_product(product, num, price)"
invisible="1"/>
<field name="cost_view"
invisible="0"/>
</group>
<group>
<field name="employee_id"
on_change="onchange_employee_id(employee_id)"/>
<field
name="department_id"/>
<field name="work_order" domain="[('customer','=',customer)]"
context="{'search_default_customer':customer}"/>
<field
name="klbh"/>
<field
name="sum_sale"/>
<field
name="sum_cost"/>
<field
name="sum_profit"/>
</group>