Skip to content

[W1][Codeunit][22][Item Jnl.-Post Line] Add integration event OnInsertValueEntryOnBeforeShouldCalcExpectedCost in procedure InsertValueEntry #30051

Description

@mavohra

Why do you need this change?

We need to intercept the expected cost calculation block in InsertValueEntry to replace the standard ItemJnlLine.Quantity and ItemJnlLine."Invoiced Quantity" references in the ShouldCalcExpectedCost condition with alternate quantity values, and to pass alternate quantity arguments to the subsequent CalcExpectedCost call.

The standard code evaluates ShouldCalcExpectedCost using ItemJnlLine.Quantity = 0 and ItemJnlLine."Invoiced Quantity" <> 0. In scenarios where alternate quantity fields drive the costing logic, these direct field references produce an incorrect result for ShouldCalcExpectedCost. When the condition does evaluate to true, the CalcExpectedCost call further passes ItemLedgEntry.Quantity as the quantity argument and compares ItemLedgEntry.Quantity = ItemLedgEntry."Invoiced Quantity" for the last parameter, both of which must also reflect the alternate quantity values.

The existing OnInsertValueEntryOnBeforeCalcExpectedCost fires after ShouldCalcExpectedCost is already computed from the standard condition and exposes it as a var parameter. A subscriber can modify the boolean result, but cannot substitute the quantity field references used inside the condition expression itself, nor replace the ItemLedgEntry.Quantity arguments passed to CalcExpectedCost. No event currently exists before the ShouldCalcExpectedCost := assignment that allows a subscriber to take full control of both the condition evaluation and the subsequent CalcExpectedCost call.

Describe the request

Add an integration event OnInsertValueEntryOnBeforeShouldCalcExpectedCost in InsertValueEntry in Codeunit 22 "Item Jnl.-Post Line" after the UpdateItemLedgEntry block and before the ShouldCalcExpectedCost condition assignment, with IsHandled wrapping both the condition assignment and the CalcExpectedCost call block.

            RecalculateCostPerUnit(ValueEntry, ItemLedgEntry);
            if UpdateItemLedgEntry(ValueEntry, ItemLedgEntry) then
                ItemLedgEntry.Modify();
        end;

        IsHandled := false;
        OnInsertValueEntryOnBeforeShouldCalcExpectedCost(ItemJnlLine, ItemLedgEntry, ValueEntry, TransferItem, ShouldCalcExpectedCost, IsHandled); // <---- New Event
        if not IsHandled then begin
            ShouldCalcExpectedCost :=
                ((ValueEntry."Entry Type" = ValueEntry."Entry Type"::"Direct Cost") and
                    (ValueEntry."Item Charge No." = '')) and
                (((ItemJnlLine.Quantity = 0) and (ItemJnlLine."Invoiced Quantity" <> 0)) or
                    (ItemJnlLine.Adjustment and not ValueEntry."Expected Cost")) and
                not ExpectedCostPosted(ValueEntry);
            OnInsertValueEntryOnBeforeCalcExpectedCost(ItemJnlLine, ItemLedgEntry, ValueEntry, TransferItem, InventoryPostingToGL, ShouldCalcExpectedCost);
            if ShouldCalcExpectedCost then begin
                if ValueEntry."Invoiced Quantity" = 0 then begin
                    if InvdValueEntry.Get(ValueEntry."Applies-to Entry") then
                        InvoicedQty := InvdValueEntry."Invoiced Quantity"
                    else
                        InvoicedQty := ValueEntry."Valued Quantity";
                end else
                    InvoicedQty := ValueEntry."Invoiced Quantity";
                CalcExpectedCost(
                  ValueEntry,
                  ItemLedgEntry."Entry No.",
                  InvoicedQty,
                  ItemLedgEntry.Quantity,
                  ValueEntry."Cost Amount (Expected)",
                  ValueEntry."Cost Amount (Expected) (ACY)",
                  ValueEntry."Sales Amount (Expected)",
                  ValueEntry."Purchase Amount (Expected)",
                  ItemLedgEntry.Quantity = ItemLedgEntry."Invoiced Quantity");
            end;
        end;

Event Signature:

[IntegrationEvent(false, false)]
local procedure OnInsertValueEntryOnBeforeShouldCalcExpectedCost(var ItemJnlLine: Record "Item Journal Line"; var ItemLedgEntry: Record "Item Ledger Entry"; var ValueEntry: Record "Value Entry"; TransferItem: Boolean; var ShouldCalcExpectedCost: Boolean; var IsHandled: Boolean)
begin
end;

Alternatives evaluated: OnInsertValueEntryOnBeforeCalcExpectedCost fires after ShouldCalcExpectedCost has already been evaluated from the standard condition using ItemJnlLine.Quantity and ItemJnlLine."Invoiced Quantity", and exposes ShouldCalcExpectedCost as a var parameter. A subscriber can modify the boolean result, but cannot replace the quantity field references used inside the condition expression, nor substitute the ItemLedgEntry.Quantity arguments passed to CalcExpectedCost. No event currently exists before the ShouldCalcExpectedCost := assignment that allows a subscriber to take full control of the entire expected cost evaluation and calculation block.

Internal work item: AB#638507

Metadata

Metadata

Assignees

No one assigned

    Labels

    SCMGitHub request for SCM areaevent-requestRequest for adding an event

    Type

    Fields

    No fields configured for Task.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions