Example #1
0
        public List <OrderCartItem> GetOrderCartItems()
        {
            using (FreshChoiceEntities db = new FreshChoiceEntities())
            {
                List <CartItem> cartItems = GetCartItems();

                if (cartItems != null)
                {
                    List <OrderCartItem> orderCartItems = new List <OrderCartItem>();
                    foreach (var item in cartItems)
                    {
                        OrderCartItem orderCartItem     = new OrderCartItem();
                        Item          selectedItem      = db.Items.Where(w => w.ItemId == item.ItemId).FirstOrDefault();
                        Image         selectedItemImage = db.Images.Where(w => w.ItemId == item.ItemId).FirstOrDefault();
                        orderCartItem.ItemId          = item.ItemId;
                        orderCartItem.Quantity        = item.CartItemQnt;
                        orderCartItem.ItemName        = selectedItem.ItemName;
                        orderCartItem.ItemPrice       = selectedItem.ItemPrice;
                        orderCartItem.ItemDescription = selectedItem.ItemDescription;
                        if (selectedItemImage != null)
                        {
                            orderCartItem.ItemImageUrl = selectedItemImage.ImageUrl;
                        }
                        orderCartItems.Add(orderCartItem);
                    }
                    return(orderCartItems);
                }
            }
            return(null);
        }
 public static List <OrderItemVariationGroup>?GetOrderCartItemVariations(this OrderCartItem cartItem)
 {
     if (cartItem.VariationJson == null)
     {
         return(null);
     }
     return(JsonConvert.DeserializeObject <List <OrderItemVariationGroup> >(cartItem.VariationJson));
 }
        /// <summary>
        /// Delete an item from a user's order
        /// </summary>
        /// <param name="id">Id of orderItem to be deleted</param>
        /// <returns>Task of completion for orderItem delete</returns>
        public async Task Delete(int orderId, int productId)
        {
            OrderCartItem item = await _context.OrderCartItem.FindAsync(orderId, productId);

            if (item != null)
            {
                _context.Entry(item).State = EntityState.Deleted;
                await _context.SaveChangesAsync();
            }
        }
Example #4
0
        public async Task <Result <OrderCartItem> > AddToCart(int productId, List <OrderItemVariationGroup> orderItemVariations, int quantity = 1)
        {
            if (_me.IsAnonymous)
            {
                return(new Failure <OrderCartItem>("需要先登录"));
            }

            var variationJson = JsonConvert.SerializeObject(orderItemVariations);
            var cartItem      = _db.OrderCartItems
                                .Where(x => x.BuyerId == _me.Id)
                                .Where(x => x.ProductId == productId)
                                .Where(x => x.VariationJson == variationJson)
                                .FirstOrDefault();

            if (cartItem == null)
            {
                cartItem = new OrderCartItem {
                    ProductId     = productId,
                    BuyerId       = _me.Id,
                    BuyerName     = _me.DisplayName,
                    VariationJson = variationJson
                };
                _db.OrderCartItems.Add(cartItem);
            }

            var product = _db.Products
                          .AsNoTracking()
                          .Include(x => x.VariationGroups).ThenInclude(x => x.Variations)
                          .Where(x => x.Id == productId)
                          .SingleOrDefault();

            if (product == null)
            {
                return(new Failure <OrderCartItem>("商品不存在"));
            }
            if (product.Status != ProductStatus.Active)
            {
                return(new Failure <OrderCartItem>($"商品{product.Status.ToLabel()},无法购买"));
            }
            if (product.OffShelveTime < DateTime.Now)
            {
                return(new Failure <OrderCartItem>("商品已过期下架"));
            }
            if (product.Stock == 0)
            {
                return(new Failure <OrderCartItem>("商品缺货"));
            }

            cartItem.Quantity += quantity;
            cartItem.Quantity  = Math.Min(cartItem.Quantity, product.Stock);

            await _db.Normalize().SaveChangesAsync();

            return(new Success <OrderCartItem>(cartItem));
        }
        /// <summary>
        /// Send a checkout request with the provided information to AuthorizeNet
        /// </summary>
        /// <returns>Redirect to Checkout Success page if transation successful, show ModelState errors on form if not</returns>
        public async Task <IActionResult> OnPost()
        {
            #region AddressBuilding
            var currentUser = await _userManager.GetUserAsync(User);

            customerAddressType billingAddress = new customerAddressType
            {
                address   = Input.BillingAddress,
                firstName = Input.FirstName,
                lastName  = Input.LastName,
                email     = currentUser.Email,
                state     = Input.BillingState,
                zip       = Input.BillingZip,
                city      = Input.BillingCity,
            };
            customerAddressType shippingAddress = new customerAddressType();
            if (Input.SameBillingAndShipping)
            {
                shippingAddress = billingAddress;
            }
            else
            {
                shippingAddress.address   = Input.ShippingAddress;
                shippingAddress.firstName = Input.FirstName;
                shippingAddress.lastName  = Input.LastName;
                shippingAddress.email     = currentUser.Email;
                shippingAddress.state     = Input.ShippingState;
                shippingAddress.zip       = Input.ShippingZip;
                shippingAddress.city      = Input.ShippingCity;
            }
            #endregion
            if (ModelState.IsValid)
            {
                var cart = await _cart.GetUserCart(currentUser.Id);

                if (cart != null && cart.CartItems != null)
                {
                    creditCardType card = new creditCardType()
                    {
                        cardNumber     = Input.CardNumber,
                        expirationDate = "1220",
                        cardCode       = "555"
                    };

                    TransactionResponse result = _payment.Run(card, billingAddress, cart.CartItems);
                    if (result.Successful)
                    {
                        decimal         total     = 0;
                        List <CartItem> cartItems = new List <CartItem>();
                        foreach (var item in cart.CartItems)
                        {
                            cartItems.Add(item);
                            total += item.Qty * item.Product.Price;
                        }

                        OrderCart order = new OrderCart()
                        {
                            CartId          = cart.Id,
                            UserId          = currentUser.Id,
                            FirstName       = billingAddress.firstName,
                            LastName        = billingAddress.lastName,
                            Date            = DateTime.Now.ToString(),
                            BillingAddress  = billingAddress.address,
                            BillingCity     = billingAddress.city,
                            BillingState    = billingAddress.state,
                            BillingZip      = billingAddress.zip,
                            ShippingAddress = shippingAddress.address,
                            ShippingCity    = shippingAddress.city,
                            ShippingState   = shippingAddress.state,
                            ShippingZip     = shippingAddress.zip
                        };
                        await _order.Create(order);

                        order.CartItems = new List <OrderCartItem>();
                        foreach (var item in cartItems)
                        {
                            OrderCartItem orderItem = new OrderCartItem()
                            {
                                OrderCartId = cart.Id,
                                ProductId   = item.ProductId,
                                Qty         = item.Qty
                            };
                            order.CartItems.Add(orderItem);
                            await _orderItem.Create(orderItem);

                            await _cartItem.Delete(item.CartId, item.ProductId);
                        }

                        await _cart.Delete(currentUser.Id);

                        await BuildCheckoutEmail(currentUser.Email, $"{order.FirstName} {order.LastName}", cartItems, $"{shippingAddress.address} {shippingAddress.city}, {shippingAddress.state} {shippingAddress.zip}", total);

                        if (Input.SaveBillingAddress)
                        {
                            currentUser.Address = Input.BillingAddress;
                            currentUser.City    = Input.BillingCity;
                            currentUser.State   = Input.BillingState;
                            currentUser.Zip     = Input.BillingZip;
                            if (Input.BillingOptionalAddition != null || Input.BillingOptionalAddition != "")
                            {
                                currentUser.OptionalAddress = Input.BillingOptionalAddition;
                            }
                            await _userManager.UpdateAsync(currentUser);
                        }

                        return(RedirectToPage("/Checkout/Success", new { response = result.Response, cartId = cart.Id }));
                    }
                    else
                    {
                        ModelState.AddModelError("", result.Response);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Something went wrong! We couldn't find anything in your cart.");
                }
            }
            else
            {
                ModelState.AddModelError("", "Invalid input. Please try again.");
            }
            return(Page());
        }
Example #6
0
        public List <AllOrderItem> GetAllOrderItems()
        {
            List <AllOrderItem> orders = new List <AllOrderItem>();

            using (FreshChoiceEntities db = new FreshChoiceEntities())
            {
                var newOrders = db.Orders
                                .Join(
                    db.OrderStatus,
                    o => o.OrderStatusId,
                    os => os.OrderStatusId,
                    (o, os) => new { order = o, orderStatus = os }
                    )
                                .Join(
                    db.Carts,
                    ows => ows.order.CartId,
                    c => c.CartId,
                    (ows, c) => new { order = ows.order, orderStatus = ows.orderStatus, cart = c }
                    )
                                .Where(w => w.order.UserId == currentUserId)
                                .OrderByDescending(o => o.order.OrderUpdatedDate)
                                .ToList();

                foreach (var order in newOrders)
                {
                    AllOrderItem orderItem = new AllOrderItem();
                    orderItem.OrderId        = order.order.OrderId;
                    orderItem.BillNo         = order.order.OrderBillNo;
                    orderItem.ConfirmationNo = order.order.OrderConfirmationNo;
                    orderItem.LastUpdate     = order.order.OrderUpdatedDate;
                    orderItem.NextDeadline   = order.order.OrderNextDeadline;
                    orderItem.OrderStatus    = order.orderStatus.OrderStatusDescription;
                    orderItem.OrderStatusId  = order.orderStatus.OrderStatusId;
                    orderItem.OrderAmount    = order.order.OrderAmount;

                    var items = db.CartItems
                                .Where(w => w.CartId == order.order.Cart.CartId)
                                .Join(db.Items,
                                      ci => ci.ItemId,
                                      i => i.ItemId,
                                      (ci, i) => new { cartItem = ci, item = i })
                                .ToList();

                    orderItem.Items = new List <OrderCartItem>();
                    foreach (var item in items)
                    {
                        OrderCartItem cartItem = new OrderCartItem();
                        cartItem.ItemId    = item.item.ItemId;
                        cartItem.Quantity  = item.cartItem.CartItemQnt;
                        cartItem.ItemName  = item.item.ItemName;
                        cartItem.ItemPrice = item.item.ItemPrice;

                        var image = db.Images.Where(w => w.ItemId == item.item.ItemId).FirstOrDefault();
                        if (image != null)
                        {
                            cartItem.ItemImageUrl = image.ImageUrl;
                        }
                        orderItem.Items.Add(cartItem);
                    }

                    orders.Add(orderItem);
                }
            }
            return(orders);
        }
 /// <summary>
 /// Update a given user's order item quantity
 /// </summary>
 /// <param name="orderItem">OrderItem information for update</param>
 /// <returns>Successful result of specified updated orderItem</returns>
 public async Task Update(OrderCartItem orderItem)
 {
     _context.Entry(orderItem).State = EntityState.Modified;
     await _context.SaveChangesAsync();
 }