private PHIEU_TIEP_NHAN LayTTPTN_DAT()
        {
            string   ma        = txtMa2.Text;
            int      idnv      = frmDANG_NHAP.idnv;
            string   idbn      = txtMaBN2.Text;
            string   idba      = "1";
            string   idbs      = cbBSK2.SelectedValue.ToString();
            int      idbs2     = int.Parse(idbs);
            int      stt       = int.Parse(txtSTT2.Text);
            string   dvk       = cbDVK.SelectedValue.ToString();
            int      iddvk     = int.Parse(dvk);
            int      tuoithang = int.Parse(txtTuoithang2.Text);
            int      tuoinam   = int.Parse(txtTuoinam2.Text);
            DateTime dt        = Convert.ToDateTime(DateNgayhen.Text);
            string   ngaygio   = dt.ToShortDateString();
            //string ngaygio = DateNgayhen.Text;
            float  mach     = 0;
            float  nhietdo  = 0;
            float  nhiptho  = 0;
            string huyetap  = "0";
            float  chieucao = 0;
            float  cannang  = 0;
            float  bmi      = 0;
            int    tt       = 0;

            PHIEU_TIEP_NHAN t = new PHIEU_TIEP_NHAN(ma, idnv, idbn, idba, iddvk, stt, idbs2, dvk, tuoithang, tuoinam, ngaygio, mach, nhietdo, nhiptho, huyetap, chieucao, cannang, bmi, tt);

            return(t);
        }
        public bool InsertPTN(PHIEU_TIEP_NHAN t)
        {
            try
            {
                SqlConnection conn = new SqlConnection(connecDB.connectionStr);
                SqlCommand    cmd  = new SqlCommand("themPHIEU_TIEP_NHAN", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                //cmd.Parameters.Add("@mathuoc", SqlDbType.NVarChar, 20);
                //cmd.Parameters["@mathuoc"].Value = mt;
                cmd.Parameters.Add("@TN_ID", SqlDbType.NVarChar, 15);
                cmd.Parameters.Add("@NV_ID", SqlDbType.Int);
                cmd.Parameters.Add("@BN_ID", SqlDbType.NVarChar, 10);
                cmd.Parameters.Add("@BA_ID", SqlDbType.NVarChar, 20);
                cmd.Parameters.Add("@DVK_ID", SqlDbType.Int);
                cmd.Parameters.Add("@TN_STT", SqlDbType.Int);
                cmd.Parameters.Add("@TN_BSKHAM", SqlDbType.Int);
                cmd.Parameters.Add("@TN_DVKHAM", SqlDbType.NVarChar, 30);
                cmd.Parameters.Add("@TN_TUOINAM", SqlDbType.Int);
                cmd.Parameters.Add("@TN_TUOITHANG", SqlDbType.Int);
                cmd.Parameters.Add("@TN_NGAYGIO", SqlDbType.Date);
                cmd.Parameters.Add("@TN_MACH", SqlDbType.Float);
                cmd.Parameters.Add("@TN_NHIETDO", SqlDbType.Float);
                cmd.Parameters.Add("@TN_NHIPTHO", SqlDbType.Float);
                cmd.Parameters.Add("@TN_HUYETAP", SqlDbType.NVarChar, 7);
                cmd.Parameters.Add("@TN_CHIEUCAO", SqlDbType.Float);
                cmd.Parameters.Add("@TN_CANNANG", SqlDbType.Float);
                cmd.Parameters.Add("@TN_BMI", SqlDbType.Float);
                cmd.Parameters.Add("@TN_TRANGTHAI", SqlDbType.Int);

                cmd.Parameters["@TN_ID"].Value        = t.Id_tn;
                cmd.Parameters["@NV_ID"].Value        = t.Id_nv;
                cmd.Parameters["@BN_ID"].Value        = t.Id_bn;
                cmd.Parameters["@BA_ID"].Value        = t.Id_ba;
                cmd.Parameters["@DVK_ID"].Value       = t.Id_ba;
                cmd.Parameters["@TN_STT"].Value       = t.Tn_stt;
                cmd.Parameters["@TN_BSKHAM"].Value    = t.Tn_bsk;
                cmd.Parameters["@TN_DVKHAM"].Value    = t.Tn_dvk;
                cmd.Parameters["@TN_TUOINAM"].Value   = t.Tn_tuoinam;
                cmd.Parameters["@TN_TUOITHANG"].Value = t.Tn_tuoithang;
                cmd.Parameters["@TN_NGAYGIO"].Value   = t.Tn_ngaygio;
                cmd.Parameters["@TN_MACH"].Value      = t.Tn_mach;
                cmd.Parameters["@TN_NHIETDO"].Value   = t.Tn_nhietdo;
                cmd.Parameters["@TN_NHIPTHO"].Value   = t.Tn_nhiptho;
                cmd.Parameters["@TN_HUYETAP"].Value   = t.Tn_huyetap;
                cmd.Parameters["@TN_CHIEUCAO"].Value  = t.Tn_chieucao;
                cmd.Parameters["@TN_CANNANG"].Value   = t.Tn_cannang;
                cmd.Parameters["@TN_BMI"].Value       = t.Tn_bmi;
                cmd.Parameters["@TN_TRANGTHAI"].Value = t.Tn_trangthai;
                conn.Open();
                int result = cmd.ExecuteNonQuery();
                conn.Close();
                return(result > 0);
            }
            catch (SqlException e)
            {
                MessageBox.Show(e.ToString());
                return(false);
            }
        }
        public bool UpdatePTN_DK(PHIEU_TIEP_NHAN t)
        {
            SqlConnection conn = new SqlConnection(connecDB.connectionStr);
            SqlCommand    cmd  = new SqlCommand("suaPHIEU_TIEP_NHAN_DK", conn);

            cmd.CommandType = CommandType.StoredProcedure;
            //cmd.Parameters.Add("@mathuoc", SqlDbType.NVarChar, 20);
            //cmd.Parameters["@mathuoc"].Value = mt;
            cmd.Parameters.Add("@TN_ID", SqlDbType.NVarChar, 10);
            cmd.Parameters["@TN_ID"].Value = t.Id_tn;
            conn.Open();
            int result = cmd.ExecuteNonQuery();

            conn.Close();
            return(result > 0);
        }
        public bool UpdatePTN(PHIEU_TIEP_NHAN t)
        {
            SqlConnection conn = new SqlConnection(connecDB.connectionStr);
            SqlCommand    cmd  = new SqlCommand("suaPHIEU_TIEP_NHAN", conn);

            cmd.CommandType = CommandType.StoredProcedure;
            //cmd.Parameters.Add("@mathuoc", SqlDbType.NVarChar, 20);
            //cmd.Parameters["@mathuoc"].Value = mt;
            cmd.Parameters.Add("@TN_ID", SqlDbType.NVarChar, 10);
            cmd.Parameters.Add("@TN_STT", SqlDbType.Int);
            cmd.Parameters.Add("@TN_BSKHAM", SqlDbType.Int);
            cmd.Parameters.Add("@TN_DVKHAM", SqlDbType.NVarChar, 30);
            cmd.Parameters.Add("@TN_TUOINAM", SqlDbType.Int);
            cmd.Parameters.Add("@TN_TUOITHANG", SqlDbType.Int);
            cmd.Parameters.Add("@TN_NGAYGIO", SqlDbType.Date);
            cmd.Parameters.Add("@TN_MACH", SqlDbType.Float);
            cmd.Parameters.Add("@TN_NHIETDO", SqlDbType.Float);
            cmd.Parameters.Add("@TN_NHIPTHO", SqlDbType.Float);
            cmd.Parameters.Add("@TN_HUYETAP", SqlDbType.Float);
            cmd.Parameters.Add("@TN_CHIEUCAO", SqlDbType.Float);
            cmd.Parameters.Add("@TN_CANNANG", SqlDbType.Float);
            cmd.Parameters.Add("@TN_BMI", SqlDbType.Float);
            cmd.Parameters.Add("@TN_TRANGTHAI", SqlDbType.Int);

            cmd.Parameters["@TN_ID"].Value        = t.Id_tn;
            cmd.Parameters["@TN_STT"].Value       = t.Tn_stt;
            cmd.Parameters["@TN_BSKHAM"].Value    = t.Tn_bsk;
            cmd.Parameters["@TN_DVKHAM"].Value    = t.Tn_dvk;
            cmd.Parameters["@TN_TUOINAM"].Value   = t.Tn_tuoinam;
            cmd.Parameters["@TN_TUOITHANG"].Value = t.Tn_tuoithang;
            cmd.Parameters["@TN_NGAYGIO"].Value   = t.Tn_ngaygio;
            cmd.Parameters["@TN_MACH"].Value      = t.Tn_mach;
            cmd.Parameters["@TN_NHIETDO"].Value   = t.Tn_nhietdo;
            cmd.Parameters["@TN_NHIPTHO"].Value   = t.Tn_nhiptho;
            cmd.Parameters["@TN_HUYETAP"].Value   = t.Tn_huyetap;
            cmd.Parameters["@TN_CHIEUCAO"].Value  = t.Tn_chieucao;
            cmd.Parameters["@TN_CANNANG"].Value   = t.Tn_cannang;
            cmd.Parameters["@TN_BMI"].Value       = t.Tn_bmi;
            cmd.Parameters["@TN_TRANGTHAI"].Value = t.Tn_trangthai;
            conn.Open();
            int result = cmd.ExecuteNonQuery();

            conn.Close();
            return(result > 0);
        }
        private void btnLuu_Click_1(object sender, EventArgs e)
        {
            BENH_NHAN       b = LayTTBN_DAT();
            PHIEU_TIEP_NHAN t = LayTTPTN_DAT();

            if (dieukien)
            {
                if (daoBN.InsertBN(b))
                {
                    if (daoPTN.InsertPTN(t))
                    {
                        MessageBox.Show("Thêm thành công");
                        loadPTNH();
                        loadPTN();
                        xuLyControl(false);
                        //txtMa.Enabled = true;
                        frm.Visible = false;
                        //resetText();
                    }
                }
                else
                {
                }
            }
            else
            {
                //if (daoPTN.UpdatePTN(t))
                //{
                //    MessageBox.Show("Sửa thành công");
                //    loadThuoc();
                //    xuLyControl(false);
                //    //txtMa.Enabled = true;
                //    frm.Visible = false;
                //    resetText();
                //    //sua(true);
                //}
            }
        }
        private void btnLuu2_Click(object sender, EventArgs e)
        {
            BENH_NHAN       b = LayTTBN();
            PHIEU_TIEP_NHAN t = LayTTPTN();

            if (dieukien == true)
            {
                dt = daoBN.getTTBN(txtMaBN2.Text);
                if (dt.Rows.Count == 0)
                {
                    //MessageBox.Show("Đăng nhập không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //txtMaBN2.Focus();
                    if (daoBN.InsertBN(b))
                    {
                        if (daoPTN.InsertPTN(t))
                        {
                            //MessageBox.Show("Thêm thành công");
                            loadPTN();
                            //loadPTNH();
                            xuLyControl(false);
                            //txtMa.Enabled = true;
                            //frm2.Visible = false;
                            //resetText();
                            xuly2(false);
                            xulyBtn(false);
                            btnCapnhat(false);
                            txtMaBN2.Enabled = false;
                        }
                    }
                }
                else
                {
                    if (daoPTN.InsertPTN(t))
                    {
                        MessageBox.Show("Thêm thành công");
                        loadPTN();
                        //loadPTNH();
                        xuLyControl(false);
                        //txtMa.Enabled = true;
                        //frm2.Visible = false;
                        //resetText();
                        xuly2(false);
                        xulyBtn(false);
                        btnCapnhat(false);
                        txtMaBN2.Enabled = false;
                    }
                }
            }

            else
            {
                if (daoBN.UpdateBN(b))
                {
                    if (daoPTN.UpdatePTN(t))
                    {
                        MessageBox.Show("Sửa thành công");
                        loadPTN();
                        loadPTNH();
                        DateChonNgay_ValueChanged(null, null);

                        xuLyControl(false);
                        //txtMa.Enabled = true;
                        //frm2.Visible = false;
                        //resetText();
                    }
                    else
                    {
                        MessageBox.Show("Sửa không thành công");
                    }
                }
            }
        }
        private PHIEU_TIEP_NHAN LayTTPTN()
        {
            string ma        = txtMa2.Text;
            int    idnv      = 1;
            string idbn      = txtMaBN2.Text;
            string idba      = "1";
            string idbs      = cbBSK2.SelectedValue.ToString();
            int    idbs2     = int.Parse(idbs);
            int    stt       = int.Parse(txtSTT2.Text);
            string dvk       = cbDVK.SelectedValue.ToString();
            int    iddvk     = int.Parse(dvk);
            int    tuoithang = int.Parse(txtTuoithang2.Text);
            int    tuoinam   = int.Parse(txtTuoinam2.Text);
            string ngaygio   = System.DateTime.Now.ToShortDateString();

            if (txtTuoinam2.Text == "")
            {
                txtTuoinam2.Text = "0";
            }
            if (txtTuoithang2.Text == "")
            {
                txtTuoithang2.Text = "0";
            }
            //string ngaygio = DateNgayhen.Text;
            if (txtMach.Text == "")
            {
                txtMach.Text = "0";
            }
            if (txtNhietdo.Text == "")
            {
                txtNhietdo.Text = "0";
            }
            if (txtNhiptho.Text == "")
            {
                txtNhiptho.Text = "0";
            }
            if (txtChieucao.Text == "")
            {
                txtChieucao.Text = "0";
            }
            if (txtHuyetap.Text == "")
            {
                txtHuyetap.Text = "0";
            }
            if (txtHuyetap2.Text == "")
            {
                txtHuyetap2.Text = "0";
            }
            if (txtCannang.Text == "")
            {
                txtCannang.Text = "0";
            }
            if (txtBMI.Text == "")
            {
                txtBMI.Text = "0";
            }

            float mach    = float.Parse(txtMach.Text);
            float nhietdo = float.Parse(txtNhietdo.Text);
            float nhiptho = float.Parse(txtNhiptho.Text);
            //float huyetap = float.Parse(txtHuyetap.Text);
            float  chieucao = float.Parse(txtChieucao.Text);
            float  cannang  = float.Parse(txtCannang.Text);
            string huyetap  = txtHuyetap.Text + "/" + txtHuyetap2.Text;
            float  bmi      = float.Parse(txtBMI.Text);
            int    tt       = 0;

            PHIEU_TIEP_NHAN t = new PHIEU_TIEP_NHAN(ma, idnv, idbn, idba, iddvk, stt, idbs2, dvk, tuoithang, tuoinam, ngaygio, mach, nhietdo, nhiptho, huyetap, chieucao, cannang, bmi, tt);

            return(t);
        }