private void buttonX8_Click(object sender, EventArgs e)
        {
            string mahd = "";

            if (dataGridView1.DataSource != null)
            {
                mahd = dataGridView1.Rows[0].Cells[0].Value.ToString();
            }
            else
            {
                mahd = cthdbll.getHoaDonBy_Ban(ban.MaBan);
            }
            int kq = cthdbll.deleteHoaDon(mahd);

            if (kq == 0)
            {
                MessageBox.Show("Hoá đơn đã thanh toán không thể huỷ");
            }
            else
            {
                loadCTHD();
            }
        }
        private void Btnmon_Click(object sender, EventArgs e)
        {
            if (banDangChon == null)
            {
                return;
            }
            string mahd = cthdbll.getHoaDonBy_Ban(banDangChon.MaBan);

            //TH co hoa don roi
            if (mahd != "")
            {
                if (danhmucchon.Equals("Pha Chế"))
                {
                    MatHang_PC mathang = (MatHang_PC)((ButtonX)sender).Tag;
                    bool       kqktr   = false;
                    if (mahd != "")
                    {
                        kqktr = cthdbll.kiemTraTonTaiTrongCTHD(mahd, mathang.MaMH_PC);
                    }
                    if (kqktr)
                    {
                        //Có rồi cập nhật
                        string soluong = frm.soluong;
                        if (soluong.Equals(""))
                        {
                            soluong = "1";
                        }
                        cthdbll.updateCTHD_PC(mahd, mathang.MaMH_PC, soluong);
                        frm.loadCTHD();
                        pHienThiChinh.Refresh();
                    }
                    else
                    {
                        //chưa có thêm mới
                        string soluong    = frm.soluong;
                        string ma_CTHDMax = mathangbll.getMaxIDCTHD_PC();
                        int    hauso_ct   = 0;
                        if (ma_CTHDMax != null)
                        {
                            hauso_ct = Convert.ToInt32(ma_CTHDMax.Split('_')[1]);
                        }


                        if (soluong.Equals(""))
                        {
                            soluong = "1";
                        }

                        if (Convert.ToInt32(soluong.Trim()) > 0)
                        {
                            ChiTietHoaDon_PhaChe cthd_pc = new ChiTietHoaDon_PhaChe();
                            cthd_pc.MaCTHD_PC = "CTHD_" + (hauso_ct + 1);
                            cthd_pc.MaHD      = mahd;
                            cthd_pc.MaMH_PC   = mathang.MaMH_PC;
                            cthd_pc.SoLuong   = Convert.ToInt32(soluong.Trim());

                            double dongia = mathangbll.getDonGia_byMaMH_PC(mathang.MaMH_PC);

                            cthd_pc.DonGia    = dongia;
                            cthd_pc.ThanhTien = Convert.ToInt32(soluong.Trim()) * dongia;
                            cthd_pc.DonViTinh = mathangbll.getDonViTinh_byMaMH_PC(mathang.MaMH_PC);

                            cthdbll.insertCTHD_PC(cthd_pc);
                            frm.loadCTHD();
                            pHienThiChinh.Refresh();
                        }
                    }
                }

                if (danhmucchon.Equals("Đóng Chai"))
                {
                    MatHang_DongChai mathang = (MatHang_DongChai)((ButtonX)sender).Tag;
                    bool             kqktr   = false;
                    if (mahd != "")
                    {
                        kqktr = cthdbll.kiemTraTonTaiTrongCTHD(mahd, mathang.MaMH_DC);
                    }
                    if (kqktr)
                    {
                        //Có rồi cập nhật
                        string soluong = frm.soluong;
                        if (soluong.Equals(""))
                        {
                            soluong = "1";
                        }
                        cthdbll.updateCTHD_DC(mahd, mathang.MaMH_DC, soluong);
                        frm.loadCTHD();
                        pHienThiChinh.Refresh();
                    }
                    else
                    {
                        //chưa có thêm mới
                        string soluong    = frm.soluong;
                        string ma_CTHDMax = mathangbll.getMaxIDCTHD_DC();
                        int    hauso_ct   = 0;
                        if (ma_CTHDMax != null)
                        {
                            hauso_ct = Convert.ToInt32(ma_CTHDMax.Split('_')[1]);
                        }


                        if (soluong.Equals(""))
                        {
                            soluong = "1";
                        }

                        if (Convert.ToInt32(soluong.Trim()) > 0)
                        {
                            ChiTietHoaDon_DongChai cthd_dc = new ChiTietHoaDon_DongChai();
                            cthd_dc.MaCTHD_DC = "CTHD_" + (hauso_ct + 1);
                            cthd_dc.MaHD      = mahd;
                            cthd_dc.MaMH_DC   = mathang.MaMH_DC;
                            cthd_dc.SoLuong   = Convert.ToInt32(soluong.Trim());

                            double dongia = mathangbll.getDonGia_byMaMH_DC(mathang.MaMH_DC);

                            cthd_dc.DonGia    = dongia;
                            cthd_dc.ThanhTien = Convert.ToInt32(soluong.Trim()) * dongia;
                            cthd_dc.DonViTinh = mathangbll.getDonViTinh_byMaMH_DC(mathang.MaMH_DC);

                            cthdbll.insertCTHD_DC(cthd_dc);
                            frm.loadCTHD();
                            pHienThiChinh.Refresh();
                        }
                    }
                }
            }

            //TH chua co hoa don
            else
            {
                int    hauso     = cthdbll.getHauSoHoaDonMax();
                string mahd_them = "HD_" + (hauso + 1);

                HoaDon hd = new HoaDon();
                hd.MaHD      = mahd_them;
                hd.MaBan     = banDangChon.MaBan;
                hd.NgayLap   = DateTime.Now;
                hd.MaNV      = "NV01";
                hd.TongTien  = 0;
                hd.TinhTrang = false;
                cthdbll.insertHoaDon(hd);

                if (danhmucchon.Equals("Pha Chế"))
                {
                    MatHang_PC mathang    = (MatHang_PC)((ButtonX)sender).Tag;
                    string     soluong    = frm.soluong;
                    string     ma_CTHDMax = mathangbll.getMaxIDCTHD_PC();
                    int        hauso_ct   = 0;
                    if (ma_CTHDMax != null)
                    {
                        hauso_ct = Convert.ToInt32(ma_CTHDMax.Split('_')[1]);
                    }

                    if (soluong.Equals(""))
                    {
                        soluong = "1";
                    }

                    if (Convert.ToInt32(soluong.Trim()) > 0)
                    {
                        ChiTietHoaDon_PhaChe cthd_pc = new ChiTietHoaDon_PhaChe();
                        cthd_pc.MaCTHD_PC = "CTHD_" + (Convert.ToInt32(hauso_ct) + 1);
                        cthd_pc.MaHD      = mahd_them;
                        cthd_pc.MaMH_PC   = mathang.MaMH_PC;
                        cthd_pc.SoLuong   = Convert.ToInt32(soluong.Trim());

                        double dongia = mathangbll.getDonGia_byMaMH_PC(mathang.MaMH_PC);

                        cthd_pc.DonGia    = dongia;
                        cthd_pc.ThanhTien = Convert.ToInt32(soluong.Trim()) * dongia;
                        cthd_pc.DonViTinh = mathangbll.getDonViTinh_byMaMH_PC(mathang.MaMH_PC);

                        cthdbll.insertCTHD_PC(cthd_pc);
                        frm.loadCTHD();
                        pHienThiChinh.Refresh();
                    }
                }

                if (danhmucchon.Equals("Đóng Chai"))
                {
                    MatHang_DongChai mathang    = (MatHang_DongChai)((ButtonX)sender).Tag;
                    string           soluong    = frm.soluong;
                    string           ma_CTHDMax = mathangbll.getMaxIDCTHD_DC();
                    int hauso_ct = 0;
                    if (ma_CTHDMax != null)
                    {
                        hauso_ct = Convert.ToInt32(ma_CTHDMax.Split('_')[1]);
                    }

                    if (soluong.Equals(""))
                    {
                        soluong = "1";
                    }

                    if (Convert.ToInt32(soluong.Trim()) > 0)
                    {
                        ChiTietHoaDon_DongChai cthd_dc = new ChiTietHoaDon_DongChai();
                        cthd_dc.MaCTHD_DC = "CTHD_" + (Convert.ToInt32(hauso_ct) + 1);
                        cthd_dc.MaHD      = mahd_them;
                        cthd_dc.MaMH_DC   = mathang.MaMH_DC;
                        cthd_dc.SoLuong   = Convert.ToInt32(soluong.Trim());

                        double dongia = mathangbll.getDonGia_byMaMH_DC(mathang.MaMH_DC);

                        cthd_dc.DonGia    = dongia;
                        cthd_dc.ThanhTien = Convert.ToInt32(soluong.Trim()) * dongia;
                        cthd_dc.DonViTinh = mathangbll.getDonViTinh_byMaMH_DC(mathang.MaMH_DC);

                        cthdbll.insertCTHD_DC(cthd_dc);
                        frm.loadCTHD();
                        pHienThiChinh.Refresh();
                    }
                }
            }
        }