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 } } }
/// <summary> Hàm tính toán và save lại bảo hiểm của từng người theo tháng </summary> /// <param name="orgs"></param> /// <param name="monthCheck">Vd: 01/12/2014</param> /// <param name="periodInsurance">Loại tạm thời , Loại Chính Thức</param> public void AnalyzeAndSaveInsuranceByMonth(string orgs, DateTime monthCheck, string periodInsurance, string codeEmp, List<Guid> socialInsPlaceIDs,string userLogin) { /* * Goal(tính toán và save lại bảo hiểm của từng NV theo tháng và phòng ban (Tạo mới hoặc update Ins_ProfileInsuranceMonthly)) * Steps : * - Step1 : Lấy DS NV theo phòng ban và theo tháng được chọn * - Step2 : xử lý tính ra tháng bắt đầu join đóng BHXH,BHYT,BHTN * - Step3 : Set giá trị cho IsLeaveNonWorkday trong profile (cho nghỉ >=14 ngày) * - Step4 : kiểm tra xem có đóng BHXH,BHYT,BHTN * - Step5 : Tính lương và mức đóng của BHXH,BHYT,BHTN * - Step6 : Tạo mới hoặc cập nhật dữ liệu vào bảng Ins_ProfileInsuranceMonthly */ //ngày 1 tháng N var beginMonth = new DateTime(monthCheck.Year, monthCheck.Month, 1); //ngày 31 tháng N DateTime endMonth = beginMonth.AddMonths(1).AddMinutes(-1); var step = 200; monthCheck = new DateTime(monthCheck.Year, monthCheck.Month, 1); var lstProfile = new List<Hre_ProfileEntity>(); using (var context = new VnrHrmDataContext()) { #region Khai báo var status = string.Empty; var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var repoInsMonthly = new Ins_ProfileInsuranceMonthlyRepository(unitOfWork); var repoSalInsuranceSalary = new Sal_InsuranceSalaryRepository(unitOfWork); var repoAtt_WorkDay = new Att_WorkDayRepository(unitOfWork); var repoAtt_LeaveDay = new Att_LeavedayRepository(unitOfWork); var repoCat_LeaveDayType = new Cat_LeaveDayTypeRepository(unitOfWork); var repoAtt_Roster = new Att_RosterRepository(unitOfWork); var repoHre_Profile = new Hre_ProfileRepository(unitOfWork); var repoIns_InsuranceRecord = new Ins_InsuranceRecordRepository(unitOfWork); var profileIDByCodeEmp = Guid.Empty; #endregion #region phân tích theo mã NV (từ đk tìm kiếm) if (!string.IsNullOrEmpty(codeEmp)) { var profileByCodeEmp = unitOfWork.CreateQueryable<Hre_Profile>(Guid.Empty, p => p.IsDelete == null && p.CodeEmp == codeEmp).FirstOrDefault(); if (profileByCodeEmp != null) { profileIDByCodeEmp = profileByCodeEmp.ID; } else { return; } } #endregion //lay ds ngay nghi var dayOffMonthChecks = unitOfWork.CreateQueryable<Cat_DayOff>(Guid.Empty, m => m.DateOff.Year == monthCheck.Year && m.DateOff.Month == monthCheck.Month).ToList(); #region lay hop dong theo phòng ban //List<object> contractPara = new List<object>(); //contractPara.AddRange(new object[16]); //contractPara[0] = null; //contractPara[1] = null; //contractPara[2] = orgs; //contractPara[3] = null; //contractPara[4] = null; //contractPara[5] = null; //contractPara[6] = null; //contractPara[7] = null; //contractPara[8] = null; //contractPara[9] = null; //contractPara[10] = null; //contractPara[11] = null; //contractPara[12] = null; //contractPara[13] = null; //contractPara[14] = 1; //contractPara[15] = int.MaxValue; //if (profileIDByCodeEmp != Guid.Empty) //{ // contractPara[1] = codeEmp; // contractPara[2] = null; //} //var contracts = GetData<Hre_ContractEntity>(contractPara, ConstantSql.hrm_hr_sp_get_Contract, ref status).Translate<Hre_Contract>().ToList(); var contracts = new List<Hre_Contract>(); #endregion #region Get Profiles by orgs var listObj = new List<object> { orgs, string.Empty, string.Empty }; if (profileIDByCodeEmp != Guid.Empty) { listObj[0] = null; listObj[2] = codeEmp; } if (!string.IsNullOrEmpty(codeEmp)) { lstProfile = unitOfWork.CreateQueryable<Hre_Profile>(Guid.Empty, m => m.CodeEmp == codeEmp).ToList().Translate<Hre_ProfileEntity>(); } else if (orgs != null) { var lstOrderNumberORG = orgs.Split(',').Select(s => int.Parse(s)).Distinct().ToList(); var orgObj = unitOfWork.CreateQueryable<Cat_OrgStructure>(Guid.Empty).Select(p => p.ID).ToList(); var orgStructureIDs = unitOfWork.CreateQueryable<Cat_OrgStructure>(Guid.Empty, m => lstOrderNumberORG.Contains(m.OrderNumber)).Select(m => m.ID).ToList(); lstProfile = new List<Hre_ProfileEntity>(); foreach (var orgIds in orgStructureIDs.Chunk(step)) { var profileByOrgs = unitOfWork.CreateQueryable<Hre_Profile>(Guid.Empty, m => m.OrgStructureID.HasValue && orgIds.Contains(m.OrgStructureID.Value)).ToList().Translate<Hre_ProfileEntity>(); lstProfile.AddRange(profileByOrgs); } } #endregion 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(); } } #region lay ds inusuranceSalary by profile list //ds profileId theo phong ban List<Guid> lstProfileIDs = lstProfile.Select(m => m.ID).ToList(); var lstInsuranceSalaryByProfile = new List<Sal_InsuranceSalary>(); foreach (var profileIds in lstProfileIDs.Chunk(step)) { var lstInsuranceSalaryByProfileTemp = unitOfWork.CreateQueryable<Sal_InsuranceSalary>(Guid.Empty, m => m.InsuranceAmount != null && m.ProfileID != null && m.DateEffect <= endMonth && profileIds.Contains(m.ProfileID.Value)).ToList(); lstInsuranceSalaryByProfile.AddRange(lstInsuranceSalaryByProfileTemp); var contractTemp = unitOfWork.CreateQueryable<Hre_Contract>(Guid.Empty, m => profileIds.Contains(m.ProfileID)).ToList(); contracts.AddRange(contractTemp); } if (lstInsuranceSalaryByProfile.Any()) { lstProfileIDs = lstInsuranceSalaryByProfile.Select(p => p.ProfileID ?? Guid.Empty).ToList(); lstProfile = lstProfile.Where(p => lstProfileIDs.Contains(p.ID)).ToList(); } #endregion #region ds NV đóng Bảo Hiểm theo phòng ban và tháng (Ins_ProfileInsuranceMonthly) var listInsMonthlyObj = new List<object> { orgs, monthCheck, 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).Translate<Ins_ProfileInsuranceMonthly>(); // lstProfileInsuranceMonthlyInDb = lstProfileInsuranceMonthlyInDb.Where(m => m.PaybackID == null ).ToList(); #endregion #region Tháng bắt đầu tham gia bảo hiểm SetMonthJoinInsuranceByProfile(lstProfile, lstInsuranceSalaryByProfile, endMonth); #endregion #region Kiem tra co dong bao hiem ko? //Kiểm tra xem co đóng BHXH,BHYT,BHTN SetIsHaveInsurnceByProfileByMonth(lstProfile, monthCheck, contracts,userLogin); if (lstProfile.Any()) { lstProfileIDs = lstProfile.Select(p => p.ID).ToList(); } #endregion #region Nghỉ 14 ngày List<Hre_ProfileEntity> lstProfileEntities = new List<Hre_ProfileEntity>(); List<string> lstLeaveInsuranceType = String.Join(",", leaveDayInsuranceTypes).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); if (HasLeaveGreater14day) { string E_APPROVE = HDTJobStatus.E_APPROVE.ToString(); var lstProfilesIdChunks = lstProfileIDs; var leaveDayTypeIDs = unitOfWork.CreateQueryable<Cat_LeaveDayType>(Guid.Empty, m => m.PaidRate == 0 || lstLeaveInsuranceType.Contains(m.InsuranceType)).Select(m => m.ID).ToList(); foreach (var profileIDs in lstProfilesIdChunks.Chunk(step)) { var lstProfileEntitiesTemp = lstProfile.Where(p => profileIDs.Contains(p.ID)).ToList(); #region workday từ 1->31 tháng N theo các NV (không có quẹt thẻ) var lstWorkday = unitOfWork.CreateQueryable<Att_Workday>(Guid.Empty, m => m.InTime1 == null && m.OutTime1 == null && m.WorkDate >= beginMonth && m.WorkDate <= endMonth && profileIDs.Contains(m.ProfileID)).Select(m => new CustomWorkDayEntity() { ProfileID = m.ProfileID, InTime1 = m.InTime1, OutTime1 = m.OutTime1, WorkDate = m.WorkDate }).ToList(); #endregion #region Leaveday (ngày nghỉ trong khoảng 1->31 tháng N) theo NV string app = LeaveDayStatus.E_APPROVED.ToString(); var lstLeaveDay = unitOfWork.CreateQueryable<Att_LeaveDay>(Guid.Empty, m => m.Status == app && m.DateStart <= endMonth && m.DateEnd >= beginMonth && profileIDs.Contains(m.ProfileID)) .Select(m => new CustomLeavedayEntity() { ProfileID = m.ProfileID, DateStart = m.DateStart, DateEnd = m.DateEnd, LeaveDayTypeID = m.LeaveDayTypeID }).ToList(); #endregion //paidrate = 0 :ko tra luong var lstLeavedayTypeRateZero = unitOfWork.CreateQueryable<Cat_LeaveDayType>(Guid.Empty, p => p.PaidRate == 0).ToList(); #region lấy ds chứng từ bảo hiểm var insRecords = unitOfWork.CreateQueryable<Ins_InsuranceRecord>(Guid.Empty, m => m.DateStart != null && m.DateEnd != null && m.DateStart <= endMonth && m.DateEnd >= beginMonth && profileIDs.Contains(m.ProfileID)) .Select(m => new CustomInsuranceRecordEntity() { ProfileID = m.ProfileID, DateStart = m.DateStart.Value, DateEnd = m.DateEnd.Value, InsuranceType = m.InsuranceType }).ToList(); #endregion var approved = RosterStatus.E_APPROVED.ToString(); var rosters = unitOfWork.CreateQueryable<Att_Roster>(Guid.Empty, p => p.Status == approved && (p.DateStart <= endMonth && p.DateEnd >= beginMonth) && profileIDs.Contains(p.ProfileID)) .Select(m => new CustomRosterEntity() { ProfileID = m.ProfileID, DateStart = m.DateStart, DateEnd = m.DateEnd, MonShiftID = m.MonShiftID, TueShiftID = m.TueShiftID, WedShiftID = m.WedShiftID, ThuShiftID = m.ThuShiftID, FriShiftID = m.FriShiftID, SatShiftID = m.SatShiftID, SunShiftID = m.SunShift2ID }) .ToList(); //kt co nghỉ hơn 14 ngay ko? SetLeaveNonWorkdayByProfile(lstProfileEntitiesTemp, beginMonth, endMonth, lstWorkday, lstLeaveDay, lstLeavedayTypeRateZero, insRecords, rosters, dayOffMonthChecks); //Hàm do Triet.Mai Hỗ trợ. tính ra số ngày không làm HDTJOb // "SC, SU, SM, SD, DP, D, M, AL, TSC,TAS" lstLeaveDay = lstLeaveDay.Where(m => m.LeaveDayTypeID != null && leaveDayTypeIDs.Contains(m.LeaveDayTypeID.Value)).ToList(); var lstHDTJob = unitOfWork.CreateQueryable<Hre_HDTJob>(Guid.Empty, m => m.Status == E_APPROVE && m.DateFrom <= endMonth && m.DateTo >= beginMonth && m.ProfileID.HasValue && profileIDs.Contains(m.ProfileID.Value)).ToList(); GetHDTJobDayByProfile(lstProfileEntitiesTemp, leaveDayTypeIDs, monthCheck, rosters, lstLeaveDay, lstHDTJob); lstProfileEntities.AddRange(lstProfileEntitiesTemp); } } lstProfile = lstProfileEntities; #endregion #region tính lương , mức đóng BHXH,BHYT,BHTN SetMoneyInsuranceByProfileByMonth(lstProfile, lstInsuranceSalaryByProfile, endMonth, orgs, contracts,userLogin); #endregion #region Save Change var lstProfileInsuranceMonthlyAddNew = new List<Ins_ProfileInsuranceMonthly>(); var lstProfileInsuranceMonthlyModify = new List<Ins_ProfileInsuranceMonthly>(); foreach (var profile in lstProfile) { if (lstProfileInsuranceMonthlyInDb.Where(m => m.ProfileID == profile.ID && m.PaybackID != null).FirstOrDefault() != null) { continue; } Ins_ProfileInsuranceMonthly insuranceByProfile = lstProfileInsuranceMonthlyInDb .FirstOrDefault(m => m.ProfileID == profile.ID && m.PaybackID == null); if (insuranceByProfile != null) //Update cái dữ liệu cũ { insuranceByProfile.WorkPlaceID = profile.WorkPlaceID; insuranceByProfile.SocialInsPlaceID = profile.SocialInsPlaceID; insuranceByProfile.IsSocialInsurance = profile.IsHaveInsSocial; insuranceByProfile.IsHealthInsurance = profile.IsHaveInsHealth; insuranceByProfile.IsUnEmpInsurance = profile.IsHaveInsUnEmp; insuranceByProfile.IsPregnant = profile.IsPregnant; insuranceByProfile.MoneySocialInsurance = profile.MoneyInsuranceSocial; insuranceByProfile.MoneyHealthInsurance = profile.MoneyInsuranceHealth; insuranceByProfile.MoneyUnEmpInsurance = profile.MoneyInsuranceUnEmp; insuranceByProfile.SalaryInsurance = profile.MoneyInsuranceTotal; insuranceByProfile.SalaryHealthInsurance = profile.MoneyInsuranceHealthTotal; insuranceByProfile.SalaryUnEmpInsurance = profile.MoneyInsuranceUnEmpTotal; insuranceByProfile.IsDecreaseWorkingDays = profile.IsDecreaseWorkingDays; insuranceByProfile.JobName = profile.JobName; insuranceByProfile.AmountHDTIns = profile.AmountHDTIns; insuranceByProfile.HDTGroupCode = profile.HDTJobGroupCode; insuranceByProfile.TypeGetData = periodInsurance; #region Tung.Ly set các tỉ lệ và mức đóng insuranceByProfile.Allowance1 = profile.Allowance1; insuranceByProfile.Allowance2 = profile.Allowance2; insuranceByProfile.Allowance3 = profile.Allowance3; insuranceByProfile.Allowance4 = profile.Allowance4; insuranceByProfile.AmountChargeIns = profile.AmountChargeIns; insuranceByProfile.SocialInsComRate = profile.SocialInsComRate; insuranceByProfile.SocialInsComAmount = profile.SocialInsComAmount; insuranceByProfile.SocialInsEmpRate = profile.SocialInsEmpRate; insuranceByProfile.SocialInsEmpAmount = profile.SocialInsEmpAmount; insuranceByProfile.HealthInsComRate = profile.HealthInsComRate; insuranceByProfile.HealthInsComAmount = profile.HealthInsComAmount; insuranceByProfile.HealthInsEmpRate = profile.HealthInsEmpRate; insuranceByProfile.HealthInsEmpAmount = profile.HealthInsEmpAmount; insuranceByProfile.UnemployComRate = profile.UnemployComRate; insuranceByProfile.UnemployComAmount = profile.UnemployComAmount; insuranceByProfile.UnemployEmpRate = profile.UnemployEmpRate; insuranceByProfile.UnemployEmpAmount = profile.UnemployEmpAmount; insuranceByProfile.IsPayback = false; insuranceByProfile.MonthYearEffect = monthCheck; #endregion lstProfileInsuranceMonthlyModify.Add(insuranceByProfile);//add vao list (Modify) } else //Tạo mới { var newInsurance = new Ins_ProfileInsuranceMonthly { ProfileID = profile.ID, WorkPlaceID = profile.WorkPlaceID, SocialInsPlaceID = profile.SocialInsPlaceID, IsSocialInsurance = profile.IsHaveInsSocial, IsHealthInsurance = profile.IsHaveInsHealth, IsUnEmpInsurance = profile.IsHaveInsUnEmp, IsPregnant = profile.IsPregnant, MoneySocialInsurance = profile.MoneyInsuranceSocial, MoneyHealthInsurance = profile.MoneyInsuranceHealth, MoneyUnEmpInsurance = profile.MoneyInsuranceUnEmp, IsDecreaseWorkingDays = profile.IsDecreaseWorkingDays, SalaryInsurance = profile.MoneyInsuranceTotal, AmountHDTIns = profile.AmountHDTIns, HDTGroupCode = profile.HDTJobGroupCode, SalaryHealthInsurance = profile.MoneyInsuranceHealthTotal, SalaryUnEmpInsurance = profile.MoneyInsuranceUnEmpTotal, TypeGetData = periodInsurance, MonthYear = monthCheck, IsPayback = false, MonthYearEffect = monthCheck, #region Tung.Ly set các tỉ lệ và mức đóng Allowance1 = profile.Allowance1, Allowance2 = profile.Allowance2, Allowance3 = profile.Allowance3, Allowance4 = profile.Allowance4, AmountChargeIns = profile.AmountChargeIns, SocialInsComRate = profile.SocialInsComRate, SocialInsComAmount = profile.SocialInsComAmount, SocialInsEmpRate = profile.SocialInsEmpRate, SocialInsEmpAmount = profile.SocialInsEmpAmount, HealthInsComRate = profile.HealthInsComRate, HealthInsComAmount = profile.HealthInsComAmount, HealthInsEmpRate = profile.HealthInsEmpRate, HealthInsEmpAmount = profile.HealthInsEmpAmount, UnemployComRate = profile.UnemployComRate, UnemployComAmount = profile.UnemployComAmount, UnemployEmpRate = profile.UnemployEmpRate, UnemployEmpAmount = profile.UnemployEmpAmount, JobName = profile.JobName, #endregion }; lstProfileInsuranceMonthlyAddNew.Add(newInsurance);//add new } } if (lstProfileInsuranceMonthlyAddNew.Any()) { repoInsMonthly.Add(lstProfileInsuranceMonthlyAddNew); } if (lstProfileInsuranceMonthlyModify.Any()) { repoInsMonthly.Edit(lstProfileInsuranceMonthlyModify); } repoInsMonthly.SaveChanges(); #endregion } }