public ActionResult DatHang(Order order)
        {
            //List<Giohang> lstgiohangnew = Session["GioHang"] as List<Giohang>;
            List <Giohang> lstGioHang1 = Session["GioHang" + Session["DangNhap"]] as List <Giohang>;
            //List<Giohang> lstGioHang = Session["DonHangDaDat" + Session["DangNhap"]] as List<Giohang>;
            //if (lstGioHang == null)
            //{
            //    //Nếu giỏ hang chưa tồn tại thì ta khởi tạo mới list giỏ hàng
            //    lstGioHang = new List<Giohang>();
            //    Session["DonHangDaDat" + Session["DangNhap"]] = lstGioHang;
            //    foreach (var item in lstGioHang1)
            //    {
            //        lstGioHang.Add(item);
            //    }
            //}
            //else
            //{
            //    foreach (var item in lstGioHang1)
            //    {
            //        lstGioHang.Add(item);
            //    }
            //}

            //Session["GioHang" + Session["DangNhap"]] = null;
            //Thêm đơn hàng
            //Order order = new Order();
            User us = Session["User"] as User;

            order.Users_id = us.Users_id;
            order.ngaytao  = DateTime.Now;
            order.tongtien = (double)Session["TienThanhToan"];
            //order.giaohang = "COD";
            db.Orders.Add(order);
            db.SaveChanges();
            List <Giohang> gh = LayGioHang();

            foreach (var item in gh)
            {
                Orders_Details ordersD = new Orders_Details();
                ordersD.Order_id    = order.Order_id;
                ordersD.products_id = item.iMaSP;
                ordersD.soluongsp   = item.iSoLuong;
                ordersD.tongtien    = item.dThanhTien;
                db.Orders_Details.Add(ordersD);
                //XoaGioHang(item.iMaSP);
            }
            db.SaveChanges();
            Session["GioHang" + Session["DangNhap"]] = null;
            return(Redirect("Index"));
        }
        public ActionResult ThongtinKH(Order order, string city_id, string district_id, string ward_id, string sonha_id)
        {
            if (city_id == "")
            {
                ViewBag.Address = "Vui lòng chọn thành phố!";
                return(View());
            }
            else if (district_id == "")
            {
                ViewBag.Address = "Vui lòng chọn quận huyện!";
                return(View());
            }
            else if (ward_id == "")
            {
                ViewBag.Address = "Vui lòng chọn phường xã!";
                return(View());
            }
            else if (sonha_id == "")
            {
                ViewBag.Address = "Vui lòng điền số nhà!";
                return(View());
            }
            //return RedirectToAction("DatHang","Cart",  new { order, sonha_id, district_id, ward_id });

            List <Giohang> lstGioHang = Session["GioHang" + Session["DangNhap"]] as List <Giohang>;
            User           user       = Session["User"] as User;

            order.address += sonha_id;
            Dictionary <string, RootObject> items = new Dictionary <string, RootObject>();
            string tmp      = "";
            var    pathWard = Path.Combine(Server.MapPath("~/Content/NguoiDungCssLayout/json/xa-phuong/" + district_id + ".json"));

            using (StreamReader rd = new StreamReader(pathWard))
            {
                tmp   = rd.ReadToEnd();
                items = JsonConvert.DeserializeObject <Dictionary <string, RootObject> >(tmp);
                tmp   = "";
                foreach (var item in items)
                {
                    if (item.Key == ward_id)
                    {
                        order.address += ", " + item.Value.Path_with_type;
                        break;
                    }
                }
            }
            List <Giohang> gh = LayGioHang();

            order.Users_id = user.Users_id;
            order.ngaytao  = DateTime.Now;
            //order.tongtien = (double)Session["TienThanhToan"];
            order.trangthai = "Chưa xác nhận";
            //db.Orders.Add(order);
            //db.SaveChanges();
            //List<Giohang> gh = LayGioHang();
            int giatien = 0;

            foreach (var item in gh)
            {
                giatien += Convert.ToInt32(item.dThanhTien);
                Orders_Details orderD = new Orders_Details();
                orderD.Order_id    = order.Order_id;
                orderD.products_id = item.iMaSP;
                orderD.soluongsp   = item.iSoLuong;
                orderD.tongtien    = item.dThanhTien;
                db.Orders_Details.Add(orderD);
                //string[] arrListStr = str.Split(',');
            }
            order.tongtien = giatien * 1.1;
            db.Orders.Add(order);
            db.SaveChanges();
            Session["GioHang" + Session["DangNhap"]] = null;
            return(Redirect("DatHangThanhCong"));
        }
Esempio n. 3
0
        public ActionResult Save_Order(FormCollection fc)
        {
            int     id = 0;
            decimal a = 2500, b = 3000, c = 2000, d = 3500, e = 6000;

            if (fc["Delivery"] == "2")
            {
                Session["price_send"] = a;
            }
            else if (fc["Delivery"] == "3")
            {
                Session["price_send"] = b;
            }
            else if (fc["Delivery"] == "4")
            {
                Session["price_send"] = c;
            }
            else if (fc["Delivery"] == "5")
            {
                Session["price_send"] = c;
            }
            else if (fc["Delivery"] == "6")
            {
                Session["price_send"] = d;
            }
            else
            {
                Session["price_send"] = e;
            }

            List <item> items   = (List <item>)Session["cart"];
            decimal     summary = 0;

            try
            {
                Order orders = new Order();
                orders.Grand_total  = Convert.ToDecimal(Session["Grand_Total"]);
                orders.Customer_Id  = Convert.ToInt32(Session["UserID"]);
                orders.Card_No      = fc["Pay"];
                orders.Card_Name    = fc["Delivery"];
                orders.Order_date   = DateTime.Now;
                orders.Order_status = "New";
                pe.Orders.Add(orders);
                pe.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                                                       validationErrors.Entry.Entity.ToString(),
                                                       validationError.ErrorMessage);
                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise;
            }

            try
            {
                foreach (item it in items)
                {
                    if (it.Pet.Pet_Id != null || it.Product.Product_Id != null || it.Trainer.Trainer_Id != null)
                    {
                        if (it.Pet.Pet_Id >= 1 && it.Pet.Pet_Id <= 39999)
                        {
                            id = it.Pet.Pet_Id;
                        }
                        else if (it.Product.Product_Id >= 40000 && it.Product.Product_Id <= 99500)
                        {
                            id = it.Product.Product_Id;
                        }
                        else if (it.Trainer.Trainer_Id >= 99500 && it.Trainer.Trainer_Id <= 110000)
                        {
                            id = it.Trainer.Trainer_Id;
                        }
                        else
                        {
                        }
                    }
                    if (id >= 1 && id <= 39999)
                    {
                        Orders_Details od = new Orders_Details();
                        od.Order_Id   = pe.Orders.Max(item => item.Order_Id);
                        od.Product_Id = it.Pet.Pet_Id;
                        od.Amount     = it.Quantity;
                        od.Sub_total  = (it.Pet.Price * it.Quantity);
                        pe.Orders_Details.Add(od);
                        pe.SaveChanges();
                    }
                    else if (id >= 40000 && id < 99500)
                    {
                        Orders_Details od = new Orders_Details();
                        od.Order_Id    = pe.Orders.Max(item => item.Order_Id);
                        od.Product_Id2 = it.Product.Product_Id;
                        od.Amount      = it.QuantityProduct;
                        od.Sub_total   = (it.Product.Price * it.QuantityProduct);
                        pe.Orders_Details.Add(od);
                        pe.SaveChanges();
                    }
                    else if (id >= 99500 && id < 110000)
                    {
                        Orders_Details od = new Orders_Details();
                        od.Order_Id    = pe.Orders.Max(item => item.Order_Id);
                        od.Product_Id3 = it.Trainer.Trainer_Id;
                        od.Amount      = it.QuantityTrainer;
                        od.Sub_total   = (it.Trainer.Price * it.QuantityTrainer);
                        pe.Orders_Details.Add(od);
                        pe.SaveChanges();
                    }
                    else
                    {
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                                                       validationErrors.Entry.Entity.ToString(),
                                                       validationError.ErrorMessage);
                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise;
            }

            if (fc["Pay"] == "30")
            {
                Session["Checkdiv"] = "1";
                return(View("AddressInput"));
            }
            else
            {
                return(View("AddressInput"));
            }
        }