Пример #1
0
        public void CalculateInsurancePayBack(List<Guid> payBackIds, DateTime monthYear,string userLogin)
        {
            /*
            *  Goal(tính toán Truy Lĩnh Bảo Hiểm - Lưu bảng Ins_ProfileInsuranceMonthly)
            *  Steps :
            *      Step1  :  lấy ds InsuranceSalaryPayback
            *      Step2  :  Duyệt InsuranceSalaryPayback 
            *      Step3  :  Duyệt fromMonthEffect đến ToMonthEffect (để insert vào Ins_ProfileMonthly tương ứng với những tháng đó)
            *      Step3  :  Lưu vào DB
            */


            using (var context = new VnrHrmDataContext())
            {
                var unitOfWork = (IUnitOfWork)(new UnitOfWork(context));
                var repoInsMonthly = new Ins_ProfileInsuranceMonthlyRepository(unitOfWork);
                var repoInsReportD02 = new Ins_ReportD02Repository(unitOfWork);
                var repoInsReportD02Item = new Ins_ReportD02ItemRepository(unitOfWork);
                var repoInsuranceSalaryPayback = new Ins_InsuranceSalaryPaybackRepository(unitOfWork);
                string status = string.Empty;
                DateTime endDayOfMonthYear = new DateTime(monthYear.Year, monthYear.Month, DateTime.DaysInMonth(monthYear.Year, monthYear.Month));
                var reportD02 = new Ins_ReportD02();
                List<Ins_ProfileInsuranceMonthly> lstProfileMonthly = new List<Ins_ProfileInsuranceMonthly>();
                List<Ins_ProfileInsuranceMonthly> lstProfileMonthlyEditing = new List<Ins_ProfileInsuranceMonthly>();
                List<Ins_ReportD02Item> lstD02Item = new List<Ins_ReportD02Item>();
                List<Ins_ReportD02Item> lstD02ItemEditing = new List<Ins_ReportD02Item>();
                var insPayBacks = unitOfWork.CreateQueryable<Ins_InsuranceSalaryPayback>(Guid.Empty, m => payBackIds.Contains(m.ID)).ToList();
                var insTypeD02Ids = insPayBacks.Select(m => m.TypeID).ToList();
                var insTypeD02s = unitOfWork.CreateQueryable<Ins_TypeD02>(Guid.Empty, m => insTypeD02Ids.Contains(m.ID)).ToList();
                //lấy tỉ lệ bảo hiểm với ngày áp dụng trước ngày kiểm tra
                var lstInsRate = unitOfWork.CreateQueryable<Cat_RateInsurance>(Guid.Empty, m => m.ApplyFrom <= endDayOfMonthYear).ToList();
                Cat_RateInsurance rateInsurance = GetInsuranceRate(endDayOfMonthYear, lstInsRate);
                var insMonthYearFrom = monthYear.AddMonths(-1);
                insMonthYearFrom = new DateTime(insMonthYearFrom.Year, insMonthYearFrom.Month,PeriodInsuranceDayPreMonthDefault);
                var insMonthYearTo = new DateTime(monthYear.Year, monthYear.Month,PeriodInsuranceDayCurrentMonthDefault );

                var beginMonthYear = insPayBacks.Min(m => m.FromMonthEffect);
                var endMonthYear = insPayBacks.Max(m => m.ToMonthEffect);

                #region ds NV đóng Bảo Hiểm theo thang
                var listInsMonthlyObj = new List<object> { null, beginMonthYear, endMonthYear, null, null, null, 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_ProfileInsMonthlyFromTo,userLogin, ref status).Translate<Ins_ProfileInsuranceMonthly>(); ;
                var profileIds = insPayBacks.Select(m => m.ProfileID).ToList();
                var hreProfiles = unitOfWork.CreateQueryable<Hre_Profile>(Guid.Empty, m => m.SocialInsPlaceID.HasValue && profileIds.Contains(m.ID)).Select(m => new { ID = m.ID, SocialInsPlaceID = m.SocialInsPlaceID.Value }).ToList();

                if (profileIds.Any())
                {
                    lstProfileInsuranceMonthlyInDb = lstProfileInsuranceMonthlyInDb.Where(m => profileIds.Contains(m.ProfileID)).ToList();
                }

                #endregion

                foreach (var payBack in insPayBacks)
                {
                    if (payBack.FromMonthEffect.HasValue && payBack.ToMonthEffect.HasValue)
                    {
                        payBack.IsCallPayBack = true;
                        //duyet tung nv
                        var profileID = payBack.ProfileID;

                        #region xoa truoc khi tinh toan truy lĩnh
                        var profMonthlyDelete = unitOfWork.CreateQueryable<Ins_ProfileInsuranceMonthly>(Guid.Empty, m => m.ProfileID == profileID
                            && m.MonthYear == payBack.MonthYear && (m.IsPayback == null || (m.IsPayback.HasValue && m.IsPayback.Value ==false)) ).FirstOrDefault();
                        if (profMonthlyDelete != null)
                        {
                            unitOfWork.RemoveObject(profMonthlyDelete);
                            unitOfWork.SaveChanges();
                        }

                        #endregion

                        #region lấy nơi đóng bảo hiểm của NV
                        var prof = hreProfiles.Where(m => m.ID == profileID).FirstOrDefault();
                        Guid? socialInsPlaceID = null;
                        if (prof != null)
                        {
                            socialInsPlaceID = prof.SocialInsPlaceID;
                        }
                        #endregion

                        for (var i = payBack.FromMonthEffect.Value; i <= payBack.ToMonthEffect.Value; i = i.AddMonths(1))
                        {
                            #region Ins_ProfileInsuranceMonthly
                            #region edit
                            var monthlyExist = lstProfileInsuranceMonthlyInDb.Where(m => m.ProfileID == profileID && m.MonthYearEffect == i && m.IsPayback == true).FirstOrDefault();
                            if (monthlyExist != null)
                            {
                                monthlyExist.IsSocialInsurance = payBack.IsSocialIns;
                                monthlyExist.IsHealthInsurance = payBack.IsMedicalIns;
                                monthlyExist.IsUnEmpInsurance = payBack.IsUnemploymentIns;
                                monthlyExist.MonthYear = payBack.MonthYear;
                                monthlyExist.MonthYearEffect = i;
                                monthlyExist.Allowance1 = 0;
                                monthlyExist.Allowance2 = 0;
                                monthlyExist.Allowance3 = 0;
                                monthlyExist.Allowance4 = 0;
                                monthlyExist.AllowanceAdditional = 0;

                                monthlyExist.JobName = payBack.JobtitleName;
                                monthlyExist.AmountHDTIns = payBack.AmoutHDTInsPayBack - payBack.AmoutHDTIns;
                                monthlyExist.PaybackID = payBack.ID;
                                monthlyExist.SalaryInsurance = payBack.InsSalaryAdjust;
                                monthlyExist.SalaryHealthInsurance = payBack.InsSalaryAdjust;
                                monthlyExist.SalaryUnEmpInsurance = payBack.InsSalaryAdjust;
                                monthlyExist.SocialInsPlaceID = socialInsPlaceID;
                                if (rateInsurance != null)
                                {
                                    monthlyExist.SocialInsComRate = rateInsurance.SocialInsCompRate;
                                    monthlyExist.SocialInsEmpRate = rateInsurance.SocialInsEmpRate;
                                    monthlyExist.HealthInsComRate = rateInsurance.HealthInsCompRate;
                                    monthlyExist.HealthInsEmpRate = rateInsurance.HealthInsEmpRate;
                                    monthlyExist.UnemployComRate = rateInsurance.UnemployInsCompRate;
                                    monthlyExist.UnemployEmpRate = rateInsurance.UnemployInsEmpRate;
                                    var moneyInsuranceTotal = payBack.InsSalaryAdjust ?? 0 + payBack.AmoutHDTIns ?? 0;
                                    #region Set tỉ lệ và tiền đóng BHXH,BHYT,BHTN

                                    if (monthlyExist.IsSocialInsurance == true)
                                    {
                                        //tong ti le BHXH do NSDLĐ và NLĐ đóng
                                        double rate = (rateInsurance.SocialInsCompRate + rateInsurance.SocialInsEmpRate);
                                        monthlyExist.MoneySocialInsurance = (float)(moneyInsuranceTotal * rate);

                                        #region [Tung.Ly ]: ti le BHXH cty dong va ti le BHXH nv dong
                                        //Số tiền BHXH do cty đóng
                                        monthlyExist.SocialInsComAmount = (double)(moneyInsuranceTotal * monthlyExist.SocialInsComRate);
                                        //số tiền BHXH do NV đóng
                                        monthlyExist.SocialInsEmpAmount = (double)(moneyInsuranceTotal * monthlyExist.SocialInsEmpRate);
                                        #endregion
                                    }
                                    if (monthlyExist.IsHealthInsurance == true)
                                    {
                                        //tong ti le BHYT do NSDLĐ và NLĐ đóng
                                        double rate = (rateInsurance.HealthInsCompRate + rateInsurance.HealthInsEmpRate);
                                        monthlyExist.MoneyHealthInsurance = (float)(moneyInsuranceTotal * rate);

                                        #region [Tung.Ly ]: ti le BHXH cty dong va ti le BHXH nv dong
                                        //Số tiền BHYT do cty đóng
                                        monthlyExist.HealthInsComAmount = (double)(moneyInsuranceTotal * monthlyExist.HealthInsComRate);
                                        //số tiền BHYT do NV đóng
                                        monthlyExist.HealthInsEmpAmount = (double)(moneyInsuranceTotal * monthlyExist.HealthInsEmpRate);
                                        #endregion
                                    }
                                    if (monthlyExist.IsUnEmpInsurance == true)
                                    {
                                        //tong ti le BHTN do NSDLĐ và NLĐ đóng
                                        double rate = (rateInsurance.UnemployInsCompRate + rateInsurance.UnemployInsEmpRate);
                                        monthlyExist.MoneyUnEmpInsurance = (float)(moneyInsuranceTotal * rate);

                                        #region [Tung.Ly ]: ti le BHTN cty dong va ti le BHXH nv dong
                                        //Số tiền BHTN do cty đóng
                                        monthlyExist.UnemployComAmount = (double)(moneyInsuranceTotal * monthlyExist.UnemployComRate);
                                        //số tiền BHTN do NV đóng
                                        monthlyExist.UnemployEmpAmount = (double)(moneyInsuranceTotal * monthlyExist.UnemployEmpRate);
                                        #endregion
                                    }
                                    monthlyExist.AmountChargeIns = monthlyExist.Allowance1 + monthlyExist.Allowance2 +
                                        monthlyExist.Allowance3 + monthlyExist.Allowance4 + moneyInsuranceTotal;
                                    #endregion
                                }
                                lstProfileMonthlyEditing.Add(monthlyExist);
                            }
                            #endregion

                            #region them moi
                            else
                            {
                                Ins_ProfileInsuranceMonthly proMonthly = new Ins_ProfileInsuranceMonthly();
                                proMonthly.MonthYear = payBack.MonthYear;
                                proMonthly.ProfileID = payBack.ProfileID;
                                proMonthly.MonthYearEffect = i;
                                proMonthly.IsPayback = true;
                                proMonthly.IsSocialInsurance = payBack.IsSocialIns;
                                proMonthly.IsHealthInsurance = payBack.IsMedicalIns;
                                proMonthly.IsUnEmpInsurance = payBack.IsUnemploymentIns;
                                proMonthly.SalaryInsurance = payBack.InsSalaryAdjust;
                                proMonthly.SalaryHealthInsurance = payBack.InsSalaryAdjust;
                                proMonthly.SalaryUnEmpInsurance = payBack.InsSalaryAdjust;
                                proMonthly.JobName = payBack.JobtitleName;
                                proMonthly.AmountHDTIns = payBack.AmoutHDTInsPayBack - payBack.AmoutHDTIns;
                                proMonthly.PaybackID = payBack.ID;
                                proMonthly.SocialInsPlaceID = socialInsPlaceID;
                                if (rateInsurance != null)
                                {
                                    proMonthly.SocialInsComRate = rateInsurance.SocialInsCompRate;
                                    proMonthly.SocialInsEmpRate = rateInsurance.SocialInsEmpRate;
                                    proMonthly.HealthInsComRate = rateInsurance.HealthInsCompRate;
                                    proMonthly.HealthInsEmpRate = rateInsurance.HealthInsEmpRate;
                                    proMonthly.UnemployComRate = rateInsurance.UnemployInsCompRate;
                                    proMonthly.UnemployEmpRate = rateInsurance.UnemployInsEmpRate;

                                    var moneyInsuranceTotal = (payBack.InsSalaryAdjust ?? 0) + (payBack.AmoutHDTIns ?? 0);

                                    #region Set tỉ lệ và tiền đóng BHXH,BHYT,BHTN

                                    if (proMonthly.IsSocialInsurance == true)
                                    {
                                        //tong ti le BHXH do NSDLĐ và NLĐ đóng
                                        double rate = (rateInsurance.SocialInsCompRate + rateInsurance.SocialInsEmpRate);
                                        proMonthly.MoneySocialInsurance = (float)(moneyInsuranceTotal * rate);

                                        #region [Tung.Ly ]: ti le BHXH cty dong va ti le BHXH nv dong
                                        //Số tiền BHXH do cty đóng
                                        proMonthly.SocialInsComAmount = (double)(moneyInsuranceTotal * proMonthly.SocialInsComRate);
                                        //số tiền BHXH do NV đóng
                                        proMonthly.SocialInsEmpAmount = (double)(moneyInsuranceTotal * proMonthly.SocialInsEmpRate);
                                        #endregion
                                    }
                                    if (proMonthly.IsHealthInsurance == true)
                                    {
                                        //tong ti le BHYT do NSDLĐ và NLĐ đóng
                                        double rate = (rateInsurance.HealthInsCompRate + rateInsurance.HealthInsEmpRate);
                                        proMonthly.MoneyHealthInsurance = (float)(moneyInsuranceTotal * rate);

                                        #region [Tung.Ly ]: ti le BHXH cty dong va ti le BHXH nv dong
                                        //Số tiền BHYT do cty đóng
                                        proMonthly.HealthInsComAmount = (double)(moneyInsuranceTotal * proMonthly.HealthInsComRate);
                                        //số tiền BHYT do NV đóng
                                        proMonthly.HealthInsEmpAmount = (double)(moneyInsuranceTotal * proMonthly.HealthInsEmpRate);
                                        #endregion
                                    }
                                    if (proMonthly.IsUnEmpInsurance == true)
                                    {
                                        //tong ti le BHTN do NSDLĐ và NLĐ đóng
                                        double rate = (rateInsurance.UnemployInsCompRate + rateInsurance.UnemployInsEmpRate);
                                        proMonthly.MoneyUnEmpInsurance = (float)(moneyInsuranceTotal * rate);

                                        #region [Tung.Ly ]: ti le BHTN cty dong va ti le BHXH nv dong
                                        //Số tiền BHTN do cty đóng
                                        proMonthly.UnemployComAmount = (double)(moneyInsuranceTotal * proMonthly.UnemployComRate);
                                        //số tiền BHTN do NV đóng
                                        proMonthly.UnemployEmpAmount = (double)(moneyInsuranceTotal * proMonthly.UnemployEmpRate);
                                        #endregion
                                    }
                                    proMonthly.AmountChargeIns = proMonthly.Allowance1 + proMonthly.Allowance2 +
                                        proMonthly.Allowance3 + proMonthly.Allowance4 + moneyInsuranceTotal;
                                    #endregion

                                }
                                proMonthly.ID = Guid.NewGuid();
                                lstProfileMonthly.Add(proMonthly);
                            }
                            #endregion
                            #endregion
                        }

                        #region Ins_ReportD02
                        #region d02
                        var periodD02 = EnumDropDown.GetEnumDescription<PeriodInsurance>(PeriodInsurance.GETTEMP);
                        var cutOfDuration = "Kỳ " + periodD02 + " - " + payBack.MonthYear.Value.ToString(ConstantFormat.HRM_Format_MonthYear);
                        var reportD02Existed = unitOfWork.CreateQueryable<Ins_ReportD02>(p => p.DateMonth.HasValue
                           && p.DateMonth.Value.Month == payBack.MonthYear.Value.Month && p.DateMonth.Value.Year == payBack.MonthYear.Value.Year
                           && p.ReportD02Name == cutOfDuration).FirstOrDefault();
                        var reportD02ID = Guid.Empty;
                                               
                        #endregion

                        if (reportD02Existed == null)
                        {
                            //insert D02Report
                            reportD02 = new Ins_ReportD02
                            {
                                DateReport = DateTime.Now,
                                DateStart = insMonthYearFrom,
                                DateEnd = insMonthYearTo,
                                DateMonth = payBack.MonthYear,
                                ReportD02Name = cutOfDuration,
                                MaxSalary = 0,
                                MinSalary = 0,
                                SociaInsCountPro = 0,
                                SociaInsTotalSalary = 0,
                                HealthInsCountPro = 0,
                                HealthInsTotalSalary = 0,
                                UnEmpInsCountPro = 0,
                                Type = PeriodInsurance.GETTEMP.ToString(),
                                UnEmpInsTotalSalary = 0
                            };
                            repoInsReportD02.Add(reportD02);
                            var statusCode = repoInsReportD02.SaveChanges();
                            reportD02ID = reportD02.ID;
                            reportD02Existed = unitOfWork.CreateQueryable<Ins_ReportD02>(Guid.Empty, m => m.ID == reportD02ID).FirstOrDefault();
                        }

                        if (reportD02Existed != null)
                        {
                            var insTypeD02 = insTypeD02s.Where(m => m.ID == payBack.TypeID).FirstOrDefault();
                            var reportD02ItemExisted = unitOfWork.CreateQueryable<Ins_ReportD02Item>(m => m.ReportD02ID == reportD02Existed.ID && m.PayBackID == payBack.ID).FirstOrDefault();
                            var monthFrom = payBack.MonthYear.Value.AddMonths(-1);
                            monthFrom = new DateTime(monthFrom.Year, monthFrom.Month,PeriodInsuranceDayPreMonthDefault );
                            var monthTo = new DateTime(payBack.MonthYear.Value.Year, payBack.MonthYear.Value.Month, PeriodInsuranceDayCurrentMonthDefault);

                            #region delete d02 truoc khi tinh truy lĩnh     
                            #region xoa D02Item truoc khi tinh toan truy lĩnh
                            if (reportD02Existed != null && reportD02Existed.ID != Guid.Empty)
                            {
                                var reportD02ItemDelete = unitOfWork.CreateQueryable<Ins_ReportD02Item>(Guid.Empty, m => m.ReportD02ID == reportD02Existed.ID && m.ProfileID == profileID && m.IsPayBack == null).ToList();
                                if (reportD02ItemDelete != null)
                                {
                                    unitOfWork.RemoveObject(typeof(Ins_ReportD02Item), reportD02ItemDelete.ToArray());
                                    unitOfWork.SaveChanges();
                                }
                            }
                            #endregion


                            if (reportD02ItemExisted != null)
                            {
                                #region Edit
                                reportD02ItemExisted.ProfileID = profileID;
                                reportD02ItemExisted.ReportD02ID = reportD02Existed.ID;
                                reportD02ItemExisted.OldBasicSalary = payBack.InsSalary;
                                reportD02ItemExisted.NewBasicSalary = payBack.InsSalaryAdjust;
                                reportD02ItemExisted.SocialInsPlaceID = socialInsPlaceID;
                                reportD02ItemExisted.IsPayBack = true;
                                reportD02ItemExisted.PayBackID = payBack.ID;
                                reportD02ItemExisted.MonthFrom = payBack.FromMonthEffect;
                                reportD02ItemExisted.MonthTo = payBack.FromMonthEffect;
                                reportD02ItemExisted.RateHealthIns = payBack.HealthInsComRate + payBack.HealthInsEmpRate;
                                reportD02ItemExisted.RateSocialIns = payBack.SocialInsComRate + payBack.SocialInsEmpRate;
                                reportD02ItemExisted.RateUnEmpIns = payBack.UnemployComRate + payBack.UnemployEmpRate;
                                reportD02ItemExisted.JobName = payBack.JobtitleName;
                                if (insTypeD02 != null)
                                {
                                    reportD02ItemExisted.Status = insTypeD02.CommentCode;
                                    reportD02ItemExisted.Type = insTypeD02.TypeCode;
                                    reportD02ItemExisted.Comment = payBack.Comment;
                                }
                                if (!lstD02ItemEditing.Any(m => m.ID == reportD02ItemExisted.ID))
                                {
                                    lstD02ItemEditing.Add(reportD02ItemExisted);
                                }

                                #endregion
                            }
                            else
                            {
                                #region Add new
                                var d02Item = new Ins_ReportD02Item();
                                d02Item.ProfileID = profileID;
                                d02Item.ReportD02ID = reportD02Existed.ID;
                                d02Item.OldBasicSalary = payBack.InsSalary;
                                d02Item.NewBasicSalary = payBack.InsSalaryAdjust;
                                d02Item.MonthFrom = payBack.FromMonthEffect;
                                d02Item.MonthTo = payBack.FromMonthEffect;
                                d02Item.IsPayBack = true;
                                d02Item.PayBackID = payBack.ID;
                                d02Item.JobName = payBack.JobtitleName;
                                d02Item.SocialInsPlaceID = socialInsPlaceID;
                                d02Item.RateHealthIns = payBack.HealthInsComRate + payBack.HealthInsEmpRate;
                                d02Item.RateSocialIns = payBack.SocialInsComRate + payBack.SocialInsEmpRate;
                                d02Item.RateUnEmpIns = payBack.UnemployComRate + payBack.UnemployEmpRate;
                                if (insTypeD02 != null)
                                {
                                    d02Item.Status = insTypeD02.CommentCode;
                                    d02Item.Type = insTypeD02.TypeCode;
                                    d02Item.Comment = payBack.Comment;
                                }
                                lstD02Item.Add(d02Item);
                                #endregion
                            }
                        }
                        #endregion
                        #endregion
                    }

                    #region Save Change
                    if (lstProfileMonthly.Any())
                    {
                        repoInsMonthly.Add(lstProfileMonthly);
                    }
                    if (lstProfileMonthlyEditing.Any())
                    {
                        repoInsMonthly.Edit(lstProfileMonthlyEditing);
                    }
                    if (lstD02Item.Any())
                    {
                        repoInsReportD02Item.Add(lstD02Item);
                    }
                    if (lstD02ItemEditing.Any())
                    {
                        repoInsReportD02Item.Edit(lstD02ItemEditing);
                    }
                    repoInsMonthly.SaveChanges();
                    #endregion
                }
            }
        }
Пример #2
0
        /// <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();
                }
            }
        }