// GET: customer_order
        public ActionResult Index(string sortOrder, string searchString)
        {
            int coubt    = db.PriceLists.Count();
            var students = from s in db.customer_Orders
                           select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                students = students.Where(s => s.CustomerName.Contains(searchString) ||
                                          s.customer_Id.Contains(searchString) || s.services.service_name.Contains(searchString) || s.order_date.Contains(searchString));
                return(View(students.ToList()));
            }

            var customer_Orders = db.customer_Orders.Include(c => c.Cloths).Include(c => c.services);

            //Diferiantiate acording to roles
            if (User.IsInRole("SAN"))
            {
                return(View(customer_Orders.ToList().Where(x => x.order_status == null)));
            }
            //He can only see his order
            customer_order co = new customer_order();

            //return View(customer_Orders.ToList().Where(x=>x.order_status==null && co.customer_Id==User.Identity.GetUserId()));
            return(View(customer_Orders.ToList().Where(x => x.customer_Id == User.Identity.GetUserId())));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            customer_order customer_order = db.customer_Orders.Find(id);

            db.customer_Orders.Remove(customer_order);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
    public IHttpActionResult SaveCustomerOrder([FromBody] IEnumerable <CartModel> theCart)
    {
        designEntity = new online_tshirt_designingEntities();

        //Takes each object
        foreach (CartModel item in theCart)
        {
            customer_order newOrder = new customer_order
            {
                ProductPaymentMthd = "cod",

                ProductQuantity = item.ProductQuantity,

                ProductQuantityPrice = item.ProductQuantityPrice,

                ProductSize = item.ProductSize,

                TotalPrice = item.TotalProductPrice,

                CustId = item.CustId,

                ProductId = item.ProductId
            };

            saveInEntity(newOrder);
        }

        //Returns the customer order
        var customerOrder = from order in designEntity.customer_order
                            join cust in designEntity.customers on order.CustId equals cust.CustId
                            join prod in designEntity.products on order.ProductId equals prod.ProductId
                            orderby order.ProductOrderTime
                            select new
        {
            order.ProductOrderTime,
            order.ProductPaymentMthd,
            order.ProductQuantity,
            order.ProductQuantityPrice,
            order.TotalPrice,

            cust.CustFirstName,
            cust.CustLastName,
            cust.CustMobNo,
            cust.CustEmailAddr,

            prod.ProductCode,
            prod.ProductCat,
            prod.ProductName,
            prod.ProductStyle,
            prod.ProductColor,
            prod.ProductImg,
            prod.ProductPrice
        };

        return(Ok(customerOrder));
    }
 public ActionResult Edit([Bind(Include = "ID,invoice_no,order_date,order_month,customer_Id,total_qty,discount,service_tax,total_paid,total_balance,delivery_date,remarks,order_status,CustomerName,ClothsID,ServiceID")] customer_order customer_order)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customer_order).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.ClothsID  = new SelectList(db.cloths, "ID", "cloth_Type", customer_order.ClothsID);
     ViewBag.ServiceID = new SelectList(db.Services, "ID", "service_name", customer_order.ServiceID);
     return(View(customer_order));
 }
        // GET: customer_order/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            customer_order customer_order = db.customer_Orders.Find(id);

            if (customer_order == null)
            {
                return(HttpNotFound());
            }
            return(View(customer_order));
        }
    private void saveInEntity(customer_order newOrder)
    {
        try
        {
            //Finally commit the changes the changes and insert the record
            //In the database
            designEntity.customer_order.Add(newOrder);

            designEntity.SaveChanges();
        }
        catch (Exception error)
        {
            System.Diagnostics.Debug.WriteLine("Error in Linq", error);
        }
    }
        // GET: customer_order/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            customer_order customer_order = db.customer_Orders.Find(id);

            if (customer_order == null)
            {
                return(HttpNotFound());
            }
            ViewBag.ClothsID  = new SelectList(db.cloths, "ID", "cloth_Type", customer_order.ClothsID);
            ViewBag.ServiceID = new SelectList(db.Services, "ID", "service_name", customer_order.ServiceID);
            return(View(customer_order));
        }
        public ActionResult Create(customer_order customer_order)
        {
            ApplicationDbContext db = new ApplicationDbContext();
            var UserID   = User.Identity.GetUserId();
            var username = User.Identity.GetUserName();


            DateTime checkDate = DateTime.Now.AddHours(3);


            if (ModelState.IsValid)
            {
                if (customer_order.total_qty < 0)
                {
                    ModelState.AddModelError("", "they can not put a value as yet");
                }
                else
                {
                    customer_order.invoice_no = customer_order.InvoiceNumber();

                    customer_order.customer_Id = UserID;

                    customer_order.order_status  = null;
                    customer_order.total_balance = customer_order.itermprice();
                    customer_order.total_paid    = customer_order.TotalPrice();
                    customer_order.order_date    = System.DateTime.Now.ToShortDateString();
                    customer_order.order_month   = System.DateTime.Now.Month.ToString();



                    if (CheckID(username) == true)
                    {
                        var Odl = db.OrderLists.Where(x => x.customerID == username).FirstOrDefault();
                        Odl.qty       = Odl.qty + customer_order.total_qty;
                        Odl.amt       = Odl.amt + customer_order.total_paid;
                        Odl.status    = "In Progress";
                        Odl.OrderDate = DateTime.Now.Date;

                        db.Entry(Odl).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    else
                    {
                        OrderList od = new OrderList();
                        od.InvoiceNo  = od.GenInvoice();
                        od.OrderDate  = DateTime.Now.Date;
                        od.customerID = User.Identity.GetUserName();
                        od.qty        = customer_order.total_qty;
                        od.amt        = customer_order.total_paid;
                        od.status     = "In progress";
                        db.OrderLists.Add(od);
                        db.SaveChanges();
                    }
                    db.customer_Orders.Add(customer_order);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }

            ViewBag.ClothsID  = new SelectList(db.cloths, "ID", "cloth_Type", customer_order.ClothsID);
            ViewBag.ServiceID = new SelectList(db.Services, "ID", "service_name", customer_order.ServiceID);
            return(View(customer_order));
        }