Monday 7 May 2018

Import Vendor postal Address in Ax 2012

void clicked()
{
    SysExcelApplication         application;
    SysExcelWorkbooks           workbooks;
    SysExcelWorkbook            workbook;
    SysExcelWorksheets          worksheets;
    SysExcelWorksheet           worksheet;
    SysExcelCells               cells;
    COMVariantType              type1;
    Name                        name;
    FileName                    filename;
    VendTable                   vendorTable;
    DirPartyTable               dirPartyTable1;
    LogisticsPostalAddress      postalAddress;
    LogisticsLocation           location;
    LogisticsLocationRole       locationRole;
    DirPartyLocationRole        partyLocationRole;
    DirPartyLocation            partyLocation;
 

    int                         row;
    str                         VendAccount;
    str                         vendnameName,vendGroup,currency;
    str                         site,warehouse,payterid,paymode;
    str  100                    Address,Description;
    Dialog                      _dialog;
    DialogField                 _file;

     str COMVariant2Str(COMVariant _cv, int _decimals = 0,int _characters = 0,int _separator1 = 0,int _separator2 = 0)
       {
            switch(_cv.variantType())
            {
                case (COMVariantType::VT_BSTR):
                    return _cv.bStr();
                case (COMVariantType::VT_R4):
                    return num2str(_cv.float(),_characters,_decimals, _separator1,_separator2);
                case (COMVariantType::VT_R8):
                    return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
                case (COMVariantType::VT_DECIMAL):
                    return num2str(_cv.decimal(),_characters,_decimals, _separator1, _separator2);
                case (COMVariantType::VT_DATE):
                    return date2str(_cv.date(),123,2,1,2, 1,4);
                case (COMVariantType::VT_EMPTY):
                    return "";
                default:
                    throw error(strfmt("@SYS26908",_cv.variantType()));
            }
            return "";
        }
    ;
    _dialog = new Dialog("Please select the file to load");
    _dialog.addText("Select file:");
    _file = _dialog.addField(ExtendedTypeStr("FilenameOpen"));
    _dialog.run();

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    filename = _file.value();
    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }
    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();
    row =1;
    do
    {
        row++;
        VendAccount     = cells.item(row, 1).value().bStr();
        vendnameName    = cells.item(row, 2).value().bStr();
        vendGroup       = COMVariant2Str(cells.item(row, 3).value());
        site            = COMVariant2Str(cells.item(row, 4).value());
        warehouse       = COMVariant2Str(cells.item(row, 5).value());
        currency        = cells.item(row, 6).value().bStr();
        payterid        = cells.item(row, 7).value().bStr();
        paymode         = cells.item(row, 8).value().bStr();
        Address         = cells.item(row, 9).value().bStr();
        Description     = cells.item(row, 10).value().bStr();
        select vendorTable where vendorTable.AccountNum == cells.item(row, 1).value().bStr();


        if(vendorTable.AccountNum == VendAccount)
        {
            select forupdate dirPartyTable1 join vendorTable  where vendorTable.AccountNum == cells.item(row, 1).value().bStr()
                                                                   && dirPartyTable1.RecId == vendorTable.Party;
            if(dirPartyTable1.RecId)
            {
                ttsBegin;
                dirPartyTable1.Name        = vendnameName;
                dirPartyTable1.update();
                ttsCommit;
            }
            select forupdate postalAddress join location join dirPartyTable1 join  vendorTable where vendorTable.AccountNum == cells.item(row, 1).value().bStr()
                                                                                                        && vendorTable.Party == dirPartyTable1.RecId
                                                                                                        && Location.RecId == dirPartyTable1.PrimaryAddressLocation
                                                                                                        && postalAddress.Location == location.RecId;


            if(postalAddress.RecId)
            {
                ttsBegin;
                postalAddress.Address      = Address;
                postalAddress.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
                postalAddress.update();
                ttsCommit;
            }
             if(Location.RecId)
              {
                   ttsBegin;
                   Location.Description       = Description;
                   Location.selectForUpdate(true);
                   Location.update();
                   ttsCommit;
              }
            vendorTable.VendGroup      = vendGroup;
            vendorTable.Currency       = currency;
            vendorTable.InventSiteId   = site;
            vendorTable.InventLocation = warehouse;
            vendorTable.PaymTermId     = payterid;
            vendorTable.PaymMode       = paymode;
            ttsBegin;
            vendorTable.selectForUpdate(true);
            vendorTable.update();
            ttsCommit;
            info("Updated Successfully");
        }
        else
        {
            dirPartyTable1.clear();
            dirPartyTable1.initValue();
            dirPartyTable1.Name             = vendnameName;
            dirPartyTable1.insert();

            location.clear();
            location.initValue();
            location.Description            = Description;
            location.IsPostalAddress        = NoYes::Yes;
            location.insert();

            postalAddress.clear();
            postalAddress.initValue();

            postalAddress.Address           = Address;
            postalAddress.CountryRegionId   = "IND";
            postalAddress.Street            ="Kukatpally";
            postalAddress.State             ="Telanaga";
            postalAddress.Location          = location.RecId;
            postalAddress.insert();

            partyLocation.clear();
            partyLocation.initValue();
            partyLocation.Party             = dirPartyTable1.RecId;
            partyLocation.Location          = location.RecId;
            partyLocation.IsPostalAddress   = NoYes::Yes;
            partyLocation.IsPrimary         = NoYes::Yes;
            partyLocation.insert();

            partyLocationRole.clear();
            partyLocationRole.initValue();
            partyLocationRole.LocationRole  = LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Business).RecId;
            partyLocationRole.PartyLocation = partyLocation.RecId;
            partyLocationRole.insert();

            vendorTable.AccountNum     = VendAccount;
            vendorTable.VendGroup      = vendGroup;
            vendorTable.Currency       = currency;
            vendorTable.InventSiteId   = site;
            vendorTable.InventLocation = warehouse;
            vendorTable.PaymTermId     = payterid;
            vendorTable.PaymMode       = paymode;
            vendorTable.Party          = dirPartyTable1.RecId;
            vendorTable.insert();
            info("Record is inserted");
        }
            type1 = cells.item(row+1, 1).value().variantType();

    }
    while (type1 != COMVariantType::VT_EMPTY);

    application.quit();
    super();
}

No comments:

Post a Comment