From c79a90244fe318bb41d6c5194340839f045f9b88 Mon Sep 17 00:00:00 2001 From: BernatPForgeFlow Date: Wed, 1 Mar 2023 11:15:00 +0100 Subject: [PATCH] [IMP] mrp_multi_level: Get BoM to explode When exploding requirements or calculating supply method, we will consider the first active BoM taking into account the routes. --- mrp_multi_level/models/product_mrp_area.py | 7 +- mrp_multi_level/wizards/mrp_multi_level.py | 80 ++++++++++++---------- 2 files changed, 49 insertions(+), 38 deletions(-) diff --git a/mrp_multi_level/models/product_mrp_area.py b/mrp_multi_level/models/product_mrp_area.py index 0b72d08eb..02df170db 100644 --- a/mrp_multi_level/models/product_mrp_area.py +++ b/mrp_multi_level/models/product_mrp_area.py @@ -205,11 +205,12 @@ class ProductMRPArea(models.Model): break rule = new_rule # Determine the supply method based on the final rule. + boms = rec.product_id.product_tmpl_id.bom_ids.filtered( + lambda x: x.type in ["normal", "phantom"] + ) rec.supply_method = ( "phantom" - if rule.action == "manufacture" - and rec.product_id.product_tmpl_id.bom_ids - and rec.product_id.product_tmpl_id.bom_ids[0].type == "phantom" + if rule.action == "manufacture" and boms and boms[0].type == "phantom" else rule.action ) diff --git a/mrp_multi_level/wizards/mrp_multi_level.py b/mrp_multi_level/wizards/mrp_multi_level.py index dcdea77ee..7adac9399 100644 --- a/mrp_multi_level/wizards/mrp_multi_level.py +++ b/mrp_multi_level/wizards/mrp_multi_level.py @@ -8,6 +8,7 @@ import logging from datetime import date, timedelta from odoo import _, api, exceptions, fields, models +from odoo.tools import float_is_zero logger = logging.getLogger(__name__) @@ -192,6 +193,17 @@ class MultiLevelMrp(models.TransientModel): mrp_action_date = mrp_date - timedelta(days=product_mrp_area.mrp_lead_time) return mrp_action_date, mrp_date_supply + @api.model + def _get_bom_to_explode(self, product_mrp_area_id): + boms = self.env["mrp.bom"] + if product_mrp_area_id.supply_method in ["manufacture", "phantom"]: + boms = product_mrp_area_id.product_id.bom_ids.filtered( + lambda x: x.type in ["normal", "phantom"] + ) + if not boms: + return False + return boms[0] + @api.model def explode_action( self, product_mrp_area_id, mrp_action_date, name, qty, action, values=None @@ -200,45 +212,43 @@ class MultiLevelMrp(models.TransientModel): mrp_date_demand = mrp_action_date if mrp_date_demand < date.today(): mrp_date_demand = date.today() - if not product_mrp_area_id.product_id.bom_ids: + bom = self._get_bom_to_explode(product_mrp_area_id) + if not bom: return False - bomcount = 0 - for bom in product_mrp_area_id.product_id.bom_ids: - if not bom.active or not bom.bom_line_ids: + pd = self.env["decimal.precision"].precision_get("Product Unit of Measure") + for bomline in bom.bom_line_ids: + if ( + float_is_zero(bomline.product_qty, precision_digits=pd) + or bomline.product_id.type != "product" + ): continue - bomcount += 1 - if bomcount != 1: + if self.with_context(mrp_explosion=True)._exclude_from_mrp( + bomline.product_id, product_mrp_area_id.mrp_area_id + ): + # Stop explosion. continue - for bomline in bom.bom_line_ids: - if bomline.product_qty <= 0.00 or bomline.product_id.type != "product": - continue - if self.with_context(mrp_explosion=True)._exclude_from_mrp( - bomline.product_id, product_mrp_area_id.mrp_area_id - ): - # Stop explosion. - continue - if bomline._skip_bom_line(product_mrp_area_id.product_id): - continue - # TODO: review: mrp_transit_delay, mrp_inspection_delay - mrp_date_demand_2 = mrp_date_demand - timedelta( - days=( - product_mrp_area_id.mrp_transit_delay - + product_mrp_area_id.mrp_inspection_delay - ) + if bomline._skip_bom_line(product_mrp_area_id.product_id): + continue + # TODO: review: mrp_transit_delay, mrp_inspection_delay + mrp_date_demand_2 = mrp_date_demand - timedelta( + days=( + product_mrp_area_id.mrp_transit_delay + + product_mrp_area_id.mrp_inspection_delay ) - move_data = self._prepare_mrp_move_data_bom_explosion( - product_mrp_area_id, - bomline, - qty, - mrp_date_demand_2, - bom, - name, - action, - values, - ) - mrpmove_id2 = self.env["mrp.move"].create(move_data) - if hasattr(action, "mrp_move_down_ids"): - action.mrp_move_down_ids = [(4, mrpmove_id2.id)] + ) + move_data = self._prepare_mrp_move_data_bom_explosion( + product_mrp_area_id, + bomline, + qty, + mrp_date_demand_2, + bom, + name, + action, + values, + ) + mrpmove_id2 = self.env["mrp.move"].create(move_data) + if hasattr(action, "mrp_move_down_ids"): + action.mrp_move_down_ids = [(4, mrpmove_id2.id)] return True @api.model