ปรับการคำนวน ขาด มาสาย ปกติ ถ้ามีการลาครึ่งวัน เช้า หรือ บ่าย ในส่วนตอนการปผระมวล และอนุมัติการลา

#2584
This commit is contained in:
Suphonchai Phoonsawat 2026-07-03 10:08:34 +07:00
parent 019a0a1be1
commit 2f2d7fe74c
2 changed files with 86 additions and 5 deletions

View file

@ -32,6 +32,9 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
private readonly LeaveBeginningRepository _leaveBeginningRepository;
private readonly ProcessUserTimeStampRepository _processUserTimeStampRepository;
private readonly UserDutyTimeRepository _userDutyTimeRepository;
private readonly DutyTimeRepository _dutyTimeRepository;
private readonly string URL = string.Empty;
@ -48,7 +51,9 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
IApplicationDBContext appDbContext,
MinIOLeaveService minIOService,
LeaveBeginningRepository leaveBeginningRepository,
ProcessUserTimeStampRepository processUserTimeStampRepository) : base(dbContext, httpContextAccessor)
ProcessUserTimeStampRepository processUserTimeStampRepository,
UserDutyTimeRepository userDutyTimeRepository,
DutyTimeRepository dutyTimeRepository) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
@ -58,6 +63,10 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
_emailSenderService = emailSenderService;
_appDbContext = appDbContext;
_userDutyTimeRepository = userDutyTimeRepository;
_dutyTimeRepository = dutyTimeRepository;
URL = (_configuration["VITE_URL_MGT"]).Replace("/api/v1", "");
Console.WriteLine($"URL : {URL}");
_minIOService = minIOService;
@ -1331,6 +1340,20 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
await _appDbContext.SaveChangesAsync();
// ปรับสถานะการลงเวลา
var currentDate = rawData.LeaveStartDate;
var defaultRound = await _dutyTimeRepository.GetDefaultAsync();
if (defaultRound is null)
{
throw new Exception("ไม่พบรอบการลงเวลาทำงาน Default");
}
var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(profile.Id, currentDate);
var roundId = effectiveDate != null ? effectiveDate.DutyTimeId : Guid.Empty;
var userRound = await _dutyTimeRepository.GetByIdAsync(roundId);
// TODO : รอดุึงรอบที่ผูกกับ user
var duty = userRound ?? defaultRound;
if (rawData.LeaveStartDate.Date == rawData.LeaveEndDate.Date)
{
var processCheckIn = await _dbContext.Set<ProcessUserTimeStamp>()
@ -1341,13 +1364,39 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
if (processCheckIn is not null)
{
var startTimeMorning = duty.StartTimeMorning;
var endTimeMorning = duty.EndTimeMorning;
var startTimeAfterNoon = duty.StartTimeAfternoon;
var endTimeAfterNoon = duty.EndTimeAfternoon;
// var currentDateTime = DateTime.Parse(currentDate.ToString("yyyy-MM-dd HH:mm"));
// var dutyEndTimeAfternoon = DateTime.Parse($"{checkout.CheckIn.ToString("yyyy-MM-dd")} {endTime1}");
// var dutyEndTimeMorning = DateTime.Parse($"{checkout.CheckIn.ToString("yyyy-MM-dd")} {endTimeMorning1}");
switch (rawData.LeaveRange.Trim().ToUpper())
{
case "MORNING":
processCheckIn.CheckInStatus = "NORMAL";
var checkIn = DateTime.Parse(processCheckIn.CheckIn.ToString("yyyy-MM-dd HH:mm"));
var startAfternoon = DateTime.Parse($"{processCheckIn.CheckIn.ToString("yyyy-MM-dd")} {startTimeAfterNoon}");
if (checkIn <= startAfternoon)
processCheckIn.CheckInStatus = "NORMAL";
else
{
processCheckIn.CheckInStatus = "LATE";
}
break;
case "AFTERNOON":
processCheckIn.CheckOutStatus = "NORMAL";
if (processCheckIn.CheckOut is not null)
{
var checkOut = DateTime.Parse(processCheckIn.CheckOut.Value.ToString("yyyy-MM-dd HH:mm"));
var endMorning = DateTime.Parse($"{processCheckIn.CheckIn.ToString("yyyy-MM-dd")} {endTimeMorning}");
if(checkOut >= endMorning)
processCheckIn.CheckOutStatus = "NORMAL";
else
{
processCheckIn.CheckOutStatus = "ABSENT";
}
}
break;
case "ALL":
processCheckIn.CheckInStatus = "NORMAL";
@ -1361,6 +1410,11 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
}
else
{
var startTimeMorning = duty.StartTimeMorning;
var endTimeMorning = duty.EndTimeMorning;
var startTimeAfterNoon = duty.StartTimeAfternoon;
var endTimeAfterNoon = duty.EndTimeAfternoon;
var from = rawData.LeaveStartDate.Date;
var to = rawData.LeaveEndDate.Date;
for (var day = from.Date; day <= to.Date; day = day.AddDays(1))
@ -1375,10 +1429,28 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
switch (rawData.LeaveRange.Trim().ToUpper())
{
case "MORNING":
var checkIn = DateTime.Parse(processCheckIn.CheckIn.ToString("yyyy-MM-dd HH:mm"));
var startAfternoon = DateTime.Parse($"{processCheckIn.CheckIn.ToString("yyyy-MM-dd")} {startTimeAfterNoon}");
if (checkIn <= startAfternoon)
processCheckIn.CheckInStatus = "NORMAL";
else
{
processCheckIn.CheckInStatus = "LATE";
}
processCheckIn.CheckInStatus = "NORMAL";
break;
case "AFTERNOON":
processCheckIn.CheckOutStatus = "NORMAL";
if (processCheckIn.CheckOut is not null)
{
var checkOut = DateTime.Parse(processCheckIn.CheckOut.Value.ToString("yyyy-MM-dd HH:mm"));
var endMorning = DateTime.Parse($"{processCheckIn.CheckIn.ToString("yyyy-MM-dd")} {endTimeMorning}");
if (checkOut >= endMorning)
processCheckIn.CheckOutStatus = "NORMAL";
else
{
processCheckIn.CheckOutStatus = "ABSENT";
}
}
break;
case "ALL":
processCheckIn.CheckInStatus = "NORMAL";