Wednesday, July 8, 2020

Customer Excel upload

static void GH_UploadCustomerRegion(Args _args)
{

    SysExcelApplication application;
    SysExcelWorkbooks   workbooks;
    SysExcelWorkbook    workbook;
    SysExcelWorksheets  worksheets;
    SysExcelWorksheet   worksheet;
    SysExcelCells       cells;
    int                 row;
    CommaTextIO         csvFile;
    container           readCon;
    counter             icount,updated;
    Dialog              dialog;
    DialogField         dfFileName;


    FileName            fileName;
    Name                name;

    DirPartyContactInfoView   contactView;
    CustTable                 CustTable;
    DirParty                  dirParty;
    DirPartyRecId             partyRecId;

    LogisticsPostalAddress    address;
    DirPartyPostalAddressView addressView;

    updated                  = 0;

    #File

    application = SysExcelApplication::construct();
    workbooks   = application.workbooks();
    dialog      = new Dialog("Pick the file");
    dfFileName  = dialog.addField(extendedTypeStr("FilenameOpen"));
    dialog.filenameLookupFilter(["All files", #AllFiles]);


    if (dialog.run())
    {
      fileName = dfFileName.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();

    ttsBegin;
    {

    row++;
    name       = strLRTrim(cells.item(row, 2).value().bStr());
    partyRecId = DirPartyTable::createNew( DirPartyType::Organization, name).RecId;

    custTable.clear();
    custTable.initValue();
    custTable.Party         = partyRecId;
    custTable.AccountNum    = strLRTrim(cells.item(row, 1).value().bStr());
    custTable.CustGroup     = strLRTrim(cells.item(row, 3).value().bStr());
    custTable.Currency      = strLRTrim(cells.item(row, 4).value().bStr());
    custTable.DlvMode       = strLRTrim(cells.item(row, 5).value().bStr());
    custTable.PaymTermId    = strLRTrim(cells.item(row, 6).value().bStr());
    custTable.update();

    custTable               = Custtable::find(strLRTrim(cells.item(row, 1).value().bStr()));
    partyRecId              = custTable.Party;

    DirParty                = DirParty::constructFromPartyRecId(partyRecId );
    address.clear();
    //address.PostBox = strLRTrim(conPeek(readCon,13));
    address.CountryRegionId = strLRTrim(cells.item(row, 7).value().bStr());
    if( address.CountryRegionId != "")
    {
        address.State           = strLRTrim(cells.item(row, 8).value().bStr());
        address.ZipCode         = strLRTrim(cells.item(row, 9).value().bStr());
        address.Street          = strLRTrim(cells.item(row, 10).value().bStr());
        address.City            = strLRTrim(cells.item(row, 11).value().bStr());

        //address.Address = conPeek(readCon,4);

        addressView.LocationName = "Address";
        addressView.IsPrimary    = NoYes::Yes;
        addressView.Party        = partyRecId;
        addressview.initFromPostalAddress(address);


        DirParty                 = DirParty::constructFromPartyRecId(addressView.Party );
        DirParty.createOrUpdatePostalAddress(addressView);
    }

    contactView.clear();

    if(strLRTrim(cells.item(row, 12).value().bStr()) != "")
    {
        contactView.LocationName = "Phone Number";
        contactView.Locator      = strLRTrim(cells.item(row, 12).value().bStr());
        contactView.Type         = LogisticsElectronicAddressMethodType::Phone;
        contactView.Party        = partyRecId;
        contactView.IsPrimary    = NoYes::Yes;
        dirParty.createOrUpdateContactInfo(contactView);
    }

    if(strLRTrim(cells.item(row, 13).value().bStr()) != "")
    {
        contactView.LocationName = "Fax Number";
        contactView.Locator      = strLRTrim(cells.item(row, 13).value().bStr());
        contactView.Type         = LogisticsElectronicAddressMethodType::Fax;
        contactView.Party        = partyRecId;
        contactView.IsPrimary    = NoYes::Yes;
        dirParty.createOrUpdateContactInfo(contactView);
    }

    if(strLRTrim(cells.item(row, 14).value().bStr()) != "")
    {
        contactView.LocationName = "Website";
        contactView.Locator      = strLRTrim(cells.item(row, 14).value().bStr());
        contactView.Type         = LogisticsElectronicAddressMethodType::URL;
        contactView.Party        = partyRecId;
        contactView.IsPrimary    = NoYes::Yes;
        dirParty.createOrUpdateContactInfo(contactView);
    }

    if(strLRTrim(cells.item(row, 15).value().bStr())!= "")
    {
        contactView.LocationName = "Email";
        contactView.Locator      = strLRTrim(cells.item(row, 15).value().bStr());
        contactView.Type         = LogisticsElectronicAddressMethodType::Email;
        contactView.Party        = partyRecId;
        contactView.IsPrimary    = NoYes::Yes;
        dirParty.createOrUpdateContactInfo(contactView);
    }



    }
    ttsCommit;
  }
}

No comments:

Post a Comment