Skip to content
Snippets Groups Projects
Commit feeef868 authored by clesgow's avatar clesgow
Browse files

[FIX] mrp{,_subcontracting}: Detect subcontracting rules

Before this commit, the rules were checked related to the currently
selected warehouse. Doing this didn't allow to check the subcontracted
locations for their related rules (like Drophip subcontractor on order).

The point here is that when no rules are found within the warehouse, it
checks the subcontracted location of the parent if the parent is
subcontracted itself.

Part of task-2985735

Part-of: odoo/odoo#104893
parent c855038c
No related branches found
No related tags found
No related merge requests found
......@@ -160,7 +160,7 @@ class ReportBomStructure(models.AbstractModel):
bom_key = bom.id
if not product_info[key].get(bom_key):
product_info[key][bom_key] = self._get_resupply_route_info(warehouse, product, current_quantity, bom)
product_info[key][bom_key] = self.with_context(product_info=product_info, parent_bom=parent_bom)._get_resupply_route_info(warehouse, product, current_quantity, bom)
route_info = product_info[key].get(bom_key, {})
quantities_info = {}
if not ignore_stock:
......@@ -250,7 +250,7 @@ class ReportBomStructure(models.AbstractModel):
bom_key = 'no_bom'
if not product_info[key].get(bom_key):
product_info[key][bom_key] = self._get_resupply_route_info(warehouse, bom_line.product_id, line_quantity)
product_info[key][bom_key] = self.with_context(product_info=product_info, parent_bom=parent_bom)._get_resupply_route_info(warehouse, bom_line.product_id, line_quantity)
route_info = product_info[key].get(bom_key, {})
quantities_info = {}
......@@ -469,12 +469,24 @@ class ReportBomStructure(models.AbstractModel):
@api.model
def _get_resupply_route_info(self, warehouse, product, quantity, bom=False):
found_rules = product._get_rules_from_location(warehouse.lot_stock_id)
found_rules = []
if self._need_special_rules(self.env.context.get('product_info'), self.env.context.get('parent_bom'), self.env.context.get('parent_product_id')):
found_rules = self._find_special_rules(product, self.env.context.get('product_info'), self.env.context.get('parent_bom'), self.env.context.get('parent_product_id'))
if not found_rules:
found_rules = product._get_rules_from_location(warehouse.lot_stock_id)
if not found_rules:
return {}
rules_delay = sum(rule.delay for rule in found_rules)
return self._format_route_info(found_rules, rules_delay, warehouse, product, bom, quantity)
@api.model
def _need_special_rules(self, product_info, parent_bom=False, parent_product_id=False):
return False
@api.model
def _find_special_rules(self, product, product_info, parent_bom=False, parent_product_id=False):
return False
@api.model
def _format_route_info(self, rules, rules_delay, warehouse, product, bom, quantity):
manufacture_rules = [rule for rule in rules if rule.action == 'manufacture' and bom]
......
......@@ -45,6 +45,24 @@ class ReportBomStructure(models.AbstractModel):
})
return lines
@api.model
def _need_special_rules(self, product_info, parent_bom=False, parent_product_id=False):
if parent_bom and parent_product_id:
parent_info = product_info.get(parent_product_id, {}).get(parent_bom.id, {})
return parent_info and parent_info.get('route_type') == 'subcontract'
return super()._need_special_rules(product_info, parent_bom, parent_product_id)
@api.model
def _find_special_rules(self, product, product_info, parent_bom=False, parent_product_id=False):
res = super()._find_special_rules(product, product_info, parent_bom, parent_product_id)
# If no rules could be found within the warehouse, check if the product is a component from a subcontracted product.
parent_info = product_info.get(parent_product_id, {}).get(parent_bom.id, {})
if parent_info and parent_info.get('route_type') == 'subcontract':
# Since the product is subcontracted, check the subcontracted location for rules instead of the warehouse.
subcontracting_loc = parent_info['supplier'].partner_id.property_stock_subcontractor
return product._get_rules_from_location(subcontracting_loc)
return res
@api.model
def _format_route_info(self, rules, rules_delay, warehouse, product, bom, quantity):
res = super()._format_route_info(rules, rules_delay, warehouse, product, bom, quantity)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment