diff --git a/addons/mrp/report/mrp_report_bom_structure.py b/addons/mrp/report/mrp_report_bom_structure.py index a64869dabf1..91c8344e3eb 100644 --- a/addons/mrp/report/mrp_report_bom_structure.py +++ b/addons/mrp/report/mrp_report_bom_structure.py @@ -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] diff --git a/addons/mrp_subcontracting/report/mrp_report_bom_structure.py b/addons/mrp_subcontracting/report/mrp_report_bom_structure.py index 8320db56c85..a4c1eeddddc 100644 --- a/addons/mrp_subcontracting/report/mrp_report_bom_structure.py +++ b/addons/mrp_subcontracting/report/mrp_report_bom_structure.py @@ -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)