Hello,
I need to calculate Royalty Amount for each item sold. For this I have extended the Item table and Item Card page. User can select a vendor and royalty percentage on the Item Card page and save it.
I need to calculate the royalty amount for each item in the Sales Lines. So when sales order is submitted, I tap into the "OnAfterPostSalesLines" event and execute below code to insert rows into the General Journal table.
I am able to calculate the royalty amount for each item correctly but the rows are not being stored properly in the General Journal table. Issues I am facing are:
1) If there are multiple SalesLines rows, only the first row is stored in the General Journal table. The second row never makes it to the General Journal table.
2) In case of consecutive run, the new rows are inserted but with old data that is already existing in the General Journal table.
Please help as I am not able to resolve this issue. If there is any other solution, please let me know.
===================================================================================================================================================
codeunit 50156 "Sales Post Royalty Codeunit"
{
EventSubscriberInstance = StaticAutomatic;
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterPostSalesLines', '', true, true)]
local procedure OnAfterPostSalesLinesPosted(var SalesHeader: Record "Sales Header"; var SalesShipmentHeader: Record "Sales Shipment Header"; var SalesInvoiceHeader: Record "Sales Invoice Header"; var SalesCrMemoHeader: Record "Sales Cr.Memo Header"; var ReturnReceiptHeader: Record "Return Receipt Header"; WhseShip: Boolean; WhseReceive: Boolean; var SalesLinesProcessed: Boolean; CommitIsSuppressed: Boolean; EverythingInvoiced: Boolean; var TempSalesLineGlobal: Record "Sales Line")
//[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnBeforePostSalesLines', '', false, false)]
//local procedure OnBeforePostSalesLines(var SalesHeader: Record "Sales Header"; var TempSalesLineGlobal: Record "Sales Line"; var TempVATAmountLine: Record "VAT Amount Line"; var EverythingInvoiced: Boolean);
var
lineItem: Record Item;
pct: decimal;
amount: decimal;
royaltyAmt: Decimal;
vendor: code[20];
counter: integer;
GenJnlLine: Record "Gen. Journal Line";
lineNo: Integer;
begin
if not TempSalesLineGlobal.IsEmpty() then begin
TempSalesLineGlobal.Reset();
counter := TempSalesLineGlobal.Count();
IF TempSalesLineGlobal.FINDSET THEN BEGIN
repeat
lineItem := TempSalesLineGlobal.GetItem();
vendor := lineItem.RoyaltyVendor;
amount := TempSalesLineGlobal."Line Amount";
royaltyAmt := (Amount) * (lineItem.RoyaltyPercentage / 100);
Message('Vendor - %1, Amount %2', lineItem.RoyaltyVendor, royaltyAmt);
GenJnlLine.Init();
GenJnlLine."Account Type" := "Gen. Journal Account Type".FromInteger(0);
GenJnlLine."Account No." := '8455';
GenJnlLine.Amount := royaltyAmt;
GenJnlLine."Document Type" := TempSalesLineGlobal."Document Type";
GenJnlLine."Document No." := TempSalesLineGlobal."Document No.";
GenJnlLine."Posting Date" := Today;
GenJnlLine."Bal. Account Type" := "Gen. Journal Account Type".FromInteger(2);
GenJnlLine."Bal. Account No." := Format(vendor);
GenJnlLine."Journal Batch Name" := 'DEFAULT';
GenJnlLine."Journal Template Name" := 'GENERAL';
IF GenJnlLine.FINDLAST THEN begin
lineNo := GenJnlLine."Line No." + 10000;
end
else begin
lineNo := 10000;
end;
GenJnlLine."Line No." := lineNo;
GenJnlLine.Insert(true);
Commit();
Message('record# %1 inserted.', lineNo);
until TempSalesLineGlobal.next() = 0;
END;
end;
end;