From a55a08ef563ca3f17b02780ff98e5c696a26e373 Mon Sep 17 00:00:00 2001 From: "Aurelien van Delft (avd)" Date: Wed, 3 May 2023 12:55:00 +0000 Subject: [PATCH] [FIX] purchase_stock: _compute_on_time_rate optimizations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Turn the call to filtered after the search on purchase.order.line into a call to _search. This allows to remove filtered by adding an additional leaf in the search domain. Add read calls to fetch fields from db and store them in cache on recordset batches. Example speedup: partner with 136555 POL 6.33s -> 3.42s opw-3277299 closes odoo/odoo#122218 X-original-commit: b3e81b092864c71a9d5d097fbfa965319acf4fba Signed-off-by: William Henrotin (whe) Signed-off-by: Van Delft Aurélien (avd) --- addons/purchase_stock/models/res_partner.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/addons/purchase_stock/models/res_partner.py b/addons/purchase_stock/models/res_partner.py index 89604b83237..50ae5b9d3ba 100644 --- a/addons/purchase_stock/models/res_partner.py +++ b/addons/purchase_stock/models/res_partner.py @@ -21,12 +21,17 @@ class ResPartner(models.Model): ('partner_id', 'in', self.ids), ('date_order', '>', fields.Date.today() - timedelta(365)), ('qty_received', '!=', 0), - ('order_id.state', 'in', ['done', 'purchase']) - ]).filtered(lambda l: l.product_id.sudo().product_tmpl_id.type != 'service') + ('order_id.state', 'in', ['done', 'purchase']), + ('product_id', 'in', self.env['product.product'].sudo()._search([('type', '!=', 'service')])) + ]) lines_qty_done = defaultdict(lambda: 0) moves = self.env['stock.move'].search([ ('purchase_line_id', 'in', order_lines.ids), - ('state', '=', 'done')]).filtered(lambda m: m.date.date() <= m.purchase_line_id.date_planned.date()) + ('state', '=', 'done')]) + # Fetch fields from db and put them in cache. + order_lines.read(['date_planned', 'partner_id', 'product_uom_qty'], load='') + moves.read(['purchase_line_id', 'date'], load='') + moves = moves.filtered(lambda m: m.date.date() <= m.purchase_line_id.date_planned.date()) for move, qty_done in zip(moves, moves.mapped('quantity_done')): lines_qty_done[move.purchase_line_id.id] += qty_done partner_dict = {}