/// <summary> /// Maps a <see cref="CustomerDisplay"/> to <see cref="ICustomer"/> /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="destination"> /// The destination. /// </param> /// <returns> /// The <see cref="ICustomer"/>. /// </returns> internal static ICustomer ToCustomer(this CustomerDisplay customer, ICustomer destination) { if (!customer.Key.Equals(Guid.Empty)) { destination.Key = customer.Key; } destination.FirstName = customer.FirstName; destination.LastName = customer.LastName; // prevent setting email to empty string if (!string.IsNullOrEmpty(customer.Email)) { destination.Email = customer.Email; } // prevent setting login name to empty string if (!string.IsNullOrEmpty(customer.LoginName)) { ((Customer)destination).LoginName = customer.LoginName; } destination.TaxExempt = customer.TaxExempt; destination.LastActivityDate = DateTime.Now; if (customer.ExtendedData != null) { ((Customer)destination).ExtendedData = customer.ExtendedData; } var addressList = new List <ICustomerAddress>(); foreach (var address in customer.Addresses) { var destAddress = destination.Addresses.FirstOrDefault(x => x.Key == address.Key); if (destAddress != null) { destAddress = address.ToCustomerAddress(destAddress); } addressList.Add(destAddress); } ((Customer)destination).Addresses = addressList; destination.Notes = customer.Notes; return(destination); }
/// <summary> /// Converts a Lucene index result into a <see cref="CustomerDisplay"/>. /// </summary> /// <param name="result"> /// The result. /// </param> /// <param name="getInvoices"> /// A function to get the invoice for a customer /// </param> /// <returns> /// The <see cref="CustomerDisplay"/>. /// </returns> internal static CustomerDisplay ToCustomerDisplay(this SearchResult result, Func <Guid, IEnumerable <InvoiceDisplay> > getInvoices) { var customer = new CustomerDisplay() { Key = FieldAsGuid(result, "customerKey"), LoginName = FieldAsString(result, "loginName"), FirstName = FieldAsString(result, "firstName"), LastName = FieldAsString(result, "lastName"), Email = FieldAsString(result, "email"), Notes = RawJsonFieldAsCollection <NoteDisplay>(result, "notes"), TaxExempt = FieldAsBoolean(result.Fields["taxExempt"]), ExtendedData = RawJsonFieldAsCollection <KeyValuePair <string, string> >(result, "extendedData") .AsExtendedDataCollection(), Addresses = RawJsonFieldAsCollection <CustomerAddress>(result, "addresses").Select(x => x.ToCustomerAddressDisplay()), LastActivityDate = FieldAsDateTime(result, "lastActivityDate") }; customer.Invoices = getInvoices.Invoke(customer.Key); return(customer); }
/// <summary> /// Saves any new customer addresses /// </summary> /// <param name="customer"> /// The customer. /// </param> private void SaveNewAddresses(CustomerDisplay customer) { foreach (var address in customer.Addresses.Where(x => x.Key == Guid.Empty)) { var customerAddress = address.ToCustomerAddress(new CustomerAddress(customer.Key)); customerAddress.AddressType = address.AddressType; _customerAddressService.Save(customerAddress); address.Key = customerAddress.Key; } }
/// <summary> /// Removes addresses deleted in the back office. /// </summary> /// <param name="customer"> /// The customer. /// </param> private void RemoveDeletedAddresses(CustomerDisplay customer) { var existing = _customerAddressService.GetByCustomerKey(customer.Key); var existingAddresses = existing as ICustomerAddress[] ?? existing.ToArray(); if (!existingAddresses.Any()) return; foreach (var delete in existingAddresses.Where(address => customer.Addresses.All(x => x.Key != address.Key)).ToList()) { _customerAddressService.Delete(delete); } }
public CustomerDisplay PutCustomer(CustomerDisplay customer) { RemoveDeletedAddresses(customer); SaveNewAddresses(customer); var merchCustomer = _customerService.GetByKey(customer.Key); merchCustomer = customer.ToCustomer(merchCustomer); _customerService.Save(merchCustomer); return merchCustomer.ToCustomerDisplay(); }
public CustomerDisplay AddCustomer(CustomerDisplay customer) { var newCustomer = _customerService.CreateCustomer( string.IsNullOrEmpty(customer.LoginName) ? customer.Email : customer.LoginName, customer.FirstName, customer.LastName, customer.Email); newCustomer.Notes = customer.Notes; newCustomer.LastActivityDate = DateTime.Today; ////((Customer)newCustomer).Addresses = customer.Addresses.Select(x => x.ToCustomerAddress(new CustomerAddress(customer.Key))); _customerService.Save(newCustomer); return newCustomer.ToCustomerDisplay(); }
public IAnonymousCustomer AddAnonymousCustomer(CustomerDisplay customer) { var newCustomer = _customerService.CreateAnonymousCustomerWithKey(); newCustomer.LastActivityDate = DateTime.Today; _customerService.Save(newCustomer); return newCustomer; }
/// <summary> /// Maps a <see cref="CustomerDisplay"/> to <see cref="ICustomer"/> /// </summary> /// <param name="customer"> /// The customer. /// </param> /// <param name="destination"> /// The destination. /// </param> /// <returns> /// The <see cref="ICustomer"/>. /// </returns> internal static ICustomer ToCustomer(this CustomerDisplay customer, ICustomer destination) { if (!customer.Key.Equals(Guid.Empty)) { destination.Key = customer.Key; } destination.FirstName = customer.FirstName; destination.LastName = customer.LastName; // prevent setting email to empty string if (!string.IsNullOrEmpty(customer.Email)) { destination.Email = customer.Email; } // prevent setting login name to empty string if (!string.IsNullOrEmpty(customer.LoginName)) { ((Customer)destination).LoginName = customer.LoginName; } destination.TaxExempt = customer.TaxExempt; destination.LastActivityDate = DateTime.Now; if (customer.ExtendedData != null) { ((Customer)destination).ExtendedData = customer.ExtendedData; } var addressList = new List <ICustomerAddress>(); foreach (var address in customer.Addresses) { var destAddress = destination.Addresses.FirstOrDefault(x => x.Key == address.Key); if (destAddress != null) { destAddress = address.ToCustomerAddress(destAddress); } addressList.Add(destAddress); } ((Customer)destination).Addresses = addressList; // set the note type field key var invoiceTfKey = Constants.TypeFieldKeys.Entity.CustomerKey; foreach (var idn in customer.Notes) { idn.EntityTfKey = invoiceTfKey; } // remove or update any notes that were previously saved and/or removed through the back office var updateNotes = customer.Notes.Where(x => x.Key != Guid.Empty).ToArray(); var notes = destination.Notes.ToList(); var removeKeys = new List <Guid>(); foreach (var n in notes) { var update = updateNotes.FirstOrDefault(x => x.Key == n.Key); if (update == null) { removeKeys.Add(n.Key); } else { n.Message = update.Message; n.InternalOnly = update.InternalOnly; } } notes.AddRange(customer.Notes.Where(x => x.Key == Guid.Empty).Select(x => x.ToNote())); destination.Notes = notes.Where(x => removeKeys.All(y => y != x.Key)); return(destination); }
public CustomerDisplay AddCustomer(CustomerDisplay customer) { var newCustomer = _customerService.CreateCustomer( customer.LoginName, customer.FirstName, customer.LastName, customer.Email); newCustomer.Notes = customer.Notes; newCustomer.LastActivityDate = DateTime.Today; ((Customer)newCustomer).Addresses = customer.Addresses.Select(x => x.ToCustomerAddress()); _customerService.Save(newCustomer); return newCustomer.ToCustomerDisplay(); }
/// <summary> /// Saves any new customer addresses /// </summary> /// <param name="customer"> /// The customer. /// </param> private void SaveNewAddresses(CustomerDisplay customer) { foreach (var address in customer.Addresses.Where(x => x.Key == Guid.Empty)) { _customerAddressService.Save(address.ToCustomerAddress(new CustomerAddress(customer.Key))); } }