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

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

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

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

添加新的total quantity



  • sale.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. 无报错,问题出在那里,请有经验的解答下, 求的值是全订单的物品数量值.谢谢



  • sale.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. 无报错,问题出在那里,请有经验的解答下, 求的值是全订单的物品数量值.谢谢


  • 管理员

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



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


  • 管理员

    看来代码不是你写得 ;)


    你现在返回的值是这样的:{ORDER_ID:{'total_quantity': QUANTITY_VAL},...}
    需要返回的值应该是这样的: {ORDER_ID: QUANTITY_VAL, ...}



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


  • 管理员

    [quote author=mrchengjin link=topic=16566.msg28759#msg28759 date=1408342762]
    回的值还是不对,还是0.00,请指点
    [/quote]


    把你修改后的代码,贴出来,大家看看。修改后要重启服务器。


    另外, 你这样的需求可以不用写function字段的,参考一下“会计凭证”表单中“分录明细”部分下面的“贷方合计”,“借方合计”。只要在销售订单表单视图中,订单行明细中 'product_uom_qty' 字段中添加“sum”属性就可以了。



  • 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测试成功,如果已经建立的表单,需要修改表单里的产品数量(任意)并保存才能显示新的计算产品量


  • 管理员

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


登录后回复
 

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