示例#1
0
        private void InitEmployee()
        {
            if (Token == null)
            {
                return;
            }

            var connections = Client.GetConnectionsAsync(Token).Result;

            var contacts = _accountingApi.GetContactsAsync(Token.AccessToken, connections[0].TenantId.ToString()).Result;
            var id       = 0;

            _employees.AddRange(
                contacts._Contacts.Select(s =>
                                          new EmployeeModel
            {
                Id        = ++id,
                ContactId = s.ContactID,
                FirstName = s.FirstName,
                LastName  = s.LastName
            }));
        }
示例#2
0
        private async Task ImportContactDetails(string accessToken, string tenantId)
        {
            Console.WriteLine("Imporing contacts into Xero");
            Contacts batchContacts = new Contacts();

            batchContacts._Contacts = new List <Contact>();
            string        connectionString = "Data Source=data.versentia.com;Initial Catalog=Versentia;User ID=VersentiaUser;Password=toomanyhands";
            SqlConnection cnn = new SqlConnection(connectionString);

            cnn.Open();
            //string sql = "SELECT TOP 10 [ClientID],[Name],[PaperLessEmail],[AddressLine1],[City],[State],[Zip] FROM[Versentia].[Accounts].[Clients]";
            string        sql        = "select TOP 500 VAC.ClientID, VAC.Name, VAC1.FirstName, VAC1.LastName, VAC1.Email, VAC1.AddressLine1, VAC1.AddressLine2, VAC1.City, VAC1.State, VAC1.Zip from [Versentia].[Accounts].[Clients] VAC INNER JOIN [Versentia].[Accounts].[ClientContacts] VACC ON VAC.ClientID = VACC.ClientID INNER JOIN [Versentia].[Accounts].[Contacts] VAC1 ON VACC.ContactID = VAC1.ContactID ORDER BY VAC.ClientID";
            SqlCommand    command    = new SqlCommand(sql, cnn);
            SqlDataReader dataReader = command.ExecuteReader();

            var contactlist = await _accountingApi.GetContactsAsync(accessToken, tenantId);

            while (dataReader.Read())
            {
                if (contactlist._Contacts.FindIndex(x => x.ContactNumber == dataReader.GetInt32(0).ToString()) > 0)
                {
                    Console.WriteLine("Contact with contact number already present, not importing : \n" + dataReader.GetInt32(0));
                    continue;
                }
                Contact contact = new Contact
                {
                    ContactNumber = dataReader.GetInt32(0).ToString(),
                    Name          = dataReader.GetString(1),
                    FirstName     = dataReader.IsDBNull(2) ? "" : dataReader.GetString(2),
                    LastName      = dataReader.IsDBNull(3) ? "" : dataReader.GetString(3),
                    EmailAddress  = dataReader.IsDBNull(4) ? "" : dataReader.GetString(4),
                    Addresses     = new List <Address>
                    {
                        new Address
                        {
                            AddressType  = Address.AddressTypeEnum.STREET,
                            AddressLine1 = dataReader.GetString(5),
                            AddressLine2 = dataReader.IsDBNull(6) ? "" : dataReader.GetString(6),
                            City         = dataReader.IsDBNull(7) ? "" : dataReader.GetString(7),
                            Region       = dataReader.IsDBNull(8) ? "" : dataReader.GetString(8),
                            PostalCode   = dataReader.IsDBNull(9) ? "" : dataReader.GetString(9)
                        }
                    }
                };
                if (contactlist._Contacts.FindIndex(x => x.Name == dataReader.GetString(1)) > 0)
                {
                    Console.WriteLine("Contact already preent in Xero \n" + dataReader.GetString(1));
                }
                else
                {
                    Console.WriteLine("Importing contact \n" + dataReader.GetInt32(0).ToString());
                    batchContacts._Contacts.Add(contact);
                }
            }
            dataReader.Close();
            Contacts partialbatchcontacts = new Contacts();

            partialbatchcontacts._Contacts = new List <Contact>();
            for (int i = 0; i < batchContacts._Contacts.Count; i++)
            {
                partialbatchcontacts._Contacts.Add(batchContacts._Contacts[i]);
                if (i % 200 == 0)
                {
                    try
                    {
                        var contacts = await _accountingApi.CreateContactsAsync(accessToken, tenantId, partialbatchcontacts);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message + "\n");
                    }
                    partialbatchcontacts._Contacts.Clear();
                }
            }
            try
            {
                var c = await _accountingApi.CreateContactsAsync(accessToken, tenantId, partialbatchcontacts);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + "\n");
            }
            Console.WriteLine("Finsihed importing contacts");
        }