Monday, September 30, 2024

Batch job: Dialog box with multi selection lookup and delete physical on-hand inventory transactions against product

 class KDZ_ClearPhysicalReserveBatch extends RunBaseBatch

{

    container    itemIdCon;

    SysLookupMultiSelectCtrl   itemMultiSelect;

    FormBuildStringControl     itemCtrl;

    FormStringControl          fsCtrlItemId,fsCtrlwarehouse, fsCtrlFromExpectedDate,fsCtrlToExpectedDate;


    DialogField dialogFromDate;

    DialogField dialogToDate;

    DialogField dialogwarehouse;


    ItemId                      itemId;

    InventLocationId            warehouse;

    DateExpected                fromExpectedDate,ToExpectedDate;


    #define.CurrentVersion(2)

    #localMacro.CurrentList

    itemIdCon,

    warehouse,

    fromExpectedDate,

    ToExpectedDate

    #endmacro

}

//Constructer method

public static KDZ_ClearPhysicalReserveBatch construct()

{

    return new KDZ_ClearPhysicalReserveBatch();

}

//calling method

public static void main(Args _args)

{

    KDZ_ClearPhysicalReserveBatch  clearPhysicalReserveBatch = KDZ_ClearPhysicalReserveBatch::construct();


    if (clearPhysicalReserveBatch.prompt())

    {

        clearPhysicalReserveBatch.run();

    }

}


//description of the dialog

client server static ClassDescription description()

{

    return "Return physical reserved product quantity";

}


// Pack and unPack

public container pack()

{

    return [#CurrentVersion, #CurrentList];

}

public boolean unpack(container packedClass)

{

    Version version = RunBase::getVersion(packedClass);

    switch(version)

    {

        case #CurrentVersion:

            [version, #CurrentList] = packedClass;

            break;


        default:

            return false;


    }

    return true;

}


// dialog method

protected Object dialog()

{

    DialogRunbase  dialog;

    DialogGroup    dialogGroup;

    FormBuildControl  formBuildControl;


    #define.itemId('Item Id')

    #define.warehouse('Warehouse')

    #define.fromExpectedDate('From Expected date')

    #define.ToExpectedDate('To Expected date')

    dialog = super();

    dialog.allowUpdateOnSelectCtrl(true);

    dialogGroup             = dialog.addGroup();

    formBuildControl        = dialog.formBuildDesign().control(dialogGroup.formBuildGroup().id());

    itemCtrl                = formBuildControl.addControl(FormControlType::String, identifierStr(itemId));

    dialogwarehouse         = dialog.addFieldValue(extendedTypeStr(InventLocationId), warehouse);

    dialogFromDate          = dialog.addFieldValue(extendedTypeStr(DateExpected), fromExpectedDate);

    dialogToDate            = dialog.addFieldValue(extendedTypeStr(DateExpected), ToExpectedDate);


    itemCtrl.label(#itemId);

    dialogwarehouse.label(#warehouse);

    dialogFromDate.label(#fromExpectedDate);

    dialogToDate.label(#ToExpectedDate);


    return dialog;

}


//dialogPostRun method

public void dialogPostRun(DialogRunbase dialog)

{

    FormRun                formRun;


    Query                  itemQuery;

    QueryBuildDataSource   qbdsItemId;

    QueryBuildRange        qbrItemId;


    Query                  warehouseQuery;

    QueryBuildDataSource   qbdswarehouse;


    itemQuery = new Query();

    qbdsItemId = itemQuery.addDataSource(tableNum(InventTable));

    qbdsItemId.addSelectionField(fieldNum(InventTable, ItemId));


    warehouseQuery = new Query();

    qbdswarehouse = warehouseQuery.addDataSource(tableNum(InventLocation));

    qbdswarehouse.addSelectionField(fieldNum(InventLocation, InventLocationId));


    super(dialog);


    formRun = dialog.dialogForm().formRun();


    if (formRun)

    {

        fsCtrlItemId        = formRun.design().control(itemCtrl.id());


        itemMultiSelect = SysLookupMultiSelectCtrl::constructWithQuery(formRun, fsCtrlItemId, itemQuery);

    }

}


// getFromDialog()

public boolean getFromDialog()

{

    container      selectitemIdCon;

    boolean ret;


    itemIdCon = conNull();


    ret = super();


    warehouse = dialogwarehouse.value();

    fromExpectedDate = dialogFromDate.value();

    ToExpectedDate = dialogToDate.value();


    this.getItemIDs(itemMultiSelect.get());


    return ret;

}


//Validation

public boolean validateParameters()

 {

     boolean isValid = true;


     if (fromExpectedDate && ToExpectedDate && fromExpectedDate > ToExpectedDate)

     {

         isValid = checkFailed(strFmt("@SYS300457", date2StrUsr(fromExpectedDate, DateFlags::FormatAll), date2StrUsr(ToExpectedDate, DateFlags::FormatAll)));

     }

     return isValid;

 }


// logic method

public void deletePhysicalReserve(container _itemIdcon,

                                   InventLocationId _warehouse,

                                   DateExpected _fromExpectedDate,

                                   DateExpected _ToExpectedDate)

{

    int                     i;

    ItemId                  itemIdLoc;

    RetailStatementTable    retailStatementTable;

    InventTrans             inventTrans;

    InventTable             inventTable;

    InventDim               inventDim;

    InventTransOrigin       inventTransOrigin;

    TransDate               fromDate,defaultFromDate;

    TransDate               toDate,defaultToDate;

    int                     j;

    container               itemDeleted;


    defaultFromDate     =  mkDate(01,01,1901);

    defaultToDate       =  systemDateGet();

    fromDate            = _fromExpectedDate;

    toDate              = _ToExpectedDate;


    itemDeleted = conNull();


    if (_itemIdcon && _warehouse && _fromExpectedDate && _ToExpectedDate)

    {

        for (i =1;i <= conLen(_itemIdcon); i++)

        {

            itemIdLoc = conPeek(_itemIdcon, i);


            ttsBegin;

            while select forUpdate inventTrans where inventTrans.ItemId == itemIdLoc &&

                                                    inventTrans.StatusIssue == StatusIssue::ReservPhysical &&

                                                    inventTrans.DatePhysical == dateNull() &&

                                                    inventTrans.DateFinancial == dateNull() &&

                                                    (inventTrans.DateExpected >= fromDate && inventTrans.DateExpected <= _ToExpectedDate)

            {

                  select firstOnly RecId,ReferenceCategory,ItemId,ReferenceId from inventTransOrigin

                                           where inventTransOrigin.RecId == inventTrans.InventTransOrigin &&

                                                 inventTransOrigin.ReferenceCategory == InventTransType::Statement &&

                                                 inventTransOrigin.ItemId == inventTrans.ItemId;


                   select firstOnly inventDimId,configId,InventLocationId,RecId from inventDim

                                    where inventDim.inventDimId == InventTrans.inventDimId &&

                                          //inventDim.configId == _configId &&

                                          inventDim.InventLocationId == _warehouse;

                   select firstOnly RecId,statementId from retailStatementTable

                                                     where retailStatementTable.statementId == inventTransOrigin.ReferenceId;


                   if (inventTrans.ItemId && inventTransOrigin.RecId && inventDim.RecId && !retailStatementTable.RecId)

                   {

                           if (inventTrans.validateDelete())

                           {

                               inventTrans.delete();


                               if (!conFind(itemDeleted, inventTrans.ItemId))

                               {

                                   info(strFmt("Item ID %1 has been returned", inventTrans.ItemId));

                                   itemDeleted += [inventTrans.ItemId];

                               }

                           }

                   }

            }

            ttsCommit;

        }

    }



 if (!itemDeleted)

    {

        info("No records found");

    }

}


//run method

public void run()

{

    super();


    if (this.validateParameters())

    {

        this.deletePhysicalReserve(itemIdCon,warehouse,fromExpectedDate,ToExpectedDate);

    }

    else

    {

        throw error('Please fill the parameters');

    }

}


//

public void getItemIDs(container _itemRecidCon)

{

    InventTable   inventTable;

    RecId         itemRecId;

    int           i;


    for (i = 1; i <= conLen(_itemRecidCon);i++)

    {

        itemRecId = conPeek(_itemRecidCon, i);


        select firstOnly inventTable where inventTable.RecId == itemRecId;

        if (inventTable.ItemId)

        {

               itemIdCon += [inventTable.ItemId];

        }

    }

}


Note: Post deletion of the on-hand physical inventory transactions, the Qty will automatically added into physical available on the product.