Exemple #1
0
        /// <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;
            }
        }
Exemple #2
0
        public DataTable GetReportDiligenceYear(DateTime DateFrom, DateTime DateTo, string orgStructureID, List<Hre_ProfileEntity> profiles, string userExport)
        {
            using (var context = new VnrHrmDataContext())
            {
                var unitOfWork = (IUnitOfWork)(new UnitOfWork(context));
                DataTable table = CreateReportDiligenceYearSchema();
                BaseService baseservice = new BaseService();
                var repoCat_OrgStructure = new Cat_OrgStructureRepository(unitOfWork);
                var orgStructures = repoCat_OrgStructure.FindBy(s => s.IsDelete == null && s.Code != null).ToList();

                var shifts = unitOfWork.CreateQueryable<Cat_Shift>().ToList();
                DateTo = DateTo.AddDays(1).AddSeconds(-1);
                string status = string.Empty;
                var lstOrgIDs = new List<Guid>();
                List<string> lstOrgString = new List<string>();
                if (orgStructureID != null)
                {
                    lstOrgString = orgStructureID.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList<string>();
                }
                foreach (var item in lstOrgString)
                {
                    Guid OrgID = Guid.Empty;
                    Guid.TryParse(item, out OrgID);
                    if (OrgID != Guid.Empty)
                    {
                        lstOrgIDs.Add(OrgID);
                    }
                }
                var RosterQuery = unitOfWork.CreateQueryable<Att_Roster>(m => m.DateStart <= DateTo && m.DateEnd >= DateFrom);
                if (lstOrgIDs.Count > 0)
                {
                    RosterQuery = RosterQuery.Where(m => m.OrgStructureID != null && lstOrgIDs.Contains(m.OrgStructureID.Value));
                }
                var rosters = RosterQuery.ToList();
                var lstProfileIDsByOrg = rosters.Select(m => m.ProfileID).Distinct().ToList();
                profiles = profiles.Where(m => lstProfileIDsByOrg.Contains(m.ID)).ToList();
                List<Guid> Profileguids = profiles.Select(s => s.ID).ToList();
                List<string> cardcodes = profiles.Select(s => s.CodeAttendance).Distinct().ToList();
                List<string> scan = unitOfWork.CreateQueryable<Att_TAMScanLog>(m => m.TimeLog >= DateFrom && m.TimeLog <= DateTo && cardcodes.Contains(m.CardCode)).Select(s => s.CardCode).ToList<string>();
                var repoAtt_Grade = new Att_GradeRepository(unitOfWork);
                var grades = repoAtt_Grade.FindBy(s => s.IsDelete == null && s.MonthStart <= DateTo && Profileguids.Contains(s.ProfileID.Value)).OrderByDescending(prop => prop.MonthStart).ToList();

                // var grades = unitOfWork.CreateQueryable<Att_Grade>(s => s.IsDelete == null && s.MonthStart <= DateTo && Profileguids.Contains(s.ProfileID.Value)).ToList().Translate<Att_Grade>();
                string E_ISDEFAULT = GradeRosterType.E_ISDEFAULT.ToString();
                string E_ISROSTER_ORG = GradeRosterType.E_ISROSTER_ORG.ToString();
                List<Att_Grade> gradesDefault = grades.Where(s => s.Cat_GradeCfg != null && s.Cat_GradeCfg.RosterType == E_ISDEFAULT).ToList();
                List<Att_Grade> gradesOrg = grades.Where(s => s.Cat_GradeCfg != null && s.Cat_GradeCfg.RosterType == E_ISROSTER_ORG).ToList();
                for (DateTime date = DateFrom.Date; date <= DateTo; date = date.AddDays(1))
                {
                    foreach (Cat_OrgStructure catOrgStructure in orgStructures)
                    {
                        List<Att_Roster> attRosters = rosters.Where(s => s.OrgStructureID == catOrgStructure.ID).ToList();
                        var profilesIID = attRosters.Select(m => m.ProfileID).Distinct().ToList();
                        List<Guid> guidsAll = new List<Guid>();
                        foreach (Cat_Shift catShift in shifts)
                        {
                            List<Guid> profileIDRoster = attRosters.Where(s => s.MonShiftID == catShift.ID || s.TueShiftID == catShift.ID || s.WedShiftID == catShift.ID || s.ThuShiftID == catShift.ID || s.FriShiftID == catShift.ID || s.SatShiftID == catShift.ID || s.SunShiftID == catShift.ID).Select(s => s.ProfileID).ToList();
                            List<Guid> profileIDDefault = gradesDefault
                                .Where(
                                        s => profilesIID.Contains(s.ProfileID.Value)
                                            && (s.Cat_GradeCfg.WorkOnMondayID == catShift.ID
                                            || s.Cat_GradeCfg.WorkOnTuesdayID == catShift.ID
                                            || s.Cat_GradeCfg.WorkOnWednesdayID == catShift.ID
                                            || s.Cat_GradeCfg.WorkOnThursdayID == catShift.ID
                                            || s.Cat_GradeCfg.WorkOnFridayID == catShift.ID
                                            || s.Cat_GradeCfg.WorkOnSaturdayID == catShift.ID
                                            || s.Cat_GradeCfg.WorkOnSundayID == catShift.ID))
                                .Select(s => s.ProfileID.Value).ToList();
                            List<Guid> profileIDgradesOrg = gradesOrg
                                .Where(
                                        s => (s.Cat_GradeCfg.WorkOnMondayID == catShift.ID
                                            || s.Cat_GradeCfg.WorkOnTuesdayID == catShift.ID
                                            || s.Cat_GradeCfg.WorkOnWednesdayID == catShift.ID
                                            || s.Cat_GradeCfg.WorkOnThursdayID == catShift.ID
                                            || s.Cat_GradeCfg.WorkOnFridayID == catShift.ID
                                            || s.Cat_GradeCfg.WorkOnSaturdayID == catShift.ID
                                            || s.Cat_GradeCfg.WorkOnSundayID == catShift.ID)
                                            && profilesIID.Contains(s.ProfileID.Value))
                                            .Select(s => s.ProfileID.Value).ToList();
                            guidsAll.AddRange(profileIDRoster);
                            guidsAll.AddRange(profileIDDefault);
                            guidsAll.AddRange(profileIDgradesOrg);
                        }
                        List<string> codeAtts = profiles.Where(s => guidsAll.Contains(s.ID)).Select(s => s.CodeAttendance).ToList();
                        DataRow row = table.NewRow();
                        row[Att_ReportDiligenceYearEntity.FieldNames.OrgStructureName] = catOrgStructure.OrgStructureName;
                        row[Att_ReportDiligenceYearEntity.FieldNames.DepartmentCode] = catOrgStructure.OrgStructureName;
                        row[Att_ReportDiligenceYearEntity.FieldNames.Date] = date;
                        row[Att_ReportDiligenceYearEntity.FieldNames.Sizes] = guidsAll.Count;
                        row[Att_ReportDiligenceYearEntity.FieldNames.DateFrom] = DateFrom;
                        row[Att_ReportDiligenceYearEntity.FieldNames.DateTo] = DateTo;
                        row[Att_ReportDiligenceYearEntity.FieldNames.UserExport] = userExport;
                        row[Att_ReportDiligenceYearEntity.FieldNames.DateExport] = DateTime.Today;
                        guidsAll = guidsAll.Distinct().ToList();
                        codeAtts = codeAtts.Where(s => !string.IsNullOrWhiteSpace(s) && !scan.Contains(s)).ToList();
                        double absence = codeAtts.Count();
                        row[Att_ReportDiligenceYearEntity.FieldNames.Absence] = absence;
                        if (guidsAll.Count > 0 || absence > 0)
                            table.Rows.Add(row);
                    }
                }

                return table;
            }

        }
Exemple #3
0
        public static List<Att_Grade> GetAllGrade(List<Guid> listProfileId, DateTime monthYear)
        {
            using (var context = new VnrHrmDataContext())
            {
                var unitOfWork = (IUnitOfWork)(new UnitOfWork(context));
                List<Att_Grade> lst = new List<Att_Grade>();
                var repoAtt_Grade = new Att_GradeRepository(unitOfWork);
                lst = repoAtt_Grade.FindBy(gd => listProfileId.Contains(gd.ProfileID.Value)
                    && gd.MonthStart <= monthYear).OrderByDescending(prop => prop.MonthStart).ToList();

                List<Att_Grade> lstGrade = new List<Att_Grade>();
                foreach (Att_Grade grade in lst)
                {
                    Att_Grade grade1 = lstGrade.FirstOrDefault(prop => prop.ProfileID == grade.ProfileID);
                    if (grade1 == null)
                    {
                        lstGrade.Add(grade);
                    }
                }
                return lstGrade;
            }
        }
Exemple #4
0
        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;
            }
        }