public Entities.BCXuatNhapTonPhieuXuatNhap[] ChiTiet()
 {
     try
     {
         Entities.BCXuatNhapTonPhieuXuatNhap[] rt = new Entities.BCXuatNhapTonPhieuXuatNhap[dtgvhienthi.RowCount];
         for (int i = 0; i < dtgvhienthi.RowCount; i++)
         {
             rt[i] = new Entities.BCXuatNhapTonPhieuXuatNhap("", dtgvhienthi["MaPhieuXuatNhap", i].Value.ToString(), dtgvhienthi["TenPhieuXuatNhap", i].Value.ToString(),
                                                             dtgvhienthi["MaHang", i].Value.ToString(), dtgvhienthi["TenHang", i].Value.ToString(), double.Parse(dtgvhienthi["SoDuDauKy", i].Value.ToString()), double.Parse(dtgvhienthi["TongNhap", i].Value.ToString()),
                                                             double.Parse(dtgvhienthi["TongXuat", i].Value.ToString()), double.Parse(dtgvhienthi["SoDuCuoiKy", i].Value.ToString()));
         }
         return(rt);
     }
     catch
     {
         return(new Entities.BCXuatNhapTonPhieuXuatNhap[0]);
     }
 }
 private void rdbtimkiem3_CheckedChanged(object sender, EventArgs e)
 {
     try
     {
         if (hienthi == null)
         {
             dtgvhienthi.DataSource = new Entities.BCXuatNhapTonPhieuXuatNhap[0];
             return;
         }
         ///////////////////////////////MRK FIX
         List <Entities.BCXuatNhapTonPhieuXuatNhap> tem0 = new List <Entities.BCXuatNhapTonPhieuXuatNhap>();
         double tong0 = 0;
         double tong1 = 0;
         double tong2 = 0;
         double tong3 = 0;
         foreach (Entities.BCXuatNhapTonPhieuXuatNhap item in tem0)
         {
             tong0 += item.SoDuDauKy;
             tong1 += item.TongNhap;
             tong2 += item.TongXuat;
             tong3 += item.SoDuCuoiKy;
             tem0.Add(item);
         }
         Entities.BCXuatNhapTonPhieuXuatNhap tem1 = new Entities.BCXuatNhapTonPhieuXuatNhap();
         tem1.HanhDong   = "Tổng: ";
         tem1.SoDuDauKy  = tong0;
         tem1.TongNhap   = tong1;
         tem1.TongXuat   = tong2;
         tem1.SoDuCuoiKy = tong3;
         tem0.Add(tem1);
         //////////////////////////////////////
         //dtgvhienthi.DataSource = hienthi;
         dtgvhienthi.DataSource = tem0.ToArray();
     }
     catch
     {
     }
     finally
     {
         fix();
     }
 }
 private void txttimkiem_TextChanged(object sender, EventArgs e)
 {
     try
     {
         if (rdbtimkiem3.Checked == true)
         {
             return;
         }
         if (txttimkiem.Text.Length == 0)
         {
             dtgvhienthi.DataSource = new Entities.BCXuatNhapTonPhieuXuatNhap[0];
             return;
         }
         int soluong = 0;
         if (hienthi != null)
         {
             if (rdbtimkiem1.Checked == true)
             {
                 for (int i = 0; i < hienthibaocao.Length; i++)
                 {
                     int kiemtra = hienthibaocao[i].MaPhieuXuatNhap.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper());
                     if (kiemtra >= 0)
                     {
                         soluong++;
                     }
                 }
                 if (soluong == 0)
                 {
                     dtgvhienthi.DataSource = new Entities.BCXuatNhapTonPhieuXuatNhap[0];
                     return;
                 }
                 Entities.BCXuatNhapTonPhieuXuatNhap[] hienthitheoid = new Entities.BCXuatNhapTonPhieuXuatNhap[soluong];
                 soluong = 0;
                 for (int i = 0; i < hienthibaocao.Length; i++)
                 {
                     int kiemtra = hienthibaocao[i].MaPhieuXuatNhap.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper());
                     if (kiemtra >= 0)
                     {
                         hienthitheoid[soluong] = hienthibaocao[i];
                         soluong++;
                     }
                 }
                 ///////////////////////////////MRK FIX
                 List <Entities.BCXuatNhapTonPhieuXuatNhap> tem0 = new List <Entities.BCXuatNhapTonPhieuXuatNhap>();
                 double tong0 = 0;
                 double tong1 = 0;
                 double tong2 = 0;
                 double tong3 = 0;
                 foreach (Entities.BCXuatNhapTonPhieuXuatNhap item in hienthitheoid)
                 {
                     tong0 += item.SoDuDauKy;
                     tong1 += item.TongNhap;
                     tong2 += item.TongXuat;
                     tong3 += item.SoDuCuoiKy;
                     tem0.Add(item);
                 }
                 Entities.BCXuatNhapTonPhieuXuatNhap tem1 = new Entities.BCXuatNhapTonPhieuXuatNhap();
                 tem1.HanhDong   = "Tổng: ";
                 tem1.SoDuDauKy  = tong0;
                 tem1.TongNhap   = tong1;
                 tem1.TongXuat   = tong2;
                 tem1.SoDuCuoiKy = tong3;
                 tem0.Add(tem1);
                 //////////////////////////////////////
                 //dtgvhienthi.DataSource = hienthitheoid;
                 dtgvhienthi.DataSource = tem0.ToArray();
             }
             if (dtgvhienthi.RowCount > 0)
             {
                 tsslexcel.Enabled = tsslPdf.Enabled = tsslWord.Enabled = tsslchitiet.Enabled = true;
             }
             else
             {
                 tsslexcel.Enabled = tsslPdf.Enabled = tsslWord.Enabled = tsslchitiet.Enabled = false;
             }
         }
     }
     finally
     {
         fix();
     }
 }
        public void TongTienNhanVien()
        {
            try
            {
                nam   = int.Parse(cbbnam.Text);
                thang = int.Parse(cbbthang.Text);
                double sddk, sddkhh, sdckhh, sdck, tongxuat, tongnhap;
                double nhapmua, nhaptralai, nhapkhac;
                double xuatban, xuattralai, xuatkhac;
                nhapmua = nhaptralai = nhapkhac = xuatban = xuattralai = xuatkhac = 0;
                sdckhh  = sddkhh = sdck = sddk = 0;
                int sotang = 0;
                //hienthi = new Entities.BCXuatNhapTonPhieuXuatNhap[chitietxuathuy.Length + chitiethdbanhang.Length + chitiethoadonnhap.Length + chitiettralaincc.Length + chitietkhachhangtralai.Length];
                sotang = 0;
                for (int k = 0; k < hanghoa.Length; k++)
                {////////////////////////// START HÀNG HÓA
                    sdckhh = sddkhh = sdck = sddk = tongxuat = tongnhap = 0;
                    // số dư đầu kỳ - chưa có select
                    for (int l = 0; l < sodukho.Length; l++)
                    {
                        if (sodukho[l].MaHangHoa == hanghoa[k].MaHangHoa)
                        {
                            sddk = sodukho[l].SoDuDauKy;
                            break;
                        }
                    }
                    // xuất hàng - phiếu xuất hủy
                    sdck = sddk = tongxuat = tongnhap = 0;

                    var querypxh = from pxh in phieuxuathuy
                                   where nam.Equals(pxh.NgayLap.Year) && thang.Equals(pxh.NgayLap.Month) && pxh.TrangThai.Equals(true)
                                   select pxh;

                    Entities.PhieuXuatHuy[] phieuxuathuyArr = querypxh.ToArray();

                    // lay chitietphieuxuathuy
                    var queryctpxh = from s1 in phieuxuathuyArr
                                     join s2 in chitietxuathuy
                                     on s1.MaPhieuXuatHuy equals s2.MaPhieuXuatHuy
                                     select s2;

                    Entities.ChiTietXuatHuy[] chitietxuathuyArr = queryctpxh.ToArray();

                    for (int o = 0; o < chitietxuathuyArr.Length; o++)
                    {
                        if (chitietxuathuyArr[o].MaHangHoa == hanghoa[k].MaHangHoa)
                        {
                            tongxuat = xuatkhac = chitietxuathuyArr[o].SoLuong;
                            sdck     = sddk + tongnhap - tongxuat;
                            hienthi  = new Entities.BCXuatNhapTonPhieuXuatNhap("", chitietxuathuyArr[o].MaPhieuXuatHuy,
                                                                               "Phiếu xuất hủy hàng hóa", hanghoa[k].MaHangHoa, hanghoa[k].TenHangHoa, sddk,
                                                                               tongnhap, tongxuat, sdck);
                            tempList.Add(hienthi);
                        }
                    }
                    // xuất hàng - hóa đơn bán hàng
                    sdck = sddk = tongxuat = tongnhap = 0;

                    var querybanhang = from bh in hdbanhang
                                       where bh.NgayBan.Month.Equals(thang) && bh.NgayBan.Year.Equals(nam)
                                       select bh;

                    Entities.HDBanHang[] hdbanhangArr = querybanhang.ToArray();
                    // lay ct hoa don ban hang
                    var queryctbanhang = from s1 in hdbanhangArr
                                         join s2 in chitiethdbanhang
                                         on s1.MaHDBanHang equals s2.MaHDBanHang
                                         select s2;

                    Entities.ChiTietHDBanHang[] chitiethdbanhangArr = queryctbanhang.ToArray();

                    for (int o = 0; o < chitiethdbanhangArr.Length; o++)
                    {
                        if (chitiethdbanhangArr[o].MaHangHoa == hanghoa[k].MaHangHoa)
                        {
                            tongxuat = chitiethdbanhangArr[o].SoLuong;
                            sdck     = sddk + tongnhap - tongxuat;
                            hienthi  = new Entities.BCXuatNhapTonPhieuXuatNhap("", chitiethdbanhangArr[o].MaHDBanHang,
                                                                               "Hóa đơn bán hàng", hanghoa[k].MaHangHoa, hanghoa[k].TenHangHoa, sddk,
                                                                               tongnhap, tongxuat, sdck);
                            tempList.Add(hienthi);
                        }
                        ////////////////////////////////////////////////MRK FIX
                        else
                        {
                            foreach (Entities.GoiHang gh in goihang)
                            {
                                foreach (Entities.ChiTietGoiHang ctgh in chitietgoihang)
                                {
                                    if (gh.MaGoiHang.Equals(ctgh.MaGoiHang) && ctgh.MaGoiHang.Equals(chitiethdbanhang[o].MaHangHoa) && ctgh.MaHangHoa.Equals(hanghoa[k].MaHangHoa))
                                    {
                                        tongxuat = chitiethdbanhang[o].SoLuong * ctgh.SoLuong;
                                        sdck     = sddk + tongnhap - tongxuat;
                                        hienthi  = new Entities.BCXuatNhapTonPhieuXuatNhap("", hdbanhang[o].MaHDBanHang,
                                                                                           "Hóa đơn bán hàng", hanghoa[k].MaHangHoa, hanghoa[k].TenHangHoa, sddk,
                                                                                           tongnhap, tongxuat, sdck);
                                        tempList.Add(hienthi);
                                    }
                                }
                            }
                        }
                        ////////////////////////////////////////////////MRK FIX
                    }
                    // xuất hàng - trả lại nhà cung cấp
                    sdck = sddk = tongxuat = tongnhap = 0;

                    var querytralaincc = from tl in tralaincc
                                         where tl.Ngaytra.Month.Equals(thang) && tl.Ngaytra.Year.Equals(nam)
                                         select tl;

                    Entities.TraLaiNCC[] tralainccArr = querytralaincc.ToArray();
                    // lay chitiet tra lai ncc
                    var querycttralaincc = from s1 in tralainccArr
                                           join s2 in chitiettralaincc
                                           on s1.MaHDTraLaiNCC equals s2.MaHDTraLaiNCC
                                           select s2;

                    Entities.ChiTietTraLaiNhaCungCap[] cttralainccArr = querycttralaincc.ToArray();

                    for (int o = 0; o < cttralainccArr.Length; o++)
                    {
                        if (cttralainccArr[o].MaHangHoa == hanghoa[k].MaHangHoa)
                        {
                            tongxuat = cttralainccArr[o].SoLuong;
                            sdck     = sddk + tongnhap - tongxuat;
                            hienthi  = new Entities.BCXuatNhapTonPhieuXuatNhap("", cttralainccArr[o].MaHDTraLaiNCC,
                                                                               "Trả lại nhà cung cấp", hanghoa[k].MaHangHoa, hanghoa[k].TenHangHoa, sddk,
                                                                               tongnhap, tongxuat, sdck);
                            tempList.Add(hienthi);
                        }
                    }
                    // nhập hàng - hóa đơn nhập
                    sdck = sddk = tongxuat = tongnhap = 0;

                    var queryhoadonnhap = from nh in hoadonnhap
                                          where nh.NgayNhap.Month.Equals(thang) && nh.NgayNhap.Year.Equals(nam)
                                          select nh;

                    Entities.HoaDonNhap[] hoadonnhapArr = queryhoadonnhap.ToArray();
                    // lay chitiet hoa don nhap
                    var querycthoadonnhap = from s1 in hoadonnhap
                                            join s2 in chitiethoadonnhap
                                            on s1.MaHoaDonNhap equals s2.MaHoaDonNhap
                                            select s2;

                    Entities.ChiTietHoaDonNhap[] cthoadonnhapArr = querycthoadonnhap.ToArray();

                    for (int o = 0; o < cthoadonnhapArr.Length; o++)
                    {
                        if (cthoadonnhapArr[o].MaHangHoa == hanghoa[k].MaHangHoa)
                        {
                            tongnhap = cthoadonnhapArr[o].SoLuong;
                            sdck     = sddk + tongnhap - tongxuat;
                            hienthi  = new Entities.BCXuatNhapTonPhieuXuatNhap("", cthoadonnhapArr[o].MaHoaDonNhap,
                                                                               "Hóa đơn nhập kho", hanghoa[k].MaHangHoa, hanghoa[k].TenHangHoa, sddk,
                                                                               tongnhap, tongxuat, sdck);
                            tempList.Add(hienthi);
                        }
                    }
                    // nhập hàng - khách hàng trả lại
                    sdck = sddk = tongxuat = tongnhap = 0;

                    var querykhachhangtralai = from tl in khachhangtralai
                                               where tl.NgayNhap.Month.Equals(thang) && tl.NgayNhap.Year.Equals(nam)
                                               select tl;

                    Entities.KhachHangTraLai[] khachhangtralaiArr = querykhachhangtralai.ToArray();
                    // lay chi tiet khach hang tra lai
                    var queryctkhtralai = from s1 in khachhangtralaiArr
                                          join s2 in chitietkhachhangtralai
                                          on s1.MaKhachHangTraLai equals s2.MaKhachHangTraLai
                                          select s2;

                    Entities.ChiTietKhachHangTraLai[] ctkhtralaiArr = queryctkhtralai.ToArray();

                    for (int o = 0; o < ctkhtralaiArr.Length; o++)
                    {
                        if (ctkhtralaiArr[o].MaHangHoa == hanghoa[k].MaHangHoa)
                        {
                            tongnhap = ctkhtralaiArr[o].SoLuong;
                            sdck     = sddk + tongnhap - tongxuat;
                            hienthi  = new Entities.BCXuatNhapTonPhieuXuatNhap("", ctkhtralaiArr[o].MaKhachHangTraLai,
                                                                               "Khách hàng trả lại", hanghoa[k].MaHangHoa, hanghoa[k].TenHangHoa, sddk,
                                                                               tongnhap, tongxuat, sdck);
                            tempList.Add(hienthi);
                        }
                    }
                    ////////////////////////// END HÀNG HÓA
                }

                // loc cac ma hoa don thanh nhom.
                List <string> maHoaDonList = new List <string>();
                foreach (Entities.BCXuatNhapTonPhieuXuatNhap nxt in tempList)
                {
                    if (!maHoaDonList.Contains(nxt.MaPhieuXuatNhap))
                    {
                        maHoaDonList.Add(nxt.MaPhieuXuatNhap);
                    }
                }

                // sap xep cac hoa don giong nhau len tren .
                List <Entities.BCXuatNhapTonPhieuXuatNhap> temp1 = new List <Entities.BCXuatNhapTonPhieuXuatNhap>();
                foreach (string st in maHoaDonList.ToArray())
                {
                    foreach (Entities.BCXuatNhapTonPhieuXuatNhap xnt1 in tempList.ToArray())
                    {
                        if (st.Equals(xnt1.MaPhieuXuatNhap))
                        {
                            temp1.Add(xnt1);
                        }
                    }
                }

                hienthibaocao = temp1.ToArray();

                ///////////////////////////////MRK FIX
                List <Entities.BCXuatNhapTonPhieuXuatNhap> tem0 = new List <Entities.BCXuatNhapTonPhieuXuatNhap>();
                double tong0 = 0;
                double tong1 = 0;
                double tong2 = 0;
                double tong3 = 0;
                foreach (Entities.BCXuatNhapTonPhieuXuatNhap item in temp1.ToArray())
                {
                    if (item == null)
                    {
                        continue;
                    }
                    tong0 += item.SoDuDauKy;
                    tong1 += item.TongNhap;
                    tong2 += item.TongXuat;
                    tong3 += item.SoDuCuoiKy;
                    tem0.Add(item);
                }
                Entities.BCXuatNhapTonPhieuXuatNhap tem1 = new Entities.BCXuatNhapTonPhieuXuatNhap();
                tem1.TenHang    = "Tổng: ";
                tem1.SoDuDauKy  = tong0;
                tem1.TongNhap   = tong1;
                tem1.TongXuat   = tong2;
                tem1.SoDuCuoiKy = tong3;
                tem0.Add(tem1);
                //////////////////////////////////////
                //dtgvhienthi.DataSource = hienthi;
                dtgvhienthi.DataSource = tem0.ToArray();

                if (tem0.ToArray().Length > 0)
                {
                    tsslexcel.Enabled = tsslPdf.Enabled = tsslWord.Enabled = tsslchitiet.Enabled = true;
                }
                else
                {
                    tsslexcel.Enabled = tsslPdf.Enabled = tsslWord.Enabled = tsslchitiet.Enabled = false;
                }
            }
            catch
            {
            }
            finally
            {
                fix();
            }
        }
        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (rdbtimkiem3.Checked == true)
                {
                    return;
                }
                if (txttimkiem.Text.Length == 0)
                {
                    dtgvhienthi.DataSource = new Entities.BCXuatNhapTonPhieuXuatNhap[0];
                    return;
                }
                int soluong = 0;
                if (hienthi != null)
                {
                    if (rdbtimkiem1.Checked == true)
                    {
                        for (int i = 0; i < hienthibaocao.Length; i++)
                        {
                            int kiemtra = hienthibaocao[i].MaPhieuXuatNhap.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper());
                            if (kiemtra >= 0)
                            {
                                soluong++;
                            }
                        }
                        if (soluong == 0)
                        {
                            dtgvhienthi.DataSource = new Entities.BCXuatNhapTonPhieuXuatNhap[0];
                            return;
                        }
                        Entities.BCXuatNhapTonPhieuXuatNhap[] hienthitheoid = new Entities.BCXuatNhapTonPhieuXuatNhap[soluong];
                        soluong = 0;
                        for (int i = 0; i < hienthibaocao.Length; i++)
                        {
                            int kiemtra = hienthibaocao[i].MaPhieuXuatNhap.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper());
                            if (kiemtra >= 0)
                            {
                                hienthitheoid[soluong] = hienthibaocao[i];
                                soluong++;
                            }
                        }
                        ///////////////////////////////MRK FIX
                        List<Entities.BCXuatNhapTonPhieuXuatNhap> tem0 = new List<Entities.BCXuatNhapTonPhieuXuatNhap>();
                        double tong0 = 0;
                        double tong1 = 0;
                        double tong2 = 0;
                        double tong3 = 0;
                        foreach (Entities.BCXuatNhapTonPhieuXuatNhap item in hienthitheoid)
                        {
                            tong0 += item.SoDuDauKy;
                            tong1 += item.TongNhap;
                            tong2 += item.TongXuat;
                            tong3 += item.SoDuCuoiKy;
                            tem0.Add(item);
                        }
                        Entities.BCXuatNhapTonPhieuXuatNhap tem1 = new Entities.BCXuatNhapTonPhieuXuatNhap();
                        tem1.HanhDong = "Tổng: ";
                        tem1.SoDuDauKy = tong0;
                        tem1.TongNhap = tong1;
                        tem1.TongXuat = tong2;
                        tem1.SoDuCuoiKy = tong3;
                        tem0.Add(tem1);
                        //////////////////////////////////////
                        //dtgvhienthi.DataSource = hienthitheoid;
                        dtgvhienthi.DataSource = tem0.ToArray();

                    }
                    if (dtgvhienthi.RowCount > 0)
                    {
                        tsslexcel.Enabled = tsslPdf.Enabled = tsslWord.Enabled = tsslchitiet.Enabled = true;
                    }
                    else
                    {
                        tsslexcel.Enabled = tsslPdf.Enabled = tsslWord.Enabled = tsslchitiet.Enabled = false;
                    }
                }
            }
            finally
            {
                fix();
            }
        }
 public Entities.BCXuatNhapTonPhieuXuatNhap[] ChiTiet()
 {
     try
     {
         Entities.BCXuatNhapTonPhieuXuatNhap[] rt = new Entities.BCXuatNhapTonPhieuXuatNhap[dtgvhienthi.RowCount];
         for (int i = 0; i < dtgvhienthi.RowCount; i++)
         {
             rt[i] = new Entities.BCXuatNhapTonPhieuXuatNhap("", dtgvhienthi["MaPhieuXuatNhap", i].Value.ToString(), dtgvhienthi["TenPhieuXuatNhap", i].Value.ToString(),
                 dtgvhienthi["MaHang", i].Value.ToString(), dtgvhienthi["TenHang", i].Value.ToString(), double.Parse(dtgvhienthi["SoDuDauKy", i].Value.ToString()), double.Parse(dtgvhienthi["TongNhap", i].Value.ToString()),
                 double.Parse(dtgvhienthi["TongXuat", i].Value.ToString()), double.Parse(dtgvhienthi["SoDuCuoiKy", i].Value.ToString()));
         }
         return rt;
     }
     catch
     {
         return new Entities.BCXuatNhapTonPhieuXuatNhap[0];
     }
 }
        private void rdbtimkiem3_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (hienthi == null)
                {
                    dtgvhienthi.DataSource = new Entities.BCXuatNhapTonPhieuXuatNhap[0];
                    return;
                }
                ///////////////////////////////MRK FIX
                List<Entities.BCXuatNhapTonPhieuXuatNhap> tem0 = new List<Entities.BCXuatNhapTonPhieuXuatNhap>();
                double tong0 = 0;
                double tong1 = 0;
                double tong2 = 0;
                double tong3 = 0;
                foreach (Entities.BCXuatNhapTonPhieuXuatNhap item in tem0)
                {
                    tong0 += item.SoDuDauKy;
                    tong1 += item.TongNhap;
                    tong2 += item.TongXuat;
                    tong3 += item.SoDuCuoiKy;
                    tem0.Add(item);
                }
                Entities.BCXuatNhapTonPhieuXuatNhap tem1 = new Entities.BCXuatNhapTonPhieuXuatNhap();
                tem1.HanhDong = "Tổng: ";
                tem1.SoDuDauKy = tong0;
                tem1.TongNhap = tong1;
                tem1.TongXuat = tong2;
                tem1.SoDuCuoiKy = tong3;
                tem0.Add(tem1);
                //////////////////////////////////////
                //dtgvhienthi.DataSource = hienthi;
                dtgvhienthi.DataSource = tem0.ToArray();

            }
            catch
            {
            }
            finally
            {
                fix();

            }
        }
        public void TongTienNhanVien()
        {
            try
            {
                nam = int.Parse(cbbnam.Text);
                thang = int.Parse(cbbthang.Text);
                double sddk, sddkhh, sdckhh, sdck, tongxuat, tongnhap;
                double nhapmua, nhaptralai, nhapkhac;
                double xuatban, xuattralai, xuatkhac;
                nhapmua = nhaptralai = nhapkhac = xuatban = xuattralai = xuatkhac = 0;
                sdckhh = sddkhh = sdck = sddk = 0;
                int sotang = 0;
                //hienthi = new Entities.BCXuatNhapTonPhieuXuatNhap[chitietxuathuy.Length + chitiethdbanhang.Length + chitiethoadonnhap.Length + chitiettralaincc.Length + chitietkhachhangtralai.Length];
                sotang = 0;
                for (int k = 0; k < hanghoa.Length; k++)
                {////////////////////////// START HÀNG HÓA
                    sdckhh = sddkhh = sdck = sddk = tongxuat = tongnhap = 0;
                    // số dư đầu kỳ - chưa có select
                    for (int l = 0; l < sodukho.Length; l++)
                    {
                        if (sodukho[l].MaHangHoa == hanghoa[k].MaHangHoa)
                        {
                            sddk = sodukho[l].SoDuDauKy;
                            break;
                        }
                    }
                    // xuất hàng - phiếu xuất hủy
                    sdck = sddk = tongxuat = tongnhap = 0;

                    var querypxh = from pxh in phieuxuathuy
                                   where nam.Equals(pxh.NgayLap.Year) && thang.Equals(pxh.NgayLap.Month) && pxh.TrangThai.Equals(true)
                                   select pxh;

                    Entities.PhieuXuatHuy[] phieuxuathuyArr = querypxh.ToArray();

                    // lay chitietphieuxuathuy
                    var queryctpxh = from s1 in phieuxuathuyArr
                                     join s2 in chitietxuathuy
                                     on s1.MaPhieuXuatHuy equals s2.MaPhieuXuatHuy
                                     select s2;

                    Entities.ChiTietXuatHuy[] chitietxuathuyArr = queryctpxh.ToArray();

                    for (int o = 0; o < chitietxuathuyArr.Length; o++)
                    {
                        if (chitietxuathuyArr[o].MaHangHoa == hanghoa[k].MaHangHoa)
                        {
                            tongxuat = xuatkhac = chitietxuathuyArr[o].SoLuong;
                            sdck = sddk + tongnhap - tongxuat;
                            hienthi = new Entities.BCXuatNhapTonPhieuXuatNhap("", chitietxuathuyArr[o].MaPhieuXuatHuy,
                                "Phiếu xuất hủy hàng hóa", hanghoa[k].MaHangHoa, hanghoa[k].TenHangHoa, sddk,
                                tongnhap, tongxuat, sdck);
                            tempList.Add(hienthi);
                        }
                    }
                    // xuất hàng - hóa đơn bán hàng
                    sdck = sddk = tongxuat = tongnhap = 0;

                    var querybanhang = from bh in hdbanhang
                                       where bh.NgayBan.Month.Equals(thang) && bh.NgayBan.Year.Equals(nam)
                                       select bh;

                    Entities.HDBanHang[] hdbanhangArr = querybanhang.ToArray();
                    // lay ct hoa don ban hang
                    var queryctbanhang = from s1 in hdbanhangArr
                                         join s2 in chitiethdbanhang
                                         on s1.MaHDBanHang equals s2.MaHDBanHang
                                         select s2;

                    Entities.ChiTietHDBanHang[] chitiethdbanhangArr = queryctbanhang.ToArray();

                    for (int o = 0; o < chitiethdbanhangArr.Length; o++)
                    {
                        if (chitiethdbanhangArr[o].MaHangHoa == hanghoa[k].MaHangHoa)
                        {
                            tongxuat = chitiethdbanhangArr[o].SoLuong;
                            sdck = sddk + tongnhap - tongxuat;
                            hienthi = new Entities.BCXuatNhapTonPhieuXuatNhap("", chitiethdbanhangArr[o].MaHDBanHang,
                                "Hóa đơn bán hàng", hanghoa[k].MaHangHoa, hanghoa[k].TenHangHoa, sddk,
                                tongnhap, tongxuat, sdck);
                            tempList.Add(hienthi);
                        }
                        ////////////////////////////////////////////////MRK FIX
                        else
                        {
                            foreach (Entities.GoiHang gh in goihang)
                            {
                                foreach (Entities.ChiTietGoiHang ctgh in chitietgoihang)
                                {
                                    if (gh.MaGoiHang.Equals(ctgh.MaGoiHang) && ctgh.MaGoiHang.Equals(chitiethdbanhang[o].MaHangHoa) && ctgh.MaHangHoa.Equals(hanghoa[k].MaHangHoa))
                                    {
                                        tongxuat = chitiethdbanhang[o].SoLuong * ctgh.SoLuong;
                                        sdck = sddk + tongnhap - tongxuat;
                                        hienthi = new Entities.BCXuatNhapTonPhieuXuatNhap("", hdbanhang[o].MaHDBanHang,
                                            "Hóa đơn bán hàng", hanghoa[k].MaHangHoa, hanghoa[k].TenHangHoa, sddk,
                                            tongnhap, tongxuat, sdck);
                                        tempList.Add(hienthi);
                                    }
                                }
                            }
                        }
                        ////////////////////////////////////////////////MRK FIX
                    }
                    // xuất hàng - trả lại nhà cung cấp
                    sdck = sddk = tongxuat = tongnhap = 0;

                    var querytralaincc = from tl in tralaincc
                                         where tl.Ngaytra.Month.Equals(thang) && tl.Ngaytra.Year.Equals(nam)
                                         select tl;

                    Entities.TraLaiNCC[] tralainccArr = querytralaincc.ToArray();
                    // lay chitiet tra lai ncc
                    var querycttralaincc = from s1 in tralainccArr
                                           join s2 in chitiettralaincc
                                           on s1.MaHDTraLaiNCC equals s2.MaHDTraLaiNCC
                                           select s2;

                    Entities.ChiTietTraLaiNhaCungCap[] cttralainccArr = querycttralaincc.ToArray();

                    for (int o = 0; o < cttralainccArr.Length; o++)
                    {
                        if (cttralainccArr[o].MaHangHoa == hanghoa[k].MaHangHoa)
                        {
                            tongxuat = cttralainccArr[o].SoLuong;
                            sdck = sddk + tongnhap - tongxuat;
                            hienthi = new Entities.BCXuatNhapTonPhieuXuatNhap("", cttralainccArr[o].MaHDTraLaiNCC,
                                "Trả lại nhà cung cấp", hanghoa[k].MaHangHoa, hanghoa[k].TenHangHoa, sddk,
                                tongnhap, tongxuat, sdck);
                            tempList.Add(hienthi);
                        }
                    }
                    // nhập hàng - hóa đơn nhập
                    sdck = sddk = tongxuat = tongnhap = 0;

                    var queryhoadonnhap = from nh in hoadonnhap
                                          where nh.NgayNhap.Month.Equals(thang) && nh.NgayNhap.Year.Equals(nam)
                                          select nh;

                    Entities.HoaDonNhap[] hoadonnhapArr = queryhoadonnhap.ToArray();
                    // lay chitiet hoa don nhap
                    var querycthoadonnhap = from s1 in hoadonnhap
                                            join s2 in chitiethoadonnhap
                                            on s1.MaHoaDonNhap equals s2.MaHoaDonNhap
                                            select s2;

                    Entities.ChiTietHoaDonNhap[] cthoadonnhapArr = querycthoadonnhap.ToArray();

                    for (int o = 0; o < cthoadonnhapArr.Length; o++)
                    {
                        if (cthoadonnhapArr[o].MaHangHoa == hanghoa[k].MaHangHoa)
                        {
                            tongnhap = cthoadonnhapArr[o].SoLuong;
                            sdck = sddk + tongnhap - tongxuat;
                            hienthi = new Entities.BCXuatNhapTonPhieuXuatNhap("", cthoadonnhapArr[o].MaHoaDonNhap,
                                "Hóa đơn nhập kho", hanghoa[k].MaHangHoa, hanghoa[k].TenHangHoa, sddk,
                                tongnhap, tongxuat, sdck);
                            tempList.Add(hienthi);
                        }
                    }
                    // nhập hàng - khách hàng trả lại
                    sdck = sddk = tongxuat = tongnhap = 0;

                    var querykhachhangtralai = from tl in khachhangtralai
                                               where tl.NgayNhap.Month.Equals(thang) && tl.NgayNhap.Year.Equals(nam)
                                               select tl;

                    Entities.KhachHangTraLai[] khachhangtralaiArr = querykhachhangtralai.ToArray();
                    // lay chi tiet khach hang tra lai
                    var queryctkhtralai = from s1 in khachhangtralaiArr
                                          join s2 in chitietkhachhangtralai
                                          on s1.MaKhachHangTraLai equals s2.MaKhachHangTraLai
                                          select s2;

                    Entities.ChiTietKhachHangTraLai[] ctkhtralaiArr = queryctkhtralai.ToArray();

                    for (int o = 0; o < ctkhtralaiArr.Length; o++)
                    {
                        if (ctkhtralaiArr[o].MaHangHoa == hanghoa[k].MaHangHoa)
                        {
                            tongnhap = ctkhtralaiArr[o].SoLuong;
                            sdck = sddk + tongnhap - tongxuat;
                            hienthi = new Entities.BCXuatNhapTonPhieuXuatNhap("", ctkhtralaiArr[o].MaKhachHangTraLai,
                                "Khách hàng trả lại", hanghoa[k].MaHangHoa, hanghoa[k].TenHangHoa, sddk,
                                tongnhap, tongxuat, sdck);
                            tempList.Add(hienthi);
                        }
                    }
                    ////////////////////////// END HÀNG HÓA
                }

                // loc cac ma hoa don thanh nhom.
                List<string> maHoaDonList = new List<string>();
                foreach (Entities.BCXuatNhapTonPhieuXuatNhap nxt in tempList)
                {
                    if (!maHoaDonList.Contains(nxt.MaPhieuXuatNhap))
                    {
                        maHoaDonList.Add(nxt.MaPhieuXuatNhap);
                    }
                }

                // sap xep cac hoa don giong nhau len tren .
                List<Entities.BCXuatNhapTonPhieuXuatNhap> temp1 = new List<Entities.BCXuatNhapTonPhieuXuatNhap>();
                foreach (string st in maHoaDonList.ToArray())
                {
                    foreach (Entities.BCXuatNhapTonPhieuXuatNhap xnt1 in tempList.ToArray())
                    {
                        if (st.Equals(xnt1.MaPhieuXuatNhap))
                        {
                            temp1.Add(xnt1);
                        }

                    }
                }

                hienthibaocao = temp1.ToArray();

                ///////////////////////////////MRK FIX
                List<Entities.BCXuatNhapTonPhieuXuatNhap> tem0 = new List<Entities.BCXuatNhapTonPhieuXuatNhap>();
                double tong0 = 0;
                double tong1 = 0;
                double tong2 = 0;
                double tong3 = 0;
                foreach (Entities.BCXuatNhapTonPhieuXuatNhap item in temp1.ToArray())
                {
                    if (item == null)
                    {
                        continue;
                    }
                    tong0 += item.SoDuDauKy;
                    tong1 += item.TongNhap;
                    tong2 += item.TongXuat;
                    tong3 += item.SoDuCuoiKy;
                    tem0.Add(item);
                }
                Entities.BCXuatNhapTonPhieuXuatNhap tem1 = new Entities.BCXuatNhapTonPhieuXuatNhap();
                tem1.TenHang = "Tổng: ";
                tem1.SoDuDauKy = tong0;
                tem1.TongNhap = tong1;
                tem1.TongXuat = tong2;
                tem1.SoDuCuoiKy = tong3;
                tem0.Add(tem1);
                //////////////////////////////////////
                //dtgvhienthi.DataSource = hienthi;
                dtgvhienthi.DataSource = tem0.ToArray();

                if (tem0.ToArray().Length > 0)
                {
                    tsslexcel.Enabled = tsslPdf.Enabled = tsslWord.Enabled = tsslchitiet.Enabled = true;
                }
                else
                {
                    tsslexcel.Enabled = tsslPdf.Enabled = tsslWord.Enabled = tsslchitiet.Enabled = false;
                }
            }
            catch
            {
            }
            finally
            {
                fix();
            }
        }