How to Settle Invoice through X++ in Ax 2012

Here i have  give sample demo example for how to settle invoice through code in ax 2012.

static void settlePayment(Args _args)
{
    CustTable custTable;
    CustTrans invCustTrans, payCustTrans;
    SpecTransManager manager;
    CustVendTransData custVendTransData;
    ;
    custTable = CustTable::find("2022");
    // Find the oldest invoice that has not been settled yet
    // for this customer you can customize the query find the invoice that you
    //want
    //to settle
    select firstonly invCustTrans
    order by TransDate asc
    where invCustTrans.AccountNum == custTable.AccountNum &&
    invCustTrans.TransType == LedgerTransType::Sales &&
    !invCustTrans.LastSettleDate;
    // Find the oldest payment that has not been settled yet
    // for this customer
    select firstonly payCustTrans
    order by TransDate asc
    where payCustTrans.AccountNum == custTable.AccountNum &&
    payCustTrans.TransType == LedgerTransType::Payment &&
    !payCustTrans.LastSettleDate;
    ttsbegin;
    // Create an object of the CustVendTransData class
    // with the invoice transaction as parameter and mark
    // it for settlement
    custVendTransData = CustVendTransData::construct(invCustTrans);
    custVendTransData.markForSettlement(CustTable);
    // Create an object of the CustVendTransData class
    // with the payment transaction as parameter and mark
    // it for settlement
    custVendTransData = CustVendTransData::construct(payCustTrans);
    custVendTransData.markForSettlement(CustTable);
    ttscommit;
    // Settle all transactions marked for settlement for this
    // customer
    if(CustTrans::settleTransact(custTable, null, true,SettleDatePrinc::DaysDate, systemdateget()))
    info("Transactions settled");
}

Related Posts

Previous
Next Post »

Thanks for comments.....