diff options
| author | Ivan Yelizariev <iel@odoo.com> | 2021-04-09 12:58:32 +0000 |
|---|---|---|
| committer | Ivan Yelizariev <iel@odoo.com> | 2021-04-13 13:48:37 +0000 |
| commit | 14abcfa92cd37db82338f9a933221142026a78bd (patch) | |
| tree | a7398d8003cad7cbe5d9b806eac26c4f039877fc /addons | |
| parent | 7cd23a2cd464ccdfcb028b4f2c948654634b973c (diff) | |
[FIX] stock_account: decrease number of queries in _run_fifo_vacuum
_run_fifo_vacuum may take significant time. This will slow down any operations
related to stock (Inventory Adjustment, POS Session closing, etc.) or even
block them due to timeout error. So, decreasing execution time is quite
desirable. This commit is doing it by decreasing number queries:
1. prefetch all_candidates before for-loop to avoid using `search` in each
iteration
2. move code about updating standard_price after the for-loop
---
opw-2499032
closes odoo/odoo#69040
Signed-off-by: William Henrotin <Whenrow@users.noreply.github.com>
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/stock_account/models/product.py | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/addons/stock_account/models/product.py b/addons/stock_account/models/product.py index 11c56c1d604..278507aad06 100644 --- a/addons/stock_account/models/product.py +++ b/addons/stock_account/models/product.py @@ -376,18 +376,24 @@ class ProductProduct(models.Model): ('stock_move_id', '!=', False), ('company_id', '=', company.id), ], order='create_date, id') + if not svls_to_vacuum: + return + + domain = [ + ('company_id', '=', company.id), + ('product_id', '=', self.id), + ('remaining_qty', '>', 0), + ('create_date', '>=', svls_to_vacuum[0].create_date), + ] + all_candidates = self.env['stock.valuation.layer'].sudo().search(domain) + for svl_to_vacuum in svls_to_vacuum: - domain = [ - ('company_id', '=', svl_to_vacuum.company_id.id), - ('product_id', '=', self.id), - ('remaining_qty', '>', 0), - '|', - ('create_date', '>', svl_to_vacuum.create_date), - '&', - ('create_date', '=', svl_to_vacuum.create_date), - ('id', '>', svl_to_vacuum.id) - ] - candidates = self.env['stock.valuation.layer'].sudo().search(domain) + # We don't use search to avoid executing _flush_search and to decrease interaction with DB + candidates = all_candidates.filtered( + lambda r: r.create_date > svl_to_vacuum.create_date + or r.create_date == svl_to_vacuum.create_date + and r.id > svl_to_vacuum.id + ) if not candidates: break qty_to_take_on_candidates = abs(svl_to_vacuum.remaining_qty) @@ -407,6 +413,8 @@ class ProductProduct(models.Model): 'remaining_value': new_remaining_value } candidate.write(candidate_vals) + if not (candidate.remaining_qty > 0): + all_candidates -= candidate qty_to_take_on_candidates -= qty_taken_on_candidate tmp_value += value_taken_on_candidate @@ -440,11 +448,6 @@ class ProductProduct(models.Model): } vacuum_svl = self.env['stock.valuation.layer'].sudo().create(vals) - # If some negative stock were fixed, we need to recompute the standard price. - product = self.with_company(company.id) - if product.cost_method == 'average' and not float_is_zero(product.quantity_svl, precision_rounding=self.uom_id.rounding): - product.sudo().with_context(disable_auto_svl=True).write({'standard_price': product.value_svl / product.quantity_svl}) - # Create the account move. if self.valuation != 'real_time': continue @@ -454,6 +457,12 @@ class ProductProduct(models.Model): # Create the related expense entry self._create_fifo_vacuum_anglo_saxon_expense_entry(vacuum_svl, svl_to_vacuum) + # If some negative stock were fixed, we need to recompute the standard price. + product = self.with_company(company.id) + if product.cost_method == 'average' and not float_is_zero(product.quantity_svl, precision_rounding=self.uom_id.rounding): + product.sudo().with_context(disable_auto_svl=True).write({'standard_price': product.value_svl / product.quantity_svl}) + + def _create_fifo_vacuum_anglo_saxon_expense_entry(self, vacuum_svl, svl_to_vacuum): """ When product is delivered and invoiced while you don't have units in stock anymore, there are chances of that product getting undervalued/overvalued. So, we should nevertheless take into account the fact that the product has |
