From 348da623709037a353a077868d9087c086d72ae0 Mon Sep 17 00:00:00 2001 From: kittapath Date: Thu, 12 Dec 2024 00:00:43 +0700 Subject: [PATCH] report leave --- .../Repositories/UserProfileRepository.cs | 62 +- .../Controllers/LeaveReportController.cs | 575 ++++++++++++------ .../Reports/GetLeaveDetailByNodeReportDto.cs | 12 + 3 files changed, 461 insertions(+), 188 deletions(-) create mode 100644 BMA.EHR.Leave/DTOs/Reports/GetLeaveDetailByNodeReportDto.cs diff --git a/BMA.EHR.Application/Repositories/UserProfileRepository.cs b/BMA.EHR.Application/Repositories/UserProfileRepository.cs index 54d48828..0922531c 100644 --- a/BMA.EHR.Application/Repositories/UserProfileRepository.cs +++ b/BMA.EHR.Application/Repositories/UserProfileRepository.cs @@ -243,6 +243,66 @@ namespace BMA.EHR.Application.Repositories } } + public async Task> 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(); + + var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body, apiKey); + if (apiResult != null) + { + var raw = JsonConvert.DeserializeObject(apiResult); + if (raw != null) + return raw.Result; + } + + return null; + } + catch + { + throw; + } + } + + public async Task> 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(); + + var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body, apiKey); + if (apiResult != null) + { + var raw = JsonConvert.DeserializeObject(apiResult); + if (raw != null) + return raw.Result; + } + + return null; + } + catch + { + throw; + } + } + public async Task> SearchProfile(string? citizenId, string? firstName, string? lastName, string accessToken) { try @@ -568,7 +628,7 @@ namespace BMA.EHR.Application.Repositories } } - public async Task> GetActiveRootAsync(string? accessToken,Guid? revisionId) + public async Task> GetActiveRootAsync(string? accessToken, Guid? revisionId) { try { diff --git a/BMA.EHR.Leave/Controllers/LeaveReportController.cs b/BMA.EHR.Leave/Controllers/LeaveReportController.cs index 0a579986..6ddd0997 100644 --- a/BMA.EHR.Leave/Controllers/LeaveReportController.cs +++ b/BMA.EHR.Leave/Controllers/LeaveReportController.cs @@ -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 } } - /// - /// LV2_035 - รายงานบัญชีแสดงวันลา ขรก + /// LV2_035 - รายงานสำหรับการลางานของข้าราชการ-กรุงเทพมหานครสามัญ และลูกจ้างประจำกรุงเทพมหานคร /// /// /// /// เมื่อทำรายการสำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน - [HttpPost("leaveday/officer")] + [HttpPost("leaveday/{type:string}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task> GetOfficerLeaveDayReport([FromBody] GetLeaveReportDto req) + public async Task> 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(); + 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(); - - 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 } /// - /// LV2_036 - รายงานบัญชีแสดงวันลา ลูกจ้าง + /// LV2_037 - รายงานการเข้างานจำแนกรายวัน รายสัปดาห์ รายเดือน แยกรายหน่วยงาน/ส่วนราชการ /// /// /// /// เมื่อทำรายการสำเร็จ /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน - [HttpPost("leaveday/employee")] + [HttpPost("time-records/{type:string}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task> GetEmployeeLeaveDayReport([FromBody] GetLeaveReportDto req) + public async Task> 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(); - - // 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(); + 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); - } - } - - - /// - /// LV2_037 - รายงานการลงเวลาประจำวัน - /// - /// - /// - /// เมื่อทำรายการสำเร็จ - /// ไม่ได้ Login เข้าระบบ - /// เมื่อเกิดข้อผิดพลาดในการทำงาน - [HttpPost("time-records/officer")] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task> 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 } } + /// + /// LV2_035 - รายงานการเข้างานสาย จำแนกเป็นรายบุคคลและหรือรายหน่วยงาน/ส่วนราชการ + /// + /// + /// + /// เมื่อทำรายการสำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpPost("late/{type:string}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetLateReport([FromBody] GetLeaveDetailByNodeReportDto req, string type) + { + try + { + var getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_LEAVE_REPORT"); + var jsonData = JsonConvert.DeserializeObject(getPermission); + if (jsonData["status"]?.ToString() != "200") + { + return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); + } + var profile = new List(); + 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(); + + 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); + } + } + + /// + /// LV2_035 - รายงานการลางาน จำแนกตามเพศ ประเภทการลา หน่วยงาน/ส่วนราชการ + /// + /// + /// + /// เมื่อทำรายการสำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpPost("leave2/{type:string}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetLeave2Report([FromBody] GetLeaveDetailByNodeReportDto req, string type) + { + try + { + var getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_LEAVE_REPORT"); + var jsonData = JsonConvert.DeserializeObject(getPermission); + if (jsonData["status"]?.ToString() != "200") + { + return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); + } + var profile = new List(); + 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(); + + 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 + } } \ No newline at end of file diff --git a/BMA.EHR.Leave/DTOs/Reports/GetLeaveDetailByNodeReportDto.cs b/BMA.EHR.Leave/DTOs/Reports/GetLeaveDetailByNodeReportDto.cs new file mode 100644 index 00000000..3081f355 --- /dev/null +++ b/BMA.EHR.Leave/DTOs/Reports/GetLeaveDetailByNodeReportDto.cs @@ -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; + } +} \ No newline at end of file