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

时间:2022-10-21 06:12:50

I have a custom module with three different amount fields for money:

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

  • Total a pagar is a float field
  • 帕格是一个漂浮场
  • Total pendiente and Total pagado are calculated from functions
  • 总的pendiente和总的pagado是根据功能计算的

This is how my Tree View looks like:

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

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


When using a group by filter, I want to show the sum of the function fields but only the first one is showing:

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

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


I tried using sum parameter in the XML record but that's not working.

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

This is my module:

这是我的模块:

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 个解决方案

#1


3  

Found the solution. The key is to overwrite the read_group method of the class:

找到了解决方案。关键是要覆盖类的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


If you want, for example, remove the sum of a column in the group by, you can do something like this:

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

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):

#1


3  

Found the solution. The key is to overwrite the read_group method of the class:

找到了解决方案。关键是要覆盖类的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


If you want, for example, remove the sum of a column in the group by, you can do something like this:

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

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):