leave report

This commit is contained in:
Suphonchai Phoonsawat 2025-09-02 14:55:07 +07:00
parent 3ae9be5869
commit 8001dd0c11
12 changed files with 678 additions and 244 deletions

View file

@ -88,7 +88,7 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
.Include(x => x.LeaveType)
.FirstOrDefaultAsync(x => x.LeaveYear == year && x.LeaveTypeId == typeId && x.ProfileId == pf.Id);
if(data == null)
if (data == null)
{
throw new Exception(GlobalMessages.DataNotFound);
}
@ -165,5 +165,164 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
return data;
}
public async Task<LeaveBeginning?> GetByYearAndTypeIdForUser2Async(int year, Guid typeId, Guid userId)
{
var pf = await _userProfileRepository.GetProfileByKeycloakIdAsync(userId, AccessToken);
if (pf == null)
{
return null;
}
var govAge = (pf?.DateStart?.Date ?? DateTime.Now.Date).DiffDay(DateTime.Now.Date);
var leaveType = await _dbContext.Set<LeaveType>().FirstOrDefaultAsync(x => x.Id == typeId);
var data = await _dbContext.Set<LeaveBeginning>()
.Include(x => x.LeaveType)
.FirstOrDefaultAsync(x => x.LeaveYear == year && x.LeaveTypeId == typeId && x.ProfileId == pf.Id);
if (data == null)
{
var limit = 0.0;
var prev = await _dbContext.Set<LeaveBeginning>()
.Include(x => x.LeaveType)
.FirstOrDefaultAsync(x => x.LeaveYear == year - 1 && x.LeaveTypeId == typeId && x.ProfileId == pf.Id);
var prevRemain = 0.0;
if (prev != null)
{
prevRemain = prev.LeaveDays - prev.LeaveDaysUsed;
}
if (govAge >= 180)
{
if (govAge >= 3650)
{
limit = 10 + prevRemain;
if (limit > 30) limit = 30;
}
else
{
limit = 10 + prevRemain;
if (limit > 20) limit = 20;
}
}
else
{
limit = 0.0;
}
data = new LeaveBeginning
{
LeaveYear = year,
LeaveTypeId = typeId,
ProfileId = pf.Id,
Prefix = pf.Prefix,
FirstName = pf.FirstName,
LastName = pf.LastName,
LeaveDaysUsed = 0,
LeaveDays = leaveType?.Code == "LV-005" ? limit : 0
};
_dbContext.Set<LeaveBeginning>().Add(data);
await _dbContext.SaveChangesAsync();
}
return data;
}
public async Task<List<LeaveBeginning>> GetAllByYearAndTypeAsync(int year, Guid typeId, List<ProfileData> userIdList)
{
var updateList = new List<LeaveBeginning>();
var result = new List<LeaveBeginning>();
var beginningList = await _dbContext.Set<LeaveBeginning>()
.Include(x => x.LeaveType)
.Where(x => x.LeaveYear == year && x.LeaveTypeId == typeId)
.ToListAsync();
foreach (var pf in userIdList)
{
//var pf = await _userProfileRepository.GetProfileByKeycloakIdAsync(id, AccessToken);
//if (pf == null)
//{
// continue; // Goto Next Id
//}
var govAge = (pf?.DateStart?.Date ?? DateTime.Now.Date).DiffDay(DateTime.Now.Date);
var leaveType = await _dbContext.Set<LeaveType>().FirstOrDefaultAsync(x => x.Id == typeId);
var data = beginningList.FirstOrDefault(x => x.ProfileId == pf.Id);
if (data == null)
{
var limit = 0.0;
var prev = await _dbContext.Set<LeaveBeginning>()
.Include(x => x.LeaveType)
.FirstOrDefaultAsync(x => x.LeaveYear == year - 1 && x.LeaveTypeId == typeId && x.ProfileId == pf.Id);
var prevRemain = 0.0;
if (prev != null)
{
prevRemain = prev.LeaveDays - prev.LeaveDaysUsed;
}
if (govAge >= 180)
{
if (govAge >= 3650)
{
limit = 10 + prevRemain;
if (limit > 30) limit = 30;
}
else
{
limit = 10 + prevRemain;
if (limit > 20) limit = 20;
}
}
else
{
limit = 0.0;
}
data = new LeaveBeginning
{
LeaveYear = year,
LeaveTypeId = typeId,
ProfileId = pf.Id,
Prefix = pf.Prefix,
FirstName = pf.FirstName,
LastName = pf.LastName,
LeaveDaysUsed = 0,
LeaveDays = leaveType?.Code == "LV-005" ? limit : 0
};
updateList.Add(data);
}
result.Add(data);
}
if (!updateList.Any())
{
await _dbContext.Set<LeaveBeginning>().AddRangeAsync(updateList);
await _dbContext.SaveChangesAsync();
}
return result;
}
}
public class ProfileData
{
public Guid Id { get; set; } = Guid.Empty;
public string Prefix { get; set; } = string.Empty;
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public DateTime? DateStart { get; set; } = null;
}
}

View file

@ -1454,7 +1454,8 @@ namespace BMA.EHR.Application.Repositories.Leaves.LeaveRequests
KeycloakUserId = grp.Key.KeycloakUserId,
LeaveTypeId = grp.Key.LeaveTypeId,
LeaveTypeCode = grp.Key.LeaveTypeCode,
SumLeaveDay = grp.Sum(x => x.LeaveTotal)
SumLeaveDay = grp.Sum(x => x.LeaveTotal),
CountLeaveDay = grp.Count()
})
.ToList();