Thursday, October 13, 2016

Reverse Settlement Accout Receivable on AX 2012 R3

static void Update_Reverse_Settlement(Args _args)
{
    CustTrans           invCustTrans,findCustTrans;
    SpecTransManager    specTransManager;
    DocumentNum         invdocnumx,paydocnumx;
    CustSettlement      custSettlement;
    AccountNum          accnumx;
    date                paydatex;
    CustTable           custTable;
    ;

    invdocnumx  = 'PY16100100561';
    accnumx     = '631331';

    select invCustTrans
    order by TransDate asc
    where   invCustTrans.DocumentNum == invdocnumx
        &&  invCustTrans.AmountMST > 0
        &&  invCustTrans.AccountNum == accnumx;
   
    while select findCustTrans where findCustTrans.RecId == invCustTrans.RecId
    {       
        custTable = custTable::find(findCustTrans.AccountNum);

        if(findCustTrans.TransType != ledgerTransType::Payment)
        {
            //We must go to the customer settlement table and locate the records that were used to settle the invoice in scope and pass them into the class
            // that manages the marking and updating of applied credits and payments to invoices. We remove the credit marking to remove the link between the invoice
            //and the credits used to settle the invoice. Then we must reverse the transactions using the reverseTransact method on the customer transaction table
              select firstonly custSettlement where custSettlement.TransCompany == findCustTrans.dataAreaId &&
                                                    custSettlement.TransRecId   == findCustTrans.RecId &&
                                                    custSettlement.AccountNum   == findCustTrans.AccountNum;

            specTransManager = SpecTransManager::newRefTableId(custTable,tablenum(custSettlement), true);
            specTransManager.insert(custSettlement.DataAreaId, custSettlement.TableId, custSettlement.RecId, custSettlement.SettleAmountCur, findCustTrans.CurrencyCode);
            custSettlement.CustVendSettlement::markOffsets(specTransManager, findCustTrans.CurrencyCode, true);

            if (CustTrans::reverseTransact(custTable,null,settleDatePrinc::DateOfPayment,custSettlement.TransDate))
            {
                specTransManager.deleteAll();
            }
        }
    }
    info(strFmt('Done %1',findCustTrans.txt));


}

Reference:

http://www.winfosoft.com/blog/uncategorized/dynamics-ax-how-to-void-an-ar-payment-and-remove-applied-credits-to-invoices-using-code
 

No comments:

Post a Comment