diff options
| author | Denis Ledoux <dle@odoo.com> | 2018-11-29 11:40:24 +0000 |
|---|---|---|
| committer | Denis Ledoux <dle@odoo.com> | 2018-11-29 11:46:22 +0000 |
| commit | 5644ab53ab6f370a2fb48242e22702655afd6655 (patch) | |
| tree | c33b070e224a81dc7e531a724c3fb23d31ea1b2e | |
| parent | 9fbac5fcbbec20777a15a46fe17a6eb7e21575d4 (diff) | |
[FIX] sale: improve team pipeline dashboard quotation counts performance
Doing the `read_group` on `sale.order` rather than
`sale.report` to get the quotations amount and count
significantly improve the performance (5x faster on big databases).
This is because `sale.report` is a view,
with a bunch of fields taking time to compute / fetch.
Besides, the algorithm to get the number of quotations
was an heuristic based on the "group by name" on the `sale.report`,
which was not as precise,
as multiple quotations could potentially have the same name,
and slowed down the query,
because it returned one line per quotation,
versus one line per team with this revision.
closes odoo/odoo#29139
| -rw-r--r-- | addons/sale/models/sales_team.py | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/addons/sale/models/sales_team.py b/addons/sale/models/sales_team.py index 3dd93e94b6f..bb0686c50c9 100644 --- a/addons/sale/models/sales_team.py +++ b/addons/sale/models/sales_team.py @@ -39,13 +39,28 @@ class CrmTeam(models.Model): def _compute_quotations_to_invoice(self): non_website_teams = self.filtered(lambda team: team.team_type != 'website') if non_website_teams: - quotation_data = self.env['sale.report'].read_group([ + query = self.env['sale.order']._where_calc([ ('team_id', 'in', non_website_teams.ids), ('state', 'in', ['draft', 'sent']), - ], ['price_total', 'team_id', 'name'], ['team_id', 'name'], lazy=False) + ]) + self.env['sale.order']._apply_ir_rules(query, 'read') + _, where_clause, where_clause_args = query.get_sql() + select_query = """ + SELECT team_id, count(*), sum(amount_total / + CASE COALESCE(currency_rate, 0) + WHEN 0 THEN 1.0 + ELSE currency_rate + END + ) as amount_total + FROM sale_order + WHERE %s + GROUP BY team_id + """ % where_clause + self.env.cr.execute(select_query, where_clause_args) + quotation_data = self.env.cr.dictfetchall() for datum in quotation_data: - self.browse(datum['team_id'][0]).quotations_amount += datum['price_total'] - self.browse(datum['team_id'][0]).quotations_count += 1 + self.browse(datum['team_id']).quotations_amount = datum['amount_total'] + self.browse(datum['team_id']).quotations_count = datum['count'] @api.multi def _compute_sales_to_invoice(self): |
