diff --git a/BMA.EHR.Leave.Service/Controllers/LeaveReportController.cs b/BMA.EHR.Leave.Service/Controllers/LeaveReportController.cs new file mode 100644 index 00000000..ae70d2f1 --- /dev/null +++ b/BMA.EHR.Leave.Service/Controllers/LeaveReportController.cs @@ -0,0 +1,636 @@ +using System.IO.Pipelines; +using BMA.EHR.Application.Repositories; +using BMA.EHR.Application.Repositories.Commands; +using BMA.EHR.Application.Repositories.Leaves.LeaveRequests; +using BMA.EHR.Domain.Common; +using BMA.EHR.Domain.Extensions; +using BMA.EHR.Domain.Models.Leave.Requests; +using BMA.EHR.Domain.Shared; +using BMA.EHR.Infrastructure.Persistence; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Swashbuckle.AspNetCore.Annotations; + +namespace BMA.EHR.Leave.Service.Controllers +{ + [Route("api/v{version:apiVersion}/leave/report")] + [ApiVersion("1.0")] + [ApiController] + [Produces("application/json")] + [Authorize] + [SwaggerTag("API ระบบลงเวลาและการลา (ดึงข้อมูลสำหรับนำไปออกรายงาน)")] + public class LeaveReportController : BaseController + { + #region " Fields " + private readonly LeaveRequestRepository _leaveRequestRepository; + private readonly UserProfileRepository _userProfileRepository; + private readonly CommandRepository _commandRepository; + + + #endregion + + #region " Constructor and Destructor " + + public LeaveReportController(LeaveRequestRepository leaveRequestRepository, + UserProfileRepository userProfileRepository, + CommandRepository commandRepository) + { + _leaveRequestRepository = leaveRequestRepository; + _userProfileRepository = userProfileRepository; + _commandRepository = commandRepository; + } + + #endregion + + #region " Methods " + + #region " Private Methods " + + private async Task GetReport01(LeaveRequest data) + { + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(data.KeycloakUserId); + if (profile == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + var fullName = $"{profile!.Prefix!.Name}{profile!.FirstName} {profile!.LastName}"; + + var lastLeaveRequest = + await _leaveRequestRepository.GetLastLeaveRequestByTypeForUserAsync(data.KeycloakUserId, + data.Type.Id); + + var rootOc = _userProfileRepository.GetRootOcId(profile.OcId ?? Guid.Empty); + var approver = string.Empty; + if (rootOc != null) + { + var list = await _commandRepository.GetOrgApproverAsync(rootOc ?? Guid.Empty); + if (list.Count > 0) + approver = list.First().Name; + } + + return new + { + template = "leave9", + reportName = $"แบบใบลาป่วย ลาคลอดบุตร ลากิจส่วนตัว_{fullName}", + data = new + { + leaveWrote = data.LeaveWrote ?? "", + dateSendLeave = data.CreatedAt.Date.ToThaiShortDate(), + leaveTypeName = data.Type.Name, + dear = approver, + fullname = fullName, + positionName = profile!.Position == null ? "-" : profile!.Position!.Name, + positionLeaveName = profile!.Position == null ? "-" : profile!.Position!.Name, + organizationName = profile!.Oc ?? "", + leaveDetail = data.LeaveDetail, + leaveDateStart = data.LeaveStartDate.Date.ToThaiShortDate(), + leaveDateEnd = data.LeaveEndDate.Date.ToThaiShortDate(), + LeaveTotal = data.LeaveStartDate.DiffDay(data.LeaveEndDate), + leaveAddress = data.LeaveAddress, + leaveNumber = data.LeaveNumber, + LeaveLastStart = lastLeaveRequest == null ? "" : lastLeaveRequest.LeaveStartDate.Date.ToThaiShortDate(), + LeaveLastEnd = lastLeaveRequest == null ? "" : lastLeaveRequest.LeaveEndDate.Date.ToThaiShortDate(), + } + }; + } + + private async Task GetReport02(LeaveRequest data) + { + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(data.KeycloakUserId); + if (profile == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + var fullName = $"{profile!.Prefix!.Name}{profile!.FirstName} {profile!.LastName}"; + + var rootOc = _userProfileRepository.GetRootOcId(profile.OcId ?? Guid.Empty); + var approver = string.Empty; + if (rootOc != null) + { + var list = await _commandRepository.GetOrgApproverAsync(rootOc ?? Guid.Empty); + if (list.Count > 0) + approver = list.First().Name; + } + + return new + { + template = "leave10", + reportName = $"แบบใบลาไปช่วยเหลือภริยาที่คลอดบุตร_{fullName}", + data = new + { + leaveWrote = data.LeaveWrote ?? "", + dateSendLeave = data.CreatedAt.Date.ToThaiShortDate(), + leaveTypeName = data.Type.Name, + dear = approver, + fullname = fullName, + positionName = profile!.Position == null ? "-" : profile!.Position!.Name, + positionLeaveName = profile!.Position == null ? "-" : profile!.Position!.Name, + organizationName = profile!.Oc ?? "", + wifeDayName = data.WifeDayName ?? "", + wifeDayDateBorn = data.WifeDayDateBorn ?? "", + leaveDateStart = data.LeaveStartDate.Date.ToThaiShortDate(), + leaveDateEnd = data.LeaveEndDate.Date.ToThaiShortDate(), + LeaveTotal = data.LeaveStartDate.DiffDay(data.LeaveEndDate), + leaveAddress = data.LeaveAddress, + leaveNumber = data.LeaveNumber, + } + }; + } + + private async Task GetReport03(LeaveRequest data) + { + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(data.KeycloakUserId); + if (profile == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + var fullName = $"{profile!.Prefix!.Name}{profile!.FirstName} {profile!.LastName}"; + + var rootOc = _userProfileRepository.GetRootOcId(profile.OcId ?? Guid.Empty); + var approver = string.Empty; + if (rootOc != null) + { + var list = await _commandRepository.GetOrgApproverAsync(rootOc ?? Guid.Empty); + if (list.Count > 0) + approver = list.First().Name; + } + + return new + { + template = "leave11", + reportName = $"แบบใบลาพักผ่อน_{fullName}", + data = new + { + leaveWrote = data.LeaveWrote ?? "", + dateSendLeave = data.CreatedAt.Date.ToThaiShortDate(), + leaveTypeName = data.Type.Name, + dear = approver, + fullname = fullName, + positionName = profile!.Position == null ? "-" : profile!.Position!.Name, + positionLeaveName = profile!.Position == null ? "-" : profile!.Position!.Name, + organizationName = profile!.Oc ?? "", + + restDayOldTotal = data.RestDayOldTotal, + restDayCurrentTotal = data.RestDayCurrentTotal, + + leaveDateStart = data.LeaveStartDate.Date.ToThaiShortDate(), + leaveDateEnd = data.LeaveEndDate.Date.ToThaiShortDate(), + LeaveTotal = data.LeaveStartDate.DiffDay(data.LeaveEndDate), + leaveAddress = data.LeaveAddress, + leaveNumber = data.LeaveNumber, + } + }; + } + + private async Task GetReport04(LeaveRequest data, bool isHajj = false) + { + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(data.KeycloakUserId); + if (profile == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + var fullName = $"{profile!.Prefix!.Name}{profile!.FirstName} {profile!.LastName}"; + + var rootOc = _userProfileRepository.GetRootOcId(profile.OcId ?? Guid.Empty); + var approver = string.Empty; + if (rootOc != null) + { + var list = await _commandRepository.GetOrgApproverAsync(rootOc ?? Guid.Empty); + if (list.Count > 0) + approver = list.First().Name; + } + + if (isHajj) + { + return new + { + template = "leave13", + reportName = $"แบบใบลาไปประกอบพิธีฮัจย์ ณ เมืองเมกกะ ประเทศซาอุดิอาระเบีย_{fullName}", + data = new + { + leaveWrote = data.LeaveWrote ?? "", + dateSendLeave = data.CreatedAt.Date.ToThaiShortDate(), + leaveTypeName = data.Type.Name, + dear = approver, + fullname = fullName, + positionName = profile!.Position == null ? "-" : profile!.Position!.Name, + positionLeaveName = profile!.Position == null ? "-" : profile!.Position!.Name, + organizationName = profile!.Oc ?? "", + + + leavegovernmentDate = data.LeaveGovernmentDate == null ? "" : data.LeaveGovernmentDate.Value.Date.ToThaiShortDate(), + hajjDayStatus = data.HajjDayStatus, + + leaveDateStart = data.LeaveStartDate.Date.ToThaiShortDate(), + leaveDateEnd = data.LeaveEndDate.Date.ToThaiShortDate(), + LeaveTotal = data.LeaveStartDate.DiffDay(data.LeaveEndDate), + + } + }; + } + else + { + return new + { + template = "leave12", + reportName = $"แบบใบลาอุปสมบท_{fullName}", + data = new + { + leaveWrote = data.LeaveWrote ?? "", + dateSendLeave = data.CreatedAt.Date.ToThaiShortDate(), + leaveTypeName = data.Type.Name, + dear = approver, + fullname = fullName, + positionName = profile!.Position == null ? "-" : profile!.Position!.Name, + positionLeaveName = profile!.Position == null ? "-" : profile!.Position!.Name, + organizationName = profile!.Oc ?? "", + + leavebirthDate = data.LeaveBirthDate == null ? "" : data.LeaveBirthDate.Value.Date.ToThaiShortDate(), + leavegovernmentDate = data.LeaveGovernmentDate == null ? "" : data.LeaveGovernmentDate.Value.Date.ToThaiShortDate(), + ordainDayStatus = data.OrdainDayStatus, + ordainDayLocationName = data.OrdainDayLocationName, + ordainDayLocationAddress = data.OrdainDayLocationAddress, + ordainDayLocationNumber = data.OrdainDayLocationNumber, + ordainDayOrdination = data.OrdainDayOrdination, + ordainDayBuddhistLentName = data.OrdainDayBuddhistLentName, + ordainDayBuddhistLentAddress = data.OrdainDayBuddhistLentAddress, + + leaveDateStart = data.LeaveStartDate.Date.ToThaiShortDate(), + leaveDateEnd = data.LeaveEndDate.Date.ToThaiShortDate(), + LeaveTotal = data.LeaveStartDate.DiffDay(data.LeaveEndDate), + + } + }; + } + } + + private async Task GetReport05(LeaveRequest data) + { + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(data.KeycloakUserId); + if (profile == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + var fullName = $"{profile!.Prefix!.Name}{profile!.FirstName} {profile!.LastName}"; + + var rootOc = _userProfileRepository.GetRootOcId(profile.OcId ?? Guid.Empty); + var approver = string.Empty; + if (rootOc != null) + { + var list = await _commandRepository.GetOrgApproverAsync(rootOc ?? Guid.Empty); + if (list.Count > 0) + approver = list.First().Name; + } + + return new + { + template = "leave14", + reportName = $"แบบใบลาเข้ารับการตรวจเลือก หรือเข้ารับการเตรียมพล_{fullName}", + data = new + { + leaveWrote = data.LeaveWrote ?? "", + dateSendLeave = data.CreatedAt.Date.ToThaiShortDate(), + leaveTypeName = data.Type.Name, + dear = approver, + fullname = fullName, + positionName = profile!.Position == null ? "-" : profile!.Position!.Name, + positionLeaveName = profile!.Position == null ? "-" : profile!.Position!.Name, + organizationName = profile!.Oc ?? "", + + absentDaySummon = data.AbsentDaySummon, + absentDayLocation = data.AbsentDayLocation, + absentDayRegistorDate = data.AbsentDayRegistorDate.Date.ToThaiShortDate(), + absentDayGetIn = data.AbsentDayGetIn, + absentDayAt = data.AbsentDayAt, + + leaveDateStart = data.LeaveStartDate.Date.ToThaiShortDate(), + leaveDateEnd = data.LeaveEndDate.Date.ToThaiShortDate(), + LeaveTotal = data.LeaveStartDate.DiffDay(data.LeaveEndDate), + + } + }; + } + + private async Task GetReport06(LeaveRequest data) + { + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(data.KeycloakUserId); + if (profile == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + var fullName = $"{profile!.Prefix!.Name}{profile!.FirstName} {profile!.LastName}"; + + var rootOc = _userProfileRepository.GetRootOcId(profile.OcId ?? Guid.Empty); + var approver = string.Empty; + if (rootOc != null) + { + var list = await _commandRepository.GetOrgApproverAsync(rootOc ?? Guid.Empty); + if (list.Count > 0) + approver = list.First().Name; + } + + return new + { + template = "leave15", + reportName = $"แบบใบลาไปศึกษา ฝึกอบรม ปฏิบัติการวิจัย หรือดูงาน_{fullName}", + data = new + { + leaveWrote = data.LeaveWrote ?? "", + dateSendLeave = data.CreatedAt.Date.ToThaiShortDate(), + leaveTypeName = data.Type.Name, + dear = approver, + fullname = fullName, + positionName = profile!.Position == null ? "-" : profile!.Position!.Name, + positionLeaveName = profile!.Position == null ? "-" : profile!.Position!.Name, + organizationName = profile!.Oc ?? "", + + leavebirthDate = data.LeaveBirthDate == null ? "" : data.LeaveBirthDate.Value.Date.ToThaiShortDate(), + leavegovernmentDate = data.LeaveGovernmentDate == null ? "" : data.LeaveGovernmentDate.Value.Date.ToThaiShortDate(), + leaveSalary = data.LeaveSalary, + leaveSalaryText = data.LeaveSalaryText, + studyDaySubject = data.StudyDaySubject ?? "", + studyDayDegreeLevel = data.StudyDayDegreeLevel ?? "", + studyDayUniversityName = data.StudyDayUniversityName ?? "", + studyDayCountry = data.StudyDayCountry ?? "", + studyDayScholarship = data.StudyDayScholarship ?? "", + studyDayTrainingSubject = data.StudyDayTrainingSubject ?? "", + studyDayTrainingName = data.StudyDayTrainingName ?? "", + + leaveDateStart = data.LeaveStartDate.Date.ToThaiShortDate(), + leaveDateEnd = data.LeaveEndDate.Date.ToThaiShortDate(), + LeaveTotal = data.LeaveStartDate.DiffDay(data.LeaveEndDate), + leaveAddress = data.LeaveAddress, + leaveNumber = data.LeaveNumber, + } + }; + } + + private async Task GetReport07(LeaveRequest data) + { + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(data.KeycloakUserId); + if (profile == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + var fullName = $"{profile!.Prefix!.Name}{profile!.FirstName} {profile!.LastName}"; + + var rootOc = _userProfileRepository.GetRootOcId(profile.OcId ?? Guid.Empty); + var approver = string.Empty; + if (rootOc != null) + { + var list = await _commandRepository.GetOrgApproverAsync(rootOc ?? Guid.Empty); + if (list.Count > 0) + approver = list.First().Name; + } + + return new + { + template = "leave16", + reportName = $"แบบใบลาไปปฏิบัติงานในองค์การระหว่างประเทศ_{fullName}", + data = new + { + leaveWrote = data.LeaveWrote ?? "", + dateSendLeave = data.CreatedAt.Date.ToThaiShortDate(), + leaveTypeName = data.Type.Name, + dear = approver, + fullname = fullName, + fullnameEng = "", + positionName = profile!.Position == null ? "-" : profile!.Position!.Name, + positionLeaveName = profile!.Position == null ? "-" : profile!.Position!.Name, + organizationName = profile!.Oc ?? "", + + leaveDateStart = data.LeaveStartDate.Date.ToThaiShortDate(), + leaveDateEnd = data.LeaveEndDate.Date.ToThaiShortDate(), + + } + }; + } + + private async Task GetReport08(LeaveRequest data) + { + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(data.KeycloakUserId); + if (profile == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + var fullName = $"{profile!.Prefix!.Name}{profile!.FirstName} {profile!.LastName}"; + + var rootOc = _userProfileRepository.GetRootOcId(profile.OcId ?? Guid.Empty); + var approver = string.Empty; + if (rootOc != null) + { + var list = await _commandRepository.GetOrgApproverAsync(rootOc ?? Guid.Empty); + if (list.Count > 0) + approver = list.First().Name; + } + + return new + { + template = "leave17", + reportName = $"แบบใบลาติดตามคู่สมรส_{fullName}", + data = new + { + leaveWrote = data.LeaveWrote ?? "", + dateSendLeave = data.CreatedAt.Date.ToThaiShortDate(), + leaveTypeName = data.Type.Name, + dear = approver, + fullname = fullName, + positionName = profile!.Position == null ? "-" : profile!.Position!.Name, + positionLeaveName = profile!.Position == null ? "-" : profile!.Position!.Name, + organizationName = profile!.Oc ?? "", + + leaveSalary = data.LeaveSalary, + leaveSalaryText = data.LeaveSalaryText, + coupleDayName = data.CoupleDayName, + coupleDayLevel = data.CoupleDayLevel, + coupleDayPosition = data.CoupleDayPosition, + coupleDayOrganizationName = "-", + coupleDayLevelCountry = data.CoupleDayLevelCountry, + coupleDayCountryHistory = data.CoupleDayCountryHistory, + coupleDayTotalHistory = data.CoupleDayTotalHistory, + coupleDayStartDateHistory = data.CoupleDayStartDateHistory.Date.ToThaiShortDate(), + coupleDayEndDateHistory = data.CoupleDayEndDateHistory.Date.ToThaiShortDate(), + coupleDaySumTotalHistory = data.CoupleDaySumTotalHistory, + + + leaveDateStart = data.LeaveStartDate.Date.ToThaiShortDate(), + leaveDateEnd = data.LeaveEndDate.Date.ToThaiShortDate(), + LeaveTotal = data.LeaveStartDate.DiffDay(data.LeaveEndDate), + } + }; + } + + private async Task GetReport09(LeaveRequest data) + { + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(data.KeycloakUserId); + if (profile == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + var fullName = $"{profile!.Prefix!.Name}{profile!.FirstName} {profile!.LastName}"; + + var rootOc = _userProfileRepository.GetRootOcId(profile.OcId ?? Guid.Empty); + var approver = string.Empty; + if (rootOc != null) + { + var list = await _commandRepository.GetOrgApproverAsync(rootOc ?? Guid.Empty); + if (list.Count > 0) + approver = list.First().Name; + } + + return new + { + template = "leave18", + reportName = $"แบบใบลาไปฟื้นฟูสมรรถภาพด้านอาชีพ_{fullName}", + data = new + { + leaveWrote = data.LeaveWrote ?? "", + dateSendLeave = data.CreatedAt.Date.ToThaiShortDate(), + leaveTypeName = data.Type.Name, + dear = approver, + fullname = fullName, + positionName = profile!.Position == null ? "-" : profile!.Position!.Name, + positionLeaveName = profile!.Position == null ? "-" : profile!.Position!.Name, + organizationName = profile!.Oc ?? "", + + + + + leaveDateStart = data.LeaveStartDate.Date.ToThaiShortDate(), + leaveDateEnd = data.LeaveEndDate.Date.ToThaiShortDate(), + } + }; + } + + #endregion + + [HttpGet("{id:guid}")] + public async Task> GetReport(Guid id, [FromQuery] bool hajj = false) + { + try + { + var data = await _leaveRequestRepository.GetByIdAsync(id); + if (data == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + dynamic result = new { }; + switch (data.Type.Code.Trim().ToUpper()) + { + case "LV-001": + case "LV-002": + case "LV-003": + { + result = await GetReport01(data); + break; + } + case "LV-004": + { + result = await GetReport02(data); + break; + } + case "LV-005": + { + result = await GetReport03(data); + break; + } + case "LV-006": + { + result = await GetReport04(data, hajj); + break; + } + case "LV-007": + { + result = await GetReport05(data); + break; + } + case "LV-008": + { + result = await GetReport06(data); + break; + } + case "LV-009": + { + result = await GetReport07(data); + break; + } + case "LV-010": + { + result = await GetReport08(data); + break; + } + case "LV-011": + { + result = await GetReport09(data); + break; + } + } + + return Success(result); + } + catch (Exception ex) + { + return Error(ex); + } + } + + [HttpGet("reject/{id:guid}")] + public async Task> GetCancelReport(Guid id) + { + try + { + var data = await _leaveRequestRepository.GetByIdAsync(id); + if (data == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + var profile = await _userProfileRepository.GetProfileByKeycloakIdAsync(data.KeycloakUserId); + if (profile == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + var fullName = $"{profile!.Prefix!.Name}{profile!.FirstName} {profile!.LastName}"; + + var result = new + { + template = "แบบใบขอยกเลิกวันลา", + reportName = $"แบบใบขอยกเลิกวันลา_{fullName}", + data = new + { + leaveWrote = data.CancelLeaveWrote ?? "", + dateSendLeave = data.CreatedAt.Date.ToThaiShortDate(), + leaveTypeName = data.Type.Name, + fullname = fullName, + positionName = profile!.Position == null ? "-" : profile!.Position!.Name, + positionLeaveName = profile!.Position == null ? "-" : profile!.Position!.Name, + organizationName = profile!.Oc ?? "", + leaveDateStart = data.LeaveStartDate.Date.ToThaiShortDate(), + leaveDateEnd = data.LeaveEndDate.Date.ToThaiShortDate() + } + }; + + return Success(result); + + + } + catch (Exception ex) + { + return Error(ex); + } + } + + #endregion + } +} \ No newline at end of file