Skip to content

[W1][Codeunit][5804][ItemCostManagement] Add integration event OnCalcLastAdjEntryAvgCostOnBeforeComputeAverageCost in CalcLastAdjEntryAvgCost before average cost computation #30152

@mavohra

Description

@mavohra

Why do you need this change?

We need to modify the average cost computation logic in CalcLastAdjEntryAvgCost in Codeunit 5804 ItemCostManagement to use an alternate costing quantity as the divisor instead of ItemLedgEntry.Quantity and to add a guard against zero costing quantity. The standard procedure divides the summed cost amounts by ItemLedgEntry.Quantity unconditionally, but when the costing quantity differs from the inventory quantity, this produces an incorrect average cost.

Describe the request

Add an integration event OnCalcLastAdjEntryAvgCostOnBeforeComputeAverageCost in CalcLastAdjEntryAvgCost in Codeunit 5804 ItemCostManagement after the IsSubOptimal computation and before the average cost calculation block.

                if ComputeThisEntry then begin
                    ItemLedgEntry.Get(ValueEntry."Item Ledger Entry No.");
                    IsSubOptimal :=
                      ItemLedgEntry.Correction or
                      ((Item."Costing Method" = Item."Costing Method"::Average) and not ValueEntry."Valued By Average Cost");

                    IsHandled := false;
                    OnCalcLastAdjEntryAvgCostOnBeforeComputeAverageCost( // <---- New Event
                      ItemLedgEntry, ValueEntry, Item, AverageCost, AverageCostACY, IsSubOptimal, IsHandled);
                    if not IsHandled then begin
                    if not IsSubOptimal or (IsSubOptimal and (AverageCost = 0)) then begin
                        ItemLedgEntry.CalcFields(
                          "Cost Amount (Expected)", "Cost Amount (Actual)",
                          "Cost Amount (Expected) (ACY)", "Cost Amount (Actual) (ACY)");
                        AverageCost :=
                          (ItemLedgEntry."Cost Amount (Expected)" +
                           ItemLedgEntry."Cost Amount (Actual)") /
                          ItemLedgEntry.Quantity;
                        AverageCostACY :=
                          (ItemLedgEntry."Cost Amount (Expected) (ACY)" +
                           ItemLedgEntry."Cost Amount (Actual) (ACY)") /
                          ItemLedgEntry.Quantity;

                        OnCalcLastAdjEntryAvgCostOnAfterCalcAverageCost(ItemLedgEntry, ValueEntry, Item, AverageCost, AverageCostACY);
                        if (AverageCost <> 0) and not IsSubOptimal then
                            exit;
                    end;
                    end;
                end;

Event Signature:

[IntegrationEvent(false, false)]
local procedure OnCalcLastAdjEntryAvgCostOnBeforeComputeAverageCost(var ItemLedgEntry: Record "Item Ledger Entry"; var ValueEntry: Record "Value Entry"; var Item: Record Item; var AverageCost: Decimal; var AverageCostACY: Decimal; var IsSubOptimal: Boolean; var IsHandled: Boolean)
begin
end;

Alternatives evaluated: OnCalcLastAdjEntryAvgCostOnBeforeSetFilters fires before SetFilters and the value entry loop begins, before any ItemLedgEntry is retrieved or IsSubOptimal is computed per entry. A subscriber there has no access to the per-entry ledger entry record or the sub-optimal flag needed to decide how to compute the average cost. OnCalcLastAdjEntryAvgCostOnAfterCalcAverageCost fires after the average cost has already been computed using the standard ItemLedgEntry.Quantity divisor, too late to substitute an alternate costing quantity. OnBeforeCalcLastAdjEntryAvgCost fires at the very start before the quantity guard and the open entries guard, before any value entries are iterated. No event fires between the IsSubOptimal computation and the if condition guarding the cost calculation block.

Metadata

Metadata

Assignees

No one assigned

    Labels

    missing-infoThe issue misses information that prevents it from completion.

    Type

    No fields configured for Task.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions