Debian Codegraph @

addons/website_sale_mrp/models/sale_order.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from collections import defaultdict
4
5 from odoo import models
6 from odoo.tools import float_is_zero
7
8
9 class SaleOrder(models.Model):
10 _inherit = 'sale.order'
11
12 def _get_unavailable_quantity_from_kits(self, product):
13 """
14 If any line of the order refers to a kit product, the availability of the product
15 might be impacted (if the product is a kit or a component of one).
16
17 This method computes the quantity that becomes unavailable for the product because
18 of the order lines that do not refer to it directly.
19
20 :param ProductProduct product: the product for which the unavailability is computed.
21 """
22 self.ensure_one()
23 unavailable_qty = 0
24 if product.is_kits:
25 # Explode the kit to fetch the set of relevant components to track.
26 kit_bom = self.env['mrp.bom'].sudo()._bom_find(product, company_id=self.company_id.id, bom_type='phantom')[product]
27 _, bom_sub_lines = kit_bom.explode(product, quantity=1.0)
28 unavailable_component_qties = {}
29 qty_per_kit = defaultdict(float)
30 for bom_line, bom_line_data in bom_sub_lines:
31 if not bom_line.product_id.is_storable:
32 # Relevant only for storable components.
33 continue
34 if float_is_zero(bom_line_data['qty'], precision_rounding=bom_line.product_uom_id.rounding):
35 # As BoMs allow components with a quantity of 0 (i.e., optional components), we
36 # skip those to avoid a division by zero.
37 continue
38 component = bom_line.product_id
39 unavailable_component_qties[component] = sum(self.order_line.filtered(lambda sol: sol.product_id == component).mapped('product_uom_qty'))
40 uom_qty_per_kit = bom_line_data['qty'] / bom_line_data['original_qty']
41 qty_per_kit[component] += bom_line.product_uom_id._compute_quantity(uom_qty_per_kit / kit_bom.product_qty, component.uom_id, round=False)
42
43 for line in self.order_line:
44 if not line.product_id.is_kits or line.product_id == product:
45 continue
46 # Other kit lines might influence the availability of the product.
47 line_kit_bom = self.env['mrp.bom'].sudo()._bom_find(line.product_id, company_id=self.company_id.id, bom_type='phantom')[line.product_id]
48 component_qties = line._get_bom_component_qty(line_kit_bom)
49 unavailable_qty += component_qties.get(product.id, {}).get('qty', 0) * line.product_uom_qty / line_kit_bom.product_qty
50 if product.is_kits:
51 # If the product is a kit, the availability of its components can be influenced by other kits.
52 for component, _ in unavailable_component_qties.items():
53 unavailable_component_qties[component] += component_qties.get(component.id, {}).get('qty', 0) * line.product_uom_qty / line_kit_bom.product_qty
54
55 if product.is_kits:
56 # If the product is a kit, recompute availability based on the availability of its components.
57 max_free_kit_qty = free_qty = product.sudo().free_qty
58 for component, unavailable_component_qty in unavailable_component_qties.items():
59 max_free_kit_qty = min(max_free_kit_qty, (component.free_qty - unavailable_component_qty) // qty_per_kit[component])
60 unavailable_qty += free_qty - max_free_kit_qty
61 return unavailable_qty