Example #1
0
        private async Task <decimal> ApplyDiscount(User user, Cart cart)
        {
            decimal discount = 0;

            if (!string.IsNullOrWhiteSpace(cart.CouponCode))
            {
                var cartInfoForCoupon = new CartInfoForCoupon
                {
                    Items = cart.Items.Select(x => new CartItemForCoupon {
                        ProductId = x.ProductId, Quantity = x.Quantity
                    }).ToList()
                };
                var couponValidationResult = await _couponService.Validate(cart.CouponCode, cartInfoForCoupon);

                if (couponValidationResult.Succeeded)
                {
                    discount = couponValidationResult.DiscountAmount;
                    _couponService.AddCouponUsage(user.Id, couponValidationResult.CouponId);
                }
                else
                {
                    throw new ApplicationException($"Unable to apply coupon {cart.CouponCode}. {couponValidationResult.ErrorMessage}");
                }
            }

            return(discount);
        }
Example #2
0
        // TODO separate getting product thumbnail, varation options from here
        public async Task <CartVm> GetActiveCartDetails(long customerId, long createdById)
        {
            var cart = await GetActiveCart(customerId, createdById);

            if (cart == null)
            {
                return(null);
            }

            var cartVm = new CartVm()
            {
                Id         = cart.Id,
                CouponCode = cart.CouponCode,
                IsProductPriceIncludeTax = cart.IsProductPriceIncludeTax,
                TaxAmount      = cart.TaxAmount,
                ShippingAmount = cart.ShippingAmount,
                OrderNote      = cart.OrderNote
            };

            cartVm.Items = _cartItemRepository
                           .Query()
                           .Include(x => x.Product).ThenInclude(p => p.ThumbnailImage)
                           .Include(x => x.Product).ThenInclude(p => p.OptionCombinations).ThenInclude(o => o.Option)
                           .Where(x => x.CartId == cart.Id)
                           .Select(x => new CartItemVm
            {
                Id               = x.Id,
                ProductId        = x.ProductId,
                ProductName      = x.Product.Name,
                ProductPrice     = x.Product.Price,
                ProductImage     = _mediaService.GetThumbnailUrl(x.Product.ThumbnailImage),
                Quantity         = x.Quantity,
                VariationOptions = CartItemVm.GetVariationOption(x.Product)
            }).ToList();

            cartVm.SubTotal = cartVm.Items.Sum(x => x.Quantity * x.ProductPrice);
            if (!string.IsNullOrWhiteSpace(cartVm.CouponCode))
            {
                var cartInfoForCoupon = new CartInfoForCoupon
                {
                    Items = cartVm.Items.Select(x => new CartItemForCoupon {
                        ProductId = x.ProductId, Quantity = x.Quantity
                    }).ToList()
                };
                var couponValidationResult = await _couponService.Validate(customerId, cartVm.CouponCode, cartInfoForCoupon);

                if (couponValidationResult.Succeeded)
                {
                    cartVm.Discount = couponValidationResult.DiscountAmount;
                }
                else
                {
                    cartVm.CouponValidationErrorMessage = couponValidationResult.ErrorMessage;
                }
            }

            return(cartVm);
        }
Example #3
0
        // TODO separate getting product thumbnail, varation options from here
        public async Task <CartVm> GetCart(long userId)
        {
            var cart = _cartRepository.Query().FirstOrDefault(x => x.UserId == userId && x.IsActive);

            if (cart == null)
            {
                return(new CartVm());
            }

            var cartVm = new CartVm()
            {
                Id             = cart.Id,
                CouponCode     = cart.CouponCode,
                TaxAmount      = cart.TaxAmount,
                ShippingAmount = cart.ShippingAmount
            };

            cartVm.Items = _cartItemRepository
                           .Query()
                           .Include(x => x.Product).ThenInclude(p => p.ThumbnailImage)
                           .Include(x => x.Product).ThenInclude(p => p.OptionCombinations).ThenInclude(o => o.Option)
                           .Where(x => x.CartId == cart.Id)
                           .Select(x => new CartItemVm
            {
                Id               = x.Id,
                ProductId        = x.ProductId,
                ProductName      = x.Product.Name,
                ProductPrice     = x.Product.Price,
                ProductImage     = _mediaService.GetThumbnailUrl(x.Product.ThumbnailImage),
                Quantity         = x.Quantity,
                VariationOptions = CartItemVm.GetVariationOption(x.Product)
            }).ToList();

            cartVm.SubTotal = cartVm.Items.Sum(x => x.Quantity * x.ProductPrice);
            if (!string.IsNullOrWhiteSpace(cartVm.CouponCode))
            {
                var cartInfoForCoupon = new CartInfoForCoupon
                {
                    Items = cartVm.Items.Select(x => new CartItemForCoupon {
                        ProductId = x.ProductId, Quantity = x.Quantity
                    }).ToList()
                };
                var couponValidationResult = await _couponService.Validate(cartVm.CouponCode, cartInfoForCoupon);

                if (couponValidationResult.Succeeded)
                {
                    cartVm.Discount = couponValidationResult.DiscountAmount;
                }
            }

            return(cartVm);
        }
Example #4
0
        private async Task <CouponValidationResult> CheckForDiscountIfAny(User user, Cart cart)
        {
            if (string.IsNullOrWhiteSpace(cart.CouponCode))
            {
                return(new CouponValidationResult {
                    Succeeded = true, DiscountAmount = 0
                });
            }

            var cartInfoForCoupon = new CartInfoForCoupon
            {
                Items = cart.Items.Select(x => new CartItemForCoupon {
                    ProductId = x.ProductId, Quantity = x.Quantity
                }).ToList()
            };

            var couponValidationResult = await _couponService.Validate(cart.CouponCode, cartInfoForCoupon);

            return(couponValidationResult);
        }
Example #5
0
        public async Task <CouponValidationResult> ApplyCoupon(long cartId, string couponCode)
        {
            var cart = _cartRepository.Query().Include(x => x.Items).FirstOrDefault(x => x.Id == cartId);

            var cartInfoForCoupon = new CartInfoForCoupon
            {
                Items = cart.Items.Select(x => new CartItemForCoupon {
                    ProductId = x.ProductId, Quantity = x.Quantity
                }).ToList()
            };
            var couponValidationResult = await _couponService.Validate(cart.CustomerId, couponCode, cartInfoForCoupon);

            if (couponValidationResult.Succeeded)
            {
                cart.CouponCode     = couponCode;
                cart.CouponRuleName = couponValidationResult.CouponRuleName;
                _cartItemRepository.SaveChanges();
            }

            return(couponValidationResult);
        }
        private static CartInfoForCoupon MakeMockCartInfoForCoupon()
        {
            var cartInfoForCoupon = new CartInfoForCoupon
            {
                Items = new List <CartItemForCoupon>
                {
                    new CartItemForCoupon
                    {
                        ProductId = 1,
                        Quantity  = 1
                    },
                    new CartItemForCoupon
                    {
                        ProductId = 2,
                        Quantity  = 2
                    }
                }
            };

            return(cartInfoForCoupon);
        }
Example #7
0
        public async Task CreateOrder(User user, Address billingAddress, Address shippingAddress)
        {
            var cart = _cartRepository
                       .Query()
                       .Include(c => c.Items).ThenInclude(x => x.Product)
                       .Where(x => x.UserId == user.Id && x.IsActive).FirstOrDefault();

            if (cart == null)
            {
                throw new ApplicationException($"Cart of user {user.Id} can no be found");
            }

            decimal discount = 0;

            if (!string.IsNullOrWhiteSpace(cart.CouponCode))
            {
                var cartInfoForCoupon = new CartInfoForCoupon
                {
                    Items = cart.Items.Select(x => new CartItemForCoupon {
                        ProductId = x.ProductId, Quantity = x.Quantity
                    }).ToList()
                };
                var couponValidationResult = await _couponService.Validate(cart.CouponCode, cartInfoForCoupon);

                if (couponValidationResult.Succeeded)
                {
                    discount = couponValidationResult.DiscountAmount;
                    _couponService.AddCouponUsage(user.Id, couponValidationResult.CouponId);
                }
                else
                {
                    throw new ApplicationException($"Unable to apply coupon {cart.CouponCode}. {couponValidationResult.ErrorMessage}");
                }
            }

            var orderBillingAddress = new OrderAddress()
            {
                AddressLine1      = billingAddress.AddressLine1,
                ContactName       = billingAddress.ContactName,
                CountryId         = billingAddress.CountryId,
                StateOrProvinceId = billingAddress.StateOrProvinceId,
                DistrictId        = billingAddress.DistrictId,
                Phone             = billingAddress.Phone
            };

            var orderShippingAddress = new OrderAddress()
            {
                AddressLine1      = shippingAddress.AddressLine1,
                ContactName       = shippingAddress.ContactName,
                CountryId         = shippingAddress.CountryId,
                StateOrProvinceId = shippingAddress.StateOrProvinceId,
                DistrictId        = shippingAddress.DistrictId,
                Phone             = shippingAddress.Phone
            };

            var order = new Order
            {
                CreatedOn       = DateTimeOffset.Now,
                CreatedById     = user.Id,
                BillingAddress  = orderBillingAddress,
                ShippingAddress = orderShippingAddress
            };

            foreach (var cartItem in cart.Items)
            {
                var orderItem = new OrderItem
                {
                    Product      = cartItem.Product,
                    ProductPrice = cartItem.Product.Price,
                    Quantity     = cartItem.Quantity
                };
                order.AddOrderItem(orderItem);
            }

            order.CouponCode           = cart.CouponCode;
            order.CouponRuleName       = cart.CouponRuleName;
            order.Discount             = discount;
            order.SubTotal             = order.OrderItems.Sum(x => x.ProductPrice * x.Quantity);
            order.SubTotalWithDiscount = order.SubTotal - discount;
            _orderRepository.Add(order);

            cart.IsActive = false;

            var vendorIds = cart.Items.Where(x => x.Product.VendorId.HasValue).Select(x => x.Product.VendorId.Value).Distinct();

            foreach (var vendorId in vendorIds)
            {
                var subOrder = new Order
                {
                    CreatedOn       = DateTimeOffset.Now,
                    CreatedById     = user.Id,
                    BillingAddress  = orderBillingAddress,
                    ShippingAddress = orderShippingAddress,
                    VendorId        = vendorId,
                    Parent          = order
                };

                foreach (var cartItem in cart.Items.Where(x => x.Product.VendorId == vendorId))
                {
                    var orderItem = new OrderItem
                    {
                        Product      = cartItem.Product,
                        ProductPrice = cartItem.Product.Price,
                        Quantity     = cartItem.Quantity
                    };

                    subOrder.AddOrderItem(orderItem);
                }

                subOrder.SubTotal = subOrder.OrderItems.Sum(x => x.ProductPrice * x.Quantity);
                _orderRepository.Add(subOrder);
            }

            _orderRepository.SaveChange();
        }
Example #8
0
        // TODO separate getting product thumbnail, varation options from here
        public async Task <CartVm> GetActiveCartDetails(long customerId, long createdById)
        {
            //var carrito = DataCarrito();
            //if (carrito == null)
            //{
            //    return null;
            //}
            //else {
            //    return carrito;
            //}

            // datos del carrito se cargan aqui
            var cart = await GetActiveCart(customerId, createdById);

            if (cart == null)
            {
                return(null);
            }

            var cartVm = new CartVm(_currencyService)
            {
                Id         = cart.Id,
                CouponCode = cart.CouponCode,
                IsProductPriceIncludeTax = cart.IsProductPriceIncludeTax,
                TaxAmount      = cart.TaxAmount,
                ShippingAmount = cart.ShippingAmount,
                OrderNote      = cart.OrderNote
            };

            //tenemos que cargar aqui los datos del carrito, tenemos la ID de los productos en el Cart.Items
            // recorremos el array.
            cartVm.Items = _cartItemRepository
                           .Query()
                           .Where(x => x.CartId == cart.Id).ToList()
                           .Select(x => new CartItemVm(_currencyService)
            {
                Id        = x.Id,
                ProductId = x.ProductId,
                // ProductName = x.Product.Name,
                //ProductPrice = x.Product.Price,
                //ProductStockQuantity = x.Product.StockQuantity,
                //ProductStockTrackingIsEnabled = x.Product.StockTrackingIsEnabled,
                //IsProductAvailabeToOrder = x.Product.IsAllowToOrder && x.Product.IsPublished && !x.Product.IsDeleted,
                Quantity = x.Quantity
            }).ToList();



            //codigo origen
            //cartVm.Items = _cartItemRepository
            //    .Query()
            //    .Include(x => x.Product).ThenInclude(p => p.ThumbnailImage)
            //    .Include(x => x.Product).ThenInclude(p => p.OptionCombinations).ThenInclude(o => o.Option)
            //    .Where(x => x.CartId == cart.Id).ToList()
            //    .Select(x => new CartItemVm(_currencyService)
            //    {
            //        Id = x.Id,
            //        ProductId = x.ProductId,
            //        ProductName = x.Product.Name,
            //        ProductPrice = x.Product.Price,
            //        ProductStockQuantity = x.Product.StockQuantity,
            //        ProductStockTrackingIsEnabled = x.Product.StockTrackingIsEnabled,
            //        IsProductAvailabeToOrder = x.Product.IsAllowToOrder && x.Product.IsPublished && !x.Product.IsDeleted,
            //        ProductImage = _mediaService.GetThumbnailUrl(x.Product.ThumbnailImage),
            //        Quantity = x.Quantity,
            //        VariationOptions = CartItemVm.GetVariationOption(x.Product)
            //    }).ToList();

            cartVm.SubTotal = cartVm.Items.Sum(x => x.Quantity * x.ProductPrice);
            if (!string.IsNullOrWhiteSpace(cartVm.CouponCode))
            {
                var cartInfoForCoupon = new CartInfoForCoupon
                {
                    Items = cartVm.Items.Select(x => new CartItemForCoupon {
                        ProductId = x.ProductId, Quantity = x.Quantity
                    }).ToList()
                };
                var couponValidationResult = await _couponService.Validate(customerId, cartVm.CouponCode, cartInfoForCoupon);

                if (couponValidationResult.Succeeded)
                {
                    cartVm.Discount = couponValidationResult.DiscountAmount;
                }
                else
                {
                    cartVm.CouponValidationErrorMessage = couponValidationResult.ErrorMessage;
                }
            }

            return(cartVm);
        }