Friday, June 1, 2018

X++ code to cancel Deliver Remainder Qty on purchase orders

Below Job is used to select a date range and checks for the ended production orders. Then searches for the purchases orders that are in open status with deliver remainder qty and cancel the qty automatically.

This Job helped our Accounting user to save lot of time. Hence, sharing with you all.


static void lan_CancelingDeliveryRemainderQty(Args _args)
{

   DialogField    dialogStartDate, dialogEndDate;
   TransDate      fromDate, todate;
   PurchLine      purchLine;
    ProdTable prodTable;
    ProdBOM prodBom;
    PurchTable purchTable;
    int cnt =0;

    Dialog     dialog = new Dialog("Select date range");

    dialogStartDate = dialog.addField(extendedTypeStr(TransDate));
    dialogEndDate   = dialog.addField(extendedTypeStr(TransDate));

    dialogStartDate.label("From Date");
    dialogEndDate.label("To Date");

    if(dialog.run())
    {
        fromDate = mkDate(1, mthOfYr(dialogStartDate.value()), year(dialogStartDate.value()));
        toDate   = (mkDate(1, (mthOfYr(dialogEndDate.value()) + 1), year(dialogEndDate.value())) - 1);

        while select * from prodTable
                    join prodBom
                    where  prodTable.ProdId  == prodBom.ProdId
                    && (prodTable.SchedEnd >= fromDate && prodTable.SchedEnd <= todate)
                        && prodBom.InventRefType == 2
                        && prodTable.ProdStatus  == 7
        {
        if(prodTable)

        {
            select * from purchTable
            where purchTable.PurchId == prodBom.InventRefId
            && (purchTable.PurchStatus == 1 || purchTable.PurchStatus == 2);

            if(purchTable)
            {

                     select forupdate purchLine where purchLine.InventRefId == prodTable.ProdId
                     && purchLine.PurchId == prodBom.InventRefId
                     && purchLine.RemainPurchPhysical > 0;
                {

                   if (purchLine)
                    {
                        ttsBegin;
                        // Set remaining inventory Qty to zero
                        purchLine.RemainInventPhysical  = 0;

                        // Set remaining physical Qty to zero
                        purchLine.RemainPurchPhysical   = 0;

                        // We have to cancel the purchLine
                        // Not necessary, I did this to do exactly like AX does
                      //  purchLine.PurchStatus           = PurchStatus::Canceled;

                        // Update PurchLine
                        purchLine.update();
                       cnt++;
                            ttsCommit;
                        // This method will update the inventory transactions
                        InventMovement::bufferSetRemainQty(purchLine);
                    } // purchLine exit
                }
            } //purchTable exit
         }// pordTable exit
        }

    }

         info(strFmt("Start Month %1", fromDate));
        info(strFmt("End Month %1", todate));

    info(strFmt("Total no. of purchlines updated: %1", cnt));


}