[FIX] purchase: loading time on partners with many children
When loading a partner form view, the ORM prefetches the computed fields purchase_order_count and supplier_invoice_count for all the related partners as well (parent, children, ..). Instead of reading the orders and invoices once per partner, we group them all together. opw 1919933 closes odoo/odoo#29788
This commit is contained in:
@@ -11,11 +11,32 @@ class res_partner(models.Model):
|
||||
|
||||
@api.multi
|
||||
def _purchase_invoice_count(self):
|
||||
PurchaseOrder = self.env['purchase.order']
|
||||
Invoice = self.env['account.invoice']
|
||||
for partner in self:
|
||||
partner.purchase_order_count = PurchaseOrder.search_count([('partner_id', 'child_of', partner.id)])
|
||||
partner.supplier_invoice_count = Invoice.search_count([('partner_id', 'child_of', partner.id), ('type', '=', 'in_invoice')])
|
||||
# retrieve all children partners and prefetch 'parent_id' on them
|
||||
all_partners = self.search([('id', 'child_of', self.ids)])
|
||||
all_partners.read(['parent_id'])
|
||||
|
||||
purchase_order_groups = self.env['purchase.order'].read_group(
|
||||
domain=[('partner_id', 'in', all_partners.ids)],
|
||||
fields=['partner_id'], groupby=['partner_id']
|
||||
)
|
||||
for group in purchase_order_groups:
|
||||
partner = self.browse(group['partner_id'][0])
|
||||
while partner:
|
||||
if partner in self:
|
||||
partner.purchase_order_count += group['partner_id_count']
|
||||
partner = partner.parent_id
|
||||
|
||||
supplier_invoice_groups = self.env['account.invoice'].read_group(
|
||||
domain=[('partner_id', 'in', all_partners.ids),
|
||||
('type', '=', 'in_invoice')],
|
||||
fields=['partner_id'], groupby=['partner_id']
|
||||
)
|
||||
for group in supplier_invoice_groups:
|
||||
partner = self.browse(group['partner_id'][0])
|
||||
while partner:
|
||||
if partner in self:
|
||||
partner.supplier_invoice_count += group['partner_id_count']
|
||||
partner = partner.parent_id
|
||||
|
||||
@api.model
|
||||
def _commercial_fields(self):
|
||||
|
||||
Reference in New Issue
Block a user