public DTOHoaDonBH(DataRow dongDL)
 {
     MaHDBH = dongDL["MaHDBH"].ToString();
     MaKH = (int)dongDL["MaKH"];
     NgayHDBH = (DateTime)dongDL["NgayHDBH"];
     TongTien = (int)dongDL["TongTien"];
     dtoxl = new DTOXLSC_HDBH(dongDL);
 }
 public DTOHoaDonBH(DataRow dongDL)
 {
     MaHDBH   = dongDL["MaHDBH"].ToString();
     MaKH     = (int)dongDL["MaKH"];
     NgayHDBH = (DateTime)dongDL["NgayHDBH"];
     TongTien = (int)dongDL["TongTien"];
     dtoxl    = new DTOXLSC_HDBH(dongDL);
 }
        protected void btnLuu_Click(object sender, EventArgs e)
        {
            if (Session["dsThanhPhamTra"] != null)
            {
                dsThanhPhamTra = (List<DTOChiTietDH>)Session["dsThanhPhamTra"];
                int dem = 0;
                string maDH = lbMaDH.Text;
                foreach (DTOChiTietDH dtoCTDH in dsThanhPhamTra)
                {
                    DTOXLSC_HDBH dtoct = new DTOXLSC_HDBH();
                    dtoct.MaDH = lbMaDH.Text;
                    dtoct.MaSP = dtoCTDH.dtoSP.MaSP;
                    dtoct.Soluong = dtoCTDH.SoLuong;
                    dtoct.ThanhTien = dtoCTDH.ThanhTien;
                    if (daoEntities.ThemXuLyTraHang(dtoct))
                    {
                        DTOChiTietDH dtoChiTietDHTra = new DTOChiTietDH();
                        DTOChiTietDH dtoChiTietDonHangDat = daoEntities.LayChiTietDonHangTheoMaDHMaSP(maDH, dtoCTDH.dtoSP.MaSP);
                        dtoChiTietDHTra.MaDH = lbMaDH.Text;
                        dtoChiTietDHTra.MaSP = dtoCTDH.dtoSP.MaSP;
                        dtoChiTietDHTra.SoLuong = dtoChiTietDonHangDat.SoLuong - dtoCTDH.SoLuong;
                        dtoChiTietDHTra.ThanhTien = dtoChiTietDonHangDat.ThanhTien - dtoCTDH.ThanhTien;

                        if (daoEntities.CapNhatChiTietDH(dtoChiTietDHTra))
                            dem++;
                    }
                }

                if(dem == dsThanhPhamTra.Count)
                {
                    DTODonDatHang dtoDonHang = daoEntities.LayDHTheoMa(maDH);
                    int tongTienTra = int.Parse(lbTongTien.Text),
                        tongTienDaDat = dtoDonHang.TongTien,
                        congnoCu = dtoDonHang.CongNoDH,
                        tongTienPhaiTra = 0, congnoConLai = 0;
                    tongTienPhaiTra = tongTienDaDat - tongTienTra;
                    congnoConLai = congnoCu - tongTienTra;
                    daoEntities.CapNhatDonHang(maDH, tongTienPhaiTra, congnoConLai);
                }

                Response.Redirect(Request.RawUrl);
            }
        }
        public bool ThemXuLyTraHang(DTOXLSC_HDBH dtoXL)
        {
            bool ketqua = false;
            ketnoi.TaoKetNoi();
            try
            {
                string lenh = "insert into XLSC_HDBH(MaDH, MaSP, Soluong, ThanhTien) values (@madh, @masp, @sl, @tt)";
                ketnoi.LenhKetNoi = new SqlCommand(lenh, ketnoi.KetNoi);

                ketnoi.LenhKetNoi.Parameters.Add("@madh", SqlDbType.VarChar);
                ketnoi.LenhKetNoi.Parameters.Add("@masp", SqlDbType.Int);
                ketnoi.LenhKetNoi.Parameters.Add("@sl", SqlDbType.Int);
                ketnoi.LenhKetNoi.Parameters.Add("@tt", SqlDbType.Int);

                ketnoi.LenhKetNoi.Parameters["@madh"].Value = dtoXL.MaDH;
                ketnoi.LenhKetNoi.Parameters["@masp"].Value = dtoXL.MaSP;
                ketnoi.LenhKetNoi.Parameters["@sl"].Value = dtoXL.Soluong;
                ketnoi.LenhKetNoi.Parameters["@tt"].Value = dtoXL.ThanhTien;


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

            }
            finally
            {
                ketnoi.DongKetNoi();
            }
            return ketqua;
        }
        public List<DTOXLSC_HDBH> LayDSXLSL_HDBH(string ma)
        {
            List<DTOXLSC_HDBH> ds = new List<DTOXLSC_HDBH>();

            string lenh = "Select * from XLSC_HDBH ct, DonDH dh, KhachHang kh, SanPham sp, LoaiSP lsp, LoaiKH l where dh.MaDH=" + ma + " and kh.MaLKH=l.MaLKH and kh.MaKH=dh.MaKH and ct.MaDH=dh.MaDH and ct.MaSP=sp.MaSP and sp.MaLoai = lsp.MaLoai";
            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)
                {
                    DTOXLSC_HDBH dtoct = new DTOXLSC_HDBH(dongDL);
                    ds.Add(dtoct);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                ketnoi.DongKetNoi();
            }
            return ds;
        }