public string ActionWaitApprove(string selectedIds) { using (var context = new VnrHrmDataContext()) { string message = string.Empty; var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var repo = new Hre_WorkHistoryRepository(unitOfWork); List<Guid> lstIds = selectedIds.Split(',').Select(x => Guid.Parse(x)).ToList(); var lstWorkHistorys = repo.FindBy(m => m.ID != null && lstIds.Contains(m.ID)).ToList(); foreach (var WorkHistory in lstWorkHistorys) { WorkHistory.Status = WorkHistoryStatus.E_WAITAPPROVE.ToString(); } repo.SaveChanges(); message = NotificationType.Success.ToString(); return message; } }
public string ActionWaitApprove(string selectedIds) { using (var context = new VnrHrmDataContext()) { string message = string.Empty; var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var repo = new Hre_WorkHistoryRepository(unitOfWork); List <Guid> lstIds = selectedIds.Split(',').Select(x => Guid.Parse(x)).ToList(); var lstWorkHistorys = repo.FindBy(m => m.ID != null && lstIds.Contains(m.ID)).ToList(); foreach (var WorkHistory in lstWorkHistorys) { WorkHistory.Status = WorkHistoryStatus.E_WAITAPPROVE.ToString(); } repo.SaveChanges(); message = NotificationType.Success.ToString(); return(message); } }
/// <summary> /// [Tam.Le] - 2014/08/08 /// Hàm xử lý load dữ liệu /// </summary> /// <param name="request"></param> /// <param name="otModel"></param> /// <returns></returns> public DataTable LoadData(DateTime dateStart, DateTime dateEnd, List<Guid> orgIDs, string ProfileName, string CodeEmp, Guid LeavedayType, out string ErrorMessages) { using (var context = new VnrHrmDataContext()) { var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); ErrorMessages = string.Empty; //bool isIncludeQuitEmp = true; List<Guid> leavedayTypeIds = new List<Guid>(); if (LeavedayType != null) leavedayTypeIds.Add(LeavedayType); List<Guid> shiftIDs = new List<Guid>(); string E_WAIT_APPROVED = LeaveDayStatus.E_WAIT_APPROVED.ToString(); string E_SUBMIT = LeaveDayStatus.E_SUBMIT.ToString(); var repoAtt_Leaveday = new Att_LeavedayRepository(unitOfWork); List<Att_LeaveDay> leaveDays = repoAtt_Leaveday.FindBy(s => s.LeaveDays > 0 && (s.Status == E_WAIT_APPROVED || s.Status == E_SUBMIT) && dateStart <= s.DateEnd && s.DateStart <= dateEnd).ToList<Att_LeaveDay>(); foreach (var item in leaveDays) { item.Status = E_WAIT_APPROVED; } var profileIds = leaveDays.Select(s => s.ProfileID).Distinct().ToList(); var repoAtt_Workday = new Att_WorkDayRepository(unitOfWork); var workDays = repoAtt_Workday.FindBy(s => profileIds.Contains(s.ProfileID) && dateStart <= s.WorkDate && s.WorkDate <= dateEnd) .Select(s => new { s.ProfileID, s.ShiftID, s.WorkDate, s.InTime1, s.OutTime1 }).ToList(); var repoHre_Profile = new Hre_ProfileRepository(unitOfWork); var profiles = repoHre_Profile.FindBy(s => profileIds.Contains(s.ID)) .Select(s => new { s.ID, s.DateQuit, s.OrgStructureID, s.ProfileName, s.CodeEmp, s.PositionID, s.JobTitleID }).ToList(); var repoCat_OrgStructure = new Cat_OrgStructureRepository(unitOfWork); var orgs = repoCat_OrgStructure.FindBy(s => s.Code != null).ToList(); var repoCat_Position = new Cat_PositionRepository(unitOfWork); var positions = repoCat_Position.FindBy(s => s.Code != null).Select(s => new { s.ID, s.Code }).ToList(); var repoCat_JobTitle = new Cat_JobTitleRepository(unitOfWork); var jobtitles = repoCat_JobTitle.FindBy(s => s.Code != null).Select(s => new { s.ID, s.Code }).ToList(); var repoCat_LeaveDayType = new Cat_LeaveDayTypeRepository(unitOfWork); var leavedayTypes = repoCat_LeaveDayType.FindBy(s => s.Code != null).Select(s => new { s.ID, s.Code, s.PaidRate }).Distinct().ToList(); var ledvedayPaidIds = leavedayTypes.Where(s => s.PaidRate > 0).Select(s => s.ID).ToList(); var repoCat_Shift = new Cat_ShiftRepository(unitOfWork); var shifts = repoCat_Shift.GetAll().ToList(); if (orgIDs != null) { profiles = profiles.Where(s => s.OrgStructureID != null && orgIDs.Contains(s.OrgStructureID.Value)).ToList(); } if (shiftIDs.Count > 0) { workDays = workDays.Where(s => s.ShiftID.HasValue && shiftIDs.Contains(s.ShiftID.Value)).ToList(); profileIds = workDays.Select(s => s.ProfileID).ToList(); profiles = profiles.Where(s => profileIds.Contains(s.ID)).ToList(); } if (leavedayTypeIds.Count > 0) { leaveDays = leaveDays.Where(s => leavedayTypeIds.Contains(s.LeaveDayTypeID)).ToList(); profileIds = leaveDays.Select(s => s.ProfileID).ToList(); profiles = profiles.Where(s => profileIds.Contains(s.ID)).ToList(); } //if (!isIncludeQuitEmp) //{ // profiles = profiles.Where(s => s.DateQuit == null || s.DateQuit > dateStart).ToList(); //} var startYear = new DateTime(dateStart.Year, 1, 1); var endYear = new DateTime(dateStart.Year, 12, DateTime.DaysInMonth(dateStart.Year, 12)); var rosterStatus = RosterStatus.E_APPROVED.ToString(); var repoAtt_Roster = new Att_RosterRepository(unitOfWork); List<Att_Roster> listRoster = repoAtt_Roster.FindBy(d => d.Status == rosterStatus && profileIds.Contains(d.ProfileID) && d.DateStart <= dateEnd && d.DateEnd >= startYear).ToList<Att_Roster>(); var repoCat_DayOff = new Cat_DayOffRepository(unitOfWork); List<Cat_DayOff> listHoliday = repoCat_DayOff.FindBy(hol => hol.DateOff >= startYear && hol.DateOff <= endYear).ToList<Cat_DayOff>(); var repoAtt_Grade = new Att_GradeRepository(unitOfWork); var listGrade = repoAtt_Grade.FindBy(d => d.MonthStart != null && d.MonthStart <= dateEnd && profileIds.Contains((Guid)d.ProfileID)).Select(d => new { d.ProfileID, d.MonthStart, d.GradeAttendanceID }).ToList(); List<Guid?> listGradeID = listGrade.Select(d => d.GradeAttendanceID).ToList(); var repoCat_Grade = new Cat_GradeAttendanceRepository(unitOfWork); List<Cat_GradeAttendance> listGradeCfg = repoCat_Grade.FindBy(d => listGradeID.Contains(d.ID)).ToList<Cat_GradeAttendance>(); List<Att_Roster> lstRosterTypeGroup = new List<Att_Roster>(); List<Att_RosterGroup> lstRosterGroup = new List<Att_RosterGroup>(); GetRosterGroup(profileIds, startYear, dateEnd, out lstRosterTypeGroup, out lstRosterGroup); var repoHre_WorkHistory = new Hre_WorkHistoryRepository(unitOfWork); List<Hre_WorkHistory> listWorkHistory = repoHre_WorkHistory.FindBy(d => profileIds.Contains(d.ProfileID) && d.DateEffective <= dateEnd).ToList<Hre_WorkHistory>(); var orgTypes = new List<Cat_OrgStructureType>(); var repoorgType = new Cat_OrgStructureTypeRepository(unitOfWork); orgTypes = repoorgType.FindBy(s => s.IsDelete == null).ToList<Cat_OrgStructureType>(); DataTable table = GetSchema(); List<string> codeRejects = new List<string>(); codeRejects.Add("SICK"); codeRejects.Add("SU"); codeRejects.Add("SD"); codeRejects.Add("D"); codeRejects.Add("D"); codeRejects.Add("DP"); codeRejects.Add("PSN"); codeRejects.Add("DSP"); codeRejects.Add("M"); List<Guid> leaveDayTypeRejectIDs = leavedayTypes.Where(s => codeRejects.Contains(s.Code)).Select(s => s.ID).ToList(); foreach (var profile in profiles) { var grade = listGrade.Where(d => d.ProfileID == profile.ID && d.MonthStart <= dateEnd).OrderByDescending(d => d.MonthStart).FirstOrDefault(); Cat_GradeAttendance gradeCfg = listGradeCfg.FirstOrDefault(d => grade != null && d.ID == grade.GradeAttendanceID); List<Hre_WorkHistory> listWorkHistoryByProfile = listWorkHistory.Where(d => d.ProfileID == profile.ID).ToList(); List<Att_Roster> listRosterByProfile = listRoster.Where(d => d.ProfileID == profile.ID && d.DateStart <= dateEnd && d.DateEnd >= dateStart).ToList(); var listRosterEntity = listRosterByProfile.Select(d => new Att_RosterEntity { ID = d.ID, ProfileID = d.ProfileID, RosterGroupName = d.RosterGroupName, Type = d.Type, Status = d.Status, DateEnd = d.DateEnd, DateStart = d.DateStart, MonShiftID = d.MonShiftID, TueShiftID = d.TueShiftID, WedShiftID = d.WedShiftID, ThuShiftID = d.ThuShiftID, FriShiftID = d.FriShiftID, SatShiftID = d.SatShiftID, SunShiftID = d.SunShiftID, MonShift2ID = d.MonShiftID, TueShift2ID = d.TueShift2ID, WedShift2ID = d.WedShift2ID, ThuShift2ID = d.ThuShift2ID, FriShift2ID = d.FriShift2ID, SatShift2ID = d.SatShift2ID, SunShift2ID = d.SunShift2ID }).ToList(); var listRosterGroupEntity = lstRosterGroup.Select(d => new Att_RosterGroupEntity { ID = d.ID, DateEnd = d.DateEnd, DateStart = d.DateStart, MonShiftID = d.MonShiftID, TueShiftID = d.TueShiftID, WedShiftID = d.WedShiftID, ThuShiftID = d.ThuShiftID, FriShiftID = d.FriShiftID, SatShiftID = d.SatShiftID, SunShiftID = d.SunShiftID, RosterGroupName = d.RosterGroupName }).ToList(); Dictionary<DateTime, Cat_Shift> listMonthShifts = Att_AttendanceLib.GetDailyShifts(profile.ID, dateStart, dateEnd, listRosterEntity, listRosterGroupEntity, shifts); for (DateTime date = dateStart; date <= dateEnd; date = date.AddDays(1)) { var leavdayProfiles = leaveDays.Where(s => s.DateStart.Date <= date.Date && date.Date <= s.DateEnd.Date && s.ProfileID == profile.ID).ToList(); if (leavdayProfiles.Count > 0) { bool isWorkDay = gradeCfg != null && Att_WorkDayHelper.IsWorkDay(date, gradeCfg, listMonthShifts, listHoliday); var leaveday = leavdayProfiles.FirstOrDefault(s => ledvedayPaidIds.Contains(s.LeaveDayTypeID)); if (!isWorkDay)// neu ngay do ko phai ngay di lam { if (leaveday != null && leaveDayTypeRejectIDs.Contains(leaveday.LeaveDayTypeID) || listHoliday.Exists(s => s.DateOff == date))// neu ngay do la ngay nghi dc xem la ko xem ca or ngay nghi { isWorkDay = true; } } if (isWorkDay) { var workDay = workDays.FirstOrDefault(s => s.ProfileID == profile.ID && s.WorkDate.Date == date.Date); DataRow row = table.NewRow(); Guid? orgId = profile.OrgStructureID; var org = orgs.FirstOrDefault(s => s.ID == profile.OrgStructureID); var orgBranch = LibraryService.GetNearestParent(orgId, OrgUnit.E_BRANCH, orgs, orgTypes); var orgOrg = LibraryService.GetNearestParent(orgId, OrgUnit.E_DEPARTMENT, orgs, orgTypes); var orgTeam = LibraryService.GetNearestParent(orgId, OrgUnit.E_TEAM, orgs, orgTypes); var orgSection = LibraryService.GetNearestParent(orgId, OrgUnit.E_SECTION, orgs, orgTypes); row[Att_LeaveDayEntity.FieldNames.CodeBranch] = orgBranch != null ? orgBranch.Code : string.Empty; row[Att_LeaveDayEntity.FieldNames.CodeOrg] = orgOrg != null ? orgOrg.Code : string.Empty; row[Att_LeaveDayEntity.FieldNames.CodeTeam] = orgTeam != null ? orgTeam.Code : string.Empty; row[Att_LeaveDayEntity.FieldNames.CodeSection] = orgSection != null ? orgSection.Code : string.Empty; row[Att_LeaveDayEntity.FieldNames.BranchName] = orgBranch != null ? orgBranch.OrgStructureName : string.Empty; row[Att_LeaveDayEntity.FieldNames.OrgName] = orgOrg != null ? orgOrg.OrgStructureName : string.Empty; row[Att_LeaveDayEntity.FieldNames.TeamName] = orgTeam != null ? orgTeam.OrgStructureName : string.Empty; row[Att_LeaveDayEntity.FieldNames.SectionName] = orgSection != null ? orgSection.OrgStructureName : string.Empty; var positon = positions.FirstOrDefault(s => s.ID == profile.PositionID); var jobtitle = jobtitles.FirstOrDefault(s => s.ID == profile.JobTitleID); row[Att_LeaveDayEntity.FieldNames.ProfileName] = profile.ProfileName; row[Att_LeaveDayEntity.FieldNames.CodeEmp] = profile.CodeEmp; row[Att_LeaveDayEntity.FieldNames.CodePosition] = positon != null ? positon.Code : string.Empty; row[Att_LeaveDayEntity.FieldNames.CodeJobtitle] = jobtitle != null ? jobtitle.Code : string.Empty; var shift = shifts.FirstOrDefault(s => workDay != null && s.ID == workDay.ShiftID); row[Att_LeaveDayEntity.FieldNames.Date] = date; row[Att_LeaveDayEntity.FieldNames.DateFrom] = dateStart; row[Att_LeaveDayEntity.FieldNames.DateTo] = dateEnd; row[Att_LeaveDayEntity.FieldNames.DateExport] = DateTime.Now; //row[Att_LeavedayEntity.FieldNames.UserExport] = Session[SessionObjects.UserLogin]; if (leaveday != null && leaveday.LeaveDays > 0) { row["Paid"] = "X"; } if (workDay != null) { row[Att_LeaveDayEntity.FieldNames.Cat_Shift] = shift.ShiftName; row[Att_LeaveDayEntity.FieldNames.udInTime] = workDay.InTime1 != null ? workDay.InTime1.Value.ToString("HH:mm") : string.Empty; row[Att_LeaveDayEntity.FieldNames.udOutTime] = workDay.OutTime1 != null ? workDay.OutTime1.Value.ToString("HH:mm") : string.Empty; } foreach (var leaday in leavedayTypes) { var leaday1 = leavdayProfiles.FirstOrDefault(s => s.LeaveDayTypeID == leaday.ID); if (leaday1 != null && leaday1.LeaveDays > 0) { row[leaday.Code] = "X"; } } if (leaveday != null) { row[Att_LeaveDayEntity.FieldNames.Status] = leaveday.Status; } table.Rows.Add(row); } } } } //EntityService.SubmitChanges(GuidContext, LoginUserID.Value); context.SaveChanges(); return table; } }
public string ValidateLeaveDayTimeOff(List<Guid> lstProfileId, List<Att_LeaveDay> listleaveDayInsert) { using (var context = new VnrHrmDataContext()) { var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var repoAtt_TimeOffInLieu = new Att_TimeOffInLieuRepository(unitOfWork); var repoAtt_Roster = new Att_RosterRepository(unitOfWork); var repoAtt_RosterGroup = new Att_RosterGroupRepository(unitOfWork); var repoHre_WorkHistory = new Hre_WorkHistoryRepository(unitOfWork); var repoCat_DayOff = new Cat_DayOffRepository(unitOfWork); var repoAtt_TimeOffInLieuMonth = new CustomBaseRepository<Att_TimeOffInLieuMonth>(unitOfWork); var repoCat_GradeAttendance = new CustomBaseRepository<Cat_GradeAttendance>(unitOfWork); var repoAtt_Grade = new CustomBaseRepository<Att_Grade>(unitOfWork); var repoHre_Profile = new CustomBaseRepository<Hre_Profile>(unitOfWork); var repoCat_Shift = new Cat_ShiftRepository(unitOfWork); var shifts = repoCat_Shift.FindBy(s => s.IsDelete == null).ToList(); string ErrorResult = string.Empty; Guid GuidContext = Guid.NewGuid(); DateTime dateMin = listleaveDayInsert.Min(m => m.DateStart); DateTime Datemax = listleaveDayInsert.Min(m => m.DateEnd); DateTime BeginMonthOfMin = new DateTime(dateMin.Year, dateMin.Month, 1); DateTime EndMonthOfMax = new DateTime(Datemax.Year, Datemax.Month, 1); EndMonthOfMax = EndMonthOfMax.AddMonths(1).AddMinutes(-1); DateTime Month4Ago = BeginMonthOfMin.AddMonths(-4); List<Att_TimeOffInLieu> lstTimeOffInLieu = repoAtt_TimeOffInLieu.FindBy(m => m.Date >= Month4Ago && m.Date < EndMonthOfMax && lstProfileId.Contains(m.ProfileID)).ToList<Att_TimeOffInLieu>(); List<Att_TimeOffInLieuMonth> lstTimeOffInLieu_Month = repoAtt_TimeOffInLieuMonth.FindBy(m => m.Month >= Month4Ago && m.Month < EndMonthOfMax && lstProfileId.Contains(m.ProfileID)).ToList<Att_TimeOffInLieuMonth>(); List<Cat_GradeAttendance> lstGradeCfg = repoCat_GradeAttendance.GetAll().ToList<Cat_GradeAttendance>(); var lstGrade = repoAtt_Grade.FindBy(m => lstProfileId.Contains((Guid)m.ProfileID) && m.MonthStart <= dateMin).Select(m => new { m.ProfileID, m.MonthStart, m.GradeAttendanceID }).OrderByDescending(m => m.MonthStart); string E_APPROVED_Roster = RosterStatus.E_APPROVED.ToString(); List<Att_Roster> lstRoster = repoAtt_Roster.FindBy(m => m.Status == E_APPROVED_Roster && m.DateEnd >= dateMin && m.DateStart <= Datemax && lstProfileId.Contains(m.ProfileID)).ToList<Att_Roster>(); List<Att_RosterGroup> lstRosterGroup = repoAtt_RosterGroup.FindBy(m => m.DateEnd >= dateMin && m.DateStart <= Datemax).ToList<Att_RosterGroup>(); string E_ROSTERGROUP = RosterType.E_ROSTERGROUP.ToString(); List<Att_Roster> lstRosterTypeGroup = lstRoster.Where(m => m.Type == E_ROSTERGROUP).ToList(); List<Hre_WorkHistory> lstWorkHistory = repoHre_WorkHistory.FindBy(m => lstProfileId.Contains(m.ProfileID)).ToList<Hre_WorkHistory>(); List<DateTime> lstDayOff = repoCat_DayOff.GetAll().Select(m => m.DateOff).ToList<DateTime>(); //Tạo ra một list giả dữ liệu List<LeaveDayValidate> lstLeaveDayValidate = new List<LeaveDayValidate>(); List<LeaveDayValidate> lstLeaveDayValidate1 = new List<LeaveDayValidate>(); foreach (var item in listleaveDayInsert) { LeaveDayValidate LeaveDayValid = new LeaveDayValidate(); LeaveDayValid.ProfileID = item.ProfileID; LeaveDayValid.DateStart = item.DateStart; LeaveDayValid.DateEnd = item.DateEnd; LeaveDayValid.Duration = item.LeaveHours.Value; LeaveDayValid.TotalDuration = item.LeaveDays ?? 1; lstLeaveDayValidate1.Add(LeaveDayValid); } foreach (var item in lstLeaveDayValidate1) { if (item.DateEnd.Date > item.DateStart.Date) { Dictionary<DateTime, DateTime> dicTime = new Dictionary<DateTime, DateTime>(); DateTime DateMinBeginMonth = new DateTime(item.DateStart.Year, item.DateStart.Month, 1); DateTime DateMaxBeginMonth = new DateTime(item.DateEnd.Year, item.DateEnd.Month, 1); if (DateMinBeginMonth < DateMaxBeginMonth) { Guid GradeID = Guid.Empty; var gradeByProfile = lstGrade.Where(m => m.ProfileID == item.ProfileID).FirstOrDefault(); if (gradeByProfile != null) { GradeID = (Guid)gradeByProfile.GradeAttendanceID; } Cat_GradeAttendance gradeCfg = lstGradeCfg.Where(m => m.ID == GradeID).FirstOrDefault(); if (gradeCfg == null) continue; List<Att_Roster> listRosterByProfile = lstRoster.Where(m => m.ProfileID == item.ProfileID).ToList(); List<Hre_WorkHistory> listWorkHistoryByProfile = lstWorkHistory.Where(m => m.ProfileID == item.ProfileID).ToList(); List<Att_Roster> lstRosterTypeGroupByProfile = lstRosterTypeGroup.Where(m => m.ProfileID == item.ProfileID).ToList(); var listRosterEntity = listRosterByProfile.Select(d => new Att_RosterEntity { ID = d.ID, ProfileID = d.ProfileID, RosterGroupName = d.RosterGroupName, Type = d.Type, Status = d.Status, DateEnd = d.DateEnd, DateStart = d.DateStart, MonShiftID = d.MonShiftID, TueShiftID = d.TueShiftID, WedShiftID = d.WedShiftID, ThuShiftID = d.ThuShiftID, FriShiftID = d.FriShiftID, SatShiftID = d.SatShiftID, SunShiftID = d.SunShiftID, MonShift2ID = d.MonShiftID, TueShift2ID = d.TueShift2ID, WedShift2ID = d.WedShift2ID, ThuShift2ID = d.ThuShift2ID, FriShift2ID = d.FriShift2ID, SatShift2ID = d.SatShift2ID, SunShift2ID = d.SunShift2ID }).ToList(); var listRosterGroupEntity = lstRosterGroup.Select(d => new Att_RosterGroupEntity { ID = d.ID, DateEnd = d.DateEnd, DateStart = d.DateStart, MonShiftID = d.MonShiftID, TueShiftID = d.TueShiftID, WedShiftID = d.WedShiftID, ThuShiftID = d.ThuShiftID, FriShiftID = d.FriShiftID, SatShiftID = d.SatShiftID, SunShiftID = d.SunShiftID, RosterGroupName = d.RosterGroupName }).ToList(); Dictionary<DateTime, Cat_Shift> dailyShifts = Att_AttendanceLib.GetDailyShifts(item.ProfileID, item.DateStart, item.DateEnd, listRosterEntity, listRosterGroupEntity, shifts); for (DateTime DateCheck = DateMinBeginMonth; DateCheck <= DateMaxBeginMonth; DateCheck = DateCheck.AddMonths(1)) { DateTime beginMonth = DateCheck; DateTime endMonth = beginMonth.AddMonths(1).AddMinutes(-1); if (DateCheck == DateMinBeginMonth) { dicTime.Add(item.DateStart, endMonth); } else if (DateCheck == DateMaxBeginMonth) { dicTime.Add(beginMonth, item.DateEnd); } else { dicTime.Add(beginMonth, endMonth); } } foreach (var dicTimeKey in dicTime.Keys) { DateTime DateS = dicTimeKey; DateTime DateE = dicTime[dicTimeKey]; double TotalDuration = 0; for (DateTime dateC = DateS; dateC <= DateE; dateC = dateC.AddDays(1)) { if (!lstDayOff.Contains(dateC) && dailyShifts.ContainsKey(dateC)) { if (dailyShifts[dateC] != null) { TotalDuration++; } } } LeaveDayValidate LeaveDayValid = new LeaveDayValidate(); LeaveDayValid.ProfileID = item.ProfileID; LeaveDayValid.DateStart = DateS; LeaveDayValid.DateEnd = DateE; LeaveDayValid.Duration = item.Duration; LeaveDayValid.TotalDuration = TotalDuration; lstLeaveDayValidate1.Add(LeaveDayValid); } } else { lstLeaveDayValidate.Add(item); } } else { lstLeaveDayValidate.Add(item); } } List<Guid> LstProfileIDs_Error_MissImport = new List<Guid>(); List<Guid> LstProfileIDs_Error_NotValid = new List<Guid>(); foreach (var item in lstLeaveDayValidate) { DateTime monthYear = new DateTime(item.DateStart.Year, item.DateStart.Month, 1); List<Att_TimeOffInLieu> lstTimeOffInlieu_ByProfile = lstTimeOffInLieu.Where(m => m.ProfileID == item.ProfileID).ToList(); List<Att_TimeOffInLieuMonth> lstTimeOffInlieuMonth_ByProfile = lstTimeOffInLieu_Month.Where(m => m.ProfileID == item.ProfileID).ToList(); double? NumAvailable = CalculateTotalHourTimeOff(item.ProfileID, lstTimeOffInlieu_ByProfile, lstTimeOffInlieuMonth_ByProfile, monthYear, 1); if (NumAvailable == null) { LstProfileIDs_Error_MissImport.Add(item.ProfileID); } else if ((item.Duration * item.TotalDuration) > NumAvailable.Value) { LstProfileIDs_Error_NotValid.Add(item.ProfileID); } } if (LstProfileIDs_Error_MissImport.Count > 0) { var profile = repoHre_Profile.FindBy(m => LstProfileIDs_Error_MissImport.Contains(m.ID)).Select(m => new { m.CodeEmp, m.ProfileName }); foreach (var item in profile) { ErrorResult += item.ProfileName + "[" + item.CodeEmp + "]; "; } if (ErrorResult.Length > 0) { ErrorResult = ErrorResult.Substring(0, ErrorResult.Length - 2); } ErrorResult = ConstantMessages.EmpDoNotConfigTimeOffBegin.TranslateString(); //ErrorResult = "EmpDoNotConfigTimeOffBegin"; } else if (LstProfileIDs_Error_NotValid.Count > 0) { var profile = repoHre_Profile.FindBy(m => LstProfileIDs_Error_NotValid.Contains(m.ID)).Select(m => new { m.CodeEmp, m.ProfileName }); foreach (var item in profile) { ErrorResult += item.ProfileName + "[" + item.CodeEmp + "]; "; } if (ErrorResult.Length > 0) { ErrorResult = ErrorResult.Substring(0, ErrorResult.Length - 2); } ErrorResult = ConstantMessages.DataNotEnoughToMakeLeave.TranslateString(); //ErrorResult = "EmpDoNotEnoughTimeOff"; } return ErrorResult; } }
public DataTable GetReportDetailedMonthlyStay(DateTime dateStart, DateTime dateEnd, List<Hre_ProfileEntity> profiles, string orgStructureID, bool isIncludeQuitEmp, Guid[] leaveDayTypeIDs, string codeEmp, bool isNotAllowZero, string userExport, bool IsCreateTemplate, string UserLogin) { using (var context = new VnrHrmDataContext()) { var baseservice = new BaseService(); string key = OverTimeStatus.E_APPROVED.ToString(); DataTable table = CreateReportDetailedMonthlyStaySchema(leaveDayTypeIDs, dateStart, dateEnd); if (IsCreateTemplate) { return table.ConfigTable(); } var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var repoAtt_Leaveday = new Att_LeavedayRepository(unitOfWork); var repoAtt_Roster = new Att_RosterRepository(unitOfWork); var repoCat_LeaveDayTypeRepository = new CustomBaseRepository<Cat_LeaveDayType>(unitOfWork); var repoCat_DayOff = new Cat_DayOffRepository(unitOfWork); var repoCat_Shift = new Cat_ShiftRepository(unitOfWork); var repoCat_GradeAtt = new Cat_GradeAttendanceRepository(unitOfWork); var repoHre_WorkHistory = new Hre_WorkHistoryRepository(unitOfWork); var repoAtt_Grade = new CustomBaseRepository<Att_Grade>(unitOfWork); List<Guid> lstProfileIDs = profiles.Select(s => s.ID).ToList(); List<Att_LeaveDayEntity> leaveDays = new List<Att_LeaveDayEntity>(); List<Cat_LeaveDayType> leavedaytypes = new List<Cat_LeaveDayType>(); string status = string.Empty; List<object> para = new List<object>(); para.AddRange(new object[3]); para[0] = (object)orgStructureID; para[1] = dateStart; para[2] = dateEnd; if (lstProfileIDs != null && lstProfileIDs.Count > 0) { leaveDays = baseservice.GetData<Att_LeaveDayEntity>(para, ConstantSql.hrm_att_getdata_LeaveDay, UserLogin, ref status).Where(s => s.Status == key).ToList(); } else { return table.ConfigTable(); } //[Tho.Bui] Get LeavedayR=type if (leaveDays != null && leaveDays.Count > 0) { List<Guid> lstLDTinLD = leaveDays.Select(s => s.LeaveDayTypeID).Distinct().ToList(); leavedaytypes = repoCat_LeaveDayTypeRepository.FindBy(s => s.IsDelete == null && lstLDTinLD.Contains(s.ID)).ToList(); List<Guid> leavedayguid = leaveDays.Select(s => s.LeaveDayTypeID).ToList(); leavedaytypes = leavedaytypes.Where(s => leavedayguid.Contains(s.ID)).ToList(); } var profileIds = leaveDays.Select(s => s.ProfileID).Distinct().ToList(); var rosterStatus = RosterStatus.E_APPROVED.ToString(); var _rosters = new List<Att_Roster>(); foreach (var tempProfileIds in lstProfileIDs.Chunk(1000)) { _rosters.AddRange(unitOfWork.CreateQueryable<Att_Roster>(Guid.Empty, d => tempProfileIds.Contains(d.ProfileID) && d.Status == rosterStatus && d.DateStart <= dateEnd && d.DateEnd >= dateStart).ToList()); } var rosters = new List<Att_RosterEntity>(); if (_rosters.Count > 0) rosters = _rosters.Translate<Att_RosterEntity>(); var repoCat_LeaveDayType = new Cat_LeaveDayTypeRepository(unitOfWork); var leavedayTypes = repoCat_LeaveDayType.FindBy(s => s.Code != null && s.IsDelete == null).Select(s => new { s.Code, s.ID, s.PaidRate, s.LeaveDayTypeName }).ToList(); var leavdayPadidIDs = leavedayTypes.Where(s => s.PaidRate > 0).Select(s => s.ID).ToList(); List<Guid> guids = profiles.Select(s => s.ID).ToList(); leaveDays = leaveDays.Where(s => guids.Contains(s.ProfileID)).ToList(); if (leaveDayTypeIDs != null) { leaveDays = leaveDays.Where(s => leaveDayTypeIDs.Contains(s.LeaveDayTypeID)).ToList(); profileIds = leaveDays.Select(s => s.ProfileID).ToList(); profiles = profiles.Where(s => profileIds.Contains(s.ID)).ToList(); } if (!isIncludeQuitEmp) { profiles = profiles.Where(s => s.DateQuit == null || s.DateQuit > dateEnd).ToList(); } //var shifts = repoCat_Shift.FindBy(s => s.IsDelete == null).ToList(); var shifts = unitOfWork.CreateQueryable<Cat_Shift>(Guid.Empty).ToList().Translate<Cat_ShiftEntity>(); List<string> codeRejects = new List<string>(); codeRejects.Add("SICK"); codeRejects.Add("SU"); codeRejects.Add("SD"); codeRejects.Add("D"); codeRejects.Add("DP"); codeRejects.Add("PSN"); codeRejects.Add("DSP"); codeRejects.Add("M"); List<Guid> leaveDayTypeRejectIDs = leavedayTypes.Where(s => codeRejects.Contains(s.Code)).Select(s => s.ID).ToList(); if (isNotAllowZero) { var profileIDs = leaveDays.Select(s => s.ProfileID).ToList(); profiles = profiles.Where(s => profileIDs.Contains(s.ID)).ToList(); } #region sai xong xóa string CodeEmpTest = string.Empty; if (CodeEmpTest != string.Empty) { char[] ext = new char[] { ';', ',' }; List<string> lstCodeEmpTest = CodeEmpTest.Split(ext, StringSplitOptions.RemoveEmptyEntries).ToList<string>(); profiles = profiles.Where(m => lstCodeEmpTest.Contains(m.CodeEmp)).ToList(); } #endregion var leveDayIDs = leaveDays.Select(s => s.LeaveDayTypeID).ToList(); leavedayTypes = leavedayTypes.Where(s => leveDayIDs.Contains(s.ID)).ToList(); var holidayDates = repoCat_DayOff.FindBy(hol => hol.DateOff >= dateStart && hol.DateOff <= dateEnd).Select(s => s.DateOff).ToList(); holidayDates = holidayDates.Select(s => s.Date).ToList(); var lstRosterGroup = unitOfWork.CreateQueryable<Att_RosterGroup>(Guid.Empty, s => s.DateStart != null && s.DateStart >= dateStart && s.DateEnd != null && s.DateEnd <= dateEnd).ToList().Translate<Att_RosterGroupEntity>(); foreach (var profile in profiles) { var rosterProfiles = rosters.Where(s => s.ProfileID == profile.ID).ToList(); List<DateTime> workDayDates = new List<DateTime>(); foreach (var roster in rosterProfiles) { for (DateTime date = roster.DateStart; date <= roster.DateEnd; date = date.AddDays(1)) { workDayDates.Add(date.Date); } } workDayDates = workDayDates.Where(s => !holidayDates.Contains(s.Date)).ToList(); DataRow row = table.NewRow(); var leaveDay = leaveDays.FirstOrDefault(s => s.ProfileID == profile.ID); var LeaveDayType = leavedaytypes.FirstOrDefault(s => s.ID != Guid.Empty && leaveDay != null && s.ID == leaveDay.LeaveDayTypeID); row[Att_ReportDetailedMonthlyStayEntity.FieldNames.ProfileName] = profile.ProfileName; row[Att_ReportDetailedMonthlyStayEntity.FieldNames.CodeEmp] = profile.CodeEmp; if (profile.E_COMPANY != null) row[Att_ReportDetailedMonthlyStayEntity.FieldNames.E_COMPANY] = profile.E_COMPANY; if (profile.E_BRANCH != null) row[Att_ReportDetailedMonthlyStayEntity.FieldNames.E_BRANCH] = profile.E_BRANCH; if (profile.E_UNIT != null) row[Att_ReportDetailedMonthlyStayEntity.FieldNames.E_UNIT] = profile.E_UNIT; if (profile.E_DIVISION != null) row[Att_ReportDetailedMonthlyStayEntity.FieldNames.E_DIVISION] = profile.E_DIVISION; if (profile.E_DEPARTMENT != null) row[Att_ReportDetailedMonthlyStayEntity.FieldNames.E_DEPARTMENT] = profile.E_DEPARTMENT; if (profile.E_TEAM != null) row[Att_ReportDetailedMonthlyStayEntity.FieldNames.E_TEAM] = profile.E_TEAM; if (profile.E_SECTION != null) row[Att_ReportDetailedMonthlyStayEntity.FieldNames.E_SECTION] = profile.E_SECTION; if (profile.JobTitleName != null) row[Att_ReportDetailedMonthlyStayEntity.FieldNames.JobTitleName] = profile.JobTitleName; if (profile.PositionName != null) row[Att_ReportDetailedMonthlyStayEntity.FieldNames.PositionName] = profile.PositionName; row[Att_ReportDetailedMonthlyStayEntity.FieldNames.DateFrom] = dateStart; row[Att_ReportDetailedMonthlyStayEntity.FieldNames.DateTo] = dateEnd; row[Att_ReportDetailedMonthlyStayEntity.FieldNames.UserExport] = userExport; row[Att_ReportDetailedMonthlyStayEntity.FieldNames.DateExport] = DateTime.Today; var leadayProfiles = leaveDays.Where(s => s.ProfileID == profile.ID).ToList(); var ledayeTyepIDs = leadayProfiles.Select(s => s.LeaveDayTypeID).ToList(); double sumPaid = 0; foreach (var leadayType in leavedayTypes.Where(s => ledayeTyepIDs.Contains(s.ID)).ToList()) { double sum = 0; var leavdayProfiles = leadayProfiles.Where(s => s.LeaveDayTypeID == leadayType.ID).ToList(); foreach (var leavdayProfile in leavdayProfiles) { var DateEnd = leavdayProfile.DateEnd; if (DateEnd > dateEnd) { DateEnd = dateEnd; } for (DateTime date = leavdayProfile.DateStart; date <= DateEnd; date = date.AddDays(1)) { bool isWorkDay = workDayDates.Contains(date); if (!isWorkDay)// neu ngay do ko phai ngay di lam { if (leaveDayTypeRejectIDs.Contains(leavdayProfile.LeaveDayTypeID))// neu ngay do la ngay nghi dc xem la ko xem ca or ngay nghi { isWorkDay = true; } } if (isWorkDay) { if (leavdayProfile.Duration > 0) { //sum++; sum += leavdayProfile.Duration; } if (leavdayPadidIDs.Contains(leavdayProfile.LeaveDayTypeID)) { sumPaid += leadayType.PaidRate; //sumPaid++; } } } } row[leadayType.Code] = sum > 0 ? (object)sum : DBNull.Value; row[Att_ReportDetailedMonthlyStayEntity.FieldNames.Paid] = sumPaid > 0 ? (object)sumPaid : DBNull.Value; } #region tinh so gio nghi tung ngay cua nhan vien var leavdayByProfile = leadayProfiles.Where(s => s.ProfileID == profile.ID).ToList(); Dictionary<DateTime, List<Guid?>> tempShiftByDate = new Dictionary<DateTime, List<Guid?>>(); tempShiftByDate = Att_AttendanceLib.GetDailyShifts(dateStart, dateEnd, profile.ID, rosterProfiles, lstRosterGroup); foreach (var leavdayProfile in leavdayByProfile) { for (DateTime date = dateStart; date < dateEnd; date = date.AddDays(1)) { bool isWorkDay = workDayDates.Contains(date); if (isWorkDay) { //lay ca lam viec cua nhan vien if (tempShiftByDate != null) { var lstShiftID = tempShiftByDate.Where(s => s.Key == date).SelectMany(s => s.Value).ToList(); if (lstShiftID.Count > 0) { var lstShiftProfileByDate = shifts.Where(s => s.WorkHours != null && lstShiftID.Contains(s.ID)).ToList(); double? _tempWorkHour = 0; _tempWorkHour = lstShiftProfileByDate.Sum(s => s.WorkHours); if (_tempWorkHour > 0) { row["Day" + date.Day.ToString()] = _tempWorkHour; } } } } } } #endregion table.Rows.Add(row); } var configs = new Dictionary<string, Dictionary<string, object>>(); var config = new Dictionary<string, object>(); var confighidden = new Dictionary<string, object>(); confighidden.Add("hidden", true); //var configdate = new Dictionary<string, object>(); //configdate.Add("format", "{0:dd/MM/yyyy}"); config.Add("width", 100); //config.Add("locked", true); if (!configs.ContainsKey("CodeEmp")) configs.Add("CodeEmp", config); if (!configs.ContainsKey("DepartmentCode")) configs.Add("DepartmentCode", config); if (!configs.ContainsKey("PositionName")) configs.Add("PositionName", config); if (!configs.ContainsKey("JobTitleName")) configs.Add("JobTitleName", config); if (!configs.ContainsKey("Paid")) configs.Add("Paid", config); if (!configs.ContainsKey("BranchCode")) configs.Add("BranchCode", config); if (!configs.ContainsKey("TeamCode")) configs.Add("TeamCode", config); if (!configs.ContainsKey("SectionCode")) configs.Add("SectionCode", config); if (!configs.ContainsKey("OrgName")) configs.Add("OrgName", config); if (!configs.ContainsKey("TeamName")) configs.Add("TeamName", config); if (!configs.ContainsKey("SectionName")) configs.Add("SectionName", config); if (!configs.ContainsKey("BranchName")) configs.Add("BranchName", config); if (!configs.ContainsKey("DateFrom")) configs.Add("DateFrom", confighidden); if (!configs.ContainsKey("DateTo")) configs.Add("DateTo", confighidden); if (!configs.ContainsKey("UserExport")) configs.Add("UserExport", confighidden); if (!configs.ContainsKey("DateExport")) configs.Add("DateExport", confighidden); foreach (var leavdayType in leavedayTypes) { if (!configs.ContainsKey(leavdayType.Code)) configs.Add(leavdayType.Code, config); } return table.ConfigTable(configs); } }
public string SaveContractAndNextSalaryApprovedEvaluation(Hre_ContractEntity contract, string userLogin) { using (var context = new VnrHrmDataContext()) { var contractServices = new Hre_ContractServices(); string message = string.Empty; var baseServices = new BaseService(); string status = string.Empty; var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); if (contract.DateEndNextContract == null) { return string.Empty; } #region Lấy WorkHistory var repoWorkhistory = new Hre_WorkHistoryRepository(unitOfWork); var lstWorkhistory = new List<Hre_WorkHistory>().Select(d => new { d.ProfileID, d.SalaryClassID }).ToList(); lstWorkhistory.AddRange(repoWorkhistory.FindBy(s => s.IsDelete == null).Select(d => new { d.ProfileID, d.SalaryClassID }).ToList()); #endregion // Lấy biến Dateend này gắn cho quá trình công tác khi cập nhật quá trinh công tác ở dưới DateTime? dateEnd = contract.DateEnd; var profile = baseServices.GetData<Hre_ProfileEntity>(contract.ProfileID, ConstantSql.hrm_hr_sp_get_ProfileById, userLogin, ref status).FirstOrDefault(); var lstObjSalaryRank = new List<object>(); lstObjSalaryRank.Add(null); lstObjSalaryRank.Add(null); lstObjSalaryRank.Add(1); lstObjSalaryRank.Add(int.MaxValue - 1); var lstSalaryRank = baseServices.GetData<Cat_SalaryRankEntity>(lstObjSalaryRank, ConstantSql.hrm_cat_sp_get_SalaryRank, userLogin, ref status).ToList(); #region Lấy Att_Grade var att_GradeRepository = new Att_GradeRepository(unitOfWork); var lstAttGrade = new List<Att_Grade>().Select(d => new { d.ProfileID, d.MonthEnd }).ToList(); lstAttGrade.AddRange(att_GradeRepository.FindBy(s => s.IsDelete == null).Select(d => new { d.ProfileID, d.MonthEnd }).ToList()); #endregion #region Lấy Sal_Grade var salGradeRepository = new Sal_GradeRepository(unitOfWork); var lstSalGrade = new List<Sal_Grade>().Select(d => new { d.ProfileID, d.MonthEnd }).ToList(); lstSalGrade.AddRange(salGradeRepository.FindBy(s => s.IsDelete == null).Select(d => new { d.ProfileID, d.MonthEnd }).ToList()); #endregion var lstObjGradePayroll = new List<object>(); lstObjGradePayroll.Add(null); lstObjGradePayroll.Add(null); lstObjGradePayroll.Add(1); lstObjGradePayroll.Add(int.MaxValue - 1); var lstGradePayroll = baseServices.GetData<Cat_GradePayrollEntity>(lstObjGradePayroll, ConstantSql.hrm_cat_sp_get_GradePayroll, userLogin, ref status).ToList(); var lstObjGradeAtt = new List<object>(); lstObjGradeAtt.AddRange(new object[10]); lstObjGradeAtt[8] = 1; lstObjGradeAtt[9] = int.MaxValue - 1; var lstGradeAtt = baseServices.GetData<Cat_GradeAttendanceEntity>(lstObjGradeAtt, ConstantSql.hrm_cat_sp_get_Cat_GradeAttendance, userLogin, ref status).ToList(); var lstObjCurrency = new List<object>(); lstObjCurrency.Add(null); lstObjCurrency.Add(null); lstObjCurrency.Add(1); lstObjCurrency.Add(int.MaxValue - 1); var lstCurrency = baseServices.GetData<Cat_CurrencyEntity>(lstObjCurrency, ConstantSql.hrm_cat_sp_get_Currency, userLogin, ref status).ToList(); var lstCurrencyNew = lstCurrency.Where(s => s.CurrencyName == "VND").FirstOrDefault(); var lstObjContractType = new List<object>(); lstObjContractType.Add(null); lstObjContractType.Add(null); lstObjContractType.Add(null); lstObjContractType.Add(null); lstObjContractType.Add(1); lstObjContractType.Add(int.MaxValue - 1); var lstContractType = baseServices.GetData<Cat_ContractTypeEntity>(lstObjContractType, ConstantSql.hrm_cat_sp_get_ContractType, userLogin, ref status).ToList(); var objInsuranceConfig = new List<object>(); objInsuranceConfig.Add(1); objInsuranceConfig.Add(int.MaxValue - 1); var lstInsuranceConfig = baseServices.GetData<Cat_InsuranceConfigEntity>(objInsuranceConfig, ConstantSql.hrm_cat_sp_get_InsuranceConfig, userLogin, ref status).ToList(); #region Lấy Sal_InsuranceSalary var objInsurance = new List<object>(); objInsurance.AddRange(new object[9]); objInsurance[7] = 1; objInsurance[8] = int.MaxValue - 1; var lstInsurance = baseServices.GetData<Sal_InsuranceSalaryEntity>(objInsurance, ConstantSql.hrm_sal_sp_get_InsuranceSalary, userLogin, ref status).ToList(); #endregion #region Lấy Hre_Contract var hreContractRepository = new Hre_ContractRepository(unitOfWork); var lstContractIdByProfileID = new List<Hre_Contract>().Select(d => new { d.ProfileID, d.ContractTypeID }).ToList(); lstContractIdByProfileID.AddRange(hreContractRepository.FindBy(s => s.IsDelete == null && s.ProfileID == contract.ProfileID).Select(d => new { d.ProfileID, d.ContractTypeID }).ToList()); #endregion var listIdContract = string.Empty; if (lstContractIdByProfileID != null) { listIdContract = string.Join(",", lstContractIdByProfileID.Select(d => d.ContractTypeID)); } var contractTypeEntity = new Cat_ContractTypeEntity(); if (contract.NextContractTypeID != null) { contractTypeEntity = lstContractType.Where(s => contract.NextContractTypeID.Value == s.ID).FirstOrDefault(); } else { message = ConstantMessages.WarningContractHaveNotNextContract.ToString().TranslateString(); return message; } if (contractTypeEntity != null) { if (contractTypeEntity.Type == EnumDropDown.TypeContract.E_NODURATION.ToString()) { return string.Empty; } } var workingHistoryEntity = lstWorkhistory.Where(s => s.ProfileID == contract.ProfileID).FirstOrDefault(); #region Lấy Sal_Grade by profileid var objSalGrade = new List<object>(); objSalGrade.Add(contract.ProfileID); objSalGrade.Add(null); objSalGrade.Add(1); objSalGrade.Add(int.MaxValue - 1); var salGradeByProfileIDEntity = baseServices.GetData<Sal_GradeEntity>(objSalGrade, ConstantSql.hrm_sal_sp_get_GradeAndAllownaceByProId, userLogin, ref status).FirstOrDefault(); #endregion #region Lấy Att_Grade by profileid var objAttGrade = new List<object>(); objAttGrade.Add(contract.ProfileID); objAttGrade.Add(null); objAttGrade.Add(1); objAttGrade.Add(int.MaxValue - 1); var attGradeByProfileIDEntity = baseServices.GetData<Att_GradeEntity>(objAttGrade, ConstantSql.hrm_att_sp_get_GradeAttendanceByProIdCutID, userLogin, ref status).FirstOrDefault(); #endregion if (contractTypeEntity == null) { return string.Empty; } // Theo task 0049932 - sinh ra lương với ngày hiệu lực = ngày bắt đầu hd. var dateEffect = DateTime.Now; var contracttypeByContract = lstContractType.Where(s => s.ID == contract.ContractTypeID).FirstOrDefault(); if (contract.ContractResult == EnumDropDown.ResultContract.PASS.ToString()) { if (contract.ContractEvaType == EnumDropDown.ContractEvaType.E_EXPIRED_APPRENTICE.ToString()) { //chưa tìm dc cách xử lý nên hard code var lstSalaryRankNew = new Cat_SalaryRankEntity(); if (contract.RankDetailForNextContract != null) { lstSalaryRankNew = lstSalaryRank.Where(s => contract.RankDetailForNextContract != null && s.ID == contract.RankDetailForNextContract.Value).FirstOrDefault(); } else { lstSalaryRankNew = lstSalaryRank.Where(s => contract.RankRateID != null && s.ID == contract.RankRateID.Value).FirstOrDefault(); } #region Xử lý Hre_Contract if (contract.TypeOfPass == EnumDropDown.TypeOfPass.E_SIGNED_NEXTCONTRACT.ToString()) { int month = 0; if (contractTypeEntity != null && contractTypeEntity.ValueTime != null) { month = (int)contractTypeEntity.ValueTime.Value; if (contractTypeEntity.UnitTime == HRM.Infrastructure.Utilities.EnumDropDown.UnitType.E_YEAR.ToString()) { month = month * 12; } contractTypeEntity.DateStart = contract.DateEnd.Value.AddDays(1); //chưa tìm dc cách xử lý nên hard code // var nextContractTypeID = Common.ConvertToGuid(contractTypeEntity.ContractNextID).ToString(); var contractEntity = new Hre_ContractEntity { ProfileID = contract.ProfileID, ProfileName = contract.ProfileName, DateStart = contract.DateEnd.Value.AddDays(1), DateSigned = contract.DateEnd.Value.AddDays(1), JobTitleID = contract.JobTitleID, PositionID = contract.PositionID, DateEnd = contractTypeEntity.DateStart.Value.AddMonths(month), Salary = lstSalaryRankNew == null ? 0 : lstSalaryRankNew.SalaryStandard, RankRateID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.ID, ClassRateID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.SalaryClassID, ClassRateName = lstSalaryRankNew == null ? string.Empty : lstSalaryRankNew.SalaryClassName, SalaryRankName = lstSalaryRankNew == null ? string.Empty : lstSalaryRankNew.SalaryRankName, ContractTypeID = contractTypeEntity.ID, DateEndNextContract = contract.DateEndNextContract, }; if (contract.DateEndNextContract != null) { contractEntity.DateEnd = contract.DateEndNextContract.Value; } contractEntity = SetNewCodeContract(contractEntity, listIdContract, userLogin); #region Nếu là loại hđ xác định thời hạn thì update lại cột TimesContract theo task 0049731 if (contracttypeByContract != null && contracttypeByContract.Type == HRM.Infrastructure.Utilities.EnumDropDown.TypeContract.E_DURATION.ToString()) { try { string times = contractEntity.ContractNo.Substring(contractEntity.ContractNo.Length - 1, 1); int Year = int.Parse(times); contractEntity.TimesContract = Year; } catch { } } #endregion contractEntity.Status = "E_APPROVED"; contractEntity.StatusEvaluation = "E_APPROVED"; contractEntity.DateExtend = contractEntity.DateEnd; if (!string.IsNullOrEmpty(contractEntity.ErrorMessage)) { return string.Empty; } message = contractServices.Add(contractEntity); dateEffect = contractEntity.DateStart; } else { contractTypeEntity.DateStart = contract.DateEnd.Value.AddDays(1); //chưa tìm dc cách xử lý nên hard code var contractEntity = new Hre_ContractEntity { ProfileID = contract.ProfileID, ProfileName = contract.ProfileName, DateStart = contract.DateEnd.Value.AddDays(1), DateSigned = contract.DateEnd.Value.AddDays(1), JobTitleID = contract.JobTitleID, PositionID = contract.PositionID, Salary = lstSalaryRankNew == null ? 0 : lstSalaryRankNew.SalaryStandard, RankRateID = lstSalaryRankNew == null ? contract.RankRateID : lstSalaryRankNew.ID, ClassRateID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.SalaryClassID, ClassRateName = lstSalaryRankNew == null ? string.Empty : lstSalaryRankNew.SalaryClassName, SalaryRankName = lstSalaryRankNew == null ? string.Empty : lstSalaryRankNew.SalaryRankName, ContractTypeID = contractTypeEntity.ID }; if (contract.DateEndNextContract != null) { contractEntity.DateEnd = contract.DateEndNextContract.Value; } contractEntity.DateExtend = contract.DateEnd; contractEntity.StatusEvaluation = "E_APPROVED"; if (!string.IsNullOrEmpty(contractEntity.ErrorMessage)) { return string.Empty; } message = contractServices.Add(contractEntity); dateEffect = contractEntity.DateStart; } } //Edit lai StatusEvaluation contract.StatusEvaluation = "E_APPROVED"; message = contractServices.Edit(contract); #endregion #region Xử Lý Sal_BasicSalary var salaryEntity = new Sal_BasicSalaryEntity { ProfileID = contract.ProfileID, ClassRateID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.SalaryClassID, RankRateID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.ID, GrossAmount = lstSalaryRankNew == null ? string.Empty : lstSalaryRankNew.SalaryStandard.ToString(), DateOfEffect = dateEffect, CurrencyID = lstCurrencyNew.ID, Note = contract.Remark, Status = "E_APPROVED" }; message = baseServices.Add(salaryEntity); #endregion #region Xử Lý Hre_Profile var profileEntity = profile.CopyData<Hre_ProfileEntity>(); Guid? _AbilityTileID = null; profileEntity.SalaryClassID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.SalaryClassID; if (profileEntity.SalaryClassID != null) { var abilityTitleBySalaryClass = baseServices.GetData<Cat_AbilityTileEntity>(Common.DotNetToOracle(profileEntity.SalaryClassID.ToString()), ConstantSql.hrm_cat_sp_get_AbilityTileBySalaryClassId, userLogin, ref status).FirstOrDefault(); if (abilityTitleBySalaryClass != null) { _AbilityTileID = abilityTitleBySalaryClass.ID; profileEntity.AbilityTileID = _AbilityTileID; } } baseServices.Edit(profileEntity); if (workingHistoryEntity != null) { if (workingHistoryEntity.SalaryClassID != lstSalaryRankNew.SalaryClassID) { var workhistoryEntity = new Hre_WorkHistoryEntity { ProfileID = contract.ProfileID, SalaryClassID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.SalaryClassID, DateEffective = dateEnd != null ? dateEnd.Value.AddDays(1) : DateTime.Now, AbilityTileID = _AbilityTileID, Status = "E_APPROVED" }; message = baseServices.Add(workhistoryEntity); } } else { var workhistoryEntity = new Hre_WorkHistoryEntity { ProfileID = contract.ProfileID, SalaryClassID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.SalaryClassID, DateEffective = dateEnd != null ? dateEnd.Value.AddDays(1) : DateTime.Now, Status = "E_APPROVED", }; message = baseServices.Add(workhistoryEntity); } #endregion #region Sal_Grade var lstGradeByProfileID = lstSalGrade.Where(s => contract.ProfileID == s.ProfileID).ToList().OrderByDescending(s => s.MonthEnd <= DateTime.Now).FirstOrDefault(); var lstGradePayrollByProfileID = lstGradePayroll.Where(s => s.Code == lstSalaryRankNew.Code).FirstOrDefault(); if (salGradeByProfileIDEntity != null) { if (salGradeByProfileIDEntity.GradePayrollID != lstGradePayrollByProfileID.ID) { var gradeEntity = new Sal_GradeEntity { ProfileID = contract.ProfileID, GradePayrollID = lstGradePayrollByProfileID == null ? Guid.Empty : lstGradePayrollByProfileID.ID, MonthStart = contract.DateSigned != null ? contract.DateSigned.Value : DateTime.Now, }; message = baseServices.Add(gradeEntity); } } else { var gradeEntity = new Sal_GradeEntity { // ID = lstGradeByProfileID == null ? Guid.Empty : lstGradeByProfileID.ID, ProfileID = contract.ProfileID, GradePayrollID = lstGradePayrollByProfileID == null ? Guid.Empty : lstGradePayrollByProfileID.ID, MonthStart = contract.DateSigned != null ? contract.DateSigned.Value : DateTime.Now, }; message = baseServices.Add(gradeEntity); } #endregion #region Att_Grade var lstAttGradeByProfileID = lstAttGrade.Where(s => contract.ProfileID == s.ProfileID).ToList().OrderByDescending(s => s.MonthEnd <= DateTime.Now).FirstOrDefault(); var lstGradeAttByProfileID = lstGradeAtt.Where(s => s.Code == lstSalaryRankNew.Code).FirstOrDefault(); if (attGradeByProfileIDEntity != null) { if (attGradeByProfileIDEntity.GradeAttendanceID != lstGradeAttByProfileID.ID) { var gradeAttEntity = new Att_GradeEntity { ProfileID = contract.ProfileID, GradeAttendanceID = lstAttGradeByProfileID == null ? Guid.Empty : lstGradeAttByProfileID.ID, MonthStart = contract.DateSigned != null ? contract.DateSigned.Value : DateTime.Now, }; message = baseServices.Add(gradeAttEntity); } } else { var gradeAttEntity = new Att_GradeEntity { // ID = lstGradeAttByProfileID == null ? Guid.Empty: lstAttGradeByProfileID.ID, ProfileID = contract.ProfileID, GradeAttendanceID = lstGradeAttByProfileID == null ? Guid.Empty : lstGradeAttByProfileID.ID, MonthStart = contract.DateSigned != null ? contract.DateSigned.Value : DateTime.Now, }; message = baseServices.Add(gradeAttEntity); } #endregion #region Xử Lý Lương BHXH if (contractTypeEntity.NoneTypeInsuarance == true) { var insuranceEntityByProfileID = lstInsurance.Where(s => s.ProfileID == contract.ProfileID && s.DateEffect == contract.DateEnd.Value.AddDays(1)).OrderByDescending(s => s.DateUpdate).FirstOrDefault(); var insuranceEntity = new Sal_InsuranceSalaryEntity { ProfileID = contract.ProfileID, InsuranceAmount = lstSalaryRankNew.SalaryStandard, DateEffect = dateEffect, IsSocialIns = contractTypeEntity.IsSocialInsurance == null ? null : contractTypeEntity.IsSocialInsurance, IsMedicalIns = contractTypeEntity.IsHealthInsurance == null ? null : contractTypeEntity.IsHealthInsurance, IsUnimploymentIns = contractTypeEntity.IsUnEmployInsurance == null ? null : contractTypeEntity.IsUnEmployInsurance, CurrencyID = lstCurrencyNew.ID }; if (insuranceEntityByProfileID != null) { insuranceEntityByProfileID.InsuranceAmount = lstSalaryRankNew.SalaryStandard; insuranceEntityByProfileID.IsSocialIns = contractTypeEntity.IsSocialInsurance == null ? null : contractTypeEntity.IsSocialInsurance; insuranceEntityByProfileID.IsUnimploymentIns = contractTypeEntity.IsUnEmployInsurance == null ? null : contractTypeEntity.IsUnEmployInsurance; insuranceEntityByProfileID.IsMedicalIns = contractTypeEntity.IsHealthInsurance == null ? null : contractTypeEntity.IsHealthInsurance; message = baseServices.Edit(insuranceEntityByProfileID); } else { message = baseServices.Add(insuranceEntity); } } if (contractTypeEntity.NoneTypeInsuarance == false) { var insuranceConfigEntity = lstInsuranceConfig.Where(s => s.ContractTypeID != null && s.ContractTypeID.Value == contractTypeEntity.ID).OrderByDescending(s => s.DateUpdate).FirstOrDefault(); if (insuranceConfigEntity != null) { var insuranceEntityByProfileID = lstInsurance.Where(s => s.ProfileID == contract.ProfileID && s.DateEffect == contract.DateEnd.Value.AddDays(1)).OrderByDescending(s => s.DateUpdate).FirstOrDefault(); var insuranceEntity = new Sal_InsuranceSalaryEntity { ProfileID = contract.ProfileID, InsuranceAmount = lstSalaryRankNew.SalaryStandard, DateEffect = dateEffect, IsSocialIns = insuranceConfigEntity.IsSocial == null ? null : insuranceConfigEntity.IsSocial, IsUnimploymentIns = insuranceConfigEntity.IsUnEmploy == null ? null : insuranceConfigEntity.IsUnEmploy, IsMedicalIns = insuranceConfigEntity.IsHealth == null ? null : insuranceConfigEntity.IsHealth, CurrencyID = lstCurrencyNew.ID }; if (insuranceEntityByProfileID != null) { insuranceEntityByProfileID.InsuranceAmount = lstSalaryRankNew.SalaryStandard; insuranceEntityByProfileID.IsSocialIns = insuranceConfigEntity.IsSocial == null ? null : insuranceConfigEntity.IsSocial; insuranceEntityByProfileID.IsUnimploymentIns = insuranceConfigEntity.IsUnEmploy == null ? null : insuranceConfigEntity.IsUnEmploy; insuranceEntityByProfileID.IsMedicalIns = insuranceConfigEntity.IsHealth == null ? null : insuranceConfigEntity.IsHealth; message = baseServices.Edit(insuranceEntityByProfileID); } else { message = baseServices.Add(insuranceEntity); } } } #endregion } if (contract.ContractEvaType == EnumDropDown.ContractEvaType.E_ANNUAL_EVALUATION.ToString() && contract.ContractResult == EnumDropDown.ResultContract.PASS.ToString()) { var lstSalaryRankNew = new Cat_SalaryRankEntity(); if (contract.RankDetailForNextContract != null) { lstSalaryRankNew = lstSalaryRank.Where(s => contract.RankDetailForNextContract != null && s.ID == contract.RankDetailForNextContract.Value).FirstOrDefault(); } else { lstSalaryRankNew = lstSalaryRank.Where(s => contract.RankRateID != null && s.ID == contract.RankRateID.Value).FirstOrDefault(); } #region Xử lý Hre_Contract int month = 0; if (contractTypeEntity != null && contractTypeEntity.ValueTime != null) { month = (int)contractTypeEntity.ValueTime.Value; if (contractTypeEntity.UnitTime == HRM.Infrastructure.Utilities.EnumDropDown.UnitType.E_YEAR.ToString()) { month = month * 12; } contractTypeEntity.DateStart = contract.DateEnd.Value.AddDays(1); //chưa tìm dc cách xử lý nên hard code var contractEntity = new Hre_ContractEntity { ProfileID = contract.ProfileID, ProfileName = contract.ProfileName, DateStart = new DateTime(DateTime.Now.Year, 6, 1), DateSigned = new DateTime(DateTime.Now.Year, 6, 1), JobTitleID = contract.JobTitleID, PositionID = contract.PositionID, DateEnd = contractTypeEntity.DateStart.Value.AddMonths(month), Salary = lstSalaryRankNew == null ? 0 : lstSalaryRankNew.SalaryStandard, RankRateID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.ID, ClassRateID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.SalaryClassID, ClassRateName = lstSalaryRankNew == null ? string.Empty : lstSalaryRankNew.SalaryClassName, SalaryRankName = lstSalaryRankNew == null ? string.Empty : lstSalaryRankNew.SalaryRankName, ContractTypeID = contractTypeEntity.ID }; if (contract.DateEndNextContract != null) { contractEntity.DateEnd = contract.DateEndNextContract.Value; } contractEntity = SetNewCodeContract(contractEntity, listIdContract, userLogin); #region Nếu là loại hđ xác định thời hạn thì update lại cột TimesContract theo task 0049731 if (contracttypeByContract != null && contracttypeByContract.Type == HRM.Infrastructure.Utilities.EnumDropDown.TypeContract.E_DURATION.ToString()) { try { string times = contractEntity.ContractNo.Substring(contractEntity.ContractNo.Length - 1, 1); int Year = int.Parse(times); contractEntity.TimesContract = Year; } catch { } } #endregion contractEntity.Status = "E_APPROVED"; contractEntity.DateExtend = contractEntity.DateEnd; contractEntity.StatusEvaluation = "E_APPROVED"; if (!string.IsNullOrEmpty(contractEntity.ErrorMessage)) { return string.Empty; } message = contractServices.Add(contractEntity); dateEffect = contractEntity.DateStart; } else { contractTypeEntity.DateStart = contract.DateEnd.Value.AddDays(1); //chưa tìm dc cách xử lý nên hard code var contractEntity = new Hre_ContractEntity { // ContractNo = getContractNo(item, item.DateSigned), ProfileID = contract.ProfileID, ProfileName = contract.ProfileName, DateStart = new DateTime(DateTime.Now.Year, 6, 1), DateSigned = new DateTime(DateTime.Now.Year, 6, 1), JobTitleID = contract.JobTitleID, PositionID = contract.PositionID, // DateEnd = null, Salary = lstSalaryRankNew == null ? 0 : lstSalaryRankNew.SalaryStandard, RankRateID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.ID, ClassRateID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.SalaryClassID, ClassRateName = lstSalaryRankNew == null ? string.Empty : lstSalaryRankNew.SalaryClassName, SalaryRankName = lstSalaryRankNew == null ? string.Empty : lstSalaryRankNew.SalaryRankName, ContractTypeID = contractTypeEntity.ID }; if (contract.DateEndNextContract != null) { contractEntity.DateEnd = contract.DateEndNextContract.Value; } contractEntity.DateExtend = contract.DateEnd; contractEntity.StatusEvaluation = "E_APPROVED"; if (!string.IsNullOrEmpty(contractEntity.ErrorMessage)) { return string.Empty; } message = contractServices.Add(contractEntity); dateEffect = contractEntity.DateStart; } //Edit lai StatusEvaluation contract.StatusEvaluation = "E_APPROVED"; message = contractServices.Edit(contract); #endregion #region Xử Lý Sal_BasicSalary var salaryEntity = new Sal_BasicSalaryEntity { ProfileID = contract.ProfileID, ClassRateID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.SalaryClassID, RankRateID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.ID, GrossAmount = lstSalaryRankNew == null ? string.Empty : lstSalaryRankNew.SalaryStandard.ToString(), DateOfEffect = new DateTime(DateTime.Now.Year, 6, 1), CurrencyID = lstCurrencyNew.ID, Note = contract.Remark, Status = "E_APPROVED" }; message = baseServices.Add(salaryEntity); #endregion #region Xử Lý Hre_Profile var profileEntity = profile.CopyData<Hre_ProfileEntity>(); profileEntity.SalaryClassID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.SalaryClassID; Guid? _AbilityTileID = null; if (profileEntity.SalaryClassID != null) { var abilityTitleBySalaryClass = baseServices.GetData<Cat_AbilityTileEntity>(Common.DotNetToOracle(profileEntity.SalaryClassID.ToString()), ConstantSql.hrm_cat_sp_get_AbilityTileBySalaryClassId, userLogin, ref status).FirstOrDefault(); if (abilityTitleBySalaryClass != null) { _AbilityTileID = abilityTitleBySalaryClass.ID; profileEntity.AbilityTileID = _AbilityTileID; } } message = baseServices.Edit(profileEntity); if (workingHistoryEntity != null) { if (workingHistoryEntity.SalaryClassID != lstSalaryRankNew.SalaryClassID) { var workhistoryEntity = new Hre_WorkHistoryEntity { ProfileID = contract.ProfileID, SalaryClassID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.SalaryClassID, DateEffective = dateEnd != null ? dateEnd.Value.AddDays(1) : DateTime.Now, AbilityTileID = _AbilityTileID, Status = "E_APPROVED" }; message = baseServices.Add(workhistoryEntity); } } else { var workhistoryEntity = new Hre_WorkHistoryEntity { ProfileID = contract.ProfileID, SalaryClassID = lstSalaryRankNew == null ? Guid.Empty : lstSalaryRankNew.SalaryClassID, DateEffective = dateEnd != null ? dateEnd.Value.AddDays(1) : DateTime.Now, Status = "E_APPROVED" }; message = baseServices.Add(workhistoryEntity); } #endregion #region Sal_Grade var lstGradeByProfileID = lstSalGrade.Where(s => contract.ProfileID == s.ProfileID).ToList().OrderByDescending(s => s.MonthEnd <= DateTime.Now).FirstOrDefault(); var lstGradePayrollByProfileID = lstGradePayroll.Where(s => s.Code == lstSalaryRankNew.Code).FirstOrDefault(); if (salGradeByProfileIDEntity != null) { if (salGradeByProfileIDEntity.GradePayrollID != lstGradePayrollByProfileID.ID) { var gradeEntity = new Sal_GradeEntity { // ID = lstGradeByProfileID == null ? Guid.Empty : lstGradeByProfileID.ID, ProfileID = contract.ProfileID, GradePayrollID = lstGradePayrollByProfileID == null ? Guid.Empty : lstGradePayrollByProfileID.ID, MonthStart = new DateTime(DateTime.Now.Year, 6, 1), }; message = baseServices.Add(gradeEntity); } } else { var gradeEntity = new Sal_GradeEntity { // ID = lstGradeByProfileID == null ? Guid.Empty : lstGradeByProfileID.ID, ProfileID = contract.ProfileID, GradePayrollID = lstGradePayrollByProfileID == null ? Guid.Empty : lstGradePayrollByProfileID.ID, MonthStart = new DateTime(DateTime.Now.Year, 6, 1) }; message = baseServices.Add(gradeEntity); } #endregion #region Att_Grade var lstAttGradeByProfileID = lstAttGrade.Where(s => contract.ProfileID == s.ProfileID).ToList().OrderByDescending(s => s.MonthEnd <= DateTime.Now).FirstOrDefault(); var lstGradeAttByProfileID = lstGradeAtt.Where(s => s.Code == lstSalaryRankNew.Code).FirstOrDefault(); if (attGradeByProfileIDEntity != null) { if (attGradeByProfileIDEntity.GradeAttendanceID != lstGradeAttByProfileID.ID) { var gradeAttEntity = new Att_GradeEntity { //ID = lstAttGradeByProfileID == null ? Guid.Empty: lstAttGradeByProfileID.ID, ProfileID = contract.ProfileID, GradeAttendanceID = lstGradeAttByProfileID == null ? Guid.Empty : lstGradeAttByProfileID.ID, MonthStart = new DateTime(DateTime.Now.Year, 6, 1) }; message = baseServices.Add(gradeAttEntity); } } else { var gradeAttEntity = new Att_GradeEntity { //ID = lstAttGradeByProfileID == null ? Guid.Empty: lstAttGradeByProfileID.ID, ProfileID = contract.ProfileID, GradeAttendanceID = lstGradeAttByProfileID == null ? Guid.Empty : lstGradeAttByProfileID.ID, MonthStart = new DateTime(DateTime.Now.Year, 6, 1) }; message = baseServices.Add(gradeAttEntity); } #endregion #region Xử Lý Lương BHXH if (contractTypeEntity.NoneTypeInsuarance == true) { var insuranceEntityByProfileID = lstInsurance.Where(s => s.ProfileID == contract.ProfileID && s.DateEffect == contract.DateEnd.Value.AddDays(1)).OrderByDescending(s => s.DateUpdate).FirstOrDefault(); var insuranceEntity = new Sal_InsuranceSalaryEntity { ProfileID = contract.ProfileID, InsuranceAmount = lstSalaryRankNew.SalaryStandard, DateEffect = dateEffect, IsSocialIns = contractTypeEntity.IsSocialInsurance == null ? null : contractTypeEntity.IsSocialInsurance, IsUnimploymentIns = contractTypeEntity.IsUnEmployInsurance == null ? null : contractTypeEntity.IsUnEmployInsurance, IsMedicalIns = contractTypeEntity.IsHealthInsurance == null ? null : contractTypeEntity.IsHealthInsurance, CurrencyID = lstCurrencyNew.ID, }; if (insuranceEntityByProfileID != null) { insuranceEntityByProfileID.InsuranceAmount = lstSalaryRankNew.SalaryStandard; insuranceEntityByProfileID.IsSocialIns = contractTypeEntity.IsSocialInsurance == null ? null : contractTypeEntity.IsSocialInsurance; insuranceEntityByProfileID.IsUnimploymentIns = contractTypeEntity.IsUnEmployInsurance == null ? null : contractTypeEntity.IsUnEmployInsurance; insuranceEntityByProfileID.IsMedicalIns = contractTypeEntity.IsHealthInsurance == null ? null : contractTypeEntity.IsHealthInsurance; message = baseServices.Edit(insuranceEntityByProfileID); } else { message = baseServices.Add(insuranceEntity); } } if (contractTypeEntity.NoneTypeInsuarance == false) { var insuranceConfigEntity = lstInsuranceConfig.Where(s => s.ContractTypeID != null && s.ContractTypeID.Value == contractTypeEntity.ID).OrderByDescending(s => s.DateUpdate).FirstOrDefault(); if (insuranceConfigEntity != null) { var insuranceEntityByProfileID = lstInsurance.Where(s => s.ProfileID == contract.ProfileID && s.DateEffect == contract.DateEnd.Value.AddDays(1)).OrderByDescending(s => s.DateUpdate).FirstOrDefault(); var insuranceEntity = new Sal_InsuranceSalaryEntity { ProfileID = contract.ProfileID, InsuranceAmount = lstSalaryRankNew.SalaryStandard, DateEffect = dateEffect, IsSocialIns = insuranceConfigEntity.IsSocial == null ? null : insuranceConfigEntity.IsSocial, IsUnimploymentIns = insuranceConfigEntity.IsUnEmploy == null ? null : insuranceConfigEntity.IsUnEmploy, IsMedicalIns = insuranceConfigEntity.IsHealth == null ? null : insuranceConfigEntity.IsHealth, CurrencyID = lstCurrencyNew.ID }; if (insuranceEntityByProfileID != null) { insuranceEntityByProfileID.InsuranceAmount = lstSalaryRankNew.SalaryStandard; insuranceEntityByProfileID.IsSocialIns = insuranceConfigEntity.IsSocial == null ? null : insuranceConfigEntity.IsSocial; insuranceEntityByProfileID.IsUnimploymentIns = insuranceConfigEntity.IsUnEmploy == null ? null : insuranceConfigEntity.IsUnEmploy; insuranceEntityByProfileID.IsMedicalIns = insuranceConfigEntity.IsHealth == null ? null : insuranceConfigEntity.IsHealth; message = baseServices.Edit(insuranceEntityByProfileID); } else { message = baseServices.Add(insuranceEntity); } } } #endregion } } return message; } }