Extend Posting routine using existing event

akositaeakositae Member Posts: 1
Hi Guys,

I'm doing a POC(NAV2018w1) right now on how to extend the posting routine using just an existing Event, Basically what I want to achieve is the ff.
1. Get Sales Transaction Information upon posting.
2. insert the Information to Customized Subsidiary Ledger(i.e WHT Entry)
3. Insert the the WHT entry to Gen. Journal Line and Post
4. Deduct the Amount(WHT) to Customer Entry and Post Customer Entry.

Sample Transaction below.
SO Total Invoice Amount 100 Exc. VAT.

Upon Posting the Transation will be the ff.

Sales -100
VAT -25
AR 123
WHT 2

Code below that I'm working so far with the summary(some values are hardcoded as intended just to continue to posting).

InserAndPostSubsidiary()- post the subsidiary and GL entries that subscribe to CDU80 OnBeforePostGLAndCustomer() publisher.
DeductWHTinCustomerAmount()-deduct the customer entry that subscribe to CDU80 OnBeforePostCustomerEntry() publisher.

LOCAL [EventSubscriber] SubscribeToOnBeforePostGLAndCustomerInCDU80(SalesHeader : Record "Sales Header";VAR TempInvoicePostBuffer : TEMPORARY Record "Invoice Post. Buffer";VAR CustLedgerEntry : Record "Cust. Ledger Entry")
InserAndPostSubsidiary(SalesHeader);

LOCAL InserAndPostSubsidiary(SalesHeader : Record "Sales Header")
IF MySubs.FINDLAST THEN
LastEntryNo := MySubs."Entry No." + 1
ELSE
LastEntryNo := 1;

MySubs.INIT;
MySubs."Entry No." := LastEntryNo;
MySubs."Document No." := SalesHeader."Posting No.";
MySubs."Line No." := 10000;
MySubs.WHT := 2; //hardcode for POC
MySubs."Account No." := '5615';
MySubs."Source No." := SalesHeader."Sell-to Customer No.";
MySubs.INSERT;
PostWHT(MySubs); //Post Ledger Entry

LOCAL PostWHT(VAR MySubs : Record MySubsidiary)
WITH MySubs DO BEGIN
GenJnlLine.INIT;
GenJnlLine."Document Type" := GenJnlLine."Document Type"::Invoice;
GenJnlLine."Document No." := "Document No.";
GenJnlLine."Line No." := GenJnlLine."Line No." + 10000;
GenJnlLine."Posting Date" := 012620D;
GenJnlLine."Journal Template Name" := 'SALES';
GenJnlLine."External Document No." := 'ECT634';
GenJnlLine."System-Created Entry" := TRUE;
GenJnlLine."Source Code" := 'SALES';
GenJnlLine.VALIDATE("Account Type",GenJnlLine."Account Type"::"G/L Account");
GenJnlLine."Account No." := "Account No.";
GenJnlLine.VALIDATE(Amount,WHT);
GenJnlLine."Dimension Set ID" := 37;
GenJnlLine."Source Type" := GenJnlLine."Source Type"::Customer;
GenJnlLine."Source No." := "Source No.";
GenPostLine.RunWithCheck(GenJnlLine);
END;

LOCAL [EventSubscriber] SubscribeToOnBeforePostCustomerEntryInCDU80(VAR GenJnlLine : Record "Gen. Journal Line";SalesHeader : Record "Sales Header";VAR TotalSalesLine : Record "Sales Line";VAR TotalSalesLineLCY : Record "Sales Line")
DeductWHTinCustomerAmount(GenJnlLine,TotalSalesLine,TotalSalesLineLCY); //Deduct customer amount - wht before post

LOCAL DeductWHTinCustomerAmount(VAR GenJnlLine : Record "Gen. Journal Line";VAR TotalSalesLine : Record "Sales Line";VAR TotalSalesLineLCY : Record "Sales Line")
WITH GenJnlLine DO BEGIN
MySubs.WHT := 2; //hardcode for POC
Amount := -TotalSalesLine."Amount Including VAT" + -MySubs.WHT;
"Amount (LCY)" := -(TotalSalesLineLCY."Amount Including VAT" - -MySubs.WHT);
"Sales/Purch. (LCY)" := -TotalSalesLineLCY.Amount;
"Profit (LCY)" := -(TotalSalesLineLCY.Amount - TotalSalesLineLCY."Unit Cost (LCY)");
"Inv. Discount (LCY)" := -TotalSalesLineLCY."Inv. Discount Amount";
END;

When I tried to post this I got the inconsistency error but when I use the inconsistency tool I got the balance amount as per image below.
xv4at16ujall.png

I need to figure out what am I missing or should I need to do it in other method, or? Please help. Thanks :).

Regards,

akositae :)
Sign In or Register to comment.