public ActionResult ThemMoi(DangKyPhong dky)
 {
     if (ModelState.IsValid)
     {
         db.DangKyPhongs.Add(dky);
         db.SaveChanges();
     }
     return(RedirectToAction("DangKyPhong"));
 }
 //[ValidateInput(false)]
 public ActionResult Sua(DangKyPhong dky)
 {
     if (ModelState.IsValid)
     {
         //Thực hiện cập nhật trong model
         db.Entry(dky).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
     return(RedirectToAction("DangKyPhong"));
 }
        public ActionResult Xoa(int MaPhong)
        {
            //lấy ra đối tượng khách hàng theo mã
            DangKyPhong dky = db.DangKyPhongs.FirstOrDefault(n => n.MaPhong == MaPhong);

            if (dky == null)
            {
                Response.StatusCode = 404;
                return(null);
            }

            return(View(dky));
        }
        public ActionResult XacnhanXoa(int MaPhong)
        {
            DangKyPhong dky = db.DangKyPhongs.FirstOrDefault(n => n.MaPhong == MaPhong);

            if (dky == null)
            {
                Response.StatusCode = 404;
                return(null);
            }
            db.DangKyPhongs.Remove(dky);
            db.SaveChanges();
            return(RedirectToAction("DangKyPhong"));
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            string phong = "";

            if (glueDoitac.Text == "")
            {
                MessageBox.Show("Vui lòng chọn khách hàng hoặc đối tác!");
                glueDoitac.Focus();
                return;
            }
            else
            {
                var idkhach = Convert.ToInt32(glueDoitac.EditValue);
                if (t == 1)
                {
                    if (txtSo.Text == "")
                    {
                        if (gcDangky.DataSource == null)
                        {
                            MessageBox.Show("Vui lòng chọn phòng để đăng ký");
                            return;
                        }
                        Dangky dk = new Dangky();
                        dk.NgayDK       = Convert.ToDateTime(dateNgay.EditValue.ToString());
                        dk.NgayCheckin  = Convert.ToDateTime(dateCheckin.EditValue.ToString());
                        dk.NgayCheckout = Convert.ToDateTime(dateCheckout.EditValue.ToString());
                        dk.SotienUT     = Convert.ToDouble(tbDattruoc.Text.ToString());
                        dk.Sokhach      = Convert.ToInt32(gvDangky.Columns[2].SummaryItem.SummaryValue);
                        dk.Sophong      = Convert.ToInt32(gvDangky.Columns[3].SummaryItem.SummaryValue);
                        dk.DaCheckin    = false;
                        dk.Ghichu       = tbNoidung.Text.ToString();
                        dk.HinhthucTT   = cbHinhthuc.EditValue.ToString();
                        dk.Loaitien     = cbxLoai.EditValue.ToString();
                        if (dk.HinhthucTT == "Tiền mặt")
                        {
                            dk.SoATM = "";
                        }
                        else
                        {
                            dk.SoATM = tbSoTK.EditValue.ToString();
                        }
                        dk.Tygia  = Convert.ToInt32(tbTygia.Text.ToString());
                        dk.MaBank = glueNganhang.EditValue.ToString();
                        dk.NgayUT = Convert.ToDateTime(dtDattruoc.EditValue.ToString());
                        db.Dangky.Add(dk);
                        db.SaveChanges();
                        var id = db.Dangky.Where(m => m.DaCheckin == false).OrderByDescending(m => m.Id).First();
                        for (int j = 0; j < gvDangky.RowCount; j++)
                        {
                            GridView dView = gvDangky.GetDetailView(j, 0) as GridView;
                            if (dView == null)
                            {
                                MessageBox.Show("Vui lòng nhập thông tin khách hàng đăng ký!");
                                return;
                            }
                            phong.Trim();
                            phong += gvDangky.GetRowCellValue(j, gvDangky.Columns[0]).ToString();
                            if (j < gvDangky.RowCount - 1)
                            {
                                phong += ",";
                            }
                            DK_Customer dk_khach = new DK_Customer();
                            Khach       khach    = new Khach();
                            DangKyPhong dk_phong = new DangKyPhong();
                            for (int i = 0; i < dView.RowCount; i++)
                            {
                                if (dView.GetRowCellValue(i, dView.Columns[0]) != null && dView.GetRowCellValue(i, dView.Columns[1]) != null)
                                {
                                    khach.HoTen    = dView.GetRowCellValue(i, dView.Columns[1]).ToString();
                                    khach.SoCMND   = dView.GetRowCellValue(i, dView.Columns[0]).ToString();
                                    khach.GioiTnh  = Convert.ToBoolean(dView.GetRowCellValue(i, dView.Columns[2]));
                                    khach.MaQT     = Convert.ToInt32(dView.GetRowCellValue(i, dView.Columns[3]));
                                    khach.NgaySinh = DateTime.Now;
                                    khach.DonVi    = "Khách hàng";
                                    if (edtDienThoai.Text == "Nhập số điện thoại")
                                    {
                                        khach.Phone = "";
                                    }
                                    else
                                    {
                                        khach.Phone = edtDienThoai.Text.ToString();
                                    }
                                    db.Khach.Add(khach);
                                    db.SaveChanges();
                                    var idkh = db.Khach.OrderByDescending(m => m.Id).First();
                                    dk_khach.IdDK         = id.Id;
                                    dk_khach.IdKH         = idkhach;
                                    dk_khach.Hoten        = dView.GetRowCellValue(i, dView.Columns[1]).ToString();
                                    dk_khach.CMND         = dView.GetRowCellValue(i, dView.Columns[0]).ToString();
                                    dk_khach.Gioitinh     = Convert.ToBoolean(dView.GetRowCellValue(i, dView.Columns[2]));
                                    dk_khach.Quoctich     = Convert.ToInt32(dView.GetRowCellValue(i, dView.Columns[3]));
                                    dk_khach.Sophong      = Convert.ToInt32(gvDangky.GetRowCellValue(j, gvDangky.Columns[0]));
                                    dk_khach.DateCheckin  = Convert.ToDateTime(dateCheckin.EditValue.ToString());
                                    dk_khach.DateCheckout = Convert.ToDateTime(dateCheckout.EditValue.ToString());
                                    dk_khach.GioCheckin   = DateTime.Now.ToShortTimeString();
                                    dk_khach.GioCheckout  = "12:00";
                                    db.DK_Customers.Add(dk_khach);
                                    db.SaveChanges();
                                    dk_phong.IDDK         = id.Id;
                                    dk_phong.SoPhong      = Convert.ToInt32(gvDangky.GetRowCellValue(j, gvDangky.Columns[0]));
                                    dk_phong.NgayCheckIn  = Convert.ToDateTime(dateCheckin.EditValue.ToString());
                                    dk_phong.NgayCheckOut = Convert.ToDateTime(dateCheckout.EditValue.ToString());
                                    dk_phong.LoaiTien     = id.Loaitien;
                                    dk_phong.TyGia        = id.Tygia;
                                    dk_phong.GhiChu       = id.Ghichu;
                                    dk_phong.DaCheckIn    = id.DaCheckin;
                                    dk_phong.DonGia       = Convert.ToInt32(gvDangky.GetRowCellValue(j, gvDangky.Columns[1]));
                                    db.DangKyPhong.Add(dk_phong);
                                }
                            }
                        }
                        txtSo.Text = id.ToString();
                        var update = db.Dangky.FirstOrDefault(m => m.Id == id.Id);
                        update.Phong = phong.ToString();
                        db.SaveChanges();
                        t = 0;
                    }
                    else
                    {
                        var id = Convert.ToInt32(txtSo.Text);
                        var dk = db.Dangky.FirstOrDefault(m => m.Id == id);
                        dk.NgayDK       = Convert.ToDateTime(dateNgay.EditValue.ToString());
                        dk.NgayCheckin  = Convert.ToDateTime(dateCheckin.EditValue.ToString());
                        dk.NgayCheckout = Convert.ToDateTime(dateCheckout.EditValue.ToString());
                        dk.SotienUT     = Convert.ToDouble(tbDattruoc.Text.ToString());
                        dk.Sokhach      = Convert.ToInt32(gvDangky.Columns[2].SummaryItem.SummaryValue);
                        dk.Sophong      = Convert.ToInt32(gvDangky.Columns[3].SummaryItem.SummaryValue);
                        dk.DaCheckin    = false;
                        dk.Ghichu       = tbNoidung.Text.ToString();
                        dk.HinhthucTT   = cbHinhthuc.EditValue.ToString();
                        dk.Loaitien     = cbxLoai.EditValue.ToString();
                        if (dk.HinhthucTT == "Tiền mặt")
                        {
                            dk.SoATM = "";
                        }
                        else
                        {
                            dk.SoATM = tbSoTK.EditValue.ToString();
                        }
                        dk.Tygia  = Convert.ToInt32(tbTygia.Text.ToString());
                        dk.MaBank = glueNganhang.EditValue.ToString();
                        dk.NgayUT = Convert.ToDateTime(dtDattruoc.EditValue.ToString());
                        phong     = dk.Phong.ToString();
                        db.SaveChanges();
                        var dkphong = db.DangKyPhong.Where(m => m.IDDK == id).Count();
                        if (gvDangky.RowCount > dkphong)
                        {
                            for (int j = dkphong; j < gvDangky.RowCount; j++)
                            {
                                GridView dView = gvDangky.GetDetailView(j, 0) as GridView;
                                phong.Trim();
                                phong += gvDangky.GetRowCellValue(j, gvDangky.Columns[0]).ToString();
                                if (j < gvDangky.RowCount - 1)
                                {
                                    phong += ",";
                                }
                                DK_Customer dk_khach = new DK_Customer();
                                Khach       khach    = new Khach();
                                DangKyPhong dk_phong = new DangKyPhong();
                                for (int i = 0; i < dView.RowCount; i++)
                                {
                                    if (dView.GetRowCellValue(i, dView.Columns[0]) != null && dView.GetRowCellValue(i, dView.Columns[1]) != null)
                                    {
                                        khach.HoTen    = dView.GetRowCellValue(i, dView.Columns[1]).ToString();
                                        khach.SoCMND   = dView.GetRowCellValue(i, dView.Columns[0]).ToString();
                                        khach.GioiTnh  = Convert.ToBoolean(dView.GetRowCellValue(i, dView.Columns[2]));
                                        khach.MaQT     = Convert.ToInt32(dView.GetRowCellValue(i, dView.Columns[3]));
                                        khach.NgaySinh = DateTime.Now;
                                        khach.DonVi    = "Khách hàng";
                                        if (edtDienThoai.Text == "Nhập số điện thoại")
                                        {
                                            khach.Phone = "";
                                        }
                                        else
                                        {
                                            khach.Phone = edtDienThoai.Text.ToString();
                                        }
                                        db.Khach.Add(khach);
                                        db.SaveChanges();
                                        dk_khach.IdDK         = id;
                                        dk_khach.IdKH         = idkhach;
                                        dk_khach.Hoten        = dView.GetRowCellValue(i, dView.Columns[1]).ToString();
                                        dk_khach.CMND         = dView.GetRowCellValue(i, dView.Columns[0]).ToString();
                                        dk_khach.Gioitinh     = Convert.ToBoolean(dView.GetRowCellValue(i, dView.Columns[2]));
                                        dk_khach.Quoctich     = Convert.ToInt32(dView.GetRowCellValue(i, dView.Columns[3]));
                                        dk_khach.Sophong      = Convert.ToInt32(gvDangky.GetRowCellValue(j, gvDangky.Columns[0]));
                                        dk_khach.DateCheckin  = Convert.ToDateTime(dateCheckin.EditValue.ToString());
                                        dk_khach.DateCheckout = Convert.ToDateTime(dateCheckout.EditValue.ToString());
                                        dk_khach.GioCheckin   = DateTime.Now.ToShortTimeString();
                                        dk_khach.GioCheckout  = "12:00";
                                        db.DK_Customers.Add(dk_khach);
                                        db.SaveChanges();
                                        dk_phong.IDDK         = id;
                                        dk_phong.SoPhong      = Convert.ToInt32(gvDangky.GetRowCellValue(j, gvDangky.Columns[0]));
                                        dk_phong.NgayCheckIn  = Convert.ToDateTime(dateCheckin.EditValue.ToString());
                                        dk_phong.NgayCheckOut = Convert.ToDateTime(dateCheckout.EditValue.ToString());
                                        dk_phong.LoaiTien     = cbxLoai.EditValue.ToString();
                                        dk_phong.TyGia        = Convert.ToInt32(tbTygia.Text.ToString());
                                        dk_phong.GhiChu       = tbNoidung.Text.ToString();
                                        dk_phong.DaCheckIn    = false;
                                        dk_phong.DonGia       = Convert.ToInt32(gvDangky.GetRowCellValue(j, gvDangky.Columns[1]));
                                        db.DangKyPhong.Add(dk_phong);
                                    }
                                }
                            }
                        }
                        else
                        {
                            for (int j = 0; j < gvDangky.RowCount; j++)
                            {
                                var         sophong  = Convert.ToInt32(gvDangky.GetRowCellValue(j, gvDangky.Columns[0]));
                                var         dkkhach  = db.DK_Customers.Where(m => m.IdDK == id && m.Sophong == sophong).Count();
                                GridView    dView    = gvDangky.GetDetailView(j, 0) as GridView;
                                DK_Customer dk_khach = new DK_Customer();
                                Khach       khach    = new Khach();
                                DangKyPhong dk_phong = new DangKyPhong();
                                int         dem      = 0;
                                for (int i = 0; i < dView.RowCount; i++)
                                {
                                    if (dView.GetRowCellValue(i, dView.Columns[0]) != null && dView.GetRowCellValue(i, dView.Columns[1]) != null)
                                    {
                                        dem++;
                                    }
                                }
                                if (dem > dkkhach)
                                {
                                    for (int i = dkkhach; i < dView.RowCount; i++)
                                    {
                                        if (dView.GetRowCellValue(i, dView.Columns[0]) != null && dView.GetRowCellValue(i, dView.Columns[1]) != null)
                                        {
                                            khach.HoTen    = dView.GetRowCellValue(i, dView.Columns[1]).ToString();
                                            khach.SoCMND   = dView.GetRowCellValue(i, dView.Columns[0]).ToString();
                                            khach.GioiTnh  = Convert.ToBoolean(dView.GetRowCellValue(i, dView.Columns[2]));
                                            khach.MaQT     = Convert.ToInt32(dView.GetRowCellValue(i, dView.Columns[3]));
                                            khach.NgaySinh = DateTime.Now;
                                            khach.DonVi    = "Khách hàng";
                                            if (edtDienThoai.Text == "Nhập số điện thoại")
                                            {
                                                khach.Phone = "";
                                            }
                                            else
                                            {
                                                khach.Phone = edtDienThoai.Text.ToString();
                                            }
                                            db.Khach.Add(khach);
                                            db.SaveChanges();
                                            var idkh = db.Khach.OrderByDescending(m => m.Id == idkhach).First();
                                            dk_khach.IdDK         = id;
                                            dk_khach.IdKH         = idkhach;
                                            dk_khach.Hoten        = dView.GetRowCellValue(i, dView.Columns[1]).ToString();
                                            dk_khach.CMND         = dView.GetRowCellValue(i, dView.Columns[0]).ToString();
                                            dk_khach.Gioitinh     = Convert.ToBoolean(dView.GetRowCellValue(i, dView.Columns[2]));
                                            dk_khach.Quoctich     = Convert.ToInt32(dView.GetRowCellValue(i, dView.Columns[3]));
                                            dk_khach.Sophong      = Convert.ToInt32(gvDangky.GetRowCellValue(i, gvDangky.Columns[0]));
                                            dk_khach.DateCheckin  = Convert.ToDateTime(dateCheckin.EditValue.ToString());
                                            dk_khach.DateCheckout = Convert.ToDateTime(dateCheckout.EditValue.ToString());
                                            dk_khach.GioCheckin   = DateTime.Now.ToShortTimeString();
                                            dk_khach.Sophong      = Convert.ToInt32(dView.GetRowCellValue(i, dView.Columns[4]));
                                            dk_khach.GioCheckout  = "12:00";
                                            db.DK_Customers.Add(dk_khach);
                                            db.SaveChanges();
                                        }
                                    }
                                }
                                else if (dem <= dkkhach)
                                {
                                    for (int i = 0; i < dkkhach; i++)
                                    {
                                        if (dView.GetRowCellValue(i, dView.Columns[0]) != null && dView.GetRowCellValue(i, dView.Columns[1]) != null)
                                        {
                                            var iddk      = Convert.ToInt32(dView.GetRowCellValue(i, dView.Columns[5]).ToString());
                                            var editkhach = db.Khach.FirstOrDefault(m => m.Id == idkhach);
                                            if (edtDienThoai.Text == "Nhập số điện thoại")
                                            {
                                                editkhach.Phone = "";
                                            }
                                            else
                                            {
                                                editkhach.Phone = edtDienThoai.Text.ToString();
                                            }
                                            db.SaveChanges();
                                            DK_Customer edit_dkkhach = (from dkcus in db.DK_Customers
                                                                        where dkcus.Id == iddk
                                                                        select dkcus).DefaultIfEmpty().Single();
                                            DK_Customer dkcustomer = new DK_Customer();
                                            dkcustomer.IdKH           = idkhach;
                                            dkcustomer.Hoten          = dView.GetRowCellValue(i, dView.Columns[1]).ToString();
                                            dkcustomer.CMND           = dView.GetRowCellValue(i, dView.Columns[0]).ToString();
                                            dkcustomer.Gioitinh       = Convert.ToBoolean(dView.GetRowCellValue(i, dView.Columns[2]));
                                            dkcustomer.Quoctich       = Convert.ToInt32(dView.GetRowCellValue(i, dView.Columns[3]).ToString());
                                            dkcustomer.Sophong        = Convert.ToInt32(gvDangky.GetRowCellValue(i, gvDangky.Columns[0]));
                                            dkcustomer.DateCheckin    = Convert.ToDateTime(dateCheckin.EditValue.ToString());
                                            dkcustomer.DateCheckout   = Convert.ToDateTime(dateCheckout.EditValue.ToString());
                                            dkcustomer.GioCheckin     = DateTime.Now.ToShortTimeString();
                                            dkcustomer.GioCheckout    = "12:00";
                                            edit_dkkhach.CMND         = dkcustomer.CMND;
                                            edit_dkkhach.Hoten        = dkcustomer.Hoten;
                                            edit_dkkhach.Gioitinh     = dkcustomer.Gioitinh;
                                            edit_dkkhach.Quoctich     = dkcustomer.Quoctich;
                                            edit_dkkhach.DateCheckin  = dkcustomer.DateCheckin;
                                            edit_dkkhach.DateCheckout = dkcustomer.DateCheckout;
                                            edit_dkkhach.IdKH         = dkcustomer.IdKH;
                                            db.SaveChanges();
                                        }
                                    }
                                }
                            }
                        }
                        var update = db.Dangky.FirstOrDefault(m => m.Id == id);
                        update.Phong = phong.ToString();
                        db.SaveChanges();
                        t = 0;
                    }
                }
                if (tam == 2)
                {
                    if (txtSo.Text != null)
                    {
                        var id = Convert.ToInt32(txtSo.Text);
                        if (gcDangky.DataSource == null)
                        {
                            var delid = db.Dangky.FirstOrDefault(m => m.Id == id);
                            db.Dangky.Remove(delid);
                            db.SaveChanges();
                            var deldk = db.DK_Customers.FirstOrDefault(m => m.IdDK == id);
                            db.DK_Customers.Remove(deldk);
                            var deldkp = db.DangKyPhong.Where(m => m.IDDK == id).ToList();
                            foreach (var item in deldkp)
                            {
                                db.DangKyPhong.Remove(item);
                            }
                            var listdkp = db.DangKyPhong.Where(m => m.IDDK == id).ToList();
                            foreach (var item in listdkp)
                            {
                                var room = db.Rooms.Where(m => m.Sophong == item.SoPhong).First();
                                room.Status = 1;
                            }
                            db.SaveChanges();
                        }
                        else
                        {
                            var dkp = db.DangKyPhong.Where(m => m.IDDK == id).OrderBy(m => m.SoPhong).ToList();
                            if (lstdkp.Count != 0)
                            {
                                foreach (var item in lstdkp)
                                {
                                    db.DangKyPhong.Remove(item);
                                    var deldkk = db.DK_Customers.Where(m => m.Sophong == item.SoPhong).First();
                                    db.DK_Customers.Remove(deldkk);
                                    var room = db.Rooms.Where(m => m.Sophong == item.SoPhong).First();
                                    room.Status = 1;
                                }
                                for (int i = 0; i < gvDangky.RowCount; i++)
                                {
                                    phong.Trim();
                                    phong += gvDangky.GetRowCellValue(i, gvDangky.Columns[0]).ToString();
                                    if (i < gvDangky.RowCount - 1)
                                    {
                                        phong += ",";
                                    }
                                }
                                var update = db.Dangky.FirstOrDefault(m => m.Id == id);
                                update.Phong = phong.ToString();
                                db.SaveChanges();
                            }
                            else
                            {
                                foreach (var item in lstdkk)
                                {
                                    db.DK_Customers.Remove(item);
                                }
                                db.SaveChanges();
                            }
                        }
                    }
                }
                MessageBox.Show("Lưu thành công!");
            }
        }