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); }
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()); }
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); } } }
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); } }
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); } } }
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); }
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(); } }
public static string LogDescriptor(this ShopifyCustomer customer) { return($"Shopify Customer {customer.ShopifyCustomerId}"); }
public static string UpdateAcumaticaCustomer(ShopifyCustomer customer) { return($"Updating Acumatica Customer from {customer.LogDescriptor()}"); }
public static string AutomatchingCustomers(ShopifyCustomer shopify, Customer acumatica) { return($"Auto-matching {shopify.LogDescriptor()} with {acumatica.LogDescriptor()}"); }
public void InsertCustomer(ShopifyCustomer customer) { Entities.ShopifyCustomers.Add(customer); Entities.SaveChanges(); }
/// <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)); }
/// <summary> /// Creates the specified customer. /// </summary> /// <param name="customer">The customer.</param> /// <returns></returns> public ShopifyCustomer Create(ShopifyCustomer customer) { return(base.Create(customer)); }
public static AcumaticaCustomer Match(this ShopifyCustomer input) { return(input.AcumaticaCustomer); }
public static string AcumaticaCustId(this ShopifyCustomer input) { return(input.HasMatch() ? input.Match().AcumaticaCustomerId : null); }
public static bool HasMatch(this ShopifyCustomer input) { return(input.Match() != null); }