using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Logging; using TTSW.Controllers; using TTSW.EF; using TTSW.Utils; using TTSW.Constant; using TTSW.Common; using TodoAPI2.Models; using System.Data; using Microsoft.Extensions.Configuration; using System.IO; using System.Net; using iTextSharp.text.pdf; using iTextSharp.text; namespace TodoAPI2.Controllers { //[Authorize] [Produces("application/json")] [Route("api/rep_eva_x")] public class rep_eva_xController : BaseController { #region Private Variables private ILogger _logger; private Irep_eva_xService _repository; private IConfiguration Configuration { get; set; } private Ieva_create_evaluation_detail_processService _process; private Iexternal_linkageService ext; Iexternal_employeeService emp; #endregion #region Properties #endregion /// /// Default constructure for dependency injection /// /// /// /// /// /// /// public rep_eva_xController(ILogger logger, Irep_eva_xService repository, IConfiguration configuration, Ieva_create_evaluation_detail_processService process, Iexternal_linkageService inext, Iexternal_employeeService inemp) { _logger = logger; _repository = repository; _process = process; Configuration = configuration; ext = inext; emp = inemp; } /// /// Get Blank Item /// /// /// /// Return a blank item /// Returns the item /// Error Occurred [HttpGet("GetBlankItem")] [ProducesResponseType(typeof(rep_eva_xWithSelectionViewModel), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult GetBlankItem() { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); var result = _repository.GetBlankItem(); return Ok(result); } catch (Exception ex) { _logger.LogCritical($"Exception in IActionResult GetBlankItem.", ex); return StatusCode(500, $"{ex.Message}"); } } /// /// Download Report /// /// /// /// Return list of items by specifced keyword /// Returns the item /// Error Occurred [HttpGet("rep_eva_p01_report")] [ProducesResponseType(typeof(FileStreamResult), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult rep_eva_p01_report(rep_eva_p01ReportRequestModel model) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); var stream = new MemoryStream(); Document document = new Document(); PdfCopy writer = new PdfCopy(document, stream); document.Open(); var data = GetReportP01(model); PdfReader reader = new PdfReader(data); reader.ConsolidateNamedDestinations(); for (int i = 1; i <= reader.NumberOfPages; i++) { PdfImportedPage page = writer.GetImportedPage(reader, i); writer.AddPage(page); } reader.Close(); writer.Close(); document.Close(); var data2 = stream.ToArray(); var stream2 = new MemoryStream(data2); return File(stream2, model.contentType); } catch (Exception ex) { _logger.LogCritical($"Exception while GetReport.", ex); return StatusCode(500, $"{ex.Message}"); } } private byte[] GetReportP01(rep_eva_p01ReportRequestModel model) { var httpclient = new WebClient(); string mainurl = MyHelper.GetConfig(Configuration, "JasperReportServer:MainURL"); string reportsite = MyHelper.GetConfig(Configuration, "JasperReportServer:reportsite"); string username = MyHelper.GetConfig(Configuration, "JasperReportServer:username"); string password = MyHelper.GetConfig(Configuration, "JasperReportServer:password"); var p1 = GetParameter(model.detail_id); var stream = new MemoryStream(); Document document = new Document(); PdfCopy writer = new PdfCopy(document, stream); document.Open(); string url = $"{mainurl}{reportsite}/rep_eva_p01.{model.filetype}?{MyHelper.GetParameterForJasperReport(p1)}&j_username={username}&j_password={password}"; var data = httpclient.DownloadData(url); PdfReader reader = new PdfReader(data); reader.ConsolidateNamedDestinations(); for (int i = 1; i <= reader.NumberOfPages; i++) { PdfImportedPage page = writer.GetImportedPage(reader, i); writer.AddPage(page); } reader.Close(); writer.Close(); document.Close(); var data2 = stream.ToArray(); return data2; } /// /// Download Report /// /// /// /// Return list of items by specifced keyword /// Returns the item /// Error Occurred [HttpGet("rep_eva_x_report")] [ProducesResponseType(typeof(FileStreamResult), 200)] [ProducesResponseType(400)] [ProducesResponseType(500)] //[ValidateAntiForgeryToken] public IActionResult rep_eva_x_report(rep_eva_xReportRequestModel model) { try { if (!MyHelper.checkAuth(Configuration, HttpContext)) return Unauthorized(); var stream = new MemoryStream(); Document document = new Document(); PdfCopy writer = new PdfCopy(document, stream); document.Open(); foreach (var k in model.detail_id) { var data = GetReport(k, model); PdfReader reader = new PdfReader(data); reader.ConsolidateNamedDestinations(); for (int i = 1; i <= reader.NumberOfPages; i++) { PdfImportedPage page = writer.GetImportedPage(reader, i); writer.AddPage(page); } reader.Close(); if(model.is_print_22 == 1) { rep_eva_p01ReportRequestModel model2 = new rep_eva_p01ReportRequestModel(); model2.filetype = model.filetype; model2.detail_id = k; var data2x = GetReportP01(model2); PdfReader reader2 = new PdfReader(data2x); reader2.ConsolidateNamedDestinations(); for (int i = 1; i <= reader2.NumberOfPages; i++) { PdfImportedPage page = writer.GetImportedPage(reader2, i); writer.AddPage(page); } reader2.Close(); } } writer.Close(); document.Close(); var data2 = stream.ToArray(); var stream2 = new MemoryStream(data2); return File(stream2, model.contentType); } catch (Exception ex) { _logger.LogCritical($"Exception while GetReport.", ex); return StatusCode(500, $"{ex.Message}"); } } private byte[] GetReport(int detail_id, rep_eva_xReportRequestModel model) { var httpclient = new WebClient(); string mainurl = MyHelper.GetConfig(Configuration, "JasperReportServer:MainURL"); string reportsite = MyHelper.GetConfig(Configuration, "JasperReportServer:reportsite"); string username = MyHelper.GetConfig(Configuration, "JasperReportServer:username"); string password = MyHelper.GetConfig(Configuration, "JasperReportServer:password"); var p1 = GetParameter(detail_id); var stream = new MemoryStream(); Document document = new Document(); PdfCopy writer = new PdfCopy(document, stream); document.Open(); var rep_type = new int[] { 1, 2, 3, 4, 5 }; var loginPositionName = (from i in emp.GetAllEmployee() where i.id.ToString() == Request.Cookies["emp_id"] select i.position_name).FirstOrDefault(); foreach (var k in rep_type) { //if (!string.IsNullOrEmpty(loginPositionName)) //{ // if (!(loginPositionName == "นักทรัพยากรบุคคล" || loginPositionName.Contains("ผู้อำนวยการ")) // && (k == 2 || k == 3 || k == 4 || k == 5) // ) // { // continue; // } //} string url = $"{mainurl}{reportsite}/rep_eva_x{k}.{model.filetype}?{MyHelper.GetParameterForJasperReport(p1)}&j_username={username}&j_password={password}"; if(k == 3) { if (p1.chief_fullname == p1.supervisor2_fullname) { url = $"{mainurl}{reportsite}/rep_eva_x{k}A.{model.filetype}?{MyHelper.GetParameterForJasperReport(p1)}&j_username={username}&j_password={password}"; if (!string.IsNullOrEmpty(p1.supervisor3A_fullname)) { url = $"{mainurl}{reportsite}/rep_eva_x{k}AC.{model.filetype}?{MyHelper.GetParameterForJasperReport(p1)}&j_username={username}&j_password={password}"; } if (string.IsNullOrEmpty(p1.supervisor2A_fullname)) { url = $"{mainurl}{reportsite}/rep_eva_x{k}A2.{model.filetype}?{MyHelper.GetParameterForJasperReport(p1)}&j_username={username}&j_password={password}"; } } else { if (!string.IsNullOrEmpty(p1.supervisor2A_fullname) && string.IsNullOrEmpty(p1.supervisor1A_fullname)) { url = $"{mainurl}{reportsite}/rep_eva_x{k}B.{model.filetype}?{MyHelper.GetParameterForJasperReport(p1)}&j_username={username}&j_password={password}"; } else if (string.IsNullOrEmpty(p1.supervisor2A_fullname) && string.IsNullOrEmpty(p1.supervisor1A_fullname)) { url = $"{mainurl}{reportsite}/rep_eva_x{k}B2.{model.filetype}?{MyHelper.GetParameterForJasperReport(p1)}&j_username={username}&j_password={password}"; } else if (string.IsNullOrEmpty(p1.supervisor3A_fullname)) { url = $"{mainurl}{reportsite}/rep_eva_x{k}C.{model.filetype}?{MyHelper.GetParameterForJasperReport(p1)}&j_username={username}&j_password={password}"; if (p1.supervisor1A_fullname == p1.supervisor2A_fullname || string.IsNullOrEmpty(p1.supervisor2A_fullname)) { url = $"{mainurl}{reportsite}/rep_eva_x{k}C2.{model.filetype}?{MyHelper.GetParameterForJasperReport(p1)}&j_username={username}&j_password={password}"; } } } } if (p1.employee_position_type == "อำนวยการ" && k == 1) { url = $"{mainurl}{reportsite}/rep_eva_x{k}a.{model.filetype}?{MyHelper.GetParameterForJasperReport(p1)}&j_username={username}&j_password={password}"; } var data = httpclient.DownloadData(url); PdfReader reader = new PdfReader(data); reader.ConsolidateNamedDestinations(); for (int i = 1; i <= reader.NumberOfPages; i++) { PdfImportedPage page = writer.GetImportedPage(reader, i); writer.AddPage(page); } reader.Close(); } writer.Close(); document.Close(); var data2 = stream.ToArray(); return data2; } private string checkLevel(int? position_type_id, string position_level_text, string name) { if (position_type_id.HasValue) { if(position_type_id == 3 || position_type_id == 4 || position_type_id == 5) { return " " + position_level_text; } } return ""; } private rep_eva_xInputModel2 GetParameter(int detail_id) { var i = new rep_eva_xInputModel2(); i.create_evaluation_detail_id = detail_id; var p = _process.Get(detail_id, null, null); i.employee_fullname = p.employee_fullname; i.employee_code = p.employee_code; i.employee_position_type = p.employee_position_type; i.employee_position_level = p.employee_position_level; i.employee_org = p.employee_org; if (i.employee_org != null) i.employee_org = i.employee_org.Replace("แผนก", ""); i.employee_position = p.employee_position; i.employee_position_full = p.employee_position + checkLevel(p.employee_position_type_id, p.employee_position_level_text, p.employee_fullname); if (!string.IsNullOrEmpty(p.employee_profile_picture)) { i.image_url = MyHelper.GetConfig(Configuration, "SiteInformation:hr_svc") + "/api/image/" + p.employee_profile_picture; // Check if i.image_url exist try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(i.image_url); request.Method = "HEAD"; request.GetResponse(); } catch { i.image_url = null; } } i.chief_fullname = p.chief_fullname; i.chief_position = p.chief_position + checkLevel(p.chief_position_type_id, p.chief_position_level_text, p.chief_fullname); i.supervisor1_remark = p.supervisor1_remark; i.supervisor2_fullname = p.supervisor2_fullname; i.supervisor2_position = p.supervisor2_position + checkLevel(p.supervisor2_position_type_id, p.supervisor2_position_level_text, p.supervisor2_fullname); i.supervisor1A_fullname = p.supervisor1A_fullname; i.supervisor1A_position = p.supervisor1A_position + checkLevel(p.supervisor1A_position_type_id, p.supervisor1A_position_level_text, p.supervisor1A_fullname); i.supervisor2A_fullname = p.supervisor2A_fullname; i.supervisor2A_position = p.supervisor2A_position + checkLevel(p.supervisor2A_position_type_id, p.supervisor2A_position_level_text, p.supervisor2A_fullname); i.supervisor3A_fullname = p.supervisor3A_fullname; i.supervisor3A_position = p.supervisor3A_position + checkLevel(p.supervisor3A_position_type_id, p.supervisor3A_position_level_text, p.supervisor3A_fullname); i.main_dept = p.employee_main_dept; if (i.main_dept != null) i.main_dept = i.main_dept.Replace("กอง",""); i.leave_period = MyHelper.GetDateStringForReport(p.leave_start_date) + " ถึง " + MyHelper.GetDateStringForReport(p.leave_end_date); i.selected_round = p.selected_round; i.plan_remark = p.plan_remark; i.eva_detail_remark = p.eva_detail_remark; var context = _process.GetDataContext(); var all_eva = (from x in context.eva_performance_plan where x.fiscal_year == p.fiscal_year orderby x.theTime select x).ToList(); foreach(var x2 in all_eva) { var start = (from n in context.eva_performance_plan_detail where n.performance_plan_id == x2.id select n.start_date).Min(); var end = (from n in context.eva_performance_plan_detail where n.performance_plan_id == x2.id select n.end_date).Max(); var text = MyHelper.GetDateStringForReport(start) + " ถึง " + MyHelper.GetDateStringForReport(end); if(x2.theTime == 1) { i.round1_text = text; } if (x2.theTime == 2) { i.round2_text = text; } } if (string.IsNullOrEmpty(i.round2_text)) { i.round2_text = $"1 เมษายน {p.fiscal_year} ถึง 30 กันยายน {p.fiscal_year}"; } var detail = (from x in context.eva_create_evaluation_detail where x.id == detail_id select x).FirstOrDefault(); if(detail != null) { //i.total_summary_supervisor2a = detail.total_summary_supervisor2A; //i.final_summary_supervisor2a = detail.Final_summary_supervisor2A; //i.total_summary_competency_supervisor2a = detail.total_summary_competency_supervisor2A; //i.final_summary_competency_supervisor2a = detail.Final_summary_competency_supervisor2A; //i.achievement_supervisor2a = detail.achievement_supervisor2A; //i.competency_supervisor2a = detail.competency_supervisor2A; //i.score_supervisor2a = detail.score_supervisor2A; //i.level_score_supervisor2a = detail.level_score_supervisor2A; checkValue(detail, ref i); } i.w1 = p.create_evaluation_score1; i.w2 = p.create_evaluation_score2; if(p.leave_start_date.HasValue && p.leave_end_date.HasValue) { var q = emp.GetLeaveOfEmployee(p.employee_id.Value, p.leave_start_date, p.leave_end_date); i.sum_day_sick_leave = q.sum_day_sick_leave; i.count_sick_leave = q.count_sick_leave; i.sum_day_personal_leave = q.sum_day_personal_leave; i.count_personal_leave = q.count_personal_leave; i.sum_day_vacation_leave = q.sum_day_vacation_leave; i.count_stop_working = q.count_stop_working; i.count_late_tad_processing_time_results = q.count_late_tad_processing_time_results; i.count_absence_tad_processing_time_results = q.count_absence_tad_processing_time_results; i.sum_day_sick_personal_leave = q.sum_day_sick_personal_leave; i.count_sick_personal_leave = q.count_sick_personal_leave; } i.txt_status_self_a_click_date = MyHelper.GetDateStringForReport(p.status_self_a_click_date); if (i.txt_status_self_a_click_date == "") i.txt_status_self_a_click_date = ".................................."; i.txt_status_chief_a_click_date = MyHelper.GetDateStringForReport(p.status_chief_a_click_date); if (i.txt_status_chief_a_click_date == "") i.txt_status_chief_a_click_date = ".................................."; i.txt_status_self_click_date = MyHelper.GetDateStringForReport(p.status_self_click_date); if (i.txt_status_self_click_date == "") i.txt_status_self_click_date = ".................................."; i.txt_status_chief_click_date = MyHelper.GetDateStringForReport(p.status_chief_click_date); if (i.txt_status_chief_click_date == "") i.txt_status_chief_click_date = ".................................."; i.txt_status_supervisor_click_date = MyHelper.GetDateStringForReport(p.status_supervisor_click_date); if (i.txt_status_supervisor_click_date == "") i.txt_status_supervisor_click_date = ".................................."; i.txt_status_supervisor1A_click_date = MyHelper.GetDateStringForReport(p.status_supervisor1A_click_date); if (i.txt_status_supervisor1A_click_date == "") i.txt_status_supervisor1A_click_date = ".................................."; i.txt_status_supervisor2A_click_date = MyHelper.GetDateStringForReport(p.status_supervisor2A_click_date); if (i.txt_status_supervisor2A_click_date == "") i.txt_status_supervisor2A_click_date = ".................................."; i.txt_status_supervisor3A_click_date = MyHelper.GetDateStringForReport(p.status_supervisor3A_click_date); if (i.txt_status_supervisor3A_click_date == "") i.txt_status_supervisor3A_click_date = ".................................."; i.print_dt = MyHelper.GetDateStringForReport(DateTime.Now) + " " + MyHelper.GetTimeStringFromDate(DateTime.Now); return i; } private void checkValue(eva_create_evaluation_detailEntity detail, ref rep_eva_xInputModel2 i) { if (detail.status_supervisor3A == "Y") { i.total_summary_supervisor2a = detail.total_summary_supervisor3A; i.final_summary_supervisor2a = detail.Final_summary_supervisor3A; i.total_summary_competency_supervisor2a = detail.total_summary_competency_supervisor3A; i.final_summary_competency_supervisor2a = detail.Final_summary_competency_supervisor3A; i.achievement_supervisor2a = detail.achievement_supervisor3A; i.competency_supervisor2a = detail.competency_supervisor3A; i.score_supervisor2a = detail.score_supervisor3A; i.level_score_supervisor2a = detail.level_score_supervisor3A; } else if (detail.status_supervisor2A == "Y") { i.total_summary_supervisor2a = detail.total_summary_supervisor2A; i.final_summary_supervisor2a = detail.Final_summary_supervisor2A; i.total_summary_competency_supervisor2a = detail.total_summary_competency_supervisor2A; i.final_summary_competency_supervisor2a = detail.Final_summary_competency_supervisor2A; i.achievement_supervisor2a = detail.achievement_supervisor2A; i.competency_supervisor2a = detail.competency_supervisor2A; i.score_supervisor2a = detail.score_supervisor2A; i.level_score_supervisor2a = detail.level_score_supervisor2A; } else if (detail.status_supervisor1A == "Y") { i.total_summary_supervisor2a = detail.total_summary_supervisor1A; i.final_summary_supervisor2a = detail.Final_summary_supervisor1A; i.total_summary_competency_supervisor2a = detail.total_summary_competency_supervisor1A; i.final_summary_competency_supervisor2a = detail.Final_summary_competency_supervisor1A; i.achievement_supervisor2a = detail.achievement_supervisor1A; i.competency_supervisor2a = detail.competency_supervisor1A; i.score_supervisor2a = detail.score_supervisor1A; i.level_score_supervisor2a = detail.level_score_supervisor1A; } else if (detail.status_supervisor == "Y" || detail.supervisor2_result == "Y") { i.total_summary_supervisor2a = detail.total_summary_supervisor; i.final_summary_supervisor2a = detail.Final_summary_supervisor; i.total_summary_competency_supervisor2a = detail.total_summary_competency_supervisor; i.final_summary_competency_supervisor2a = detail.Final_summary_competency_supervisor; i.achievement_supervisor2a = detail.achievement_supervisor; i.competency_supervisor2a = detail.competency_supervisor; i.score_supervisor2a = detail.score_supervisor; i.level_score_supervisor2a = detail.level_score_supervisor; } else if (detail.status_chief == "Y") { i.total_summary_supervisor2a = detail.total_summary_chief; i.final_summary_supervisor2a = detail.Final_summary_chief; i.total_summary_competency_supervisor2a = detail.total_summary_competency_chief; i.final_summary_competency_supervisor2a = detail.Final_summary_competency_chief; i.achievement_supervisor2a = detail.achievement_chief; i.competency_supervisor2a = detail.competency_chief; i.score_supervisor2a = detail.score_chief; i.level_score_supervisor2a = detail.level_score_chief; } else { i.total_summary_supervisor2a = null; i.final_summary_supervisor2a = null; i.total_summary_competency_supervisor2a = null; i.final_summary_competency_supervisor2a = null; i.achievement_supervisor2a = null; i.competency_supervisor2a = null; i.score_supervisor2a = null; i.level_score_supervisor2a = null; } } } }