Thursday 14 December 2017

update proj dimensions in ax 2012

class ProjDimUpload
{
    SysExcelApplication     application;
    SysExcelWorkbooks       workbooks;
    SysExcelWorkbook        workbook;
    SysExcelWorksheets      worksheets;
    SysExcelWorksheet       worksheet;
    SysExcelCells           cells;
}

.................................................................................

public void importProjDim()
{
    ProjId              projId;

    RecId               recIdDefaultDimension;


    //table
    ProjTable               projTable;

    container   financialDimensionFromExcel;
    container   conDimensionName;
    Dialog                  dialog;
    DialogGroup             dialogGroup;
    DialogField             dialogfilename;
    Filename                filename;
    COMVariantType          type;

    str                     strDate;
    int                     row;

    ;
       startLengthyOperation();
    //variable yang diisi manual
    filename              = "C:\\Proj  excel Dim upload.xlsx";
   


    dialog          = new Dialog("Select File For Proj Master Dim Import");
    dialogGroup     = dialog.addGroup("Excel File");
    dialogfilename  = dialog.addField(extendedTypeStr(FilenameOpen));

    if (dialog.run())
    {
        application = SysExcelApplication::construct();
        workbooks   = application.workbooks();
        filename=dialogfilename.value();
         try
    {
        if (workbooks.open(filename, false /*Update links*/, true /*Read only*/))
        {
            workbook   = workbooks.item(1);
            workSheets = workbook.worksheets();
            workSheet  = workSheets.itemFromNum(1); //worksheet keberapa dari excel di mulai dari angka 1
            cells      = workSheet.cells();
            conDimensionName = this.TIDgetDimensionName();
            row = 2;

            ttsBegin;
            while (cells.item(row,2).value().bStr() != "")
            {
                //financial dimension
                financialDimensionFromExcel = conNull();
                //dimension value 1
                financialDimensionFromExcel += cells.item(row,2).value().bStr();
                //dimension value 2
                financialDimensionFromExcel += cells.item(row,3).value().bStr();
                //dimension value 3
                financialDimensionFromExcel += cells.item(row,4).value().bStr();
                //dimension value 4
                financialDimensionFromExcel += cells.item(row,5).value().bStr();
                //dimension value 5
                financialDimensionFromExcel += cells.item(row,6).value().bStr();
                //dimension value 6
                financialDimensionFromExcel += cells.item(row,7).value().bStr();
                //dimension value 7
                financialDimensionFromExcel += cells.item(row,8).value().bStr();
                //dimension value 8
                financialDimensionFromExcel += cells.item(row,9).value().bStr();
                //dimension value 9
                financialDimensionFromExcel += cells.item(row,10).value().bStr();
                //dimension value 10
                financialDimensionFromExcel += cells.item(row,11).value().bStr();
                //generateDefaultDimension
                recIdDefaultDimension       = this.TIDcreateDefaultDimension(conDimensionName,financialDimensionFromExcel);

                //variable
                //assetId           = this.TIDComVariant2STR(cells.item(row,2).value());
                projId      = this.TIDComVariant2STR(cells.item(row,1).value());

                //table
                projTable      = ProjTable::find(projId);

                //validation
                if(!projTable)
                {
                    throw error(strFmt("Row : %1 ,ProjId not exists, please check again",row));
                }
             

                    projTable.selectForUpdate(true);

                    projTable.DefaultDimension      = recIdDefaultDimension;
                    projTable.update();
                row++;
                }
             
            }
            ttsCommit;
            application.quit();
        }

    catch(Exception::Error)
    {
        info("Proj upload Error");
        application.quit();
    }
    }
    info(strFmt("%1 Dimensions had been updated",row));
}

......................................................................


public  str TIDComVariant2STR(COMVariant _variant)
{
    str valueStr;
    ;

    switch(_variant.variantType())
    {
        case COMVariantType::VT_EMPTY   :
            valueStr = '';
            break;

        case COMVariantType::VT_BSTR    :

            valueStr = _variant.bStr();
            break;

        case COMVariantType::VT_R4      :
        case COMVariantType::VT_R8      :

            if(_variant.double())
            {
                valueStr = strFmt("@SYS311964",
                                    num2Str0(_variant.double(), 0),
                                    num2str(_variant.double(),
                                    0,
                                    numOfDec(_variant.double()),
                                    1,
                                    0));
            }
            break;

        default                         :
            throw error(strfmt("@SYS26908",
                                _variant.variantType()));
    }

    return valueStr;
}


...........................

Public DimensionDefault TIDcreateDefaultDimension(container _attr, container _value, boolean _createIfNotFound = true)
{
    DimensionAttributeValueSetStorage   valueSetStorage = new DimensionAttributeValueSetStorage();
    DimensionDefault                    result;
    int                                 i;
    DimensionAttribute                  dimensionAttribute;
    DimensionAttributeValue             dimensionAttributeValue;
    //_attr is dimension name in table DimensionAttribute
    container               conAttr =   _attr;
    container               conValue = _value;
    str                     dimValue;

    for (i = 1; i <= conLen(conAttr); i++)
    {
        dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,i));

        if (dimensionAttribute.RecId == 0)
        {
            continue;
        }

        dimValue = conPeek(conValue,i);

        if (dimValue != "")
        {
            // _createIfNotFound is "true". A dimensionAttributeValue record will be created if not found.
            dimensionAttributeValue = dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,_createIfNotFound);

            // Add the dimensionAttibuteValue to the default dimension
            valueSetStorage.addItem(dimensionAttributeValue);
        }
    }
    result = valueSetStorage.save();
    return result;
}


..............................

public  container TIDgetDimensionName()
{
    DimensionAttribute              dimAttr;
    DimensionAttributeSetItem       dimAttrSetItem;
    DimensionEnumeration            dimensionSetId;
    DimensionAttributeValue         dimAttributeValue;
    container                       DimensionName;

    dimensionSetId      = DimensionCache::getDimensionAttributeSetForLedger();

    while select dimAttr order by Name
                where dimAttr.Type != DimensionAttributeType::MainAccount
            join RecId from dimAttrSetItem
                where dimAttrSetItem.DimensionAttribute     == dimAttr.RecId &&
                      dimAttrSetItem.DimensionAttributeSet  == dimensionSetId
            {
                dimensionName += dimAttr.Name;

            }

    return DimensionName;
}

.......................................................................

public static void main(Args args)
{
   ProjDimUpload projDimUpload = new ProjDimUpload();
    projDimUpload.importProjDim();
}

No comments:

Post a Comment