diff options
| author | William Henrotin <whe@odoo.com> | 2021-05-04 15:54:22 +0000 |
|---|---|---|
| committer | William Henrotin <whe@odoo.com> | 2021-05-06 13:38:11 +0000 |
| commit | 363c1916c40d0a85ea4d8e08223b78a4ef30d61a (patch) | |
| tree | e84b802b6237983ea77835de7c4407956e1210e9 /addons | |
| parent | c449fcb6274c9e68d4470423b5e2d91df3566615 (diff) | |
[FIX] stock, purchase_stock: read virtual_available in batch
Performance improvement for missing products computation.
- Call _prepare_sellers only once
- _compute_quantities works in batch for product with the same
context. Use this advantage to speedup the computation of virtual
available for each warehouse and delivery date.
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/purchase_stock/models/stock_rule.py | 5 | ||||
| -rw-r--r-- | addons/stock/models/stock_orderpoint.py | 23 |
2 files changed, 19 insertions, 9 deletions
diff --git a/addons/purchase_stock/models/stock_rule.py b/addons/purchase_stock/models/stock_rule.py index 51c68706d31..f6a14616e50 100644 --- a/addons/purchase_stock/models/stock_rule.py +++ b/addons/purchase_stock/models/stock_rule.py @@ -149,10 +149,11 @@ class StockRule(models.Model): """ delay, delay_description = super()._get_lead_days(product) buy_rule = self.filtered(lambda r: r.action == 'buy') - if not buy_rule or not product._prepare_sellers(): + seller = product._prepare_sellers() + if not buy_rule or not seller: return delay, delay_description buy_rule.ensure_one() - supplier_delay = product._prepare_sellers()[0].delay + supplier_delay = seller[0].delay if supplier_delay: delay_description += '<tr><td>%s</td><td class="text-right">+ %d %s</td></tr>' % (_('Vendor Lead Time'), supplier_delay, _('day(s)')) security_delay = buy_rule.picking_type_id.company_id.po_lead diff --git a/addons/stock/models/stock_orderpoint.py b/addons/stock/models/stock_orderpoint.py index d45d3d72d24..c2b61ea4771 100644 --- a/addons/stock/models/stock_orderpoint.py +++ b/addons/stock/models/stock_orderpoint.py @@ -319,19 +319,28 @@ class StockWarehouseOrderpoint(models.Model): # Recompute the forecasted quantity for missing product today but at this time # with their real lead days. key_to_remove = [] + + # group product by lead_days and warehouse in order to read virtual_available + # in batch + pwh_per_day = defaultdict(list) for (product, warehouse), quantity in to_refill.items(): product = self.env['product.product'].browse(product) warehouse = self.env['stock.warehouse'].browse(warehouse) rules = product._get_rules_from_location(warehouse.lot_stock_id) lead_days = rules._get_lead_days(product)[0] - virtual_available = product.with_context( + pwh_per_day[(lead_days, warehouse)].append(product.id) + for (days, warehouse), p_ids in pwh_per_day.items(): + products = self.env['product.product'].browse(p_ids) + qties = products.with_context( warehouse=warehouse.id, - to_date=fields.datetime.now() + relativedelta.relativedelta(days=lead_days) - ).virtual_available - if float_compare(virtual_available, 0, precision_rounding=product.uom_id.rounding) >= 0: - key_to_remove.append((product.id, warehouse.id)) - else: - to_refill[(product.id, warehouse.id)] = virtual_available + to_date=fields.datetime.now() + relativedelta.relativedelta(days=days) + ).read(['virtual_available']) + for qty in qties: + if float_compare(qty['virtual_available'], 0, precision_rounding=product.uom_id.rounding) >= 0: + key_to_remove.append((qty['id'], warehouse.id)) + else: + to_refill[(qty['id'], warehouse.id)] = qty['virtual_available'] + for key in key_to_remove: del to_refill[key] if not to_refill: |
