Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

添加新的total quantity



  • sale.py<br />from osv import osv,fields<br />  <br />class sale_order(osv.osv):<br /> _name='sale.order'<br /> _inherit = 'sale.order'<br /> <br /> def amount_all(self, cr, uid, ids, field_name, arg, context=None):<br /> res = {}<br /> for order in self.browse(cr, uid, ids, context=context):<br /> res[order.id] = {<br /> 'total_quantity': 0.0,<br /> }<br /> val2 = 0.0<br /> for line in order.order_line:<br /> val2 += line.product_uom_qty<br /> res[order.id]['total_quantity'] = val2<br /> return res<br /><br /> def _get_order(self, cr, uid, ids, context=None):<br /> result = {}<br /> for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):<br /> result[line.order_id.id] = True<br /> return result.keys() <br /> <br /> _columns = {<br />   <br /> 'total_quantity':fields.function(amount_all,type='float',string='Total Quantity',<br /> store={<br /> 'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),<br /> 'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),<br /> },<br /> multi='sums', help="The total quantity", track_visibility='always'),<br /> }<br />        <br />sale_order()  <br /><br />sale_view.xml<br /> <xpath expr="//field[@name='amount_untaxed']" position="before"><br /> <field name="total_quantity" /><br /> </xpath> <br /><br />新添加的total_quantity field 回值显示0.00. 无报错,问题出在那里,请有经验的解答下, 求的值是全订单的物品数量值.谢谢



  • sale.py<br />from osv import osv,fields<br />  <br />class sale_order(osv.osv):<br /> _name='sale.order'<br /> _inherit = 'sale.order'<br /> <br /> def amount_all(self, cr, uid, ids, field_name, arg, context=None):<br /> res = {}<br /> for order in self.browse(cr, uid, ids, context=context):<br /> res[order.id] = {<br /> 'total_quantity': 0.0,<br /> }<br /> val2 = 0.0<br /> for line in order.order_line:<br /> val2 += line.product_uom_qty<br /> res[order.id]['total_quantity'] = val2<br /> return res<br /><br /> def _get_order(self, cr, uid, ids, context=None):<br /> result = {}<br /> for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):<br /> result[line.order_id.id] = True<br /> return result.keys() <br /> <br /> _columns = {<br />   <br /> 'total_quantity':fields.function(amount_all,type='float',string='Total Quantity',<br /> store={<br /> 'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),<br /> 'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),<br /> },<br /> multi='sums', help="The total quantity", track_visibility='always'),<br /> }<br />        <br />sale_order()  <br /><br />sale_view.xml<br /> <xpath expr="//field[@name='amount_untaxed']" position="before"><br /> <field name="total_quantity" /><br /> </xpath> <br /><br />新添加的total_quantity field 回值显示0.00. 无报错,问题出在那里,请有经验的解答下, 求的值是全订单的物品数量值.谢谢


  • 管理员

    amount_all的返回值的数据格式错误。应该返回一个id-value的mapping。<br />[code]res[order.id]=value[/code]



  • 添加return res[order.id]=value? 还是别的,请解释下,不太明白,谢谢


  • 管理员

    看来代码不是你写得 ;) <br /><br /><br />你现在返回的值是这样的:{ORDER_ID:{'total_quantity': QUANTITY_VAL},...}<br />需要返回的值应该是这样的: {ORDER_ID: QUANTITY_VAL, ...}



  • 回的值还是不对,还是0.00,请指点


  • 管理员

    [quote author=mrchengjin link=topic=16566.msg28759#msg28759 date=1408342762]<br />回的值还是不对,还是0.00,请指点<br />[/quote]<br /><br /><br />把你修改后的代码,贴出来,大家看看。修改后要重启服务器。<br /><br /><br />另外, 你这样的需求可以不用写function字段的,参考一下“会计凭证”表单中“分录明细”部分下面的“贷方合计”,“借方合计”。只要在销售订单表单视图中,订单行明细中 'product_uom_qty' 字段中添加“sum”属性就可以了。



  • sale.py<br /># -- coding: utf-8 --<br /><br />from osv import osv,fields<br />  <br />class sale_order(osv.osv):<br /> _name='sale.order'<br /> _inherit = 'sale.order'<br /> <br /> def qty_all(self, cr, uid, ids, field_name, arg, context=None):<br /> res = {}<br /> for order in self.browse(cr, uid, ids, context=context):<br /> res[order.id] = {<br /> 'total_quantity': 0.0,<br /> }<br /> val2 = 0.0 <br /> for line in order.order_line:<br /> val2 += line.product_uom_qty <br /> res[order.id] = val2<br /> return res<br /> <br /> def _get_order(self, cr, uid, ids, context=None):<br /> result = {}<br /> for line in self.pool.get('sale.order.line').browse(cr, uid, ids, context=context):<br /> result[line.order_id.id] = True<br /> return result.keys() <br /> <br /> _columns = { <br /> <br /> 'total_quantity':fields.function(qty_all,type='float',string='Total Quantity',<br /> store={<br /> 'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),<br /> 'sale.order.line': (_get_order, ['product_uom_qty'], 10),<br /> }, help="The total quantity", track_visibility='always'),<br /> }<br /><br />经修改和测试后能用的代码,谢谢大家指导.<br />6.1测试成功,如果已经建立的表单,需要修改表单里的产品数量(任意)并保存才能显示新的计算产品量


  • 管理员

    [code]  <br />for order in self.browse(cr, uid, ids, context=context):        <br />    res[order.id] = {'total_quantity': 0.0,}[/code]<br />这段代码就不需要了。


登录后回复
 

与 Odoo 中文社区 的连接断开,我们正在尝试重连,请耐心等待