microsoft / ALAppExtensions

Repository for collaboration on Microsoft AL application add-on and localization extensions for Microsoft Dynamics 365 Business Central.
MIT License
792 stars 622 forks source link

[Event Request] CalcNoOfReservedFromStockSalesOrders / DrillDownNoOfReservedFromStockSalesOrders #27651

Open fvet opened 1 week ago

fvet commented 1 week ago

Describe the request

In #27592, a solution has been added to overrule the CalcNoOfReservedFromStockSalesOrders. I think this request should be re-evaluated and adopt the same pattern on some other places too:

Image


However, it would be better if the code can be refactored to avoid code cloning and provide one single place where filters are placed on the SalesHeader, allowing us to use one single event publisher to include additional filters on the SalesHeader and exclude certain records. (e.g. those Completely Shipped,

Whether Sales Cue of Activities Mgt. is used, is up to MS.

Possible code suggestion:

#region CalcNoOfReservedFromStockSalesOrders
     procedure CalcNoOfReservedFromStockSalesOrders() : Integer
    var
        [SecurityFiltering(SecurityFilter::Filtered)]
        SalesHeader: Record "Sales Header";
    begin
        exit(CalcNoOfReservedFromStockSalesOrders(SalesHeader)); // Moved the SalesHeader filter logic to a separate function
    end;
    #endregion CalcNoOfReservedFromStockSalesOrders

     #region DrillDownNoOfReservedFromStockSalesOrders
     procedure DrillDownNoOfReservedFromStockSalesOrders()
    var
        [SecurityFiltering(SecurityFilter::Filtered)]
        SalesHeader: Record "Sales Header";
    begin
        CalcNoOfReservedFromStockSalesOrders(SalesHeader);  // Moved the SalesHeader filter logic to a separate function

        SalesHeader.MarkedOnly(true);
        Page.Run(Page::"Sales Order List", SalesHeader);
    end;
    #endregion DrillDownNoOfReservedFromStockSalesOrders

    #region CalcNoOfReservedFromStockSalesOrders
    local procedure CalcNoOfReservedFromStockSalesOrders(var SalesHeader: Record "Sales Header") Number: Integer
    begin                        
        SalesHeader.SetRange("Document Type", SalesHeader."Document Type"::Order);
        SalesHeader.SetLoadFields("Document Type", "No.");

        OnCalcNoOfReservedFromStockSalesOrders_OnBeforeSetFilters(SalesHeader); // Added publisher to allow custom filtering

        if SalesHeader.FindSet() then
            repeat
                if SalesHeader.GetQtyReservedFromStockState() = Enum::"Reservation From Stock"::Full then begin
                    Number += 1;
                    SalesHeader.Mark(true);
                end;
            until SalesHeader.Next() = 0;
    end;
    #endregion CalcNoOfReservedFromStockSalesOrders

    #region OnCalcNoOfReservedFromStockSalesOrders_OnBeforeSetFilters
    [IntegrationEvent(false, false)]
    local procedure OnCalcNoOfReservedFromStockSalesOrders_OnBeforeSetFilters(var SalesHeader: Record "Sales Header")
    begin
       // Supports adding filters on one place only, allowing the calculation and drilldown to be correct at all places (Sales Cue / Act. Mgt)
    end;
    #endregion OnCalcNoOfReservedFromStockSalesOrders_OnBeforeSetFilters

Additional context

x Internal work item: AB#558152