Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Event Request] Codeunit 5870 "Calculate BOM Tree" - Procedure CalcAvailability - OnBeforeCheckAvailability #28024

Open
mavohra opened this issue Jan 30, 2025 · 0 comments

Comments

@mavohra
Copy link

mavohra commented Jan 30, 2025

Describe the request

Please add an event OnBeforeCheckAvailability in the procedure CalcAvailability in Codeunit 5870 "Calculate BOM Tree"

 local procedure CalcAvailability(var BOMBuffer: Record "BOM Buffer"; Input: Decimal; IsTest: Boolean): Boolean
    var
        ParentBOMBuffer: Record "BOM Buffer";
        ExpectedQty: Decimal;
        AvailQty: Decimal;
        MaxTime: Integer;
        IsHandled: Boolean;
    begin
        if BOMBuffer.Indentation = 0 then begin
            if IsTest then
                if TempMemoizedResult.Get(Input) then
                    exit(TempMemoizedResult.Output);

            ResetUpdatedAvailability();
        end;

        MaxTime := 0;
        ParentBOMBuffer := BOMBuffer;
        while (BOMBuffer.Next() <> 0) and (ParentBOMBuffer.Indentation < BOMBuffer.Indentation) do
            if ParentBOMBuffer.Indentation + 1 = BOMBuffer.Indentation then begin
                TempItemAvailByDate.SetRange("Item No.", BOMBuffer."No.");
                TempItemAvailByDate.SetRange(Date, BOMBuffer."Needed by Date");
                TempItemAvailByDate.SetRange("Variant Code", BOMBuffer."Variant Code");
                if LocationSpecific then
                    TempItemAvailByDate.SetRange("Location Code", BOMBuffer."Location Code");
                TempItemAvailByDate.FindFirst();
                if BOMBuffer."Calculation Formula" = BOMBuffer."Calculation Formula"::"Fixed Quantity" then begin
                    ExpectedQty := Round(BOMBuffer."Qty. per Parent", UOMMgt.QtyRndPrecision());
                    AvailQty := TempItemAvailByDate."Available Qty"
                end
                else begin
                    ExpectedQty := Round(BOMBuffer."Qty. per Parent" * Input, UOMMgt.QtyRndPrecision());
                    AvailQty := TempItemAvailByDate."Updated Available Qty";
                end;

               
                OnBeforeCheckAvailability(BOMBuffer, AvailQty, ExpectedQty, IsHandled); // <--- New Event
                if IsHandled then
                    exit;

                if AvailQty < ExpectedQty then begin // <---- This needs to be AvailQty <= ExpectedQty
                    if BOMBuffer."Is Leaf" then begin
                        if MarkBottleneck then begin
                            BOMBuffer.Bottleneck := true;
                            BOMBuffer.Modify(true);
                        end;
                        BOMBuffer := ParentBOMBuffer;
                        if (BOMBuffer.Indentation = 0) and IsTest then
                            AddMemoizedResult(Input, false);
                        exit(false);
                    end;
                    if AvailQty <> 0 then
                        ReduceAvailability(BOMBuffer."No.", BOMBuffer."Variant Code", BOMBuffer."Location Code", BOMBuffer."Needed by Date", AvailQty, BOMBuffer."Calculation Formula");
                    if not IsTest then begin
                        BOMBuffer."Available Quantity" := AvailQty;
                        BOMBuffer.Modify();
                    end;
                    if not CalcAvailability(BOMBuffer, ExpectedQty - AvailQty, IsTest) then begin
                        if MarkBottleneck then begin
                            BOMBuffer.Bottleneck := true;
                            BOMBuffer.Modify(true);
                        end;
                        BOMBuffer := ParentBOMBuffer;
                        if (BOMBuffer.Indentation = 0) and IsTest then
                            AddMemoizedResult(Input, false);
                        exit(false);
                    end;
                    if not IsTest then
                        if MaxTime < (ParentBOMBuffer."Needed by Date" - BOMBuffer."Needed by Date") + BOMBuffer."Rolled-up Lead-Time Offset" then
                            MaxTime := (ParentBOMBuffer."Needed by Date" - BOMBuffer."Needed by Date") + BOMBuffer."Rolled-up Lead-Time Offset";
                end else begin
                    if not IsTest then begin
                        if BOMBuffer."Calculation Formula" <> BOMBuffer."Calculation Formula"::"Fixed Quantity" then begin
                            BOMBuffer."Available Quantity" := ExpectedQty;
                            BOMBuffer.Modify();
                        end;
                        if MaxTime < (ParentBOMBuffer."Needed by Date" - BOMBuffer."Needed by Date") + BOMBuffer."Rolled-up Lead-Time Offset" then
                            MaxTime := (ParentBOMBuffer."Needed by Date" - BOMBuffer."Needed by Date") + BOMBuffer."Rolled-up Lead-Time Offset";
                    end;
                    ReduceAvailability(BOMBuffer."No.", BOMBuffer."Variant Code", BOMBuffer."Location Code", BOMBuffer."Needed by Date", ExpectedQty, BOMBuffer."Calculation Formula");
                end;
            end;
        BOMBuffer := ParentBOMBuffer;
        BOMBuffer."Rolled-up Lead-Time Offset" := MaxTime;
        BOMBuffer.Modify(true);
        if (BOMBuffer.Indentation = 0) and IsTest then
            AddMemoizedResult(Input, true);
        exit(true);
    end;
   [IntegrationEvent(false, false)]
    local procedure OnBeforeCheckAvailability(var BOMBuffer: Record "BOM Buffer"; var AvailQty: Decimal; var ExpectedQty: Decimal; var IsHandled: Boolean)
    begin
    end;

Additional context

We need to replace the base logic if AvailQty < ExpectedQty with our custom logic if AvailQty <= ExpectedQty at line No. 802 in the procedure CalcAvailability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant