diff --git a/BMA.EHR.Application/ApplicationServicesRegistration.cs b/BMA.EHR.Application/ApplicationServicesRegistration.cs index 502acb37..911d01eb 100644 --- a/BMA.EHR.Application/ApplicationServicesRegistration.cs +++ b/BMA.EHR.Application/ApplicationServicesRegistration.cs @@ -1,6 +1,7 @@ using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Repositories.Commands; using BMA.EHR.Application.Repositories.MessageQueue; +using BMA.EHR.Application.Repositories.Reports; using Microsoft.Extensions.DependencyInjection; namespace BMA.EHR.Application @@ -23,6 +24,7 @@ namespace BMA.EHR.Application services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); return services; } diff --git a/BMA.EHR.Application/Repositories/Reports/RetireReportRepository.cs b/BMA.EHR.Application/Repositories/Reports/RetireReportRepository.cs index 672d378b..53f7141e 100644 --- a/BMA.EHR.Application/Repositories/Reports/RetireReportRepository.cs +++ b/BMA.EHR.Application/Repositories/Reports/RetireReportRepository.cs @@ -1,13 +1,10 @@ using BMA.EHR.Application.Common.Interfaces; using BMA.EHR.Domain.Extensions; using BMA.EHR.Domain.Models.Retirement; -using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +//using Telerik.Reporting; +//using Telerik.Reporting.Processing; namespace BMA.EHR.Application.Repositories.Reports { @@ -16,46 +13,137 @@ namespace BMA.EHR.Application.Repositories.Reports #region " Fields " private readonly IApplicationDBContext _dbContext; + private readonly IWebHostEnvironment _hostingEnvironment; #endregion #region " Constructor and Destructor " - public RetireReportRepository(IApplicationDBContext dbContext) + public RetireReportRepository(IApplicationDBContext dbContext, IWebHostEnvironment hostEnvironment) { _dbContext = dbContext; + _hostingEnvironment = hostEnvironment; + } #endregion #region " Methods " - public async Task> GetListRetirePeriodAsync() + //public async Task> GetListRetirePeriodAsync(Guid Id) + //{ + // try + // { + // // 1. query + // //var data = await _dbContext.Set() + // // .Include(x => x.RetirementProfiles) + // // .ThenInclude(x => x.Profile) + // // .ToListAsync(); + + // // 2. data not found throw exception + + // // 3. Load Report File + + // //return data; + // } + // catch + // { + // throw; + // } + //} + + public async Task GetProfileRetirementdAsync(Guid retireId) { - try + var retire = await _dbContext.Set() + .Include(x => x.RetirementProfiles) + .FirstOrDefaultAsync(x => x.Id == retireId); + if (retire == null) { - // 1. query - var data = await _dbContext.Set() - .Include(x => x.RetirementPeriodHistorys) - .Include(x => x.RetirementProfiles) - .ThenInclude(x => x.Profile) - .ToListAsync(); + var retireHistorys = await _dbContext.Set().AsQueryable() + .FirstOrDefaultAsync(x => x.Id == retireId); + if (retireHistorys == null) + return null; - // 2. data not found throw exception - - - // 3. Load Report File - - - - return data; + var profile_retireHistory = await _dbContext.Set() + .Where(x => x.RetirementPeriod == retire) + .OrderBy(x => x.Order) + .Select(x => new + { + order = x.Order, + id = x.Id, + reason = x.Reason, + remove = x.Remove, + profileId = x.Profile.Id, + citizenId = x.Profile.CitizenId, + prefix = x.Profile.Prefix == null ? null : x.Profile.Prefix.Name, + fullName = $"{x.Profile.FirstName} {x.Profile.LastName}", + organizationOrganization = x.Profile.OrganizationOrganization, + oc = x.Profile.Oc, + position = x.Profile.Position == null ? null : x.Profile.Position.Name, + positionType = x.Profile.PositionType == null ? null : x.Profile.PositionType.Name, + positionExecutive = x.Profile.PositionExecutive, + posNo = x.Profile.PosNo == null ? null : x.Profile.PosNo.Name, + positionEmployeePosition = x.Profile.PositionEmployeePosition, + positionEmployeeLevel = x.Profile.PositionEmployeeLevel, + positionEmployeeGroup = x.Profile.PositionEmployeeGroup, + posNoEmployee = x.Profile.PosNoEmployee, + }) + .ToListAsync(); + return new { retireHistorys.Id, retireHistorys.CreatedAt, Year = retireHistorys.Year.ToThaiYear().ToString().ToThaiNumber(), retireHistorys.Round, retireHistorys.Type, retireHistorys.TypeReport, Total = retireHistorys.Total.ToString().ToThaiNumber(), profile = profile_retireHistory }; } - catch + else { - throw; + var profile_retire = await _dbContext.Set() + .Where(x => x.RetirementPeriod == retire) + .OrderBy(x => x.Order) + .Select(x => new + { + order = x.Order, + id = x.Id, + reason = x.Reason, + remove = x.Remove, + profileId = x.Profile.Id, + citizenId = x.Profile.CitizenId, + prefix = x.Profile.Prefix == null ? null : x.Profile.Prefix.Name, + fullName = $"{x.Profile.FirstName} {x.Profile.LastName}", + organizationOrganization = x.Profile.OrganizationOrganization, + oc = x.Profile.Oc, + position = x.Profile.Position == null ? null : x.Profile.Position.Name, + positionType = x.Profile.PositionType == null ? null : x.Profile.PositionType.Name, + positionExecutive = x.Profile.PositionExecutive, + posNo = x.Profile.PosNo == null ? null : x.Profile.PosNo.Name, + positionEmployeePosition = x.Profile.PositionEmployeePosition, + positionEmployeeLevel = x.Profile.PositionEmployeeLevel, + positionEmployeeGroup = x.Profile.PositionEmployeeGroup, + posNoEmployee = x.Profile.PosNoEmployee, + }) + .ToListAsync(); + + return new { retire.Id, retire.CreatedAt, Year = retire.Year.ToThaiYear().ToString().ToThaiNumber(), retire.Round, retire.Type, retire.TypeReport, Total = profile_retire.Count.ToString().ToThaiNumber(), profile = profile_retire }; } + + //Report + //var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Reports", $"32-ประกาศเกษียณลูกจ้างประจำ.trdp"); + //ReportPackager reportPacker = new ReportPackager(); + //Telerik.Reporting.Report? report = null; + + //using (var sourceStream = System.IO.File.OpenRead(rptFile)) + //{ + // report = (Telerik.Reporting.Report)reportPacker.UnpackageDocument(sourceStream); + //} + + //System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable(); + //InstanceReportSource instanceReportSource = new InstanceReportSource() + //{ + // ReportDocument = report, + //}; + + //ReportProcessor reportProcessor = new ReportProcessor(_configuration); + //RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo); + //var content = result.DocumentBytes; + //return File(content, "application/pdf", $"ประกาศเกษียณลูกจ้างประจำ_{null}.pdf"); } #endregion diff --git a/BMA.EHR.Domain/Extensions/IntegerExtension.cs b/BMA.EHR.Domain/Extensions/IntegerExtension.cs index fc1a1857..730132fc 100644 --- a/BMA.EHR.Domain/Extensions/IntegerExtension.cs +++ b/BMA.EHR.Domain/Extensions/IntegerExtension.cs @@ -1,3 +1,5 @@ +using System.Text; + namespace BMA.EHR.Domain.Extensions { public static class IntegerExtension @@ -40,5 +42,25 @@ namespace BMA.EHR.Domain.Extensions { return number.ToString("#,##0"); } + + public static string ToThaiNumber(this string value) + { + string arabicNumbers = "0123456789"; + string thaiNumbers = "๐๑๒๓๔๕๖๗๘๙"; + StringBuilder ThaiYear = new StringBuilder(); + foreach (char digit in value) + { + int index = arabicNumbers.IndexOf(digit); + if (index >= 0) + { + ThaiYear.Append(thaiNumbers[index]); + } + else + { + ThaiYear.Append(digit); + } + } + return ThaiYear.ToString(); + } } } \ No newline at end of file diff --git a/BMA.EHR.Report.Service/Controllers/RetireReportController.cs b/BMA.EHR.Report.Service/Controllers/RetireReportController.cs index 918cbfce..27b829c7 100644 --- a/BMA.EHR.Report.Service/Controllers/RetireReportController.cs +++ b/BMA.EHR.Report.Service/Controllers/RetireReportController.cs @@ -1,18 +1,88 @@ using BMA.EHR.Domain.Common; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; +using BMA.EHR.Domain.Extensions; +using BMA.EHR.Application.Repositories.Reports; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; +using Telerik.Reporting; +using Telerik.Reporting.Processing; namespace BMA.EHR.Report.Service.Controllers { [Route("api/v{version:apiVersion}/report/retire")] - [ApiVersion("2.0")] + [ApiVersion("1.0")] [ApiController] [Produces("application/json")] - [Authorize] + //[Authorize] [SwaggerTag("API รายงานระบบเกษียณ")] public class RetireReportController : BaseController { + private readonly RetireReportRepository _service; + private readonly IWebHostEnvironment _hostingEnvironment; + private readonly IConfiguration _configuration; + + public RetireReportController(RetireReportRepository service, IWebHostEnvironment hostingEnvironment, IConfiguration configuration) + { + _service = service; + _hostingEnvironment = hostingEnvironment; + _configuration = configuration; + } + + [HttpGet("{Id}")] + public async Task> GetProfileRetirement([FromRoute] Guid Id) + { + var retire = await _service.GetProfileRetirementdAsync(Id); + if (retire == null) + { + return NotFound(retire); + } + else + { + var rptFile = Path.Combine(_hostingEnvironment.ContentRootPath, "Reports", $"32-ประกาศเกษียณลูกจ้างประจำ.trdp"); + ReportPackager reportPacker = new ReportPackager(); + Telerik.Reporting.Report? report = null; + using (var sourceStream = System.IO.File.OpenRead(rptFile)) + { + report = (Telerik.Reporting.Report)reportPacker.UnpackageDocument(sourceStream); + } + //Add Parameter + report.ReportParameters["Year"].Value = retire.GetType().GetProperty("Year").GetValue(retire); + report.ReportParameters["Total"].Value = retire.GetType().GetProperty("Total").GetValue(retire); + + var _profileList = new List(); + + foreach (var profile in retire.GetType().GetProperty("profile").GetValue(retire)) + { + string thaiOrder = profile.GetType().GetProperty("order").GetValue(profile).ToString()+"."; + thaiOrder = thaiOrder.ToThaiNumber(); + _profileList.Add(new + { + order = thaiOrder, + fullName = profile.GetType().GetProperty("fullName").GetValue(profile).ToString(), + position = profile.GetType().GetProperty("position").GetValue(profile).ToString(), + posNo = profile.GetType().GetProperty("posNo").GetValue(profile).ToString(), + organizationOrganization = profile.GetType().GetProperty("organizationOrganization").GetValue(profile).ToString(), + }); + } + + //Binding to Table + var tblProfile = (Telerik.Reporting.Table)report.Items["detailSection1"].Items["table1"]; + tblProfile.DataSource = _profileList; + + + System.Collections.Hashtable deviceInfo = new System.Collections.Hashtable(); + InstanceReportSource instanceReportSource = new InstanceReportSource() + { + ReportDocument = report, + }; + + ReportProcessor reportProcessor = new ReportProcessor(_configuration); + RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, deviceInfo); + var content = result.DocumentBytes; + return File(content, "application/pdf", $"ประกาศเกษียณลูกจ้างประจำ{null}.pdf"); + } + //return Success(retire); + } + + } } diff --git a/BMA.EHR.Report.Service/Reports/32-ประกาศเกษียณลูกจ้างประจำ.trdp b/BMA.EHR.Report.Service/Reports/32-ประกาศเกษียณลูกจ้างประจำ.trdp index a0cc5f8e..bbb55bcf 100644 Binary files a/BMA.EHR.Report.Service/Reports/32-ประกาศเกษียณลูกจ้างประจำ.trdp and b/BMA.EHR.Report.Service/Reports/32-ประกาศเกษียณลูกจ้างประจำ.trdp differ