Update customer invoice settlement in D365 Finance and operation with new settleTransaction method.

In d365 finance old methods settleTransact is obsolete which we were using to settle customer payment and invoice.

Here you have new sample code which can help you.

 public  void settlePayments(CustTable _custTable)
    {
        CustTrans  payCustTrans;
        CustTrans custTrans;
        CustTransOpen custTransOpen, custTransPayment;

        SpecTransManager specTransManager;
        boolean doSpecTransRecordsExist = false;
        Amount  sumInvAmountCur; 
        boolean     markedInvoices;

        // create a specification offset voucher for the current customer
        SpecTransExecutionContext specTransExecutionContext = SpecTransExecutionContext::newFromSource(_custTable);
        specTransManager = SpecTransManager::newFromSpec(specTransExecutionContext.parmSpecContext());
// Serach for latest payment for selected customer
  while select custTransPayment
            order by TransDate asc
            where custTransPayment.AccountNum == _custTable.AccountNum
            && custTransPayment.AmountMST < 0
             join payCustTrans
            where payCustTrans.RecId == custTransPayment.refrecId
            && payCustTrans.TransType == LedgerTransType::Payment
            //&& !payCustTrans.LastSettleDate
        {
            amount += abs(custTransPayment.AmountMST);

            if (!specTransManager.exist(custTransPayment.DataAreaId, custTransPayment.TableId, custTransPayment.RecId))
            {
                doSpecTransRecordsExist = true;
                custTrans = custTrans::find(custTransPayment.RefRecId);
                specTransManager.insert(custTransPayment.DataAreaId,
                                            custTransPayment.TableId,
                                            custTransPayment.RecId,
                                            custTransPayment.AmountCur,
                                            custTrans.CurrencyCode);
            }
        }

        // Clear the table buffers.
        custTransOpen = null;
        custTrans = null;
        custInvoiceJour = null;

        // add open invoice transactions to the offset voucher
        while select custTransOpen
                order by TransDate,RecId asc
                where custTransOpen.AccountNum == _custTable.AccountNum
                && custTransOpen.AmountMST > 0
                && custTransOpen.AmountMST <= amount
                join CurrencyCode from custTrans
                where custTransOpen.RefRecId == custTrans.RecId
        {
            if(custTransOpen.AmountCur <= amount)
            {
                amount -= custTransOpen.AmountCur; 
            }
            else
            {
                continue; 
            }
            doSpecTransRecordsExist = true;
            markedInvoices = true; 
            sumInvAmountCur += custTransOpen.AmountCur;
            // check to see if the record already exists in the specManager before
            // adding it again - this will help from having over settlement issues.
            if (!specTransManager.exist(custTransOpen.DataAreaId, custTransOpen.TableId, custTransOpen.RecId))
            {
                doSpecTransRecordsExist = true;
                
                specTransManager.insert(custTransOpen.DataAreaId,
                                        custTransOpen.TableId,
                                        custTransOpen.RecId,
                                        custTransOpen.AmountCur,
                                        custTrans.CurrencyCode);
            }
        }

        // settle the transactions
        if(markedInvoices)
        {
            CustTrans::settleTransaction(specTransExecutionContext, CustTransSettleTransactionParameters::construct());
        }
        else if (doSpecTransRecordsExist)
        {
            specTransManager.deleteAll();
        }

Leave a comment

Design a site like this with WordPress.com
Get started