private int ComputeAttendance(IUnitOfWork unitOfWork, Guid userID, DateTime monthYear, Guid cutOffDurationID, DateTime attendanceFrom, DateTime attendanceTo, Hre_ProfileEntity profile, Cat_GradeAttendance gradeCfg, List<Cat_Shift> listShift, List<Att_AnnualDetail> listAnnualDetailByProfile, List<Hre_HDTJob> listHDTJobByProfile, List<Att_LeaveDayEntity> listLeaveDayByProfile, List<Cat_LeaveDayType> listLeaveDayType, List<Att_OvertimeEntity> listOvertimeByProfile, List<Cat_OvertimeType> listOvertimeType, List<Att_PregnancyEntity> listPregnancyByProfile, List<Cat_LateEarlyRule> listLateEarlyRule, List<Att_Workday> listWorkDayByProfile, List<Att_AttendanceTableEntity> listPreAttendanceTableByProfile, List<Att_TimeOffInLieuEntity> listTimeOffInLieuByProfile, Sys_AllSetting notAutoRegHolidayLeave, Sys_AllSetting otholidayscompute400, Sys_AllSetting missTAM_LeaveType, Dictionary<DateTime, List<Guid?>> listMonthShiftID, List<Cat_DayOff> listHoliday) { Guid profileID = profile.ID; if (gradeCfg == null || profile == null) { return 0; } DateTime midCutOffDate = attendanceTo; bool isMidCutOffDay = false; #region Tạo Att_AttendanceTable theo khoảng thời gian Att_AttendanceTable attendanceTable = new Att_AttendanceTable(); unitOfWork.AddObject(typeof(Att_AttendanceTable), attendanceTable); attendanceTable.Status = AttendanceTableStatus.E_WAITING.ToString(); attendanceTable.ID = Guid.NewGuid(); attendanceTable.ProfileID = profileID; attendanceTable.MonthYear = monthYear; attendanceTable.DateStart = attendanceFrom; attendanceTable.DateEnd = attendanceTo; double anlDayTaken = 0D; double sickDayTaken = 0D; double coBeginPeriod = 0D; double coEndPeriod = 0D; if (cutOffDurationID != Guid.Empty) { attendanceTable.CutOffDurationID = cutOffDurationID; } //Tính công đến ngày cấu hình trong grade, những ngày sau mặc định full công - Khách hàng CPG if (gradeCfg != null && gradeCfg.IsMonthlyMidCutOff == true && gradeCfg.MidCutOffDay > 0) { if (!profile.DateQuit.HasValue || profile.DateQuit > attendanceTo) { midCutOffDate = new DateTime(monthYear.Year, monthYear.Month, gradeCfg.MidCutOffDay.Value); //Luôn tạo một dòng Ứng công lúc tính công, kô cần biết có nghỉ không Att_AdvanceTableItem advanceTableItem = new Att_AdvanceTableItem(); advanceTableItem.Att_AttendanceTable = attendanceTable; advanceTableItem.DateFrom = midCutOffDate.AddDays(1); advanceTableItem.DateTo = attendanceTo; isMidCutOffDay = true; } } #endregion #region Tạo Att_AttendanceTableItem theo khoảng thời gian List<Att_AttendanceTableItem> listAttendanceTableItemByProfile = new List<Att_AttendanceTableItem>(); for (DateTime date = attendanceFrom.Date; date <= attendanceTo; date = date.AddDays(1)) { var workdayByProfile = listWorkDayByProfile.Where(d => d.WorkDate.Date == date).FirstOrDefault(); Guid? shiftID1 = Guid.Empty; Guid? shiftID2 = Guid.Empty; var shiftByDate = listMonthShiftID.Where(d => d.Key.Date == date).Select(d => d.Value).FirstOrDefault(); if (shiftByDate != null) { if (shiftByDate.Count() > 0) { shiftID1 = shiftByDate[0]; } if (shiftByDate.Count() > 1) { shiftID2 = shiftByDate[1]; } } if (workdayByProfile == null && gradeCfg.EDType == PayrollComputeMethod.E_SUBTRACT.ToString()) { if (shiftID1.HasValue && shiftID1 != Guid.Empty) { workdayByProfile = new Att_Workday { ShiftID = shiftID1, ShiftApprove = shiftID1, ShiftActual = shiftID1, WorkDate = date }; if (shiftID2.HasValue && shiftID2 != Guid.Empty) { workdayByProfile.Shift2ID = shiftID2; workdayByProfile.ShiftApprove2 = shiftID2; workdayByProfile.ShiftActual2 = shiftID2; } if (listHoliday.Any(d => d.DateOff.Date == date)) { workdayByProfile.Type = WorkdayType.E_HOLIDAY.ToString(); } } } Att_AttendanceTableItem attendanceTableItem = new Att_AttendanceTableItem(); listAttendanceTableItemByProfile.Add(attendanceTableItem); attendanceTableItem.ID = Guid.NewGuid(); attendanceTableItem.Att_AttendanceTable = attendanceTable; attendanceTableItem.DutyCode = DutyCode.E_OFF.ToString(); attendanceTableItem.WorkDate = date; double nightDuration = 0F; double workDuration = 0F; attendanceTableItem.WorkPaidHours = 0; attendanceTableItem.PaidLeaveHours = 0; attendanceTableItem.UnpaidLeaveHours = 0; attendanceTableItem.LateEarlyMinutes = 0; attendanceTableItem.LateInMinutes = 0; attendanceTableItem.EarlyOutMinutes = 0; attendanceTableItem.LeaveHours = 0; attendanceTableItem.LeaveDays = 0; attendanceTableItem.WorkHourFirstHaftShift = 0; attendanceTableItem.WorkHourLastHaftShift = 0; if (workdayByProfile != null) { attendanceTableItem.MissInOutReasonID = workdayByProfile.MissInOutReason; attendanceTableItem.FirstInTime = workdayByProfile.FirstInTime; attendanceTableItem.LastOutTime = workdayByProfile.LastOutTime; attendanceTableItem.RootInTime = workdayByProfile.FirstInTime; attendanceTableItem.RootOutTime = workdayByProfile.LastOutTime; attendanceTableItem.ShiftID = workdayByProfile.ShiftID; attendanceTableItem.Shift2ID = workdayByProfile.Shift2ID; List<DateTime?> listInTime = new List<DateTime?>(); List<DateTime?> listOutTime = new List<DateTime?>(); listInTime.Add(workdayByProfile.InTime1); listInTime.Add(workdayByProfile.InTime2); listInTime.Add(workdayByProfile.InTime3); listInTime.Add(workdayByProfile.InTime4); listOutTime.Add(workdayByProfile.OutTime1); listOutTime.Add(workdayByProfile.OutTime2); listOutTime.Add(workdayByProfile.OutTime3); listOutTime.Add(workdayByProfile.OutTime4); attendanceTableItem.FirstInTime = listInTime.Where(d => d.HasValue).OrderBy(d => d).FirstOrDefault(); attendanceTableItem.LastOutTime = listOutTime.Where(d => d.HasValue).OrderBy(d => d).LastOrDefault(); if (workdayByProfile.ShiftApprove.HasValue) { attendanceTableItem.ShiftID = workdayByProfile.ShiftApprove.Value; } if (workdayByProfile.ShiftApprove2.HasValue) { attendanceTableItem.Shift2ID = workdayByProfile.ShiftApprove2.Value; } if (gradeCfg.AttendanceMethod == AttendanceMethod.E_FULL.ToString()) { if (listMonthShiftID.Any(d => d.Key.Date == date)) { //Bao.Tran: khi không kiểm tra vào ra thì ưu tiên ca đã tạo trong lịch làm việc if (shiftID1 != Guid.Empty) { attendanceTableItem.ShiftID = shiftID1; } if (shiftID2 != Guid.Empty) { attendanceTableItem.Shift2ID = shiftID2; } } //Bao.Tran: yêu cầu nếu công ko kiểm tra in/out thì không lấy in/out attendanceTableItem.FirstInTime = null; attendanceTableItem.LastOutTime = null; //comment code này do ảnh hưởng task 0046036 : ko tính được ngày nghỉ lễ ////Bao.Tran: do khi THNC cũ, Nếu ko bỏ type thì nó sẽ tính ngày công trên là holiday (attendanceTableItem.IsHoliday = true;) //workdayByProfile.Type = WorkdayType.E_NORMAL.ToString(); } //kiểm tra ngày làm việc có Lịch làm việc hay ko? if (attendanceTableItem.ShiftID != null && workdayByProfile.FirstInTime != null && workdayByProfile.LastOutTime != null) { #region Kiểm tra HDTJob var listHDTJobByProfileByDate = listHDTJobByProfile.Where(d => (d.DateFrom.Value.Date <= date && d.DateTo == null) || (d.DateTo != null && d.DateFrom.Value.Date <= date && d.DateTo.Value.Date >= date)).ToList(); foreach (var HDTJobByProfile in listHDTJobByProfileByDate) { if (HDTJobByProfile != null) { attendanceTableItem.HDTJobType = HDTJobByProfile.Type; } } #endregion } } var shiftByProfile1 = listShift.Where(d => d.ID == attendanceTableItem.ShiftID).FirstOrDefault(); var shiftByProfile2 = listShift.Where(d => d.ID == attendanceTableItem.Shift2ID).FirstOrDefault(); if (shiftByProfile1 != null) { attendanceTableItem.Cat_Shift = shiftByProfile1; attendanceTableItem.AvailableHours = shiftByProfile1.udAvailableHours; } if (shiftByProfile2 != null) { attendanceTableItem.Cat_Shift1 = shiftByProfile2; } if ((workdayByProfile != null && workdayByProfile.Type == WorkdayType.E_HOLIDAY.ToString()) || (attendanceTableItem.ShiftID.HasValue && attendanceTableItem.ShiftID != Guid.Empty)) { //Ngày đang xét là ngày làm việc bình thường attendanceTableItem.DutyCode = DutyCode.E_ON.ToString(); if (workdayByProfile.Type == WorkdayType.E_HOLIDAY.ToString()) { attendanceTableItem.IsHoliday = true; } } else { //Ngày đang xét không phải là ngày làm việc bình thường attendanceTableItem.DutyCode = DutyCode.E_OFF.ToString(); } #region Kiểm tra ngày nghỉ - leaveday if (attendanceTableItem.DutyCode != DutyCode.E_OFF.ToString()) { var listLeaveDayByProfileByDate = listLeaveDayByProfile.Where(d => d.DateStart.Date <= date && d.DateEnd.Date >= date).ToList(); foreach (var leaveDayByProfile in listLeaveDayByProfileByDate) { if (leaveDayByProfile != null) { double leaveHours = 0D; double leaveDays = 0D; var leaveDayType = listLeaveDayType.Where(d => d.ID == leaveDayByProfile.LeaveDayTypeID).FirstOrDefault(); var standardWorkHours = attendanceTableItem.AvailableHours; if (leaveDayByProfile.DurationType == LeaveDayDurationType.E_FULLSHIFT.ToString()) { //Lấy giờ nghỉ của ngày đang xét dựa vào ca làm việc leaveHours = standardWorkHours; leaveDays = 1;//Nghỉ full shift } else { leaveHours = leaveDayByProfile.LeaveHours.GetDouble(); leaveDays = leaveDayByProfile.LeaveDays.GetDouble(); } leaveHours = leaveHours > standardWorkHours ? standardWorkHours : leaveHours; leaveDays = leaveDays > 1 ? 1 : leaveDays;//Tối đa là nghỉ 1 ngày if (!attendanceTableItem.LeaveTypeID.HasValue) { attendanceTableItem.LeaveTypeID = leaveDayByProfile.LeaveDayTypeID; attendanceTableItem.Cat_LeaveDayType = leaveDayType; attendanceTableItem.LeaveHours = leaveHours; attendanceTableItem.LeaveDays = leaveDays; } else if (!attendanceTableItem.ExtraLeaveTypeID.HasValue) { attendanceTableItem.ExtraLeaveTypeID = leaveDayByProfile.LeaveDayTypeID; attendanceTableItem.Cat_LeaveDayType1 = leaveDayType; attendanceTableItem.ExtraLeaveHours = leaveHours; attendanceTableItem.ExtraLeaveDays = leaveDays; } if (leaveDayType != null && leaveDays > 0) { if (leaveDayType.Code == LeavedayTypeCode.ANL.ToString()) { anlDayTaken += leaveDays; } if (leaveDayType.Code == LeavedayTypeCode.SICK.ToString()) { sickDayTaken += leaveDays; } } } } } #endregion #region Kiểm tra làm thêm - overtime if (gradeCfg != null && gradeCfg.IsReceiveOvertimeBonus.Get_Boolean()) { var listOvertimeByProfileByDate = listOvertimeByProfile.Where(d => d.WorkDate.Date == date).ToList(); foreach (var overtime in listOvertimeByProfileByDate) { if (overtime != null) { double overtimeHours = overtime.ConfirmHours; string overTimeStatus = OverTimeStatus.E_APPROVED.ToString(); if (overTimeStatus == OverTimeStatus.E_APPROVED.ToString()) { overtimeHours = overtime.ApproveHours.GetDouble(); } else if (overTimeStatus == OverTimeStatus.E_CONFIRM.ToString()) { overtimeHours = overtime.ConfirmHours; } if (!attendanceTableItem.OvertimeTypeID.HasValue) { attendanceTableItem.OvertimeTypeID = overtime.OvertimeTypeID; attendanceTableItem.Cat_OvertimeType = listOvertimeType.Where(s => s.ID == overtime.OvertimeTypeID).FirstOrDefault(); attendanceTableItem.OvertimeDurationType = overtime.DurationType; attendanceTableItem.OvertimeRegisterHours = overtime.RegisterHours; attendanceTableItem.OvertimeHours = overtimeHours; } else if (!attendanceTableItem.ExtraOvertimeTypeID.HasValue) { attendanceTableItem.ExtraOvertimeTypeID = overtime.OvertimeTypeID; attendanceTableItem.Cat_OvertimeType1 = listOvertimeType.Where(s => s.ID == overtime.OvertimeTypeID).FirstOrDefault(); attendanceTableItem.ExtraOvertimeDurationType = overtime.DurationType; attendanceTableItem.ExtraOvertimeRegisterHours = overtime.RegisterHours; attendanceTableItem.ExtraOvertimeHours = overtimeHours; } else if (!attendanceTableItem.ExtraOvertimeType2ID.HasValue) { attendanceTableItem.ExtraOvertimeType2ID = overtime.OvertimeTypeID; attendanceTableItem.Cat_OvertimeType2 = listOvertimeType.Where(s => s.ID == overtime.OvertimeTypeID).FirstOrDefault(); attendanceTableItem.ExtraOvertimeDurationType2 = overtime.DurationType; attendanceTableItem.ExtraOvertimeRegisterHours2 = overtime.RegisterHours; attendanceTableItem.ExtraOvertimeHours2 = overtimeHours; } else if (!attendanceTableItem.ExtraOvertimeType3ID.HasValue) { attendanceTableItem.ExtraOvertimeType3ID = overtime.OvertimeTypeID; attendanceTableItem.Cat_OvertimeType3 = listOvertimeType.Where(s => s.ID == overtime.OvertimeTypeID).FirstOrDefault(); attendanceTableItem.ExtraOvertimeDurationType3 = overtime.DurationType; attendanceTableItem.ExtraOvertimeRegisterHours3 = overtime.RegisterHours; attendanceTableItem.ExtraOvertimeHours3 = overtimeHours; } } } } #endregion #region Kiểm tra có thai sản không var pregnancyByProfile = listPregnancyByProfile.Where(d => date >= d.DateStart && date <= d.DateEnd).FirstOrDefault(); if (pregnancyByProfile != null) { attendanceTableItem.IsHavingPregTreatment = true; } #endregion #region Nhân viên đã nghỉ hoặc chưa đi làm if ((profile.DateQuit.HasValue && profile.DateQuit.Value.Date <= date.Date) || (profile.DateHire.HasValue && profile.DateHire.Value.Date > date.Date)) { if (attendanceTableItem.DutyCode == DutyCode.E_ON.ToString()) { if (profile.DateHire.HasValue && profile.DateHire.Value.Date > date.Date) { if (shiftByProfile1 != null) { attendanceTableItem.udNotHiredHours = shiftByProfile1.WorkHours.GetDouble(); } else { attendanceTableItem.udNotHiredHours = attendanceTableItem.udStandardWorkHours; } if (shiftByProfile2 != null) { attendanceTableItem.udNotHiredHours += shiftByProfile2.WorkHours.GetDouble(); } } if (profile.DateQuit.HasValue && profile.DateQuit.Value.Date <= date.Date) { if (shiftByProfile1 != null) { attendanceTableItem.udTerminatedHours = shiftByProfile1.WorkHours.GetDouble(); } else { attendanceTableItem.udTerminatedHours = attendanceTableItem.udStandardWorkHours; } if (shiftByProfile2 != null) { attendanceTableItem.udTerminatedHours += shiftByProfile2.WorkHours.GetDouble(); } } } attendanceTableItem.Cat_LeaveDayType = null; continue; } #endregion #region Loại ngày nghỉ là ngày làm việc if (attendanceTableItem.Cat_LeaveDayType != null) { //Trường hợp ngày nghỉ tính như ngày đi làm => không tính nghỉ if (attendanceTableItem.Cat_LeaveDayType.IsWorkDay) { attendanceTableItem.WorkHours += attendanceTableItem.LeaveHours; attendanceTableItem.WorkPaidHours += attendanceTableItem.LeaveHours; attendanceTableItem.LeaveWorkDayHour = attendanceTableItem.LeaveHours; attendanceTableItem.LeaveWorkDayDays = attendanceTableItem.LeaveHours / attendanceTableItem.udStandardWorkHours; attendanceTableItem.Cat_LeaveDayType2 = attendanceTableItem.Cat_LeaveDayType; attendanceTableItem.Cat_LeaveDayType = null; attendanceTableItem.LeaveHours = 0; attendanceTableItem.LeaveDays = 0; } else { attendanceTableItem.PaidLeaveHours += attendanceTableItem.LeaveHours * attendanceTableItem.Cat_LeaveDayType.PaidRate; attendanceTableItem.UnpaidLeaveHours += attendanceTableItem.LeaveHours * (1 - attendanceTableItem.Cat_LeaveDayType.PaidRate); attendanceTableItem.PaidLeaveDays = attendanceTableItem.PaidLeaveHours / attendanceTableItem.udStandardWorkHours; attendanceTableItem.UnpaidLeaveDays = attendanceTableItem.UnpaidLeaveHours / attendanceTableItem.udStandardWorkHours; } } if (attendanceTableItem.Cat_LeaveDayType1 != null) { if (attendanceTableItem.Cat_LeaveDayType1.IsWorkDay) { attendanceTableItem.WorkHours += attendanceTableItem.ExtraLeaveHours; attendanceTableItem.WorkPaidHours += attendanceTableItem.ExtraLeaveHours; attendanceTableItem.Cat_LeaveDayType1 = null; attendanceTableItem.ExtraLeaveHours = 0; } else { attendanceTableItem.PaidLeaveHours += attendanceTableItem.ExtraLeaveHours * attendanceTableItem.Cat_LeaveDayType1.PaidRate; attendanceTableItem.UnpaidLeaveHours += attendanceTableItem.ExtraLeaveHours * (1 - attendanceTableItem.Cat_LeaveDayType1.PaidRate); } } #endregion if (attendanceTableItem.DutyCode == DutyCode.E_ON.ToString()) { #region IsHavingPregTreatment if (attendanceTableItem.IsHavingPregTreatment) { if ((attendanceTableItem.WorkPaidHours + attendanceTableItem.PaidLeaveHours) > attendanceTableItem.AvailableHours) { attendanceTableItem.WorkPaidHours = attendanceTableItem.AvailableHours - attendanceTableItem.PaidLeaveHours; } else { attendanceTableItem.WorkPaidHours = attendanceTableItem.WorkPaidHours; } } #endregion #region LateEarlyMinutes attendanceTableItem.LateEarlyMinutes = (int)workdayByProfile.LateEarlyDuration.GetDouble(); attendanceTableItem.RealLateEarlyMinutes = attendanceTableItem.LateEarlyMinutes; #endregion #region Tính số giờ đi làm if (workdayByProfile != null && shiftByProfile1 != null) { DateTime timeShiftStart1 = workdayByProfile.WorkDate.Date.Add(shiftByProfile1.InTime.TimeOfDay); DateTime timeShiftEnd1 = timeShiftStart1.AddHours(shiftByProfile1.CoOut); //Thời gian bắt đầu và kết thúc nghỉ giữa ca - dùng cho tính toán DateTime timeShiftBreakIn1 = timeShiftStart1.AddHours(shiftByProfile1.CoBreakIn); DateTime timeShiftBreakOut1 = timeShiftStart1.AddHours(shiftByProfile1.CoBreakOut); workDuration = GetDuration(workdayByProfile, timeShiftStart1, timeShiftEnd1, timeShiftBreakIn1, timeShiftBreakOut1, shiftByProfile1.InOutDynamic); #region Tính số giờ làm ca đêm if (shiftByProfile1.IsNightShift) { DateTime nightTimeStart = workdayByProfile.WorkDate.Date.AddHours(22); DateTime nightTimeEnd = workdayByProfile.WorkDate.Date.AddDays(1).AddHours(6); nightTimeStart = workdayByProfile.WorkDate.Date.Add(shiftByProfile1.NightTimeStart.Value.TimeOfDay); nightTimeEnd = workdayByProfile.WorkDate.Date.Add(shiftByProfile1.NightTimeEnd.Value.TimeOfDay); nightTimeEnd = nightTimeStart > nightTimeEnd ? nightTimeEnd.AddDays(1) : nightTimeEnd; //Truong hop nghi giua ca nam trong khoang bat dau ca dem nightDuration = GetDuration(workdayByProfile, nightTimeStart, nightTimeEnd, timeShiftBreakIn1, timeShiftBreakOut1, shiftByProfile1.InOutDynamic); } #endregion } if (workdayByProfile != null && shiftByProfile2 != null) { DateTime timeShiftStart2 = workdayByProfile.WorkDate.Date.Add(shiftByProfile2.InTime.TimeOfDay); DateTime timeShiftEnd2 = timeShiftStart2.AddHours(shiftByProfile2.CoOut); //Thời gian bắt đầu và kết thúc nghỉ giữa ca - dùng cho tính toán DateTime timeShiftBreakIn2 = timeShiftStart2.AddHours(shiftByProfile2.CoBreakIn); DateTime timeShiftBreakOut2 = timeShiftStart2.AddHours(shiftByProfile2.CoBreakOut); workDuration += GetDuration(workdayByProfile, timeShiftStart2, timeShiftEnd2, timeShiftBreakIn2, timeShiftBreakOut2, shiftByProfile2.InOutDynamic); #region Tính số giờ làm ca đêm if (shiftByProfile2.IsNightShift) { DateTime nightTimeStart = workdayByProfile.WorkDate.Date.AddHours(22); DateTime nightTimeEnd = workdayByProfile.WorkDate.Date.AddDays(1).AddHours(6); nightTimeStart = workdayByProfile.WorkDate.Date.Add(shiftByProfile2.NightTimeStart.Value.TimeOfDay); nightTimeEnd = workdayByProfile.WorkDate.Date.Add(shiftByProfile2.NightTimeEnd.Value.TimeOfDay); nightTimeEnd = nightTimeStart > nightTimeEnd ? nightTimeEnd.AddDays(1) : nightTimeEnd; //Truong hop nghi giua ca nam trong khoang bat dau ca dem nightDuration = GetDuration(workdayByProfile, nightTimeStart, nightTimeEnd, timeShiftBreakIn2, timeShiftBreakOut2, shiftByProfile2.InOutDynamic); } #endregion } attendanceTableItem.NightShiftHours = (nightDuration / 60);//lưu số giờ attendanceTableItem.WorkPaidHours = (workDuration / 60);//lưu số giờ attendanceTableItem.WorkHours = (workDuration / 60);//lưu số giờ attendanceTableItem.WorkPaidHours = attendanceTableItem.WorkPaidHours < 0 ? 0 : attendanceTableItem.WorkPaidHours; attendanceTableItem.WorkPaidHours = attendanceTableItem.WorkPaidHours > attendanceTableItem.AvailableHours ? attendanceTableItem.AvailableHours : attendanceTableItem.WorkPaidHours; #endregion if (gradeCfg.AttendanceMethod == AttendanceMethod.E_FULL.ToString()) { #region gradeCfg.AttendanceMethod = E_FULL if (shiftByProfile1 != null && shiftByProfile1.IsNightShift == true) { if (shiftByProfile1.NightTimeEnd != null && shiftByProfile1.NightTimeStart != null) { DateTime dateStartNightShift = shiftByProfile1.NightTimeStart.Value; DateTime dateEndNightShift = shiftByProfile1.NightTimeEnd.Value; if (dateStartNightShift > dateEndNightShift) { dateEndNightShift = dateEndNightShift.AddDays(1); } attendanceTableItem.NightShiftHours = dateEndNightShift.Subtract(dateStartNightShift).TotalHours; } else { attendanceTableItem.NightShiftHours = shiftByProfile1.WorkHours ?? 0; } } else if (shiftByProfile2 != null && shiftByProfile2.IsNightShift == true) { if (shiftByProfile2.NightTimeEnd != null && shiftByProfile2.NightTimeStart != null) { DateTime dateStartNightShift = shiftByProfile2.NightTimeStart.Value; DateTime dateEndNightShift = shiftByProfile2.NightTimeEnd.Value; if (dateStartNightShift > dateEndNightShift) { dateEndNightShift = dateEndNightShift.AddDays(1); } attendanceTableItem.NightShiftHours = dateEndNightShift.Subtract(dateStartNightShift).TotalHours; } else { attendanceTableItem.NightShiftHours = shiftByProfile2.WorkHours ?? 0; } } if ((shiftByProfile1 != null && shiftByProfile1.IsNightShift == true) || shiftByProfile2 != null && shiftByProfile2.IsNightShift == true) { if (attendanceTableItem.Cat_LeaveDayType != null) { double hourLeave = attendanceTableItem.LeaveHours + attendanceTableItem.PaidLeaveHours + attendanceTableItem.ExtraLeaveHours; attendanceTableItem.NightShiftHours = attendanceTableItem.NightShiftHours - hourLeave; if (attendanceTableItem.NightShiftHours < 0) { attendanceTableItem.NightShiftHours = 0; } } } if (attendanceTableItem.IsHoliday) { attendanceTableItem.LeaveHours = attendanceTableItem.PaidLeaveHours = 0; attendanceTableItem.WorkPaidHours = attendanceTableItem.LateEarlyMinutes = 0; attendanceTableItem.UnpaidLeaveHours = attendanceTableItem.AvailableHours; bool isNoLongerWorking = false; if ((profile.DateHire.HasValue && attendanceTableItem.WorkDate < profile.DateHire.Value) || (profile.DateQuit.HasValue && attendanceTableItem.WorkDate >= profile.DateQuit.Value)) { isNoLongerWorking = true; } if (!attendanceTableItem.IsHavingPregTreatment && !isNoLongerWorking && (notAutoRegHolidayLeave == null || notAutoRegHolidayLeave.Value1 != bool.TrueString)) { attendanceTableItem.WorkPaidHours = 0; attendanceTableItem.LateEarlyMinutes = 0; attendanceTableItem.UnpaidLeaveHours = 0; attendanceTableItem.LeaveHours = attendanceTableItem.AvailableHours; attendanceTableItem.PaidLeaveHours = attendanceTableItem.AvailableHours; attendanceTableItem.Cat_LeaveDayType = listLeaveDayType.Where(d => d.Code == LeavedayTypeCode.HLD.ToString()).FirstOrDefault(); } } attendanceTableItem.WorkHours = 0;//Thời gian làm việc = 0 attendanceTableItem.WorkPaidHours = attendanceTableItem.AvailableHours - attendanceTableItem.UnpaidLeaveHours - attendanceTableItem.PaidLeaveHours; #endregion } #region Trường hợp tăng ca trong ngày lễ if (attendanceTableItem.Cat_LeaveDayType != null && attendanceTableItem.Cat_LeaveDayType.Code == LeavedayTypeCode.HLD.ToString() && (otholidayscompute400 == null || otholidayscompute400.Value1 != bool.TrueString) && attendanceTableItem.Cat_OvertimeType != null) { var totalOvertimeHours = attendanceTableItem.OvertimeHours + attendanceTableItem.ExtraOvertimeHours + attendanceTableItem.ExtraOvertimeHours2 + attendanceTableItem.ExtraOvertimeHours3; if (gradeCfg.EDType == PayrollComputeMethod.E_SUBTRACT.ToString()) { if (attendanceTableItem.AvailableHours > totalOvertimeHours) { attendanceTableItem.LeaveHours = attendanceTableItem.AvailableHours - totalOvertimeHours; attendanceTableItem.ExtraLeaveHours = totalOvertimeHours; attendanceTableItem.PaidLeaveHours = attendanceTableItem.LeaveHours; attendanceTableItem.Cat_LeaveDayType1 = null; } else { attendanceTableItem.Cat_LeaveDayType = null; attendanceTableItem.LeaveHours = attendanceTableItem.AvailableHours; attendanceTableItem.PaidLeaveHours = 0; } } else { if (attendanceTableItem.AvailableHours >= totalOvertimeHours) { attendanceTableItem.LeaveHours = attendanceTableItem.PaidLeaveHours = attendanceTableItem.AvailableHours - totalOvertimeHours; } else { attendanceTableItem.LeaveHours = attendanceTableItem.PaidLeaveHours = 0; } } attendanceTableItem.WorkPaidHours = 0; } #endregion } unitOfWork.AddObject(typeof(Att_AttendanceTableItem), attendanceTableItem); } #endregion #region Tính toán lại phép năm và phép ốm trong kỳ var annualLeaveDetail = listAnnualDetailByProfile.Where(d => d.Type == AnnualLeaveDetailType.E_ANNUAL_LEAVE.ToString()).FirstOrDefault(); var sickLeaveDetail = listAnnualDetailByProfile.Where(d => d.Type == AnnualLeaveDetailType.E_SICK_LEAVE.ToString()).FirstOrDefault(); var preAttendanceTable = listPreAttendanceTableByProfile.Where(d => d.ProfileID == profileID).FirstOrDefault(); attendanceTable.AnlDayTaken = anlDayTaken; attendanceTable.SickDayTaken = sickDayTaken; if (preAttendanceTable != null) { if (annualLeaveDetail != null && annualLeaveDetail.MonthYear.HasValue && annualLeaveDetail.MonthYear.Value.Date == monthYear.Date) { attendanceTable.AnlDayAdjacent = anlDayTaken; } else { //VinhTran - 0030581 - AnlDayAdjacent là số ngày đã nghỉ tính tới đầu tháng đang tính công attendanceTable.AnlDayAdjacent = preAttendanceTable.AnlDayAdjacent.GetDouble() + anlDayTaken; } if (sickLeaveDetail != null && sickLeaveDetail.MonthYear.HasValue && sickLeaveDetail.MonthYear.Value.Date == monthYear.Date) { attendanceTable.SickDayAdjacent = sickDayTaken; } else { //VinhTran - 0030581 - AnlDayAdjacent là số ngày đã nghỉ tính tới đầu tháng đang tính công attendanceTable.SickDayAdjacent = preAttendanceTable.SickDayAdjacent.GetDouble() + sickDayTaken; } } else { attendanceTable.AnlDayAdjacent = anlDayTaken; attendanceTable.SickDayAdjacent = sickDayTaken; } if (annualLeaveDetail != null && annualLeaveDetail.MonthYear.HasValue) { attendanceTable.TotalAnlDayAvailable = annualLeaveDetail.Available.Get_Double(); attendanceTable.AnlDayAvailable = attendanceTable.TotalAnlDayAvailable.GetDouble() - attendanceTable.AnlDayAdjacent.GetDouble(); } else if (preAttendanceTable != null) { attendanceTable.AnlDayAvailable = preAttendanceTable.AnlDayAvailable - attendanceTable.AnlDayTaken; } if (sickLeaveDetail != null && sickLeaveDetail.MonthYear.HasValue) { attendanceTable.TotalSickDayAvailable = sickLeaveDetail.Available.Get_Double(); attendanceTable.SickDayAvailable = attendanceTable.TotalSickDayAvailable.GetDouble() - attendanceTable.SickDayAdjacent.GetDouble(); } else if (preAttendanceTable != null) { //tháng 7: còn 13, liền kề 1, hiện tại 1 => tháng 8: liền kề 2, hiện tại 1, còn 12 attendanceTable.SickDayAvailable = preAttendanceTable.SickDayAvailable.GetDouble() - attendanceTable.SickDayTaken.GetDouble(); } #endregion #region Tính toán lại phép bù trong kỳ foreach (var leaveDayByProfile in listLeaveDayByProfile) { if (leaveDayByProfile != null) { var dateStart = leaveDayByProfile.DateStart; var dateEnd = leaveDayByProfile.DateEnd; if (leaveDayByProfile.DateOvertimeOff.HasValue) { dateStart = leaveDayByProfile.DateOvertimeOff.Value; dateEnd = leaveDayByProfile.DateOvertimeOff.Value; } for (DateTime date = dateStart.Date; date <= dateEnd; date = date.AddDays(1)) { double leaveHours = 0D; double leaveDays = 0D; double standardWorkHours = 0; var leaveDayType = listLeaveDayType.Where(d => d.ID == leaveDayByProfile.LeaveDayTypeID).FirstOrDefault(); var attendanceTableItem = attendanceTable.Att_AttendanceTableItem.Where(d => d.WorkDate == date).FirstOrDefault(); if (attendanceTableItem!=null) standardWorkHours = attendanceTableItem.udStandardWorkHours; if (leaveDayByProfile.DurationType == LeaveDayDurationType.E_FULLSHIFT.ToString()) { //Lấy giờ nghỉ của ngày đang xét dựa vào ca làm việc leaveHours = standardWorkHours; leaveDays = 1;//Nghỉ full shift } else { leaveHours = leaveDayByProfile.LeaveHours.GetDouble(); leaveDays = leaveDayByProfile.LeaveDays.GetDouble(); } leaveHours = leaveHours > standardWorkHours ? standardWorkHours : leaveHours; leaveDays = leaveDays > 1 ? 1 : leaveDays;//Tối đa là nghỉ 1 ngày if (leaveDayType != null && leaveDays > 0) { if (leaveDayType.Code == LeavedayTypeCode.CPS.ToString() || leaveDayType.Code == LeavedayTypeCode.OCPS.ToString()) { if ((leaveDayByProfile.DateOvertimeOff.HasValue && leaveDayByProfile.DateStart > attendanceTo) || date > attendanceTo) { //Nghỉ CO tháng sau => coEndPeriod coEndPeriod += leaveDays; } else if (date >= attendanceFrom && date <= attendanceTo) { //Nghỉ CO trong tháng => coBeginPeriod coBeginPeriod += leaveDays; } } } } } } attendanceTable.COBeginPeriod = coBeginPeriod; attendanceTable.COEndPeriod = coEndPeriod; #endregion #region Cập nhật Att_AttendanceTable sau khi tính xong #region Sum type HDTJob var _type4 = attendanceTable.Att_AttendanceTableItem.Where(d => d.HDTJobType == EnumDropDown.HDTJobType.E_TYPE4.ToString()); var _type5 = attendanceTable.Att_AttendanceTableItem.Where(d => d.HDTJobType == EnumDropDown.HDTJobType.E_TYPE5.ToString()); attendanceTable.HDTJobType1 = EnumDropDown.HDTJobType.E_TYPE4.ToString(); attendanceTable.HDTJobType2 = EnumDropDown.HDTJobType.E_TYPE5.ToString(); attendanceTable.HDTJobDayCount1 = _type4 != null ? _type4.Count() : 0; attendanceTable.HDTJobDayCount2 = _type5 != null ? _type5.Count() : 0; #endregion attendanceTable.RealWorkDayCount = attendanceTable.Att_AttendanceTableItem.Where(d => d.udWorkHours > 0).Sum(d => (d.WorkPaidHours + d.LateEarlyMinutes / 60.0) / (d.udStandardWorkHours * d.udStandardWorkHours / d.udWorkHours)); attendanceTable.NightShiftHours = attendanceTable.Att_AttendanceTableItem.Where(d => d.DutyCode == DutyCode.E_ON.ToString()).Sum(d => d.NightShiftHours); attendanceTable.OTNightShiftHours = attendanceTable.Att_AttendanceTableItem.Where(d => d.DutyCode == DutyCode.E_ON.ToString()).Sum(d => d.OTNightShiftHours.GetDouble()); attendanceTable.LateEarlyDeductionHours += attendanceTable.Att_AttendanceTableItem.Sum(d => d.LateEarlyMinutes) / 60.0; attendanceTable.UnPaidLeave = attendanceTable.Att_AttendanceTableItem.Sum(d => d.UnpaidLeaveDays.GetDouble()); attendanceTable.PaidLeaveDays = attendanceTable.Att_AttendanceTableItem.Sum(d => d.PaidLeaveDays.GetDouble()); //Tính nghỉ bù attendanceTable.COBeginPeriod = coBeginPeriod; attendanceTable.COEndPeriod = coEndPeriod; //Tính số ngày công chuẩn int weekendDaysCount = attendanceTable.Att_AttendanceTableItem.Where(d => d.IsHoliday).Count(); attendanceTable.StdWorkDayCount = GetStandardWorkDays(gradeCfg, listHoliday, listShift, listMonthShiftID, monthYear, attendanceFrom, attendanceTo); attendanceTable.StdWorkDayOTCount = GetOTStandardWorkDays(weekendDaysCount, gradeCfg, listHoliday, listShift, listMonthShiftID, monthYear, attendanceFrom, attendanceTo); //Tổng số ngày được trả lương của nhân viên đang xét => ngày thực tế attendanceTable.PaidWorkDayCount = attendanceTable.RealWorkDayCount; if (gradeCfg.EDType == PayrollComputeMethod.E_SUBTRACT.ToString()) { //Số ngày công thực tế + số ngày nghỉ hưởng lương Double workAndPaidLeaveDays = attendanceTable.RealWorkDayCount; double unHiredDays = 0; double terminatedDays = 0; if (attendanceTable.Att_AttendanceTableItem.Count() > 0) { workAndPaidLeaveDays += attendanceTable.Att_AttendanceTableItem.Sum(d => d.PaidLeaveHours / d.udStandardWorkHours); } //Nếu nhân viên không vào làm hoặc nghỉ việc trong tháng và số ngày công thực tế + số ngày nghỉ hưởng lương > số ngày công tối thiểu if (((profile.DateHire.HasValue && profile.DateHire.Value.Date < attendanceFrom) || workAndPaidLeaveDays > attendanceTable.StdWorkDayCount) && ((!profile.DateQuit.HasValue || profile.DateQuit.Value.Date > attendanceTo) || workAndPaidLeaveDays > attendanceTable.StdWorkDayCount) && workAndPaidLeaveDays >= gradeCfg.MinWorkDay.Value) { //Tổng số ngày nghỉ loại 1 và loại 2 khi có trường hợp 1 ngày mà nghỉ 2 loại Double totalLeaveDays = attendanceTable.Att_AttendanceTableItem.Sum(d => (d.LeaveHours + d.ExtraLeaveHours) / d.udStandardWorkHours); if (isMidCutOffDay) { totalLeaveDays = attendanceTable.Att_AttendanceTableItem.Where(d => d.WorkDate <= midCutOffDate) .Sum(d => d.LeaveHours + d.ExtraLeaveHours / d.udStandardWorkHours);//Số ngày nghỉ không ứng công } //Cộng thêm số ngày chưa làm việc - trường hợp làm nửa ca totalLeaveDays += attendanceTable.Att_AttendanceTableItem.Sum(d => d.udNotHiredHours / d.udStandardWorkHours); if (profile.DateQuit.HasValue) { //Cộng thêm số ngày đã nghỉ việc totalLeaveDays += attendanceTable.Att_AttendanceTableItem.Sum(d => d.udTerminatedHours / d.udStandardWorkHours); } //Tính số ngày công tính lương = số ngày công chuẩn - tổng số ngày nghỉ attendanceTable.PaidWorkDayCount = attendanceTable.StdWorkDayCount - totalLeaveDays; if (attendanceTable.PaidWorkDayCount < 0) { attendanceTable.PaidWorkDayCount = 0; } } else if ((gradeCfg.IsApplySubSalaryNewQuitEmp.GetBoolean() && workAndPaidLeaveDays >= gradeCfg.MinWorkDayNewQuitEmp.GetDouble()) && ((profile.DateHire.HasValue && profile.DateHire.Value.Date >= attendanceFrom) || (profile.DateQuit.HasValue && profile.DateQuit.Value.Date <= attendanceTo))) { Double totalLeaveDays = attendanceTable.Att_AttendanceTableItem.Sum(it => (it.LeaveHours + it.ExtraLeaveHours) / it.udStandardWorkHours); Double DayNonShiftInUnEmployeeTime = 0;//Thời gian của nhân viên không có lịch làm việc trước khi vào công ty hoặc là sau khi ra khỏi công ty trong tháng if (profile.DateHire.HasValue && profile.DateHire.Value > attendanceFrom) { unHiredDays = profile.DateHire.Value.Subtract(attendanceFrom).Days; for (DateTime DateCheck = attendanceFrom; DateCheck < profile.DateHire.Value; DateCheck = DateCheck.AddDays(1)) { if (listMonthShiftID == null || !listMonthShiftID.Any(d => d.Key.Date == DateCheck.Date && d.Value != null)) { DayNonShiftInUnEmployeeTime++; } } } if (profile.DateQuit.HasValue && profile.DateQuit.Value < attendanceTo) { terminatedDays = attendanceTo.Subtract(profile.DateQuit.Value).Days + 1; for (DateTime DateCheck = profile.DateQuit.Value; DateCheck < attendanceTo; DateCheck = DateCheck.AddDays(1)) { if (listMonthShiftID == null || !listMonthShiftID.Any(d => d.Key.Date == DateCheck.Date && d.Value != null)) { DayNonShiftInUnEmployeeTime++; } } } Double paidWorkDay = attendanceTable.StdWorkDayCount - totalLeaveDays; paidWorkDay = paidWorkDay - (unHiredDays + terminatedDays - DayNonShiftInUnEmployeeTime) * gradeCfg.RateUneven.GetDouble(); attendanceTable.PaidWorkDayCount = paidWorkDay; } } #region Tính tổng thời gian theo từng loại ngày nghỉ và loại làm thêm TotalGroupByLeaveDayType(attendanceTable, attendanceTo); TotalGroupByOvertimeType(attendanceTable, attendanceTo); #endregion #region Tính tổng thời gian và làm tròn attendanceTable.TotalPaidWorkDayCount = attendanceTable.PaidWorkDayCount; attendanceTable.TotalRealWorkDayCount = attendanceTable.RealWorkDayCount; //Cấu hình làm tròng trễ sớm theo tháng if (gradeCfg != null && gradeCfg.IsLateEarlyFirstLastShiftRound == true) { attendanceTable.LateEarlyDeductionHours = Att_AttendanceLib.RoundLateEarlyMinutes(listLateEarlyRule.Where(d => d.GradeCfgID == gradeCfg.ID).ToList(), Convert.ToInt32(attendanceTable.LateEarlyDeductionHours * 60)) / 60; } #endregion #endregion return attendanceTable != null ? 1 : 0; }
private Att_Workday CreateWorkday(IUnitOfWork unitOfWork, DateTime date, Hre_ProfileEntity profile, WorkdayConfig inOutConfig, List<Cat_DayOffEntity> listHoliday, List<Att_Workday> listWorkday, Att_Workday workday, bool isFromShift2, Guid? shiftID, Cat_Shift shiftInfo, List<Cat_Shift> listShift, Dictionary<DateTime, List<Guid?>> listMonthShifts, out Guid? actualShiftID, List<Att_TAMScanLogEntity> listTamScanLogByCardCode, List<Att_TAMScanLogEntity> listTamScanLogByCardCodeUnchecked) { #region Trường hợp có lịch làm việc #region Kiểm tra dữ liệu quẹt theo theo ngày (ca) var listTamScanLogByShift = GetTamScanLogByShift(date, listShift, shiftID, listMonthShifts, listTamScanLogByCardCodeUnchecked.ToArray()); //Checked = true để không tự detect shift cho nó listTamScanLogByShift.ForEach(d => d.Checked = true); actualShiftID = null; if (workday == null) { workday = new Att_Workday(); unitOfWork.AddObject(typeof(Att_Workday), workday); } #endregion if (inOutConfig == null || inOutConfig.TypeLoadData.IsNullOrEmpty() || inOutConfig.TypeLoadData == TypeLoadData.E_DEFAULT.ToString() || inOutConfig.TypeLoadData == TypeLoadData.E_MAXMIN.ToString()) { if (listTamScanLogByShift.Count() >= 2) { #region Trường hợp có ít nhất 2 dòng quẹt thẻ thuộc ca đang xét var inTime = listTamScanLogByShift.Select(d => d.TimeLog).FirstOrDefault(); var outTime = listTamScanLogByShift.Select(d => d.TimeLog).LastOrDefault(); //Khi phát hiện 2 quẹt thẻ hợp lệ với ca đăng ký thì nhân luôn - không detect shift workday.FirstInTime = !workday.FirstInTime.HasValue || workday.FirstInTime > inTime ? inTime : workday.FirstInTime; workday.LastOutTime = !workday.LastOutTime.HasValue || workday.LastOutTime < outTime ? outTime : workday.LastOutTime; if (isFromShift2) { workday.InTime2 = inTime; workday.OutTime2 = outTime; } else { workday.InTime1 = inTime; workday.OutTime1 = outTime; } if (inOutConfig.DetectWrongShift == Boolean.TrueString) { //Trường hợp wrong-shift và detected-shift thì phải check lại actualShiftID actualShiftID = GetDetectedShiftID(inTime, outTime, listShift, null); if (actualShiftID != Guid.Empty && actualShiftID != shiftID) { var listTest = GetTamScanLogByShift(inTime.Value.Date, listShift, actualShiftID, listMonthShifts, listTamScanLogByShift.ToArray()); if (listTest != null && listTest.Count() >= 2 && listTest.Any(d => d.TimeLog.HasValue && d.TimeLog.Value.Date == date)) { workday.Type = WorkdayType.E_WRONG_SHIFT.ToString(); } else { actualShiftID = null; } } } #endregion } else if (listTamScanLogByShift.Count() == 1) { #region Trường hợp có 1 dòng quẹt thẻ thuộc ca đang xét if (listTamScanLogByShift.Any(d => d.SrcType == TAMScanType.E_OUT.ToString())) { //Dữ liệu quẹt thẻ được lấy từ máy chấm công ra - có option máy vào máy ra workday.Type = WorkdayType.E_MISS_IN.ToString(); if (isFromShift2) { workday.OutTime2 = listTamScanLogByShift.Select(d => d.TimeLog).LastOrDefault(); } else { workday.OutTime1 = listTamScanLogByShift.Select(d => d.TimeLog).LastOrDefault(); } } else { //So sanh độ lệch với thời gian của ca -> gần bên nào thì tính theo bên đó var timeLog = listTamScanLogByShift.Select(d => d.TimeLog).FirstOrDefault(); var shiftInTime = date.Add(shiftInfo.InTime.TimeOfDay); if (timeLog.Value <= shiftInTime || timeLog.Value.Subtract(shiftInTime) < shiftInTime.AddHours(shiftInfo.CoOut).Subtract(timeLog.Value)) { //1 dòng quẹt thẻ rơi vào nửa đầu của ca workday.Type = WorkdayType.E_MISS_OUT.ToString(); if (isFromShift2) { workday.InTime2 = timeLog; } else { workday.InTime1 = timeLog; } } if (inOutConfig.DetectWrongShift == Boolean.TrueString) { #region Tìm quẹt thẻ phía trước //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 <= 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 < timeLog).FirstOrDefault(); var currentShiftDuration = shiftInfo.MinIn + shiftInfo.CoOut + shiftInfo.MaxOut; 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ẻ tiếp theo mà phù hợp duration thì ghép sai ca if (preTamScanLog != null && preTamScanLog.TimeLog.HasValue && timeLog.Value .Subtract(preTamScanLog.TimeLog.Value).TotalHours <= currentShiftDuration) { //Trường hợp wrong-shift và detected-shift thì phải check lại actualShiftID actualShiftID = GetDetectedShiftID(preTamScanLog.TimeLog, timeLog, listShift, null); var listTest = GetTamScanLogByShift(preTamScanLog.TimeLog.Value.Date, listShift, actualShiftID, listMonthShifts, new Att_TAMScanLogEntity[] { preTamScanLog, listTamScanLogByShift.FirstOrDefault() }); if (preTamScanLog.TimeLog.Value.Date == date.Date && listTest != null && listTest.Count() >= 2 && !listTest.Any(d => d.TimeLog.HasValue && d.TimeLog.Value.Date < date.Date)) { if (isFromShift2) { workday.InTime2 = preTamScanLog.TimeLog; workday.OutTime2 = timeLog; } else { workday.InTime1 = preTamScanLog.TimeLog; workday.OutTime1 = timeLog; } workday.FirstInTime = !workday.FirstInTime.HasValue || workday.FirstInTime > preTamScanLog.TimeLog ? preTamScanLog.TimeLog : workday.FirstInTime; workday.LastOutTime = !workday.LastOutTime.HasValue || workday.LastOutTime < timeLog ? timeLog : workday.LastOutTime; workday.Type = WorkdayType.E_WRONG_SHIFT.ToString(); preTamScanLog.Checked = true; } else { actualShiftID = null; } } } #endregion #region Tìm quẹt thẻ phía sau if (workday.Type == WorkdayType.E_MISS_OUT.ToString() || string.IsNullOrWhiteSpace(workday.Type)) { if (string.IsNullOrWhiteSpace(workday.Type)) { //1 dòng quẹt thẻ rơi vào nửa sau của ca workday.Type = WorkdayType.E_MISS_IN.ToString(); if (isFromShift2) { workday.OutTime2 = timeLog; } else { workday.OutTime1 = timeLog; } } //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 >= 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) { var nextTamScanLog = listNextTamScanLog.Where(d => d.TimeLog > timeLog).FirstOrDefault(); var currentShiftDuration = shiftInfo.MinIn + shiftInfo.CoOut + shiftInfo.MaxOut; //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(timeLog.Value).TotalHours <= currentShiftDuration) { //Trường hợp wrong-shift và detected-shift thì phải check lại actualShiftID actualShiftID = GetDetectedShiftID(timeLog, nextTamScanLog.TimeLog, listShift, null); var listTest = GetTamScanLogByShift(timeLog.Value.Date, listShift, actualShiftID, listMonthShifts, new Att_TAMScanLogEntity[] { listTamScanLogByShift.FirstOrDefault(), nextTamScanLog }); if (timeLog.Value.Date == date.Date && listTest != null && listTest.Count() >= 2 && !listTest.Any(d => d.TimeLog.HasValue && d.TimeLog.Value.Date < date.Date)) { if (isFromShift2) { workday.InTime1 = timeLog; workday.OutTime1 = nextTamScanLog.TimeLog; } else { workday.InTime1 = timeLog; workday.OutTime1 = nextTamScanLog.TimeLog; } workday.FirstInTime = !workday.FirstInTime.HasValue || workday.FirstInTime > timeLog ? timeLog : workday.FirstInTime; workday.LastOutTime = !workday.LastOutTime.HasValue || workday.LastOutTime < nextTamScanLog.TimeLog ? nextTamScanLog.TimeLog : workday.LastOutTime; workday.Type = WorkdayType.E_WRONG_SHIFT.ToString(); nextTamScanLog.Checked = true; } else { actualShiftID = null; } } } } #endregion } else { if (IsInTime(date, timeLog, shiftInfo)) { if (isFromShift2) { workday.InTime2 = timeLog; } else { workday.InTime1 = timeLog; } workday.Type = WorkdayType.E_MISS_OUT.ToString(); } else { if (isFromShift2) { workday.OutTime2 = timeLog; } else { workday.OutTime1 = timeLog; } workday.Type = WorkdayType.E_MISS_IN.ToString(); } } } #endregion } else { #region Trường hợp không có dòng quẹt thẻ thuộc ca đang xét var listTamScanLogByDate = listTamScanLogByCardCodeUnchecked.Where(d => d.TimeLog.Value.Date == date).ToList(); var shiftInTime = date.Add(shiftInfo.InTime.TimeOfDay); 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 = shiftInfo.MinIn + shiftInfo.CoOut + shiftInfo.MaxOut; 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) { //Trường hợp wrong-shift và detected-shift thì phải check lại actualShiftID actualShiftID = GetDetectedShiftID(preTamScanLog.TimeLog, item.TimeLog, listShift, null); var listTest = GetTamScanLogByShift(preTamScanLog.TimeLog.Value.Date, listShift, actualShiftID, listMonthShifts, new Att_TAMScanLogEntity[] { preTamScanLog, item }); if (preTamScanLog.TimeLog.Value.Date == date.Date && listTest != null && listTest.Count() >= 2 && listTest.Any(d => d.TimeLog.HasValue && d.TimeLog.Value.Date < date.Date)) { if (isFromShift2) { workday.InTime2 = preTamScanLog.TimeLog; workday.OutTime2 = item.TimeLog; } else { workday.InTime1 = preTamScanLog.TimeLog; workday.OutTime1 = item.TimeLog; } workday.FirstInTime = !workday.FirstInTime.HasValue || workday.FirstInTime > preTamScanLog.TimeLog ? preTamScanLog.TimeLog : workday.FirstInTime; workday.LastOutTime = !workday.LastOutTime.HasValue || workday.LastOutTime < item.TimeLog ? item.TimeLog : workday.LastOutTime; workday.Type = WorkdayType.E_WRONG_SHIFT.ToString(); preTamScanLog.Checked = true; isWrongShiftDetected = true; break;//chỉ hỗ trợ 1 ca } else { actualShiftID = null; } } 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) { var nextTamScanLog = listNextTamScanLog.Where(d => d.TimeLog > item.TimeLog).FirstOrDefault(); var currentShiftDuration = shiftInfo.MinIn + shiftInfo.CoOut + shiftInfo.MaxOut; //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) { //Trường hợp wrong-shift và detected-shift thì phải check lại actualShiftID actualShiftID = GetDetectedShiftID(item.TimeLog, nextTamScanLog.TimeLog, listShift, null); var listTest = GetTamScanLogByShift(item.TimeLog.Value.Date, listShift, actualShiftID, listMonthShifts, new Att_TAMScanLogEntity[] { item, nextTamScanLog }); if (item.TimeLog.Value.Date == date.Date && listTest != null && listTest.Count() >= 2 && listTest.Any(d => d.TimeLog.HasValue && d.TimeLog.Value.Date < date.Date)) { if (isFromShift2) { workday.InTime2 = item.TimeLog; workday.OutTime2 = nextTamScanLog.TimeLog; } else { workday.InTime1 = item.TimeLog; workday.OutTime1 = nextTamScanLog.TimeLog; } workday.FirstInTime = !workday.FirstInTime.HasValue || workday.FirstInTime > item.TimeLog ? item.TimeLog : workday.FirstInTime; workday.LastOutTime = !workday.LastOutTime.HasValue || workday.LastOutTime < nextTamScanLog.TimeLog ? nextTamScanLog.TimeLog : workday.LastOutTime; workday.Type = WorkdayType.E_WRONG_SHIFT.ToString(); nextTamScanLog.Checked = true; isWrongShiftDetected = true; break;//chỉ hỗ trợ 1 ca } else { actualShiftID = null; } } else { listTimeLog.Add(item); } } else { if (!listTamScanLogByNextShift.Any(d => d.TimeLog == item.TimeLog)) { listTimeLog.Add(item); } listTimeLog = listTimeLog.Where(d => !listTamScanLogByNextShift.Contains(d)).ToList(); } } } if (!isWrongShiftDetected) { var listRemove = new List<Att_TAMScanLogEntity>(); if (listTimeLog.Count() >= 2) { var currentShiftDuration = shiftInfo.MinIn + shiftInfo.CoOut + shiftInfo.MaxOut; var timeLog1 = listTimeLog.OrderBy(d => d.TimeLog).FirstOrDefault(); var timeLog2 = listTimeLog.OrderBy(d => d.TimeLog).LastOrDefault(); if (isFromShift2) { workday.InTime2 = timeLog1.TimeLog; workday.OutTime2 = timeLog2.TimeLog; } else { workday.InTime1 = timeLog1.TimeLog; workday.OutTime1 = timeLog2.TimeLog; } workday.FirstInTime = !workday.FirstInTime.HasValue || workday.FirstInTime > timeLog1.TimeLog ? timeLog1.TimeLog : workday.FirstInTime; workday.LastOutTime = !workday.LastOutTime.HasValue || workday.LastOutTime < timeLog2.TimeLog ? timeLog2.TimeLog : workday.LastOutTime; isWrongShiftDetected = true; if (timeLog2.TimeLog.HasValue && timeLog1.TimeLog.HasValue && timeLog2.TimeLog.Value .Subtract(timeLog1.TimeLog.Value).TotalHours <= currentShiftDuration) { //Trường hợp wrong-shift và detected-shift thì phải check lại actualShiftID actualShiftID = GetDetectedShiftID(timeLog1.TimeLog, timeLog2.TimeLog, listShift, null); var listTest = GetTamScanLogByShift(timeLog1.TimeLog.Value.Date, listShift, actualShiftID, listMonthShifts, new Att_TAMScanLogEntity[] { timeLog1, timeLog2 }); if (listTest != null && listTest.Count() >= 2 && listTest.Any(d => d.TimeLog.HasValue && d.TimeLog.Value.Date < date.Date)) { workday.Type = WorkdayType.E_WRONG_SHIFT.ToString(); } else { workday.Type = WorkdayType.E_LONGIN_SHIFT.ToString(); } } else { foreach (var item in listTimeLog.Where(d => d.TimeLog > timeLog1.TimeLog).OrderByDescending(d => d.TimeLog)) { if (isFromShift2) { workday.OutTime2 = item.TimeLog; } else { workday.OutTime1 = item.TimeLog; } workday.LastOutTime = !workday.LastOutTime.HasValue || workday.LastOutTime < item.TimeLog ? item.TimeLog : workday.LastOutTime; if (item.TimeLog > timeLog1.TimeLog && item.TimeLog.HasValue && timeLog1.TimeLog.HasValue && item.TimeLog.Value.Subtract(timeLog1.TimeLog.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)) { var inTimeValue = listTimeLog.Select(d => d.TimeLog).FirstOrDefault(); if (isFromShift2) { workday.InTime2 = inTimeValue; } else { workday.InTime1 = inTimeValue; } workday.FirstInTime = !workday.FirstInTime.HasValue || workday.FirstInTime > inTimeValue ? inTimeValue : workday.FirstInTime; workday.Type = WorkdayType.E_MISS_OUT.ToString();//miss-in hay miss-out cũng như nhau } else { var outTimeValue = listTimeLog.Select(d => d.TimeLog).FirstOrDefault(); if (isFromShift2) { workday.OutTime2 = outTimeValue; } else { workday.OutTime1 = outTimeValue; } workday.LastOutTime = !workday.LastOutTime.HasValue || workday.LastOutTime < outTimeValue ? outTimeValue : workday.LastOutTime; 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 (!isWrongShiftDetected) { if (listHoliday.Any(d => d.DateOff.Date == date.Date)) { //Có ca mà không inout thì xem có phải holiday workday.Type = WorkdayType.E_HOLIDAY.ToString(); } else { workday.Type = WorkdayType.E_MISS_IN_OUT.ToString(); } } #endregion } } else if (inOutConfig.TypeLoadData == TypeLoadData.E_TYPEINOUT.ToString()) { //Xử lý cho các loại khác } return workday; #endregion }