Sunday, 29 March 2020

How to reserve, unreserve item from inventory using x++ code


Hi Viewers,

Today will see how to reserve/Un reserve an item from inventory.

If you want to reserve then pass negative Qty (-1 * Qty)

If you want to Un reserve then pass positive Qty (1 * Qty)


Below sample code is to reserve an item

this.ReserveItem(salesLine, -1 * Qty)

    /// <summary>
    ///     Reserve/Unreserve item
    /// </summary>
    /// <param name = "_salesLine">
    ///     The _salesLine table buffer
    /// </param>
    /// <param name = "_lineQty">
    ///     The _lineQty
    /// </param>
    public void ReserveItem(SalesLine  _salesLine, SalesQty  _lineQty)
    {
        InventUpd_Reservation       inventUpd_Reservation;
        inventMovement              movement;
        InventDim                   inventDimCriteria;
        InventDimParm               inventDimParm;

        movement            = InventMovement::construct(_salesLine);
        inventDimCriteria   = InventDim::find(_salesLine.inventDimId);
        inventDimParm       = movement.initDimParmAutoReserv(inventDimCriteria);

        // Product dimensions must be specified on movement and should be enforced - even when blank.
        inventDimParm.setAllProductDimensions();

        inventUpd_Reservation = InventUpd_Reservation::newMovement(movement, _lineQty, true);
        inventUpd_Reservation.parmInventDimFixed(InventDimFixedClass::inventDimParm2InventDimFixed(inventDimParm));
        inventUpd_Reservation.parmInventDimCriteria(inventDimCriteria);
        inventUpd_Reservation.parmInventDimParm(inventDimParm);
        inventUpd_Reservation.updateNow();
    }

Tuesday, 10 March 2020

Import file from folder using x++ code

Hi viewers,

Today I am going to show, import files from the folder and after importing move files to the corresponding folders(error, success)


public class LLCOMSSecProdFeed extends RunBaseBatch
{
    object                  formRun;

    FilePath                folderPath, destFolderPath, errorFolderPath;
    DialogField             dialogFolderPath, dialogDestFolderPath, dialogErrorFolderPath;

    #DEFINE.CurrentVersion(1)
    #LOCALMACRO.CurrentList
        folderPath,
        destFolderPath,
        errorFolderPath
    #ENDMACRO
}

public Object dialog(   DialogRunbase   _dialog          = null
                    ,   boolean         _forceOnClient   = false
                    )
{
    DialogRunbase dialog;

    dialog  = super();

    dialog.caption("Secondary product feed");

    //Description
    dialog.addGroup("Description").columns(2);
    dialog.addInfoImage();
    dialog.addText("Select folder to extract file", 400);

    //File format
    dialog.addGroup("File format").columns(2);
    dialog.addImage(10890);
    dialog.addText(strFmtLB("@LLC4149"), 400);

    //Folder
    dialog.addGroup("Source directory");
    dialogFolderPath = dialog.addFieldValue(extendedTypeStr(FilePath), folderPath);

    //Archive
    dialog.addGroup("Archieve directory");
    dialogDestFolderPath = dialog.addFieldValue(extendedTypeStr(FilePath), destFolderPath);

    //Error
    dialog.addGroup(“Error directory”);
    dialogErrorFolderPath = dialog.addFieldValue(extendedTypeStr(FilePath), errorFolderPath);

    return dialog;
}

public boolean getFromDialog()
{
    folderPath      = dialogFolderPath.value();
    destFolderPath  = dialogDestFolderPath.value();
    errorFolderPath = dialogErrorFolderPath.value();

    return super();
}

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;
}

public static ClassDescription description()
{
    return "Description";
}

public server static LLCOMSSecProdFeed construct()
{
    return new LLCOMSSecProdFeed();
}

public boolean canGoBatchJournal()
{
    return true;
}

public static void main(args _args )
{
    LLCOMSSecProdFeed       llcomsSecProdFeed;
    ;

    llcomsSecProdFeed = LLCOMSSecProdFeed::construct();

    if(llcomsSecProdFeed.prompt())
        llcomsSecProdFeed.run();
}

public void run()
{
    InteropPermission           interopPerm;
    Counter                     fileCount;
    Counter                     loop, retryCount=0, failedFileCount=0;
    System.IO.DirectoryInfo     directory;
    System.IO.FileInfo[]        files;
    System.IO.FileInfo          file;
    System.DateTime             dateTime;
    str                         fileNameTemp, fileName;
    boolean                     success;
    str                         utcTimeAsStr;

    interopPerm = new InteropPermission(InteropKind::ClrInterop);
    interopPerm.assert();

    folderPath  = @folderPath;
    directory   = new System.IO.DirectoryInfo(folderPath);
    files       = directory.GetFiles('*.csv');

    fileCount   = files.get_Length();
    CodeAccessPermission::revertAssert();

    setPrefix(strFmt("@LLC3298"));

    for (loop = 0; loop < fileCount; loop++)
    {
        file            = files.GetValue(loop);
        fileNameTemp    = file.get_FullName();
        fileName        = file.get_Name();

        setPrefix(strFmt("@LLC3299",fileName));

        success = this.processFile(fileNameTemp);

        System.GC::Collect();
        dateTime = System.DateTime::get_UtcNow();

        utcTimeAsStr = dateTime.ToString('yyyyMMddHHmmss');

        if(success)
        {
            this.moveFile(@fileNameTemp,@destFolderPath + @'\' + utcTimeAsStr + @'_' + @fileName);
        }
        else
        {
            this.moveFile(@fileNameTemp,@errorFolderPath + @'\' + utcTimeAsStr + @'_' + @fileName);
            failedFileCount++;
        }
    }
    if (failedFileCount > 0)
    {
        throw error(strFmt("@LLC3300",failedFileCount));
    }
}

public boolean processFile(Filename _fileNameFrom)
{
    CommaIo                 importFile;
    FileIOPermission        fioPermission;
    Container               record;
    Filename                filenameId;
    ItemId                  itemId;
    boolean                 firstRow;
    boolean                 ret;
    System.Exception        ex;

    #File

    #define.FileColumns(1)

    fioPermission = new FileIOPermission(_fileNameFrom,'r');
    fioPermission.assert();

    importFile = new CommaIo(_fileNameFrom,'r');

    if (!importFile || importFile.status() != IO_Status::Ok)
    {
        throw error(strfmt("@LLC3295",_fileNameFrom));
    }

    importFile.inFieldDelimiter(',');
    filenameId = strLRTrim(conPeek(fileNameSplit(_fileNameFrom), 2));

    firstRow = true;
    ret      = true;

    try
    {
        //Header
        record = importFile.read();

        //wrong format?
        if(conLen(record) != #FileColumns)
            throw error("@SYS98433");

        ttsBegin;

        while (importFile.status() == IO_Status::Ok)
        {
            record = importFile.read();

            //empty record?
            if(record == conNull())
                break;

            if(firstRow == true)
            {
                firstRow = false;
                continue;
            }

            [itemId] = record;

            if(!InventTable::exist(itemId))
            {
                info(strFmt("@LLC4150", filenameId, itemId));

                ret = false;

                continue;
            }

            LLCOMSProdFeedControl::createForItemId(itemId);
        }
        ttsCommit;
        importFile.finalize();
        importFile = null;

        CodeAccessPermission::revertAssert();
    }
    catch
    {
        error(strfmt("@SYS96977"));
        ex = ClrInterop::getLastException();
        if (ex != null)
        {
            ex = ex.get_InnerException();
            if (ex != null)
            {
                error(ex.ToString());
            }
        }

        importFile.finalize();
        importFile = null;

        CodeAccessPermission::revertAssert();
        ret = false;
    }

    return ret;
}

 private void moveFile(str _fileNameFrom, str _fileNameTo)
{
    InteropPermission           interopPerm;

    #File

    interopPerm = new InteropPermission(InteropKind::ClrInterop);
    interopPerm.assert();

    System.IO.File::Move(_fileNameFrom,_fileNameTo);

    CodeAccessPermission::revertAssert();
}