Sunday, June 29, 2025

Code to get customer and vendor posted and unposted invoice journals in D365F&O X++

   public void clicked()

        {

            VendInvoiceInfoLine         vendInvoiceInfoLine;

            ACCOUNTINGDISTRIBUTION      accoutingDistribution;

            LedgerAccount               ledgerAccount;

            VendInvoiceInfoTable        vendInvoiceInfoTable;

            Amount                      totalAmount;

            container                   con;

            VendInvoiceJour             vendInvoiceJour;

            VendInvoiceTrans            vendInvoiceTrans;

            LedgerJournalTable          ledgerJournalTable;

            LedgerJournalTrans          ledgerJournalTrans;

            RecId                       dynamicVendorAccount;

            GeneralJournalAccountEntry  generalJournalAccountEntry;

            GeneralJournalEntry         generalJournalEntry;

            CustInvoiceTable            custInvoiceTable;

            CustInvoiceLine             custInvoiceLine;

            CustInvoiceJour             custInvoiceJour;

            CustInvoiceTrans            custInvoiceTrans;

            MainAccount                 mainaccount;


            super();


            RPTLines.GLAccount =  GLAccount.valueStr();

            

            if(RPTLines.PartyType == PwC_PartyType::Vendor)

            {

                con =str2con(RPTLines.GLAccount,";",false);


                for(int c=1; c<=conLen(con);c++)

                {

                    Amount  unpostedTotal,postedTotal,transTotal;


                    //for unposted vendor invoices

                    while select vendInvoiceInfoLine

                        join vendInvoiceInfoTable

                            where vendInvoiceInfoTable.TableRefId == vendInvoiceInfoLine.TableRefId

                        join  accoutingDistribution

                             where accoutingDistribution.SourceDocumentLine ==  vendInvoiceInfoLine.SourceDocumentLine

                            && vendInvoiceInfoTable.ParmId  ==  vendInvoiceInfoLine.ParmId

                            && vendInvoiceInfoTable.DocumentDate >= RPTLines.FromDate

                            && vendInvoiceInfoTable.DocumentDate <= RPTLines.ToDate

                            &&  vendInvoiceInfoTable.ParmJobStatus != ParmJobStatus::Executed

                    {

                        ledgerAccount = MainAccount::findByLedgerDimension(accoutingDistribution.LedgerDimension).MainAccountId;


                        if(ledgerAccount == conPeek(con, c))

                        {

                            unpostedTotal += vendInvoiceInfoLine.LineAmount;

                        }

                    }


                    //posted vendor invoices

                    while select generalJournalAccountEntry

                            join generalJournalEntry

                                where generalJournalEntry.RecId == generalJournalAccountEntry.GeneralJournalEntry

                            join vendInvoiceJour

                                where vendInvoiceJour.LedgerVoucher == generalJournalEntry.SubledgerVoucher

                                &&  vendInvoiceJour.InvoiceDate >= RPTLines.FromDate

                                &&  vendInvoiceJour.InvoiceDate <= RPTLines.ToDate

                    {

                        ledgerAccount = MainAccount::findByLedgerDimension(generalJournalAccountEntry.LedgerDimension).MainAccountId;


                        if(ledgerAccount == conPeek(con, c))

                        {

                            postedTotal += generalJournalAccountEntry.AccountingCurrencyAmount;

                        }

                    }


                    dynamicVendorAccount = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(RPTLines.PartyNumber,LedgerJournalACType::Vend);


                    //GL entries for vendor

                    while select ledgerJournalTrans

                            join ledgerJournalTable

                            where ledgerJournalTable.JournalNum == ledgerJournalTrans.JournalNum

                            && ledgerJournalTrans.DocumentDate >= RPTLines.FromDate

                            &&  ledgerJournalTrans.DocumentDate <=  RPTLines.ToDate

                            && ledgerJournalTrans.LedgerDimension == dynamicVendorAccount

                            && ledgerJournalTrans.Invoice !=''

                            && ledgerJournalTable.Posted == NoYes::No

                    {

                        ledgerAccount = MainAccount::findByLedgerDimension(ledgerJournalTrans.OffsetLedgerDimension).MainAccountId;


                        if(ledgerAccount == conPeek(con, c))

                        {

                            if(ledgerJournalTrans.AmountCurDebit)

                            {

                                transTotal += ledgerJournalTrans.AmountCurDebit;

                            }

                            else if(ledgerJournalTrans.AmountCurCredit)

                            {

                                transTotal += ledgerJournalTrans.AmountCurCredit;

                            }

                        }

                    }

                            

                    totalAmount +=  unpostedTotal + postedTotal + transTotal;

                }


                RPTLines.TransactionAmount =  totalAmount;

                

            }


            else  if(RPTLines.PartyType == PwC_PartyType::Customer)

            {

                con =str2con(RPTLines.GLAccount,";",false);

                Amount custTotalAmount;

                for(int c=1; c<=conLen(con);c++)

                {

                    Amount  unposted,GLAmount;

                    mainaccount = MainAccount::findByMainAccountId(conPeek(con,c));


                    RecId  mainaccountValue = LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountRecId(mainaccount.RecId);


                    //posted and unposted invoices

                    while select custInvoiceLine

                       join  custInvoiceTable

                        where custInvoiceTable.RecId == custInvoiceLine.ParentRecId

                        && custInvoiceTable.DocumentDate >= RPTLines.FromDate

                        &&  custInvoiceTable.DocumentDate <=  RPTLines.ToDate

                        && custInvoiceLine.LedgerDimension == mainaccountValue

                    {

                        unposted += custInvoiceLine.AmountCur;

                    }


                    dynamicVendorAccount = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(RPTLines.PartyNumber,LedgerJournalACType::Cust);


                    //GL entries for customer

                    while select ledgerJournalTrans

                            join ledgerJournalTable

                            where ledgerJournalTable.JournalNum == ledgerJournalTrans.JournalNum

                            && ledgerJournalTrans.DocumentDate >= RPTLines.FromDate

                            &&  ledgerJournalTrans.DocumentDate <=  RPTLines.ToDate

                            && ledgerJournalTrans.LedgerDimension == dynamicVendorAccount

                            && ledgerJournalTrans.Invoice !=''

                    {

                        ledgerAccount = MainAccount::findByLedgerDimension(ledgerJournalTrans.OffsetLedgerDimension).MainAccountId;


                        if(ledgerAccount == conPeek(con, c))

                        {

                            if(ledgerJournalTrans.AmountCurDebit)

                            {

                                GLAmount += ledgerJournalTrans.AmountCurDebit;

                            }

                            else if(ledgerJournalTrans.AmountCurCredit)

                            {

                                GLAmount += ledgerJournalTrans.AmountCurCredit;

                            }

                        }

                    }

                    custTotalAmount +=  unposted + GLAmount;

                }

                RPTLines.TransactionAmount =  custTotalAmount;

            }

            GLAccount.text('');

        }

No comments:

Post a Comment