Example #1
0
        public IHttpActionResult GetOrder(int id)
        {
            var order     = new OrdersModel();
            var ordsD     = new List <Order_detail>();
            var orderById = db.Orders.Find(id);
            var odById    = db.Order_details.Where(od => od.OrderID == id);

            if (orderById == null)
            {
                return(NotFound());
            }
            else
            {
                order.ID   = orderById.ID;
                order.Date = orderById.Date;
                foreach (var od in odById)
                {
                    var ordD = new Order_detail
                    {
                        ID            = od.ID,
                        Quantity      = od.Quantity,
                        StatusOrder   = od.StatusOrder,
                        TotalPrice    = od.TotalPrice,
                        SubTotalPrice = od.SubTotalPrice,
                        ProductID     = od.ProductID,
                        OrderID       = od.OrderID
                    };
                    ordsD.Add(ordD);
                }
                order.Order_details = ordsD.ToList();
                db.SaveChanges();
            }

            return(Ok(order));
        }
Example #2
0
        public ActionResult thanh_toan(FormCollection fc)
        {
            Order order = new Order();

            order.id       = DateTime.Now.ToString("ddmmyyyyhhmmss");
            order.username = fc["username"];
            order.phone    = fc["phone"];
            order.email    = fc["email"];
            order.address  = fc["address"];
            order.date     = DateTime.Now;
            order.total    = fc["total"];
            order.sum      = fc["sum"];
            DAOOrder dao = new DAOOrder();

            dao.Add_order(order);

            List <CartItem> giohang = Session["giohang"] as List <CartItem>;

            foreach (var item in giohang)
            {
                Order_detail orderDetail = new Order_detail();
                orderDetail.image      = item.Hinh;
                orderDetail.name       = item.TenSanPham;
                orderDetail.quantity   = item.SoLuong;
                orderDetail.price      = item.DonGia;
                orderDetail.id_order   = order.id;
                orderDetail.product_id = item.SanPhamID;
                dao.order_detail(orderDetail);
            }

            TempData["msg"] = "INSERT SUCCESS";
            Session.Remove("giohang");
            return(RedirectToAction("dat_hang", "Home", new{ order_id = order.id }));
        }
Example #3
0
        public void AddToCart(int product_id, int quantity)
        {
            Order basicCart = _db.Orders
                              .Where(o => o.type == "cart")
                              .Include(o => o.order_details)
                              .FirstOrDefault(O => O.user_id == _user_id);

            if (basicCart == null)
            {
                basicCart = new Order()
                {
                    user_id       = _user_id,
                    type          = "cart",
                    status        = "open",
                    order_details = new List <Order_detail>()
                };
                _db.Orders.Add(basicCart);
            }

            Product product = _db.Products.Find(product_id);

            Order_detail order_detail = new Order_detail()
            {
                quantity = quantity,
                product  = product,
                status   = "1"
            };

            basicCart.order_details.Add(order_detail);
            _db.SaveChanges();
            Response.Redirect("/order/cart");
        }
Example #4
0
        //GET: api/Orders
        public IHttpActionResult GetAll()
        {
            var orderL = new List <OrdersModel>();
            var or     = db.Orders.ToList();

            foreach (var o in or)
            {
                var orderD = new List <Order_detail>();
                var orM    = new OrdersModel();
                orM.ID   = o.ID;
                orM.Date = o.Date;
                orderL.Add(orM);
                var ord = db.Order_details.Where(od => od.OrderID == orM.ID).ToList();
                foreach (var od in ord)
                {
                    var orDM = new Order_detail
                    {
                        ID            = od.ID,
                        Quantity      = od.Quantity,
                        StatusOrder   = od.StatusOrder,
                        TotalPrice    = od.TotalPrice,
                        SubTotalPrice = od.SubTotalPrice,
                        ProductID     = od.ProductID,
                        OrderID       = od.OrderID
                    };
                    orderD.Add(orDM);
                }
                orM.Order_details = orderD.ToList();
            }

            db.SaveChanges();
            return(Ok(orderL));
        }
        public ActionResult DeleteConfirmed(long id)
        {
            Order_detail order_detail = db.Order_detail.Find(id);

            db.Order_detail.Remove(order_detail);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Example #6
0
        public void DeleteFromCart(string detail_id)
        {
            int          id           = Int32.Parse(detail_id);
            Order_detail itemToDelete = _db.Order_details.Find(id);

            _db.Order_details.Remove(itemToDelete);
            _db.SaveChanges();
            Response.Redirect("/order/cart");
        }
Example #7
0
        public void SaveForLater(string detail_id)
        {
            int          id         = Int32.Parse(detail_id);
            Order_detail itemToSave = _db.Order_details.Find(id);
            Order        basicCart  = _db.Orders
                                      .Where(o => o.type == "later")
                                      .FirstOrDefault(O => O.user_id == _user_id);

            itemToSave.Orderid = basicCart.id;
            _db.SaveChanges();
            Response.Redirect("/order/cart");
        }
 public ActionResult Edit([Bind(Include = "id,order_id,product_id,amount,total_price,created_at")] Order_detail order_detail)
 {
     if (ModelState.IsValid)
     {
         db.Entry(order_detail).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.order_id   = new SelectList(db.Orders, "id", "customer_name", order_detail.order_id);
     ViewBag.product_id = new SelectList(db.Products, "id", "name", order_detail.product_id);
     return(View(order_detail));
 }
 public bool Insert(Order_detail detail)
 {
     try
     {
         db.Order_detail.Add(detail);
         db.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
        // GET: Order_detail/Details/5
        public ActionResult Details(long?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Order_detail order_detail = db.Order_detail.Find(id);

            if (order_detail == null)
            {
                return(HttpNotFound());
            }
            return(View(order_detail));
        }
        // GET: Order_detail/Edit/5
        public ActionResult Edit(long?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Order_detail order_detail = db.Order_detail.Find(id);

            if (order_detail == null)
            {
                return(HttpNotFound());
            }
            ViewBag.order_id   = new SelectList(db.Orders, "id", "customer_name", order_detail.order_id);
            ViewBag.product_id = new SelectList(db.Products, "id", "name", order_detail.product_id);
            return(View(order_detail));
        }
Example #12
0
        //Thêm thông tin vào bảng Order_detail
        public void order_detail(Order_detail order)
        {
            DBConnection    dbConnection = new DBConnection();
            MySqlConnection connection   = dbConnection.ConnectionSql();

            connection.Open();

            string       sql_Add = "INSERT INTO `order_detail` ( image, name, quantity, price, id_order, product_id) VALUES (@image, @name, @quantity, @price, @id_order, @product_id)";
            MySqlCommand command = new MySqlCommand(sql_Add, connection);

            command.Parameters.AddWithValue("@image", order.image);
            command.Parameters.AddWithValue("@name", order.name);
            command.Parameters.AddWithValue("@quantity", order.quantity);
            command.Parameters.AddWithValue("@price", order.price);
            command.Parameters.AddWithValue("@id_order", order.id_order);
            command.Parameters.AddWithValue("@product_id", order.product_id);

            command.ExecuteNonQuery();
        }
Example #13
0
        public ActionResult Payment(string shipName, string mobile, string address, string email)
        {
            double totalprice  = Convert.ToDouble(Request.Form["totalprice"]);
            var    order       = new Order();
            var    usersession = (UserLogin)Session[Ban_Hang_Dien_Tu_CNWeb.Common.CommonConstants.USER_SESSION];

            if (usersession == null)
            {
            }
            else
            {
                Customer customer = db.Customers.Find(usersession.UserID);
                order.customer_id = customer.id;
            }
            order.created_at  = DateTime.Now;
            order.address     = address;
            order.email       = email;
            order.code        = shipName;
            order.phone       = mobile;
            order.total_price = totalprice;
            try
            {
                var id        = new OrderDao().Insert(order);
                var cart      = (List <CartItem>)Session[CartSession];
                var detailDao = new OrderDetailDao();
                foreach (var item in cart)
                {
                    var orderDetail = new Order_detail();

                    orderDetail.product_id  = item.Product.id;
                    orderDetail.order_id    = id;
                    orderDetail.total_price = item.Product.price;
                    orderDetail.amount      = item.Quantity;
                    detailDao.Insert(orderDetail);
                }
            }
            catch (Exception ex)
            {
                return(Redirect("/loi-thanh-toan"));
            }

            return(Redirect("/hoan-thanh"));
        }
        public ActionResult PlaceOrder(ShipmentInfo info)
        {
            List <CheckoutCartItem> cart = Session["checkout_cart"] as List <CheckoutCartItem>;

            if (cart == null)
            {
                return(Redirect(Request.UrlReferrer.ToString()));
            }

            db.ShipmentInfoes.Add(info);
            db.SaveChanges();

            Order order = new Order();

            order.user_id     = (int)Session["user_id"];
            order.shipment_id = info.id;
            order.created_on  = DateTime.Now;
            order.status      = "Pending";

            db.Orders.Add(order);
            db.SaveChanges();

            foreach (var cartItem in cart)
            {
                Order_detail detail = new Order_detail {
                    product_id = cartItem.product_id,
                    quantity   = cartItem.quantity,
                    order_id   = order.id
                };

                db.Order_detail.Add(detail);
            }
            db.SaveChanges();
            Session["checkout_cart"] = null;
            Session["cart"]          = null;
            TempData["message"]      = "Your order has been saved. Your order id is : " + order.id;
            return(Redirect("/Product/Index"));
        }
Example #15
0
        public ActionResult Payment(string shipName, string mobile, string address, string email)
        {
            double totalprice  = Convert.ToDouble(Request.Form["totalprice"]);
            var    order       = new Order();
            var    usersession = (UserLogin)Session[BanDoNoiThat.Common.CommonConstants.USER_SESSION];

            if (usersession == null)
            {
            }
            else
            {
                Customer customer = db.Customers.Find(usersession.UserID);
                order.id_customer = customer.id;
            }
            order.created_at    = DateTime.Now;
            order.address       = address;
            order.email         = email;
            order.customer_name = shipName;
            order.phone         = mobile;
            order.total_price   = totalprice;
            try
            {
                var id        = new OrderDao().Insert(order);
                var cart      = (List <CartItem>)Session[CartSession];
                var detailDao = new OrderDetailDao();
                foreach (var item in cart)
                {
                    var remain_quantity = db.Products.Find(item.Product.id).remain_quantity;
                    if (item.Quantity <= remain_quantity)
                    {
                    }
                    else
                    {
                        MessageBox.Show("Please do not exceed the remaining quantity");
                        return(RedirectToAction("Index"));
                    }
                }

                foreach (var item in cart)
                {
                    var remain_quantity = db.Products.Find(item.Product.id).remain_quantity;
                    if (item.Quantity <= remain_quantity)
                    {
                        var orderDetail = new Order_detail();
                        orderDetail.created_at  = DateTime.Now;
                        orderDetail.product_id  = item.Product.id;
                        orderDetail.order_id    = id;
                        orderDetail.total_price = item.Product.price * item.Quantity;
                        orderDetail.amount      = item.Quantity;
                        detailDao.Insert(orderDetail);
                        db.Products.Find(item.Product.id).remain_quantity -= item.Quantity;
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                return(Redirect("/loi-thanh-toan"));
            }

            return(Redirect("/hoan-thanh"));
        }
Example #16
0
 public Task RemoveAsync(Order_detail order_Detail)
 {
     _context.Remove(order_Detail);
     return(_context.SaveChangesAsync());
 }
Example #17
0
 public Task ModifiedAsync(Order_detail order_Detail)
 {
     _context.Entry(order_Detail).State = EntityState.Modified;
     return(_context.SaveChangesAsync());
 }
Example #18
0
 public Task AddAsync(Order_detail order_Detail)
 {
     _context.Order_details.AddAsync(order_Detail);
     return(_context.SaveChangesAsync());
 }
    protected void Page_Load(object sender, EventArgs e)
    {
        string connection = @"Data Source=DESKTOP-GCLEMNC\SQLEXPRESS;Initial Catalog=shop;Integrated Security=True";

        conn = new SqlConnection(connection);

        string name    = Request.Form["name"];
        string address = Request.Form["address"];
        string phone   = Request.Form["phone"];

        DateTime order_date = DateTime.Now;
        string   note       = Request.Form["note"];


        conn.Open();

        string     sql = "insert into orders values(@name, @phone, @address, @status, @orders_date, @note)";
        SqlCommand cmd = new SqlCommand(sql, conn);

        cmd.Parameters.AddWithValue("name", name);

        cmd.Parameters.AddWithValue("phone", phone);
        cmd.Parameters.AddWithValue("address", address);
        cmd.Parameters.AddWithValue("status", 0);
        cmd.Parameters.AddWithValue("orders_date", order_date.ToString());
        cmd.Parameters.AddWithValue("note", note);

        SqlDataReader dr = cmd.ExecuteReader();

        conn.Close();
        //Tao danh sach
        List <Order_detail> ds = new List <Order_detail>();
        //Cau lenh truy van

        string sql1 = "select * from orders order by orders_id DESC";

        conn.Open();
        //Thuc thi cau lenh

        SqlCommand cmd1 = new SqlCommand(sql1, conn);
        //Doc du lieu tren table trong sql
        SqlDataReader rd = cmd1.ExecuteReader();

        while (rd.Read())
        {
            Order_detail c = new Order_detail();
            c.orders_id = (int)rd["orders_id"];

            ds.Add(c);
        }
        conn.Close();
        int orders_id = 0;

        for (int i = 0; i < ds.Count(); i++)
        {
            Order_detail o = ds[i];
            orders_id = o.orders_id;
            if (orders_id > 0)
            {
                if (Session["giohang"] != null)
                {
                    List <Product_Detail> listCart = Session["giohang"] as List <Product_Detail>;

                    for (int j = 0; j < listCart.Count(); j++)
                    {
                        Product_Detail pd   = listCart[j];
                        string         sql2 = "insert into orders_detail values(@orders_id, @product_detail_id, @quantity, @price)";
                        conn.Open();
                        SqlCommand cmd2 = new SqlCommand(sql2, conn);

                        cmd2.Parameters.AddWithValue("orders_id", orders_id);

                        cmd2.Parameters.AddWithValue("product_detail_id", pd.product_detail_id);
                        cmd2.Parameters.AddWithValue("quantity", pd.quantity);
                        cmd2.Parameters.AddWithValue("price", pd.price);

                        SqlDataReader ex = cmd2.ExecuteReader();
                        conn.Close();
                    }
                }
                else
                {
                }
            }
            break;
        }
        if (Session["giohang"] != null)
        {
            List <Product_Detail> listCart = Session["giohang"] as List <Product_Detail>;

            for (int j = 0; j < listCart.Count(); j++)
            {
                Product_Detail pdd       = listCart[j];
                string         sqlUpdate = "Update product_detail set product_detail.quantity = product_detail.quantity-@quantity where product_detail.product_detail_id=@id";
                conn.Open();
                SqlCommand cmdU = new SqlCommand(sqlUpdate, conn);

                cmdU.Parameters.AddWithValue("id", pdd.product_detail_id);
                cmdU.Parameters.AddWithValue("quantity", pdd.quantity);
                cmdU.Parameters.AddWithValue("price", pdd.price);
                SqlDataReader ex1 = cmdU.ExecuteReader();
                conn.Close();
            }
        }
    }