diff --git a/BMA.EHR.Leave.Service/Controllers/LeaveController.cs b/BMA.EHR.Leave.Service/Controllers/LeaveController.cs index 36024048..4a84ef4b 100644 --- a/BMA.EHR.Leave.Service/Controllers/LeaveController.cs +++ b/BMA.EHR.Leave.Service/Controllers/LeaveController.cs @@ -277,7 +277,7 @@ namespace BMA.EHR.Leave.Service.Controllers } else { - var inUseRound = _userDutyTimeRepository.GetFirstInUseRound(oldData.Id); + var inUseRound = await _userDutyTimeRepository.GetFirstInUseRound(oldData.Id); //if (inUseRound != null || oldData.IsActive || oldData.IsDefault) if (inUseRound != null) @@ -402,6 +402,12 @@ namespace BMA.EHR.Leave.Service.Controllers [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task> CheckInAsync([FromForm] CheckTimeDto data) { + var userId = UserId == null ? Guid.Empty : Guid.Parse(UserId); + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(userId); + + if (profile == null) + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + if (data.Img == null) throw new Exception(GlobalMessages.NoFileToUpload); var currentDate = DateTime.Now; @@ -414,6 +420,19 @@ namespace BMA.EHR.Leave.Service.Controllers var currentCheckInProcess = await _processUserTimeStampRepository.GetTimestampByDateAsync(Guid.Parse(UserId), currentDate); + var defaultRound = await _dutyTimeRepository.GetDefaultAsync(); + if (defaultRound == null) + { + return Error("ไม่พบรอบการลงเวลาทำงาน Default", StatusCodes.Status404NotFound); + } + + var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(profile.Id); + var roundId = effectiveDate != null ? effectiveDate.DutyTimeId : Guid.Empty; + var userRound = await _dutyTimeRepository.GetByIdAsync(roundId); + + // TODO : รอดุึงรอบที่ผูกกับ user + var duty = userRound ?? defaultRound; + // create check in object if (data.CheckInId == null) { @@ -441,6 +460,11 @@ namespace BMA.EHR.Leave.Service.Controllers CheckIn = currentDate }; + var checkInStatus = DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) > + DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {duty.StartTimeMorning}") ? + "LATE" : + "NORMAL"; + // process - รอทำใน queue var checkin_process = new ProcessUserTimeStamp { @@ -452,7 +476,8 @@ namespace BMA.EHR.Leave.Service.Controllers CheckInPOI = data.POI, CheckInRemark = data.Remark, CheckInImageUrl = fileName, - CheckIn = currentDate + CheckIn = currentDate, + CheckInStatus = checkInStatus }; await _userTimeStampRepository.AddAsync(checkin); @@ -482,6 +507,14 @@ namespace BMA.EHR.Leave.Service.Controllers return Error(new Exception(GlobalMessages.DataNotFound), StatusCodes.Status404NotFound); } + var checkOutStatus = DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) < + DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {duty.EndTimeAfternoon}") ? + "LATE" : + DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm")) < + DateTime.Parse($"{currentDate.ToString("yyyy-MM-dd")} {duty.EndTimeMorning}") ? + "ABSENT" : + "NORMAL"; + if (checkout_process != null) { checkout_process.CheckOutLat = data.Lat; @@ -492,6 +525,7 @@ namespace BMA.EHR.Leave.Service.Controllers checkout_process.CheckOutRemark = data.Remark; checkout_process.CheckOutImageUrl = fileName; checkout_process.CheckOut = currentDate; + checkout_process.CheckOutStatus = checkOutStatus; await _processUserTimeStampRepository.UpdateAsync(checkout_process); }