From 1cee545732e96ab0c65b42639b62f514a45b4d99 Mon Sep 17 00:00:00 2001 From: Carlos Dauden Date: Wed, 3 May 2017 17:27:12 +0200 Subject: [PATCH] [9.0][IMP] partner_payment_return_risk: Improve performance using search_group --- partner_payment_return_risk/models/res_partner.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/partner_payment_return_risk/models/res_partner.py b/partner_payment_return_risk/models/res_partner.py index ef0f57f88..3a1eb4892 100644 --- a/partner_payment_return_risk/models/res_partner.py +++ b/partner_payment_return_risk/models/res_partner.py @@ -20,10 +20,16 @@ class ResPartner(models.Model): @api.multi @api.depends('invoice_ids.state', 'invoice_ids.returned_payment') def _compute_risk_payment_return(self): + AccountInvoice = self.env['account.invoice'] for partner in self: - invoices = partner.invoice_ids.filtered( - lambda x: x.returned_payment and x.state == 'open') - partner.risk_payment_return = sum(invoices.mapped('residual')) + partner.risk_payment_return = AccountInvoice.read_group( + [('id', '=', partner.id), + ('returned_payment', '=', True), + ('state', '=', 'open'), + ], + ['residual'], + [] + )[0]['residual'] @api.model def _risk_field_list(self):