public ShopifyCustomer ToShopify(Customer customer)
        {
            if (customer == null)
            {
                return(null);
            }

            var shopifyCustomer = new ShopifyCustomer();

            shopifyCustomer.FirstName = customer.Number.ToString();

            if (!string.IsNullOrEmpty(customer.DisplayName))
            {
                shopifyCustomer.LastName = customer.DisplayName;
            }

            if (!string.IsNullOrEmpty(customer.PhoneNumber))
            {
                shopifyCustomer.Phone = customer.PhoneNumber;
            }

            if (!string.IsNullOrEmpty(customer.Email))
            {
                shopifyCustomer.Email = customer.Email;
            }

            return(shopifyCustomer);
        }
예제 #2
0
        public Customer FindAcumaticaCustomer(ShopifyCustomer shopifyCustomer)
        {
            var customersByIdJson = _customerClient.SearchCustomerByCustomerId(shopifyCustomer.ShopifyCustomerId.ToString());
            var customersById     = customersByIdJson.DeserializeFromJson <List <Customer> >();

            if (customersById.Count == 1)
            {
                return(customersById.First());
            }

            var customersByEmailJson = _customerClient.SearchCustomerByEmail(shopifyCustomer.ShopifyPrimaryEmail);
            var customers            = customersByEmailJson.DeserializeFromJson <List <Customer> >();

            if (customers.Count == 0)
            {
                return(null);
            }
            if (customers.Count == 1)
            {
                _logService.Log(LogBuilder.FoundAcumaticaCustomerByEmail(shopifyCustomer.ShopifyPrimaryEmail));
            }
            else
            {
                _logService.Log(LogBuilder.MultipleCustomersWithSameEmail(shopifyCustomer.ShopifyPrimaryEmail));
            }

            return(customers.First());
        }
예제 #3
0
        public ShopifyCustomer UpsertCustomer(Customer customer)
        {
            using (var transaction = _orderRepository.BeginTransaction())
            {
                var existingCustomer = _orderRepository.RetrieveCustomer(customer.id);

                if (existingCustomer == null)
                {
                    var newCustomer = new ShopifyCustomer();
                    newCustomer.ShopifyCustomerId   = customer.id;
                    newCustomer.ShopifyPrimaryEmail = customer.email;
                    newCustomer.DateCreated         = DateTime.UtcNow;
                    newCustomer.LastUpdated         = DateTime.UtcNow;
                    newCustomer.NeedsCustomerPut    = true;

                    _orderRepository.InsertCustomer(newCustomer);
                    _shopifyJsonService.Upsert(ShopifyJsonType.Customer, customer.id, customer.SerializeToJson());
                    transaction.Commit();
                    return(newCustomer);
                }
                else
                {
                    existingCustomer.ShopifyPrimaryEmail = customer.email;
                    existingCustomer.NeedsCustomerPut    = true;
                    existingCustomer.LastUpdated         = DateTime.UtcNow;

                    _orderRepository.SaveChanges();
                    _shopifyJsonService.Upsert(ShopifyJsonType.Customer, customer.id, customer.SerializeToJson());
                    transaction.Commit();
                    return(existingCustomer);
                }
            }
        }
예제 #4
0
        public AcumaticaCustomer CreateAcumaticaCustomerRecord(ShopifyCustomer shopifyRecord, Customer acumaticaCustomer)
        {
            using (var transaction = _acumaticaOrderRepository.BeginTransaction())
            {
                var newRecord = new AcumaticaCustomer();

                newRecord.AcumaticaCustomerId       = acumaticaCustomer.CustomerID.value;
                newRecord.AcumaticaMainContactEmail = acumaticaCustomer.MainContact.Email.value;
                newRecord.DateCreated = DateTime.UtcNow;
                newRecord.LastUpdated = DateTime.UtcNow;

                shopifyRecord.AcumaticaCustomer = newRecord;
                shopifyRecord.NeedsCustomerPut  = false;

                _acumaticaOrderRepository.InsertCustomer(newRecord);

                _acumaticaJsonService.Upsert(
                    AcumaticaJsonType.Customer,
                    acumaticaCustomer.CustomerID.value,
                    acumaticaCustomer.SerializeToJson());

                transaction.Commit();
                return(newRecord);
            }
        }
예제 #5
0
        public AcumaticaCustomer PushCustomer(ShopifyCustomer shopifyRecord)
        {
            if (shopifyRecord.HasMatch())
            {
                // Already matched - push updates to Acumatica
                //
                UpdateCustomerInAcumatica(shopifyRecord);
                return(shopifyRecord.AcumaticaCustomer);
            }
            else
            {
                // No matching record
                //
                var customerInAcumatica = FindAcumaticaCustomer(shopifyRecord);

                if (customerInAcumatica == null)
                {
                    // Unable to locate Customer in Acumatica? Push brand new Customer to Acumatica API
                    //
                    _logService.Log(LogBuilder.CreateAcumaticaCustomer(shopifyRecord));
                    var newCustomer       = BuildCustomer(shopifyRecord);
                    var newCustomerResult = _customerClient.WriteCustomer(newCustomer);

                    // Then create a SQL record thereof
                    //
                    var acumaticaRecord = CreateAcumaticaCustomerRecord(shopifyRecord, newCustomerResult);

                    return(acumaticaRecord);
                }
                else
                {
                    // Found Customer in Acumatica! Create a SQL record for it...
                    //
                    _logService.Log(LogBuilder.AutomatchingCustomers(shopifyRecord, customerInAcumatica));
                    var acumaticaRecord = CreateAcumaticaCustomerRecord(shopifyRecord, customerInAcumatica);

                    // ... and then now push an update from Shopify into Acumatica
                    // TODO - make this update in Acumatica optional
                    //
                    UpdateCustomerInAcumatica(shopifyRecord);

                    return(acumaticaRecord);
                }
            }
        }
예제 #6
0
        private Customer BuildCustomer(ShopifyCustomer customerRecord)
        {
            var shopifyCustomer = _shopifyJsonService.RetrieveCustomer(customerRecord.ShopifyCustomerId);

            var name     = shopifyCustomer.first_name + " " + shopifyCustomer.last_name;
            var settings = _settingsRepository.RetrieveSettings();

            var customer = new Customer();
            var newAcumaticaCustomerId
                = customerRecord.AcumaticaCustomer == null
                    ? customerRecord.ShopifyCustomerId.ToString()
                    : customerRecord.AcumaticaCustomer.AcumaticaCustomerId;

            customer.CustomerID   = newAcumaticaCustomerId.ToValue();
            customer.CustomerName = name.ToValue();
            //customer.TaxZone = settings.AcumaticaTaxZone.ToValue();

            customer.CustomerClass = settings.AcumaticaCustomerClass.ToValue();

            var address = new Address();

            if (shopifyCustomer.default_address != null)
            {
                var shopifyAddress = shopifyCustomer.default_address;

                address.AddressLine1 = shopifyAddress.address1.ToValue();
                address.AddressLine2 = shopifyAddress.address2.ToValue();
                address.City         = shopifyAddress.city.ToValue();
                address.State        = shopifyAddress.province.ToValue();
                address.PostalCode   = shopifyAddress.zip.ToValue();
                address.CountryID    = shopifyAddress.country_code.ToValue();
            }

            var mainContact = new Contact();

            mainContact.Address   = address;
            mainContact.FirstName = shopifyCustomer.first_name.ToValue();
            mainContact.LastName  = shopifyCustomer.last_name.ToValue();
            mainContact.Phone1    = shopifyCustomer.phone.ToValue();
            mainContact.Email     = shopifyCustomer.email.ToValue();

            customer.MainContact = mainContact;
            customer.AccountRef  = $"Shopify Customer #{shopifyCustomer.id}".ToValue();
            return(customer);
        }
예제 #7
0
        public void UpdateCustomerInAcumatica(ShopifyCustomer shopifyRecord)
        {
            _logService.Log(LogBuilder.UpdateAcumaticaCustomer(shopifyRecord));

            using (var transaction = _acumaticaOrderRepository.BeginTransaction())
            {
                var acumaticaCustomer = BuildCustomer(shopifyRecord);
                var result            = _customerClient.WriteCustomer(acumaticaCustomer);

                var acumaticaRecord = shopifyRecord.AcumaticaCustomer;
                acumaticaRecord.AcumaticaMainContactEmail = acumaticaCustomer.MainContact.Email.value;
                acumaticaRecord.LastUpdated = DateTime.UtcNow;

                shopifyRecord.NeedsCustomerPut = false;
                _syncOrderRepository.SaveChanges();

                _acumaticaJsonService.Upsert(
                    AcumaticaJsonType.Customer, acumaticaRecord.AcumaticaCustomerId, result.SerializeToJson());
                transaction.Commit();
            }
        }
예제 #8
0
 public static string LogDescriptor(this ShopifyCustomer customer)
 {
     return($"Shopify Customer {customer.ShopifyCustomerId}");
 }
예제 #9
0
 public static string UpdateAcumaticaCustomer(ShopifyCustomer customer)
 {
     return($"Updating Acumatica Customer from {customer.LogDescriptor()}");
 }
예제 #10
0
 public static string AutomatchingCustomers(ShopifyCustomer shopify, Customer acumatica)
 {
     return($"Auto-matching {shopify.LogDescriptor()} with {acumatica.LogDescriptor()}");
 }
예제 #11
0
 public void InsertCustomer(ShopifyCustomer customer)
 {
     Entities.ShopifyCustomers.Add(customer);
     Entities.SaveChanges();
 }
예제 #12
0
 /// <summary>
 /// Updates the specified customer.
 /// </summary>
 /// <param name="customer">The customer.</param>
 /// <returns></returns>
 public ShopifyCustomer Update(ShopifyCustomer customer)
 {
     return(base.Update(customer, (long)customer.Id));
 }
예제 #13
0
 /// <summary>
 /// Creates the specified customer.
 /// </summary>
 /// <param name="customer">The customer.</param>
 /// <returns></returns>
 public ShopifyCustomer Create(ShopifyCustomer customer)
 {
     return(base.Create(customer));
 }
예제 #14
0
 public static AcumaticaCustomer Match(this ShopifyCustomer input)
 {
     return(input.AcumaticaCustomer);
 }
예제 #15
0
 public static string AcumaticaCustId(this ShopifyCustomer input)
 {
     return(input.HasMatch() ? input.Match().AcumaticaCustomerId : null);
 }
예제 #16
0
 public static bool HasMatch(this ShopifyCustomer input)
 {
     return(input.Match() != null);
 }