void loadPhieuXuat(DataTable bang)
 {
     if (px.Count != 0)
     {
         for (int i = 0; i < px.Count; i++)
         {
             px.RemoveAt(i);
         }
         px = XL_PhieuXuat.ChuyenDoiDTOPhieuXuat(bang);
     }
 }
        private void buttonThem_Click(object sender, EventArgs e)
        {
            //load du lieu
            loadPhieuXuat(XL_PhieuXuat.LayDuLieuPhieuXuat());
            //co hieu
            int cohieu = 1;
            //phieu nhap
            string maphieuxuat       = textBoxMaPhieuXuat.Text;
            string ngayxuat          = dateTimePickerNgayXuat.Value.ToString();
            string manhanvien        = gridLookUpEditNhanVien.Text;
            string khachang          = gridLookUpEditMaKhachHang.Text;
            string dieukhoan         = gridLookUpEditDieuKhoanThanhToan.Text;
            string hinhthucthanhtoan = gridLookUpEditHinhThucThanhToan.Text;
            string hanthanhtoan      = dateTimePickerHanThanhToan.Value.ToString();
            string tientratruoc      = textBoxTienTraTruoc.Text;
            string ngaygiao          = dateTimePickerNgayGiao.Value.ToString();
            //tien
            double TongTien = 0;
            int    soluong;
            float  gia;

            for (int i = 0; i < dataGridViewCTPhieuXuat.Rows.Count; i++)
            {
                //object m = dataGridViewCTPhieuXuat["SoLuong", i].Value;
                //object n = dataGridViewCTPhieuXuat["GiaLucNhap", i].Value;
                object n = dataGridViewCTPhieuXuat.Rows[i].Cells["GiaLucNhap"].Value;
                object m = dataGridViewCTPhieuXuat.Rows[i].Cells["SoLuong"].Value;
                if (m != null && n != null)
                {
                    soluong = int.Parse(m.ToString());
                    gia     = float.Parse(n.ToString());
                    double thue = (soluong * gia) * 0.1;
                    TongTien += ((soluong * gia) + thue);
                }
            }
            textBoxTongTien.Text = TongTien.ToString();
            //xet ma px
            for (int i = 0; i < px.Count; i++)
            {
                if (maphieuxuat == px[i].MaPhieuXuat)
                {
                    cohieu = 0;
                    break;
                }
            }

            //phieunhap
            if (maphieuxuat != "" && cohieu == 1 && ngayxuat != "" && hanthanhtoan != "" && manhanvien != "" && ngaygiao != "" && TongTien != 0 &&
                khachang != "" && dieukhoan != "" && hinhthucthanhtoan != "" && tientratruoc != "" && dataGridViewCTPhieuXuat.Rows.Count - 1 != 0)
            {
                bool ThemPhieuXuat = XL_PhieuXuat.ThemPhieuXuat(maphieuxuat, ngayxuat, manhanvien, khachang, hinhthucthanhtoan, dieukhoan, TongTien, hanthanhtoan, tientratruoc, ngaygiao);
                if (ThemPhieuXuat == true)
                {
                    List <bool> ThemCTPhieuXuat = new List <bool>();
                    int         lengt           = dataGridViewCTPhieuXuat.Rows.Count;
                    //ct phieu nhap
                    int sl = ctpx.Count;
                    for (int i = 0; i < lengt; i++)
                    {
                        //
                        object m     = dataGridViewCTPhieuXuat.Rows[i].Cells["MaSP"].Value;
                        object n     = dataGridViewCTPhieuXuat.Rows[i].Cells["GiaLucNhap"].Value;
                        object o     = dataGridViewCTPhieuXuat.Rows[i].Cells["SoLuong"].Value;
                        object makho = dataGridViewCTPhieuXuat.Rows[i].Cells["MaKho"].Value;
                        if (m != null && n != null && o != null)
                        {
                            CTPhieuXuat temp = new CTPhieuXuat();
                            //cap phat ma ct phieu xuat tu dong
                            sl += 1;
                            temp.MaChiTietXuat = string.Format("CTPX" + sl.ToString());

                            temp.MaPhieuXuat = maphieuxuat;
                            temp.MaSP        = m.ToString();
                            temp.MaKho       = makho.ToString();
                            temp.DonGiaSP    = float.Parse(n.ToString());
                            temp.SoLuongMua  = int.Parse(o.ToString());
                            float thue = 0.1f;
                            temp.Thue = thue;
                            //tien moi loai sp
                            float  soluongsp = int.Parse(o.ToString());
                            float  giasp     = float.Parse(n.ToString());
                            double tienthue  = (soluongsp * giasp * 0.1);
                            double tien      = ((soluongsp * giasp) + tienthue);
                            temp.ThanhTien = float.Parse(tien.ToString());

                            bool Them = XL_CTPhieuXuat.ThemCTPhieuXuat(temp.MaChiTietXuat, temp.MaPhieuXuat, temp.MaSP, temp.MaKho, temp.DonGiaSP, temp.ThanhTien, temp.Thue, temp.SoLuongMua);
                            bool Sua  = XL_SanPham.CapNhatSanPham(temp.MaKho, temp.MaSP, temp.SoLuongMua);

                            //  ThemCTPhieuNhap.Add(Them);
                        }
                    }
                    //bien dem xem co luu dc tat ca k
                    int dem = 0;
                    for (int i = 0; i < ThemCTPhieuXuat.Count; i++)
                    {
                        if (ThemCTPhieuXuat[i] == true)
                        {
                            dem++;
                        }
                    }
                    if (dem == ThemCTPhieuXuat.Count)
                    {
                        MessageBox.Show("Thêm thành công!");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Thêm thất bại!");
                        this.Close();
                    }
                }
            }
        }