public async Task <IActionResult> Create(Sale sale)
        {
            if (ModelState.IsValid)
            {
                sale.User = await this.userHelper.GetUserByEmailAsync(this.User.Identity.Name);

                await saleRepository.CreateAsync(sale);

                return(RedirectToAction(nameof(Index)));
            }
            return(View(sale));
        }
Ejemplo n.º 2
0
        public async Task CreateSaleAsync(long customerId, Dictionary <int, int> products)
        {
            Sale sale = new Sale
            {
                CustomerId = customerId,
                Total      = products.Values.Sum()
            };
            int saleId = await _saleRepository.CreateAsync(sale);

            IEnumerable <Bill> bills = products.Select(x => new Bill
            {
                SaleId    = saleId,
                ProductId = x.Key,
                Quantity  = x.Value
            });

            _billRepository.Create(bills);
        }
Ejemplo n.º 3
0
        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);
        }
    }
}