[IMP] project_purchase: improve performance for purchase_orders_count

Before this commit, when the `purchase_orders_count` field is computed,
we do a search in `purchase.order.line` to get the purchase order linked
to the projects in `self` of the `_compute_purchase_orders_count`
method. However, if no project has AA set this search is useless, we can
directly set `purchase_orders_count` to 0 for all projects because the
linked between the POs and the projects is made with AA of the project.

This commit changes the method to search the PO linked to the projects
only when at least one project has AA set, otherwise we directly set the
`purchase_orders_count` to 0 for all projects contained in `self`.

task-2710808
This commit is contained in:
Xavier BOL (xbo)
2022-04-01 15:33:00 +02:00
committed by Laurent Stukkens (LTU)
parent 265a45a0c6
commit 7f218598fe
+3 -1
View File
@@ -11,8 +11,10 @@ class Project(models.Model):
@api.depends('analytic_account_id')
def _compute_purchase_orders_count(self):
if not self.analytic_account_id:
self.purchase_orders_count = 0
return
purchase_orders_data = self.env['purchase.order.line']._read_group([
('account_analytic_id', '!=', False),
('account_analytic_id', 'in', self.analytic_account_id.ids)
], ['account_analytic_id', 'order_id:count_distinct'], ['account_analytic_id'])
mapped_data = dict([(data['account_analytic_id'][0], data['order_id']) for data in purchase_orders_data])