Create and post general ledger journal

Create and post general ledger journal:

/// <summary>
/// Helper class to reverse project invoice voucher transactions and match those, which relates to customer
/// </summary>
class VKProjectInvoicePostingHelper
{
    LedgerJournalTrans  paymLedgerJournalTrans;

    /// <summary>
    /// Entry point to execute reversal and matching process
    /// </summary>
    /// <param name = "_projProposalJour">ProjProposalJour record</param>
    public void reverseTranasctions(ProjProposalJour _projProposalJour)
    {
        if (_projProposalJour && _projProposalJour.ProjInvoiceId)
        {
            ProjInvoiceJour     projInvoiceJour     = ProjInvoiceJour::find(_projProposalJour.ProjInvoiceId, _projProposalJour.InvoiceDate);
            LedgerJournalTable  ledgerJournalTable  = this.createJournal(projInvoiceJour);

            if (ledgerJournalTable)
            {
                info(strFmt("General journal number %1", ledgerJournalTable.JournalNum));
                this.postGeneralJournal(ledgerJournalTable);
            }
        }
    }

    /// <summary>
    /// Creates general journal based on project invoice voucher transactions
    /// </summary>
    /// <param name = "_projInvoiceJour">ProjInvoiceJour record</param>
    /// <returns>LedgerJournalTable record</returns>
    public LedgerJournalTable createJournal(ProjInvoiceJour _projInvoiceJour)
    {
        GeneralJournalEntry         generalJournalEntry;
        GeneralJournalAccountEntry  generalJournalAccountEntry;
        LedgerJournalTable          ledgerJournalTable;
        LedgerJournalTrans          ledgerJournalTrans;

        while select generalJournalEntry
                where generalJournalEntry.SubledgerVoucher == _projInvoiceJour.LedgerVoucher
                join generalJournalAccountEntry
                    where generalJournalAccountEntry.GeneralJournalEntry == generalJournalEntry.RecId
        {
            if (!ledgerJournalTable)
            {
                ledgerJournalTable = this.createJournalHeader(_projInvoiceJour);
            }
                
            ledgerJournalTrans.clear();
            ledgerJournalTrans.JournalNum             = ledgerJournalTable.JournalNum;
            ledgerJournalTrans.TransDate              = _projInvoiceJour.InvoiceDate;
            if (LedgerPostingType::CustBalance == generalJournalAccountEntry.PostingType)
            {
                ledgerJournalTrans.AccountType            = LedgerJournalACType::Cust;
                ledgerJournalTrans.LedgerDimension        = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(_projInvoiceJour.InvoiceAccount, ledgerJournalTrans.AccountType);
                ledgerJournalTrans.DefaultDimension = LedgerDimensionFacade::getDefaultDimensionFromLedgerDimension(generalJournalAccountEntry.LedgerDimension);
            }
            else
            {
                ledgerJournalTrans.AccountType            = LedgerJournalACType::Ledger;
                ledgerJournalTrans.LedgerDimension        = generalJournalAccountEntry.LedgerDimension;
            }
            if (LedgerPostingType::Tax == generalJournalAccountEntry.PostingType)
            {
                TaxTrans taxTrans;
                select firstonly taxTrans
                    where taxTrans.Voucher              == _projInvoiceJour.LedgerVoucher
                        &&taxTrans.TransDate            == generalJournalEntry.AccountingDate
                        &&taxTrans.SourceCurrencyCode   == generalJournalAccountEntry.TransactionCurrencyCode
                        &&taxTrans.SourceTaxAmountCur   == generalJournalAccountEntry.TransactionCurrencyAmount;

                if (taxTrans)
                {
                    ledgerJournalTrans.TaxGroup     = taxTrans.TaxGroup;
                    ledgerJournalTrans.TaxItemGroup = taxTrans.TaxItemGroup;
                    ledgerJournalTrans.TaxCode      = taxTrans.TaxCode;
                }
            }
            //ledgerJournalTrans.OffsetAccountType      = ledgerJournalTransOrig.OffsetAccountType;
            //ledgerJournalTrans.OffsetLedgerDimension  = ledgerJournalTransOrig.OffsetLedgerDimension;
            //ledgerJournalTrans.OffsetDefaultDimension = ledgerJournalTransOrig.OffsetDefaultDimension;

            ledgerJournalTrans.CurrencyCode           =   _projInvoiceJour.CurrencyId;
            if (generalJournalAccountEntry.IsCredit)
            {
                ledgerJournalTrans.AmountCurDebit   = abs(generalJournalAccountEntry.AccountingCurrencyAmount);
            }
            else
            {
                ledgerJournalTrans.AmountCurCredit  = abs(generalJournalAccountEntry.AccountingCurrencyAmount);
            }

            //addition fields
            //ledgerJournalTrans.Approver           = HcmWorker::userId2Worker(curuserid());
            //ledgerJournalTrans.Approved           = NoYes::Yes;
            ledgerJournalTrans.Txt                = strFmt("Reverse invoice %1", _projInvoiceJour.ProjInvoiceId);
            ledgerJournalTrans.SkipBlockedForManualEntryCheck = true;

            ledgerJournalTrans.defaultRow();
            ledgerJournalTrans.insert();
            if (LedgerPostingType::CustBalance == generalJournalAccountEntry.PostingType)
            {
                paymledgerJournalTrans.data(ledgerJournalTrans);
            }
        }
        return ledgerJournalTable;
    }

    /// <summary>
    /// Creates general journal header
    /// </summary>
    /// <param name = "_projInvoiceJour">ProjInvoiceJour record</param>
    /// <returns>LedgerJournalTable record</returns>
    public LedgerJournalTable createJournalHeader(ProjInvoiceJour _projInvoiceJour)
    {
        ProjParameters      projParameters = ProjParameters::find();
        LedgerJournalTable  ledgerJournalTable;
 
        if (!projParameters.VKJournalName)
        {
            throw Error("Please specify journal name");
        }

        ledgerJournalTable.clear();
        ledgerJournalTable.initValue();
        ledgerJournalTable.JournalNum = JournalTableData::newTable(ledgerJournalTable).nextJournalId();
        ledgerJournalTable.initFromLedgerJournalName(projParameters.VKJournalName);
        // journal description, should be after initFromLedgerJournalName
        ledgerJournalTable.Name = strFmt("Some journal name %1", _projInvoiceJour.ProjInvoiceId);
        ledgerJournalTable.CurrencyCode = _projInvoiceJour.CurrencyId;
        ledgerJournalTable.insert();
 
        return ledgerJournalTable;
    }

    /// <summary>
    /// Posts general journal
    /// </summary>
    /// <param name = "_ledgerJournalTable">LedgerJournalTable record</param>
    void postGeneralJournal(LedgerJournalTable _ledgerJournalTable)
    {
        LedgerJournalCheckPost  ledgerJournalCheckPost;
 
        ledgerJournalCheckPost  = ledgerJournalCheckPost::newLedgerJournalTable(_ledgerJournalTable, NoYes::Yes);
        ledgerJournalCheckPost.runOperation();
    }
}



Support The Author

 If you found value in what I share, I've set up a Buy Me a Coffee page as a way to show your support.

Buy Me a Coffee

Post a Comment


All Comments


No comments. Be the first one to comment on this post.

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics 365 FO.

Authors are allowed to set their own "buy me a coffee" link.
Join us.

Blog Tags