(self)
This method will try to apply the procure method MTO on some moves if
        a compatible MTO route is found. Else the procure method will be set to MTS
This method will try to apply the procure method MTO on some moves if
        a compatible MTO route is found. Else the procure method will be set to MTS
This method will try to apply the procure method MTO on some moves if a compatible MTO route is found . Else the procure method will be set to MTS
def _adjust_procure_method(self):
        """ This method will try to apply the procure method MTO on some moves if
        a compatible MTO route is found. Else the procure method will be set to MTS
        """
        # Prepare the MTSO variables. They are needed since MTSO moves are handled separately.
        # We need 2 dicts:
        # - needed quantity per location per product
        # - forecasted quantity per location per product
        mtso_products_by_locations = defaultdict(list)
        mtso_needed_qties_by_loc = defaultdict(dict)
        mtso_free_qties_by_loc = {}
        mtso_moves = self.env['stock.move']

        for move in self:
            product_id = move.product_id
            domain = [
                ('location_src_id', '=', move.location_id.id),
                ('location_id', '=', move.location_dest_id.id),
                ('action', '!=', 'push')
            ]
            rules = self.env['procurement.group']._search_rule(False, move.product_packaging_id, product_id, move.warehouse_id, domain)
            if rules:
                if rules.procure_method in ['make_to_order', 'make_to_stock']:
                    move.procure_method = rules.procure_method
                else:
                    # Get the needed quantity for the `mts_else_mto` moves.
                    mtso_needed_qties_by_loc[rules.location_src_id].setdefault(product_id.id, 0)
                    mtso_needed_qties_by_loc[rules.location_src_id][product_id.id] += move.product_qty

                    # This allow us to get the forecasted quantity in batch later on
                    mtso_products_by_locations[rules.location_src_id].append(product_id.id)
                    mtso_moves |= move
            else:
                move.procure_method = 'make_to_stock'

        # Get the forecasted quantity for the `mts_else_mto` moves.
        for location, product_ids in mtso_products_by_locations.items():
            products = self.env['product.product'].browse(product_ids).with_context(location=location.id)
            mtso_free_qties_by_loc[location] = {product.id: product.free_qty for product in products}

        # Now that we have the needed and forecasted quantity per location and per product, we can
        # choose whether the mtso_moves need to be MTO or MTS.
        for move in mtso_moves:
            needed_qty = move.product_qty
            forecasted_qty = mtso_free_qties_by_loc[move.location_id][move.product_id.id]
            if float_compare(needed_qty, forecasted_qty, precision_rounding=product_id.uom_id.rounding) <= 0:
                move.procure_method = 'make_to_stock'
                mtso_free_qties_by_loc[move.location_id][move.product_id.id] -= needed_qty
            else:
                move.procure_method = 'make_to_order'
def _adjust_procure_method ( self ) : # Prepare the MTSO variables. They are needed since MTSO moves are handled separately. # We need 2 dicts: # - needed quantity per location per product # - forecasted quantity per location per product mtso_products_by_locations = defaultdict ( list ) mtso_needed_qties_by_loc = defaultdict ( dict ) mtso_free_qties_by_loc = { } mtso_moves = self . env [ 'stock.move' ] for move in self : product_id = move . product_id domain = [ ( 'location_src_id' , '=' , move . location_id . id ) , ( 'location_id' , '=' , move . location_dest_id . id ) , ( 'action' , '!=' , 'push' ) ] rules = self . env [ 'procurement.group' ] . _search_rule ( False , move . product_packaging_id , product_id , move . warehouse_id , domain ) if rules : if rules . procure_method in [ 'make_to_order' , 'make_to_stock' ] : move . procure_method = rules . procure_method else : # Get the needed quantity for the `mts_else_mto` moves. mtso_needed_qties_by_loc [ rules . location_src_id ] . setdefault ( product_id . id , 0 ) mtso_needed_qties_by_loc [ rules . location_src_id ] [ product_id . id ] += move . product_qty # This allow us to get the forecasted quantity in batch later on mtso_products_by_locations [ rules . location_src_id ] . append ( product_id . id ) mtso_moves |= move else : move . procure_method = 'make_to_stock' # Get the forecasted quantity for the `mts_else_mto` moves. for location , product_ids in mtso_products_by_locations . items ( ) : products = self . env [ 'product.product' ] . browse ( product_ids ) . with_context ( location = location . id ) mtso_free_qties_by_loc [ location ] = { product . id : product . free_qty for product in products } # Now that we have the needed and forecasted quantity per location and per product, we can # choose whether the mtso_moves need to be MTO or MTS. for move in mtso_moves : needed_qty = move . product_qty forecasted_qty = mtso_free_qties_by_loc [ move . location_id ] [ move . product_id . id ] if float_compare ( needed_qty , forecasted_qty , precision_rounding = product_id . uom_id . rounding ) <= 0 : move . procure_method = 'make_to_stock' mtso_free_qties_by_loc [ move . location_id ] [ move . product_id . id ] -= needed_qty else : move . procure_method = 'make_to_order'