report leave

This commit is contained in:
kittapath 2024-12-12 00:00:43 +07:00
parent 2510009cc6
commit 348da62370
3 changed files with 461 additions and 188 deletions

View file

@ -243,6 +243,66 @@ namespace BMA.EHR.Application.Repositories
}
}
public async Task<List<GetProfileByKeycloakIdDto>> GetProfileWithKeycloakAllOfficer(string? accessToken, string? node, string? nodeId)
{
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/keycloak-all-officer";
var apiKey = _configuration["API_KEY"];
var body = new
{
node = node,
nodeId = nodeId,
};
var profiles = new List<SearchProfileDto>();
var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body, apiKey);
if (apiResult != null)
{
var raw = JsonConvert.DeserializeObject<GetListProfileByKeycloakIdResultDto>(apiResult);
if (raw != null)
return raw.Result;
}
return null;
}
catch
{
throw;
}
}
public async Task<List<GetProfileByKeycloakIdDto>> GetProfileWithKeycloakAllEmployee(string? accessToken, string? node, string? nodeId)
{
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/keycloak-all-employee";
var apiKey = _configuration["API_KEY"];
var body = new
{
node = node,
nodeId = nodeId,
};
var profiles = new List<SearchProfileDto>();
var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body, apiKey);
if (apiResult != null)
{
var raw = JsonConvert.DeserializeObject<GetListProfileByKeycloakIdResultDto>(apiResult);
if (raw != null)
return raw.Result;
}
return null;
}
catch
{
throw;
}
}
public async Task<List<SearchProfileDto>> SearchProfile(string? citizenId, string? firstName, string? lastName, string accessToken)
{
try
@ -568,7 +628,7 @@ namespace BMA.EHR.Application.Repositories
}
}
public async Task<List<GetActiveRootDto>> GetActiveRootAsync(string? accessToken,Guid? revisionId)
public async Task<List<GetActiveRootDto>> GetActiveRootAsync(string? accessToken, Guid? revisionId)
{
try
{

View file

@ -15,6 +15,7 @@ using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Annotations;
using System.Globalization;
using System.Security.Claims;
using BMA.EHR.Application.Responses.Profiles;
namespace BMA.EHR.Leave.Service.Controllers
{
@ -731,20 +732,19 @@ namespace BMA.EHR.Leave.Service.Controllers
}
}
/// <summary>
/// LV2_035 - รายงานบัญชีแสดงวันลา ขรก
/// LV2_035 - รายงานสำหรับการลางานของข้าราชการ-กรุงเทพมหานครสามัญ และลูกจ้างประจำกรุงเทพมหานคร
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("leaveday/officer")]
[HttpPost("leaveday/{type:string}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetOfficerLeaveDayReport([FromBody] GetLeaveReportDto req)
public async Task<ActionResult<ResponseObject>> GetLeaveDayReport([FromBody] GetLeaveDetailByNodeReportDto req, string type)
{
try
{
@ -754,8 +754,15 @@ namespace BMA.EHR.Leave.Service.Controllers
{
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
}
//var profile = await _userProfileRepository.SearchProfile(null, null, null);
var profile = await _userProfileRepository.GetProfileWithKeycloak(AccessToken);
var profile = new List<GetProfileByKeycloakIdDto>();
if (type.Trim().ToUpper() == "OFFICER")
{
profile = await _userProfileRepository.GetProfileWithKeycloakAllOfficer(AccessToken, req.node, req.nodeId);
}
else
{
profile = await _userProfileRepository.GetProfileWithKeycloakAllEmployee(AccessToken, req.node, req.nodeId);
}
if (profile.Count > 0)
{
profile = profile.Where(p => p.IsLeave == false).ToList();
@ -767,8 +774,6 @@ namespace BMA.EHR.Leave.Service.Controllers
var count = 1;
var employees = new List<dynamic>();
foreach (var p in profile)
{
var keycloakUserId = p.Keycloak ?? Guid.Empty;
@ -810,7 +815,6 @@ namespace BMA.EHR.Leave.Service.Controllers
req.StartDate,
req.EndDate);
var defaultRound = await _dutyTimeRepository.GetDefaultAsync();
if (defaultRound == null)
{
@ -889,19 +893,17 @@ namespace BMA.EHR.Leave.Service.Controllers
var result = new
{
template = "LeaveHalfYear-ขรก",
reportName = "LeaveHalfYear-Officer",
template = type.Trim().ToUpper() == "OFFICER" ? "LeaveHalfYear-ขรก" : "LeaveHalfYear-ลูกจ้าง",
reportName = type.Trim().ToUpper() == "OFFICER" ? "LeaveHalfYear-Officer" : "LeaveHalfYear-Employee",
data = new
{
leaveDateStart = req.StartDate.Date.ToThaiShortDate().ToThaiNumber(),
leaveDateEnd = req.EndDate.Date.ToThaiShortDate().ToThaiNumber(),
organizationName = profile.First().Oc ?? "",
leaveTitleType = req.Type == "FULL" ? "หนึ่งปี" : "ครึ่งปี",
employees = employees
}
};
return Success(result);
}
catch (Exception ex)
@ -911,18 +913,18 @@ namespace BMA.EHR.Leave.Service.Controllers
}
/// <summary>
/// LV2_036 - รายงานบัญชีแสดงวันลา ลูกจ้าง
/// LV2_037 - รายงานการเข้างานจำแนกรายวัน รายสัปดาห์ รายเดือน แยกรายหน่วยงาน/ส่วนราชการ
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("leaveday/employee")]
[HttpPost("time-records/{type:string}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetEmployeeLeaveDayReport([FromBody] GetLeaveReportDto req)
public async Task<ActionResult<ResponseObject>> GetTimeRecordsOfficerReport([FromBody] GetLeaveDetailByNodeReportDto req, string type)
{
try
{
@ -932,158 +934,15 @@ namespace BMA.EHR.Leave.Service.Controllers
{
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
}
var profile = await _userProfileRepository.SearchProfileEmployee(null, null, null, AccessToken ?? "");
var count = 1;
var employees = new List<dynamic>();
// get leave day
var leaveDays = await _leaveRequestRepository.GetSumApproveLeaveByTypeAndRange(req.StartDate, req.EndDate);
var leaveCounts = await _leaveRequestRepository.GetCountApproveLeaveByTypeAndRange(req.StartDate, req.EndDate);
var leaveTypes = await _leaveTypeRepository.GetAllAsync();
foreach (var p in profile)
var profile = new List<GetProfileByKeycloakIdDto>();
if (type.Trim().ToUpper() == "OFFICER")
{
var keycloakUserId = p.Keycloak ?? Guid.Empty;
var sickDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-001");
var sickDayCount = sickDay != null ? sickDay.SumLeaveDay : 0;
var sickCountData = leaveCounts.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-001");
var sickCount = sickCountData != null ? sickCountData.CountLeave : 0;
var personalDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-002");
var personalDayCount = personalDay != null ? personalDay.SumLeaveDay : 0;
var personalCountData = leaveCounts.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-002");
var personalCount = personalCountData != null ? personalCountData.CountLeave : 0;
var maternityDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-003");
var maternityDayCount = maternityDay != null ? maternityDay.SumLeaveDay : 0;
var maternityCountData = leaveCounts.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-003");
var maternityCount = maternityCountData != null ? maternityCountData.CountLeave : 0;
var wifeDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-004");
var wifeDayCount = wifeDay != null ? wifeDay.SumLeaveDay : 0;
var wifeCountData = leaveCounts.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-004");
var wifeCount = wifeCountData != null ? wifeCountData.CountLeave : 0;
var restDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-005");
var restDayCount = restDay != null ? restDay.SumLeaveDay : 0;
var restCountData = leaveCounts.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-005");
var restCount = restCountData != null ? restCountData.CountLeave : 0;
var ordainDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-006");
var ordainDayCount = ordainDay != null ? ordainDay.SumLeaveDay : 0;
var ordainCountData = leaveCounts.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-006");
var ordainCount = ordainCountData != null ? ordainCountData.CountLeave : 0;
var timeStamps = await _processUserTimeStampRepository.GetTimeStampHistoryByRangeForUserAsync(p.Keycloak ?? Guid.Empty,
req.StartDate,
req.EndDate);
var defaultRound = await _dutyTimeRepository.GetDefaultAsync();
if (defaultRound == null)
{
return Error("ไม่พบรอบการลงเวลา Default", StatusCodes.Status404NotFound);
}
//var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty);
var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(p.Id);
var roundId = effectiveDate != null ? effectiveDate.DutyTimeId : Guid.Empty;
var userRound = await _dutyTimeRepository.GetByIdAsync(roundId);
var duty = userRound ?? defaultRound;
var processTimeStamps = timeStamps
.Select(d => new
{
d.Id,
CheckInStatus = DateTime.Parse(d.CheckIn.ToString("yyyy-MM-dd HH:mm")) >
DateTime.Parse($"{d.CheckIn.Date.ToString("yyyy-MM-dd")} {duty.StartTimeMorning}") ?
"LATE" :
"NORMAL",
CheckOutStatus = d.CheckOut == null ? "" :
DateTime.Parse(d.CheckOut.Value.ToString("yyyy-MM-dd HH:mm")) <
DateTime.Parse($"{d.CheckIn.Date.ToString("yyyy-MM-dd")} {duty.EndTimeAfternoon}") ?
"LATE" :
DateTime.Parse(d.CheckOut.Value.ToString("yyyy-MM-dd HH:mm")) <
DateTime.Parse($"{d.CheckIn.Date.ToString("yyyy-MM-dd")} {duty.EndTimeMorning}") ?
"ABSENT" :
"NORMAL",
});
var absentCount = processTimeStamps.Count(x => x.CheckOutStatus == "ABSENT");
var lateCount = processTimeStamps.Count(x => x.CheckInStatus == "LATE");
var emp = new
{
no = count,
fullName = $"{p.Prefix}{p.FirstName} {p.LastName}",// _userProfileRepository.GetUserFullName(p.Keycloak ?? Guid.Empty, AccessToken),
posNo = p.PosNo ?? "",
reason = "",
sickDay = sickDayCount,
sickDayCount = sickCount,
personalDay = personalDayCount,
personalDayCount = personalCount,
leaveDay = sickDayCount + personalDayCount,
leaveTotal = sickCount + personalCount, //*รวมจำนวนครั้งลาป่วยและลากิจ(13)
maternityDay = maternityDayCount,
wifeDay = wifeDayCount,
ordainDay = ordainDayCount,
absentTotal = absentCount,
lateTotal = lateCount,
leaveTotalText = $"{sickDayCount + personalDayCount}/{sickCount + personalCount}"
};
employees.Add(emp);
count++;
profile = await _userProfileRepository.GetProfileWithKeycloakAllOfficer(AccessToken, req.node, req.nodeId);
}
var result = new
else
{
template = "LeaveHalfYear-ลูกจ้าง",
reportName = "LeaveHalfYear-Employee",
data = new
{
leaveDateStart = req.StartDate.Date.ToThaiShortDate().ToThaiNumber(),
leaveDateEnd = req.EndDate.Date.ToThaiShortDate().ToThaiNumber(),
organizationName = profile.First().Oc ?? "",
leaveTitleType = req.Type == "FULL" ? "หนึ่งปี" : "ครึ่งปี",
employees = employees
}
};
return Success(result);
}
catch (Exception ex)
{
return Error(ex);
}
}
/// <summary>
/// LV2_037 - รายงานการลงเวลาประจำวัน
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("time-records/officer")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetTimeRecordsOfficerReport([FromBody] GetLeaveDetailReportDto req)
{
try
{
//var profile = await _userProfileRepository.SearchProfile(null, null, null);
var profile = await _userProfileRepository.GetProfileWithKeycloak(AccessToken);
profile = await _userProfileRepository.GetProfileWithKeycloakAllEmployee(AccessToken, req.node, req.nodeId);
}
if (profile.Count > 0)
{
profile = profile.Where(p => p.IsLeave == false).ToList();
@ -1267,26 +1126,9 @@ namespace BMA.EHR.Leave.Service.Controllers
}
var dateStamp = "";
var reportName = "";
switch (req.Type.ToUpper())
{
case "MONTH":
{
var dd = req.StartDate.Date;
reportName = "TimeStamp-Month";
dateStamp = $"เดือน {dd.ToString("MMMM", _culture.DateTimeFormat)} พ.ศ. {dd.Year.ToThaiYear()}".ToThaiNumber();
break;
}
default:
dateStamp = req.StartDate.Date.ToThaiFullDate().ToThaiNumber();
reportName = "TimeStamp-Day";
break;
}
var item = new
{
DateTimeStamp = dateStamp,
DateTimeStamp = req.StartDate.Date.ToThaiFullDate().ToThaiNumber() + "-" + req.EndDate.Date.ToThaiFullDate().ToThaiNumber(),
officerTotal = profile.Count,
workTotal = workTotal,
restTotal = restTotal,
@ -1298,12 +1140,10 @@ namespace BMA.EHR.Leave.Service.Controllers
employees = employees
};
var result = new
{
template = reportName,
reportName = reportName,
template = "TimeStamp-Day",
reportName = "TimeStamp-Day",
data = item
};
@ -1315,6 +1155,367 @@ namespace BMA.EHR.Leave.Service.Controllers
}
}
/// <summary>
/// LV2_035 - รายงานการเข้างานสาย จำแนกเป็นรายบุคคลและหรือรายหน่วยงาน/ส่วนราชการ
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("late/{type:string}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetLateReport([FromBody] GetLeaveDetailByNodeReportDto req, string type)
{
try
{
var getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_LEAVE_REPORT");
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
if (jsonData["status"]?.ToString() != "200")
{
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
}
var profile = new List<GetProfileByKeycloakIdDto>();
if (type.Trim().ToUpper() == "OFFICER")
{
profile = await _userProfileRepository.GetProfileWithKeycloakAllOfficer(AccessToken, req.node, req.nodeId);
}
else
{
profile = await _userProfileRepository.GetProfileWithKeycloakAllEmployee(AccessToken, req.node, req.nodeId);
}
if (profile.Count > 0)
{
profile = profile.Where(p => p.IsLeave == false).ToList();
}
// get leave day
var leaveDays = await _leaveRequestRepository.GetSumApproveLeaveByTypeAndRange(req.StartDate, req.EndDate);
var leaveTypes = await _leaveTypeRepository.GetAllAsync();
var count = 1;
var employees = new List<dynamic>();
foreach (var p in profile)
{
var keycloakUserId = p.Keycloak ?? Guid.Empty;
var sickDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-001");
var sickDayCount = sickDay != null ? sickDay.SumLeaveDay : 0;
var personalDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-002");
var personalDayCount = personalDay != null ? personalDay.SumLeaveDay : 0;
var maternityDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-003");
var maternityDayCount = maternityDay != null ? maternityDay.SumLeaveDay : 0;
var wifeDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-004");
var wifeDayCount = wifeDay != null ? wifeDay.SumLeaveDay : 0;
var restDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-005");
var restDayCount = restDay != null ? restDay.SumLeaveDay : 0;
var ordainDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-006");
var ordainDayCount = ordainDay != null ? ordainDay.SumLeaveDay : 0;
var absentDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-007");
var absentDayCount = absentDay != null ? absentDay.SumLeaveDay : 0;
var studyDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-008");
var studyDayCount = studyDay != null ? studyDay.SumLeaveDay : 0;
var agencyDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-009");
var agencyDayCount = agencyDay != null ? agencyDay.SumLeaveDay : 0;
var coupleDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-010");
var coupleDayCount = coupleDay != null ? coupleDay.SumLeaveDay : 0;
var therapyDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-011");
var therapyDayCount = therapyDay != null ? therapyDay.SumLeaveDay : 0;
var timeStamps = await _processUserTimeStampRepository.GetTimeStampHistoryByRangeForUserAsync(p.Keycloak ?? Guid.Empty,
req.StartDate,
req.EndDate);
var defaultRound = await _dutyTimeRepository.GetDefaultAsync();
if (defaultRound == null)
{
return Error("ไม่พบรอบการลงเวลา Default", StatusCodes.Status404NotFound);
}
//var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty);
var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(p.Id);
var roundId = effectiveDate != null ? effectiveDate.DutyTimeId : Guid.Empty;
var userRound = await _dutyTimeRepository.GetByIdAsync(roundId);
var duty = userRound ?? defaultRound;
var processTimeStamps = timeStamps
.Select(d => new
{
d.Id,
CheckInStatus = DateTime.Parse(d.CheckIn.ToString("yyyy-MM-dd HH:mm")) >
DateTime.Parse($"{d.CheckIn.Date.ToString("yyyy-MM-dd")} {duty.StartTimeMorning}") ?
"LATE" :
"NORMAL",
CheckOutStatus = d.CheckOut == null ? "" :
DateTime.Parse(d.CheckOut.Value.ToString("yyyy-MM-dd HH:mm")) <
DateTime.Parse($"{d.CheckIn.Date.ToString("yyyy-MM-dd")} {duty.EndTimeAfternoon}") ?
"LATE" :
DateTime.Parse(d.CheckOut.Value.ToString("yyyy-MM-dd HH:mm")) <
DateTime.Parse($"{d.CheckIn.Date.ToString("yyyy-MM-dd")} {duty.EndTimeMorning}") ?
"ABSENT" :
"NORMAL",
});
var absentCount = processTimeStamps.Count(x => x.CheckOutStatus == "ABSENT");
var lateCount = processTimeStamps.Count(x => x.CheckInStatus == "LATE");
var emp = new
{
no = count,
fullName = $"{p.Prefix}{p.FirstName} {p.LastName}",// _userProfileRepository.GetUserFullName(p.Keycloak ?? Guid.Empty, AccessToken),
position = p.Position == null ? "" : p.Position,
//positionLevel = p.PosLevel == null ? "" : p.PosLevel,
//posNo = p.ProfileSalary == null ? "" : p.ProfileSalary.PosNo,
positionLevel = p.PositionLevel == null ? "" : p.PositionLevel,
posNo = p.PosNo == null ? "" : p.PosNo,
reason = "",
sickDayCount = sickDayCount,
maternityDayCount = maternityDayCount,
wifeDayCount = wifeDayCount,
personalDayCount = personalDayCount,
restDayCount = restDayCount,
ordainDayCount = ordainDayCount,
absentDayCount = absentDayCount,
studyDayCount = studyDayCount,
agencyDayCount = agencyDayCount,
coupleDayCount = coupleDayCount,
therapyDayCount = therapyDayCount,
absentTotal = absentCount,
lateTotal = lateCount,
leaveTotal = sickDayCount +
maternityDayCount +
wifeDayCount +
personalDayCount +
restDayCount +
ordainDayCount +
absentDayCount +
studyDayCount +
agencyDayCount +
coupleDayCount +
therapyDayCount
};
employees.Add(emp);
count++;
}
var result = new
{
template = "LeaveHalfYear-Officer",
reportName = "LeaveHalfYear-Officer",
data = new
{
leaveDateStart = req.StartDate.Date.ToThaiShortDate().ToThaiNumber(),
leaveDateEnd = req.EndDate.Date.ToThaiShortDate().ToThaiNumber(),
organizationName = profile.First().Oc ?? "",
employees = employees,
headerText = type.Trim().ToUpper() == "OFFICER" ? "ข้าราชการสามัญ" : "ลูกจ้างประจำ"
}
};
return Success(result);
}
catch (Exception ex)
{
return Error(ex);
}
}
/// <summary>
/// LV2_035 - รายงานการลางาน จำแนกตามเพศ ประเภทการลา หน่วยงาน/ส่วนราชการ
/// </summary>
/// <returns>
/// </returns>
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpPost("leave2/{type:string}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<ResponseObject>> GetLeave2Report([FromBody] GetLeaveDetailByNodeReportDto req, string type)
{
try
{
var getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_LEAVE_REPORT");
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
if (jsonData["status"]?.ToString() != "200")
{
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
}
var profile = new List<GetProfileByKeycloakIdDto>();
if (type.Trim().ToUpper() == "OFFICER")
{
profile = await _userProfileRepository.GetProfileWithKeycloakAllOfficer(AccessToken, req.node, req.nodeId);
}
else
{
profile = await _userProfileRepository.GetProfileWithKeycloakAllEmployee(AccessToken, req.node, req.nodeId);
}
if (profile.Count > 0)
{
profile = profile.Where(p => p.IsLeave == false).ToList();
}
// get leave day
var leaveDays = await _leaveRequestRepository.GetSumApproveLeaveByTypeAndRange(req.StartDate, req.EndDate);
var leaveTypes = await _leaveTypeRepository.GetAllAsync();
var count = 1;
var employees = new List<dynamic>();
foreach (var p in profile)
{
var keycloakUserId = p.Keycloak ?? Guid.Empty;
var sickDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-001");
var sickDayCount = sickDay != null ? sickDay.SumLeaveDay : 0;
var personalDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-002");
var personalDayCount = personalDay != null ? personalDay.SumLeaveDay : 0;
var maternityDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-003");
var maternityDayCount = maternityDay != null ? maternityDay.SumLeaveDay : 0;
var wifeDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-004");
var wifeDayCount = wifeDay != null ? wifeDay.SumLeaveDay : 0;
var restDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-005");
var restDayCount = restDay != null ? restDay.SumLeaveDay : 0;
var ordainDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-006");
var ordainDayCount = ordainDay != null ? ordainDay.SumLeaveDay : 0;
var absentDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-007");
var absentDayCount = absentDay != null ? absentDay.SumLeaveDay : 0;
var studyDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-008");
var studyDayCount = studyDay != null ? studyDay.SumLeaveDay : 0;
var agencyDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-009");
var agencyDayCount = agencyDay != null ? agencyDay.SumLeaveDay : 0;
var coupleDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-010");
var coupleDayCount = coupleDay != null ? coupleDay.SumLeaveDay : 0;
var therapyDay = leaveDays.FirstOrDefault(x => x.KeycloakUserId == keycloakUserId && x.LeaveTypeCode == "LV-011");
var therapyDayCount = therapyDay != null ? therapyDay.SumLeaveDay : 0;
var timeStamps = await _processUserTimeStampRepository.GetTimeStampHistoryByRangeForUserAsync(p.Keycloak ?? Guid.Empty,
req.StartDate,
req.EndDate);
var defaultRound = await _dutyTimeRepository.GetDefaultAsync();
if (defaultRound == null)
{
return Error("ไม่พบรอบการลงเวลา Default", StatusCodes.Status404NotFound);
}
//var userRound = await _dutyTimeRepository.GetByIdAsync(profile.DutyTimeId ?? Guid.Empty);
var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(p.Id);
var roundId = effectiveDate != null ? effectiveDate.DutyTimeId : Guid.Empty;
var userRound = await _dutyTimeRepository.GetByIdAsync(roundId);
var duty = userRound ?? defaultRound;
var processTimeStamps = timeStamps
.Select(d => new
{
d.Id,
CheckInStatus = DateTime.Parse(d.CheckIn.ToString("yyyy-MM-dd HH:mm")) >
DateTime.Parse($"{d.CheckIn.Date.ToString("yyyy-MM-dd")} {duty.StartTimeMorning}") ?
"LATE" :
"NORMAL",
CheckOutStatus = d.CheckOut == null ? "" :
DateTime.Parse(d.CheckOut.Value.ToString("yyyy-MM-dd HH:mm")) <
DateTime.Parse($"{d.CheckIn.Date.ToString("yyyy-MM-dd")} {duty.EndTimeAfternoon}") ?
"LATE" :
DateTime.Parse(d.CheckOut.Value.ToString("yyyy-MM-dd HH:mm")) <
DateTime.Parse($"{d.CheckIn.Date.ToString("yyyy-MM-dd")} {duty.EndTimeMorning}") ?
"ABSENT" :
"NORMAL",
});
var absentCount = processTimeStamps.Count(x => x.CheckOutStatus == "ABSENT");
var lateCount = processTimeStamps.Count(x => x.CheckInStatus == "LATE");
var emp = new
{
no = count,
fullName = $"{p.Prefix}{p.FirstName} {p.LastName}",// _userProfileRepository.GetUserFullName(p.Keycloak ?? Guid.Empty, AccessToken),
position = p.Position == null ? "" : p.Position,
//positionLevel = p.PosLevel == null ? "" : p.PosLevel,
//posNo = p.ProfileSalary == null ? "" : p.ProfileSalary.PosNo,
positionLevel = p.PositionLevel == null ? "" : p.PositionLevel,
posNo = p.PosNo == null ? "" : p.PosNo,
reason = "",
sickDayCount = sickDayCount,
maternityDayCount = maternityDayCount,
wifeDayCount = wifeDayCount,
personalDayCount = personalDayCount,
restDayCount = restDayCount,
ordainDayCount = ordainDayCount,
absentDayCount = absentDayCount,
studyDayCount = studyDayCount,
agencyDayCount = agencyDayCount,
coupleDayCount = coupleDayCount,
therapyDayCount = therapyDayCount,
absentTotal = absentCount,
lateTotal = lateCount,
leaveTotal = sickDayCount +
maternityDayCount +
wifeDayCount +
personalDayCount +
restDayCount +
ordainDayCount +
absentDayCount +
studyDayCount +
agencyDayCount +
coupleDayCount +
therapyDayCount
};
employees.Add(emp);
count++;
}
var result = new
{
template = type.Trim().ToUpper() == "OFFICER" ? "LeaveHalfYear-ขรก" : "LeaveHalfYear-ลูกจ้าง",
reportName = type.Trim().ToUpper() == "OFFICER" ? "LeaveHalfYear-Officer" : "LeaveHalfYear-Employee",
data = new
{
leaveDateStart = req.StartDate.Date.ToThaiShortDate().ToThaiNumber(),
leaveDateEnd = req.EndDate.Date.ToThaiShortDate().ToThaiNumber(),
organizationName = profile.First().Oc ?? "",
employees = employees
}
};
return Success(result);
}
catch (Exception ex)
{
return Error(ex);
}
}
#endregion
}
}

View file

@ -0,0 +1,12 @@
namespace BMA.EHR.Leave.Service.DTOs.Reports
{
public class GetLeaveDetailByNodeReportDto
{
public DateTime StartDate { get; set; } = DateTime.MinValue;
public DateTime EndDate { get; set; } = DateTime.MinValue;
public string? node { get; set; } = string.Empty;
public string? nodeId { get; set; } = string.Empty;
}
}