/// <summary> /// [Hieu.Van] 05/06/2014 /// Phân Tích Nghỉ Phép Và Trễ Sớm /// </summary> /// <returns></returns> public List<Att_WorkdayEntity> ComputeLeaveLateEarly(DateTime? DateS, DateTime? DateE, List<Hre_ProfileEntity> lstProfileIDsFull, string udType) { using (var context = new VnrHrmDataContext()) { var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); List<Att_WorkdayEntity> lstEntity = new List<Att_WorkdayEntity>(); List<Guid> lstProfileIDs = lstProfileIDsFull.Select(s => s.ID).ToList(); if (DateS == null || DateE == null) return lstEntity; DateTime DateStart = DateS.Value; DateTime DateEnd = DateE.Value.Date.AddDays(1).AddMilliseconds(-1); string E_LATE_EARLY = WorkdayType.E_LATE_EARLY.ToString(); string E_MISS_IN = WorkdayType.E_MISS_IN.ToString(); string E_MISS_OUT = WorkdayType.E_MISS_OUT.ToString(); string E_MISS_IN_OUT = WorkdayType.E_MISS_IN_OUT.ToString(); var repoWorkday = new CustomBaseRepository<Att_Workday>(unitOfWork); var lstWorkday = repoWorkday .FindBy(m => m.IsDelete == null && ((m.Type == E_LATE_EARLY && m.LateEarlyDuration != null && m.LateEarlyDuration > 0) || m.InTime1 == null || m.OutTime1 == null) && m.WorkDate >= DateStart && m.WorkDate < DateEnd && lstProfileIDs.Contains(m.ProfileID)) .OrderBy(s => s.WorkDate) .ToList().Translate<Att_WorkdayEntity>(); SetStatusLeaveOnWorkday(lstWorkday); List<string> lstTypeData = null; if (udType != null) { lstTypeData = udType.Split(',').ToList(); } if (lstTypeData != null) { if (!lstTypeData.Any(m => m == ComputeLeavedayType.E_DATA_LEAVE.ToString())) { lstWorkday = lstWorkday.Where(m => (m.udLeavedayCode1 == null || m.udLeavedayCode1 == string.Empty) && (m.udLeavedayCode2 == null || m.udLeavedayCode2 == string.Empty)).ToList(); } if (!lstTypeData.Any(m => m == ComputeLeavedayType.E_DATA_NON_LEAVE.ToString())) { lstWorkday = lstWorkday.Where(m => m.udLeavedayCode1 != null || m.udLeavedayCode2 != null).ToList(); } } var repoShift = new Cat_ShiftRepository(unitOfWork); var lstShift = repoShift.FindBy(s => s.IsDelete == null).ToList(); Att_WorkdayEntity entity = null; foreach (var workDay in lstWorkday) { entity = new Att_WorkdayEntity(); if (workDay.ShiftID != null) { var shiftApprove = lstShift.FirstOrDefault(s => s.ID == workDay.ShiftID); entity.ShiftApproveName = shiftApprove != null ? shiftApprove.ShiftName : string.Empty; entity.ShiftName = shiftApprove != null ? shiftApprove.ShiftName : string.Empty; entity.ShiftCode = shiftApprove != null ? shiftApprove.Code : string.Empty; } if (workDay.ProfileID != null) { var profile = lstProfileIDsFull.FirstOrDefault(s => s.ID == workDay.ProfileID); entity.ProfileName = profile != null ? profile.ProfileName : string.Empty; entity.CodeEmp = profile != null ? profile.CodeEmp : string.Empty; } entity.ID = workDay.ID; entity.TotalRow = lstWorkday.Count; entity.WorkDate = workDay.WorkDate; entity.EarlyDuration1 = workDay.EarlyDuration1 ?? 0; entity.EarlyDuration2 = workDay.EarlyDuration2 ?? 0; entity.EarlyDuration3 = workDay.EarlyDuration3 ?? 0; entity.EarlyDuration4 = workDay.EarlyDuration4 ?? 0; entity.FirstInTime = workDay.FirstInTime; entity.InTime1 = workDay.InTime1; entity.InTime2 = workDay.InTime2; entity.InTime3 = workDay.InTime3; entity.InTime4 = workDay.InTime4; entity.LastOutTime = workDay.LastOutTime; entity.LateDuration1 = workDay.LateDuration1 ?? 0; entity.LateDuration2 = workDay.LateDuration2 ?? 0; entity.LateDuration3 = workDay.LateDuration3 ?? 0; entity.LateDuration4 = workDay.LateDuration4 ?? 0; entity.LateEarlyDuration = workDay.LateEarlyDuration ?? 0; entity.OutTime1 = workDay.OutTime1; entity.OutTime2 = workDay.OutTime2; entity.OutTime3 = workDay.OutTime3; entity.OutTime4 = workDay.OutTime4; entity.ProfileID = workDay.ProfileID; entity.ShiftActual = workDay.ShiftActual; entity.ShiftApprove = workDay.ShiftApprove; entity.ShiftID = workDay.ShiftID; // entity.ShiftName = workDay.ShiftName; entity.SrcType = workDay.SrcType; entity.Status = workDay.Status; entity.Type = workDay.Type; entity.UserUpdate = workDay.UserUpdate; entity.DateUpdate = workDay.DateUpdate; entity.WorkDuration = workDay.WorkDuration; entity.udLeavedayCode1 = workDay.udLeavedayCode1; entity.udLeavedayCode2 = workDay.udLeavedayCode2; entity.udLeavedayStatus1 = workDay.udLeavedayStatus1; entity.udLeavedayStatus2 = workDay.udLeavedayStatus2; lstEntity.Add(entity); } return lstEntity; } }
public string ValidateSaveWorkday(Att_WorkdayEntity WorkdaySave, Att_WorkdayEntity WorkdayOld, string UserLogin) { using (var context = new VnrHrmDataContext()) { var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var repoHre_CardHistory = new CustomBaseRepository<Hre_CardHistory>(unitOfWork); var repoHre_Profile = new CustomBaseRepository<Hre_Profile>(unitOfWork); var repoAtt_TAMScanLog = new CustomBaseRepository<Att_TAMScanLog>(unitOfWork); var repoCat_Shift = new CustomBaseRepository<Cat_Shift>(unitOfWork); var repoCat_TAMScanReasonMiss = new CustomBaseRepository<Cat_TAMScanReasonMiss>(unitOfWork); var repoAtt_Workday = new CustomBaseRepository<Att_Workday>(unitOfWork); string message = ""; List<Att_TAMScanLog> lstTamScanLog = new List<Att_TAMScanLog>(); string Notes = string.Empty; #region cập nhật giờ intime va outtime //1. xử lí việc insert vào TamScanlog //2. xử lí việc chuyển in và out theo logic in phải nhỏ hơn out //3. xử lý việc Validate inout không thẻ lơn hơn workday 1 ngày DateTime? Intime1 = null; if (WorkdaySave.InTime1 != null) { Intime1 = WorkdaySave.InTime1.Value.Date; } if (!string.IsNullOrEmpty(WorkdaySave.TempTimeIn) && Intime1.HasValue) { DateTime TimeScan = DateTime.ParseExact(WorkdaySave.TempTimeIn, "HH:mm:ss", null); Intime1 = Intime1.Value.AddHours(TimeScan.Hour).AddMinutes(TimeScan.Minute).AddSeconds(TimeScan.Second); } DateTime? OutTime1 = null; if (WorkdaySave.OutTime1 != null) { OutTime1 = WorkdaySave.InTime1.Value.Date; } if (!string.IsNullOrEmpty(WorkdaySave.TempTimeOut) && OutTime1.HasValue) { DateTime TimeScan = DateTime.ParseExact(WorkdaySave.TempTimeOut, "HH:mm:ss", null); OutTime1 = OutTime1.Value.AddHours(TimeScan.Hour).AddMinutes(TimeScan.Minute).AddSeconds(TimeScan.Second); } Guid? MissInOutReason = WorkdaySave.MissInOutReason; //Check validate ko cho phép Intime hoặc out time if (((WorkdayOld.InTime1 == null && Intime1 != null) || (WorkdayOld.OutTime1 == null && OutTime1 != null)) && (MissInOutReason == null || MissInOutReason == Guid.Empty)) { message = ConstantMessages.plsInputTAMScanReasonMissBeforeChangeInOut.TranslateString(); return message; } //Chuyển dổi vị trí in và out if (Intime1 != null && OutTime1 != null && (Intime1 > OutTime1)) { DateTime DateChange = Intime1.Value; Intime1 = OutTime1; OutTime1 = DateChange; // DataBeChangedBecauseInTimeIsAfterOutTime } bool isChangeTamScan = false; if (Intime1 != null) { if (WorkdayOld.InTime1 != Intime1) { Notes += "Intime1:" + string.Format("{0:dd/MM/yyyy hh:mm:ss}", Intime1); } if (WorkdayOld.InTime1 == null) //Thiếu Intime. Thì cập nhật vào lstTamscanLog { Att_TAMScanLog Tams = new Att_TAMScanLog(); Tams.ID = Guid.NewGuid(); Tams.TimeLog = Intime1; Tams.Status = TAMScanStatus.E_MANUAL.ToString(); lstTamScanLog.Add(Tams); isChangeTamScan = true; } else { string E_MANUAL = TAMScanStatus.E_MANUAL.ToString(); Att_TAMScanLog tamScan = repoAtt_TAMScanLog.FindBy(m => m.TimeLog == WorkdayOld.InTime1.Value && m.ProfileID == WorkdayOld.ProfileID && m.Status == E_MANUAL).FirstOrDefault(); if (tamScan != null) { tamScan.TimeLog = Intime1; isChangeTamScan = true; } } WorkdaySave.InTime1 = Intime1; //Cập nhật vào workday } else if (WorkdayOld.InTime1 != null && Intime1 == null) { Notes += "Intime1:" + "Null"; WorkdaySave.InTime1 = Intime1; //Cập nhật vào workday string E_MANUAL = TAMScanStatus.E_MANUAL.ToString(); //Att_TAMScanLog tamScan = EntityService.CreateQueryable<Att_TAMScanLog>(false, GuidContext, Guid.Empty, m => m.TimeLog == WorkDay.InTime1.Value && m.ProfileID == WorkDay.ProfileID && m.Status == E_MANUAL).FirstOrDefault(); Att_TAMScanLog tamScan = repoAtt_TAMScanLog.FindBy(m => m.TimeLog == WorkdayOld.InTime1.Value && m.ProfileID == WorkdayOld.ProfileID && m.Status == E_MANUAL).FirstOrDefault(); if (tamScan != null) { tamScan.IsDelete = true; } } if (OutTime1 != null) { if (WorkdayOld.OutTime1 != OutTime1) { Notes += "OutTime1:" + string.Format("{0:dd/MM/yyyy hh:mm:ss}", OutTime1); } if (WorkdayOld.OutTime1 == null) //Thiếu OutTime. Thì cập nhật vào lstTamscanLog { Att_TAMScanLog Tams = new Att_TAMScanLog(); Tams.ID = Guid.NewGuid(); Tams.TimeLog = OutTime1; Tams.Status = TAMScanStatus.E_MANUAL.ToString(); lstTamScanLog.Add(Tams); isChangeTamScan = true; } WorkdaySave.OutTime1 = OutTime1; } else if (WorkdayOld.OutTime1 != null && OutTime1 == null) { Notes += "OutTime1:" + "Null"; WorkdaySave.OutTime1 = OutTime1; //Cập nhật vào workday //Att_TAMScanLog tamScan = EntityService.CreateQueryable<Att_TAMScanLog>(false, GuidContext, Guid.Empty, m => m.TimeLog == WorkDay.OutTime1.Value && m.ProfileID == WorkDay.ProfileID && m.Status == E_MANUAL).FirstOrDefault(); Att_TAMScanLog tamScan = repoAtt_TAMScanLog.FindBy(m => m.TimeLog == WorkdayOld.InTime1.Value && m.ProfileID == WorkdayOld.ProfileID && m.Status == TAMScanStatus.E_MANUAL.ToString()).FirstOrDefault(); if (tamScan != null) { tamScan.TimeLog = OutTime1; isChangeTamScan = true; } } #endregion #region xử lý tamsCanLog DateTime workdate = WorkdayOld.WorkDate; string cardCode = string.Empty; var Card = repoHre_CardHistory.FindBy(m => (!m.IsDelete.HasValue || m.IsDelete != true) && m.ProfileID == WorkdayOld.ProfileID && m.CardCode != null && m.DateEffect <= workdate).OrderByDescending(m => m.DateEffect).FirstOrDefault(); //Hre_CardHistoryEntity Card = EntityService.CreateQueryable<Hre_CardHistory>(false, GuidContext, Guid.Empty, m => m.ProfileID == WorkDay.ProfileID && m.CardCode != null && m.DateEffect <= workdate).OrderByDescending(m => m.DateEffect).FirstOrDefault(); if (Card != null) { cardCode = Card.CardCode; } else { string CodeAttendance = repoHre_Profile.FindBy(m => m.IsDelete == null && m.ID == WorkdayOld.ProfileID).Select(m => m.CodeAttendance).FirstOrDefault(); //string CodeAttendance = EntityService.CreateQueryable<Hre_Profile>(false, GuidContext, Guid.Empty, m => m.ID == WorkDay.ProfileID).Select(m => m.CodeAttendance).FirstOrDefault(); cardCode = CodeAttendance; } if (cardCode == string.Empty) { //return new { valid = true }; message = ConstantMessages.Error.TranslateString(); return message; } if (lstTamScanLog.Count > 0) { foreach (var Tam in lstTamScanLog) { Tam.CardCode = cardCode; Tam.ProfileID = WorkdayOld.ProfileID; } //EntityService.AddEntity<Att_TAMScanLog>(GuidContext, lstTamScanLog.ToArray()); repoAtt_TAMScanLog.Add(lstTamScanLog); } #endregion #region Xử lý Xóa Trong tamsCanLog if (WorkdayOld.InTime1 != null && Intime1 == null) { string E_MANUAL = TAMScanStatus.E_MANUAL.ToString(); List<Att_TAMScanLog> tamScan = repoAtt_TAMScanLog.FindBy(m => m.IsDelete == null && m.TimeLog == WorkdayOld.InTime1 && m.CardCode == cardCode).ToList(); //List<Att_TAMScanLog> tamScan = EntityService.CreateQueryable<Att_TAMScanLog>(false, GuidContext, Guid.Empty, m => m.TimeLog == InTimeBeforeChanges.Value && m.CardCode == cardCode // && m.Status == E_MANUAL).ToList<Att_TAMScanLog>(); if (tamScan != null && tamScan.Count > 0) { foreach (var item in tamScan) { item.IsDelete = true; } } } if (WorkdayOld.OutTime1 != null && OutTime1 == null) { string E_MANUAL = TAMScanStatus.E_MANUAL.ToString(); List<Att_TAMScanLog> tamScan = repoAtt_TAMScanLog.FindBy(m => m.IsDelete == null && m.TimeLog == WorkdayOld.OutTime1 && m.CardCode == cardCode).ToList(); //List<Att_TAMScanLog> tamScan = EntityService.CreateQueryable<Att_TAMScanLog>(false, GuidContext, Guid.Empty, m => m.TimeLog == OutTimeBeforeChanges.Value && m.CardCode == cardCode // && m.Status == E_MANUAL).ToList<Att_TAMScanLog>(); if (tamScan.Count > 0) { foreach (var item in tamScan) { item.IsDelete = true; } } } #endregion #region Thay đổi Shift var ShiftAll = repoCat_Shift.FindBy(m => m.IsDelete == null).ToList(); Guid ShiftIDActual = Guid.Empty; Guid ShiftIDApprove = Guid.Empty; string ShiftCodeActual = string.Empty; string ShiftCodeApprove = string.Empty; var ShiftActual = ShiftAll.Where(m => m.ID == WorkdaySave.ShiftActual).FirstOrDefault(); var ShiftApprove = ShiftAll.Where(m => m.ID == WorkdaySave.ShiftApprove).FirstOrDefault(); if (ShiftActual != null) { ShiftIDActual = ShiftActual.ID; ShiftCodeActual = ShiftActual.Code; } if (ShiftApprove != null) { ShiftIDApprove = ShiftApprove.ID; ShiftCodeApprove = ShiftApprove.Code; } if (WorkdayOld.ShiftActual != ShiftIDActual) { Notes += "ShiftCodeActual:" + ShiftActual.Code; Notes += "ShiftCodeApprove:" + ShiftActual.Code; if (ShiftActual != null) // nếu như ShiftActual thay đổi thì thay đổi { ShiftIDActual = ShiftActual.ID; ShiftCodeActual = ShiftActual.Code; ShiftIDApprove = ShiftActual.ID; ShiftCodeApprove = ShiftActual.Code; } } else if (WorkdayOld.ShiftApprove != ShiftIDApprove) { Notes += "ShiftCodeApprove:" + ShiftCodeApprove; } WorkdaySave.ShiftActual = ShiftIDActual; WorkdaySave.ShiftApprove = ShiftIDApprove; #endregion #region thay dổi lateEarly string LateEarLyModify = string.Empty; double MinuteLateEarly = 0; if (WorkdaySave.LateEarlyDuration != null) { MinuteLateEarly = WorkdaySave.LateEarlyDuration.Value; } string LateEarlyReason = string.Empty; if (!string.IsNullOrEmpty(WorkdaySave.LateEarlyReason)) { LateEarlyReason = WorkdaySave.LateEarlyReason; } bool IsChangeLateEarly = false; if (WorkdayOld.LateEarlyDuration != null && (WorkdayOld.LateEarlyDuration != MinuteLateEarly)) { IsChangeLateEarly = true; Notes += "LateEarlyDuration:" + MinuteLateEarly; WorkdaySave.LateEarlyDuration = MinuteLateEarly; } if ((WorkdayOld.LateEarlyReason == null && !string.IsNullOrEmpty(LateEarlyReason)) || WorkdayOld.LateEarlyReason != LateEarlyReason) { Notes += "LateEarlyReason:" + LateEarlyReason; WorkdaySave.LateEarlyReason = LateEarlyReason; } //Cat_TAMScanReasonMiss TAMScanReasonMiss = EntityService.CreateQueryable<Cat_TAMScanReasonMiss>(false, GuidContext, Guid.Empty, m => m.TAMScanReasonMissName == MissInOutReason).FirstOrDefault(); Cat_TAMScanReasonMiss TAMScanReasonMiss = repoCat_TAMScanReasonMiss.FindBy(m => m.IsDelete == null && m.ID == MissInOutReason).FirstOrDefault(); if (TAMScanReasonMiss != null) { if (WorkdayOld.MissInOutReason == Guid.Empty || WorkdayOld.MissInOutReason != TAMScanReasonMiss.ID) { Notes += "MissInOutReason:" + TAMScanReasonMiss.TAMScanReasonMissName; } WorkdaySave.TAMScanReasonMissName = TAMScanReasonMiss.TAMScanReasonMissName; } else { Notes += "MissInOutReason:" + "NULL"; WorkdaySave.MissInOutReason = null; } #endregion #region Đăng Ký Nghỉ LeaveDay string LeavedayCode = string.Empty; if (WorkdaySave.udLeavedayCode1 != null && WorkdayOld.udLeavedayCode1 != WorkdaySave.udLeavedayCode1) { LeavedayCode = WorkdaySave.udLeavedayCode1; if (LeavedayCode == "CO") { message = ConstantMessages.CantRegisterCO.TranslateString(); return message; //Common.MessageBoxs(Messages.Msg, LanguageManager.GetString(Messages.CantRegisterCO), MessageBox.Icon.WARNING, string.Empty); //return new { valid = true }; } } if (WorkdayOld != null && (!string.IsNullOrEmpty(WorkdayOld.udLeavedayCode1) || !string.IsNullOrEmpty(WorkdayOld.udLeavedayCode2))) { if (!string.IsNullOrEmpty(WorkdayOld.udLeavedayStatus1) && WorkdayOld.udLeavedayStatus1 == LeaveDayStatus.E_APPROVED.ToString()) { message = ConstantMessages.StatusApproveCannotEdit.TranslateString(); return message; //Common.MessageBoxs(Messages.Msg, LanguageManager.GetString(Messages.DataCantBeModify), MessageBox.Icon.WARNING, string.Empty); //return new { valid = true }; } } #endregion string TypeOld = WorkdaySave.Type; //if (isChangeTamScan) //{ // WorkdaySave.udIsManualFromTamScan = true; //} List<Att_WorkdayEntity> lstWorkdaySave = new List<Att_WorkdayEntity>() { WorkdaySave }; List<Att_Workday> lstWorkdayNew = ComputeWorkday(lstWorkdaySave.Translate<Att_Workday>(), ShiftAll, isChangeTamScan, UserLogin); WorkdaySave.Type = TypeOld; if (lstWorkdayNew.Count > 0) { Att_Workday workdayNew = lstWorkdayNew.FirstOrDefault(); if (workdayNew != null) { if (LeavedayCode != "") { string ErrLeave = SaveLeaveDay(workdayNew, LeavedayCode, UserLogin); if (ErrLeave != string.Empty) { return ErrLeave; //Common.MessageBoxs(Messages.Msg, ErrLeave, MessageBox.Icon.WARNING, string.Empty); //return new { valid = true }; } } workdayNew.Type = TypeOld; if (IsChangeLateEarly) workdayNew.LateEarlyDuration = MinuteLateEarly; //if (Notes != string.Empty) //{ // Notes = "(" + UserLogin + "-" + string.Format("{0:dd/MM/yyyy hh:mm:ss}", DateTime.Now) + "-[" + Notes + "])"; //} string NoteValidated = workdayNew.Note + Notes; if (NoteValidated.Length > 2000) { NoteValidated = NoteValidated.Substring(NoteValidated.Length - 2000, 2000); } if (Notes != string.Empty) { workdayNew.SrcType = WorkdaySrcType.E_MANUAL.ToString(); } workdayNew.Note = NoteValidated; if (workdayNew.Type == string.Empty) { workdayNew.Type = WorkdayType.E_NORMAL.ToString(); } DateTime DateNew = workdayNew.WorkDate; Guid ProfileID = workdayNew.ProfileID; } } repoAtt_Workday.Edit(lstWorkdayNew); //EntityService.AddEntity<Att_Workday>(GuidContext, lstWorkdayNew.ToArray()); //EntityService.SubmitChanges(GuidContext, LoginUserID); repoAtt_Workday.SaveChanges(); //unitOfWork.SaveChanges(); return message; } }