Ejemplo n.º 1
0
        protected async Task <Customer> ExportSingleCustomer(CommitCRM.Account account, SQLiteConnection connection, string remoteHost)
        {
            return(await Task.Factory.StartNew(() =>
            {
                var myNameValueCollection = new NameValueCollection();
                string fullname = account.GetFieldValue("FLDCRDCONTACT");
                myNameValueCollection.Add("business_name", account.CompanyName);
                if (!string.IsNullOrEmpty(fullname) && !string.IsNullOrEmpty(account.LastName))
                {
                    myNameValueCollection.Add("firstname", fullname.Replace(account.LastName, string.Empty));
                }
                else
                {
                    myNameValueCollection.Add("firstname", fullname);
                }
                myNameValueCollection.Add("lastname", account.LastName);
                myNameValueCollection.Add("email", account.EmailAddress1);
                myNameValueCollection.Add("phone", Regex.Replace(account.Phone1, @"[^.0-9\s]", ""));
                myNameValueCollection.Add("mobile", Regex.Replace(account.Phone2, @"[^.0-9\s]", ""));
                myNameValueCollection.Add("address", account.AddressLine1);
                myNameValueCollection.Add("address_2", account.AddressLine2);
                myNameValueCollection.Add("city", account.City);
                myNameValueCollection.Add("state", account.State);
                myNameValueCollection.Add("zip", account.Zip);
                myNameValueCollection.Add("notes", account.Notes);

                var newCustomer = RepairShoprUtils.ExportCustomer(myNameValueCollection, remoteHost);
                if (newCustomer != null)
                {
                    using (SQLiteCommand cmdINewItem = new SQLiteCommand(string.Format("INSERT INTO  Account (AccountId,CustomerId) VALUES('{0}','{1}')", account.AccountREC_ID, newCustomer.Id), connection))
                        cmdINewItem.ExecuteNonQuery();

                    var contacts = new CommitCRM.ObjectQuery <CommitCRM.Contact>();
                    contacts.AddCriteria(CommitCRM.Contact.Fields.ParentAccountREC_ID, CommitCRM.OperatorEnum.opEqual, account.AccountREC_ID);
                    contacts.AddCriteria(CommitCRM.Contact.Fields.AccountType, CommitCRM.OperatorEnum.opEqual, 5);

                    var contactsResult = contacts.FetchObjects();
                    foreach (CommitCRM.Contact contact in contactsResult)
                    {
                        string contactname = contact.GetFieldValue("FLDCRDCONTACT");
                        NameValueCollection contactNameCollection = new NameValueCollection();
                        if (account.EmailAddress1.Contains("@"))
                        {
                            myNameValueCollection.Add("email", contact.EmailAddress1);
                        }
                        contactNameCollection.Add("phone", contact.Phone1);
                        contactNameCollection.Add("mobile", contact.Phone2);
                        contactNameCollection.Add("address", contact.AddressLine1);
                        contactNameCollection.Add("address_2", contact.AddressLine2);
                        contactNameCollection.Add("city", contact.City);
                        contactNameCollection.Add("state", contact.State);
                        contactNameCollection.Add("zip", contact.Zip);
                        contactNameCollection.Add("customer_id", newCustomer.Id);
                        contactNameCollection.Add("name", contactname);

                        RepairShoprUtils.ExportContact(contactNameCollection, remoteHost);
                    }
                }

                return newCustomer;
            }));
        }