public void PrepareToAdd(Customer customer, Guid originStore) { if (customer.Addresses.IsNull() || customer.Addresses.Count.IsZero()) { throw new ArgumentException("Endereço inválido. Informe ao menos 1"); } customer.IsValid(); customer.OriginStore = originStore; customer.SaveDate = DateTime.Now; customer.UpdateDate = DateTime.Now; customer.Status = true; AddressCustomer contactAddress = customer.Addresses.Where(a => a.Purpose == AddressPurposeType.Contact).FirstOrDefault(); if (!contactAddress.IsNull() && !contactAddress.Street.IsNullOrWhiteSpace()) { contactAddress.AddressCustomerCode = Guid.NewGuid(); bool containFaxPhone = false; contactAddress.Phones.ForEach(p => { if (p.PhoneType == PhoneType.Fax) { containFaxPhone = true; } }); if (!containFaxPhone) { contactAddress.Phones.Add(new Phone() { CodeCountry = string.Empty, DDD = string.Empty, Number = string.Empty, PhoneType = PhoneType.Fax }); } } customer.Addresses = new List <AddressCustomer>(); ClearCaractersSpecialAddress(contactAddress); customer.Addresses.Add(contactAddress); customer.Addresses.ForEach(address => address.RemoveSpecialCharacter()); customer.AddressData = customer.Addresses.Serialize().RemoveHeaderXML(); if (customer is Person) { ((Person)customer).BirthDate = ((Person)customer).BirthDate.Value.Date; } customer.Account = null; }
public Customer PrepareToUpdate(Store currentStore, Customer customer, Customer newCustomer) { if (newCustomer.Addresses.IsNull() || newCustomer.Addresses.Count.IsZero()) { throw new ArgumentException("Endereço inválido. Informe ao menos 1"); } newCustomer.IsValid(currentStore); if (newCustomer is Person) { var newPerson = (Person)newCustomer; var person = (Person)customer; person.Transfer(newPerson); } else { if (newCustomer is Company && newCustomer.GetType() != customer.GetType())//register info incomplete { customer = new Company((Person)customer); ((Company)customer).Transfer((Company)newCustomer); } else { ((Company)customer).Transfer((Company)newCustomer); } } customer.UpdateDate = DateTime.Now; customer.Status = true; customer.Addresses = customer.AddressData.Deserialize <List <AddressCustomer> >(); #region " Address " var contactAddressRegistered = customer.Addresses.Where(a => a.Purpose == AddressPurposeType.Contact).FirstOrDefault(); List <AddressCustomer> lstAddress = new List <AddressCustomer>(); AddressCustomer newContactAddress = newCustomer.Addresses.Where(a => a.Purpose == AddressPurposeType.Contact).FirstOrDefault(); if (!newContactAddress.IsNull() && !newContactAddress.Street.IsNullOrWhiteSpace()) { if (!contactAddressRegistered.IsNull() && !contactAddressRegistered.AddressCustomerCode.IsEmpty()) { newContactAddress.AddressCustomerCode = contactAddressRegistered.AddressCustomerCode; newContactAddress.UpdateDate = DateTime.Now; } else { newContactAddress.AddressCustomerCode = Guid.NewGuid(); newContactAddress.SaveDate = DateTime.Now; newContactAddress.UpdateDate = DateTime.Now; } #region " Phones " bool containFaxPhone = false; newContactAddress.Phones.ForEach(p => { if (p.PhoneType == PhoneType.Fax) { containFaxPhone = true; } }); if (!containFaxPhone) { AddressCustomer contactAddressResgistered = newCustomer.Addresses.Where(a => a.Purpose == AddressPurposeType.Contact).FirstOrDefault(); if (!contactAddressResgistered.IsNull()) { Phone fax = contactAddressResgistered.Phones.Where(p => p.PhoneType == PhoneType.Fax).FirstOrDefault(); if (!fax.IsNull()) { newContactAddress.Phones.Add(new Phone() { CodeCountry = fax.CodeCountry, DDD = fax.DDD, Number = fax.Number, PhoneType = PhoneType.Fax }); } } } #endregion lstAddress.Add(newContactAddress); } List <AddressCustomer> lstShippingAddress; if (currentStore.IsMain) { lstShippingAddress = newCustomer.Addresses.Where(a => a.Purpose != AddressPurposeType.Contact).ToList(); newCustomer.Addresses.ForEach(a => { if (!a.AddressCustomerCode.IsEmpty()) { a.UpdateDate = DateTime.Now; } else { a.AddressCustomerCode = Guid.NewGuid(); a.SaveDate = DateTime.Now; a.UpdateDate = DateTime.Now; } }); } else { lstShippingAddress = customer.Addresses.Where(a => a.Purpose != AddressPurposeType.Contact).ToList(); } if (!lstShippingAddress.IsNull()) { lstAddress.AddRange(lstShippingAddress); } lstAddress.ForEach(address => address.RemoveSpecialCharacter()); customer.Addresses = lstAddress; customer.AddressData = lstAddress.Serialize().RemoveHeaderXML(); #endregion if (customer is Person) { ((Person)customer).BirthDate = ((Person)customer).BirthDate.Value.Date; } return(customer); }