public async Task <ValidationResult> ValidateDeleteAddressAsync(DeleteAddressInput input) { ValidationResult validationResult = new(); if (string.IsNullOrWhiteSpace(input.CustomerEmail)) { validationResult.Messages.Add(new(nameof(DeleteAddressInput.CustomerEmail), "Debe indicar un cliente.")); } else { Customer customer = await _customerRepository.GetCustomer(input.CustomerEmail); if (customer is null) { validationResult.Messages.Add(new(nameof(DeleteAddressInput.CustomerEmail), "No existe el cliente.")); } AddressCustomer addressCustomer = await _addressRepository.GetByCustomerAsync(input.CustomerEmail); if (addressCustomer is null || addressCustomer.Addresses is null || !addressCustomer.Addresses.Any(x => x.Id == input.AddressId)) { validationResult.Messages.Add(new(nameof(DeleteAddressInput.AddressId), "La dirección no existe.")); } } return(validationResult); }
public AddressCustomer Transfer() { var addressCustomer = new AddressCustomer() { AddressCustomerCode = this.AddressCustomerCode, AddressGlobalIdCode = 0, City = this.City, Complement = this.Complement, ContactName = this.ContactName, Country = this.Country, District = this.District, ExternalType = ExternalType.AddressExternal, Name = this.Name, Number = this.Number, Others = this.Others, Purpose = this.Purpose, Removed = this.Removed, State = this.State, Status = this.Status, Street = this.Street, Type = this.Type, ZipCode = this.ZipCode, Phones = this.Phones.Select(p => p.Transfer()).ToList() }; return(addressCustomer); }
public void ClearCaractersSpecialAddress(AddressCustomer addressCustomer) { addressCustomer.City = !addressCustomer.City.IsNullOrWhiteSpace() ? addressCustomer.City.Replace("\0", "").Replace("�", "").Replace("\\0", "") : ""; addressCustomer.Name = !addressCustomer.Name.IsNullOrWhiteSpace() ? addressCustomer.Name.Replace("\0", "").Replace("�", "").Replace("\\0", "") : ""; addressCustomer.ContactName = !addressCustomer.ContactName.IsNullOrWhiteSpace() ? addressCustomer.ContactName.Replace("\0", "").Replace("�", "").Replace("\\0", "") : ""; addressCustomer.Number = !addressCustomer.Number.IsNullOrWhiteSpace() ? addressCustomer.Number.Replace("\0", "").Replace("�", "").Replace("\\0", "") : ""; addressCustomer.District = !addressCustomer.District.IsNullOrWhiteSpace() ? addressCustomer.District.Replace("\0", "").Replace("�", "").Replace("\\0", "") : ""; addressCustomer.Street = !addressCustomer.Street.IsNullOrWhiteSpace() ? addressCustomer.Street.Replace("�", "").Replace("\0", "").Replace("\\0", "") : ""; addressCustomer.Country = !addressCustomer.Country.IsNullOrWhiteSpace() ? addressCustomer.Country.Replace("\0", "").Replace("�", "").Replace("\\0", "") : ""; addressCustomer.ZipCode = !addressCustomer.ZipCode.IsNullOrWhiteSpace() ? addressCustomer.ZipCode.Replace("\0", "").Replace("�", "").Replace("\\0", "") : ""; addressCustomer.Complement = !addressCustomer.Complement.IsNullOrWhiteSpace() ? addressCustomer.Complement.Replace("\0", "").Replace("�", "").Replace("\\0", "") : ""; }
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 async Task <ValidationResult> ValidateUpdateAddressAsync(UpdateAddressInput input) { ValidationResult validationResult = new(); if (string.IsNullOrWhiteSpace(input.CustomerEmail)) { validationResult.Messages.Add(new(nameof(UpdateAddressInput.CustomerEmail), "Debe indicar un cliente.")); } else { Customer customer = await _customerRepository.GetCustomer(input.CustomerEmail); if (customer is null) { validationResult.Messages.Add(new(nameof(UpdateAddressInput.CustomerEmail), "No existe el cliente.")); } AddressCustomer addressCustomer = await _addressRepository.GetByCustomerAsync(input.CustomerEmail); if (addressCustomer is null || addressCustomer.Addresses is null || !addressCustomer.Addresses.Any(x => x.Id == input.AddressId)) { validationResult.Messages.Add(new(nameof(UpdateAddressInput.AddressId), "La dirección no existe.")); } } if (string.IsNullOrWhiteSpace(input.Name)) { validationResult.Messages.Add(new(nameof(UpdateAddressInput.Name), "Debe ingresar un nombre.")); } if (string.IsNullOrWhiteSpace(input.ZipCode)) { validationResult.Messages.Add(new(nameof(UpdateAddressInput.ZipCode), "Debe ingresar un código postal.")); } if (string.IsNullOrWhiteSpace(input.City)) { validationResult.Messages.Add(new(nameof(UpdateAddressInput.City), "Debe ingresar una ciudad.")); } if (string.IsNullOrWhiteSpace(input.Street)) { validationResult.Messages.Add(new(nameof(UpdateAddressInput.Street), "Debe ingresar una calle.")); } if (!Enum.IsDefined(input.Type)) { validationResult.Messages.Add(new(nameof(UpdateAddressInput.Type), "El tipo de dirección no es valido.")); } return(validationResult); }
public Address(AddressCustomer customer) { AddressCustomerCode = customer.AddressCustomerCode; City = customer.City; Complement = customer.Complement; ContactName = customer.ContactName; Country = customer.Country; District = customer.District; Name = customer.Name; Number = customer.Number; Others = customer.Others; Purpose = customer.Purpose; Removed = customer.Removed; SaveDate = customer.SaveDate; State = customer.State; Status = customer.Status; Street = customer.Street; Type = customer.Type; UpdateDate = customer.UpdateDate; ZipCode = customer.ZipCode; Phones = customer.Phones.Select(p => new Phone(p)).ToList(); }
/// <summary> /// Creates member at AH from data returned by SFG, returns true if member already exists /// </summary> /// <param name="member"></param> /// <returns></returns> public static bool CreateMember(Member member) { using (SupportDataDataContext context = new SupportDataDataContext(ConfigurationManager.ConnectionStrings["AHT_MainConnectionString"].ConnectionString)) { var customer = (from a in context.Customers where a.cusUserName == member.UserName select a).SingleOrDefault(); var sfglink = (from a in context.SFG_CustomerNumbers where a.SFGCustNum == member.MemberId select a).SingleOrDefault(); if (customer == null && sfglink != null) { //someone else already has that sfgid return(false); } try { if (customer == null) { AddressCustomer address = new AddressCustomer(); address.addAddress1 = member.Address.Address1; address.addAddress2 = member.Address.Address2; address.addAddress3 = member.Address.Address3; address.addCity = member.Address.City; address.addCountry = member.Address.Country; address.addDateCreated = DateTime.Now; address.addDateUpdated = DateTime.Now; address.addPostalCode = member.Address.PostalCode; address.addRegion = member.Address.State; context.AddressCustomers.InsertOnSubmit(address); context.SubmitChanges(); var custnum = (from a in context.Customers select a.cusCustNum).Max(); int nextcustnum = int.Parse(custnum) + 1; customer = new Customer(); customer.addID = address.addID; customer.cusCustNum = nextcustnum.ToString(); customer.cusFirstName = member.FirstName; customer.cusLastName = member.LastName; customer.cusCustType = "UNKNOWN"; customer.cusPriFirstName = member.FirstName; customer.cusPriLastName = member.LastName; customer.cusPrefix = member.Salutation; customer.cusSuffix = member.Suffix; customer.cusEmail = member.Email; customer.cusUserName = member.UserName; customer.cusIsCharterMem = false; customer.cusIsDeleted = false; customer.cusSex = 'U'; customer.cusHasDisplayName = true; customer.cusDisplayName = member.UserName; customer.cusGUID = Guid.NewGuid(); context.Customers.InsertOnSubmit(customer); context.SubmitChanges(); } if (sfglink == null) { sfglink = new SFG_CustomerNumber(); sfglink.SFGCustNum = member.MemberId; sfglink.cusID = customer.cusID; context.SFG_CustomerNumbers.InsertOnSubmit(sfglink); context.SubmitChanges(); } } catch { return(false); } } return(true); }
public async Task <ValidationResult> CreateSaleAsync(CreateSaleInput input) { ValidationResult validationResult = new(); if (!Enum.IsDefined(input.PaymentProvider)) { validationResult.Messages.Add(new(nameof(CreateSaleInput.PaymentProvider), "El proveedor de pago no es valido.")); } if (string.IsNullOrWhiteSpace(input.CustomerEmail)) { validationResult.Messages.Add(new(nameof(CreateSaleInput.CustomerEmail), "Debe indicar un cliente.")); } else { Customer customer = await _customerRepository.GetCustomer(input.CustomerEmail); if (customer is null) { validationResult.Messages.Add(new(nameof(CreateSaleInput.CustomerEmail), "No existe el cliente.")); } else { AddressCustomer addressCustomer = await _addressRepository.GetByCustomerAsync(input.CustomerEmail); Address address = addressCustomer.Addresses?.SingleOrDefault(x => x.Id == input.AddressId); if (address is null) { validationResult.Messages.Add(new(nameof(CreateSaleInput.AddressId), "La dirección no existe.")); } } } if (!string.IsNullOrWhiteSpace(input.CouponCode)) { CouponCode couponCode = await _couponCodeRepository.GetCouponAsync(input.CouponCode); if (couponCode is null) { validationResult.Messages.Add(new(nameof(CreateSaleInput.CouponCode), "El código de cupón no existe.")); } } if (input.Products is null) { validationResult.Messages.Add(new(nameof(CreateSaleInput.Products), "Debe seleccionar productos.")); } else { foreach (ProductDefinitive productDefinitive in input.Products) { Product product = await _productRepository.GetAsync(productDefinitive.ProductId); if (product is null) { validationResult.Messages.Add(new(nameof(CreateSaleInput.Products), $"El producto con Id {productDefinitive.ProductId} no existe.")); } else if (!product.IsActive) { validationResult.Messages.Add(new(nameof(CreateSaleInput.Products), $"El producto {product.Name} no esta activo.")); } else if (product.Stock <= 0) { validationResult.Messages.Add(new(nameof(CreateSaleInput.Products), $"El producto {product.Name} no tiene stock.")); } else { if (productDefinitive.Variants is not null) { IEnumerable <ProductVariant> variants = await _productVariantRepository.GetByProduct(product); if (variants is not null) { foreach (ProductVariantPair variantPair in productDefinitive.Variants) { ProductVariant productVariant = variants.SingleOrDefault(x => x.Name == variantPair.Name); if (productVariant is null) { validationResult.Messages.Add(new(nameof(CreateSaleInput.Products), $"El producto {product.Name} no tiene variaciones de {variantPair.Name}.")); } else if (!productVariant.Values.Contains(variantPair.Value)) { validationResult.Messages.Add(new(nameof(CreateSaleInput.Products), $"El producto {product.Name} no tiene una variación de {variantPair.Name} con el valor {variantPair.Value}.")); } } } } } } } return(validationResult); }
public async Task <OperationResult <SaleOrderDto> > CreateSaleAsync(CreateSaleInput input) { var validationResult = await _saleValidator.CreateSaleAsync(input); if (validationResult.IsSuccess) { IEnumerable <Product> products = _productRepository.GetAvailableProducts(input.Products.Select(x => x.ProductId)); Customer customer = await _customerRepository.GetCustomer(input.CustomerEmail); AddressCustomer addressCustomer = await _addressRepository.GetByCustomerAsync(input.CustomerEmail); Address address = addressCustomer.Addresses.Single(x => x.Id == input.AddressId); CouponCode couponCode = null; if (!string.IsNullOrWhiteSpace(input.CouponCode)) { couponCode = await _couponCodeRepository.GetCouponAsync(input.CouponCode); } Order order = new() { Status = OrderStatus.Created, CreationDate = DateTime.Now, CustomerEmail = input.CustomerEmail, Type = OrderType.ProductSale, TotalAmount = CalculateTotalAmount(couponCode, products, customer) }; order = await _orderRepository.CreateAsync(order); Sale sale = new() { CouponCode = input.CouponCode, OrderId = order.Id, Products = products.Select(x => x.Id), Status = SaleStatus.PendingPayment }; sale = await _saleRepository.CreateAsync(sale); SaleDetail saleDetail = new() { SaleId = sale.Id, Products = input.Products }; saleDetail = await _saleDetailRepository.CreateAsync(saleDetail); IPaymentProvider paymentProvider = _paymentProviderFactory.CreatePaymentProvider(input.PaymentProvider); Invoice invoice = await paymentProvider.CreateInvoice(order); invoice = await _invoiceRepository.CreateAsync(invoice); Delivery delivery = new() { AddressId = input.AddressId, OrderId = order.Id, Status = DeliveryStatus.Created }; delivery = await _deliveryRepository.CreateAsync(delivery); return(OperationResult <SaleOrderDto> .Success(new(sale.ConvertToDto(), order.ConvertToDto(), invoice.ConvertToDto()))); } return(OperationResult <SaleOrderDto> .Fail(validationResult)); } private static decimal CalculateTotalAmount(CouponCode couponCode, IEnumerable <Product> products, Customer customer) { decimal totalAmount = products.Sum(x => x.Price); if (couponCode is null) { return(totalAmount); } if (totalAmount < couponCode.MinAmount || totalAmount > couponCode.MaxAmount) { return(totalAmount); } if (DateTime.Today < couponCode.DateStart || DateTime.Today > couponCode.DateExpire) { return(totalAmount); } List <Product> productsToOff = new(); if (couponCode.Customers?.Contains(customer.Email) ?? false) { productsToOff.AddRange(products); } else { if (couponCode.Categories?.Any() ?? false) { productsToOff.AddRange(products.Where(x => couponCode.Categories.Contains(x.Category))); } if (couponCode.SubCategories?.Any() ?? false) { productsToOff.AddRange(products.Where(x => couponCode.SubCategories.Any(y => y.Category == x.Category && y.SubCategory == x.SubCategory))); } if (couponCode.Products?.Any() ?? false) { productsToOff.AddRange(products.Where(x => couponCode.Products.Contains(x.Id))); } } decimal valueOff = couponCode.Type switch { CouponCodeOffType.Percentage => (couponCode.Value / productsToOff.Sum(x => x.Price)) * 100, CouponCodeOffType.SpecificAmount => productsToOff.Sum(x => x.Price) - couponCode.Value, _ => throw new ArgumentException("CouponCodeOffType invalido.", nameof(couponCode)) }; return(totalAmount - valueOff); } } }
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); }