protected void btbLuu_Click(object sender, EventArgs e)
        {
            DTOThanhToan dtocn = new DTOThanhToan();
            dtocn.MaDH = txtMaDH.Text;
            dtocn.MaNV = int.Parse(txtNhanVien.Text);
            dtocn.NgayThanhToan = Convert.ToDateTime(txtNgayThanhToan.Text);
            dtocn.Sotien = int.Parse(txtDatCoc.Text);
            dtocn.HanTT = Convert.ToDateTime(txtHanTT.Text);



            DAO_Entity daokh = new DAO_Entity();

            XuLy_TiepNhanDonHang control = new XuLy_TiepNhanDonHang();
            control.themdatcoc(dtocn);

            DTODonDatHang dtodh = new DTODonDatHang();
            dtodh.MaDH = txtMaDH.Text;
            int tong = int.Parse(txtTong.Text);
            int no = int.Parse(txtDatCoc.Text);
            dtodh.CongNoDH = tong - no;
            daokh.CapNhatCongNo(dtodh);


            Response.Redirect("~/QLBoPhan_TiepXucKH/TaoDonHang.aspx?MaKH=" + int.Parse(txtMaKH.Text));
        }
        protected void btnLuu_Click(object sender, EventArgs e)
        {
            DTOCongNoBanHang dto = new DTOCongNoBanHang();
            dto.MaDH = lbMaDH.Text;
            dto.MaKH = int.Parse(lbMaKH.Text);
            dto.MaNV = int.Parse(txtMaNV.Text);
            dto.NgayTraNo = Convert.ToDateTime(lbNgayTra.Text);
            dto.TienTT = int.Parse(txtTienTT.Text);

            DAO_Entity dao = new DAO_Entity();
            dao.ThemCongNoBH(dto);

            DTODonDatHang dtodh = new DTODonDatHang();
            dtodh.MaDH = lbMaDH.Text;
            int tong = int.Parse(lbTienTra.Text);
            int no = int.Parse(txtTienTT.Text);
            dtodh.CongNoDH = tong - no;
            dao.CapNhatCongNo(dtodh);

            if (dtodh.CongNoDH == 0)
            {
                dao.CapNhatTrangThaisauThanhToan(dtodh);
            }

            Response.Redirect("~/QLBoPhan_ThanhPham/QuanLyQuanNo_BH/DanhSachCongNo_BH.aspx");
        }
 public DTOXLSC_HDBH(DataRow dongDL)
 {
     STT       = (int)dongDL["STT"];
     MaDH      = dongDL["MaDH"].ToString();
     MaSP      = (int)dongDL["MaSP"];
     TenSP     = dongDL["TenSP"].ToString();
     Gia       = (int)dongDL["Gia"];
     dtosp     = new DTOSanPham(dongDL);
     dtoDH     = new DTODonDatHang(dongDL);
     Soluong   = (int)dongDL["Soluong"];
     ThanhTien = (int)dongDL["ThanhTien"];
 }
        public DTOChiTietDonHang(DataRow dongDL)
        {
            //MaDH = (int)dongDL["MaDH"];
            //    MaSP = (int)dongDL["MaSP"];

            // TenSP = dongDL["TenSP"].ToString();
            //     Gia = (int)dongDL["Gia"];
            dtoDH     = new DTODonDatHang(dongDL);
            dtoSP     = new DTOSanPham(dongDL);
            SoLuong   = (int)dongDL["SoLuong"];
            ThanhTien = (int)dongDL["ThanhTien"];
        }
  public DTOChiTietDonHang(DataRow dongDL)
  {
      //MaDH = (int)dongDL["MaDH"];
  //    MaSP = (int)dongDL["MaSP"];
               
     // TenSP = dongDL["TenSP"].ToString();
 //     Gia = (int)dongDL["Gia"];
      dtoDH = new DTODonDatHang(dongDL);
      dtoSP = new DTOSanPham(dongDL);
      SoLuong = (int)dongDL["SoLuong"];
      ThanhTien = (int)dongDL["ThanhTien"];
      
  }
        public DTOXLSC_HDBH(DataRow dongDL)
        {
            STT = (int)dongDL["STT"];
            MaDH = dongDL["MaDH"].ToString();
            MaSP = (int)dongDL["MaSP"];
            TenSP = dongDL["TenSP"].ToString();
            Gia = (int)dongDL["Gia"];
            dtosp = new DTOSanPham(dongDL);
            dtoDH = new DTODonDatHang(dongDL);
            Soluong = (int)dongDL["Soluong"];
            ThanhTien = (int)dongDL["ThanhTien"];

        }
        public DTOXuLyPhieuGiao(DataRow dongDL)
        {
            STT  = (int)dongDL["STT"];
            MaDH = dongDL["MaDH"].ToString();
            // MaKH = (int)dongDL["MaKH"];
            MaSP  = (int)dongDL["MaSP"];
            TenSP = dongDL["TenSP"].ToString();
            Gia   = (int)dongDL["Gia"];

            dtoDH     = new DTODonDatHang(dongDL);
            dtoSP     = new DTOSanPham(dongDL);
            SoLuong   = (int)dongDL["SoLuong"];
            ThanhTien = (int)dongDL["ThanhTien"];
        }
        protected void btLuuCt_Click(object sender, EventArgs e)
        {
            int ma = int.Parse(lbMadh.Text);
            DAO_Entity daokh = new DAO_Entity();
            DTODonDatHang dtodh = new DTODonDatHang();
            DTOChiTietDonHang dtoct = daokh.LayTongTien(ma);
            
            dtodh.TongTien = int.Parse(dtoct.ThanhTien.ToString());
            dtodh.CongNoDH = dtodh.TongTien;
            daokh.CapNhatTongTien(dtodh);
            
            Response.Redirect("~/QLBoPhan_TiepXucKH/TaoDonHang.aspx?MaKH="+lbMaKH.Text);

        }
        public DTOXuLyPhieuGiao(DataRow dongDL)
        {
            STT = (int)dongDL["STT"];
            MaDH = dongDL["MaDH"].ToString();
            // MaKH = (int)dongDL["MaKH"];
            MaSP = (int)dongDL["MaSP"];
            TenSP = dongDL["TenSP"].ToString();
            Gia = (int)dongDL["Gia"];

            dtoDH = new DTODonDatHang(dongDL);
            dtoSP = new DTOSanPham(dongDL);
            SoLuong = (int)dongDL["SoLuong"];
            ThanhTien = (int)dongDL["ThanhTien"];

        }
 public void themdh(DTODonDatHang dtodh)
 {
     DAO_Entity dao = new DAO_Entity();
     dao.ThemDonHang(dtodh);
 }
        //DAOThemDonHang
        public bool ThemDonHang(DTODonDatHang dtodh)
        {
            bool ketqua = false;
            ketnoi.TaoKetNoi();
            try
            {
                string lenh = "insert into DonDH(maDH, MaKH, NgayTaoDH, NgayGH, TongTien, CongNoDH, TrangThai) values (@madh, @makh, @ngaytao, @ngaygiao, @tongtien, @congno, @trangthai)";
                ketnoi.LenhKetNoi = new SqlCommand(lenh, ketnoi.KetNoi);

                ketnoi.LenhKetNoi.Parameters.Add("@madh", SqlDbType.VarChar);
                ketnoi.LenhKetNoi.Parameters.Add("@makh", SqlDbType.Int);
                ketnoi.LenhKetNoi.Parameters.Add("@ngaytao", SqlDbType.DateTime);
                ketnoi.LenhKetNoi.Parameters.Add("@ngaygiao", SqlDbType.DateTime);
                ketnoi.LenhKetNoi.Parameters.Add("@tongtien", SqlDbType.Int);

                ketnoi.LenhKetNoi.Parameters.Add("@congno", SqlDbType.Int);
                ketnoi.LenhKetNoi.Parameters.Add("@trangthai", SqlDbType.NVarChar);

                ketnoi.LenhKetNoi.Parameters["@madh"].Value = dtodh.MaDH;
                ketnoi.LenhKetNoi.Parameters["@makh"].Value = dtodh.MaKH;
                ketnoi.LenhKetNoi.Parameters["@ngaytao"].Value = dtodh.NgayTaoDH;
                ketnoi.LenhKetNoi.Parameters["@ngaygiao"].Value = dtodh.NgayGH;
                ketnoi.LenhKetNoi.Parameters["@tongtien"].Value = dtodh.TongTien;

                ketnoi.LenhKetNoi.Parameters["@congno"].Value = dtodh.CongNoDH;
                ketnoi.LenhKetNoi.Parameters["@trangthai"].Value = dtodh.TrangThai;

                if (ketnoi.LenhKetNoi.ExecuteNonQuery() != 0)
                    ketqua = true;
            }
            catch (Exception)
            {


            }
            finally
            {
                ketnoi.DongKetNoi();
            }
            return ketqua;
        }
        public bool CapNhatTrangThaisauThanhToan(DTODonDatHang dtoDH)
        {
            bool ketqua = false;
            ketnoi.TaoKetNoi();
            try
            {
                string lenh = "update DonDH set TrangThai=N'Đóng' where MaDH=@ma";
                ketnoi.LenhKetNoi = new SqlCommand(lenh, ketnoi.KetNoi);

                ketnoi.LenhKetNoi.Parameters.Add("@ma", SqlDbType.VarChar);

                ketnoi.LenhKetNoi.Parameters["@ma"].Value = dtoDH.MaDH;

                if (ketnoi.LenhKetNoi.ExecuteNonQuery() != 0)
                    ketqua = true;
            }
            catch (Exception)
            {

            }
            finally
            {
                ketnoi.DongKetNoi();
            }
            return ketqua;
        }
        //DAOThanhToanBH
        public bool CapNhatTongTien(DTODonDatHang dtoDH)
        {
            bool ketqua = false;
            ketnoi.TaoKetNoi();
            try
            {
                string lenh = "update DonDH set TongTien = @tt, CongNoDH = @cc where MaDH=@ma";
                ketnoi.LenhKetNoi = new SqlCommand(lenh, ketnoi.KetNoi);

                ketnoi.LenhKetNoi.Parameters.Add("@ma", SqlDbType.Int);
                ketnoi.LenhKetNoi.Parameters.Add("@tt", SqlDbType.Int);
                ketnoi.LenhKetNoi.Parameters.Add("@cc", SqlDbType.Int);

                ketnoi.LenhKetNoi.Parameters["@ma"].Value = dtoDH.MaDH;
                ketnoi.LenhKetNoi.Parameters["@tt"].Value = dtoDH.TongTien;
                ketnoi.LenhKetNoi.Parameters["@cc"].Value = dtoDH.CongNoDH;

                if (ketnoi.LenhKetNoi.ExecuteNonQuery() != 0)
                    ketqua = true;
            }
            catch (Exception)
            {

            }
            finally
            {
                ketnoi.DongKetNoi();
            }
            return ketqua;
        }
        public DTODonDatHang LayDonHangTraHang(string ma)
        {
            ketnoi.TaoKetNoi();
            DTODonDatHang dtoDH = null;
            try
            {
                string lenh = "Select * from KhachHang kh, DonDH dh, LoaiKH l where kh.MaKH=dh.MaKH and l.MaLKH=kh.MaLKH and dh.MaDH=@ma";
                ketnoi.LenhKetNoi = new SqlCommand(lenh, ketnoi.KetNoi);

                ketnoi.LenhKetNoi.Parameters.Add("@ma", SqlDbType.VarChar);
                ketnoi.LenhKetNoi.Parameters["@ma"].Value = ma;

                DataTable bang = new DataTable();
                ketnoi.TichHopCSDL = new SqlDataAdapter(ketnoi.LenhKetNoi);
                ketnoi.TichHopCSDL.Fill(bang);
                DataRow dong = bang.Rows[0];
                dtoDH = new DTODonDatHang(dong);
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                ketnoi.DongKetNoi();
            }
            return dtoDH;
        }
        public List<DTODonDatHang> TimTheoma(int ma)
        {
            List<DTODonDatHang> dsTim = new List<DTODonDatHang>();
            string lenh = "Select * from KhachHang kh, DonDH dh where kh.MaKH=dh.MaKH and kh.MaKH = @maKH";
            ketnoi.TaoKetNoi();
            DataTable bang = new DataTable();
            try
            {
                ketnoi.LenhKetNoi = new SqlCommand(lenh, ketnoi.KetNoi);
                ketnoi.LenhKetNoi.Parameters.Add("@maKH", SqlDbType.Int);
                ketnoi.LenhKetNoi.Parameters["@maKH"].Value = ma;

                ketnoi.TichHopCSDL = new SqlDataAdapter(ketnoi.LenhKetNoi);
                ketnoi.TichHopCSDL.Fill(bang);
                foreach (DataRow dongDL in bang.Rows)
                {
                    DTODonDatHang dtoKH = new DTODonDatHang(dongDL);
                    dsTim.Add(dtoKH);
                }
            }
            catch (Exception)
            {

            }
            finally
            {
                ketnoi.DongKetNoi();
            }
            return dsTim;
        }
        //lay danh sach don hang chua xu ly
        public List<DTODonDatHang> LayDSDonHangDangXuLy()
        {
            List<DTODonDatHang> dsDonHang = new List<DTODonDatHang>();
            string lenh = "Select * from KhachHang kh, DonDH dh, LoaiKH lkh where dh.TrangThai=N'Đang Xử Lý' and kh.MaKH=dh.MaKH and lkh.MaLKH=kh.MaLKH";
            ketnoi.TaoKetNoi();
            DataTable bang = new DataTable();
            try
            {
                ketnoi.LenhKetNoi = new SqlCommand(lenh, ketnoi.KetNoi);

                ketnoi.TichHopCSDL = new SqlDataAdapter(ketnoi.LenhKetNoi);
                ketnoi.TichHopCSDL.Fill(bang);
                foreach (DataRow dongDL in bang.Rows)
                {
                    DTODonDatHang dtoKH = new DTODonDatHang(dongDL);
                    dsDonHang.Add(dtoKH);
                }
            }
            catch (Exception)
            {

            }
            finally
            {
                ketnoi.DongKetNoi();
            }
            return dsDonHang;
        }
        protected void btnLuu_Click(object sender, EventArgs e)
        {
            dsThanhPhamChon = (List<DTOChiTietDH>)Session["dsThanhPhamChon"];
            try
            {
                string maDH = DateTime.Now.ToString("yyyyMMddHHmmss");
                DTODonDatHang dtoDonHang = new DTODonDatHang();
                int tongtien = 0;
                foreach (DTOChiTietDH row in dsThanhPhamChon)
                {
                    tongtien += row.ThanhTien;
                }
                dtoDonHang.MaDH = maDH;
                dtoDonHang.MaKH = int.Parse(lbMaKH.Text);
                dtoDonHang.NgayTaoDH = Convert.ToDateTime(txtNgayTaoDH.Text);
                dtoDonHang.NgayGH = Convert.ToDateTime(txtGiao.Text);

                dtoDonHang.TongTien = 0;
                dtoDonHang.CongNoDH = 0;
                dtoDonHang.TongTien = tongtien;
                dtoDonHang.CongNoDH = tongtien;
                dtoDonHang.TrangThai = "Chưa Xử Lý";

                //XuLy_TiepNhanDonHang control = new XuLy_TiepNhanDonHang();
                //control.themdh(dtoDonHang);

                if (daoEntities.ThemDonHang(dtoDonHang))
                {
                    foreach(DTOChiTietDH dtoCTDH in dsThanhPhamChon)
                    {
                        dtoCTDH.MaDH = maDH;
       
                        //control.themct(dtoCTDH);

                        if (daoEntities.ThemChiTietDH(dtoCTDH))
                        {
                            Session["dsThanhPhamChon"] = null;
                        }
                    }
                }
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public List<DTODonDatHang> LayDSDHTheoMa(string ma)
        {
            List<DTODonDatHang> ds = new List<DTODonDatHang>();
            string lenh = "Select * from KhachHang kh, DonDH dh, LoaiKH l where kh.MaKH=" + ma + " and kh.MaKH=dh.MaKH and l.MaLKH=kh.MaLKH";
            //ketnoi.LenhKetNoi.Parameters.Add("@ma", SqlDbType.Int);
            //ketnoi.LenhKetNoi.Parameters["@ma"].Value = ma;
            //string lenh = "Select dh.MaDH,kh.MaKH,kh.TenKH,dh.TongTien,dh.CongNoDH from KhachHang kh, DonDH dh where kh.MaKH=" + ma + " and kh.MaKH=dh.MaKH";
            ketnoi.TaoKetNoi();
            DataTable bang = new DataTable();
            try
            {
                ketnoi.LenhKetNoi = new SqlCommand(lenh, ketnoi.KetNoi);
                //dong bo lenh ket noi voi du lieu trong database
                ketnoi.TichHopCSDL = new SqlDataAdapter(ketnoi.LenhKetNoi);
                ketnoi.TichHopCSDL.Fill(bang);
                foreach (DataRow dongDL in bang.Rows)
                {
                    DTODonDatHang dtoDH = new DTODonDatHang(dongDL);
                    ds.Add(dtoDH);
                }
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                ketnoi.DongKetNoi();
            }
            return ds;
        }