private void AutoGenAllChungTu()
        {
            try
            {
                NhapNccBusiness  khoBusiness;
                int              code    = 0;
                List <DMKhoInfo> listKho = DMKhoDataProvider.GetListDMKhoInfor();
                frmProgress.Instance.MaxValue = listKho.Count;
                frmProgress.Instance.Value    = 0;
                foreach (DMKhoInfo dmKhoInfo in listKho)
                {
                    currentKho      = dmKhoInfo;
                    currentTrungTam = DMTrungTamDataProvider.GetTrungTamByIdInfo(dmKhoInfo.IdTrungTam);
                    frmProgress.Instance.PushStatus();
                    TryLock();
                    frmProgress.Instance.PopStatus();
                    frmProgress.Instance.PushStatus();
                    frmProgress.Instance.MaxValue    = 5;
                    frmProgress.Instance.Value       = 0;
                    frmProgress.Instance.Description = currentKho.MaKho + ": Đang đồng bộ số liệu ... ";
                    isComplete = false;
                    SynsChungTuNhap();
                    frmProgress.Instance.PopStatus();
                    frmProgress.Instance.PushStatus();
                    frmProgress.Instance.Description = currentKho.MaKho + ": Đang import số liệu ...";
                    lstDataSource = tmp_NhapHang_UserProvider.GetNhapHangUserInfor(dmKhoInfo.IdKho);
                    frmProgress.Instance.MaxValue = lstDataSource.Count;
                    frmProgress.Instance.Value    = 0;
                    foreach (tmp_NhapHang_UserInfo tmpNhapHangUserInfo in lstDataSource)
                    {
                        frmProgress.Instance.Value += 1;
                        ChungTuXuatNhapNccInfo chungTuXuatNhapNccInfo = tblChungTuDataProvider.GetChungTuNhapNCCFromSoPO(
                            tmpNhapHangUserInfo.SoPO, tmpNhapHangUserInfo.SoPhieuNhap, Convert.ToInt32(TransactionType.NHAP_PO),
                            currentKho.IdKho, NgayLap, tmpNhapHangUserInfo.IdChungTu);

                        if (chungTuXuatNhapNccInfo == null)
                        {
                            chungTuXuatNhapNccInfo = new ChungTuXuatNhapNccInfo
                            {
                                SoChungTu   = CommonProvider.Instance.GetSoPhieu(Declare.Prefix.PhieuNhapHangMua),
                                NgayLap     = tmpNhapHangUserInfo.NgayNhap,
                                SoPO        = tmpNhapHangUserInfo.SoPO,
                                SoPhieuNhap = tmpNhapHangUserInfo.SoPhieuNhap,
                                LoaiChungTu = Convert.ToInt32(TransactionType.NHAP_PO),
                                IdDoiTuong  = tmpNhapHangUserInfo.IdDoiTuong,
                                IdKho       = currentKho.IdKho
                            };
                        }

                        khoBusiness = new NhapNccBusiness(chungTuXuatNhapNccInfo);

                        if (chungTuXuatNhapNccInfo.IdChungTu == 0)
                        {
                            khoBusiness.ListChiTietChungTu = KeToanNhapNccDataProvider.Instance.GetListNhapHangUserInfoFromOid(khoBusiness.ChungTu.SoPO, khoBusiness.ChungTu.SoPhieuNhap, Convert.ToInt32(LoaiGiaoDichPO.NHAP_HANG_NHA_CUNG_CAP), currentKho.IdKho);

                            foreach (ChungTuXuatNhapNccChiTietInfo chungTuXuatNhapNccChiTietInfo in khoBusiness.ListChiTietChungTu)
                            {
                                if (khoBusiness.ListChiTietHangHoa.Exists(delegate(ChungTuNhapNccChiTietHangHoaInfo match)
                                {
                                    return(match.IdSanPham == chungTuXuatNhapNccChiTietInfo.IdSanPham &&
                                           match.TransactionID == chungTuXuatNhapNccChiTietInfo.TransactionID);
                                }))
                                {
                                    continue;
                                }

                                //import ma vach tu file

                                dvFillter.RowFilter = String.Format("SoPO='{0}' and SoPhieuNhap='{1}' and MaSanPham='{2}' and MaKho='{3}'",
                                                                    chungTuXuatNhapNccInfo.SoPO, chungTuXuatNhapNccInfo.SoPhieuNhap, chungTuXuatNhapNccChiTietInfo.MaSanPham, dmKhoInfo.MaKho);

                                DataTable dtTemp = dvFillter.ToTable();
                                foreach (DataRow dataRow in dtTemp.Rows)
                                {
                                    ChungTuNhapNccChiTietHangHoaInfo chungTuNhapNccChiTietHangHoaInfo =
                                        new ChungTuNhapNccChiTietHangHoaInfo
                                    {
                                        DonGia           = 0,
                                        IdChungTuChiTiet = 0,
                                        IdSanPham        = chungTuXuatNhapNccChiTietInfo.IdSanPham,
                                        TransactionID    =
                                            chungTuXuatNhapNccChiTietInfo.TransactionID
                                    };
                                    chungTuNhapNccChiTietHangHoaInfo.MaVach  = Convert.ToString(dataRow["MaVach"]);
                                    chungTuNhapNccChiTietHangHoaInfo.SoLuong = Convert.ToInt32(dataRow["SoLuong"]);
                                    khoBusiness.ListChiTietHangHoa.Add(chungTuNhapNccChiTietHangHoaInfo);
                                }
                            }
                            try
                            {
                                if (khoBusiness.ListChiTietHangHoa.Count > 0)
                                {
                                    khoBusiness.SaveChungTu();
                                }
                                else
                                {
                                    File.AppendAllText(Application.StartupPath + "\\ChuaNhapMaVachResult.txt", "\n"
                                                       + " - SoChungTu: " + chungTuXuatNhapNccInfo.SoChungTu
                                                       + " - PO: " + chungTuXuatNhapNccInfo.SoPO + " - PN: " + chungTuXuatNhapNccInfo.SoPhieuNhap);
                                }
                            }
                            catch (Exception ex)
                            {
                                File.AppendAllText(Application.StartupPath + "\\ImportResult.txt", "\n" + ex.Message
                                                   + " - PO: " + chungTuXuatNhapNccInfo.SoPO + " - PN: " + chungTuXuatNhapNccInfo.SoPhieuNhap);
                            }
                        }
                        else //if (khoBusiness.ListChiTietHangHoa.Count == 0)
                        {
                            DataTable dtTemp = null;
                            foreach (ChungTuXuatNhapNccChiTietInfo chungTuXuatNhapNccChiTietInfo in khoBusiness.ListChiTietChungTu)
                            {
                                if (khoBusiness.GetListChiTietHangHoaByIdSanPham(chungTuXuatNhapNccChiTietInfo.IdSanPham).Count < chungTuXuatNhapNccChiTietInfo.SoLuong)
                                {
                                    dvFillter.RowFilter = String.Format("SoPO='{0}' and SoPhieuNhap='{1}' and MaSanPham='{2}' and MaKho='{3}'",
                                                                        chungTuXuatNhapNccInfo.SoPO, chungTuXuatNhapNccInfo.SoPhieuNhap, chungTuXuatNhapNccChiTietInfo.MaSanPham, dmKhoInfo.MaKho);

                                    dtTemp = dvFillter.ToTable();
                                    foreach (DataRow dataRow in dtTemp.Rows)
                                    {
                                        string maVach = Convert.ToString(dataRow["MaVach"]);
                                        if (khoBusiness.ListChiTietHangHoa.Exists(delegate(ChungTuNhapNccChiTietHangHoaInfo match)
                                        {
                                            return(match.MaVach == maVach);
                                        }))
                                        {
                                            continue;
                                        }
                                        ChungTuNhapNccChiTietHangHoaInfo chungTuNhapNccChiTietHangHoaInfo =
                                            new ChungTuNhapNccChiTietHangHoaInfo
                                        {
                                            DonGia           = 0,
                                            IdChungTuChiTiet = 0,
                                            IdSanPham        = chungTuXuatNhapNccChiTietInfo.IdSanPham,
                                            TransactionID    =
                                                chungTuXuatNhapNccChiTietInfo.TransactionID
                                        };
                                        chungTuNhapNccChiTietHangHoaInfo.MaVach  = maVach;
                                        chungTuNhapNccChiTietHangHoaInfo.SoLuong = Convert.ToInt32(dataRow["SoLuong"]);

                                        khoBusiness.ListChiTietHangHoa.Add(chungTuNhapNccChiTietHangHoaInfo);
                                    }
                                }
                            }
                            try
                            {
                                if (dtTemp != null && dtTemp.Rows.Count > 0 && khoBusiness.ListChiTietHangHoa.Count > 0)
                                {
                                    khoBusiness.SaveChungTu();
                                }
                                else
                                {
                                    //try
                                    //{
                                    //    khoBusiness.DeleteChungTu();
                                    //    tmp_NhapHang_LogDataProvider.Delete(chungTuXuatNhapNccInfo.SoPO, chungTuXuatNhapNccInfo.SoPhieuNhap, Convert.ToInt32(LoaiGiaoDichPO.NHAP_HANG_NHA_CUNG_CAP), Declare.IdKho);
                                    //}
                                    //catch (Exception)
                                    //{
                                    //    File.AppendAllText(Application.StartupPath + "\\ChuaNhapMaVachResult.txt", "\n"
                                    //        + " - SoChungTu: " + chungTuXuatNhapNccInfo.SoChungTu
                                    //        + " - PO: " + chungTuXuatNhapNccInfo.SoPO + " - PN: " + chungTuXuatNhapNccInfo.SoPhieuNhap);
                                    //}
                                }
                            }
                            catch (Exception ex)
                            {
                                File.AppendAllText(Application.StartupPath + "\\ImportResult.txt", "\n" + ex.Message
                                                   + " - PO: " + chungTuXuatNhapNccInfo.SoPO + " - PN: " + chungTuXuatNhapNccInfo.SoPhieuNhap);
                            }
                        }
                    }
                    frmProgress.Instance.PopStatus();
                    frmProgress.Instance.Value += 1;
                }

                frmProgress.Instance.Description = "Đã hoàn thành.";
                frmProgress.Instance.Value       = frmProgress.Instance.MaxValue;
                frmProgress.Instance.IsCompleted = true;
            }
            catch (Exception ex)
            {
#if DEBUG
                if (isComplete)
                {
                    MessageBox.Show(ex.ToString());
                }
#else
                if (isComplete)
                {
                    MessageBox.Show(ex.Message);
                }
#endif
                EventLogProvider.Instance.WriteLog(ex.ToString()
                                                   + "\nUser: "******"\nKho: " + Declare.IdKho,
                                                   this.Name);
            }
        }
        private void AutoGenChungTu1()
        {
            NhapNccBusiness khoBusiness;
            int             code      = 0;
            DMKhoInfo       dmKhoInfo = DMKhoDataProvider.GetKhoByIdInfo(Declare.IdKho);

            foreach (tmp_NhapHang_UserInfo tmpNhapHangUserInfo in lstDataSource)
            {
                frmProgress.Instance.Value += 1;
                ChungTuXuatNhapNccInfo chungTuXuatNhapNccInfo = tblChungTuDataProvider.GetChungTuNhapNCCFromSoPO(
                    tmpNhapHangUserInfo.SoPO, tmpNhapHangUserInfo.SoPhieuNhap,
                    Convert.ToInt32(TransactionType.NHAP_PO), Declare.IdKho, NgayLap, tmpNhapHangUserInfo.IdChungTu);

                if (chungTuXuatNhapNccInfo == null)
                {
                    chungTuXuatNhapNccInfo = new ChungTuXuatNhapNccInfo
                    {
                        SoChungTu   = CommonProvider.Instance.GetSoPhieu(Declare.Prefix.PhieuNhapHangMua),
                        NgayLap     = tmpNhapHangUserInfo.NgayNhap,
                        SoPO        = tmpNhapHangUserInfo.SoPO,
                        SoPhieuNhap = tmpNhapHangUserInfo.SoPhieuNhap,
                        LoaiChungTu = Convert.ToInt32(TransactionType.NHAP_PO),
                        IdDoiTuong  = tmpNhapHangUserInfo.IdDoiTuong,
                        IdKho       = Declare.IdKho
                    };
                }

                khoBusiness = new NhapNccBusiness(chungTuXuatNhapNccInfo);

                if (chungTuXuatNhapNccInfo.IdChungTu == 0)
                {
                    khoBusiness.ListChiTietChungTu = KeToanNhapNccDataProvider.Instance.GetListNhapHangUserInfoFromOid(khoBusiness.ChungTu.SoPO, khoBusiness.ChungTu.SoPhieuNhap, Convert.ToInt32(LoaiGiaoDichPO.NHAP_HANG_NHA_CUNG_CAP),
                                                                                                                       khoBusiness.ChungTu.NgayLap, chungTuXuatNhapNccInfo.IdChungTu);

                    foreach (ChungTuXuatNhapNccChiTietInfo chungTuXuatNhapNccChiTietInfo in khoBusiness.ListChiTietChungTu)
                    {
                        if (khoBusiness.ListChiTietHangHoa.Exists(delegate(ChungTuNhapNccChiTietHangHoaInfo match)
                        {
                            return(match.IdSanPham == chungTuXuatNhapNccChiTietInfo.IdSanPham &&
                                   match.TransactionID == chungTuXuatNhapNccChiTietInfo.TransactionID);
                        }))
                        {
                            continue;
                        }

                        //import ma vach tu file

                        dvFillter.RowFilter = String.Format("SoPO='{0}' and SoPhieuNhap='{1}' and MaSanPham='{2}' and MaKho='{3}'",
                                                            chungTuXuatNhapNccInfo.SoPO, chungTuXuatNhapNccInfo.SoPhieuNhap, chungTuXuatNhapNccChiTietInfo.MaSanPham, dmKhoInfo.MaKho);

                        DataTable dtTemp = dvFillter.ToTable();
                        foreach (DataRow dataRow in dtTemp.Rows)
                        {
                            ChungTuNhapNccChiTietHangHoaInfo chungTuNhapNccChiTietHangHoaInfo =
                                new ChungTuNhapNccChiTietHangHoaInfo
                            {
                                DonGia           = 0,
                                IdChungTuChiTiet = 0,
                                IdSanPham        = chungTuXuatNhapNccChiTietInfo.IdSanPham,
                                TransactionID    =
                                    chungTuXuatNhapNccChiTietInfo.TransactionID
                            };
                            chungTuNhapNccChiTietHangHoaInfo.MaVach  = Convert.ToString(dataRow["MaVach"]);
                            chungTuNhapNccChiTietHangHoaInfo.SoLuong = Convert.ToInt32(dataRow["SoLuong"]);
                            khoBusiness.ListChiTietHangHoa.Add(chungTuNhapNccChiTietHangHoaInfo);
                        }
                        #region Auto gen mavach
                        //tu sinh ma vach

                        //for (int i = 0; i < chungTuXuatNhapNccChiTietInfo.SoLuong; i++)
                        //{
                        //    ChungTuNhapNccChiTietHangHoaInfo chungTuNhapNccChiTietHangHoaInfo =
                        //        new ChungTuNhapNccChiTietHangHoaInfo
                        //            {
                        //                DonGia = 0,
                        //                IdChungTuChiTiet = 0,
                        //                IdSanPham = chungTuXuatNhapNccChiTietInfo.IdSanPham,
                        //                TransactionID =
                        //                    chungTuXuatNhapNccChiTietInfo.TransactionID,
                        //                SoLuong =
                        //                    chungTuXuatNhapNccChiTietInfo.TrungMaVach == 0
                        //                        ? 1
                        //                        : chungTuXuatNhapNccChiTietInfo.SoLuong,
                        //                MaVach =
                        //                    chungTuXuatNhapNccChiTietInfo.MaSanPham + Declare.IdKho + DateTime.Now.Day +
                        //                    code.ToString().PadLeft(5, '0')
                        //            };

                        //    khoBusiness.ListChiTietHangHoa.Add(chungTuNhapNccChiTietHangHoaInfo);

                        //code += 1;

                        //if (chungTuXuatNhapNccChiTietInfo.TrungMaVach == 1)
                        //{
                        //    break;
                        //}
                        //else
                        //{
                        //    HangHoa_ChiTietInfo hangHoaChiTietInfo = TblHangHoaChiTietDataProvider.GetHangHoaChiTietByMaVach(Declare.IdKho,
                        //                                                            chungTuXuatNhapNccChiTietInfo.
                        //                                                                IdSanPham,
                        //                                                            chungTuNhapNccChiTietHangHoaInfo
                        //                                                                .MaVach, 0);
                        //    if (hangHoaChiTietInfo != null)
                        //    {
                        //        chungTuNhapNccChiTietHangHoaInfo.MaVach =
                        //            chungTuXuatNhapNccChiTietInfo.MaSanPham + Declare.IdKho + DateTime.Now.Day +
                        //            code.ToString().PadLeft(5, '0');
                        //        code += 1;
                        //    }
                        //}
                        //}
                        #endregion
                    }
                    try
                    {
                        if (khoBusiness.ListChiTietHangHoa.Count > 0)
                        {
                            khoBusiness.SaveChungTu();
                        }
                        else
                        {
                            File.AppendAllText(Application.StartupPath + "\\ChuaNhapMaVachResult.txt", "\n"
                                               + " - SoChungTu: " + chungTuXuatNhapNccInfo.SoChungTu
                                               + " - PO: " + chungTuXuatNhapNccInfo.SoPO + " - PN: " + chungTuXuatNhapNccInfo.SoPhieuNhap);
                        }
                    }
                    catch (Exception ex)
                    {
                        File.AppendAllText(Application.StartupPath + "\\ImportResult.txt", "\n" + ex.Message
                                           + " - PO: " + chungTuXuatNhapNccInfo.SoPO + " - PN: " + chungTuXuatNhapNccInfo.SoPhieuNhap);
                    }
                }
                else //if (khoBusiness.ListChiTietHangHoa.Count == 0)
                {
                    DataTable dtTemp = null;
                    foreach (ChungTuXuatNhapNccChiTietInfo chungTuXuatNhapNccChiTietInfo in khoBusiness.ListChiTietChungTu)
                    {
                        if (khoBusiness.GetListChiTietHangHoaByIdSanPham(chungTuXuatNhapNccChiTietInfo.IdSanPham).Count < chungTuXuatNhapNccChiTietInfo.SoLuong)
                        {
                            dvFillter.RowFilter = String.Format("SoPO='{0}' and SoPhieuNhap='{1}' and MaSanPham='{2}' and MaKho='{3}'",
                                                                chungTuXuatNhapNccInfo.SoPO, chungTuXuatNhapNccInfo.SoPhieuNhap, chungTuXuatNhapNccChiTietInfo.MaSanPham, dmKhoInfo.MaKho);

                            dtTemp = dvFillter.ToTable();
                            foreach (DataRow dataRow in dtTemp.Rows)
                            {
                                string maVach = Convert.ToString(dataRow["MaVach"]);
                                if (khoBusiness.ListChiTietHangHoa.Exists(delegate(ChungTuNhapNccChiTietHangHoaInfo match)
                                {
                                    return(match.MaVach == maVach);
                                }))
                                {
                                    continue;
                                }
                                ChungTuNhapNccChiTietHangHoaInfo chungTuNhapNccChiTietHangHoaInfo =
                                    new ChungTuNhapNccChiTietHangHoaInfo
                                {
                                    DonGia           = 0,
                                    IdChungTuChiTiet = 0,
                                    IdSanPham        = chungTuXuatNhapNccChiTietInfo.IdSanPham,
                                    TransactionID    =
                                        chungTuXuatNhapNccChiTietInfo.TransactionID
                                };
                                chungTuNhapNccChiTietHangHoaInfo.MaVach  = maVach;
                                chungTuNhapNccChiTietHangHoaInfo.SoLuong = Convert.ToInt32(dataRow["SoLuong"]);

                                khoBusiness.ListChiTietHangHoa.Add(chungTuNhapNccChiTietHangHoaInfo);
                            }
                        }
                    }
                    try
                    {
                        if (dtTemp != null && dtTemp.Rows.Count > 0 && khoBusiness.ListChiTietHangHoa.Count > 0)
                        {
                            khoBusiness.SaveChungTu();
                        }
                        else
                        {
                            //try
                            //{
                            //    khoBusiness.DeleteChungTu();
                            //    tmp_NhapHang_LogDataProvider.Delete(chungTuXuatNhapNccInfo.SoPO, chungTuXuatNhapNccInfo.SoPhieuNhap, Convert.ToInt32(LoaiGiaoDichPO.NHAP_HANG_NHA_CUNG_CAP), Declare.IdKho);
                            //}
                            //catch (Exception)
                            //{
                            //    File.AppendAllText(Application.StartupPath + "\\ChuaNhapMaVachResult.txt", "\n"
                            //        + " - SoChungTu: " + chungTuXuatNhapNccInfo.SoChungTu
                            //        + " - PO: " + chungTuXuatNhapNccInfo.SoPO + " - PN: " + chungTuXuatNhapNccInfo.SoPhieuNhap);
                            //}
                        }
                    }
                    catch (Exception ex)
                    {
                        File.AppendAllText(Application.StartupPath + "\\ImportResult.txt", "\n" + ex.Message
                                           + " - PO: " + chungTuXuatNhapNccInfo.SoPO + " - PN: " + chungTuXuatNhapNccInfo.SoPhieuNhap);
                    }
                }
            }
            frmProgress.Instance.Description = "Đã hoàn thành.";
            frmProgress.Instance.Value       = frmProgress.Instance.MaxValue;
            frmProgress.Instance.IsCompleted = true;
        }