Beispiel #1
0
        public static void GetRosterGroup(List<Guid> lstProfileID, DateTime? DateFrom, DateTime? DateTo, out List<Att_Roster> lstRosterTypeGroup, out List<Att_RosterGroup> lstRosterGroup)
        {
            using (var context = new VnrHrmDataContext())
            {
                var unitOfWork = (IUnitOfWork)(new UnitOfWork(context));
                lstRosterGroup = new List<Att_RosterGroup>();
                lstRosterTypeGroup = new List<Att_Roster>();

                string E_APPROVED = RosterStatus.E_APPROVED.ToString();
                string E_ROSTERGROUP = RosterType.E_ROSTERGROUP.ToString();
                var repoAtt_Roster = new Att_RosterRepository(unitOfWork);
                var repoAtt_RosterGroup = new Att_RosterGroupRepository(unitOfWork);
                if (DateFrom == null || DateTo == null)
                {
                    lstRosterTypeGroup = repoAtt_Roster.FindBy(m => lstProfileID.Contains(m.ProfileID) && m.Status == E_APPROVED && m.Type == E_ROSTERGROUP).ToList<Att_Roster>();
                    lstRosterGroup = repoAtt_RosterGroup.FindBy(m => m.DateStart != null && m.DateEnd != null).ToList<Att_RosterGroup>();
                }
                else
                {
                    lstRosterTypeGroup = repoAtt_Roster.FindBy(m => lstProfileID.Contains(m.ProfileID) && m.Status == E_APPROVED && m.Type == E_ROSTERGROUP && m.DateStart <= DateTo).ToList<Att_Roster>();
                    lstRosterGroup = repoAtt_RosterGroup.FindBy(m => m.DateStart != null && m.DateEnd != null && m.DateStart <= DateTo && m.DateEnd >= DateFrom).ToList<Att_RosterGroup>();
                }
            }
        }
Beispiel #2
0
        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;
            }
        }