public JsonResult AddCart(SanPham s)
        {
            if (Session["giohang"] == null)
            {
                Session["giohang"] = new List <CTDonHang>();
            }
            List <CTDonHang> giohang = Session["giohang"] as List <CTDonHang>;
            CTDonHang        d       = null;

            //Kiểm tra xem sản phẩm khách đang chọn đã có trong giỏ hàng chưa
            if (giohang.Find(m => m.MaSP == s.MaSP) == null)
            {
                d         = new CTDonHang();
                d.MaSP    = s.MaSP;
                d.DonGia  = s.DonGia;
                d.SoLuong = 1;
                giohang.Add(d);
            }
            else
            {
                giohang.Find(m => m.MaSP == s.MaSP).SoLuong = giohang.Find(m => m.MaSP == s.MaSP).SoLuong + 1;
            }
            float soluong = 0;

            foreach (CTDonHang c in giohang)
            {
                soluong = soluong + c.SoLuong;
            }
            return(Json(new { ctdon = d, sl = soluong }, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 2
0
        public void TaoDonHang(DonHang dh)
        {
            dh.NgayDat = DateTime.Now;

            dh.TongTien = _GioHangDb.TinhTongTienGioHang();

            _context.DonHangDb.Add(dh);

            var chitietgiohang = _GioHangDb.ChiTietGioHang;

            foreach (var c in chitietgiohang)
            {
                var chitietdonhang = new CTDonHang()
                {
                    SoLuong    = c.SoLuong,
                    ID_SanPham = c.SanPham.ID_SanPham,
                    ID_DonHang = dh.ID_DonHang,
                    Gia        = c.SanPham.DonGia
                };

                _context.CTDonHangDb.Add(chitietdonhang);
            }

            _context.SaveChanges();
        }
        //public ActionResult DatHang()
        //{
        //    if(Session["Client_GioHang"] == null)
        //    {
        //        return RedirectToAction("Index", "Client_TrangChu");
        //    }
        //    List<Client_GioHang> lstGiohang = LayClient_GioHang();
        //    ViewBag.TongSoLuong = TongSoLuong();
        //    ViewBag.TongTien = TongTien();

        //    return View(lstGiohang);
        //}

        public ActionResult DatHang(FormCollection collection)
        {
            string  s                = collection["inputtext"];
            var     MaKH             = db.KhachHangs.SingleOrDefault(n => n.SoDT == s).KhachHangId;
            DonHang DH               = new DonHang();
            List <Client_GioHang> gh = LayClient_GioHang();

            DH.KhachHangId      = MaKH;
            DH.CuaHangId        = 4;
            DH.NgayMua          = DateTime.Now;
            DH.SoLuongBan       = TongSoLuong();
            DH.TongTien         = TongTien();
            ViewBag.TongSoLuong = TongSoLuong();
            ViewBag.TongTien    = TongTien();
            db.DonHangs.Add(DH);
            db.SaveChanges();
            foreach (var item in gh)
            {
                CTDonHang CTDH = new CTDonHang();
                CTDH.DonhangId = DH.DonHangId;
                CTDH.SanPhamId = item.cSanPhamId;
                CTDH.SoLuong   = item.cSoLuong;
                CTDH.DonGia    = item.cDonGia;
                CTDH.GiamGia   = item.cGiamGia;
                db.CTDonHangs.Add(CTDH);
            }
            db.SaveChanges();
            Session["Client_GioHang"] = null;
            return(RedirectToAction("Index", "client_TrangChu"));
        }
Exemplo n.º 4
0
        public rs dat_hang(DatHangForm model)
        {
            rs r;

            using (var __db = new vuong_cms_context())
            {
                if (ModelState.IsValid)
                {
                    using (var tx = __db.Database.BeginTransaction())
                    {
                        try
                        {
                            //donhang
                            DonHang donhang = new DonHang();
                            donhang.UserId               = model.UserId;
                            donhang.GhiChuDonHang        = model.GhiChu;
                            donhang.TrangThaiGiaoHangId  = 1;
                            donhang.TrangThaiThanhToanId = 1;
                            //ctdh
                            donhang.CTDonHangs = new Collection <CTDonHang>();
                            foreach (var gio in model.ChiTietDonHang)
                            {
                                var       sanphamdb = __db.Product.Find(gio.SanPhamId);
                                CTDonHang newct     = new CTDonHang()
                                {
                                    SanPhamId = gio.SanPhamId,
                                    SoLuong   = gio.SoLuong,
                                };
                                newct.ThanhTien = newct.DonGia * newct.SoLuong;
                                donhang.CTDonHangs.Add(newct);
                            }
                            var __tongtiendonhang = donhang.CTDonHangs.Sum(s => s.DonGia * s.SoLuong);
                            donhang.TongTienHang = __tongtiendonhang;

                            //user
                            __db.DonHangs.Add(donhang);
                            __db.SaveChanges();

                            //com
                            tx.Commit();
                            r = rs.T("Đặt hàng thành công");
                        }
                        catch (Exception ex)
                        {
                            r = rs.F("Lỗi máy chủ " + ex.Message);
                            tx.Rollback();
                        }
                    }
                }
                else
                {
                    //all error
                    r = rs.F(string.Join(";", ModelState.Values
                                         .SelectMany(x => x.Errors)
                                         .Select(x => x.ErrorMessage).Distinct()));
                }
            }
            return(r);
        }
Exemplo n.º 5
0
        public ActionResult DeleteConfirmed(int id)
        {
            CTDonHang cTDonHang = db.CTDonHangs.Find(id);

            db.CTDonHangs.Remove(cTDonHang);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Exemplo n.º 6
0
        public ActionResult Payment(string diachiadd, string mobileadd, DateTime dateout)
        {
            // A
            var order = new DonHang();

            order.Ngaylap        = DateTime.Now;
            order.Diachigiaohang = diachiadd;
            order.Phone          = mobileadd;
            order.Trangthai      = true;
            DateTime?date = null;
            DateTime temp;

            order.Ngaynhanhang = dateout;

            //if (DateTime.TryParse(dateout, out temp))
            //{
            //    if (temp != null)
            //        date = temp;
            //}

            //if (date != null)
            //    order.Ngaynhanhang = date.Value;


            //nếu login
            if (CommonConstant.USERNAME != null)
            {
                order.KhachhangID = new TaikhoanFunction().GetByID(CommonConstant.USERNAME.username);

                var account = new TaikhoanFunction().FindEntity(order.KhachhangID);
                order.Hotenkhachhang = account.Tenkhachhang;
            }
            try
            {
                var id        = new DonhangFunction().Insert(order);
                var cart      = (Cart)Session["CartSession"];
                var detailDao = new CTDonhangFunction();
                foreach (var item in cart.Lines)
                {
                    var orderDetail = new CTDonHang();
                    orderDetail.SanphamID = item.sanpham.SanphamID;
                    orderDetail.DonhangID = id;
                    orderDetail.Soluong   = item.Quantity;
                    orderDetail.Dongia    = (item.sanpham.Giabandau * item.Quantity);
                    detailDao.Insert(orderDetail);
                }
                Session["CartSession"] = null;
            }
            catch (Exception ex)
            {
                //ghi log
                return(RedirectToAction("Loi")); // action Loi ở đâu?
            }

            return(RedirectToAction("MuaHangThanhCong", "Cart"));
        }
Exemplo n.º 7
0
        public int Insert(CTDonHang model)
        {
            CTDonHang dbEntry = db.CTDonHangs.Find(model.CTDonhangID);

            if (dbEntry != null)
            {
                return(0);
            }
            db.CTDonHangs.Add(model);
            db.SaveChanges();
            return(1);
        }
Exemplo n.º 8
0
 public ActionResult Edit([Bind(Include = "CTDonHangId,SoLuong,DonGia,GiamGia,DonhangId,SanPhamId")] CTDonHang cTDonHang)
 {
     if (ModelState.IsValid)
     {
         db.Entry(cTDonHang).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.DonhangId = new SelectList(db.DonHangs, "DonHangId", "HinhThucTT", cTDonHang.DonhangId);
     ViewBag.SanPhamId = new SelectList(db.SanPhams, "SanPhamId", "TenSP", cTDonHang.SanPhamId);
     return(View(cTDonHang));
 }
Exemplo n.º 9
0
        public int tangSoLuong(int maDH, string maSP)
        {
            CTDonHang ctTim = db.CTDonHangs.FirstOrDefault(n => n.MaDH.Equals(maDH) && n.MaSP.Equals(maSP));

            if (ctTim == null)
            {
                return(-1);
            }
            ctTim.SoLuong += 1;
            db.SubmitChanges();
            return(int.Parse(ctTim.SoLuong.ToString()));
        }
Exemplo n.º 10
0
        public bool them(CTDonHang ctDon)
        {
            var       list  = db.CTDonHangs.ToList();
            CTDonHang ctTim = db.CTDonHangs.FirstOrDefault(n => n.MaDH.Equals(ctDon.MaDH) && n.MaSP.Equals(ctDon.MaSP));

            if (ctTim != null)
            {
                return(false);
            }

            db.CTDonHangs.InsertOnSubmit(ctDon);
            return(true);
        }
Exemplo n.º 11
0
        public bool xoa(int maDH, string maSP)
        {
            CTDonHang ctTim = db.CTDonHangs.FirstOrDefault(n => n.MaDH.Equals(maDH) && n.MaSP.Equals(maSP));

            if (ctTim == null)
            {
                return(false);
            }
            db.CTDonHangs.DeleteOnSubmit(ctTim);
            db.SubmitChanges();
            _PublicStatic.clearCache(db);
            return(true);
        }
Exemplo n.º 12
0
        // GET: CTDonHangs/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CTDonHang cTDonHang = db.CTDonHangs.Find(id);

            if (cTDonHang == null)
            {
                return(HttpNotFound());
            }
            return(View(cTDonHang));
        }
Exemplo n.º 13
0
        // GET: CTDonHangs/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CTDonHang cTDonHang = db.CTDonHangs.Find(id);

            if (cTDonHang == null)
            {
                return(HttpNotFound());
            }
            ViewBag.DonhangId = new SelectList(db.DonHangs, "DonHangId", "HinhThucTT", cTDonHang.DonhangId);
            ViewBag.SanPhamId = new SelectList(db.SanPhams, "SanPhamId", "TenSP", cTDonHang.SanPhamId);
            return(View(cTDonHang));
        }
Exemplo n.º 14
0
 private void button1_Click_1(object sender, EventArgs e)
 {
     try
     {
         string mausac   = cb_MauSac.SelectedValue.ToString();
         int    kichco   = int.Parse(cb_KichCo.SelectedValue.ToString());
         string chatlieu = cb_ChatLieu.SelectedValue.ToString();
         int    masp     = int.Parse(cb_SanPham.SelectedValue.ToString());
         // viết cho phần hiển thị sản phẩm
         string tensp    = cb_SanPham.Text;
         string Mausac   = cb_MauSac.Text;
         string Chatlieu = cb_ChatLieu.Text;
         int    kichcos  = int.Parse(cb_KichCo.Text);
         float  gia      = SanPhamModels.SanPhams.getGia(masp);
         int    soluong  = 0;
         try
         {
             soluong = int.Parse(txt_SoLuong.Text);
             CTDonHang a       = new CTDonHang();
             Cart      giohang = new Cart(tensp, Mausac, Chatlieu, kichcos, gia, soluong, soluong * gia);
             cartList.Add(giohang);
             a.MaCTSp  = SanPhamModels.SanPhams.getCTsp(chatlieu, kichco, mausac, masp);
             a.SoLuong = soluong;
             listCT.Add(a);
             soluongss++;
             txt_SoLuong.Text = null;
             button2.Enabled  = true;
             button3.Enabled  = true;
             foreach (Cart item in cartList)
             {
                 tongtien += item.Giatien;
             }
             getdata();
         }
         catch (Exception)
         {
             MessageBox.Show("Số lượng không được để trống và chỉ được nhập số");
         }
     }
     catch (Exception)
     {
         MessageBox.Show("Không thể thêm sản phẩm");
     }
 }
Exemplo n.º 15
0
        public ActionResult Payment(string HoTen, string DienThoai, string DiaChi, string Email)
        {
            if (csdl.KhachHangs.ToList().Exists(x => x.HoTen.Trim() == HoTen.Trim() && x.DienThoai.Trim() == DienThoai.Trim()) == false)
            {
                KhachHang kh = new KhachHang();
                kh.HoTen     = HoTen;
                kh.DiaChi    = DiaChi;
                kh.Email     = Email;
                kh.DienThoai = DienThoai;
                csdl.KhachHangs.InsertOnSubmit(kh);
                csdl.SubmitChanges();
            }
            var     DetailCustomer = csdl.KhachHangs.FirstOrDefault(x => x.HoTen.Trim() == HoTen.Trim() && x.DienThoai.Trim() == DienThoai.Trim());
            DonHang NewOrder       = new DonHang();

            NewOrder.MaKH              = DetailCustomer.MaKH;
            NewOrder.NgayDat           = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
            NewOrder.NgayGiao          = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
            NewOrder.TinhTrangGiaoHang = false;
            NewOrder.DaThanhToan       = false;
            csdl.DonHangs.InsertOnSubmit(NewOrder);
            csdl.SubmitChanges();
            var ListCart = Session[CommonConstants.Cart_SESSION] as List <CartItem>;

            try
            {
                foreach (var item in ListCart)
                {
                    CTDonHang ct = new CTDonHang();
                    ct.MaSP    = item.Product.MaSP;
                    ct.MaDH    = NewOrder.MaDH;
                    ct.SoLuong = item.quantity;
                    ct.DonGia  = item.Product.GiaBan * item.quantity;
                    csdl.CTDonHangs.InsertOnSubmit(ct);
                    csdl.SubmitChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(Redirect("Success"));
        }
Exemplo n.º 16
0
        public bool suaSL(DataTable dtChiTietDonHang)
        {
            foreach (DataRow CTDonHang in dtChiTietDonHang.Rows)
            {
                SanPhamPublic SP    = new SanPhamPublic();
                SanPhamBUS    spbus = new SanPhamBUS();
                try
                {
                    SP.MASP = CTDonHang.Field <string>("MaSP");

                    int slcu = spbus.LaySanPham(SP).SOLUONG;
                    int slt  = CTDonHang.Field <int>("SoLuong");
                    SP.SOLUONG = slcu - slt;
                    spbus.capnhatsoluong(SP);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return(true);
        }
Exemplo n.º 17
0
        public ActionResult MuaHang(FormCollection collection)
        {
            string s    = collection["inputtext"];
            var    MaKH = db.KhachHangs.SingleOrDefault(n => n.SoDT == s).KhachHangId;


            DonHang        DH   = new DonHang();
            List <GioHang> gh   = LayGioHang();
            var            MaCH = int.Parse(User.TenCuaHang());

            DH.KhachHangId      = MaKH;
            DH.CuaHangId        = MaCH;
            DH.NgayMua          = DateTime.Now;
            DH.SoLuongBan       = TongSoLuong();
            DH.TongTien         = TongTien();
            ViewBag.TongSoLuong = TongSoLuong();
            ViewBag.TongTien    = TongTien();
            db.DonHangs.Add(DH);

            foreach (var item in gh)
            {
                CTDonHang CTDH = new CTDonHang();
                CTDH.DonhangId = DH.DonHangId;
                CTDH.SanPhamId = item.gSanPhamId;
                CTDH.SoLuong   = item.gSoLuong;
                CTDH.DonGia    = item.gDonGia;
                CTDH.GiamGia   = item.gGiamGia;
                db.CTDonHangs.Add(CTDH);

                var get = db.SanPhams.SingleOrDefault(n => n.SanPhamId == item.gSanPhamId);
                get.SoLuong         = (get.SoLuong - item.gSoLuong);
                db.Entry(get).State = EntityState.Modified;
            }
            db.SaveChanges();
            Session["GioHang"] = null;
            return(RedirectToAction("DS", "BanHang2"));
        }
Exemplo n.º 18
0
        public rs dat_hang_qr(DatHangQRForm model)
        {
            rs r;

            using (var __db = new vuong_cms_context())
            {
                if (ModelState.IsValid)
                {
                    using (var tx = __db.Database.BeginTransaction())
                    {
                        try
                        {
                            var quan = __db.Quan.FirstOrDefault(f => f.Id == model.QuanId);

                            //var ban = __db.Ban.First(f => f.Id == model.BanId && f.QuanId == model.QuanId);
                            if (quan == null)
                            {
                                throw new Exception("Bàn và quán không hợp lệ, vui lòng thử lại!");
                            }

                            var BanArr = new List <int>();
                            if (!string.IsNullOrWhiteSpace(quan.BanArr))
                            {
                                try
                                {
                                    BanArr = quan.BanArr.Split(',').Select(Int32.Parse).Distinct().ToList();
                                }
                                catch
                                {
                                }
                            }
                            if (model.HinhThucMuaHangId == 1 && !BanArr.Contains(model.Ban))
                            {
                                throw new Exception("Số Bàn nhập không tồn tại trong quán " + quan.TenQuan + ", vui lòng thử lại!");
                            }
                            //donhang
                            DonHang donhang = new DonHang();
                            //donhang.BanId = model.BanId;
                            donhang.Ban                  = model.Ban;
                            donhang.QuanId               = model.QuanId;
                            donhang.GhiChuDonHang        = "";
                            donhang.TrangThaiGiaoHangId  = 1;
                            donhang.TrangThaiThanhToanId = 1;
                            donhang.UserId               = null;
                            donhang.BaseUserId           = quan.UserId;
                            donhang.DeviceType           = 2;//mobile
                            donhang.UniqueID             = model.UniqueID;
                            donhang.ModelNumber          = model.ModelNumber;
                            donhang.HinhThucMuaHangId    = model.HinhThucMuaHangId;
                            donhang.SDT                  = model.SDT;
                            donhang.YeuCauKhac           = model.YeuCauKhac;
                            donhang.DiaChiGiaoHang       = model.DiaChiGiaoHang;
                            donhang.TienKhachDua         = 0;
                            //ctdh
                            donhang.CTDonHangs = new Collection <CTDonHang>();
                            foreach (var gio in model.ChiTietDonHang)
                            {
                                Product sp = __db.Product.FirstOrDefault(f => f.Id == gio.SanPhamId && f.ProductCat.QuanId == model.QuanId);

                                if (sp == null)
                                {
                                    throw new Exception("Sản phẩm không tồn tại, vui lòng xóa tất cả sản phẩm giỏ hàng và chọn lại!");
                                }
                                var    price  = sp.Price;
                                string tengia = "";
                                if (gio.GiaId != 0)
                                {
                                    var check_gia = __db.BangGiaCT.FirstOrDefault(f => f.Id == gio.GiaId);
                                    if (check_gia == null)
                                    {
                                        throw new Exception("Bảng giá sản phẩm đã cũ, vui lòng tạo lại đơn hàng!");
                                    }
                                    else
                                    {
                                        price  = check_gia.Price;
                                        tengia = check_gia.Ten;
                                    }
                                }
                                CTDonHang newct = new CTDonHang()
                                {
                                    SanPhamId = gio.SanPhamId,
                                    SoLuong   = gio.SoLuong,
                                    DonGia    = price,
                                    ThucDonId = gio.ThucDonId,
                                    TenGia    = tengia,
                                    GiaId     = gio.GiaId,
                                };
                                newct.ThanhTien = newct.DonGia * newct.SoLuong;
                                donhang.CTDonHangs.Add(newct);
                            }
                            var __tongtiendonhang = donhang.CTDonHangs.Sum(s => (int?)s.DonGia * s.SoLuong) ?? 0;
                            donhang.TongTienHang = __tongtiendonhang;

                            //user
                            __db.DonHangs.Add(donhang);
                            __db.SaveChanges();

                            //com
                            tx.Commit();
                            r = rs.T("Đặt hàng thành công");
                            IHubContext context = GlobalHost.ConnectionManager.GetHubContext <UsersOnLine>();
                            context.Clients.All.notify_new_donhang(donhang.BaseUserId, "Đơn hàng mới tại bàn: " + donhang.Ban);
                        }
                        catch (Exception ex)
                        {
                            r = rs.F(ex.Message);
                            tx.Rollback();
                        }
                    }
                }
                else
                {
                    //all error
                    r = rs.F("Lỗi, vui lòng thử lại. " + string.Join(";", ModelState.Values
                                                                     .SelectMany(x => x.Errors)
                                                                     .Select(x => x.ErrorMessage).Distinct()));
                }
            }
            return(r);
        }
Exemplo n.º 19
0
        public CTDonHang FindEntity(int ID_CTDH)
        {
            CTDonHang dbEntry = db.CTDonHangs.Find(ID_CTDH);

            return(dbEntry);
        }
        public ActionResult Client_GioHang(FormCollection collection, string RadSDT)
        {
            string  tenKho           = collection["MaKhoHang"];
            var     KhoHangId        = db.KhoHangs.SingleOrDefault(n => n.TenKho == tenKho).KhoHangId;
            DonHang DH               = new DonHang();
            List <Client_GioHang> gh = LayClient_GioHang();

            DH.KhachHangId = 1;
            DH.CuaHangId   = KhoHangId;
            DH.NgayMua     = DateTime.Now;
            DH.SoLuongBan  = TongSoLuong();
            int tongtien = TongTien();

            DH.TongTien    = TongTien();
            DH.TokenKey    = RandomNumber(6);
            DH.TrangThaiDH = EnumExtensions.GetDescription(Enumstatus.NotConfirm);
            string tenform = collection["TenForm"];

            switch (tenform)
            {
            case "form1":
                DH.HinhThucTT = EnumExtensions.GetDescription(Enumstatus.SHIP_COD);
                break;

            case "form2":
                DH.HinhThucTT = EnumExtensions.GetDescription(Enumstatus.TT_Online);
                break;

            case "form3":
                DH.HinhThucTT = EnumExtensions.GetDescription(Enumstatus.Pick_Up);
                break;
            }

            ViewBag.TongSoLuong = TongSoLuong();
            ViewBag.TongTien    = TongTien();
            db.DonHangs.Add(DH);
            db.SaveChanges();
            foreach (var item in gh)
            {
                CTDonHang CTDH = new CTDonHang();
                CTDH.DonhangId = DH.DonHangId;
                CTDH.SanPhamId = item.cSanPhamId;
                CTDH.SoLuong   = item.cSoLuong;
                CTDH.DonGia    = item.cDonGia;
                CTDH.GiamGia   = item.cGiamGia;
                db.CTDonHangs.Add(CTDH);
            }
            db.SaveChanges();
            Session["Client_GioHang"] = null;
            if (collection["rad"] == "RadSDT")
            {
                string     ToNumber = "+84" + collection["Sdt"].Trim(',', '0');
                ServiceSMS sms      = new ServiceSMS();
                sms.SendSMS(ToNumber, DH.TokenKey);
                return(RedirectToAction("VerifySMS"));
            }
            if (collection["rad"] == "RadMail")
            {
                SendMail();
                return(RedirectToAction("VerifyMail"));
            }
            return(View());

            void SendMail()
            {
                var         tenKH          = collection["TenKH"].Trim(',');
                var         Sdt            = collection["Sdt"].Trim(',');
                var         Diachi         = collection["Diachi"].Trim(',');
                var         Email          = collection["Email"].Trim(',');
                DateTime    NgayGiaoDuKien = DH.NgayMua.AddDays(3);
                var         callbackUrl    = "https://truongproduction2706.azurewebsites.net/" + Url.Action("ConfirmDH", "Client_GioHang", new { @TokenKey = DH.TokenKey });
                EmailSender email          = new EmailSender();

                email.ConfirmDatHangAsync(tenKH, Email, tenKH, DH.SoLuongBan.ToString(), string.Format("{0:0,0}", DH.TongTien), DH.NgayMua.ToShortDateString(), Email, Diachi, NgayGiaoDuKien.ToShortDateString(), callbackUrl);
            }
        }