miércoles, 21 de mayo de 2014

Ax 2012 consumir webservice para crear empleados en c#

string carpeta = @"carpeta";
            string archivo = @"archivo excel";
            HcmWorkerImportServiceClient proxy = new HcmWorkerImportServiceClient();
         
            CallContext context = new CallContext();
            context.Company = "Company";

            string firstName, lastName, personelNumber, jobTitle;
            int records;

            firstName = "";
            lastName = "";
            personelNumber = "";
            jobTitle = "";
            records = 0;

            //Create COM Objects. Create a COM object for everything that is referenced
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(carpeta+archivo);
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            //iterate over the rows and columns and print to the console as it appears in the file
            //excel is not zero based!!
            for (int i = 1; i <= rowCount; i++)
            {
                string str=Convert.ToString(xlRange.Cells[i, 4].Value2());

                if (str == null || i == 1)
                {
                    continue;
                }
                firstName = xlRange.Cells[i, 1].Value2() == null ? "" : xlRange.Cells[i, 1].Value2().ToString();  //a            
                lastName = xlRange.Cells[i, 2].Value2() == null ? "" : xlRange.Cells[i, 2].Value2().ToString(); //b
                string company = xlRange.Cells[i, 6].Value2() == null ? "" : xlRange.Cells[i, 6].Value2().ToString();//f              
                personelNumber = xlRange.Cells[i, 4].Value2() == null ? "" : xlRange.Cells[i, 4].Value2().ToString();//d
                //personelNumber = "te1" + personelNumber;
                jobTitle = xlRange.Cells[i, 5].Value2() == null ? "" : xlRange.Cells[i, 5].Value2().ToString();//e
                string calle = xlRange.Cells[i, 10].Value2() == null ? "" : xlRange.Cells[i, 10].Value2().ToString();//j
                string calle2 = xlRange.Cells[i, 11].Value2() == null ? "" : xlRange.Cells[i, 11].Value2().ToString();//k
                string city = xlRange.Cells[i, 12].Value2() == null ? "" : xlRange.Cells[i, 12].Value2().ToString();//l
                string state = xlRange.Cells[i, 13].Value2() == null ? "" : xlRange.Cells[i, 13].Value2().ToString();//m
                string zipcode = xlRange.Cells[i, 14].Value2() == null ? "" : xlRange.Cells[i, 14].Value2().ToString();//n
                string phone = xlRange.Cells[i, 15].Value2() == null ? "" : xlRange.Cells[i, 15].Value2().ToString();//o
                string email = xlRange.Cells[i, 18].Value2() == null ? "" : xlRange.Cells[i, 18].Value2().ToString();//r
                string phonework = xlRange.Cells[i, 16].Value2() == null ? "" : xlRange.Cells[i, 16].Value2().ToString();//p
                string phonecell = xlRange.Cells[i, 17].Value2() == null ? "" : xlRange.Cells[i, 17].Value2().ToString();//q
                string Departamento = xlRange.Cells[i, 8].Value2() == null ? "" : xlRange.Cells[i, 8].Value2().ToString();//h
                string location = xlRange.Cells[i, 19].Value2() == null ? "" : xlRange.Cells[i, 19].Value2().ToString();//s

                   
                AxdHcmWorkerImport worker = new AxdHcmWorkerImport();
                AxdEntity_HcmWorker hcmWorkerTable = new AxdEntity_HcmWorker();
                AxdEntity_DirPerson_DirPerson party = new AxdEntity_DirPerson_DirPerson();
                                     
                party.NameAlias = firstName + " " + lastName;//a and b
                party.ProfessionalTitle = jobTitle;//e
                //party.PartyNumber = "EMP-123";
                //party.Gender = AxdEnum_Gender.Female;
                //party.MaritalStatus = AxdEnum_DirPersonMaritalStatus.Married;

                AxdEntity_DirPersonName personName = new AxdEntity_DirPersonName();
                personName.FirstName = firstName;//a
                personName.LastName = lastName;//b

                party.DirPersonName = personName;

                ///// postal
                AxdEntity_DirPartyPostalAddressView postalview = new AxdEntity_DirPartyPostalAddressView();
                postalview.Street = calle + ", " + calle2;//j and k
                postalview.City = city; // l
                postalview.State = state;//m
                postalview.ZipCode = zipcode;//n
                postalview.CountryRegionId = "USA";
                //postalview.StreetNumber = "123";
                postalview.RecIdSpecified = false;
                postalview.Roles = "Home";
                /////
                party.DirPartyPostalAddressView = new AxdEntity_DirPartyPostalAddressView[] { postalview };              
                ////contact info
                ///// postal
                /*
                AxdEntity_DirPartyContactInfoView emailInfo = new AxdEntity_DirPartyContactInfoView();
                emailInfo.Type = AxdEnum_LogisticsElectronicAddressMethodType.Email;
                emailInfo.Roles = "Invoice";
                emailInfo.Locator = "anybody@mail.org";
                emailInfo.IsPrimary = AxdEnum_NoYes.No;
                emailInfo.LocatorExtension = "";
                emailInfo.LocationName = "Primary Email";
                */
                /*
                AxdEntity_DirPartyContactInfoView phoneInfo = new AxdEntity_DirPartyContactInfoView();
                phoneInfo.Type = AxdEnum_LogisticsElectronicAddressMethodType.Phone;
                phoneInfo.Roles = "Business";
                phoneInfo.Locator = "999";              
                phoneInfo.IsPrimary = AxdEnum_NoYes.Yes;
                phoneInfo.LocatorExtension = "456";
                phoneInfo.LocationName = "Primary Phone";              
                //phoneInfo.IsPrivateSpecified = true;
                //phoneInfo.IsLocationOwner = AxdEnum_NoYes.Yes;
                phoneInfo.IsPrivate = AxdEnum_NoYes.Yes;
                party.DirPartyContactInfoView = new AxdEntity_DirPartyContactInfoView[] {  phoneInfo };
                 * */
                /////////////////

                hcmWorkerTable.DirPerson = party;
                hcmWorkerTable.PersonnelNumber = personelNumber;//d;
             

                AxdEntity_HcmEmployment personEmployment = new AxdEntity_HcmEmployment();
                personEmployment.EmploymentType = AxdEnum_HcmEmploymentType.Employee;
                personEmployment.LegalEntity = company;//f
               
                AxdType_DimensionAttributeValueSet  dimset = new AxdType_DimensionAttributeValueSet();
               
                AxdType_DimensionAttributeValue dimva1 = new AxdType_DimensionAttributeValue();
                dimva1.Name = "Department";
                dimva1.Value = Departamento;


                AxdType_DimensionAttributeValue dimva2 = new AxdType_DimensionAttributeValue();
                //dimva2.Name = "LocationSite";
                dimva2.Name = "Location";
                dimva2.Value = location;

                dimset.Values = new AxdType_DimensionAttributeValue[] { dimva1, dimva2 };
                personEmployment.DefaultDimension = dimset;
               
                hcmWorkerTable.HcmEmployment = new AxdEntity_HcmEmployment[1] { personEmployment };
             
                worker.HcmWorker = new AxdEntity_HcmWorker[1] { hcmWorkerTable };
               
                try
                {
                    proxy.create(context, worker);
                    StreamWriter log = new StreamWriter(carpeta + "\\Log.txt", true);                  
                    log.WriteLine(company+" "+personelNumber + " " +"Created");
                    log.Flush();
                    log.Close();
                    System.Console.WriteLine(company + " " + personelNumber + " " + "Created");
                    records++;
                }
                catch(Exception e)
                {
                 

                    StreamWriter log = new StreamWriter(carpeta  +"\\Log.txt", true);
                    log.WriteLine(DateTime.Now.ToString() + " Error");
                    log.WriteLine(company + " " + personelNumber + " " + e.Message);
                    log.Flush();
                    log.Close();

                    //throw;
                }
            }

            //cleanup
            GC.Collect();
            GC.WaitForPendingFinalizers();

            //rule of thumb for releasing com objects:
            //  never use two dots, all COM objects must be referenced and released individually
            //  ex: [somthing].[something].[something] is bad

            //release com objects to fully kill excel process from running in the background
            Marshal.ReleaseComObject(xlRange);
            Marshal.ReleaseComObject(xlWorksheet);

            //close and release
            xlWorkbook.Close();
            Marshal.ReleaseComObject(xlWorkbook);

            //quit and release
            xlApp.Quit();
            Marshal.ReleaseComObject(xlApp);
            Console.WriteLine("Records Inserted" + records);
            Console.ReadKey();

No hay comentarios:

Publicar un comentario