Showing posts with label journal. Show all posts
Showing posts with label journal. Show all posts

Tuesday, April 26, 2022

D365 F&O / AX 2012 - Account Payable settle payment with invoice x++

 Hello Guys..!


Here I'm sharing code for Settle Vendor Invoice/Payment through X++  

Code:
-----------
static void VendorSettlement(Args _args)
{
VendTable vendTable;
VendTrans invVendTrans, payVendTrans;
SpecTransManager manager;
CustVendTransData custVendTransData;
;
vendTable = VendTable::find("12345");
// Find the oldest unsettled invoice
select firstonly invVendTrans  order by TransDate asc
                            where invVendTrans.AccountNum == vendTable.AccountNum &&
                            invVendTrans.TransType == LedgerTransType::Purch &&
                            !invVendTrans.closed;
// Find the oldest unsettled payment
select firstonly payVendTrans order by TransDate asc
                    where payVendTrans.AccountNum == vendTable.AccountNum &&
                    payVendTrans.TransType == LedgerTransType::Payment &&
                    !payVendTrans.closed;
ttsbegin;
// Create an object of the CustVendTransData class with the invoice transaction as parameter
custVendTransData = CustVendTransData::construct(invVendTrans);
// Mark it for settlement
custVendTransData.markForSettlement(vendTable);
// Create an object of the CustVendTransData class with the payment transaction as parameter
custVendTransData = CustVendTransData::construct(payVendTrans);
//mark it for settlement
custVendTransData.markForSettlement(vendTable);
ttscommit;
// Settle all marked transactions
if(VendTrans::settleTransact(vendTable, null, true,SettleDatePrinc::DaysDate, systemdateget()))
info("Transactions settled");
}

reference Click Here

D365 F&O / AX 2012 Create a payment journal x++ - Account Payable



LedgerJournalTable jourTable;

    LedgerJournalTrans jourTrans;

    LedgerJournalTableData jourTableData;

    JournalTransData jourTransData;

    LedgerJournalStatic jourStatic;

    DimensionDynamicAccount ledgerDim;

    DimensionDynamicAccount offsetLedgerDim;

    NumberSeq numberseq;


ttsBegin;

    ledgerDim = DimensionStorage::getDynamicAccount('29901-00162',LedgerJournalACType::Vend);

    offsetLedgerDim = AxdDimensionUtil::getLedgerAccountId(['1213011','1213011',1,'Vendor','29901-00162']); //DimensionStorage::getDynamicAccount('NBE.SAR',LedgerJournalACType::Bank);


    jourTableData = JournalTableData::newTable(jourTable);

    jourTable.JournalNum = jourTableData.nextJournalId();

    jourTable.JournalType = LedgerJournalType::Payment;

    jourTable.JournalName = '207';

    jourTableData.initFromJournalName(

    LedgerJournalName::find(jourTable.JournalName));

    jourStatic = jourTableData.journalStatic();

    jourTransData = jourStatic.newJournalTransData(

    jourTrans,

    jourTableData);

    jourTransData.initFromJournalTable();


    jourTrans.CurrencyCode ='SAR';

    jourTrans.initValue();

   numberseq = NumberSeq::NewGetVoucherFromCode('207');

    jourTrans.TransDate = systemDateGet();

    jourTrans.AccountType = LedgerJournalACType::Vend;

    jourTrans.LedgerDimension = ledgerDim;

    jourTrans.Txt = 'Vendor payment journal demo';

    jourTrans.OffsetAccountType = LedgerJournalACType::Ledger;

    jourTrans.OffsetLedgerDimension = offsetLedgerDim;

    jourTrans.AmountCurDebit = 1000;

    //jourTransData.create();

    jourTable.insert();

    jourTransData.insert();

    ttsCommit;

    info(strFmt(

    "Journal '%1' has been created", jourTable.JournalNum));

    ttsCommit; 

Wednesday, March 9, 2022

D365 F&O Create GL general journal using X++

 D365 F&O Create GL general journal using X++ 

In Microsoft Dynamics 365 for Finance and Operations, creating the general journal through X++ is different than creating in Microsoft Dynamics AX 2012. In D365FO, there is an abstract class named as MCRLedgerJournal , which you have to inherit and implement by yourselves.

 

I have created the MCRLedgerJournal_SF class which inherits (extends) the abstract class. To see how to implement the abstract class, you can get the example by viewing MCRLedgerJournal_CustPayment, MCRLedgerJournal_Daily and MCRLedgerJournal_Payment etc. classes.


In our case, I have copied the MCRLedgerJournal_Daily class and pasted it in my own class (MCRLedgerJournal_SF) and modifies it according to my requirements. Apart from this, you can also use the MCRLedgerJournal_Daily class directly if you don’t want to implement your own class.


Following is the code to implement the MCRLedgerJournal abstract class.

class MCRLedgerJournal_ SF extends MCRLedgerJournal
{
    public LedgerJournalTrans createLedgerJournalTrans(AmountCurCredit _amtCurCredit, AmountCurDebit _amtCurDebit, LedgerJournalACType _ledgerJournalACType = LedgerJournalACType::Ledger)
    {
        LedgerJournalTrans ledgerJournalTrans;
 
        // Validate the input to the ledger journal trans and ensure that a ledgerjournaltable
        // exists before creating the ledger journalTrans.
        if (this.validateInputLedgerJourTrans(_amtCurCredit, _amtCurDebit)
            && ledgerJournalTable.RecId != 0)
        {
            this.initLedgerJournalTrans(ledgerJournalTrans);
 
            ledgerJournalTrans.JournalNum = ledgerJournalTable.JournalNum;
            ledgerJournalTrans.AccountType = LedgerJournalACType::Ledger;
            ledgerJournalTrans.LedgerDimension = ledgerAccount;
            ledgerJournalTrans.OffsetAccountType = ledgerOffsetAccountType;
            ledgerJournalTrans.OffsetLedgerDimension = ledgerOffsetAccount;
            ledgerJournalTrans.Txt = transTxt;
            ledgerJournalTrans.CurrencyCode = curCode;
            ledgerJournalTrans.ExchRate = exchRate;
            ledgerJournalTrans.Approver = approver;
            ledgerJournalTrans.Approved = approved;
            // Bank reconciliation specific.
            ledgerJournalTrans.MCRCCGeneralLedgerId = MCRCCGeneralLedgerId;
            ledgerJournalTrans.Due = dueDate;
            ledgerJournalTrans.LineNum = lineNum;
            ledgerJournalTrans.AccountType = ledgerAccountType;
            ledgerJournalTrans.Voucher = voucherNum;
            ledgerJournalTrans.PaymReference = paymReference;
            ledgerJournalTrans.BankTransType = bankTransType;
            ledgerJournalTrans.PaymMode = paymentMode;
            // Add the ref paym id and the paym order id to ledger journal trans.
            ledgerJournalTrans.MCRRefPaymID = MCRRefPaymID;
            ledgerJournalTrans.MCRPaymOrderId = MCRPaymOrderID;
 
            if (_amtCurCredit != 0)
            {
                ledgerJournalTrans.AmountCurCredit = _amtCurCredit;
            }
            if (_amtCurDebit != 0)
            {
                ledgerJournalTrans.AmountCurDebit = _amtCurDebit;
            }
            ledgerJournalTrans.initValue();
            ledgerJournalTrans.defaultRow();
 
            ledgerJournalTrans.insert();
        }
        else
        {
            throw error("@MCR35845");
        }
 
        return ledgerJournalTrans;
    }
 
    /// <summary>
    /// Creates a new instance of the <c>MCRLedgerJournal_Daily</c> class.
    /// </summary>
    /// <param name="_ledgerJournalType">
    /// The type of journal to be used in the instantiation of this class.
    /// </param>
    /// <param name="_journalName">
    /// The name of the journal to be used in the instantiation of this
    /// class; optional.
    /// </param>
    public void new(LedgerJournalType _ledgerJournalType, LedgerJournalNameId _journalName="")
    {
        super(_ledgerJournalType, _journalName);
    }
 
    protected boolean validateInputLedgerJourTrans(AmountCur _amtCurCredit, AmountCur _amtCurDebit)
    {
        boolean ret;
 
        ret = super(_amtCurCredit, _amtCurDebit);
        if (ret)
        {
            if (ledgerAccount == 0)
            {
                throw error(strFmt("@MCR36388"));
            }
        }
        return ret;
    }
 
}

 

Now, the next step is to create the General journal using the above created class. I have written a job which creates the General journal using X++. See the following code:


class GenerateGeneralJournalJob
{
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {       
        MCRLedgerJournal        journalTable;
        LedgerJournalTable      ledgerJournalTable;
        Counter                 recordsInserted;
 
        ttsbegin;
        //Creates the journal header table
        journalTable = new MCRLedgerJournal_SF(LedgerJournalType::Daily);        
        ledgerJournalTable = journalTable.createLedgerJournalTable();
        journalTable.parmLedgerJournalTable(ledgerJournalTable);
        journalTable.parmMCRCCGeneralLedgerId();
        journalTable.parmCurrencyCode(CompanyInfoHelper::standardCurrency());
 
 
        //I am creating two lines to balance out each other.
 
        // creates a new line in general journal
        journalTable.parmLineNum();
        journalTable.parmLedgerAccountType(LedgerJournalACType::Vend);
        journalTable.parmLedgerAccount(LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber("V-000001",LedgerJournalACType::Vend)); //You need to use the vendor account as per your environment's data.
        journalTable.parmTransDate(today());
        journalTable.parmTransTxt("txt");
        journalTable.createLedgerJournalTrans(abs(0),abs(100),LedgerJournalACType::Vend);
 
        // creates a new line in general journal
        journalTable.parmLineNum();
        journalTable.parmLedgerAccountType(LedgerJournalACType::Ledger);
        //Using RecId of Ledger Dimension, you need to enter the RecId as per your environment's data.
        journalTable.parmLedgerAccount(5637179073); 
        journalTable.parmTransDate(today());
        journalTable.parmTransTxt("txt");
        journalTable.createLedgerJournalTrans(abs(100),abs(0),LedgerJournalACType::Ledger);
        ttscommit;
    }
 
}