Wednesday 16 September 2015

address import(zipcode,city,state,county,country/ region id) insert through excel sheet

static void AddressImport(Args _args)
{
    SysExcelApplication             application;
    SysExcelWorkbooks               workbooks;
    SysExcelWorkbook                workbook;
    SysExcelWorksheets              worksheets;
    SysExcelWorksheet               worksheet;
    SysExcelCells                   cells;
    COMVariantType                  type;
    Name                            name;
    FileName                        filename;
    InventPosting                   InventPosting;

    DimensionAttributeValueCombination DimensionAttributeValueCombination;

    int row =1;
    LogisticsAddressZipCode          zipCode;
    LogisticsAddressZipCodeId        zipCodeId;
    LogisticsAddressCity             city;
    LogisticsAddressStateId          stateId;
    LogisticsAddressState            state;
    LogisticsAddresssCity            cityRecord;
    LogisticsAddressCountyName       countyName;
    LogisticsAddressCountyId         countyId;
    LogisticsAddressCounty           county;

    boolean                          badRecord;
    int                              i=1;
    int                              numProcessedRecords=0;
    LogisticsAddressCountryRegionId  _countryRegionId;

    //LogisticsAddressStateId          stateId;
    //LogisticsAddressCountyId         countyId;
    //LogisticsAddressCountyName       countyName;

    application = SysExcelApplication::construct();
    workbooks   = application.workbooks();

    //specify the file path that you want to read
    filename    = "C:\\Users\\v-irsha\\Desktop\\2009 DMF\\Vendor\\Vend addresses countycountryregidstatecityzipcode WITH NO EMPTY ROWSCOLUMN.xlsx";
    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }

    ttsbegin;
    workbook    = workbooks.item(1);
    worksheets  = workbook.worksheets();
    worksheet   = worksheets.itemFromNum(1); //Here 3 is the worksheet Number
    cells       = worksheet.cells();

    do
    {
        row++;
        zipCodeId        = cells.item(row, 2).value().bStr();
        if (zipCodeId == "")
            zipCodeId = int2str(cells.item(row, 2).value().double());

        stateId          = cells.item(row, 3).value().bStr();
        city             = cells.item(row, 5).value().bStr();
        _countryRegionId = cells.item(row, 1).value().bStr();
       /// switch(_countryRegionId )
        //{
            //case "US" :
            //_countryRegionId = _countryRegionId +"A";
            //break;
            //case "Brazil" :
             //_countryRegionId = "BRA";
            //break;
            //case "Mexico" :
             //_countryRegionId = "MEX";
            //break;
            //case "Colombia" :
             //_countryRegionId = "COL";
            //break;
            //case "Argentina" :
             //_countryRegionId = "ARG";
            //break;
            //case "Japan" :
             //_countryRegionId = "JPN";
            //break;
            //case "Canada" :
             //_countryRegionId = "CAN";
            //break;
            //case "CA" :
             //_countryRegionId = "CAN";
            //break;
            //case "COSTA RICA" :
             //_countryRegionId = "CRI";
            //break;
            //
        //}
        countyId         = cells.item(row, 4).value().bStr();

        if (stateId != '')
            state= LogisticsAddressState::find(_countryRegionId, stateId);
            if (!state.RecId)
            {
                state.CountryRegionId   = _countryRegionId;
                state.StateId           = stateId;
                state.insert();
            }
            // Check if county exists
            if(countyId != '')
            county = LogisticsAddressCounty::find(_countryRegionId, stateId, countyId);
                if(!county.RecId)
        {
            county.CountyId = countyId;
            county.Name = countyId;
            county.CountryRegionId = _countryRegionId;
            county.StateId = stateId;
            county.insert();
        }
            if (city != '')
            {
                select firstonly cityRecord where
                    cityRecord.Name             == city &&
                    cityRecord.StateId          == stateId &&
                    cityRecord.CountryRegionId  == _countryRegionId &&
                    cityRecord.CountyId         == countyId;
            }

            if (!cityRecord.RecId)
            {
                cityRecord.CountryRegionId  = _countryRegionId;
                cityRecord.Name             = city;
                cityRecord.StateId          = stateId;
                cityRecord.CountyId         = countyId;
                cityRecord.insert();
            }

            select firstonly zipCode where
                zipCode.ZipCode         == zipCodeId &&
                zipCode.State           == stateId &&
                zipCode.County          == countyId &&
                zipCode.CountryRegionId == _countryRegionId &&
                zipCode.CityRecId       == cityRecord.RecId;

            if (!zipCode.RecId)
            {
                zipCode.ZipCode         = zipCodeId;
                zipCode.City            = city;
                zipCode.CityRecId       = cityRecord.RecId;
                zipCode.CountryRegionId = _countryRegionId;
                zipCode.State           = stateId;
                zipCode.County          = countyId;
                zipCode.insert();

                numProcessedRecords++;
            }

        type = cells.item(row+1, 1).value().variantType();
    }

    while (type != COMVariantType::VT_EMPTY);

    info(strFmt("%1 Inserted",numProcessedRecords));

    ttsCommit;

    application.quit();
}





new job with streeet also:


static void ImportAddress(Args _args)
{
    SysExcelApplication                 application;
    SysExcelWorkbooks                   workbooks;
    SysExcelWorkbook                    workbook;
    SysExcelWorksheets                  worksheets;
    SysExcelWorksheet                   worksheet;
    SysExcelCells                       cells;
    COMVariantType                      type;
    Name                                name;
    FileName                            filename;

    DimensionAttributeValueCombination  DimensionAttributeValueCombination;

    int row = 1;
    LogisticsAddressZipCode             zipCode;
    LogisticsAddressZipCodeId           zipCodeId;
    LogisticsAddressCity                city;
    LogisticsAddressStateId             stateId;
    LogisticsAddressState               state;
    LogisticsAddresssCity               cityRecord;
    LogisticsAddressCountyName          countyName;
    LogisticsAddressCountyId            countyId;
    LogisticsAddressCounty              county;
    LogisticsAddressStreet_RU           Street;
    Description                         Street_name,Street_id;
    LogisticsPostalAddress              PostalAddress;
    LogisticsAddressing                 Address;
    EffectiveDateTime                   validFrom;
    ExpirationDateTime                  validTo;

    boolean                             badRecord;
    int                                 i = 1;
    int                                 numProcessedRecords = 0;
    LogisticsAddressCountryRegionId     _countryRegionId;

    application = SysExcelApplication::construct();
    workbooks   = application.workbooks();

    //specify the file path that you want to read
    //filename    = "C:\\DMF\\DIXF_vimsin\\Ready to Import\\Imported on 26102015\\AddressMasterDIT01_26102015.xlsx";
    filename    = "C:\\Users\\v-irsha\\Desktop\\28 oct 2015 cust\\AX2009 Customer addresses master.xlsx";//"C:\\DMF\\DIXF_vimsin\\Ready to Import\\Imported on 26102015\\AX2009 Customer addresses master.xlsx";
    //filename    = "C:\\Users\\v-vimsin\\Documents\\Vimal\\DIXF\\LogisticsPostalAddress - Copy.xlsx";
    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }

    ttsbegin;
    workbook    = workbooks.item(1);
    worksheets  = workbook.worksheets();
    worksheet   = worksheets.itemFromNum(1); //Here 1 is the worksheet Number
    cells       = worksheet.cells();

    do
    {
        row++;
        zipCodeId        = cells.item(row, 4).value().bStr();
        if (zipCodeId == "")
            zipCodeId = int2str(cells.item(row, 4).value().double());

        stateId             = cells.item(row, 3).value().bStr();
        city                = cells.item(row, 2).value().bStr();
        _countryRegionId    = cells.item(row, 1).value().bStr();
        countyId            = cells.item(row, 5).value().bStr();
        Street_name         = cells.item(row, 6).value().bStr();
        Address             = cells.item(row, 7).value().bStr();
        validFrom           = str2datetime(cells.item(row, 8).value().bStr(),213);
        validTo             = str2datetime(cells.item(row, 9).value().bStr(),213);

        if (stateId != "")
        {
            state= LogisticsAddressState::find(_countryRegionId, stateId);
            if (!state.RecId)
            {
                state.CountryRegionId   = _countryRegionId;
                state.StateId           = stateId;
                state.insert();
            }
        }
            // Check if county exists
        if (countyId != "")
        {
            county = LogisticsAddressCounty::find(_countryRegionId, stateId, countyId);
            if (!county.RecId)
            {
                // County needs a state
                county.CountryRegionId  = _countryRegionId;
                county.StateId          = stateId;
                county.CountyId         = countyId;
                county.Name             = countyName;
                county.insert();
            }
        }
         // Check if city exists
        if (city != '')
        {
            select firstonly cityRecord where
                cityRecord.Name             == city &&
                cityRecord.StateId          == stateId &&
                cityRecord.CountryRegionId  == _countryRegionId &&
                cityRecord.CountyId         == countyId;

            if (!cityRecord.RecId)
            {
                cityRecord.CountryRegionId  = _countryRegionId;
                cityRecord.Name             = city;
                cityRecord.StateId          = stateId;
                cityRecord.CountyId         = countyId;
                cityRecord.insert();
            }
        }
        // Check if Street exists
        if (Street_name != "")
        {
            Street = LogisticsAddressStreet_RU::find(Street_name, _countryRegionId,stateId,countyId,LogisticsAddresssCity::find(city).RecId,0);
            if (!Street.RecId)
            {
                Street.CountryRegion    = _countryRegionId;
                Street.City             = LogisticsAddresssCity::find(city).RecId;
                Street.State            = stateId;
                Street.County           = countyId;
                Street.Name             = Street_name;
                Street.Description      = Street_name;
                Street.insert();
            }
        }
        // Check if zipCode exists
        if (zipCodeId != "")
        {
            select firstonly zipCode where
                zipCode.ZipCode         == zipCodeId &&
                zipCode.State           == stateId &&
                zipCode.County          == countyId &&
                zipCode.CountryRegionId == _countryRegionId &&
                zipCode.CityRecId       == cityRecord.RecId;

            if (!zipCode.RecId)
            {
                zipCode.ZipCode         = zipCodeId;
                zipCode.City            = city;
                zipCode.CityRecId       = cityRecord.RecId;
                zipCode.CountryRegionId = _countryRegionId;
                zipCode.State           = stateId;
                zipCode.County          = countyId;
                zipCode.StreetName      = Street_name;
                zipCode.insert();

                numProcessedRecords++;
            }
        }
        // insert address into table LogisticsPostalAddress
        /*if (Address != "")
        {
            select firstOnly PostalAddress
                where PostalAddress.CityRecId == cityRecord.RecId
                   && PostalAddress.CountryRegionId == _countryRegionId
                   && PostalAddress.State == state.StateId
                   && PostalAddress.StreetId_RU == Street.RecId
                   && PostalAddress.ZipCodeRecId == zipCode.RecId;
            if (!PostalAddress.RecId)
            {
                PostalAddress.Address           = Address;
                PostalAddress.ValidFrom         = validFrom;
                PostalAddress.ValidTo           = validTo;
                PostalAddress.CityRecId         = cityRecord.RecId;
                PostalAddress.City              = cityRecord.Name;
                PostalAddress.CountryRegionId   = _countryRegionId;
                PostalAddress.State             = state.StateId;
                PostalAddress.County            = county.CountyId;
                PostalAddress.StreetId_RU       = Street.RecId;
                PostalAddress.Street            = Street.Name;
                PostalAddress.ZipCodeRecId      = zipCode.RecId;
                PostalAddress.ZipCode           = zipCode.ZipCode;
                PostalAddress.insert();
            }
        }*/

        type = cells.item(row+1, 1).value().variantType();
    }

    while (type != COMVariantType::VT_EMPTY);

    info(strFmt("%1 Inserted",numProcessedRecords));

    ttsCommit;

    application.quit();
}