Beispiel #1
0
        public int CreateOrder(Order order)
        {
            decimal orderTotal = 0;
            var cartItems = GetCartItems();

            //Iterate the items in the cart

            foreach (var cartItem in cartItems)
            {
                OrderItem orderItem = new OrderItem()
                {
                    ProductId = cartItem.ProductId,
                    OrderId = order.Id,
                    Quantities=cartItem.Count
                };
                Product product = dbContext.Products.FirstOrDefault(p => p.Id == cartItem.ProductId);
                product.Stock -= cartItem.Count;

                dbContext.OrderItems.Add(orderItem);

                orderTotal += (cartItem.Count * cartItem.Product.ProductUnitPrice);
            }

            dbContext.SaveChanges();

            //Empty the shopping cart
            EmptyCart();

            return order.Id;
        }
        public ActionResult PlaceOrder()
        {
            CheckoutModel checkoutModel = GetCheckoutModel();
            ShoppingCart  cart = ShoppingCart.GetCart(this.HttpContext);
            Order order = new Order();
            if (checkoutModel.Step1 == null)
            {
                return RedirectToAction("CheckoutStep1");
            }
            if (checkoutModel.Step2 == null)
            {
                return RedirectToAction("CheckoutStep2");
            }
            if (checkoutModel.Step3 == null)
            {
                return RedirectToAction("CheckoutStep3");
            }
            order.UserName = User.Identity.Name;
            order.OrderDate = DateTime.Now.Date;
            order.OrderNumber = CreateOrderNumber();
            order.OrderStatus = "Received";
            order.CustomerBillingAddress = dbContext.CustomerAddresses.FirstOrDefault(a => a.Id == checkoutModel.Step1.BillingAddressId);
            order.CustomerShippingAddress = dbContext.CustomerAddresses.FirstOrDefault(a => a.Id == checkoutModel.Step2.ShippingAddressId);
            dbContext.Orders.Add(order);
            foreach (var cartItem in cart.GetCartItems())
            {
                if (!CheckStock(cartItem.ProductId, cartItem.Count))
                {
                    TempData["message"] = "Requested quantity not available for Item: " + cartItem.Product.ProductName;
                    return RedirectToAction("Index");
                }
            }
            try
            {
                dbContext.SaveChanges();
            }
            catch
            {
                 return View("error", new string[] { "Database Error!" });
            }
            Order orderCreated = dbContext.Orders.FirstOrDefault(o => o.OrderNumber == order.OrderNumber);

            PaymentInfo paymentInfo = new PaymentInfo() {
                Order=orderCreated,

                Name=checkoutModel.Step3.Name,
                CreditCardNumber=checkoutModel.Step3.CreditCardNumber,
                ExpiredDate=checkoutModel.Step3.ExpiredDate,
                SecurityCode=checkoutModel.Step3.SecurityCode
            };
            dbContext.PaymentInfoes.Add(paymentInfo);
            var cartItems = cart.GetCartItems();

            foreach (var cartItem in cartItems)
            {

                OrderItem orderItem = new OrderItem()
                {
                    ProductId = cartItem.ProductId,
                    OrderId = orderCreated.Id,
                    Quantities = cartItem.Count
                };

                Product product = dbContext.Products.FirstOrDefault(p => p.Id == cartItem.ProductId);
                product.Stock -= cartItem.Count;
                dbContext.Entry(product).State = EntityState.Modified;
                dbContext.OrderItems.Add(orderItem);

            }
            try
            {
                dbContext.SaveChanges();
            }

               catch
            {

                dbContext.DeleteOrder(orderCreated.Id);
                return View("error", new string[] { "Database Error!" });
            }
            cart.EmptyCart();

            Session.Remove("Checkout");
            try
            {
                SendConfirmationEmail(orderCreated);
            }
            catch
            {
                return View("error", new string[] { "Sorry, we have problem to send confirmation email to you, please log into My Account to Track your order." });
            }
            return RedirectToAction("OrderConfirmation", new { id = orderCreated.Id });
        }