在树视图中使用“分组依据”时如何汇总其他列?

时间:2014-12-17 22:40:22

标签: python xml odoo openerp-8

我有一个包含三个不同金额字段的自定义模块:

  • Total a pagarfloat字段
  • Total pendienteTotal pagado是根据函数
  • 计算的

这就是我的树视图的样子:

enter image description here


当按过滤器使用组时,我想显示功能字段的总和,但只显示第一个:

enter image description here


我尝试在XML记录中使用sum参数,但这不起作用。

这是我的模块:

class res_partner_current_account(osv.osv):
    def _amount_pending_wrapper(self, cr, uid, ids, field_name, arg, context=None):
        """
        Wrapper because of direct method passing as parameter for function fields
        """
        return self._amount_pending(cr, uid, ids, field_name, arg, context=context)

    def _amount_pending(self, cr, uid, ids, field_name, arg, context=None):
        cur_obj = self.pool.get('res.currency')
        res = {}
        for current_account in self.browse(cr, uid, ids, context=context):
            res[current_account.id] = {
                'amount_pending': 0.0,
                'amount_payed': 0.0,
            }
            cur = current_account.currency_id
            payed = 0.0
            for line in current_account.pay_line:
                payed += line.amount
            pending = current_account.amount_total - payed
            res[current_account.id]['amount_pending'] = cur_obj.round(cr, uid, cur, pending)
            res[current_account.id]['amount_payed'] = cur_obj.round(cr, uid, cur, payed)
        return res

    _name = 'res.partner.current.account'
    _columns = {
        'name': fields.char('Concepto'),
        'order_id': fields.many2one('sale.order', 'Presupuesto', select=True, required=True, ondelete='cascade'),
        'general_account_id': fields.many2one('res.partner.general.account', 'Cuenta general', select=True, required=True, ondelete='cascade'),
        'currency_id': fields.many2one('res.currency', 'Moneda', required=True),
        'amount_total': fields.float('Total a pagar', help='El total de la deuda'),
        'amount_pending': fields.function(_amount_pending_wrapper, digits_compute=dp.get_precision('Account'), string='Total pendiente', multi='sums', help='El importe pendiente por pagar'),
        'amount_payed': fields.function(_amount_pending_wrapper, digits_compute=dp.get_precision('Account'),  string='Total pagado', multi='sums', help='El importe acumulado de pagos'),
        'pay_line': fields.one2many('res.partner.current.account.line', 'current_account_id', 'Líneas de pago',),
        'partner_id': fields.related('general_account_id', 'partner_id', type='many2one', relation='res.partner', string='Contratista', readonly=True, store=True),
        'account_analytic_id': fields.related('general_account_id', 'account_analytic_id', type='many2one', relation='account.analytic.account', string='Proyecto', readonly=True, store=True),
    }
    _defaults = {
        'amount_total': 0.0,
    }
    _rec_name = 'name'
    _order = 'create_date desc'

    def create_pay(self, cr, uid, ids, context=None):
        return {
            'name': 'Pagos',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'res.partner.current.account',
            'view_id': False,
            'res_id': ids[0],
            'type': 'ir.actions.act_window',
        }

1 个答案:

答案 0 :(得分:4)

找到解决方案。关键是要覆盖类的read_group方法:

class your_class(osv.osv):
    # ...        

    def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False, lazy=True):
        res = super(your_class, self).read_group(cr, uid, domain, fields, groupby, offset, limit=limit, context=context, orderby=orderby, lazy=lazy)
        if 'amount_pending' in fields:
            for line in res:
                if '__domain' in line:
                    lines = self.search(cr, uid, line['__domain'], context=context)
                    pending_value = 0.0
                    for current_account in self.browse(cr, uid, lines, context=context):
                        pending_value += current_account.amount_pending
                    line['amount_pending'] = pending_value
        if 'amount_payed' in fields:
            for line in res:
                if '__domain' in line:
                    lines = self.search(cr, uid, line['__domain'], context=context)
                    payed_value = 0.0
                    for current_account in self.browse(cr, uid, lines, context=context):
                        payed_value += current_account.amount_payed
                    line['amount_payed'] = payed_value
        return res


例如,如果您想要删除组中的列的总和,您可以执行以下操作:

class your_class(osv.osv):
    # ...     

    def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False, lazy=True):
        if 'column' in fields:
            fields.remove('column')
        return super(your_class, self).read_group(cr, uid, domain, fields, groupby, offset, limit=limit, context=context, orderby=orderby, lazy=lazy):