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 5940 ServContractManagement - procedure InsertMultipleServLedgEntries #27687

Open
matthiasrabus opened this issue Nov 25, 2024 · 0 comments
Labels
event-request Request for adding an event SCM GitHub request for SCM area

Comments

@matthiasrabus
Copy link

matthiasrabus commented Nov 25, 2024

Describe the request

To implement a complex change in the calculation of the service ledger entry, i need the following four events. Please add them, if possible.

procedure InsertMultipleServLedgEntries(var NoOfPayments: Integer; var DueDate: Date; var NonDistrAmount: array[4] of Decimal; var InvRoundedAmount: array[4] of Decimal; var ServHeader: Record "Service Header"; InvFrom: Date; NextInvDate: Date; AddingNewLines: Boolean; CountOfEntryLoop: Integer; ServContractLine: Record "Service Contract Line"; AmountRoundingPrecision: Decimal)
var
	ServContractHeader: Record "Service Contract Header";
	Index: Integer;
	IsHandled: Boolean;
begin
	if CountOfEntryLoop = 0 then
		exit;

	OnBeforeInsertMultipleServLedgEntries(ServLedgEntry, ServContractLine);

	CheckMParts := false;
	if DueDate <> CalcDate('<CM>', DueDate) then begin
		DueDate := CalcDate('<-CM-1D>', DueDate);
		ServContractHeader.Get(ServContractLine."Contract Type", ServContractLine."Contract No.");
		CheckMParts := true;
	end;
	
	//FIRST EVENT BEGIN
	OnInsertMultipleServLedgEntriesUpdateDateFieldsBeforeCalcNonDistrAmount(InvFrom, DueDate, NoOfPayments);
	//FIRST EVENT END
	
	NonDistrAmount[AmountType::Amount] :=
	  -CalcContractLineAmount(ServContractLine."Line Amount", InvFrom, DueDate);
	NonDistrAmount[AmountType::UnitPrice] :=
	  -CalcContractLineAmount(ServContractLine."Line Value", InvFrom, DueDate);
	NonDistrAmount[AmountType::UnitCost] :=
	  CalcContractLineAmount(ServContractLine."Line Cost", InvFrom, DueDate);
	NonDistrAmount[AmountType::DiscAmount] :=
	  CalcContractLineAmount(ServContractLine."Line Discount Amount", InvFrom, DueDate);
	ServLedgEntry."Service Item No. (Serviced)" := ServContractLine."Service Item No.";
	ServLedgEntry."Item No. (Serviced)" := ServContractLine."Item No.";
	ServLedgEntry."Serial No. (Serviced)" := ServContractLine."Serial No.";
	DueDate := NextInvDate;
	if CheckMParts and (NoOfPayments > 1) then begin
		//SECOND EVENT BEGIN
          IsHandled := false;
		OnInsertMultipleServLedgEntriesUpdateOnBeforeUpdateNoOfPayments(CheckMParts, NoOfPayments, IsHandled);
		if not IsHandled then
		//SECOND EVENT END
		NoOfPayments := NoOfPayments - 1;
		// the count of invoice lines should never exceed the count of payments
		if CountOfEntryLoop > NoOfPayments then
			CountOfEntryLoop := NoOfPayments;
	end;

	if AddingNewLines then
		DueDate := InvFrom;
	for Index := 1 to CountOfEntryLoop do begin
		//THIRD EVENT BEGIN
		IsHandled := false;		
		OnInsertMultipleServLedgEntriesOnBeforeSetSetServLedgEntryAmounts(ServLedgEntry,InvRoundedAmount,NonDistrAmount,NoOfPayments,Index,CountOfEntryLoop,AmountRoundingPrecision,DueDate,IsHandled);
		if not IsHandled then
		//THIRD EVENT END
		SetServLedgEntryAmounts(
		  ServLedgEntry, InvRoundedAmount,
		  NonDistrAmount[AmountType::Amount] / (NoOfPayments + 1 - Index),
		  NonDistrAmount[AmountType::UnitPrice] / (NoOfPayments + 1 - Index),
		  NonDistrAmount[AmountType::UnitCost] / (NoOfPayments + 1 - Index),
		  NonDistrAmount[AmountType::DiscAmount] / (NoOfPayments + 1 - Index),
		  AmountRoundingPrecision);
		ServLedgEntry."Cost Amount" := ServLedgEntry."Charged Qty." * ServLedgEntry."Unit Cost";

		NonDistrAmount[AmountType::Amount] -= ServLedgEntry."Amount (LCY)";
		NonDistrAmount[AmountType::UnitPrice] -= ServLedgEntry."Unit Price";
		NonDistrAmount[AmountType::UnitCost] -= ServLedgEntry."Unit Cost";
		NonDistrAmount[AmountType::DiscAmount] -= ServLedgEntry."Contract Disc. Amount";

		ServLedgEntry."Entry No." := NextEntry;
		UpdateServLedgEntryAmount(ServLedgEntry, ServHeader);
		ServLedgEntry."Posting Date" := DueDate;
		ServLedgEntry.Prepaid := true;
		IsHandled := false;
		OnInsertMultipleServLedgEntriesOnBeforeServLedgEntryInsert(ServLedgEntry, ServContractHeader, ServContractLine, NonDistrAmount, IsHandled, ServHeader);
		if IsHandled then
			exit;
		ServLedgEntry.Insert();
		NextEntry += 1;
		DueDate := CalcDate('<1M>', DueDate);
		//FOURTH EVENT BEGIN
		OnInsertMultipleServLedgEntriesOnAfterSetDueDate(Index, FirstServEntryNo, DueDate, ServLedgEntry, CountOfEntryLoop, InvRoundedAmount, NonDistrAmount, ServHeader, AmountRoundingPrecision);
		//FOURTH EVENT END
	end;
end;

The integration events:

[IntegrationEvent(false,false)]
local procedure OnInsertMultipleServLedgEntriesUpdateDateFieldsBeforeCalcNonDistrAmount(var InvFrom: Date; var DueDate: Date; NoOfPayments: Integer)
begin
end;

[IntegrationEvent(false,false)]
local procedure OnInsertMultipleServLedgEntriesUpdateOnBeforeUpdateNoOfPayments(CheckMParts: Boolean; var NoOfPayments: Integer; var IsHandled: Boolean)
begin
end;

[IntegrationEvent(false,false)]
local procedure OnInsertMultipleServLedgEntriesOnBeforeSetSetServLedgEntryAmounts(var ServLedgEntry: Record "Service Ledger Entry"; var InvRoundedAmount: array[4] of Decimal; NonDistrAmount: array[4] of Decimal; NoOfPayments: Integer; Index: Integer; CountOfEntryLoop: Integer; AmountRoundingPrecision: Decimal; DueDate: Date; var IsHandled: Boolean)
begin
end;

[IntegrationEvent(false, false)]
local procedure OnInsertMultipleServLedgEntriesOnAfterSetDueDate(Index: Integer; var FirstServEntryNo: Integer; var DueDate: Date; ServLedgEntry: Record "Service Ledger Entry"; CountOfEntryLoop: Integer; InvRoundedAmount: array[4] of Decimal; var NonDistrAmount: array[4] of Decimal; var ServHeader: Record "Service Header"; AmountRoundingPrecision: Decimal)
begin
end;

Additional context

Please add these events to allow me to implement the necessary changes
Internal work item: AB#558846

@ChethanT ChethanT added event-request Request for adding an event SCM GitHub request for SCM area labels Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event-request Request for adding an event SCM GitHub request for SCM area
Projects
None yet
Development

No branches or pull requests

2 participants