sale.py
# -- coding: utf-8 --
from osv import osv,fields
class sale_order(osv.osv):
_name='sale.order'
_inherit = 'sale.order'
def qty_all(self, cr, uid, ids, field_name, arg, context=None):
res = {}
for order in self.browse(cr, uid, ids, context=context):
res[order.id] = {
'total_quantity': 0.0,
}
val2 = 0.0
for line in order.order_line:
val2 += line.product_uom_qty
res[order.id] = val2
return res
def _get_order(self, cr, uid, ids, context=None):
result = {}
for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):
result[line.order_id.id] = True
return result.keys()
_columns = {
'total_quantity':fields.function(qty_all,type='float',string='Total Quantity',
store={
'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
'sale.order.line': (_get_order, ['product_uom_qty'], 10),
}, help="The total quantity", track_visibility='always'),
}
经修改和测试后能用的代码,谢谢大家指导.
6.1测试成功,如果已经建立的表单,需要修改表单里的产品数量(任意)并保存才能显示新的计算产品量
mrchengjin
-
添加新的total quantity -
添加新的total quantity回的值还是不对,还是0.00,请指点
-
添加新的total quantity添加return res[order.id]=value? 还是别的,请解释下,不太明白,谢谢
-
添加新的total quantitysale.py
from osv import osv,fields
class sale_order(osv.osv):
_name='sale.order'
_inherit = 'sale.order'
def amount_all(self, cr, uid, ids, field_name, arg, context=None):
res = {}
for order in self.browse(cr, uid, ids, context=context):
res[order.id] = {
'total_quantity': 0.0,
}
val2 = 0.0
for line in order.order_line:
val2 += line.product_uom_qty
res[order.id]['total_quantity'] = val2
return res
def _get_order(self, cr, uid, ids, context=None):
result = {}
for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):
result[line.order_id.id] = True
return result.keys()
_columns = {
'total_quantity':fields.function(amount_all,type='float',string='Total Quantity',
store={
'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
},
multi='sums', help="The total quantity", track_visibility='always'),
}
sale_order()
sale_view.xml
<xpath expr="//field[@name='amount_untaxed']" position="before">
<field name="total_quantity" />
</xpath>
新添加的total_quantity field 回值显示0.00. 无报错,问题出在那里,请有经验的解答下, 求的值是全订单的物品数量值.谢谢