From 23e54113c29533043ed7ed9b64d9bc4db90be455 Mon Sep 17 00:00:00 2001
From: Denis Ledoux <dle@odoo.com>
Date: Thu, 18 Feb 2021 13:57:47 +0000
Subject: [PATCH] [IMP] pos_sale: `pos_order_amount_total` performances using
 read_group

Using `read_group` to gather such a sum is always more performant
than a `search` followed by a `mapped`.

In the case of the studied database,
the computation never ended,
while it ends in a few seconds
using `read_group`.

upg-7528

closes odoo/odoo#66502

X-original-commit: 8547bb6145c3ddf9c7205ea10deef22473362cce
Signed-off-by: Denis Ledoux (dle) <dle@odoo.com>
---
 addons/pos_sale/models/crm_team.py | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/addons/pos_sale/models/crm_team.py b/addons/pos_sale/models/crm_team.py
index a0cc50b5d6d1..a1cfb13f0cf3 100644
--- a/addons/pos_sale/models/crm_team.py
+++ b/addons/pos_sale/models/crm_team.py
@@ -20,6 +20,9 @@ class CrmTeam(models.Model):
 
     def _compute_pos_order_amount_total(self):
         for team in self:
-            team.pos_order_amount_total = sum(self.env['report.pos.order'].search(
-                [('session_id', 'in', team.pos_config_ids.mapped('session_ids').filtered(lambda s: s.state == 'opened').ids)]
-            ).mapped('price_total'))
+            res = self.env['report.pos.order'].read_group(
+                [("config_id.crm_team_id", "=", team.id), ("session_id.state", "=", "opened")],
+                ["price_total"],
+                [],
+            )
+            team.pos_order_amount_total = sum(r["price_total"] for r in res if r["price_total"])
-- 
GitLab