Thursday, January 21, 2016

Product Category Update ax 2012


//CSV

static void UpdateInsertProdCatCSV(Args _args)
{
    EcoResProduct                   ecoResProduct;
    EcoResProductCategory           ecoResProductCategory;
    ecoResCategoryHierarchy         ecoResCategoryHierarchy;
    Name                            itemName,CatHrName,CatName;
    CommaTextIO                 csvFile;
    container                   readCon;
    counter                     icount,inserted;
    Dialog                      dialog;
    DialogField                 dfFileName;
    FileName                    fileName;
    ItemId      itemId;
     #File

    dialog = new Dialog("PicK CSV file");
    dfFileName = dialog.addField(extendedTypeStr("FilenameOpen"));
    dialog.filenameLookupFilter(["All files", #AllFiles]);
    info(curext());
    if (dialog.run())
    {
        csvFile = new CommaTextIo(dfFileName.value(), 'r');
        csvFile.inFieldDelimiter(',');
        readCon = csvFile.read();
        ttsBegin;
        while(csvFile.status() == IO_Status::OK)
        {
            readCon = csvFile.read();
            if(readCon)
            {
                icount++;
                itemName  = conpeek(readCon,1);
                CatHrName  = conpeek(readCon,2);
                CatName  = conpeek(readCon,3);
                select forUpdate ecoResProduct
                    where ecoResProduct.DisplayProductNumber ==  itemName;   // Item Number
                select forUpdate ecoResProductCategory
                    where ecoResProductCategory.Product == EcoResProduct::findByDisplayProductNumber(ecoResProduct.DisplayProductNumber).RecId;
                if(ecoResProductCategory)
                {
                    ttsBegin;
                    ecoResProductCategory.Product = EcoResProduct::findByDisplayProductNumber(ecoResProduct.DisplayProductNumber).RecId;
                    ecoResCategoryHierarchy = ecoResCategoryHierarchy::findByName(CatHrName);
                    ecoResProductCategory.CategoryHierarchy = ecoResCategoryHierarchy.RecId;
                        ecoResProductCategory.Category = EcoResCategory::findByName(CatName,ecoResCategoryHierarchy.RecId).RecId;
                        ecoResProductCategory.update();
                    ttsCommit;
                }
                if(!ecoResProductCategory)
                {
                    ecoResProductCategory.Product = EcoResProduct::findByDisplayProductNumber(ecoResProduct.DisplayProductNumber).RecId;
                    ecoResCategoryHierarchy = ecoResCategoryHierarchy::findByName(CatHrName);
                    ecoResProductCategory.CategoryHierarchy = ecoResCategoryHierarchy.RecId;
                    // Category Nam
                    ecoResProductCategory.Category = EcoResCategory::findByName(CatName,ecoResCategoryHierarchy.RecId).RecId;
                    ecoResProductCategory.insert();
                }
            }
        }
        ttsCommit;
    }
}

//Job - One time update

static void UpdateInsertProductCategoryHierarchy(Args _args)
{
    EcoResProduct _EcoResProduct;
    EcoResProductCategory _EcoResProductCategory;
    EcoResCategoryHierarchy EcoResCategoryHierarchy;
    ;
    select forUpdate _EcoResProduct
    where _EcoResProduct.DisplayProductNumber == "AS000000001";   // Item Number
    select forUpdate _EcoResProductCategory
    where _EcoResProductCategory.Product == EcoResProduct::findByDisplayProductNumber(_EcoResProduct.DisplayProductNumber).RecId;
    if(_EcoResProductCategory)
    {
        ttsBegin;
        _EcoResProductCategory.Product = EcoResProduct::findByDisplayProductNumber(_EcoResProduct.DisplayProductNumber).RecId;
        EcoResCategoryHierarchy = EcoResCategoryHierarchy::findByName("PROCUREMENT");
        _EcoResProductCategory.CategoryHierarchy = EcoResCategoryHierarchy.RecId;
          _EcoResProductCategory.Category = EcoResCategory::findByName('ALL',EcoResCategoryHierarchy.RecId).RecId;
           _EcoResProductCategory.update();
        ttsCommit;
    }
    if(!_EcoResProductCategory)
    {
        ttsBegin;
        _EcoResProductCategory.Product = EcoResProduct::findByDisplayProductNumber(_EcoResProduct.DisplayProductNumber).RecId;
        EcoResCategoryHierarchy = EcoResCategoryHierarchy::findByName("PROCUREMENT");//Category Hierarchy Name
        _EcoResProductCategory.CategoryHierarchy = EcoResCategoryHierarchy.RecId;
        // Category Nam
        _EcoResProductCategory.Category = EcoResCategory::findByName('ALL',EcoResCategoryHierarchy.RecId).RecId;
        _EcoResProductCategory.insert();
        ttsCommit;
    }
}

//EXCEL

static void UpdateInsertProductCategoryHierarchy(Args _args)
{
    EcoResProduct                   ecoResProduct;
    EcoResProductCategory           ecoResProductCategory;
    ecoResCategoryHierarchy         ecoResCategoryHierarchy;
    Name                            itemName,CatHrName,CatName;
    SysExcelApplication application;
    SysExcelWorkbooks   workbooks;
    SysExcelWorkbook    workbook;
    SysExcelWorksheets  worksheets;
    SysExcelWorksheet   worksheet;
    SysExcelCells       cells;
    COMVariantType      type;
    int                 row = 1;
    int i=0;
    Dialog              dialog;
    DialogField         dfFileName;
    Filename template;
    #File

    dialog = new Dialog("Select Excel to Import/Update Product Category");
    dfFileName = dialog.addField(extendedTypeStr("FilenameOpen"));
    dialog.filenameLookupFilter(["All files", #AllFiles]);
    if (dialog.run())
    {
        template = dfFileName.value();
        application = SysExcelApplication::construct();
        workbooks = application.workbooks();
        try
        {
            workbooks.open(template);
        }
        catch (Exception::Error)
        {
            throw error("File cannot be opened.");
        }
        workbook = workbooks.item(1);
        worksheets = workbook.worksheets();
        worksheet = worksheets.itemFromNum(1);
        cells = worksheet.cells();
        do
        {
            row++;
            itemName  = cells.item(row,3).value().bStr();
            CatHrName  = "Procurement";
            CatName  = cells.item(row,2).value().bStr();
            select forUpdate ecoResProduct
            where ecoResProduct.DisplayProductNumber ==  itemName;   // Item Number
            select forUpdate ecoResProductCategory
            where ecoResProductCategory.Product == EcoResProduct::findByDisplayProductNumber(ecoResProduct.DisplayProductNumber).RecId;
            if(ecoResProductCategory)
            {
                ttsBegin;
                ecoResProductCategory.Product = EcoResProduct::findByDisplayProductNumber(ecoResProduct.DisplayProductNumber).RecId;
                ecoResCategoryHierarchy = ecoResCategoryHierarchy::findByName(CatHrName);
                ecoResProductCategory.CategoryHierarchy = ecoResCategoryHierarchy.RecId;
                    ecoResProductCategory.Category = EcoResCategory::findByName(CatName,ecoResCategoryHierarchy.RecId).RecId;
                    ecoResProductCategory.update();
                ttsCommit;
            }
            if(!ecoResProductCategory)
            {
                ttsBegin;
                ecoResProductCategory.Product = EcoResProduct::findByDisplayProductNumber(ecoResProduct.DisplayProductNumber).RecId;
                ecoResCategoryHierarchy = ecoResCategoryHierarchy::findByName(CatHrName);
                ecoResProductCategory.CategoryHierarchy = ecoResCategoryHierarchy.RecId;
                // Category Nam
                ecoResProductCategory.Category = EcoResCategory::findByName(CatName,ecoResCategoryHierarchy.RecId).RecId;
                ecoResProductCategory.insert();
                ttsCommit;
            }
            i++;
            type = cells.item(row+1, 1).value().variantType();
        }
        while (type != COMVariantType::VT_EMPTY);
        application.quit();
        info(strFmt("No of Record inserted %1",i));
    }
}

No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...