Exemple #1
0
        public ActionResult Thankyou()
        {
            ApplicationDbContext _applicationDbContext = new ApplicationDbContext();
            List <CartVM>        cartList = Session["cart"] as List <CartVM>;

            if (cartList == null)
            {
                TempData["cartEmptyMessage"] = "Your Cart is Empty Now";
                return(RedirectToAction("Index", "ClientSide"));
            }
            Order      orderObj = new Order();
            int        orderID  = 0;
            ShippingVM shipping = Session["shippingInfo"] as ShippingVM;


            var userId = User.Identity.GetUserId();



            var userAccount = new ApplicationDbContext().Users.Where(x => x.Id == userId).SingleOrDefault();
            OrderShippingHanlder orderShippingHanlder = new OrderShippingHanlder();
            int orderShipId = orderShippingHanlder.AddShiping(shipping);

            using (ApplicationDbContext _context = new ApplicationDbContext())
            {
                orderObj.orderStatusID   = 1;
                orderObj.OrderShippingID = orderShipId;
                orderObj.createAt        = DateTime.Now;
                orderObj.paymentType     = shipping.paymentType;
                orderObj.userID          = userId;

                orderObj.totalBill = Convert.ToDouble(cartList.Sum(x => x.Total));

                _context.Orders.Add(orderObj);
                _context.SaveChanges();
                orderID = orderObj.Id;

                OrderDetail objDetail = new OrderDetail();

                foreach (var item in cartList)
                {
                    objDetail.OrderId     = orderID;
                    objDetail.ProductID   = item.ProductID;
                    objDetail.ProductName = item.ProductName;
                    objDetail.Price       = item.Price;
                    objDetail.Quantity    = item.Quantity;
                    objDetail.Total       = item.Total;

                    _context.OrderDetails.Add(objDetail);
                    _context.SaveChanges();
                }
            }



            ViewBag.ShoppingCartList = cartList.ToList();

            ViewBag.grandTotal = orderObj.totalBill;


            string strBody = "Dear User " + userAccount.user_Name + ",<br> Thank you so much for shopping  its Worth is ."
                             + "<br/><br/>" + orderObj.totalBill + "<br/><br/><br/><b> Best Regards & Thanks</b>: MensBytes <br/>";

            MB.sendEmailFunction(userAccount.Email, "Thankyou for shopping", strBody);

            Session["shippingInfo"] = null;
            Session["cart"]         = null;
            return(View());
        }