/// <summary> BC NV huong tro cap nang nhoc, doc hai, nguy hiem </summary> /// <param name="DateStart"></param> /// <param name="DateEnd"></param> /// <param name="orgIds"></param> /// <returns></returns> public DataTable BC_EmpHardJob(DateTime DateStart, DateTime DateEnd, string orgIds) { using (var context = new VnrHrmDataContext()) { var status = string.Empty; var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var repoCatExchangeRate = new Cat_ExchangeRateRepository(unitOfWork); var repoCatValueEntity = new Cat_ValueEntityRepository(unitOfWork); var repoSal_InsuranceSalary = new Sal_InsuranceSalaryRepository(unitOfWork); var repoIns_ProfileInsuranceMonthly = new Ins_ProfileInsuranceMonthlyRepository(unitOfWork); var repoAtt_LeaveDay = new Att_LeavedayRepository(unitOfWork); var repoIns_InsuranceRecord = new Ins_InsuranceRecordRepository(unitOfWork); #region DS Profile int orgOrderNumber = 0; var lstOrgOrderNumber = orgIds.Split(',').Where(p => int.TryParse(p, out orgOrderNumber)).Select(p => orgOrderNumber).ToList(); lstOrgOrderNumber = lstOrgOrderNumber.Where(p => p != 0).ToList(); var catOrgIDs = unitOfWork.CreateQueryable<Cat_OrgStructure>(Guid.Empty, p => p.IsDelete == null && lstOrgOrderNumber.Contains(p.OrderNumber)).Select(p => p.ID).ToList(); var lstProfiles = unitOfWork.CreateQueryable<Hre_Profile>(Guid.Empty, p => catOrgIDs.Contains(p.OrgStructureID ?? Guid.Empty)).Select(p => new { p.ProfileName, p.ID, p.DateHire, p.CodeEmp, p.SocialInsNo, p.DateQuit, p.SalaryClassID, }).ToList(); #endregion DataTable dt = CreateReportEmpHardJobScheme(DateStart, DateEnd); List<Guid> lstProfileIDTotal = lstProfiles.Select(m => m.ID).ToList(); foreach (var lstProfileID in lstProfileIDTotal.Chunk(500)) { //Lấy ds trích nộp BH (được hưởng tiền HDTJob) theo NV vào khoảng thời gian truyền vào var lstProfileMonthly = unitOfWork.CreateQueryable<Ins_ProfileInsuranceMonthly>(Guid.Empty, m => m.IsDelete == null && m.ProfileID != null && lstProfileID.Contains(m.ProfileID.Value) && m.MonthYear >= DateStart && m.MonthYear <= DateEnd && m.MonthYear != null && (m.AmountHDTIns != null && m.AmountHDTIns.Value > 0) ) .Select(m => new CustomProfileMonthlyEntity() { ProfileID = m.ProfileID, SalaryInsurance = m.SalaryInsurance, IsSocialInsurance = m.IsSocialInsurance, MonthYear = m.MonthYear.Value, MoneyHDTJob = m.AmountHDTIns, IsPregnant = m.IsPregnant, IsDecreaseWorkingDays = m.IsDecreaseWorkingDays }) .ToList(); int stt = 0; foreach (var profileID in lstProfileID) { stt++; var hasValue = false; DataRow dr = null; for (DateTime datecheck = DateStart; datecheck <= DateEnd; datecheck = datecheck.AddMonths(1)) { if (dr == null) { dr = dt.NewRow(); } #region Kiểm tra xem có đóng HDT Job ko var Ins = lstProfileMonthly.Where(p => p.MonthYear == datecheck && p.ProfileID == profileID).FirstOrDefault(); if (Ins != null) { if (Ins.MoneyHDTJob != null && Ins.MoneyHDTJob > 0) { if (Ins.IsPregnant != null && Ins.IsPregnant.Value) { dr[datecheck.ToString(MONTHYEAR)] = ConstantDisplay.HRM_Ins_ReportEmpHardJob_PregnantLeave.TranslateString(); hasValue = true; } else if (Ins.IsDecreaseWorkingDays != null && Ins.IsDecreaseWorkingDays.Value) { dr[datecheck.ToString(MONTHYEAR)] = ConstantDisplay.HRM_Ins_ReportEmpHardJob_DecreaseWorkingDays.TranslateString(); hasValue = true; } else { dr[datecheck.ToString(MONTHYEAR)] = String.Format("{0:#,###,###.##}", Ins.MoneyHDTJob); hasValue = true; } } } #endregion } if (hasValue) { var profile = lstProfiles.Where(m => m.ID == profileID).FirstOrDefault(); if (profile != null) { dr[Ins_ReportEmpHardJoblEntity.FieldNames.CodeEmp] = profile.CodeEmp; dr[Ins_ReportEmpHardJoblEntity.FieldNames.ProfileName] = profile.ProfileName; } dt.Rows.Add(dr); } } } return dt.ConfigTable(true); } }
/// <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 } }