Esempio n. 1
0
        public void UpdateUserMasterDataGroup(Guid userID, List<Guid?> listMasterDataGroupID)
        {
            if (userID != Guid.Empty && listMasterDataGroupID != null)
            {
                using (var context = new VnrHrmDataContext())
                {
                    IUnitOfWork unitOfWork = new UnitOfWork(context);

                    var listMasterDataGroupNumber = unitOfWork.CreateQueryable<Cat_MasterDataGroup>(d => d.OrderNumber.HasValue
                        && listMasterDataGroupID.Contains(d.ID)).Select(d => d.OrderNumber.Value).ToList();

                    var userMasterDataGroup = unitOfWork.CreateQueryable<Sys_UserMasterData>(d =>
                        d.UserID == userID).FirstOrDefault();

                    if (userMasterDataGroup == null)
                    {
                        userMasterDataGroup = new Sys_UserMasterData
                        {
                            UserID = userID
                        };

                        unitOfWork.AddObject(typeof(Sys_UserMasterData), userMasterDataGroup);
                    }

                    userMasterDataGroup.MasterDataGroups = listMasterDataGroupNumber.ToBinary();
                    userMasterDataGroup.MasterDataGroup = string.Join(",", listMasterDataGroupNumber);
                    unitOfWork.SaveChanges(userID);
                }
            }

        }
Esempio n. 2
0
 public Guid CreateComputingTask(Guid userID, DateTime monthYear)
 {
     using (var context = new VnrHrmDataContext())
     {
         IUnitOfWork unitOfWork = new UnitOfWork(context);
         Sys_AsynTask task = new Sys_AsynTask();
         task.ID = Guid.NewGuid();
         task.PercentComplete = 0.01D;
         task.TimeStart = DateTime.Now;
         task.Status = AsynTaskStatus.Doing.ToString();
         task.Type = AsynTask.Attendance_Computing.ToString();
         task.Summary = "Attendance: " + monthYear.ToString("MM/yyyy");
         unitOfWork.AddObject(typeof(Sys_AsynTask), task);
         unitOfWork.SaveChanges(userID);
         return task.ID;
     }
 }
Esempio n. 3
0
        public Guid CreateComputingTask(Guid userID, DateTime dateFrom, DateTime dateTo)
        {
            #region Khởi tạo Sys_AsynTask cho mỗi lần xử lý

            using (var context = new VnrHrmDataContext())
            {
                IUnitOfWork  unitOfWork = new UnitOfWork(context);
                Sys_AsynTask task       = new Sys_AsynTask();
                task.ID = Guid.NewGuid();
                task.PercentComplete = 0.01D;
                task.TimeStart       = DateTime.Now;
                task.Status          = AsynTaskStatus.Doing.ToString();
                task.Type            = AsynTask.Download_TimeLog.ToString();
                task.Summary         = "TimeLog: " + dateFrom.ToString("dd/MM/yyyy") + " - " + dateTo.ToString("dd/MM/yyyy");
                unitOfWork.AddObject(typeof(Sys_AsynTask), task);
                unitOfWork.SaveChanges(userID);
                return(task.ID);
            }

            #endregion
        }
Esempio n. 4
0
        public Guid CreateComputingTask(Guid userID, DateTime dateFrom, DateTime dateTo)
        {
            #region Khởi tạo Sys_AsynTask cho mỗi lần xử lý

            using (var context = new VnrHrmDataContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                Sys_AsynTask task = new Sys_AsynTask();
                task.ID = Guid.NewGuid();
                task.PercentComplete = 0.01D;
                task.TimeStart = DateTime.Now;
                task.Status = AsynTaskStatus.Doing.ToString();
                task.Type = AsynTask.Download_TimeLog_CMS.ToString();
                task.Summary = "CMS TimeLog: " + dateFrom.ToString("dd/MM/yyyy") + " - " + dateTo.ToString("dd/MM/yyyy");
                unitOfWork.AddObject(typeof(Sys_AsynTask), task);
                unitOfWork.SaveChanges(userID);
                return task.ID;
            }

            #endregion
        }
Esempio n. 5
0
        public List <Att_TAMScanLog> SaveTAMLog(DataTable table, DateTime dateFrom,
                                                DateTime dateTo, Guid userID, List <Guid> listProfileID, bool filterByCodeEmp)
        {
            using (var context = new VnrHrmDataContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);

                if (table.Columns.Count == 2)
                {
                    table.Columns.Add();
                    table.Columns.Add();
                }
                else if (table.Columns.Count == 3)
                {
                    table.Columns.Add();
                }

                DataRow[] rows = new DataRow[table.Rows.Count];
                table.Rows.CopyTo(rows, 0);

                string waitStatus = ProfileStatusSyn.E_WAITING.ToString();
                List <Att_TAMScanLog> listTAMScanLog = new List <Att_TAMScanLog>();

                var profileQueryable = unitOfWork.CreateQueryable <Hre_Profile>(userID, pro => (pro.StatusSyn == null ||
                                                                                                pro.StatusSyn != waitStatus) && (pro.DateQuit == null || pro.DateQuit.Value > dateFrom));

                var cardHistoryQueryable = unitOfWork.CreateQueryable <Hre_CardHistory>(userID);

                if (listProfileID != null && listProfileID.Count() > 0)
                {
                    profileQueryable     = profileQueryable.Where(pro => listProfileID.Contains(pro.ID));
                    cardHistoryQueryable = cardHistoryQueryable.Where(pro => pro.ProfileID.HasValue && listProfileID.Contains(pro.ProfileID.Value));
                }
                else
                {
                    cardHistoryQueryable = cardHistoryQueryable.Where(cr => cr.Hre_Profile != null &&
                                                                      (!cr.Hre_Profile.IsDelete.HasValue || cr.Hre_Profile.IsDelete == false));
                }

                var listAllProfile = profileQueryable.Select(d =>
                                                             new Hre_ProfileEntity
                {
                    ID      = d.ID,
                    CodeEmp = d.CodeEmp
                }).ToList <Hre_ProfileEntity>();

                var listAllCardHistory = cardHistoryQueryable.Select(d =>
                                                                     new Hre_CardHistoryEntity
                {
                    ID         = d.ID,
                    ProfileID  = d.ProfileID,
                    CardCode   = d.CardCode,
                    DateEffect = d.DateEffect
                }).ToList <Hre_CardHistoryEntity>();

                for (int i = 0; i < rows.Length; i++)
                {
                    DataRow row      = rows[i];
                    string  cardCode = row[0].ToString();
                    cardCode = cardCode.ToUpper().Trim();

                    Att_TAMScanLog tam = new Att_TAMScanLog();

                    if (filterByCodeEmp)
                    {
                        tam.CodeEmp = cardCode;
                        tam.Comment = "Hệ thống cũ";
                    }
                    else
                    {
                        tam.CardCode = cardCode;
                    }

                    tam.TimeLog   = Convert.ToDateTime(row[1].ToString().Trim());
                    tam.Type      = row[2].ToString().Trim();
                    tam.MachineNo = row[3].ToString().Trim();
                    tam.Status    = TAMScanStatus.E_LOADED.ToString();
                    List <Hre_ProfileEntity> listProfileByCode = null;

                    if (!string.IsNullOrWhiteSpace(tam.CardCode))
                    {
                        var listCardHistoryByCode = listAllCardHistory.Where(ch => ch.CardCode != null &&
                                                                             ch.CardCode.ToString().ToUpper().Trim() == cardCode).ToList();

                        if (listCardHistoryByCode.Count() <= 0)
                        {
                            listProfileByCode = listAllProfile.Where(d => d.CodeAttendance != null &&
                                                                     d.CodeAttendance.ToString().ToUpper().Trim() == cardCode).ToList();
                        }
                        else
                        {
                            var listProfileIDByCode = listCardHistoryByCode.Select(d => d.ProfileID).ToList();
                            listProfileByCode = listAllProfile.Where(d => listProfileIDByCode.Contains(d.ID)).ToList();
                        }
                    }
                    else if (!string.IsNullOrWhiteSpace(tam.CodeEmp))
                    {
                        listProfileByCode = listAllProfile.Where(d => d.CodeEmp != null &&
                                                                 d.CodeEmp.ToString().ToUpper().Trim() == cardCode).ToList();
                    }

                    if (listProfileByCode != null && listProfileByCode.Count > 0)
                    {
                        var dateStart = tam.TimeLog.HasValue ? tam.TimeLog.Value.Date : dateFrom;
                        var dateEnd   = tam.TimeLog.HasValue ? tam.TimeLog.Value.Date.AddDays(1) : dateTo;
                        var rec       = ConvertToRecord(tam, listProfileByCode, listAllCardHistory, dateStart, dateEnd);

                        if (rec != null)
                        {
                            listTAMScanLog.Add(rec);
                        }
                    }
                }

                DeleteTAMLog(unitOfWork, dateFrom, dateTo, listTAMScanLog, userID);
                unitOfWork.AddObject(listTAMScanLog.ToArray());
                unitOfWork.SaveChanges(userID);
                return(listTAMScanLog);
            }
        }
Esempio n. 6
0
        public List<Can_TamScanLogCMS> SaveTAMLog(DataTable table, DateTime dateFrom,
            DateTime dateTo, Guid userID, List<Guid> listProfileID, bool filterByCodeEmp)
        {
            using (var context = new VnrHrmDataContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);

                if (table.Columns.Count == 2)
                {
                    table.Columns.Add();
                    table.Columns.Add();
                }
                else if (table.Columns.Count == 3)
                {
                    table.Columns.Add();
                }

                DataRow[] rows = new DataRow[table.Rows.Count];
                table.Rows.CopyTo(rows, 0);

                string waitStatus = ProfileStatusSyn.E_WAITING.ToString();
                List<Can_TamScanLogCMS> listTAMScanLog = new List<Can_TamScanLogCMS>();

                var profileQueryable = unitOfWork.CreateQueryable<Hre_Profile>(userID, pro => (pro.StatusSyn == null
                    || pro.StatusSyn != waitStatus) && (pro.DateQuit == null || pro.DateQuit.Value > dateFrom));

                var cardHistoryQueryable = unitOfWork.CreateQueryable<Hre_CardHistory>(userID);

                if (listProfileID != null && listProfileID.Count() > 0)
                {
                    profileQueryable = profileQueryable.Where(pro => listProfileID.Contains(pro.ID));
                    cardHistoryQueryable = cardHistoryQueryable.Where(pro => pro.ProfileID.HasValue && listProfileID.Contains(pro.ProfileID.Value));
                }
                else
                {
                    cardHistoryQueryable = cardHistoryQueryable.Where(cr => cr.Hre_Profile != null
                        && (!cr.Hre_Profile.IsDelete.HasValue || cr.Hre_Profile.IsDelete == false));
                }

                var listAllProfile = profileQueryable.Select(d =>
                    new Hre_ProfileEntity
                    {
                        ID = d.ID,
                        CodeEmp = d.CodeEmp
                    }).ToList<Hre_ProfileEntity>();

                var listAllCardHistory = cardHistoryQueryable.Select(d =>
                    new Hre_CardHistoryEntity
                    {
                        ID = d.ID,
                        ProfileID = d.ProfileID,
                        CardCode = d.CardCode,
                        DateEffect = d.DateEffect
                    }).ToList<Hre_CardHistoryEntity>();

                for (int i = 0; i < rows.Length; i++)
                {
                    DataRow row = rows[i];
                    string cardCode = row[0].ToString();
                    cardCode = cardCode.ToUpper().Trim();

                    Can_TamScanLogCMS tam = new Can_TamScanLogCMS();

                    if (filterByCodeEmp)
                    {
                        tam.CodeEmp = cardCode;
                        tam.Comment = "Hệ thống cũ";
                    }
                    else
                    {
                        tam.CardCode = cardCode;
                    }

                    tam.TimeLog = Convert.ToDateTime(row[1].ToString().Trim());
                    tam.Type = row[2].ToString().Trim();
                    tam.MachineCode = row[3].ToString().Trim();
                    tam.Status = TAMScanStatus.E_LOADED.ToString();
                    List<Hre_ProfileEntity> listProfileByCode = null;

                    if (!string.IsNullOrWhiteSpace(tam.CardCode))
                    {
                        var listCardHistoryByCode = listAllCardHistory.Where(ch => ch.CardCode != null
                            && ch.CardCode.ToString().ToUpper().Trim() == cardCode).ToList();

                        if (listCardHistoryByCode.Count() <= 0)
                        {
                            listProfileByCode = listAllProfile.Where(d => d.CodeAttendance != null
                                && d.CodeAttendance.ToString().ToUpper().Trim() == cardCode).ToList();
                        }
                        else
                        {
                            var listProfileIDByCode = listCardHistoryByCode.Select(d => d.ProfileID).ToList();
                            listProfileByCode = listAllProfile.Where(d => listProfileIDByCode.Contains(d.ID)).ToList();
                        }
                    }
                    else if (!string.IsNullOrWhiteSpace(tam.CodeEmp))
                    {
                        listProfileByCode = listAllProfile.Where(d => d.CodeEmp != null
                            && d.CodeEmp.ToString().ToUpper().Trim() == cardCode).ToList();
                    }

                    if (listProfileByCode != null && listProfileByCode.Count > 0)
                    {
                        var dateStart = tam.TimeLog.HasValue ? tam.TimeLog.Value.Date : dateFrom;
                        var dateEnd = tam.TimeLog.HasValue ? tam.TimeLog.Value.Date.AddDays(1) : dateTo;
                        var rec = ConvertToRecord(tam, listProfileByCode, listAllCardHistory, dateStart, dateEnd);

                        if (rec != null)
                        {
                            listTAMScanLog.Add(rec);
                        }
                    }
                }

                DeleteTAMLog(unitOfWork, dateFrom, dateTo, listTAMScanLog, userID);
                unitOfWork.AddObject(listTAMScanLog.ToArray());
                unitOfWork.SaveChanges(userID);
                return listTAMScanLog;
            }
        }
Esempio n. 7
0
        private int ComputeAttendance(Guid userID, Guid asynTaskID, DateTime monthYear, Guid cutOffDurationID,
            out DataErrorCode dataErrorCode, int totalProfile, int totalComputed, params Hre_ProfileEntity[] listProfile)
        {
            using (var context = new VnrHrmDataContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                int attendanceTableCount = 0;

                List<Guid> listProfileID = listProfile.Select(s => s.ID).ToList();
                totalProfile = totalProfile <= 0 ? listProfileID.Count() : totalProfile;

                #region Xóa bảng công cũ đã tính trước đó

                dataErrorCode = DeleteAttendance(monthYear,
                    cutOffDurationID, listProfileID);

                if (dataErrorCode == DataErrorCode.Locked
                    || dataErrorCode == DataErrorCode.Error)
                {
                    return attendanceTableCount;
                }

                #endregion

                #region Tính khoảng thời gian theo cutOffDurationID

                DateTime attendanceFrom = monthYear.AddDays(1 - monthYear.Day).Date;
                DateTime attendanceTo = attendanceFrom.AddMonths(1).AddSeconds(-1);

                var cutOffDuration = unitOfWork.CreateQueryable<Att_CutOffDuration>(Guid.Empty,
                    d => d.ID == cutOffDurationID).Select(d => new
                    {
                        d.DateStart,
                        d.DateEnd,
                        d.OvertimeStart,
                        d.OvertimeEnd,
                        d.LeavedayStart,
                        d.LeavedayEnd,
                    }).FirstOrDefault();

                if (cutOffDuration != null)
                {
                    attendanceFrom = cutOffDuration.DateStart;
                    attendanceTo = cutOffDuration.DateEnd;
                }

                #endregion

                #region Tải các thông tin danh mục có liên quan

                string overTimeMethod = MethodOption.E_CASHOUT.ToString();
                string overTimeStatus = OverTimeStatus.E_APPROVED.ToString();
                string pregnancyType = PregnancyType.E_LEAVE_EARLY.ToString();
                string leaveStatus = LeaveDayStatus.E_APPROVED.ToString();
                string hdtJobStatus = HDTJobStatus.E_APPROVE.ToString();
                string leavedayTypeNOPAY = LeavedayTypeCode.NOPAY.ToString();
                string leavedayTypeABS = LeavedayTypeCode.ABS.ToString();
                string leavedayTypeHLD = LeavedayTypeCode.HLD.ToString();
                string rosterStatus = RosterStatus.E_APPROVED.ToString();

                string NOTAUTOREGISTERHOLIDAYLEAVE = AppConfig.HRM_ATT_NOTAUTOREGISTERHOLIDAYLEAVE.ToString();
                string OT_HOLIDAYSCOMPUTE400 = AppConfig.HRM_ATT_OT_HOLIDAYSCOMPUTE400.ToString();
                string MISSTAM_LEAVETYPE = AppConfig.HRM_ATT_MISSTAM_LEAVETYPE.ToString();
                string HRM_ATT_OT_OVERTIMESTATUS = AppConfig.HRM_ATT_OT_OVERTIMESTATUS.ToString();

                var standardWorkdayConfig = unitOfWork.CreateQueryable<Sys_AllSetting>(d => d.Name == NOTAUTOREGISTERHOLIDAYLEAVE
                    || d.Name == OT_HOLIDAYSCOMPUTE400 || d.Name == MISSTAM_LEAVETYPE || d.Name == HRM_ATT_OT_OVERTIMESTATUS).ToList();

                var notAutoRegHolidayLeave = standardWorkdayConfig.Where(s => s.Name == NOTAUTOREGISTERHOLIDAYLEAVE).FirstOrDefault();
                var otholidayscompute400 = standardWorkdayConfig.Where(s => s.Name == OT_HOLIDAYSCOMPUTE400).FirstOrDefault();
                var missTAM_LeaveType = standardWorkdayConfig.Where(s => s.Name == MISSTAM_LEAVETYPE).FirstOrDefault();
                var statusOT = standardWorkdayConfig.Where(s => s.Name == HRM_ATT_OT_OVERTIMESTATUS).FirstOrDefault();

                if (statusOT != null)
                {
                    overTimeStatus = statusOT.Value1;
                }

                DateTime startYear = new DateTime(monthYear.Year, 1, 1).AddMonths(-1);//đầu năm
                DateTime endYear = new DateTime(monthYear.Year, 12, 31).Date.AddDays(1).AddSeconds(-1);
                DateTime preMonthYear = monthYear.AddMonths(-1);

                List<Cat_DayOff> listHoliday = unitOfWork.CreateQueryable<Cat_DayOff>(d =>
                    d.DateOff >= startYear && d.DateOff <= endYear).ToList();

                //Tất cả roster từ đầu năm đến thời điểm tính công - cần cho mục tính tính phép năm còn lại cho từng nhân viên.
                var listRoster = unitOfWork.CreateQueryable<Att_Roster>(d => d.DateStart <= attendanceTo && d.DateEnd >= startYear
                    && d.Status == rosterStatus && listProfileID.Contains(d.ProfileID)).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 listRosterGroup = unitOfWork.CreateQueryable<Att_RosterGroup>(m => m.DateStart != null && m.DateEnd != null
                    && m.DateStart <= attendanceTo && m.DateEnd >= attendanceFrom).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();

                var listWorkHistory = unitOfWork.CreateQueryable<Hre_WorkHistory>(d => d.DateEffective <= attendanceTo
                    && listProfileID.Contains(d.ProfileID)).ToList();

                List<Att_Roster> lstRosterTypeGroup = new List<Att_Roster>();
                List<Att_RosterGroup> lstRosterGroup = new List<Att_RosterGroup>();

                GetRosterGroup(listProfileID, startYear, attendanceTo, out lstRosterTypeGroup, out lstRosterGroup);
                var listLateEarlyRule = unitOfWork.CreateQueryable<Cat_LateEarlyRule>().ToList<Cat_LateEarlyRule>();
                var listShift = unitOfWork.CreateQueryable<Cat_Shift>().ToList<Cat_Shift>();

                var currentYear = unitOfWork.CreateQueryable<Att_AnnualDetail>(d => monthYear == d.MonthYear).Select(d => d.Year).FirstOrDefault();
                var monthStart = unitOfWork.CreateQueryable<Att_AnnualDetail>(d => d.Year == currentYear).OrderBy(d => d.MonthYear).Select(d => d.MonthYear).FirstOrDefault();

                var listAnnualDetail = unitOfWork.CreateQueryable<Att_AnnualDetail>(d => d.MonthYear == monthStart
                    && d.ProfileID.HasValue && listProfileID.Contains(d.ProfileID.Value));

                var listPreAttendanceTable = unitOfWork.CreateQueryable<Att_AttendanceTable>(d => d.MonthYear.HasValue
                    && d.MonthYear == preMonthYear && listProfileID.Contains(d.ProfileID)).Select(d =>
                        new Att_AttendanceTableEntity
                        {
                            ID = d.ID,
                            AnlDayTaken = d.AnlDayTaken,
                            AnlDayAdjacent = d.AnlDayAdjacent,
                            SickDayTaken = d.SickDayTaken,
                            SickDayAdjacent = d.SickDayAdjacent,
                            AnlDayAvailable = d.AnlDayAvailable,
                            SickDayAvailable = d.SickDayAvailable,
                            ProfileID = d.ProfileID
                        }).ToList<Att_AttendanceTableEntity>();

                var listPregnancy = unitOfWork.CreateQueryable<Att_Pregnancy>(d => d.DateEnd >= attendanceFrom && d.DateStart <= attendanceTo
                    && d.Type == pregnancyType && listProfileID.Contains(d.ProfileID)).Select(d => new Att_PregnancyEntity
                    {
                        ID = d.ID,
                        ProfileID = d.ProfileID,
                        DateStart = d.DateStart,
                        DateEnd = d.DateEnd,
                        TypePregnancyEarly = d.TypePregnancyEarly
                    }).ToList<Att_PregnancyEntity>();

                var listOvertime = unitOfWork.CreateQueryable<Att_Overtime>(d => d.Status == overTimeStatus
                    && (d.MethodPayment == null || d.MethodPayment == overTimeMethod) && d.WorkDateRoot >= attendanceFrom
                    && d.WorkDateRoot <= attendanceTo && listProfileID.Contains(d.ProfileID)).Select(d => new Att_OvertimeEntity
                    {
                        ID = d.ID,
                        ProfileID = d.ProfileID,
                        OvertimeTypeID = d.OvertimeTypeID,
                        DurationType = d.DurationType,
                        WorkDateRoot = d.WorkDateRoot,
                        WorkDate = d.WorkDate,
                        ApproveHours = d.ApproveHours,
                        ConfirmHours = d.ConfirmHours,
                        RegisterHours = d.RegisterHours
                    }).ToList<Att_OvertimeEntity>();

                List<Guid> listOvertimeTypeID = listOvertime.Select(o => o.OvertimeTypeID).Distinct().ToList();
                var listOvertimeType = unitOfWork.CreateQueryable<Cat_OvertimeType>(d => listOvertimeTypeID.Contains(d.ID)).ToList();

                var listLeaveDay = unitOfWork.CreateQueryable<Att_LeaveDay>(d => d.Status == leaveStatus && ((d.DateStart <= attendanceTo
                    && d.DateEnd >= attendanceFrom) || d.DateOvertimeOff >= attendanceFrom && d.DateOvertimeOff <= attendanceTo)
                    && listProfileID.Contains(d.ProfileID)).Select(d => new Att_LeaveDayEntity
                    {
                        ID = d.ID,
                        ProfileID = d.ProfileID,
                        DateStart = d.DateStart,
                        DateEnd = d.DateEnd,
                        DateOvertimeOff = d.DateOvertimeOff,
                        LeaveDayTypeID = d.LeaveDayTypeID,
                        DurationType = d.DurationType,
                        LeaveHours = d.LeaveHours,
                        LeaveDays = d.LeaveDays
                    }).ToList<Att_LeaveDayEntity>();

                List<Guid> listLeaveDayTypeID = listLeaveDay.Select(o => o.LeaveDayTypeID).Distinct().ToList();
                var listLeaveDayType = unitOfWork.CreateQueryable<Cat_LeaveDayType>(d => listLeaveDayTypeID.Contains(d.ID)
                    || d.Code == leavedayTypeABS || d.Code == leavedayTypeNOPAY || d.Code == leavedayTypeHLD).ToList();

                List<Hre_HDTJob> listHDTJob = new List<Hre_HDTJob>();
                foreach (var templstProfileIds in listProfileID.Chunk(1000))
                {
                    listHDTJob.AddRange(unitOfWork.CreateQueryable<Hre_HDTJob>(d => d.Status == hdtJobStatus && ((d.DateFrom <= attendanceTo && d.DateTo == null)
                    || (d.DateTo != null && d.DateFrom <= attendanceTo && d.DateTo >= attendanceFrom)) && templstProfileIds.Contains(d.ProfileID.Value)).ToList());
                }
                //var listHDTJob = unitOfWork.CreateQueryable<Hre_HDTJob>(d => d.Status == hdtJobStatus && ((d.DateFrom <= attendanceTo && d.DateTo == null)
                //    || (d.DateTo != null && d.DateFrom <= attendanceTo && d.DateTo >= attendanceFrom)) && listProfileID.Contains(d.ProfileID.Value)).ToList();

                //Danh sách cấu hình grade theo danh sách nhân viên có trong danh sách mã thẻ
                var listGrade = unitOfWork.CreateQueryable<Att_Grade>(d => d.MonthStart.HasValue && d.MonthStart <= attendanceTo
                    && listProfileID.Contains(d.ProfileID.Value)).Select(d => new
                    {
                        d.ProfileID,
                        d.MonthStart,
                        d.GradeAttendanceID
                    }).ToList();

                listGrade = listGrade.GroupBy(d => d.ProfileID).Select(d =>
                    d.OrderByDescending(p => p.MonthStart).FirstOrDefault()).ToList();

                //Danh sách cấu hình gradeCfg theo danh sách grade tìm được ở trên
                var listGradeCfgID = listGrade.Select(d => d.GradeAttendanceID).Distinct().ToList();
                var listGradeCfg = unitOfWork.CreateQueryable<Cat_GradeAttendance>(d => listGradeCfgID.Contains(d.ID)).ToList();

                var lstTimeOffInLieu = unitOfWork.CreateQueryable<Att_TimeOffInLieu>(d => d.Date != null
                    && d.Date >= attendanceFrom && d.Date <= attendanceTo).Select(m => new
                    {
                        m.ProfileID,
                        m.UnusualLeaves,
                        m.TakenLeaves
                    }).ToList();

                DateTime month3Ago = new DateTime(attendanceTo.Year, attendanceTo.Month, 1);
                month3Ago = month3Ago.AddMonths(-3);

                List<Att_TimeOffInLieuMonth> lstTimeOffInLieu_ByMonth_3Month = unitOfWork.CreateQueryable<Att_TimeOffInLieuMonth>(d =>
                    d.Month != null && d.Month >= month3Ago && d.Month <= attendanceTo && listProfileID.Contains(d.ProfileID)).ToList();

                List<Att_TimeOffInLieuMonth> lstTimeOffInLieuMonth = lstTimeOffInLieu_ByMonth_3Month.Where(d =>
                    d.Month != null && d.Month >= attendanceFrom && d.Month <= attendanceTo).ToList<Att_TimeOffInLieuMonth>();

                List<Att_TimeOffInLieuMonth> lstTimeOffInLieuMonthInsert = new List<Att_TimeOffInLieuMonth>();
                DateTime beginYear = new DateTime(attendanceTo.Year, 1, 1);

                #endregion

                #region Bắt đầu tính công cho từng người

                var listWorkDay = unitOfWork.CreateQueryable<Att_Workday>(d => d.WorkDate >= attendanceFrom
                    && d.WorkDate <= attendanceTo && listProfileID.Contains(d.ProfileID)).ToList();

                List<Att_TimeOffInLieu> lstTimeOffInLieu_3Month = unitOfWork.CreateQueryable<Att_TimeOffInLieu>(d =>
                    d.Date >= month3Ago && d.Date <= attendanceTo && listProfileID.Contains(d.ProfileID)).ToList();

                var listTimeOffInLieu = lstTimeOffInLieu_3Month.Where(s => listProfileID.Contains(s.ProfileID)
                    && attendanceFrom <= s.Date && s.Date <= attendanceTo).Select(s => new Att_TimeOffInLieuEntity
                    {
                        ID = s.ID,
                        Date = s.Date,
                        ProfileID = s.ProfileID,
                        OvertimeID = s.OvertimeID,
                        LeaveDayID = s.LeaveDayID,
                        UnusualLeaves = s.UnusualLeaves,
                        LeaveHours = s.LeaveHours,
                        TakenLeaves = s.TakenLeaves
                    }).ToList<Att_TimeOffInLieuEntity>();

                using (var taskContext = new VnrHrmDataContext())
                {
                    IUnitOfWork taskUnitOfWork = new UnitOfWork(taskContext);
                    Sys_AsynTask asynTask = null;

                    if (asynTaskID != Guid.Empty)
                    {
                        asynTask = taskUnitOfWork.CreateQueryable<Sys_AsynTask>(s =>
                           s.ID == asynTaskID).FirstOrDefault();
                    }

                    int totalComputedProfileMustSubmitTask = 50;
                    int totalComputedProfileMustSubmit = 50;
                    int totalComputedProfileForSubmit = 0;
                    int totalComputedProfileForTask = 0;

                    totalComputedProfileMustSubmitTask = totalProfile * 5 / 100;

                    if (totalComputedProfileMustSubmitTask > listProfile.Count())
                    {
                        totalComputedProfileMustSubmitTask = listProfile.Count();
                    }

                    foreach (var profileId in listProfileID)
                    {
                        try
                        {
                            #region Những dữ liệu liên quan tính công theo từng nhân viên

                            var profile = listProfile.Where(d => d.ID == profileId).FirstOrDefault();
                            var listAnnualDetailByProfile = listAnnualDetail.Where(d => d.ProfileID == profileId).ToList();
                            var listPreAttendanceTableByProfile = listPreAttendanceTable.Where(d => d.ProfileID == profileId).ToList();
                            var listWorkDayByProfile = listWorkDay.Where(d => d.ProfileID == profileId).OrderBy(d => d.WorkDate).ToList();
                            var listTimeOffInLieuByProfile = listTimeOffInLieu.Where(d => d.ProfileID == profileId).ToList();

                            var listHDTJobByProfile = listHDTJob.Where(d => d.ProfileID == profileId).ToList();
                            var listPregnancyByProfile = listPregnancy.Where(prg => prg.ProfileID == profileId && prg.DateEnd >= attendanceFrom && prg.DateStart <= attendanceTo).ToList();
                            var listLeaveDayByProfile = listLeaveDay.Where(d => d.ProfileID == profileId && ((d.DateStart <= attendanceTo && d.DateEnd >= attendanceFrom)
                                || (d.DateOvertimeOff.HasValue && d.DateOvertimeOff >= attendanceFrom && d.DateOvertimeOff <= attendanceTo))).ToList();

                            var listOvertimeByProfile = listOvertime.Where(d => d.ProfileID == profileId && d.WorkDate >= attendanceFrom && d.WorkDate <= attendanceTo).ToList();
                            var gradeCfgIDByProfile = listGrade.Where(d => d.ProfileID == profileId && d.MonthStart <= attendanceTo).Select(d => d.GradeAttendanceID).FirstOrDefault();
                            var gradeCfgByProfile = listGradeCfg.Where(d => d.ID == gradeCfgIDByProfile).FirstOrDefault();

                            List<Hre_WorkHistory> listWorkHistoryByProfile = listWorkHistory.Where(d => d.ProfileID == profileId).ToList();
                            var listRosterByProfile = listRoster.Where(d => d.ProfileID == profileId && d.DateStart <= attendanceTo && d.DateEnd >= attendanceFrom).ToList();
                            var listMonthShifts = Att_AttendanceLib.GetDailyShifts(attendanceFrom, attendanceTo, profile.ID, listRosterByProfile, listRosterGroup);

                            #endregion

                            attendanceTableCount += ComputeAttendance(unitOfWork, userID, monthYear, cutOffDurationID, attendanceFrom, attendanceTo, profile,
                                gradeCfgByProfile, listShift, listAnnualDetailByProfile, listHDTJobByProfile, listLeaveDayByProfile, listLeaveDayType,
                                listOvertimeByProfile, listOvertimeType, listPregnancyByProfile, listLateEarlyRule, listWorkDayByProfile,
                                listPreAttendanceTableByProfile, listTimeOffInLieuByProfile, notAutoRegHolidayLeave, otholidayscompute400,
                                missTAM_LeaveType, listMonthShifts, listHoliday);

                            #region Cập Nhật Dữ Liệu Cho Việc Tính TimeOffInLieuMonth and Year

                            double? balanceLeaves = (new Att_LeavedayServices()).CalculateTotalHourTimeOff(profileId, lstTimeOffInLieu_3Month.Where(m =>
                                m.ProfileID == profileId).ToList(), lstTimeOffInLieu_ByMonth_3Month.Where(m => m.ProfileID == profileId).ToList(), monthYear, 1);

                            double UnusualLeaves = lstTimeOffInLieu.Where(m => m.ProfileID == profileId).Sum(m => m.UnusualLeaves ?? 0);
                            double TakenLeaves = lstTimeOffInLieu.Where(m => m.ProfileID == profileId).Sum(m => m.TakenLeaves ?? 0);
                            double RemainLeaves = UnusualLeaves - TakenLeaves + (balanceLeaves ?? 0);

                            var timeOffInLieuMonth_ByProfile = lstTimeOffInLieuMonth.Where(m =>
                                m.ProfileID == profileId).FirstOrDefault();

                            if (timeOffInLieuMonth_ByProfile == null)
                            {
                                //tạo mới
                                timeOffInLieuMonth_ByProfile = new Att_TimeOffInLieuMonth();
                                timeOffInLieuMonth_ByProfile.ID = Guid.NewGuid();
                                timeOffInLieuMonth_ByProfile.ProfileID = profileId;
                                timeOffInLieuMonth_ByProfile.BalanceLeaves = balanceLeaves ?? 0;
                                timeOffInLieuMonth_ByProfile.UnusualLeaves = UnusualLeaves;
                                timeOffInLieuMonth_ByProfile.TakenLeaves = TakenLeaves;
                                timeOffInLieuMonth_ByProfile.RemainLeaves = RemainLeaves;
                                timeOffInLieuMonth_ByProfile.Month = new DateTime(attendanceTo.Year, attendanceTo.Month, 1);
                                unitOfWork.AddObject(typeof(Att_TimeOffInLieuMonth), timeOffInLieuMonth_ByProfile);
                            }
                            else
                            {
                                //chinh sua
                                timeOffInLieuMonth_ByProfile.BalanceLeaves = balanceLeaves ?? 0;
                                timeOffInLieuMonth_ByProfile.UnusualLeaves = UnusualLeaves;
                                timeOffInLieuMonth_ByProfile.TakenLeaves = TakenLeaves;
                                timeOffInLieuMonth_ByProfile.RemainLeaves = RemainLeaves;
                            }

                            #endregion

                            #region Chia thành nhiều giai đoạn lưu bảng công

                            if (asynTask != null)
                            {
                                totalComputedProfileForTask++;
                                double percent = (double)totalComputedProfileForTask / (double)totalProfile;

                                if (totalComputedProfileForTask >= totalComputedProfileMustSubmitTask)
                                {
                                    asynTask.PercentComplete = asynTask.PercentComplete + percent;
                                    dataErrorCode = taskUnitOfWork.SaveChanges(userID);
                                    totalComputedProfileForTask = 0;

                                    if (dataErrorCode == DataErrorCode.Locked)
                                    {
                                        break;
                                    }
                                }
                            }

                            //Nên submit lần đầu tiên khi đã tính được 5 profile
                            bool firstSubmit = listProfileID.ToList().IndexOf(profileId) == 5;
                            firstSubmit = totalComputed >= 5 ? false : firstSubmit;//đã chạy 1 lần
                            totalComputedProfileForSubmit++;

                            if (firstSubmit || totalComputedProfileForSubmit >= totalComputedProfileMustSubmit)
                            {
                                totalComputedProfileForSubmit = firstSubmit ? totalComputedProfileForSubmit : 0;
                                dataErrorCode = unitOfWork.SaveChanges(userID);

                                if (dataErrorCode == DataErrorCode.Locked)
                                {
                                    break;
                                }
                            }

                            #endregion
                        }
                        catch
                        {
                            throw new Exception(profileId.ToString());
                        }
                    }
                }

                #endregion

                //Lưu tất cả kết quả tính công
                dataErrorCode = unitOfWork.SaveChanges(userID);
                return attendanceTableCount;
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Convert và lưu dữ liệu import vào database.
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="listImportData"></param>
        /// <returns></returns>
        public bool Save(Guid userID, IList listImportData, string userLogin)
        {
            bool result = false;

            if (!isDataSaved)
            {
                if (listImportData != null && listImportData.Count > 0)
                {
                    using (var context = new VnrHrmDataContext())
                    {
                        IUnitOfWork unitOfWork = new UnitOfWork(context);
                        var objectType = listImportData[0].GetPropertyValue(Constant.ObjectType);

                        IList listObj = new ArrayList();
                        var status = DataErrorCode.Success;

                        if (objectType != null)
                        {
                            if (this.ImportMode == ImportDataMode.Update || this.ImportMode == ImportDataMode.Skip)
                            {
                                var listObjectID = listImportData.AsQueryable().Select(Constant.ID).GetList().OfType<Guid>().Distinct().ToArray();
                                listEntityResult = unitOfWork.CreateQueryable(Guid.Empty, (Type)objectType, "@0.Contains(outerIt." + Constant.ID + ")", listObjectID).GetList();

                                foreach (var importData in listImportData)
                                {
                                    var objectID = importData.GetPropertyValue(Constant.ID).TryGetValue<Guid>();
                                    var objectDuplicate = listEntityResult.AsQueryable().Where(Constant.ID + " = @0", objectID).FirstOrDefault();

                                    if (objectDuplicate != null)
                                    {
                                        //Copy dữ liệu import vào dữ liệu trong database
                                        importData.CopyData(objectDuplicate, Constant.ID);
                                    }
                                    else if (this.ImportMode == ImportDataMode.Skip)
                                    {
                                        objectDuplicate = ((Type)objectType).CreateInstance();
                                        importData.CopyData(objectDuplicate, Constant.ID);
                                        unitOfWork.AddObject((Type)objectType, objectDuplicate);
                                    }

                                    if (objectDuplicate != null)
                                    {
                                        listObj.Add(objectDuplicate);
                                    }

                                    var currentIndex = listImportData.IndexOf(importData);

                                    if ((currentIndex + 1) == listImportData.Count || (currentIndex > 0
                                        && listImportData.IndexOf(importData) % 9999 == 0))
                                    {
                                        status = unitOfWork.SaveChanges(userID, true);
                                    }

                                    if (ProgressChanged != null)
                                    {
                                        double percent = ((double)listImportData.IndexOf(importData) / (double)listImportData.Count) * 100;
                                        ProgressChanged(new ProgressEventArgs
                                        {
                                            ID = UserID,
                                            Name = "SaveData",
                                            Value = "SaveData".TranslateString(),
                                            Percent = (int)percent
                                        });
                                    }
                                }

                                result = true;
                            }
                            else
                            {
                                listEntityResult = ((Type)objectType).CreateList();

                                foreach (var importData in listImportData)
                                {
                                    var objectDuplicate = ((Type)objectType).CreateInstance();
                                    importData.CopyData(objectDuplicate, Constant.ID);
                                    unitOfWork.AddObject((Type)objectType, objectDuplicate);

                                    var currentIndex = listImportData.IndexOf(importData);

                                    if ((currentIndex + 1) == listImportData.Count || (currentIndex > 0
                                        && listImportData.IndexOf(importData) % 9999 == 0))
                                    {
                                        status = unitOfWork.SaveChanges(userID, true);
                                    }

                                    if (ProgressChanged != null)
                                    {
                                        double percent = ((double)listImportData.IndexOf(importData) / (double)listImportData.Count) * 100;
                                        ProgressChanged(new ProgressEventArgs
                                        {
                                            ID = UserID,
                                            Name = "SaveData",
                                            Value = "SaveData".TranslateString(),
                                            Percent = (int)percent
                                        });
                                    }
                                }

                                listObj = listEntityResult;
                                result = true;
                            }

                            isDataSaved = true;

                            //Xử lý task vụ sau khi import
                            if (status == DataErrorCode.Success)
                            {
                                if ((Type)objectType == typeof(Rec_Candidate))
                                {
                                    if (listObj.Count > 0)
                                    {
                                        var listGuidID = new List<Guid>();

                                        foreach (var item in listObj)
                                        {
                                            if (item != null)
                                            {
                                                listGuidID.Add((Guid)item.GetPropertyValue(Constant.ID));
                                            }
                                        }

                                        DoActionAfterImport afterImport = new DoActionAfterImport();
                                        afterImport.ImportRecruitmentHistory(listGuidID, userLogin);
                                    }
                                }
                            }
                            else
                            {
                                result = false;
                            }

                            #region ImportCompleted

                            if (ProgressChanged != null)
                            {
                                ProgressChanged(new ProgressEventArgs
                                {
                                    ID = UserID,
                                    Name = "SaveData",
                                    Value = "SaveData".TranslateString(),
                                    Percent = 100
                                });
                            }

                            #endregion
                        }
                    }
                }
            }

            return result;
        }
Esempio n. 9
0
        private int ComputeWorkday(Guid asynTaskID, Guid userID, int totalProfile, int totalComputed, double timeoutMinutes,
            DateTime dateFrom, DateTime dateTo, out DataErrorCode dataErrorCode, params Hre_ProfileEntity[] listProfile)
        {
            List<Att_Workday> listWorkday = new List<Att_Workday>();
            List<Att_Workday> listWorkdayChecked = new List<Att_Workday>();
            List<Att_Workday> listWorkdayAnalyze = new List<Att_Workday>();

            dateFrom = dateFrom <= DateTime.MinValue ? DateTime.Now : dateFrom;
            dateTo = dateTo <= DateTime.MinValue ? DateTime.Now : dateTo;
            DateTime dateStart = dateFrom.AddDays(-1);
            DateTime dateEnd = dateTo.AddDays(1);

            using (var context = new VnrHrmDataContext())
            {
                var workHistoryServices = new Hre_WorkHistoryServices();
                var leavedayServices = new Att_LeavedayServices();
                var unitOfWork = new UnitOfWork(context);

                string waitStatus = ProfileStatusSyn.E_WAITING.ToString();
                string rosterStatus = RosterStatus.E_APPROVED.ToString();
                string leaveDayStatus = LeaveDayStatus.E_APPROVED.ToString();
                String appConfigInfo = AppConfig.E_SERVER_TAM.ToString();
                string E_LEAVE_EARLY = PregnancyType.E_LEAVE_EARLY.ToString();

                string workdaySrcType = WorkdaySrcType.E_MANUAL.ToString();
                string workdayStatus1 = WorkdayStatus.E_APPROVED.ToString();
                string workdayStatus2 = WorkdayStatus.E_WAIT_APPROVED.ToString();
                var listProfileID = listProfile.Select(d => d.ID).ToArray();

                #region Delete Workday đã tổng hợp trước đó

                if (unitOfWork.CheckLock(typeof(Att_Workday), dateFrom, dateTo))
                {
                    dataErrorCode = DataErrorCode.Locked;

                    CompleteComputingTask(asynTaskID, userID,
                        totalComputed, totalProfile, dataErrorCode);

                    return listWorkday.Count();
                }
                else
                {
                    Task task = Task.Run(() => DeleteWorkday(userID, dateFrom, dateTo,
                        workdaySrcType, workdayStatus1, workdayStatus2, listProfileID));
                }

                #endregion

                #region Khởi tạo dữ liệu cho lần tổng hợp

                var tamScanLogQueryable = unitOfWork.CreateQueryable<Att_TAMScanLog>(Guid.Empty, d => d.ProfileID.HasValue
                    && listProfileID.Contains(d.ProfileID.Value) && d.TimeLog.HasValue && d.TimeLog >= dateStart && d.TimeLog <= dateEnd);

                //Danh sách quẹt thẻ theo điều kiện được chọn
                var listAllTamScanLog = tamScanLogQueryable.Select(d => new Att_TAMScanLogEntity
                {
                    ID = d.ID,
                    ProfileID = d.ProfileID,
                    CardCode = d.CardCode,
                    CodeEmp = d.CodeEmp,
                    TimeLog = d.TimeLog,
                    SrcType = d.SrcType,
                    Type = d.Type
                }).ToList();

                var listRoster = unitOfWork.CreateQueryable<Att_Roster>(Guid.Empty, d => d.Status == rosterStatus && d.DateStart <= dateEnd
                    && d.DateEnd >= dateStart.Date && listProfileID.Contains(d.ProfileID)).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 listRosterGroup = unitOfWork.CreateQueryable<Att_RosterGroup>(Guid.Empty, m => m.DateStart != null && m.DateEnd != null
                    && m.DateStart <= dateEnd && m.DateEnd >= dateStart.Date).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();

                var listShift = unitOfWork.CreateQueryable<Cat_Shift>(Guid.Empty).ToList();

                var listHoliday = unitOfWork.CreateQueryable<Cat_DayOff>(Guid.Empty,
                    d => d.DateOff >= dateFrom.Date && d.DateOff <= dateTo).Select(d =>
                        new Cat_DayOffEntity
                        {
                            ID = d.ID,
                            DateOff = d.DateOff,
                            Type = d.Type
                        }).ToList();

                var listWorkdayExisting = unitOfWork.CreateQueryable<Att_Workday>(Guid.Empty, d => d.WorkDate >= dateStart
                    && d.WorkDate <= dateEnd && (d.SrcType == workdaySrcType || d.Status == workdayStatus1
                    || d.Status == workdayStatus2) && listProfileID.Contains(d.ProfileID)).ToList();

                var listLeaveDay = unitOfWork.CreateQueryable<Att_LeaveDay>(Guid.Empty, d => d.Status == leaveDayStatus
                    && d.DateStart <= dateTo && d.DateEnd >= dateFrom.Date && listProfileID.Contains(d.ID)).Select(d =>
                        new
                        {
                            d.ID,
                            d.ProfileID,
                            d.LeaveDayTypeID,
                            d.DateOvertimeOff,
                            d.DateStart,
                            d.DateEnd
                        }).ToList();

                var listLeaveDayType = unitOfWork.CreateQueryable<Cat_LeaveDayType>(Guid.Empty).Select(d =>
                    new
                    {
                        d.ID,
                        d.Code,
                        d.MissInOutReasonID
                    }).ToList();

                var listAllSetting = unitOfWork.CreateQueryable<Sys_AllSetting>(Guid.Empty, i => i.Name == AppConfig.HRM_ATT_WORKDAY_SUMMARY_MAXHOURSONESHIFT.ToString()
                    || i.Name == AppConfig.HRM_ATT_WORKDAY_SUMMARY_MAXHOURSNEXTINOUT.ToString() || i.Name == AppConfig.HRM_ATT_WORKDAY_SUMMARY_MINMINUTESSAMEATT.ToString()
                    || i.Name == AppConfig.HRM_ATT_WORKDAY_SUMMARY_SYMBOL.ToString() || i.Name == AppConfig.HRM_ATT_WORKDAY_SUMMARY_DETECTSHIFT.ToString()
                    || i.Name == AppConfig.HRM_ATT_WORKDAY_SUMMARY_TYPELOADDATA.ToString()).Select(d => new { d.Name, d.Value1, d.Value2 }).ToList();

                var inOutConfigMaxHoursOneShift = listAllSetting.Where(i => i.Name == AppConfig.HRM_ATT_WORKDAY_SUMMARY_MAXHOURSONESHIFT.ToString()).FirstOrDefault();
                var inOutConfigMaxHoursNextInOut = listAllSetting.Where(i => i.Name == AppConfig.HRM_ATT_WORKDAY_SUMMARY_MAXHOURSNEXTINOUT.ToString()).FirstOrDefault();
                var inOutConfigMinMinutesSameAtt = listAllSetting.Where(i => i.Name == AppConfig.HRM_ATT_WORKDAY_SUMMARY_MINMINUTESSAMEATT.ToString()).FirstOrDefault();
                var inOutConfigSymbol = listAllSetting.Where(i => i.Name == AppConfig.HRM_ATT_WORKDAY_SUMMARY_SYMBOL.ToString()).FirstOrDefault();
                var inOutConfigDetectShift = listAllSetting.Where(i => i.Name == AppConfig.HRM_ATT_WORKDAY_SUMMARY_DETECTSHIFT.ToString()).FirstOrDefault();
                var inOutConfigTypeLoadData = listAllSetting.Where(i => i.Name == AppConfig.HRM_ATT_WORKDAY_SUMMARY_TYPELOADDATA.ToString()).FirstOrDefault();

                var inOutConfig = new string[] { string.Empty }.Select(d => new WorkdayConfig
                {
                    MaxHoursOneShift = inOutConfigMaxHoursOneShift != null ? inOutConfigMaxHoursOneShift.Value1 : string.Empty,
                    MaxHoursNextInOut = inOutConfigMaxHoursNextInOut != null ? inOutConfigMaxHoursNextInOut.Value1 : string.Empty,
                    MinMinutesSameAtt = inOutConfigMinMinutesSameAtt != null ? inOutConfigMinMinutesSameAtt.Value1 : string.Empty,
                    TypeLoadData = inOutConfigTypeLoadData != null ? inOutConfigTypeLoadData.Value1 : string.Empty,
                    SymbolIn = inOutConfigSymbol != null ? inOutConfigTypeLoadData.Value1 : string.Empty,
                    SymbolOut = inOutConfigSymbol != null ? inOutConfigTypeLoadData.Value2 : string.Empty,
                    DetectShift = inOutConfigDetectShift != null ? inOutConfigTypeLoadData.Value1 : string.Empty,
                    DetectWrongShift = inOutConfigDetectShift != null ? inOutConfigTypeLoadData.Value2 : string.Empty,
                }).FirstOrDefault();

                List<Att_Pregnancy> lstPrenancy = unitOfWork.CreateQueryable<Att_Pregnancy>(Guid.Empty, m => m.Type == E_LEAVE_EARLY
                    && m.DateEnd >= dateFrom && m.DateStart < dateTo && listProfileID.Contains(m.ProfileID)).ToList();

                List<Cat_LateEarlyRule> lstLateEarlyRule = unitOfWork.CreateQueryable<Cat_LateEarlyRule>(Guid.Empty).ToList();
                List<Cat_GradeAttendance> lstGradeConfig = unitOfWork.CreateQueryable<Cat_GradeAttendance>(Guid.Empty).ToList();

                List<Att_Grade> lstGrade = unitOfWork.CreateQueryable<Att_Grade>(Guid.Empty, m => m.ProfileID.HasValue && listProfileID.Contains(m.ProfileID.Value)).ToList();
                List<DateTime> lstDayOff = unitOfWork.CreateQueryable<Cat_DayOff>(Guid.Empty, m => m.DateOff >= dateStart && m.DateOff <= dateEnd).Select(m => m.DateOff).ToList<DateTime>();

                #endregion

                #region Bắt đầu xử lý tổng hợp in out

                string sameAttConfig = inOutConfig != null ? inOutConfig.MinMinutesSameAtt : string.Empty;
                var minMinutesSameAtt = VnResource.Helper.Data.DataHelper.TryGetValue<double>(sameAttConfig);

                //Loại bỏ những dòng quẹt thẻ liền kề nhau trong khoảng thời gian như cấu hình
                var listTamScanLog = RemoveSameTimeLog(listAllTamScanLog, minMinutesSameAtt);

                //Giữ danh sách những quẹt thẻ tạm thời được cho là trùng với những quẹt thẻ khác để xử lý sau
                var listTamScanLogRemove = listAllTamScanLog.Where(d => !listTamScanLog.Contains(d)).ToList();

                using (var taskContext = new VnrHrmDataContext())
                {
                    var taskUnitOfWork = new UnitOfWork(taskContext);
                    Sys_AsynTask asynTask = null;

                    if (asynTaskID != Guid.Empty)
                    {
                        asynTask = taskUnitOfWork.CreateQueryable<Sys_AsynTask>(s =>
                           s.ID == asynTaskID).FirstOrDefault();
                    }

                    int totalComputedProfileMustSubmitTask = 50;
                    int totalComputedProfileMustSubmit = 200;
                    int totalComputedProfileForSubmit = 0;
                    int totalComputedProfileForTask = 0;

                    totalComputedProfileMustSubmitTask = totalProfile * 5 / 100;

                    if (totalComputedProfileMustSubmitTask > listProfile.Count())
                    {
                        totalComputedProfileMustSubmitTask = listProfile.Count();
                    }

                    foreach (var profile in listProfile)
                    {
                        #region Cập nhật thời gian tính timeout cho task

                        if (asynTask != null)
                        {
                            bool mustSaveTask = false;

                            if (asynTask.TimeEnd.HasValue)
                            {
                                var timeoutDate = DateTime.Now.AddMinutes(-timeoutMinutes);

                                if (timeoutDate.AddMinutes(1) >= asynTask.TimeEnd.Value)
                                {
                                    asynTask.TimeEnd = DateTime.Now;
                                    mustSaveTask = true;
                                }
                            }

                            totalComputedProfileForTask++;
                            double percent = totalComputedProfileForTask / (double)totalProfile;

                            if (totalComputedProfileForTask >= totalComputedProfileMustSubmitTask)
                            {
                                var totalPercent = asynTask.PercentComplete + percent;
                                asynTask.PercentComplete = totalPercent;
                                asynTask.TimeEnd = DateTime.Now;
                                mustSaveTask = true;
                            }

                            if (mustSaveTask)
                            {
                                taskUnitOfWork.SaveChanges(userID);
                                totalComputedProfileForTask = 0;
                            }
                        }

                        #endregion

                        #region Những dữ liệu liên quan theo từng nhân viên

                        var listRosterByProfile = listRoster.Where(d => d.ProfileID == profile.ID).ToList();
                        var listMonthShifts = Att_AttendanceLib.GetDailyShifts(dateStart, dateEnd, profile.ID, listRosterByProfile, listRosterGroup);

                        #endregion

                        #region Xử lý tổng hợp in out theo từng ngày

                        for (DateTime date = dateFrom.Date; date <= dateTo; date = date.AddDays(1))
                        {
                            Att_Workday workday = listWorkdayExisting.Where(d => d.ProfileID == profile.ID && d.WorkDate == date).FirstOrDefault();
                            var shiftByDate = listMonthShifts.Where(d => d.Key == date).Select(d => d.Value).FirstOrDefault();

                            Guid? shiftID1 = Guid.Empty;
                            Guid? shiftID2 = Guid.Empty;

                            if (shiftByDate != null)
                            {
                                if (shiftByDate.Count() > 0)
                                {
                                    shiftID1 = shiftByDate[0];
                                }

                                if (shiftByDate.Count() > 1)
                                {
                                    shiftID2 = shiftByDate[1];
                                }
                            }

                            var shiftInfo1 = listShift.Where(d => d.ID == shiftID1).FirstOrDefault();
                            var shiftInfo2 = listShift.Where(d => d.ID == shiftID2).FirstOrDefault();

                            if ((profile.DateQuit.HasValue && profile.DateQuit.Value.Date <= date)
                                || (profile.DateHire.HasValue && profile.DateHire.Value.Date > date))
                            {
                                if (workday != null)
                                {
                                    workday.IsDelete = true;
                                }

                                continue;
                            }

                            if (workday != null)
                            {
                                if (shiftByDate == null)
                                {
                                    shiftByDate = new List<Guid?>();
                                }

                                if ((!shiftID1.HasValue || shiftID1 == Guid.Empty))
                                {
                                    if (workday.ShiftActual.HasValue)
                                    {
                                        shiftByDate.Insert(0, workday.ShiftActual);
                                    }
                                }

                                if ((!shiftID2.HasValue || shiftID2 == Guid.Empty))
                                {
                                    if (workday.ShiftActual2.HasValue)
                                    {
                                        if (shiftByDate.Count() == 0)
                                        {
                                            shiftByDate.Add(Guid.Empty);
                                        }

                                        shiftByDate.Add(workday.ShiftActual2);
                                    }
                                }

                                if (listMonthShifts.ContainsKey(date))
                                {
                                    listMonthShifts[date] = shiftByDate;
                                }
                                else
                                {
                                    listMonthShifts.Add(date, shiftByDate);
                                }

                                if ((workday.SrcType == workdaySrcType || workday.Status == workdayStatus1 || workday.Status == workdayStatus2))
                                {
                                    listWorkday.Add(workday);
                                    continue;
                                }
                            }

                            Guid? actualShiftID1 = null;
                            Guid? actualShiftID2 = null;
                            bool isPreWorkday = false;

                            var listTamScanLogByCardCode = listTamScanLog.Where(d => d.ProfileID == profile.ID).OrderBy(d => d.TimeLog).ToList();
                            var listTamScanLogByCardCodeUnchecked = listTamScanLogByCardCode.Where(d => !d.Checked && d.TimeLog.HasValue).ToList();

                            if (shiftID1.HasValue && shiftID1 != Guid.Empty)
                            {
                                #region Trường hợp có lịch làm việc - ca thứ nhất

                                workday = CreateWorkday(unitOfWork, date, profile, inOutConfig, listHoliday, listWorkday, workday,
                                    false, shiftID1, shiftInfo1, listShift, listMonthShifts, out actualShiftID1,
                                    listTamScanLogByCardCode, listTamScanLogByCardCodeUnchecked);

                                #endregion
                            }
                            else if (shiftID2.HasValue && shiftID2 != Guid.Empty)
                            {
                                #region Trường hợp có lịch làm việc - ca thứ hai

                                workday = CreateWorkday(unitOfWork, date, profile, inOutConfig, listHoliday, listWorkday, workday,
                                    true, shiftID2, shiftInfo2, listShift, listMonthShifts, out actualShiftID2,
                                    listTamScanLogByCardCode, listTamScanLogByCardCodeUnchecked);

                                #endregion
                            }
                            else if (inOutConfig != null && inOutConfig.DetectShift == Boolean.TrueString)
                            {
                                #region Trường hợp không có lịch làm việc

                                var listTamScanLogByDate = listTamScanLogByCardCodeUnchecked.Where(d =>
                                    d.TimeLog.Value.Date == date).ToList();

                                if (listTamScanLogByDate.Count() > 0)
                                {
                                    var shiftInTime = date.Date.AddHours(12);
                                    var listTimeLog = new List<Att_TAMScanLogEntity>();
                                    bool isWrongShiftDetected = false;

                                    foreach (var item in listTamScanLogByDate)
                                    {
                                        if (item.TimeLog < shiftInTime)
                                        {
                                            //Quẹt thẻ gần với thời gian vào của ca -> miss-out hoặc làm sai ca
                                            //Tìm ca làm việc trước gần nhất và dòng quẹt thẻ trước gần nhất của nhân viên đang xét
                                            var preShift = listMonthShifts.Where(d => d.Key >= date.AddDays(-1) && d.Key < date).OrderByDescending(d => d.Key).FirstOrDefault();
                                            var preShiftValue = preShift.Value != null && preShift.Value.Count() > 0 ? preShift.Value.LastOrDefault() : null;

                                            //Lấy dòng quẹt thẻ hiện tại và 2 dòng quẹt thẻ phía trước gần nhất của nhân viên đang xét để kiểm tra ca trước
                                            var listPreTamScanLog = listTamScanLogByCardCode.Where(d => d.TimeLog <= item.TimeLog).OrderByDescending(d => d.TimeLog).Take(3).ToList();
                                            var listTamScanLogByPreShift = GetTamScanLogByShift(preShift.Key, listShift, preShiftValue, listMonthShifts, listPreTamScanLog.ToArray());

                                            //Nếu 2 dòng quẹt thẻ phía trước không thuộc một ca khác trong lịch của nhân viên
                                            if (listTamScanLogByPreShift == null || listTamScanLogByPreShift.Count() < 2)
                                            {
                                                var preTamScanLog = listPreTamScanLog.Where(d => d.TimeLog < item.TimeLog).FirstOrDefault();
                                                var currentShiftDuration = 12;//cho ca tối đa được 12 tiếng đồng hồ

                                                if (preTamScanLog != null && listWorkday.Any(d => d.ProfileID == profile.ID && (d.InTime1 == preTamScanLog.TimeLog
                                                    || d.InTime2 == preTamScanLog.TimeLog || d.InTime3 == preTamScanLog.TimeLog || d.InTime4 == preTamScanLog.TimeLog
                                                    || d.OutTime1 == preTamScanLog.TimeLog || d.OutTime2 == preTamScanLog.TimeLog || d.OutTime3 == preTamScanLog.TimeLog
                                                    || d.OutTime4 == preTamScanLog.TimeLog)))
                                                {
                                                    //Trường hợp quẹt thẻ này đã sử dụng
                                                    preTamScanLog = null;
                                                }

                                                //Nếu quẹt thẻ hiện tại kết với quẹt thẻ trước đó mà phù hợp duration thì ghép sai ca
                                                if (preTamScanLog != null && preTamScanLog.TimeLog.HasValue && item.TimeLog.Value
                                                    .Subtract(preTamScanLog.TimeLog.Value).TotalHours <= currentShiftDuration)
                                                {
                                                    if (preTamScanLog.TimeLog < date)
                                                    {
                                                        if (preShiftValue.HasValue)
                                                        {
                                                            workday = listWorkday.Where(d => d.ProfileID == profile.ID
                                                                && d.WorkDate == date.AddDays(-1)).FirstOrDefault();

                                                            if (workday == null)
                                                            {
                                                                if (preTamScanLog.TimeLog.Value.Date >= dateFrom.Date
                                                                    && preTamScanLog.TimeLog.Value.Date <= dateTo.Date)
                                                                {
                                                                    workday = new Att_Workday();
                                                                    unitOfWork.AddObject(typeof(Att_Workday), workday);
                                                                    workday.WorkDate = preTamScanLog.TimeLog.Value.Date;
                                                                    workday.FirstInTime = workday.InTime1 = preTamScanLog.TimeLog;
                                                                    workday.LastOutTime = workday.OutTime1 = item.TimeLog;
                                                                    workday.Type = WorkdayType.E_DETECTED_SHIFT.ToString();

                                                                    isPreWorkday = true;
                                                                    preTamScanLog.Checked = true;
                                                                    isWrongShiftDetected = true;
                                                                    break;//chỉ hỗ trợ 1 ca
                                                                }
                                                            }
                                                            else
                                                            {
                                                                listTimeLog.Add(item);
                                                            }
                                                        }
                                                        else
                                                        {
                                                            listTimeLog.Add(item);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (workday == null)
                                                        {
                                                            workday = new Att_Workday();
                                                            unitOfWork.AddObject(typeof(Att_Workday), workday);
                                                        }

                                                        workday.FirstInTime = workday.InTime1 = preTamScanLog.TimeLog;
                                                        workday.LastOutTime = workday.OutTime1 = item.TimeLog;
                                                        workday.Type = WorkdayType.E_DETECTED_SHIFT.ToString();

                                                        preTamScanLog.Checked = true;
                                                        isWrongShiftDetected = true;
                                                        break;//chỉ hỗ trợ 1 ca
                                                    }
                                                }
                                                else
                                                {
                                                    listTimeLog.Add(item);
                                                }
                                            }
                                            else
                                            {
                                                if (!listTamScanLogByPreShift.Any(d => d.TimeLog == item.TimeLog))
                                                {
                                                    listTimeLog.Add(item);
                                                }

                                                listTimeLog = listTimeLog.Where(d => !listTamScanLogByPreShift.Contains(d)).ToList();
                                            }
                                        }
                                        else if (item.TimeLog > shiftInTime)
                                        {
                                            //Quẹt thẻ gần với thời gian ra của ca -> miss-in hoặc làm sai ca
                                            //Tìm ca làm việc tiếp theo gần nhất và dòng quẹt thẻ tiếp theo gần nhất của nhân viên đang xét
                                            var nextShift = listMonthShifts.Where(d => d.Key > date && d.Key <= date.AddDays(1)).OrderBy(d => d.Key).FirstOrDefault();
                                            var nextShiftValue = nextShift.Value != null && nextShift.Value.Count() > 0 ? nextShift.Value.FirstOrDefault() : null;

                                            //Lấy dòng quẹt thẻ hiện tại và 2 dòng quẹt thẻ tiếp theo gần nhất của nhân viên đang xét để kiểm tra ca tiếp theo
                                            var listNextTamScanLog = listTamScanLogByCardCodeUnchecked.Where(d => d.TimeLog >= item.TimeLog).OrderBy(d => d.TimeLog).Take(3).ToList();
                                            var listTamScanLogByNextShift = GetTamScanLogByShift(nextShift.Key, listShift, nextShiftValue, listMonthShifts, listNextTamScanLog.ToArray());

                                            //Nếu 2 dòng quẹt thẻ tiếp theo không thuộc một ca khác trong lịch của nhân viên
                                            if (listTamScanLogByNextShift == null || listTamScanLogByNextShift.Count() < 2)
                                            {
                                                listTimeLog.Add(item);

                                                foreach (var nextTamScanLog in listNextTamScanLog.Where(d => d.TimeLog > item.TimeLog))
                                                {
                                                    var currentShiftDuration = 12;//cho ca tối đa được 12 tiếng đồng hồ

                                                    //Nếu quẹt thẻ hiện tại kết với quẹt thẻ tiếp theo mà phù hợp duration thì ghép sai ca
                                                    if (nextTamScanLog != null && nextTamScanLog.TimeLog.HasValue && nextTamScanLog
                                                        .TimeLog.Value.Subtract(item.TimeLog.Value).TotalHours <= currentShiftDuration)
                                                    {
                                                        listTimeLog.Add(nextTamScanLog);
                                                    }
                                                }

                                            }
                                            else
                                            {
                                                if (!listTamScanLogByNextShift.Any(d => d.TimeLog == item.TimeLog))
                                                {
                                                    listTimeLog.Add(item);
                                                }

                                                listTimeLog = listTimeLog.Where(d => !listTamScanLogByNextShift.Contains(d)).ToList();
                                            }
                                        }
                                    }

                                    if (!isWrongShiftDetected)
                                    {
                                        if (listTimeLog.Count() > 0)
                                        {
                                            var listRemove = new List<Att_TAMScanLogEntity>();

                                            if (workday == null)
                                            {
                                                workday = new Att_Workday();
                                                unitOfWork.AddObject(typeof(Att_Workday), workday);
                                            }

                                            if (listTimeLog.Count() >= 2)
                                            {
                                                workday.FirstInTime = workday.InTime1 = listTimeLog.OrderBy(d => d.TimeLog).Select(d => d.TimeLog).FirstOrDefault();
                                                workday.LastOutTime = workday.OutTime1 = listTimeLog.OrderBy(d => d.TimeLog).Select(d => d.TimeLog).LastOrDefault();
                                                var currentShiftDuration = 16;//cho ca tối đa được 12 tiếng đồng hồ
                                                isWrongShiftDetected = true;

                                                if (workday.LastOutTime.HasValue && workday.FirstInTime.HasValue && workday.LastOutTime.Value
                                                    .Subtract(workday.FirstInTime.Value).TotalHours <= currentShiftDuration)
                                                {
                                                    workday.Type = WorkdayType.E_DETECTED_SHIFT.ToString();
                                                }
                                                else
                                                {
                                                    foreach (var item in listTimeLog.Where(d => d.TimeLog > workday.FirstInTime).OrderByDescending(d => d.TimeLog))
                                                    {
                                                        workday.LastOutTime = workday.OutTime1 = item.TimeLog;

                                                        if (item.TimeLog > workday.FirstInTime && item.TimeLog.HasValue && workday.FirstInTime.HasValue
                                                            && item.TimeLog.Value.Subtract(workday.FirstInTime.Value).TotalHours <= currentShiftDuration)
                                                        {
                                                            workday.Type = WorkdayType.E_DETECTED_SHIFT.ToString();
                                                            break;
                                                        }
                                                        else
                                                        {
                                                            workday.Type = WorkdayType.E_LONGIN_SHIFT.ToString();
                                                        }
                                                    }

                                                    listRemove = listTimeLog.Where(d => d.TimeLog > workday.LastOutTime).ToList();
                                                }
                                            }
                                            else if (listTimeLog.Count() == 1)
                                            {
                                                if (listTimeLog.Any(d => d.TimeLog < shiftInTime))
                                                {
                                                    workday.FirstInTime = workday.InTime1 = listTimeLog.Select(d => d.TimeLog).FirstOrDefault();
                                                    workday.Type = WorkdayType.E_MISS_OUT.ToString();//miss-in hay miss-out cũng như nhau
                                                }
                                                else
                                                {
                                                    workday.LastOutTime = workday.OutTime1 = listTimeLog.Select(d => d.TimeLog).FirstOrDefault();
                                                    workday.Type = WorkdayType.E_MISS_IN.ToString();//miss-in hay miss-out cũng như nhau
                                                }

                                                isWrongShiftDetected = true;
                                            }

                                            listTimeLog.Where(d => !listRemove.Contains(d)).ToList().ForEach(d => d.Checked = true);
                                        }
                                    }

                                    if (workday != null)
                                    {
                                        //Trường hợp wrong-shift và detected-shift thì phải check lại actualShiftID
                                        actualShiftID1 = GetDetectedShiftID(workday.InTime1, workday.OutTime1, listShift, null);
                                    }
                                }

                                #endregion
                            }

                            if (workday != null)
                            {
                                if (string.IsNullOrWhiteSpace(workday.Type))
                                {
                                    workday.Type = WorkdayType.E_NORMAL.ToString();
                                }

                                workday.SrcType = WorkdaySrcType.E_NORMAL.ToString();
                                workday.ProfileID = profile.ID;

                                listTamScanLogByCardCode = listTamScanLogRemove.Where(d => d.ProfileID == profile.ID).OrderBy(d => d.TimeLog).ToList();
                                listTamScanLogByCardCodeUnchecked = listTamScanLogByCardCode.Where(d => !d.Checked && d.TimeLog.HasValue).ToList();

                                workday.FirstInTime = workday.InTime1 = FindTimeLogAvailable(listTamScanLogByCardCode, workday.InTime1, minMinutesSameAtt, false);
                                workday.LastOutTime = workday.OutTime1 = FindTimeLogAvailable(listTamScanLogByCardCode, workday.OutTime1, minMinutesSameAtt, true);

                                if (!isPreWorkday)
                                {
                                    workday.WorkDate = date;
                                    workday.InTimeRoot = workday.InTime1;
                                    workday.OutTimeRoot = workday.OutTime1;
                                }

                                if (shiftID1 != null && shiftID1 != Guid.Empty)
                                {
                                    workday.ShiftID = shiftID1.Value;
                                    workday.ShiftActual = shiftID1.Value;
                                    workday.ShiftApprove = shiftID1.Value;
                                }

                                if (shiftID2 != null && shiftID2 != Guid.Empty)
                                {
                                    workday.Shift2ID = shiftID2.Value;
                                    workday.ShiftActual2 = shiftID2.Value;
                                    workday.ShiftApprove2 = shiftID2.Value;
                                }

                                if (actualShiftID1.HasValue && actualShiftID1 != Guid.Empty)
                                {
                                    workday.ShiftActual = actualShiftID1;
                                    workday.ShiftApprove = actualShiftID1;
                                }

                                if (actualShiftID2.HasValue && actualShiftID2 != Guid.Empty)
                                {
                                    workday.ShiftActual2 = actualShiftID2;
                                    workday.ShiftApprove2 = actualShiftID2;
                                }

                                var listLeaveDayByProfile = listLeaveDay.Where(d => d.ProfileID == profile.ID).ToList();

                                var leaveDayID1 = listLeaveDayByProfile.Where(d => workday.WorkDate >= d.DateStart
                                    && workday.WorkDate <= d.DateEnd).Select(d => d.ID).FirstOrDefault();

                                var leaveDayID2 = listLeaveDayByProfile.Where(d => d.ID != workday.LeaveDayID1 && workday.WorkDate
                                    >= d.DateStart && workday.WorkDate <= d.DateEnd).Select(d => d.ID).FirstOrDefault();

                                workday.LeaveDayID1 = leaveDayID1 != Guid.Empty ? (Guid?)leaveDayID1 : null;
                                workday.LeaveDayID2 = leaveDayID2 != Guid.Empty ? (Guid?)leaveDayID2 : null;

                                workday.MissInOutReason = listLeaveDayType.Where(d => listLeaveDayByProfile.Any(p => p.LeaveDayTypeID == d.ID)
                                    && d.MissInOutReasonID != null).Select(d => d.MissInOutReasonID).FirstOrDefault();

                                listWorkday.Add(workday);
                            }
                        }

                        #endregion

                        #region Chia thành nhiều giai đoạn lưu ngày công

                        //Nên submit lần đầu tiên khi đã tính được 5 profile
                        bool firstSubmit = listProfile.ToList().IndexOf(profile) == 5;
                        firstSubmit = totalComputed >= 5 ? false : firstSubmit;//đã chạy 1 lần
                        totalComputedProfileForSubmit++;
                        totalComputed++;

                        if (firstSubmit || totalComputedProfileForSubmit >= totalComputedProfileMustSubmit)
                        {
                            listWorkdayAnalyze = listWorkday.Where(d => !listWorkdayChecked.Contains(d)).ToList();
                            listWorkdayChecked.AddRange(listWorkdayAnalyze);

                            leavedayServices.AnalyzeLeaveLate(listWorkdayAnalyze, lstPrenancy, lstLateEarlyRule,
                                listShift, lstGrade, lstGradeConfig, lstDayOff);

                            totalComputedProfileForSubmit = firstSubmit ? totalComputedProfileForSubmit : 0;
                            dataErrorCode = unitOfWork.SaveChanges(userID);

                            if (dataErrorCode == DataErrorCode.Locked)
                            {
                                break;
                            }
                        }

                        #endregion
                    }
                }

                #endregion

                #region Lưu kết quả tổng hợp in out

                listWorkdayAnalyze = listWorkday.Where(d => !listWorkdayChecked.Contains(d)).ToList();
                listWorkdayChecked.AddRange(listWorkdayAnalyze);

                leavedayServices.AnalyzeLeaveLate(listWorkdayAnalyze, lstPrenancy, lstLateEarlyRule,
                    listShift, lstGrade, lstGradeConfig, lstDayOff);

                unitOfWork.SetCorrectOrgStructureID(listWorkday);
                dataErrorCode = unitOfWork.SaveChanges(userID);

                #endregion
            }

            return listWorkday.Count();
        }
Esempio n. 10
0
 public virtual void Add(List <TEntity> listEntity)
 {
     UnitOfWork.AddObject(typeof(TEntity), listEntity.ToArray());
 }
Esempio n. 11
0
 public virtual void Add(TEntity entity)
 {
     UnitOfWork.AddObject(typeof(TEntity), entity);
 }
Esempio n. 12
0
        public DataErrorCode SaveRoster()
        {
            var dataErrorCode = DataErrorCode.Success;
            var ListRosterCorrect = GetImportObject();

            if (ListRosterCorrect != null && ListRosterCorrect.Count() > 0)
            {
                using (var context = new VnrHrmDataContext())
                {
                    IUnitOfWork unitOfWork = new UnitOfWork(context);
                    var listDuplicate = new List<Att_Roster>();

                    var listRoster = ListRosterCorrect.Select(d =>
                        new Att_Roster
                        {
                            ProfileID = d.ProfileID,
                            DateStart = d.DateStart,
                            DateEnd = d.DateEnd,
                            MonShiftID = d.MonShiftID,
                            TueShiftID = d.TueShiftID,
                            WedShiftID = d.WedShiftID,
                            ThuShiftID = d.ThuShiftID,
                            FriShiftID = d.FriShiftID,
                            SatShiftID = d.SatShiftID,
                            SunShiftID = d.SunShiftID,
                            Type = RosterType.E_DEFAULT.ToString(),
                            Status = RosterStatus.E_APPROVED.ToString()
                        }).ToArray();

                    if (ImportType == ImportRosterType.OverrideMonth)
                    {
                        var listProfileID = listRoster.Select(d => d.ProfileID).Distinct().ToList();
                        var dateStart = listRoster.Where(d => d.DateStart > SqlDateTime.MinValue.Value).Select(d => d.DateStart.Date).OrderBy(d => d).FirstOrDefault();
                        var dateEnd = dateStart.AddMonths(1).Date;//Sau này nếu nghiệp vụ thay đổi chỉ xóa những ngày khai báo thì lấy max của cột DateEnd trong danh sách

                        //Không xóa theo kiểu miền giao DateStart và DateEnd -> có thể sai trường hợp roster dài nhiều tháng
                        unitOfWork.Delete<Att_Roster>(unitOfWork.CreateQueryable<Att_Roster>(d => d.DateStart >= dateStart
                            && d.DateEnd <= dateEnd && listProfileID.Contains(d.ProfileID)));
                    }
                    else if (ImportType == ImportRosterType.OverrideHasValue)
                    {
                        var listProfileID = listRoster.Select(d => d.ProfileID).Distinct().ToList();
                        var dateStart = listRoster.Where(d => d.DateStart > SqlDateTime.MinValue.Value).Select(d => d.DateStart.Date).OrderBy(d => d).FirstOrDefault();
                        var dateEnd = listRoster.Where(d => d.DateEnd > SqlDateTime.MinValue.Value).Select(d => d.DateEnd.Date).OrderBy(d => d).LastOrDefault();

                        var listOldRoster = unitOfWork.CreateQueryable<Att_Roster>(Guid.Empty, d => d.DateStart <= dateEnd
                            && d.DateEnd >= dateStart && listProfileID.Contains(d.ProfileID)).ToList<Att_Roster>();

                        foreach (var roster in listRoster)
                        {
                            DateTime rosterStart = roster.DateStart.Date;
                            DateTime rosterEnd = roster.DateEnd.Date;

                            for (DateTime date = rosterStart; date <= rosterEnd; date = date.AddDays(1))
                            {
                                var oldRoster = listOldRoster.Where(d => d.DateStart.Date <= date &&
                                    d.DateEnd.Date >= date && d.ProfileID == roster.ProfileID).FirstOrDefault();

                                if (oldRoster != null)
                                {
                                    if (date.DayOfWeek == DayOfWeek.Monday && roster.MonShiftID.HasValue)
                                    {
                                        oldRoster.MonShiftID = roster.MonShiftID;
                                        roster.MonShiftID = null;
                                    }
                                    else if (date.DayOfWeek == DayOfWeek.Tuesday && roster.TueShiftID.HasValue)
                                    {
                                        oldRoster.TueShiftID = roster.TueShiftID;
                                        roster.TueShiftID = null;
                                    }
                                    else if (date.DayOfWeek == DayOfWeek.Wednesday && roster.WedShiftID.HasValue)
                                    {
                                        oldRoster.WedShiftID = roster.WedShiftID;
                                        roster.WedShiftID = null;
                                    }
                                    else if (date.DayOfWeek == DayOfWeek.Thursday && roster.ThuShiftID.HasValue)
                                    {
                                        oldRoster.ThuShiftID = roster.ThuShiftID;
                                        roster.ThuShiftID = null;
                                    }
                                    else if (date.DayOfWeek == DayOfWeek.Friday && roster.FriShiftID.HasValue)
                                    {
                                        oldRoster.FriShiftID = roster.FriShiftID;
                                        roster.FriShiftID = null;
                                    }
                                    else if (date.DayOfWeek == DayOfWeek.Saturday && roster.SatShiftID.HasValue)
                                    {
                                        oldRoster.SatShiftID = roster.SatShiftID;
                                        roster.SatShiftID = null;
                                    }
                                    else if (date.DayOfWeek == DayOfWeek.Sunday && roster.SunShiftID.HasValue)
                                    {
                                        oldRoster.SunShiftID = roster.SunShiftID;
                                        roster.SunShiftID = null;
                                    }
                                }
                            }

                            if ((!roster.MonShiftID.HasValue && !roster.TueShiftID.HasValue && !roster.WedShiftID.HasValue
                                && !roster.ThuShiftID.HasValue && !roster.FriShiftID.HasValue && !roster.SatShiftID.HasValue
                                && !roster.SunShiftID.HasValue) || roster.DateStart > roster.DateEnd)
                            {
                                listDuplicate.Add(roster);
                            }
                        }
                    }

                    var listImported = listRoster.Where(d => !listDuplicate.Contains(d));
                    unitOfWork.SetCorrectOrgStructureID(listImported.ToList());

                    unitOfWork.AddObject(listImported.ToArray());
                    dataErrorCode = unitOfWork.SaveChanges(LoginUserID);

                    RemoveImportObject();
                    RemoveInvalidObject();
                }
            }

            return dataErrorCode;
        }