public ActionResult Orders()
        {
            bool flag = false;

            context = new ApplicationDbContext();
            do
            {
                try
                {
                    var prdlist = (List <Dto_Cart>)Session["Products"];
                    if (prdlist != null)
                    {
                        Claim displayName = ClaimsPrincipal.Current.FindFirst(ClaimsPrincipal.Current.Identities.First().NameClaimType);
                        TempData["Name"] = displayName != null ? displayName.Value : String.Empty;
                        var usr = context.Users.Where(us => us.UserName == displayName.Value).FirstOrDefault();

                        Orders ord = new Orders();
                        ord.CustomerId = usr.Id;
                        //ord.Customer = usr;
                        ord.DeliverDate = DateTime.Now.Date.AddDays(3);//DateTime.Parse("" + (DateTime.Now.Date.Day + 3) + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year);
                        ord.OrderDate   = DateTime.Now.Date;
                        bool f = false;
                        foreach (var item in prdlist)
                        {
                            var prd = context.Products.FirstOrDefault(prds => prds.ID == item.user.ID);
                            if (item.Quantity > prd.Quantity)
                            {
                                f = true;
                                break;
                            }
                        }
                        if (!f)
                        {
                            decimal price = 0;
                            foreach (var item in prdlist)
                            {
                                //context.Entry(item.user).State = EntityState.Detached;
                                var prds = context.Products.FirstOrDefault(ds => ds.ID == item.user.ID);
                                if (item.Quantity <= prds.Quantity)
                                {
                                    OrderUserProduct ordprd = new OrderUserProduct();
                                    //ordprd.Product = item.user;
                                    ordprd.Product_ID = item.user.ID;
                                    ordprd.Quantity   = item.Quantity;
                                    ord.OrderUserProduct.Add(ordprd);
                                    price += item.Quantity * item.user.Price;
                                    var prd = context.Products.Where(pd => pd.ID == item.user.ID).FirstOrDefault();
                                    prd.Quantity     -= item.Quantity;
                                    TempData["Error"] = "";
                                }
                                else
                                {
                                    TempData["Error"] = "Quantity Not Available";
                                    return(RedirectToAction("Checkout", "Home"));
                                }
                            }
                            ord.TotalOrderCash = price;

                            context.Orders.Add(ord);
                            context.SaveChanges();
                            flag = true;

                            Session["Products"] = null;
                            TempData["Error"]   = "Order has been made successfully";
                            return(RedirectToAction("Checkout", "Home"));
                        }
                        else
                        {
                            TempData["Error"] = "Quantity Not Available";
                            return(RedirectToAction("Checkout", "Home"));
                        }
                    }
                    else
                    {
                        TempData["Error"] = "there is no products added in the cart";
                        return(RedirectToAction("Checkout", "Home"));
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    context = new ApplicationDbContext();
                    Session["TempError"] = "Qunatity you need is not available";
                    flag = false;
                }
            }while (flag == false);
            return(RedirectToAction("Index", "Home"));
        }
示例#2
0
        public IHttpActionResult Orders([FromBody] OrderModel prdlist)
        {
            bool flag = false;

            context = new ApplicationDbContext();
            do
            {
                try
                {
                    if (prdlist != null)
                    {
                        Orders ord = new Orders();
                        string o   = "";
                        ord.CustomerId = prdlist.CustomerId;
                        //ord.Customer = usr;
                        ord.DeliverDate = DateTime.Now.Date.AddDays(3);
                        ord.OrderDate   = DateTime.Now.Date;
                        bool f  = false;
                        var  us = context.Users.FirstOrDefault(u => u.Id == ord.CustomerId);

                        foreach (var item in prdlist.orderUserProduct)
                        {
                            var prd = context.products.FirstOrDefault(prds => prds.ID == item.Product_ID);
                            if (item.Quantity > prd.Quantity)
                            {
                                f = true;
                                break;
                            }
                        }
                        if (!f)
                        {
                            decimal price = 0;
                            foreach (var item in prdlist.orderUserProduct)
                            {
                                //context.Entry(item.user).State = EntityState.Detached;
                                var prds = context.products.FirstOrDefault(ds => ds.ID == item.Product_ID);
                                if (item.Quantity <= prds.Quantity)
                                {
                                    OrderUserProduct ordprd = new OrderUserProduct();
                                    //ordprd.Product = item.user;
                                    ordprd.Product_ID = item.Product_ID;
                                    ordprd.Quantity   = item.Quantity;
                                    ord.orderUserProduct.Add(ordprd);
                                    price += item.Quantity * prds.Price;
                                    var prd = context.products.Where(pd => pd.ID == item.Product_ID).FirstOrDefault();
                                    prd.Quantity -= item.Quantity;

                                    o += $"Order Product :" +
                                         $"  Product ID ={prds.ID} \n" +
                                         $"  Product Name = {prds.Name} \n" +
                                         $"  Product Price = {prds.Price} \n" +
                                         $"  Product Qunatity = {item.Quantity} \n";
                                }
                                else
                                {
                                    return(BadRequest("Qunatity Error"));
                                }
                            }
                            ord.TotalOrderCash = price;

                            context.orders.Add(ord);

                            context.SaveChanges();
                            string o1 = $"Order ID : {ord.ID} \n " +
                                        $"order Date : {ord.OrderDate} \n" +
                                        $"deliver Date : {ord.DeliverDate} \n " +
                                        $"TotalOrderCash : {ord.TotalOrderCash} \n " +
                                        $"Customer Name : { us.UserName} \n " +
                                        $"Customer Addres {"" + us.City + "," + us.Country}\n" +
                                        $"" + o;

                            var identityClaims         = (ClaimsIdentity)User.Identity;
                            IEnumerable <Claim> claims = identityClaims.Claims;
                            AccountModel        model  = new AccountModel()
                            {
                                UserName  = identityClaims.FindFirst("Username").Value,
                                Email     = identityClaims.FindFirst("Email").Value,
                                FirstName = identityClaims.FindFirst("FirstName").Value,
                                LastName  = identityClaims.FindFirst("LastName").Value,
                                LoggedOn  = identityClaims.FindFirst("LoggedOn").Value,
                                ID        = identityClaims.FindFirst("Id").Value
                            };
                            SendEmailNotification(model.Email, model.UserName, o1);
                            flag = true;
                            return(Ok("Successed"));
                        }
                        else
                        {
                            return(BadRequest("Error"));
                        }
                    }
                    else
                    {
                        return(BadRequest("Error"));
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    context = new ApplicationDbContext();

                    flag = false;
                }
            }while (flag == false);
            return(Ok("Successed"));
        }