Friday, August 6, 2021

Add , update and delete BOM lines using X++ job

Belo job is used to add, update and delete bom items 

Make sure the import file is .csv 




static void lanMassupdateBOMlines_2019(Args _args)

{

 CommaTextIo     commaTextIo;

    container values;

    FolderName      sourceFolder = @"C:\MassUpdate";


    System.Array    files;

    int fileCount, i, intdel, intadd;

    real BOMQty;

    Filename    filepath;

    Filename    filename;

    Filename    fileType;

    str         currentFile,itemgroup,colorid,sizeid,bomcolor,bomsize,action,flushprinciple;


        InventTable inventtable;

    BOMVersion bomVersion;

    BOM bom;


    BOMTable bomTable;

  ItemId itemID, bomitemID;

    InventDim           inventDim,inventdimnew,inventdimBOM;

    InventDimCombination inventdimcombination;


    boolean             isFirst, ignore;


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


    try

    {

        files = System.IO.Directory::GetFiles(sourceFolder);


        fileCount = files.get_Length();


        for(i = 0; i < fileCount; i++)

        {

            currentFile = files.GetValue(i);

            commaTextIo = new CommaTextIo(currentFile,'R');

            isFirst = true;


            values = commaTextIo.read();



            ttsBegin;


            while(values != conNull())

            {

    itemID = any2str(conPeek(values, 1));

                  itemgroup = any2str(conPeek(values, 2));

                // get Item master

                    inventtable = InventTable::find(itemID);

  colorid =  any2str(conPeek(values, 4));

                       sizeid =  any2str(conPeek(values, 5));

                             bomitemID = any2str(conPeek(values, 7));

                       bomcolor = any2str(conPeek(values, 8));

                              bomsize =  any2str(conPeek(values, 9));

                BOMQty = str2num(conPeek(values, 10));

                  flushprinciple = any2str(conPeek(values, 11));

                  action =  any2str(conPeek(values, 12));

                 if(colorid == 'ANY' && sizeid == 'ALL' && action == 'DELETE')

                   {

                                 // get list of BOM versions associated to the Item and Site 'CAC'

                  while select * from bomVersion

                      where bomVersion.itemId == inventtable.ItemId && bomVersion.Name like '*CA'


                  {


                          select forUpdate bom  where bom.BOMId == bomVersion.BOMId

                          && bom.ItemId == bomitemID;

                      if (bom)


                          {

                                 ttsBegin;

                                bom.delete();

                              intdel++;


                                ttsCommit;

                          }


                       }


                       }

                else

                       if(colorid != 'ANY' && sizeid == 'ALL' && action == 'DELETE')

                {

                             // get list of BOM versions associated to the Item and Site 'CAC'

                  while select * from bomVersion

                      where bomVersion.itemId == inventtable.ItemId && bomVersion.Name like '*CA'


                  {

                            select * from inventdim where inventDim.inventDimId == bomVersion.InventDimId;


                       //check if BOM line already exists on the BOM Version

                   //select * from bom where bom.ItemId == bomitemID && bom.BOMId == bomVersion.BOMId && bom.BOMQty == 1.02;


                  //  if(!bom)

                  //  {


                        // if no BOM exists create new BOM line



                       if(inventDim.InventColorId == colorid)


                       {


                          select forUpdate bom  where bom.BOMId == bomVersion.BOMId

                          && bom.ItemId == bomitemID;

                      if (bom)


                          {

                                 ttsBegin;

                                bom.delete();

                              intdel++;


                                ttsCommit;

                          }



                      }


                      }

                }


            if(colorid == 'ANY' && sizeid == 'ALL' && action == 'ADD')

                {


                      // get list of BOM versions associated to the Item and Site 'CAC'

                  while select * from bomVersion

                      where bomVersion.itemId == inventtable.ItemId && bomVersion.Name like '*CA'


                  {

                            select * from inventdim where inventDim.inventDimId == bomVersion.InventDimId;


                       //check if BOM line already exists on the BOM Version

                   select * from bom where bom.ItemId == bomitemID && bom.BOMId == bomVersion.BOMId

                   && bom.BOMQty == BOMQty;


                    if(!bom)

                    {


                        // if no BOM exists create new BOM line


                       if(inventDim.InventColorId == colorid || colorid == 'ANY')


                       {

                            bom.clear();

                            inventdimnew.clear();

                            bom.initValue();


                            bom.BOMId = bomVersion.BOMId;

                            bom.ItemId = bomitemID;

                            bom.BOMQty = str2num(conPeek(values, 10));

                            bom.OprNum = 0;

                            bom.UnitId = 'EA';

                            inventdimnew.InventColorId = bomcolor;

                            inventdimnew.InventSiteId = 'CAC';


                               if (bomsize == 'ALL')

                               {

                             inventdimnew.InventSizeId = inventDim.InventSizeId;

                               }

                               else

                               {

                                    inventdimnew.InventSizeId = bomsize;

                               }


                           // inventdimnew.InventLocationId =  inventDim.InventLocationId;

                            inventdimnew.InventLocationId =  'CA';


                            bom.InventDimId = InventDim::findOrCreate(inventdimnew).inventDimId;

                           if (flushprinciple == 'Manual')

                           {

                            bom.ProdFlushingPrincip = 1;

                           }

                           else

                           {

                              bom.ProdFlushingPrincip = 3;

                           }

                                // validate the BOM

                            if(bom.validateWrite())

                            {


                                 bom.insert();

                                 intadd++;


                            }

                            }

                      }

                      }

                    }

                    else


                    {


              if(colorid != 'ANY' && sizeid == 'ALL' && action == 'ADD' )

                {


                      // get list of BOM versions associated to the Item and Site 'CAC'

                  while select * from bomVersion

                      where bomVersion.itemId == inventtable.ItemId && bomVersion.Name like '*CA'


                  {

                            select * from inventdim where inventDim.inventDimId == bomVersion.InventDimId;


                       //check if BOM line already exists on the BOM Version

                 select * from bom where bom.ItemId == bomitemID && bom.BOMId == bomVersion.BOMId && bom.BOMQty == BOMQty;


                if(!bom)

                {


                        // if no BOM exists create new BOM line


                       if(inventDim.InventColorId == colorid)


                       {

                            bom.clear();

                            inventdimnew.clear();

                            bom.initValue();


                            bom.BOMId = bomVersion.BOMId;

                            bom.ItemId = bomitemID;

                            bom.BOMQty = str2num(conPeek(values, 10));

                            bom.OprNum = 0;

                            bom.UnitId = 'EA';

                            inventdimnew.InventColorId = bomcolor;

                            inventdimnew.InventSiteId = 'CAC';


                               if (bomsize == 'ALL')

                               {

                             inventdimnew.InventSizeId = inventDim.InventSizeId;

                               }

                               else

                               {

                                    inventdimnew.InventSizeId = bomsize;

                               }


                            //inventdimnew.InventLocationId =  inventDim.InventLocationId;

                           inventdimnew.InventLocationId =  'CA';

                            bom.InventDimId = InventDim::findOrCreate(inventdimnew).inventDimId;

                           if (flushprinciple == 'Manual')

                           {

                            bom.ProdFlushingPrincip = 1;

                           }

                           else

                           {

                              bom.ProdFlushingPrincip = 3;

                           }

                                // validate the BOM

                            if(bom.validateWrite())

                            {


                                 bom.insert();

                                 intadd++;

                            }

                            }


                      }

                     }


                   }


                }



                values = commaTextIo.read();


            }



            ttsCommit;

          //  Box::info("Success, Imported BOM lines for New Fit HangTag");

            [filepath, filename, fileType] = fileNameSplit(currentFile);


 info(strfmt("Total number of Boms deleted are - %1", intdel));

                 info(strfmt("Total number of BOMs added are - %1", intadd));

        }

    }

    catch

    {

        error("Import process failed");

    }


    CodeAccessPermission::revertAssert();

}

No comments:

Post a Comment