import { AppDataSource } from "../database/data-source"; import { Body, Example, Get, Path, Post, Put, Request, Response, Route, Security, SuccessResponse, Tags, Delete, Query, } from "tsoa"; import HttpStatusCode from "../interfaces/http-status"; import { Evaluation } from "../entities/Evaluation"; import HttpSuccess from "../interfaces/http-success"; import { EvaluationLogs } from "../entities/EvaluationLogs"; import HttpError from "../interfaces/http-error"; import { Education } from "../entities/Education"; import { Certificate } from "../entities/Certificate"; import { Salary } from "../entities/Salary"; import { Training } from "../entities/Training"; import { Assessment } from "../entities/Assessment"; import { Director } from "../entities/Director"; import { Meeting } from "../entities/Meeting"; import { Brackets } from "typeorm"; import CallAPI from "../interfaces/call-api"; import { RequestWithUser } from "../middlewares/user"; import Extension from "../interfaces/extension"; import { Not } from "typeorm"; @Route("api/v1/evaluation/report") @Tags("report") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถทำรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class ReoportController { private evaluationRepository = AppDataSource.getRepository(Evaluation); private evaluationLogsRepository = AppDataSource.getRepository(EvaluationLogs); private educationRepository = AppDataSource.getRepository(Education); private certificateRepository = AppDataSource.getRepository(Certificate); private salaryRepository = AppDataSource.getRepository(Salary); private trainingRepository = AppDataSource.getRepository(Training); private assessmentRepository = AppDataSource.getRepository(Assessment); private directorRepository = AppDataSource.getRepository(Director); private meetingRepository = AppDataSource.getRepository(Meeting); /** * Report รายงานสรุปจำนวนผลงานการประเมิน * * @summary Report รายงานสรุปจำนวนผลงานการประเมิน * */ @Get() async sumaryEvaluationReport(@Query("year") year?: string, @Query("rootid") rootId?: string, @Query("nameOrg") nameOrg?: string) { const yearInAD = year?year:null; const yearInBE = yearInAD ? (parseInt(yearInAD) - 543).toString() : null; let evaluation = []; evaluation = await AppDataSource.getRepository(Evaluation) .createQueryBuilder("evaluation") .where('evaluation.evaluationResult NOT IN (:...evaluationResults)', { evaluationResults: ['PENDING'] }) .andWhere( yearInBE && yearInBE != null? 'YEAR(createdAt) = :year': "1=1", { year: yearInBE }) .andWhere('evaluation.orgRootId = :rootId',{ rootId: rootId }) .andWhere('evaluation.step = :step', { step: 'DONE' }) .getMany(); const groupedData = evaluation.reduce((acc:any, item) => { const key = item.root; if (!acc[key]) { acc[key] = { agency: item.root && item.root != "" ? item.root: "-", submitter: 0, passCount: 0, notPassCount: 0, }; } if (item.evaluationResult === 'PASS') { acc[key].passCount++; } else if (item.evaluationResult === 'NOTPASS') { acc[key].notPassCount++; } return acc; }, {}); const result = Object.values(groupedData).map((item: any) => ({ ...item , submitter: Extension.ToThaiNumber((item.passCount + item.notPassCount).toString()), passCount: Extension.ToThaiNumber(item.passCount.toString()), notPassCount: Extension.ToThaiNumber(item.notPassCount.toString()), })); const submitter = Object.values(groupedData).reduce((acc: number, item: any) => { return acc + item.passCount + item.notPassCount; }, 0); const sumPass = Object.values(groupedData).reduce((acc: number, item: any) => { return acc + item.passCount; }, 0); const sumNotPass = Object.values(groupedData).reduce((acc: number, item: any) => { return acc + item.notPassCount; }, 0); return new HttpSuccess({ template: "summary-evaluation", reportName: "xlsx-report", data: { year: year?Extension.ToThaiNumber(year.toString()):Extension.ToThaiNumber(new Date().getFullYear().toString()), date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())), mainAgency: rootId && result.length > 0?result[0].agency: rootId && result.length === 0 ? '':'หน่วยงานทั้งหมด', list: result, total: Extension.ToThaiNumber(submitter.toString()), sumPass: Extension.ToThaiNumber(sumPass.toString()), sumNotPass: Extension.ToThaiNumber(sumNotPass.toString()), }, }); } /** * Report ระบบประเมินบุคคล * * @summary Report ระบบประเมินบุคคล (ADMIN & USER) * * @param {string} id id ข้อมูลการประเมิน */ @Get("check-spec-report/{id}") async checkSpecGetReport(@Request() request: RequestWithUser, @Path() id: string) { const evaluation = await AppDataSource.getRepository(Evaluation) .createQueryBuilder("evaluation") .leftJoin("evaluation.education", "education") .leftJoin("evaluation.certificate", "certificate") .leftJoin("evaluation.salaries", "salaries") .leftJoin("evaluation.training", "training") .leftJoin("evaluation.assessment", "assessment") .where("evaluation.id = :id", { id }) .select([ "evaluation.id", "evaluation.userId", "evaluation.subject", "evaluation.isEducationalQft", "evaluation.isGovermantServiceHtr", "evaluation.isOperatingExp", "evaluation.isMinPeriodOfTenure", "evaluation.isHaveSpecificQft", "evaluation.isHaveProLicense", "evaluation.isHaveMinPeriodOrHoldPos", "evaluation.type", "evaluation.prefix", "evaluation.fullName", "evaluation.position", "evaluation.posNo", "evaluation.oc", "evaluation.salary", "evaluation.positionLevel", "evaluation.birthDate", "evaluation.govAge", "evaluation.experience", "education.educationLevel", "education.institute", "education.isDate", "education.startDate", "education.endDate", "education.finishDate", "education.isEducation", "education.degree", "education.field", "education.fundName", "education.gpa", "education.country", "education.other", "education.duration", "education.durationYear", "certificate.certificateType", "certificate.issuer", "certificate.certificateNo", "certificate.issueDate", "certificate.expireDate", "salaries.date", "salaries.amount", "salaries.positionSalaryAmount", "salaries.mouthSalaryAmount", "salaries.position", "salaries.posNo", "salaries.salaryClass", "salaries.salaryRef", "salaries.refCommandNo", "salaries.refCommandDate", "salaries.salaryStatus", "training.name", "training.topic", "training.startDate", "training.endDate", "training.yearly", "training.place", "training.duration", "training.department", "training.numberOrder", "training.dateOrder", "assessment.date", "assessment.point1Total", "assessment.point1", "assessment.point2Total", "assessment.point2", "assessment.pointSumTotal", "assessment.pointSum", ]) .getOne(); if (!evaluation) { return "ไม่พบข้อมูล"; } let root: any; let dateStart: any; let dateRetireLaw: any; let org: any; let commanderFullname: any; let commanderPosition: any; let commanderRootName: any; let commanderOrg: any; let commanderAboveFullname: any; let commanderAbovePosition: any; let commanderAboveRootName: any; let commanderAboveOrg: any; if (!evaluation.userId) { return "ไม่พบข้อมูลผู้ขอประเมิน"; } await new CallAPI() .GetData(request, `/org/profile/keycloak/commander/${evaluation.userId}`) .then(async (x) => { (root = x.root), (dateStart = x.dateStart), (dateRetireLaw = x.dateRetireLaw), (org = x.org), (commanderFullname = x.commanderFullname), (commanderPosition = x.commanderPosition), (commanderRootName = x.commanderRootName), (commanderOrg = x.commanderOrg), (commanderAboveFullname = x.commanderAboveFullname), (commanderAbovePosition = x.commanderAbovePosition), (commanderAboveRootName = x.commanderAboveRootName), (commanderAboveOrg = x.commanderAboveOrg); }) .catch(); const evaluationOld = await this.evaluationRepository.find({ where: { id: Not(id), userId: evaluation.userId, step: "DONE", }, }); let subjectOld = evaluationOld.length > 0 ? evaluationOld.map((x) => x.subject).join(", ") : "ไม่มี"; let thaiYear: number = new Date().getFullYear() + 543; let years = { lastTwoYear: Extension.ToThaiNumber((thaiYear - 2).toString()), lastOneYear: Extension.ToThaiNumber((thaiYear - 1).toString()), currentYear: Extension.ToThaiNumber(thaiYear.toString()), }; const dataEvaluation = { isEducationalQft: evaluation.isEducationalQft, isGovermantServiceHtr: evaluation.isGovermantServiceHtr, isOperatingExp: evaluation.isOperatingExp, isMinPeriodOfTenure: evaluation.isMinPeriodOfTenure, isHaveSpecificQft: evaluation.isHaveSpecificQft, isHaveProLicense: evaluation.isHaveProLicense, isHaveMinPeriodOrHoldPos: evaluation.isHaveMinPeriodOrHoldPos, type: evaluation.type, prefix: evaluation.prefix, fullName: evaluation.fullName ? `${evaluation.fullName}` : "-", position: evaluation.position ? evaluation.position : "-", posNo: evaluation.posNo ? Extension.ToThaiNumber(evaluation.posNo) : "-", oc: evaluation.oc ? evaluation.oc : "-", org: org ? org : "-", //สังกัด root: root ? root : "-", //หน่วยงาน salary: evaluation.salary ? Extension.ToThaiNumber(evaluation.salary) : "-", positionLevel: evaluation.positionLevel ? evaluation.positionLevel : "-", birthDate: evaluation.birthDate != null && evaluation.birthDate != "" ? Extension.ToThaiNumber( Extension.ToThaiShortDate_noPrefix(new Date(evaluation.birthDate)), ) : "-", govAge: evaluation.govAge != null ? Extension.ToThaiNumber(evaluation.govAge) : "-", experience: evaluation.experience ? evaluation.experience : "-", dateStart: dateStart ? Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date(dateStart))) : "-", dateRetireLaw: dateRetireLaw ? Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date(dateRetireLaw))) : "-", subject: evaluation.subject != null ? evaluation.subject : "-", subjectOld: subjectOld, educations: evaluation.education.length > 0 ? evaluation.education.map((education) => ({ educationLevel: education.educationLevel ? Extension.ToThaiNumber(education.educationLevel) : "-", institute: education.institute ? Extension.ToThaiNumber(education.institute) : "-", finishYear: education.finishDate ? Extension.ToThaiNumber( Extension.ToThaiYear(education.finishDate.getFullYear()).toString(), ) : "-", isDate: education.isDate, startDate: education.startDate, endDate: education.endDate, finishDate: education.finishDate ? Extension.ToThaiNumber(Extension.ToThaiShortDate(education.finishDate).toString()) : "-", isEducation: education.isEducation, degree: education.degree ? Extension.ToThaiNumber(education.degree) : "-", field: education.field ? Extension.ToThaiNumber(education.field) : "-", fundName: education.fundName, gpa: education.gpa, country: education.country, other: education.other, duration: education.duration, durationYear: education.durationYear, })) : [ { educationLevel: "-", institute: "-", finishYear: "-", finishDate: "-", degree: "-", field: "-", }, ], certificates: evaluation.certificate.length > 0 ? evaluation.certificate.map((certificate) => ({ certificateType: certificate.certificateType ? Extension.ToThaiNumber(certificate.certificateType) : "-", issuer: certificate.issuer ? Extension.ToThaiNumber(certificate.issuer) : "-", certificateNo: certificate.certificateNo ? Extension.ToThaiNumber(certificate.certificateNo) : "-", issueDate: certificate.issueDate, expireDate: certificate.expireDate, })) : [ { certificateType: "-", issuer: "-", certificateNo: "-", issueDate: "-", expireDate: "-", }, ], salaries: evaluation.salaries.length > 0 ? evaluation.salaries.map((salaries) => ({ date: salaries.date ? Extension.ToThaiNumber(Extension.ToThaiShortDate_noPrefix(salaries.date)) : "-", amount: salaries.amount ? Extension.ToThaiNumber(salaries.amount.toLocaleString()) : "-", position: salaries.position ? Extension.ToThaiNumber(salaries.position) : "-", positionSalaryAmount: salaries.positionSalaryAmount, mouthSalaryAmount: salaries.mouthSalaryAmount, posNo: salaries.posNo, salaryClass: salaries.salaryClass, salaryRef: salaries.salaryRef, refCommandNo: salaries.refCommandNo, refCommandDate: salaries.refCommandDate, salaryStatus: salaries.salaryStatus, })) : [ { date: "-", amount: "-", position: "-", }, ], trainings: evaluation.training.length > 0 ? evaluation.training.map((training) => ({ name: training.name ? Extension.ToThaiNumber(training.name) : "-", topic: training.topic ? Extension.ToThaiNumber(training.topic) : "-", startDate: training.startDate ? Extension.ToThaiNumber(Extension.ToThaiShortDate_noPrefix(training.startDate)) : "-", endDate: training.endDate ? Extension.ToThaiNumber(Extension.ToThaiShortDate_noPrefix(training.endDate)) : "-", yearly: training.yearly ? Extension.ToThaiNumber(training.yearly.toString()) : "-", place: training.place, duration: training.duration, department: training.department, numberOrder: training.numberOrder, dateOrder: training.dateOrder, })) : [ { name: "-", topic: "-", yearly: "-", }, ], assessments: evaluation.assessment.map((assessment) => ({ date: assessment.date, point1Total: assessment.point1Total, point1: assessment.point1, point2Total: assessment.point2Total, point2: assessment.point2, pointSumTotal: assessment.pointSumTotal, pointSum: assessment.pointSum, })), commanderFullname: commanderFullname ? commanderFullname : "-", commanderPosition: commanderPosition ? commanderPosition : "-", commanderRootName: commanderRootName ? commanderRootName : "-", commanderOrg: commanderOrg ? commanderOrg : "-", commanderAboveFullname: commanderAboveFullname ? commanderAboveFullname : "-", commanderAbovePosition: commanderAbovePosition ? commanderAbovePosition : "-", commanderAboveRootName: commanderAboveRootName ? commanderAboveRootName : "-", commanderAboveOrg: commanderAboveOrg ? commanderAboveOrg : "-", years: years, }; if (!dataEvaluation) { return "ไม่พบข้อมูล"; } return new HttpSuccess(dataEvaluation); } }