/// <summary> Lưu D02 </summary>
        public void ComputeD02(List<Ins_InsuranceReportD02Entity> lstD02, Ins_ReportD02Entity d02, DateTime dtMonthYear, DateTime dtStart, DateTime dtEnd, string periodD02)
        {
            using (var context = new VnrHrmDataContext())
            {
                var unitOfWork = (IUnitOfWork)(new UnitOfWork(context));
                var repoReportD02 = new Ins_ReportD02Repository(unitOfWork);
                var repoReportD02Item = new Ins_ReportD02ItemRepository(unitOfWork);
                var reportD02 = new Ins_ReportD02();
                var reportD02ID = Guid.Empty;
                var count = 0;
                count = repoReportD02.FindBy(p => p.IsDelete == null && p.DateMonth == dtMonthYear).Count();
                count++;

                //test
                count = 1;//test xoa sau

                //tự sinh ra kỳ
                var cutOfDuration = "Kỳ " + periodD02 + " - " + dtMonthYear.ToString(ConstantFormat.HRM_Format_MonthYear);
                var reportD02Existed = repoReportD02.FindBy(p => p.IsDelete == null && p.DateMonth.HasValue
                    && p.DateMonth.Value.Month == dtMonthYear.Month && p.DateMonth.Value.Year == dtMonthYear.Year
                    && p.ReportD02Name == cutOfDuration).FirstOrDefault();
                var d02ItemsExist = new List<Ins_ReportD02Item>();
                var d02ItemsModify = new List<Ins_ReportD02Item>();
                var d02ItemsAdding = new List<Ins_ReportD02Item>();
                #region Chỉnh Sửa
                if (reportD02Existed != null)
                {
                    var d02ID = reportD02Existed.ID;
                    reportD02ID = reportD02Existed.ID;

                    reportD02Existed.SociaInsCountPro = d02.SociaInsCountPro;
                    reportD02Existed.SociaInsTotalSalary = d02.SociaInsTotalSalary;
                    reportD02Existed.HealthInsCountPro = d02.HealthInsCountPro;
                    reportD02Existed.HealthInsTotalSalary = d02.HealthInsTotalSalary;
                    reportD02Existed.UnEmpInsCountPro = d02.UnEmpInsCountPro;
                    reportD02Existed.UnEmpInsTotalSalary = d02.UnEmpInsTotalSalary;
                    reportD02Existed.MaxSalary = d02.MaxSalary;
                    reportD02Existed.MinSalary = d02.MinSalary;
                    reportD02Existed.Type = d02.Type;
                    repoReportD02.Edit(reportD02Existed);
                    repoReportD02.SaveChanges();

                    var profileIds = lstD02.Select(m => m.ProfileID).ToList();
                    d02ItemsExist = unitOfWork.CreateQueryable<Ins_ReportD02Item>(Guid.Empty, p => profileIds.Contains(p.ProfileID) && p.ReportD02ID == d02ID && (p.IsPayBack == null || (p.IsPayBack != null && p.IsPayBack == false))).ToList();
                    if (d02ItemsExist.Any())
                    {
                        unitOfWork.RemoveObject(typeof(Ins_ReportD02Item), d02ItemsExist.ToArray());
                        unitOfWork.SaveChanges();
                    }
                    d02ItemsExist = repoReportD02Item.FindBy(p => p.IsDelete == null && p.ReportD02ID == d02ID && (p.IsPayBack == null || (p.IsPayBack != null && p.IsPayBack == false))).ToList();

                }
                #endregion

                #region Tạo mới
                if (lstD02.Count > 0 && reportD02Existed == null)
                {
                    //insert D02Report
                    reportD02 = new Ins_ReportD02
                    {
                        DateReport = DateTime.Now,
                        DateStart = dtStart,
                        DateEnd = dtEnd,
                        DateMonth = dtMonthYear,
                        ReportD02Name = cutOfDuration,
                        MaxSalary = d02.MaxSalary,
                        MinSalary = d02.MinSalary,
                        SociaInsCountPro = d02.SociaInsCountPro,
                        SociaInsTotalSalary = d02.SociaInsTotalSalary,
                        HealthInsCountPro = d02.HealthInsCountPro,
                        HealthInsTotalSalary = d02.HealthInsTotalSalary,
                        UnEmpInsCountPro = d02.UnEmpInsCountPro,
                        Type = d02.Type,
                        UnEmpInsTotalSalary = d02.UnEmpInsTotalSalary
                    };
                    repoReportD02.Add(reportD02);
                    repoReportD02.SaveChanges();
                    reportD02ID = reportD02.ID;
                }
                #endregion

                //get d02Item
                var d02ItemPayBack = unitOfWork.CreateQueryable<Ins_ReportD02Item>(Guid.Empty, m => m.ReportD02ID == reportD02ID && m.PayBackID != null).ToList();

                foreach (var insInsuranceReportD02Entity in lstD02)
                {
                    if (!insInsuranceReportD02Entity.ProfileID.HasValue || (insInsuranceReportD02Entity.ProfileID.HasValue && insInsuranceReportD02Entity.ProfileID.Value == Guid.Empty))
                    {
                        continue;
                    }

                    if (d02ItemPayBack.Where(m => m.ProfileID == insInsuranceReportD02Entity.ProfileID && m.PayBackID != null).FirstOrDefault() != null)
                    {
                        continue;
                    }

                    var notCardHeath = false;
                    bool.TryParse(insInsuranceReportD02Entity.NotCardHealth, out notCardHeath);
                    #region lay so thu tu
                    var itemOrder = 0;
                    var strItemOrder = string.Empty;
                    if (!string.IsNullOrEmpty(insInsuranceReportD02Entity.STT))
                    {
                        strItemOrder = insInsuranceReportD02Entity.STT.ToString();
                    }
                    int.TryParse(strItemOrder, out itemOrder);
                    #endregion
                    var reportD02Item = new Ins_ReportD02Item()
                    {
                        Comment = insInsuranceReportD02Entity.Comment,
                        ReportD02ID = reportD02ID,
                        ProfileID = insInsuranceReportD02Entity.ProfileID,
                        Type = insInsuranceReportD02Entity.Status1,
                        Status = insInsuranceReportD02Entity.Status,
                        RateHealthIns = insInsuranceReportD02Entity.RateBHYT,
                        RateSocialIns = insInsuranceReportD02Entity.RateBHXH,
                        RateUnEmpIns = insInsuranceReportD02Entity.RateBHTN,
                        NewBasicSalary = insInsuranceReportD02Entity.NewBasicSalary,
                        JobName = insInsuranceReportD02Entity.JobName,
                        Allowance1 = insInsuranceReportD02Entity.Allowance1,
                        Allowance2 = insInsuranceReportD02Entity.Allowance2,
                        Allowance3 = insInsuranceReportD02Entity.Allowance3,
                        AllowanceAdditional = insInsuranceReportD02Entity.AllowanceAdditional,
                        SocialInsPlaceID = insInsuranceReportD02Entity.SocialInsPlaceID,
                        NotCardHealth = notCardHeath,
                        ItemOrder = itemOrder,
                        MonthFrom = dtStart,
                        MonthTo = dtEnd
                    };

                    var d02ItemExisted = d02ItemsExist.Where(p => p.ProfileID == insInsuranceReportD02Entity.ProfileID && p.Status == insInsuranceReportD02Entity.Status).FirstOrDefault();
                    if (d02ItemExisted != null)
                    {
                        if (d02ItemExisted.Type != insInsuranceReportD02Entity.Status1
                            || d02ItemExisted.RateHealthIns != insInsuranceReportD02Entity.RateBHYT
                            || d02ItemExisted.RateSocialIns != insInsuranceReportD02Entity.RateBHXH
                            || d02ItemExisted.RateUnEmpIns != insInsuranceReportD02Entity.RateBHTN
                            || d02ItemExisted.NewBasicSalary != insInsuranceReportD02Entity.NewBasicSalary
                            || d02ItemExisted.NotCardHealth != notCardHeath
                            || d02ItemExisted.Comment != insInsuranceReportD02Entity.Comment
                            || d02ItemExisted.JobName != insInsuranceReportD02Entity.JobName
                            || d02ItemExisted.SocialInsPlaceID != insInsuranceReportD02Entity.SocialInsPlaceID)
                        {
                            d02ItemExisted.Type = insInsuranceReportD02Entity.Status1;
                            d02ItemExisted.RateHealthIns = insInsuranceReportD02Entity.RateBHYT;
                            d02ItemExisted.RateSocialIns = insInsuranceReportD02Entity.RateBHXH;
                            d02ItemExisted.RateUnEmpIns = insInsuranceReportD02Entity.RateBHTN;
                            d02ItemExisted.NewBasicSalary = insInsuranceReportD02Entity.NewBasicSalary;
                            d02ItemExisted.NotCardHealth = notCardHeath;
                            d02ItemExisted.Status = insInsuranceReportD02Entity.Status;
                            d02ItemExisted.Type = insInsuranceReportD02Entity.Status1;
                            d02ItemExisted.ItemOrder = itemOrder;
                            d02ItemExisted.MonthFrom = dtStart;
                            d02ItemExisted.MonthTo = dtEnd;
                            d02ItemExisted.JobName = insInsuranceReportD02Entity.JobName;
                            d02ItemExisted.Allowance1 = insInsuranceReportD02Entity.Allowance1;
                            d02ItemExisted.Allowance2 = insInsuranceReportD02Entity.Allowance2;
                            d02ItemExisted.Allowance3 = insInsuranceReportD02Entity.Allowance3;
                            d02ItemExisted.AllowanceAdditional = insInsuranceReportD02Entity.AllowanceAdditional;
                            d02ItemExisted.SocialInsPlaceID = insInsuranceReportD02Entity.SocialInsPlaceID;
                            d02ItemExisted.Comment = insInsuranceReportD02Entity.Comment;
                            d02ItemsModify.Add(d02ItemExisted);
                        }
                    }
                    else
                    {
                        d02ItemsAdding.Add(reportD02Item);
                    }
                }

                if (d02ItemsModify.Any())
                {
                    repoReportD02Item.Edit(d02ItemsModify);
                }
                if (d02ItemsAdding.Any())
                {
                    repoReportD02Item.Add(d02ItemsAdding);
                }

                if (d02ItemsModify.Any() || d02ItemsAdding.Any())
                {
                    //save change
                    repoReportD02Item.SaveChanges();
                }
            }
        }
        /// <summary> Compute D02 và xuat bao cao D02 </summary>
        /// <param name="all"></param>
        /// <param name="increase"></param>
        /// <param name="descrease"></param>
        /// <param name="dtMonthYear"></param>
        /// <param name="orgIds"></param>
        /// <returns></returns>
        public List<Ins_InsuranceReportD02Entity> LoadData(bool? all, bool? increase, bool? descrease, DateTime? dtMonthYear, string orgIds, string searchNoteType, string searchStatus, string codeEmp, List<Guid> socialInsPlaceIDs, string userLogin)
        {
            var monthYear = dtMonthYear ?? DateTime.Now;
            DateTime dateFrom = monthYear.AddMonths(-1);
            dateFrom = new DateTime(dateFrom.Year, dateFrom.Month, InsuranceServices.PeriodInsuranceDayPreMonthDefault);
            DateTime dateTo = new DateTime(monthYear.Year, monthYear.Month, InsuranceServices.PeriodInsuranceDayCurrentMonthDefault);
            var status = string.Empty;
            var allCheck = all ?? false;
            var inCreaseCheck = increase ?? false;
            var descreaseCheck = descrease ?? false;

            DateTime dateFromNewHONDA = new DateTime(dateFrom.Year, dateFrom.Month, PeriodInsuranceDayPreMonth);//18
            DateTime dateToNewHONDA = new DateTime(dateTo.Year, dateTo.Month, PeriodInsuranceDayCurrentMonth);//17
            //
            int? sociaInsCountPro = 0;
            double? sociaInsTotalSalary = 0;
            int? healthInsCountPro = 0;
            double? healthInsTotalSalary = 0;
            int? unEmpInsCountPro = 0;
            double? unEmpInsTotalSalary = 0;
            double? maxSalary = long.MinValue + 1;
            double? minSalary = long.MaxValue - 1;
            //

            //lay ds Ins_ProfileInsuranceMonthly
            //List<object> listInsMonthlyObj = new List<object>();
            //listInsMonthlyObj.Add(orgs);
            //listInsMonthlyObj.Add(monthYear);
            //var profileInsMonthlys = GetData<Hre_ProfileEntity>(listInsMonthlyObj, ConstantSql.hrm_ins_sp_get_ProfileInsMonthly, ref status);
            Hashtable htbData = new Hashtable();
            var d02Reports = new List<Ins_InsuranceReportD02Entity>();
            using (var context = new VnrHrmDataContext())
            {
                var unitOfWork = (IUnitOfWork)(new UnitOfWork(context));
                var profileInsMonthly = new Ins_ProfileInsuranceMonthlyRepository(unitOfWork);
                var orgStructureRepo = new Cat_OrgStructureRepository(unitOfWork);
                var insInsuranceRecord = new Ins_InsuranceRecordRepository(unitOfWork);
                var hre_ContractRepo = new Hre_ContractRepository(unitOfWork);
                var cat_ContractTypeRepo = new Cat_ContractTypeRepository(unitOfWork);
                var att_LeaveDayRepo = new Att_LeavedayRepository(unitOfWork);

                #region Lấy DS Ins_ProfileInsuranceMonthly (lấy tất cả)
                var listInsMonthlyObj = new List<object> { orgIds, null, null, 1, int.MaxValue - 1 };
                if (Common.UseDataBaseName == DATABASETYPE.SQLSERVER.ToString())
                {
                    listInsMonthlyObj.Add("id");
                }
                var lstProfileInsuranceMonthlyInDb = GetData<Ins_ProfileInsuranceMonthlyEntity>(listInsMonthlyObj, ConstantSql.hrm_ins_sp_get_ProfileInsMonthly, userLogin, ref status);
                #endregion

                //get profiles trong Ins_ProfileInsuranceMonthly theo thang kiểm tra
                var lstProfile = lstProfileInsuranceMonthlyInDb.Where(s => (s.IsDelete == null || s.IsDelete.Value==false) && s.MonthYear.HasValue
                    && (s.MonthYear.Value.Year == dateFrom.Year || s.MonthYear.Value.Year == dateTo.Year)
                    && (s.MonthYear.Value.Month == dateFrom.Month || s.MonthYear.Value.Month == dateTo.Month)
                    && s.MonthYearEffect.HasValue && s.MonthYear == s.MonthYearEffect).ToList().Translate<Ins_ProfileInsuranceMonthly>();

                #region lay ins_ProfileInsuranceMonthly de tinh thai san 1 nam lien ke
                //Lấy Ds Ins_ProfileInsuranceMonthly 12 tháng trước so với tháng kiểm tra (de tinh thai san 1 nam lien ke)
                List<Ins_ProfileInsuranceMonthlyEntity> lstProfilePregnant = null;
                #endregion


                //var lstProfile = profileInsMonthly.FindBy(s => s.IsDelete == null && s.MonthYear.HasValue
                //    && (s.MonthYear.Value.Year == dateFrom.Year || s.MonthYear.Value.Year == dateTo.Year)
                //    && (s.MonthYear.Value.Month == dateFrom.Month || s.MonthYear.Value.Month == dateTo.Month)).ToList();

                //get InsuranceRecord (Chứng từ bảo hiểm)
                var lstInsuranceRecord = insInsuranceRecord.GetAll().Where(p => p.IsDelete == null).ToList();// lay tat ca chứng từ bảo hiểm (sau nay sửa lai sau)


                #region Lay ds Phong Ban
                var orgs = GetDataNotParam<Cat_OrgStructure>(ConstantSql.hrm_cat_sp_get_AllOrg, userLogin, ref status).ToList();
                List<Cat_OrgStructureEntity> orgStructures = orgs.Translate<Cat_OrgStructureEntity>();
                #endregion

                #region Lấy profile theo phong ban
                List<object> listObj = new List<object>();
                listObj.Add(orgIds);
                listObj.Add(string.Empty);
                listObj.Add(string.Empty);
                var profilebyOrgs = GetData<Hre_ProfileEntity>(listObj, ConstantSql.hrm_hr_sp_get_ProfileIdsByOrg, userLogin, ref status);
                if (!string.IsNullOrEmpty(codeEmp))
                {
                    profilebyOrgs = profilebyOrgs.Where(p => p.CodeEmp == codeEmp).ToList();
                }
                else if (profilebyOrgs != null)
                {
                    profilebyOrgs = profilebyOrgs.Where(p => p.DateHire != null
                                && (p.DateQuit == null || p.DateQuit > dateFrom)
                                && p.DateHire <= monthYear).ToList();
                }


                var profileIds = profilebyOrgs.Select(p => p.ID).ToList();
                #endregion

                #region  lay ds hop dong đã ký so với tháng chọn (Loại HD có đóng BH)
                var contractTypeObjs = cat_ContractTypeRepo.FindBy(p => p.IsDelete == null).ToList();
                //Ds loại HĐ đóng BHXH
                var contractSocialTypeIDObjs = contractTypeObjs.Where(p => p.IsSocialInsurance.HasValue && p.IsSocialInsurance.Value).ToList();
                //Ds loại HĐ đóng BHTN
                var contractUnEmpTypeIDObjs = contractTypeObjs.Where(p => p.IsUnEmployInsurance.HasValue && p.IsUnEmployInsurance.Value).ToList();
                var contracts = new List<Hre_Contract>();
                var contractSocials = new List<Hre_Contract>();
                var contractUnEmps = new List<Hre_Contract>();
                if (contractSocialTypeIDObjs.Any())
                {
                    var contractSocialTypeIds = contractSocialTypeIDObjs.Select(p => p.ID).ToList();
                    var contractUnEmpTypeIds = contractUnEmpTypeIDObjs.Select(p => p.ID).ToList();
                    //Ds Hop Đồng của cac NV
                    contracts = hre_ContractRepo.FindBy(p => p.IsDelete == null && profileIds.Contains(p.ProfileID)).OrderBy(p => p.DateStart).ToList();
                    //Ds Hop đồng thuộc loại đóng BHXH
                    contractSocials = contracts.Where(p => contractSocialTypeIds.Contains(p.ContractTypeID)).OrderBy(p => p.DateStart).ToList();
                    //Ds Hop đồng thuộc loại đóng BHTN
                    contractUnEmps = contracts.Where(p => contractUnEmpTypeIds.Contains(p.ContractTypeID)).OrderBy(p => p.DateStart).ToList();
                }

                #endregion

                #region lấy Leaveday
                string E_APPROVED = LeaveDayStatus.E_APPROVED.ToString();
                string E_PREGNANCY_SUCKLE = InsuranceRecordType.E_PREGNANCY_SUCKLE.ToString();

                var lstLeaveday = att_LeaveDayRepo.FindBy(m => m.IsDelete == null && m.Status == E_APPROVED && m.DateStart <= dateTo && m.DateEnd >= dateFrom && m.Cat_LeaveDayType != null
                    && m.Cat_LeaveDayType.InsuranceType == E_PREGNANCY_SUCKLE && profileIds.Contains(m.ProfileID)).ToList();
                #endregion

                List<object> salInsuranceSalaryParams = new List<object>();
                salInsuranceSalaryParams.Add(null);
                salInsuranceSalaryParams.Add(null);
                salInsuranceSalaryParams.Add(orgIds);
                salInsuranceSalaryParams.Add(null);
                salInsuranceSalaryParams.Add(null);
                salInsuranceSalaryParams.Add(null);
                salInsuranceSalaryParams.Add(null);
                salInsuranceSalaryParams.Add(1);
                salInsuranceSalaryParams.Add(Int32.MaxValue - 1);
                var dateEnd = new DateTime(dateTo.Year, dateTo.Month, DateTime.DaysInMonth(dateTo.Year, dateTo.Month));
                var lstInsuranceSalaryByProfile = GetData<Sal_InsuranceSalary>(salInsuranceSalaryParams, ConstantSql.hrm_sal_sp_get_InsuranceSalary, userLogin, ref status)
                    .Where(p => p.DateEffect <= dateEnd && profileIds.Contains(p.ProfileID ?? Guid.Empty) && p.InsuranceAmount.HasValue).ToList();

                if (socialInsPlaceIDs != null)
                {
                    socialInsPlaceIDs = socialInsPlaceIDs.Where(p => p != null && p != Guid.Empty).ToList();
                    if (socialInsPlaceIDs != null && socialInsPlaceIDs.Any())
                    {
                        lstProfile = lstProfile.Where(p => socialInsPlaceIDs != null && socialInsPlaceIDs.Count > 0 && socialInsPlaceIDs.Contains(p.SocialInsPlaceID ?? Guid.Empty)).ToList();
                    }
                }



                foreach (var profile in profilebyOrgs)
                {
                    //lay các chung tu bao hiem cua nv
                    List<Ins_InsuranceRecord> lstInsRecordProfile = lstInsuranceRecord.Where(rec => rec.ProfileID == profile.ID).ToList();
                    var lstleavePregByProfile = lstLeaveday.Where(rec => rec.ProfileID == profile.ID).ToList();
                    var lstInsRecordPregByProfile = lstInsRecordProfile.Where(m => m.InsuranceType == E_PREGNANCY_SUCKLE).ToList();


                    var profileMonthlys = lstProfile.Where(p => p.ProfileID == profile.ID).OrderBy(p => p.MonthYear).ToList();



                    var inFirst = profileMonthlys.FirstOrDefault(p => p.MonthYear.HasValue && p.MonthYear.Value.Month == dateFrom.Month);
                    var inNow = profileMonthlys.LastOrDefault(p => p.MonthYear.HasValue && p.MonthYear.Value.Month == dateTo.Month);

                    if (inFirst == null)
                    {
                        inFirst = new Ins_ProfileInsuranceMonthly
                        {
                            IsHealthInsurance = false,
                            IsSocialInsurance = false,
                            IsUnEmpInsurance = false
                        };
                    }

                    if (inNow != null)
                    {
                        profile.JobName = inNow.JobName;
                        #region set Phu cap
                        if (inNow != null)
                        {
                            profile.Allowance1 = inNow.Allowance1;
                            profile.Allowance2 = inNow.Allowance2;
                            profile.Allowance3 = inNow.Allowance3;
                            profile.Allowance4 = inNow.Allowance4;
                        }
                        #endregion

                        #region Thống kê số lượng người đóng BHXH,BHYT,BHTN và tổng lương BHXH,BHTN,BHTN
                        if (inNow.IsSocialInsurance.HasValue && inNow.IsSocialInsurance.Value)
                        {
                            sociaInsCountPro++;
                            sociaInsTotalSalary += inNow.MoneySocialInsurance;
                        }
                        if (inNow.IsHealthInsurance.HasValue && inNow.IsHealthInsurance.Value)
                        {
                            healthInsCountPro++;
                            healthInsTotalSalary += inNow.MoneyHealthInsurance;
                        }
                        if (inNow.IsUnEmpInsurance.HasValue && inNow.IsUnEmpInsurance.Value)
                        {
                            unEmpInsCountPro++;
                            unEmpInsTotalSalary += inNow.MoneyUnEmpInsurance;
                        }
                        if (inNow.SalaryInsurance.HasValue && inNow.SalaryInsurance.Value > maxSalary)
                        {
                            maxSalary = inNow.SalaryInsurance.Value;
                        }
                        if (inNow.SalaryInsurance.HasValue && inNow.SalaryInsurance.Value < minSalary)
                        {
                            minSalary = inNow.SalaryInsurance.Value;
                        }
                        #endregion
                        
                        #region tang

                        if (IsIncreaseInsurance(inFirst, inNow) && (inCreaseCheck == true || allCheck == true))
                        {
                            String strPre = InsuranceRecordType.E_PREGNANCY_SUCKLE.ToString();
                            List<Ins_InsuranceRecord> lstInsPre = lstInsRecordProfile.Where(ins => ins.InsuranceType == strPre && ins.DateEnd > dateFrom && ins.DateEnd < dateTo).ToList();
                            String strSickLong = InsuranceRecordType.E_SICK_LONG.ToString();
                            String strSickShort = InsuranceRecordType.E_SICK_SHORT.ToString();
                            List<Ins_InsuranceRecord> lstInsSick = lstInsRecordProfile.Where(ins => ins.InsuranceType == strSickLong || ins.InsuranceType == strSickShort && ins.DayCount > 30
                                && ins.DateEnd > dateFrom && ins.DateEnd < dateTo).ToList();

                            #region Tăng LĐ
                            //lay ds hop dong cua NV
                            var contractProfile = contractSocials.Where(p => p.ProfileID == profile.ID).OrderBy(p => p.DateStart).FirstOrDefault();
                            Hre_Contract contract = null;
                            //lay hd moi nhat cua nv nằm trong khoảng 16-[N-1] đến 15-[N]
                            if (contractProfile != null && dateFromNewHONDA <= contractProfile.DateStart && contractProfile.DateStart <= dateToNewHONDA)
                            {
                                contract = contractProfile;
                            }

                            #region Tăng do Nghỉ 14 ngày đi làm lại

                            //Kiểm tra Tháng N-1 có ký HD ?    
                            //tang moi lao dong (Tháng N-1 ký HD nhưng chưa đóng BH do nghỉ hơn 14 ngày , Tháng N đóng bảo hiểm => thuoc loại tăng mới LĐ
                            if (inFirst.IsDecreaseWorkingDays.HasValue && inFirst.IsDecreaseWorkingDays.Value)
                            {
                                var contractDatefrom = new DateTime(dateFrom.Year, dateFrom.Month, 1);
                                var contractDateTo = contractDatefrom.AddMonths(1).AddDays(-1);
                                if (contractProfile != null && contractDatefrom <= contractProfile.DateStart && contractProfile.DateStart <= contractDateTo)
                                {
                                    //ký hợp đồng tháng trước
                                    SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_TANG.ToString(), TypeInsuranceD02TS.E_TANG_LD.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "I.1");
                                }
                                else
                                {
                                    //tang do nghỉ 14 ngày đi làm lại
                                    SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_TANG.ToString(), TypeInsuranceD02TS.E_TANG_LEAVE_14WORKINGDAYS.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "I.1");
                                }
                            }
                            #endregion

                            else if (contract != null)
                            {
                                //lay hd moi nhat cua nv nằm trong khoảng 16-[N-1] đến 15-[N]=>tăng lao động
                                SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_TANG.ToString(), TypeInsuranceD02TS.E_TANG_LD.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "I.1");
                            }

                            #endregion

                            #region Tăng Thai Sản
                            else if (HasPregnant(dateTo.AddMonths(-1), lstleavePregByProfile, lstInsRecordPregByProfile))
                            {
                                SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_TANG.ToString(), TypeInsuranceD02TS.E_TANG_TS.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "I.1");
                            }
                            #endregion

                            #region Tăng Bệnh
                            else if (lstInsSick.Count() > 0 /* && allCheck == false*/)
                            {
                                SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_TANG.ToString(), TypeInsuranceD02TS.E_TANG_BENH.ToString(), inFirst.MoneySocialInsurance, inNow.MoneySocialInsurance, monthYear, orgStructures, "I.1");
                            }
                            #endregion

                            #region Honda - Tăng Đóng BHTN
                            else if ((!inFirst.IsUnEmpInsurance.HasValue || (inFirst.IsUnEmpInsurance.HasValue &&
                              !inFirst.IsUnEmpInsurance.Value))
                              && inNow.IsUnEmpInsurance.HasValue && inNow.IsUnEmpInsurance.Value)
                            {
                                var contractUnEmpProfile = contractUnEmps.Where(p => p.ProfileID == profile.ID).OrderBy(p => p.DateStart).FirstOrDefault();
                                if (contractUnEmpProfile != null && dateFrom <= contractUnEmpProfile.DateStart && contractUnEmpProfile.DateStart <= dateTo)
                                {
                                    SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_TANG.ToString(), TypeInsuranceD02TS.E_TANG_BHTN.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "I.2");
                                }
                            }
                            #endregion

                            #region Tăng BHYT
                            else if ((!inFirst.IsHealthInsurance.HasValue || (inFirst.IsHealthInsurance.HasValue &&
                               !inFirst.IsHealthInsurance.Value))
                               && inNow.IsHealthInsurance.HasValue && inNow.IsHealthInsurance.Value)
                            {
                                SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_TANG.ToString(), TypeInsuranceD02TS.E_TANG_BHYT.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "I.1");
                            }
                            #endregion

                        }
                        #endregion

                        #region giam
                        if (IsDecreaseInsurance(inFirst, inNow) && (descreaseCheck == true || allCheck == true))
                        {
                            String strPre = InsuranceRecordType.E_PREGNANCY_SUCKLE.ToString();
                            List<Ins_InsuranceRecord> lstInsPre = lstInsRecordProfile.Where(ins => ins.InsuranceType == strPre && ins.DateEnd > dateFrom && ins.DateStart < dateTo).ToList();
                            String strSickLong = InsuranceRecordType.E_SICK_LONG.ToString();
                            String strSickShort = InsuranceRecordType.E_SICK_SHORT.ToString();
                            List<Ins_InsuranceRecord> lstInsSick = lstInsRecordProfile.Where(ins => ins.InsuranceType == strSickLong && ins.InsuranceType == strSickShort
                                                                                                   && ins.DayCount > 30
                                                                                                   && ins.DateEnd > dateFrom && ins.DateEnd < dateTo).ToList();
                            if (inFirst.IsDecreaseWorkingDays == null)
                            {
                                inFirst.IsDecreaseWorkingDays = false;
                            }
                            if (inNow.IsDecreaseWorkingDays == null)
                            {
                                inNow.IsDecreaseWorkingDays = false;
                            }
                            #region giam

                            //neu NV nghỉ từ 16 tháng [N-1] đến 15 tháng N => Tạm Hoãn hoặc nghỉ luôn
                            if (profile.DateQuit.HasValue && dateFrom <= profile.DateQuit && profile.DateQuit <= dateTo)
                            {
                                #region Set trạng thái trả thẻ
                                var statusGiam_BHYT = string.Empty;
                                if (profile.ReceiveHealthIns.HasValue && profile.ReceiveHealthIns.Value)
                                {
                                    // Giam LD tra the 
                                    statusGiam_BHYT = TypeInsuranceD02TS.E_GIAM_LD_BHYT.ToString();
                                }
                                else if ((!profile.ReceiveHealthIns.HasValue || profile.ReceiveHealthIns == false))
                                {
                                    // Giảm Lao Động Không Trả Thẻ
                                    statusGiam_BHYT = TypeInsuranceD02TS.E_GIAM_LD_NOT_BHYT.ToString();
                                }
                                else
                                {
                                    // Giảm LĐ 
                                    statusGiam_BHYT = TypeInsuranceD02TS.E_GIAM_LD.ToString();
                                }
                                //HonDa
                                statusGiam_BHYT = TypeInsuranceD02TS.E_GIAM_LD_BHYT.ToString();
                                #endregion

                                if (profile.StopWorkType == EnumDropDown.StopWorkType.E_SUSPENSE.ToString())
                                {
                                    //tạm hoãn
                                    SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM.ToString(), TypeInsuranceD02TS.E_GIAM_QUIT_SUSPENSE.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "II.1");
                                }
                                else if (!string.IsNullOrEmpty(statusGiam_BHYT))
                                {
                                    #region Nghỉ Việc Sau Khi Nghỉ Sinh Con
                                    if (HasPregnant(dateTo.AddMonths(-1), lstleavePregByProfile, lstInsRecordPregByProfile))
                                    {
                                        SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM.ToString(), TypeInsuranceD02TS.E_GIAM_TS_QUIT.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "II.1");
                                    }
                                    else
                                    {
                                        SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM.ToString(), statusGiam_BHYT, inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "II.1");
                                    }
                                    #endregion
                                }
                            }

                            #region Giam Thai San
                            else if (HasPregnant(dateTo, lstleavePregByProfile, lstInsRecordPregByProfile) 
                                && (!profile.DateQuit.HasValue || profile.DateQuit.Value > dateTo))
                            {
                                if (lstProfilePregnant == null)
                                {
                                    //chỉ lay 1 lần
                                    #region lay ins_ProfileInsuranceMonthly de tinh thai san 1 nam lien ke
                                    var dtPreDateCheck = dateFrom.AddYears(-1);
                                    //Lấy Ds Ins_ProfileInsuranceMonthly 12 tháng trước so với tháng kiểm tra (de tinh thai san 1 nam lien ke)
                                    lstProfilePregnant = lstProfileInsuranceMonthlyInDb.Where(s => s.MonthYear.HasValue
                                        && (dtPreDateCheck <= s.MonthYear.Value && s.MonthYear <= dateFrom)).ToList();
                                    #endregion
                                }

                                // Nhân viên đóng bảo hiểm đủ 6 tháng trong vòng 12 tháng liền kề
                                var insCount = lstProfilePregnant.Where(p => p.ProfileID == profile.ID && p.IsSocialInsurance.HasValue && p.IsSocialInsurance.Value).Count();
                                if (insCount >= 6)
                                {
                                    SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM.ToString(), TypeInsuranceD02TS.E_GIAM_TS.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "II.1");
                                }
                                else
                                {
                                    SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM.ToString(), TypeInsuranceD02TS.E_GIAM_LEAVE_14WORKINGDAYS.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "II.1");
                                }
                            }

                            #endregion

                            #region Giam benh
                            else if (lstInsSick.Count() > 0 /*&& allCheck == false*/)
                            {
                                SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM.ToString(), TypeInsuranceD02TS.E_GIAM_BENH.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "II.1");
                            }
                            #endregion

                            #region giam do Nghỉ 14 ngày đi làm lại

                            // tháng N là false (hoac null) => Giảm do nghỉ >= 14 ngày                            
                            else if (inNow.IsDecreaseWorkingDays.HasValue && inNow.IsDecreaseWorkingDays.Value)
                            {
                                SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM.ToString(), TypeInsuranceD02TS.E_GIAM_LEAVE_14WORKINGDAYS.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "II.1");
                            }
                            #endregion

                            #endregion
                        }//ket thuc giảm
                        #endregion

                        #region Thay doi luong

                        else if (inFirst != null && inFirst.SalaryInsurance != inNow.SalaryInsurance)
                        {
                            //-------Giam luong
                            if (inFirst != null && inFirst.SalaryInsurance > inNow.SalaryInsurance)
                            {
                                #region giảm lương và thay đổi chức danh nghề
                                if (inFirst.JobName != inNow.JobName)
                                {
                                    //giảm lương và thay đổi chức danh nghề
                                    SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM_LUONG_CHANGEJOBNAME.ToString(), TypeInsuranceD02TS.E_GIAM_LUONG_CHANGEJOBNAME.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "IV.");
                                }
                                #endregion
                                #region Giảm Lương
                                else
                                {
                                    //giảm lương
                                    SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM.ToString(), TypeInsuranceD02TS.E_GIAM_LUONG.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "IV.");
                                }
                                #endregion
                            }
                            //------------Tang luong-----------
                            else if (inFirst != null && inFirst.SalaryInsurance < inNow.SalaryInsurance)
                            {
                                #region tăng lương và thay đổi chức danh nghề
                                if (inFirst.JobName != inNow.JobName)
                                {
                                    //tăng lương và thay đổi chức danh nghề
                                    SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_TANG_LUONG_CHANGEJOBNAME.ToString(), TypeInsuranceD02TS.E_TANG_LUONG_CHANGEJOBNAME.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "III.");
                                }
                                #endregion
                                #region tăng lương
                                else
                                {
                                    //tăng lương 
                                    SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_TANG.ToString(), TypeInsuranceD02TS.E_TANG_LUONG.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "III.");
                                }
                                #endregion
                            }
                        }
                        #endregion

                        #region Thay đổi chức danh nghề (không đổi lương)
                        //Thay đổi chức danh nghề (không đổi lương)
                        if (inFirst != null && inNow != null && inFirst.SalaryInsurance == inNow.SalaryInsurance && inFirst.JobName != inNow.JobName)
                        {
                            SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_CHANGEJOBNAME.ToString(), TypeInsuranceD02TS.E_CHANGEJOBNAME.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "V.");
                        }
                        #endregion

                        #region Trường Hợp đặc biệt (Honda)

                        #region Tháng N-1 nghỉ Thai Sản và tháng N nghỉ luon =>Nghỉ luôn sau thai sản
                        if (inFirst != null && inFirst.IsPregnant.HasValue && inFirst.IsPregnant.Value
                            && inNow != null && (inNow.IsPregnant == null || inNow.IsPregnant.Value == false)
                            && profile.DateQuit.HasValue && dateFrom <= profile.DateQuit && profile.DateQuit <= dateTo)                       
                        {
                            //>Nghỉ luôn sau thai sản
                            SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM.ToString(), TypeInsuranceD02TS.E_GIAM_TS_QUIT.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "II.1");
                        }
                        #region Tháng N-1 nghỉ Thai Sản và tháng N nghỉ hơn 14 ngày => Nghỉ 14 ngày sau thai sản
                        else if (inNow != null && inNow.IsDecreaseWorkingDays.HasValue && inNow.IsDecreaseWorkingDays.Value
                            && HasPregnant(dateTo.AddMonths(-1), lstleavePregByProfile, lstInsRecordPregByProfile)
                            && (inNow.IsPregnant == null || inNow.IsPregnant.Value == false))
                        {
                            //Giảm do nghỉ hơn 14 ngày sau thai sản
                            SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM.ToString(), TypeInsuranceD02TS.E_GIAM_PREGNANT_14WORKINGDAYS.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "II.1");
                        }
                        #endregion
                        #endregion

                        #region tháng N-1 giảm 14 ngày và tháng này nghỉ việc
                        if (profile != null && inFirst != null && inNow != null
                            && !HasPregnant(dateTo.AddMonths(-1), lstleavePregByProfile, lstInsRecordPregByProfile)
                            && inFirst.IsDecreaseWorkingDays.HasValue && inFirst.IsDecreaseWorkingDays.Value
                            && profile.DateQuit.HasValue && dateFrom <= profile.DateQuit && profile.DateQuit <= dateTo)
                        {
                            //Nghỉ việc mà trước đó nghỉ >=14 ngày
                            SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM.ToString(), TypeInsuranceD02TS.E_GIAM_LEAVE_PREMONTH_14WORKINGDAYS.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "II.1");
                        }
                        #endregion

                      
                        
                        #region Nghiệp Vụ Chuyển Nơi Đóng BH (Honda)
                        if (inFirst != null && inNow != null 
                            && (inFirst.SocialInsPlaceID.HasValue && inFirst.SocialInsPlaceID.Value != Guid.Empty) 
                            && (inNow.SocialInsPlaceID.HasValue && inNow.SocialInsPlaceID.Value != Guid.Empty)
                            && (inFirst.SocialInsPlaceID.Value != inNow.SocialInsPlaceID.Value))
                        {
                            //thang truoc giam do chuyen noi dong bao hiem
                            profile.JobName = string.Copy(inFirst.JobName);
                            SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_TANG.ToString(), TypeInsuranceD02TS.E_TANG_LD_CHANGE_INSPLACE.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "I.1");
                            //thang check tang do chuyen noi dong bao hiem
                            profile.JobName = string.Copy(inNow.JobName);
                            SetHashtableData(htbData, profile, ref d02Reports, TypeInsuranceD02TS.E_GIAM.ToString(), TypeInsuranceD02TS.E_GIAM_LD_CHANGE_INSPLACE.ToString(), inFirst.SalaryInsurance, inNow.SalaryInsurance, monthYear, orgStructures, "II.1");
                        }
                        #endregion

                        #endregion

                    }
                }//ket thuc foreach profile
            }

            //duyet hashtable => danh sach nv theo loai bao hiem (tangLd,tangBHXH,.....)
            var result = new List<Ins_InsuranceReportD02Entity>();
            var tang = TypeInsuranceD02TS.E_TANG.ToString();
            var giam = TypeInsuranceD02TS.E_GIAM.ToString();
            var tangLD = TypeInsuranceD02TS.E_GIAM_LD.ToString();
            foreach (DictionaryEntry entry in htbData)
            {
                var lst = new List<Ins_InsuranceReportD02Entity>();
                lst = (List<Ins_InsuranceReportD02Entity>)entry.Value;
                var d02Entity = lst.FirstOrDefault(p => p.OrderGroup != null);
                var orderGroup = string.Empty;
                if (d02Entity != null)
                {
                    orderGroup = d02Entity.OrderGroup;
                }

                var proName = entry.Key.ToString();
                if (!string.IsNullOrEmpty(proName))
                {
                    proName = GetDescription(proName);
                }
                var title = new Ins_InsuranceReportD02Entity()
                {
                    ProfileID = Guid.Empty,
                    ProfileName = proName,
                    OrderGroup = orderGroup,
                    CodeEmp = orderGroup,
                    BOLD = true.ToString()
                };

                lst.Insert(0, title);
                if (lst.Any(p => p.Status1 == tang) && !result.Any(p => p.Status == tangLD) && !result.Any(p => p.ProfileName == "Tăng"))
                {
                    lst.Insert(0, new Ins_InsuranceReportD02Entity { ProfileID = Guid.Empty, ProfileName = "Tăng", CodeEmp = "I", OrderGroup = "I" });
                }
                if (lst.Any(p => p.Status1 == giam) && !result.Any(p => p.ProfileName == "Giảm"))
                {
                    lst.Insert(0, new Ins_InsuranceReportD02Entity { ProfileID = Guid.Empty, ProfileName = "Giảm", CodeEmp = "II", OrderGroup = "II" });
                }
                //if (lst.Any(p => p.Status == TypeInsuranceD02TS.E_GIAM_LD.ToString()) && !result.Any(p => p.Status1 == giam) && !result.Any(p => p.ProfileName == "Giảm Lao Động"))
                //{
                //    lst.Insert(0, new Ins_InsuranceReportD02Entity { ProfileName = "Giảm Lao Động", CodeEmp = "II", OrderGroup = "II" });
                //}

                result.AddRange(lst);
            }

            result = result.OrderBy(p => p.OrderGroup).ToList();

            #region compute D02

            var periodIns = EnumDropDown.GetEnumDescription<PeriodInsurance>(PeriodInsurance.GETTEMP);
            var d02 = new Ins_ReportD02Entity
            {
                DateReport = DateTime.Now,
                SociaInsCountPro = sociaInsCountPro,
                SociaInsTotalSalary = sociaInsTotalSalary,
                HealthInsCountPro = healthInsCountPro,
                HealthInsTotalSalary = healthInsTotalSalary,
                UnEmpInsCountPro = unEmpInsCountPro,
                UnEmpInsTotalSalary = unEmpInsTotalSalary,
                MaxSalary = maxSalary,
                MinSalary = minSalary,
                Type = PeriodInsurance.GETTEMP.ToString()
            };
            //luu D02
            ComputeD02(result, d02, monthYear, dateFrom, dateTo, periodIns);
            #endregion

            return result;
        }