[FIX] purchase_stock: _compute_on_time_rate optimizations

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) <whe@odoo.com>
Signed-off-by: Van Delft Aurélien (avd) <avd@odoo.com>
This commit is contained in:
Aurelien van Delft (avd)
2023-05-24 13:10:51 +02:00
parent 9d6086186b
commit a55a08ef56
+8 -3
View File
@@ -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 = {}