Skip to content

Commit

Permalink
[PERF] mrp, *: add checks to _compute_days_to_order
Browse files Browse the repository at this point in the history
In both mrp and purchase_stock, `_compute_days_to_order` does some computation
when the rule_ids' action is 'manufacture' and 'buy' respectively. This forces
the computation of rule_ids on all orderpoints in self. This commit adds
some prechecks and prefiltering on self to reduce the size
of the recordset that needs the value of rule_ids.

Part-of: odoo#183964
Signed-off-by: William Henrotin (whe) <[email protected]>
  • Loading branch information
Aurelienvd committed Nov 12, 2024
1 parent 370f3bc commit 88be692
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion addons/mrp/models/stock_orderpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ def _set_visibility_days(self):

def _compute_days_to_order(self):
res = super()._compute_days_to_order()
for orderpoint in self:
# Avoid computing rule_ids in case no manufacture rules.
if not self.env['stock.rule'].search([('action', '=', 'manufacture')]):
return res
# Compute rule_ids only for orderpoint with boms
orderpoints_with_bom = self.filtered(lambda orderpoint: orderpoint.product_id.variant_bom_ids or orderpoint.product_id.bom_ids)
for orderpoint in orderpoints_with_bom:
if 'manufacture' in orderpoint.rule_ids.mapped('action'):
boms = (orderpoint.product_id.variant_bom_ids or orderpoint.product_id.bom_ids)
orderpoint.days_to_order = boms and boms[0].days_to_prepare_mo or 0
Expand Down
7 changes: 6 additions & 1 deletion addons/purchase_stock/models/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ def _set_visibility_days(self):

def _compute_days_to_order(self):
res = super()._compute_days_to_order()
for orderpoint in self:
# Avoid computing rule_ids if no stock.rules with the buy action
if not self.env['stock.rule'].search([('action', '=', 'buy')]):
return res
# Compute rule_ids only for orderpoint whose compnay_id.days_to_purchase != orderpoint.days_to_order
orderpoints_to_compute = self.filtered(lambda orderpoint: orderpoint.days_to_order != orderpoint.company_id.days_to_purchase)
for orderpoint in orderpoints_to_compute:
if 'buy' in orderpoint.rule_ids.mapped('action'):
orderpoint.days_to_order = orderpoint.company_id.days_to_purchase
return res
Expand Down

0 comments on commit 88be692

Please sign in to comment.