458 lines
19 KiB
TypeScript
458 lines
19 KiB
TypeScript
import {
|
||
Controller,
|
||
Get,
|
||
Post,
|
||
Put,
|
||
Delete,
|
||
Route,
|
||
Security,
|
||
Tags,
|
||
Body,
|
||
Path,
|
||
Request,
|
||
Query,
|
||
} from "tsoa";
|
||
import { AppDataSource } from "../database/data-source";
|
||
import { Brackets, IsNull, Not } from "typeorm";
|
||
import HttpSuccess from "../interfaces/http-success";
|
||
import HttpError from "../interfaces/http-error";
|
||
import HttpStatusCode from "../interfaces/http-status";
|
||
import { KpiPeriod } from "../entities/kpiPeriod";
|
||
import { KpiUserEvaluation } from "../entities/kpiUserEvaluation";
|
||
import { off } from "process";
|
||
import Extension from "../interfaces/extension";
|
||
import { KpiRole } from "../entities/kpiRole";
|
||
import { KpiPlan } from "../entities/kpiPlan";
|
||
import { KpiUserDevelopment } from "../entities/kpiUserDevelopment";
|
||
|
||
@Route("api/v1/kpi/report")
|
||
@Tags("Report")
|
||
@Security("bearerAuth")
|
||
export class ReportController extends Controller {
|
||
private kpiPeriodRepository = AppDataSource.getRepository(KpiPeriod);
|
||
private kpiUserEvaluationRepository = AppDataSource.getRepository(KpiUserEvaluation);
|
||
private kpiRoleRepository = AppDataSource.getRepository(KpiRole);
|
||
private kpiPlanRepository = AppDataSource.getRepository(KpiPlan);
|
||
private kpiUserDevelopmentRepository = AppDataSource.getRepository(KpiUserDevelopment);
|
||
|
||
@Post("announcement")
|
||
async GetReportAnnouncement(
|
||
@Body()
|
||
requestBody: {
|
||
type: string;
|
||
root?: string | null;
|
||
periodId?: string | null;
|
||
profileId?: string | null;
|
||
// filters?: string | null;
|
||
// keyword?: string | null;
|
||
},
|
||
) {
|
||
let templateName: any;
|
||
let reportName: any;
|
||
let formattedData: any;
|
||
|
||
//KPI1, KPI2, KPI3, KPI7, KPI9
|
||
let data: any;
|
||
let dataKpiUserEvaluations: any
|
||
let period1: any;
|
||
let period2: any;
|
||
|
||
if (requestBody.root && requestBody.periodId) {
|
||
// data = await this.kpiPeriodRepository.createQueryBuilder("period")
|
||
// .leftJoinAndSelect("period.kpiUserEvaluations", "kpiUserEvaluations")
|
||
// .where("period.id = :periodId", { periodId: requestBody.periodId })
|
||
// .andWhere("kpiUserEvaluations.orgId = :orgId", { orgId: requestBody.root })
|
||
// .getOne();
|
||
|
||
// if(data == null || data == undefined){
|
||
// throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการ ของหน่วยงานที่เลือก`);
|
||
// }
|
||
data = await this.kpiPeriodRepository.findOne({
|
||
where: { id: requestBody.periodId }
|
||
})
|
||
dataKpiUserEvaluations = await this.kpiUserEvaluationRepository.find({
|
||
where: {
|
||
kpiPeriodId: String(requestBody.periodId),
|
||
orgId: requestBody.root
|
||
}
|
||
})
|
||
data = {
|
||
id: data.id,
|
||
year: data.year,
|
||
durationKPI: data.durationKPI,
|
||
startDate: data.startDate,
|
||
endDate: data.endDate,
|
||
kpiUserEvaluations: dataKpiUserEvaluations
|
||
}
|
||
|
||
if(data.durationKPI == "APR") {
|
||
period1 = `${Extension.ToThaiFullDate2(data.startDate)} ถึง ${Extension.ToThaiFullDate2(data.endDate)}`
|
||
let _period2 = await this.kpiPeriodRepository.findOne({
|
||
where: {
|
||
year: data.year, durationKPI: "OCR", isActive: true
|
||
}
|
||
});
|
||
period2 = _period2 ? `${Extension.ToThaiFullDate2(_period2?.startDate)} ถึง ${Extension.ToThaiFullDate2(_period2?.endDate)}` : ""
|
||
|
||
}
|
||
else if(data.durationKPI == "OCT"){
|
||
period2 = `${Extension.ToThaiFullDate2(data.startDate)} ถึง ${Extension.ToThaiFullDate2(data.endDate)}`
|
||
let _period1 = await this.kpiPeriodRepository.findOne({
|
||
where: {
|
||
year: data.year, durationKPI: "APR", isActive: true
|
||
}
|
||
});
|
||
period1 = _period1 ? `${Extension.ToThaiFullDate2(_period1?.startDate)} ถึง ${Extension.ToThaiFullDate2(_period1?.endDate)}` : ""
|
||
}
|
||
}
|
||
|
||
if (requestBody.type == "KPI1") {
|
||
templateName = "KPI1";
|
||
reportName = "KPI1";
|
||
/*ROOT*/
|
||
const userEvaluationOrg = await this.kpiUserEvaluationRepository.find({
|
||
where: {
|
||
orgId: String(requestBody.root),
|
||
// child1Id: "" || IsNull(),
|
||
// child2Id: "" || IsNull(),
|
||
// child3Id: "" || IsNull(),
|
||
// child4Id: "" || IsNull(),
|
||
}
|
||
});
|
||
const resultAll = userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(userEvaluationOrg.length.toString())
|
||
: "๐"
|
||
const result = userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(userEvaluationOrg.filter((x:any) => x.evaluationResults != "PENDING").length.toString())
|
||
: "๐"
|
||
const excellent = userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(userEvaluationOrg.filter((x:any) => x.evaluationResults == "EXCELLENT").length.toString())
|
||
: "๐"
|
||
const verygood = userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(userEvaluationOrg.filter((x:any) => x.evaluationResults == "VERY_GOOD").length.toString())
|
||
: "๐"
|
||
const good = userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(userEvaluationOrg.filter((x:any) => x.evaluationResults == "GOOD").length.toString())
|
||
: "๐"
|
||
const fair = userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(userEvaluationOrg.filter((x:any) => x.evaluationResults == "FAIR").length.toString())
|
||
: "๐"
|
||
const improvment = userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(userEvaluationOrg.filter((x:any) => x.evaluationResults == "IMPROVEMENT").length.toString())
|
||
: "๐"
|
||
/*END ROOT*/
|
||
formattedData = {
|
||
year: data?.year ? Extension.ToThaiNumber((data.year+543).toString()) : "",
|
||
period1: Extension.ToThaiNumber(period1),
|
||
period2: Extension.ToThaiNumber(period2),
|
||
durationKPI: data?.durationKPI,
|
||
root: data?.kpiUserEvaluations != null && data?.kpiUserEvaluations.length > 0
|
||
? data?.kpiUserEvaluations[0].orgEvaluator
|
||
: "",
|
||
userEvaluations: [
|
||
{
|
||
no: "๑",
|
||
root: userEvaluationOrg.length > 0
|
||
? userEvaluationOrg[0].orgEvaluator ?? ""
|
||
: "",
|
||
resultAll: resultAll,
|
||
result: result,
|
||
excellent: excellent,
|
||
verygood: verygood,
|
||
good: good,
|
||
fair: fair,
|
||
improvment: improvment,
|
||
}
|
||
]
|
||
}
|
||
}
|
||
if (requestBody.type == "KPI2") {
|
||
templateName = "KPI2";
|
||
reportName = "KPI2";
|
||
const excellent = data.length > 0
|
||
? data.fla((x:any, idx:number) => ({
|
||
no: Extension.ToThaiNumber((idx+1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
org: x.org ? x.org : "",
|
||
})).filter((x:any) => x.evaluationResults == "EXCELLENT")
|
||
: []
|
||
const verygood = data.length > 0
|
||
? data.map((x:any, idx:number) => ({
|
||
no: Extension.ToThaiNumber((idx+1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
org: x.org ? x.org : "",
|
||
})).filter((x:any) => x.evaluationResults == "VERY_GOOD")
|
||
: []
|
||
const good = data.length > 0
|
||
? data.map((x:any, idx:number) => ({
|
||
no: Extension.ToThaiNumber((idx+1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
org: x.org ? x.org : "",
|
||
})).filter((x:any) => x.evaluationResults == "GOOD")
|
||
: []
|
||
const fair = data.length > 0
|
||
? data.map((x:any, idx:number) => ({
|
||
no: Extension.ToThaiNumber((idx+1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
org: x.org ? x.org : "",
|
||
})).filter((x:any) => x.evaluationResults == "FAIR")
|
||
: []
|
||
const improvment = data.length > 0
|
||
? data.map((x:any, idx:number) => ({
|
||
no: Extension.ToThaiNumber((idx+1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
org: x.org ? x.org : "",
|
||
})).filter((x:any) => x.evaluationResults == "IMPROVEMENT")
|
||
: []
|
||
formattedData = {
|
||
year: data?.year ? Extension.ToThaiNumber((data.year+543).toString()) : "",
|
||
period1: Extension.ToThaiNumber(period1),
|
||
period2: Extension.ToThaiNumber(period2),
|
||
durationKPI: data?.durationKPI,
|
||
root: data?.kpiUserEvaluations != null && data?.kpiUserEvaluations.length > 0
|
||
? data?.kpiUserEvaluations[0].orgEvaluator
|
||
: "",
|
||
excellents: {
|
||
count: Extension.ToThaiNumber(excellent.length.toString()),
|
||
data: excellent
|
||
},
|
||
verygoods: {
|
||
count: Extension.ToThaiNumber(verygood.length.toString()),
|
||
data: verygood
|
||
},
|
||
goods: {
|
||
count: Extension.ToThaiNumber(good.length.toString()),
|
||
data: good
|
||
},
|
||
fairs: {
|
||
count: Extension.ToThaiNumber(fair.length.toString()),
|
||
data: fair
|
||
},
|
||
improvments: {
|
||
count: Extension.ToThaiNumber(improvment.length.toString()),
|
||
data: improvment
|
||
},
|
||
}
|
||
}
|
||
if (requestBody.type == "KPI3") {
|
||
templateName = "KPI3";
|
||
reportName = "KPI3";
|
||
const userEvaluations_ = data?.kpiUserEvaluations?.length > 0
|
||
? data.kpiUserEvaluations.map((x:any, idx:number) => ({
|
||
no: Extension.ToThaiNumber((idx+1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
point1: x.summaryPoint >= 90.00 ? Extension.ToThaiNumber(x.summaryPoint.toString()) : "",
|
||
point2: x.summaryPoint >= 80.00 && x.summaryPoint <= 89.99 ? Extension.ToThaiNumber(x.summaryPoint.toString()) : "",
|
||
point3: x.summaryPoint >= 70.00 && x.summaryPoint <= 79.99 ? Extension.ToThaiNumber(x.summaryPoint.toString()) : "",
|
||
point4: x.summaryPoint >= 60.00 && x.summaryPoint <= 69.99 ? Extension.ToThaiNumber(x.summaryPoint.toString()) : "",
|
||
point5: x.summaryPoint < 60.00 ? Extension.ToThaiNumber(x.summaryPoint.toString()) : "",
|
||
remark: ""
|
||
}))
|
||
: []
|
||
// const prefixEvaluator_ = data.kpiUserEvaluations.find((x:any) => x.prefixEvaluator);
|
||
// const firstNameEvaluator_ = data.kpiUserEvaluations.firstNameEvaluator ? data.kpiUserEvaluations.firstNameEvaluator : "";
|
||
// const lastNameEvaluator_ = data.kpiUserEvaluations.lastNameEvaluator ? data.kpiUserEvaluations.lastNameEvaluator : "";
|
||
formattedData = {
|
||
year: data?.year ? Extension.ToThaiNumber((data.year+543).toString()) : "",
|
||
period1: Extension.ToThaiNumber(period1),
|
||
period2: Extension.ToThaiNumber(period2),
|
||
durationKPI: data?.durationKPI,
|
||
root: data?.kpiUserEvaluations != null && data?.kpiUserEvaluations.length > 0
|
||
? data?.kpiUserEvaluations[0].orgEvaluator
|
||
: "",
|
||
fullNameEvaluator: "-",
|
||
positionEvaluator: "-",
|
||
posTypeNameEvaluator: "-",
|
||
posLevelNameEvaluator: "-",
|
||
orgEvaluator: "-",
|
||
|
||
fullNameCommander: "-",
|
||
positionCommander: "-",
|
||
posTypeNameCommander: "-",
|
||
posLevelNameCommander: "-",
|
||
orgCommander: "-",
|
||
|
||
fullNameCommanderHigh: "-",
|
||
positionCommanderHigh: "-",
|
||
posTypeNameCommanderHigh: "-",
|
||
posLevelNameCommanderHigh: "-",
|
||
orgCommanderHigh: "-",
|
||
userEvaluations : userEvaluations_
|
||
}
|
||
}
|
||
if (requestBody.type == "KPI4") {
|
||
templateName = "KPI4";
|
||
reportName = "KPI4";
|
||
//use_filter
|
||
}
|
||
if (requestBody.type == "KPI5") {
|
||
templateName = "KPI5";
|
||
reportName = "KPI5";
|
||
//use_filter
|
||
}
|
||
if (requestBody.type == "KPI6") {
|
||
templateName = "KPI6";
|
||
reportName = "KPI6";
|
||
//use_filter
|
||
}
|
||
if (requestBody.type == "KPI7") {
|
||
templateName = "KPI7";
|
||
reportName = "KPI7";
|
||
formattedData = {
|
||
year: data?.year ? Extension.ToThaiNumber((data.year+543).toString()) : "",
|
||
period1: Extension.ToThaiNumber(period1),
|
||
period2: Extension.ToThaiNumber(period2),
|
||
durationKPI: data?.durationKPI,
|
||
root: data?.kpiUserEvaluations != null && data?.kpiUserEvaluations.length > 0
|
||
? data?.kpiUserEvaluations[0].orgEvaluator
|
||
: "",
|
||
userEvaluations: data.kpiUserEvaluations.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
developName: x.topicEvaluator ? x.topicEvaluator : "",
|
||
developEvaluator: x.developEvaluator ? x.developEvaluator : "",
|
||
target: "", //เป้าหมาย
|
||
timeEvaluator: x.timeEvaluator ? x.timeEvaluator : "",
|
||
developResults: "", //วิธีการวัดผลการพัฒนา
|
||
evaluationResults: x.evaluationResults
|
||
? Extension.EvaluationResult(x.evaluationResults)
|
||
: "",
|
||
})),
|
||
}
|
||
}
|
||
if (requestBody.type == "KPI8") {
|
||
templateName = "KPI8";
|
||
reportName = "KPI8";
|
||
//use_filter
|
||
}
|
||
if (requestBody.type == "KPI9") {
|
||
templateName = "KPI9";
|
||
reportName = "KPI9";
|
||
formattedData = {
|
||
root: data?.kpiUserEvaluations != null && data?.kpiUserEvaluations.length > 0
|
||
? data?.kpiUserEvaluations[0].orgEvaluator
|
||
: "",
|
||
period: data?.durationKPI == "APR" ? "๑": data?.durationKPI == "OCT" ? "๒" : "-",
|
||
year: Extension.ToThaiNumber((data.year+543).toString()),
|
||
userEvaluations: data.kpiUserEvaluations.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
evaluationResults: x.evaluationResults
|
||
? Extension.EvaluationResult(x.evaluationResults)
|
||
: "",
|
||
})),
|
||
}
|
||
}
|
||
|
||
return new HttpSuccess({
|
||
template: templateName,
|
||
reportName: reportName,
|
||
data: formattedData,
|
||
});
|
||
}
|
||
|
||
@Get("kpi-user/{id}")
|
||
async GetReportKpi9(@Path() id?: string) {
|
||
const kpiUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
|
||
relations: ["kpiPeriod"],
|
||
where: { id: id },
|
||
});
|
||
if (!kpiUserEvaluation) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ประเมิน");
|
||
|
||
let formattedData = {
|
||
durationKPI: kpiUserEvaluation.kpiPeriod.durationKPI,
|
||
fullname:
|
||
(kpiUserEvaluation.prefix == null ? "" : kpiUserEvaluation.prefix) +
|
||
"" +
|
||
(kpiUserEvaluation.firstName == null ? "" : kpiUserEvaluation.firstName) +
|
||
" " +
|
||
(kpiUserEvaluation.lastName == null ? "" : kpiUserEvaluation.lastName),
|
||
position: kpiUserEvaluation.position,
|
||
posTypeName: kpiUserEvaluation.posTypeName,
|
||
posLevelName: kpiUserEvaluation.posLevelName,
|
||
org: kpiUserEvaluation.org,
|
||
|
||
fullnameEvaluator:
|
||
(kpiUserEvaluation.prefixEvaluator == null ? "" : kpiUserEvaluation.prefixEvaluator) +
|
||
"" +
|
||
(kpiUserEvaluation.firstNameEvaluator == null ? "" : kpiUserEvaluation.firstNameEvaluator) +
|
||
" " +
|
||
(kpiUserEvaluation.lastNameEvaluator == null ? "" : kpiUserEvaluation.lastNameEvaluator),
|
||
positionEvaluator: kpiUserEvaluation.positionEvaluator,
|
||
posTypeNameEvaluator: kpiUserEvaluation.posTypeNameEvaluator,
|
||
posLevelNameEvaluator: kpiUserEvaluation.posLevelNameEvaluator,
|
||
orgEvaluator: kpiUserEvaluation.orgEvaluator,
|
||
|
||
year: Extension.ToThaiNumber(String(Extension.ToThaiYear(kpiUserEvaluation.kpiPeriod.year))),
|
||
|
||
startDateApr:
|
||
kpiUserEvaluation.kpiPeriod == null ||
|
||
kpiUserEvaluation.kpiPeriod.startDate == null ||
|
||
kpiUserEvaluation.kpiPeriod.durationKPI != "APR"
|
||
? "-"
|
||
: Extension.ToThaiNumber(Extension.ToThaiFullDate(kpiUserEvaluation.kpiPeriod.startDate)),
|
||
endDateApr:
|
||
kpiUserEvaluation.kpiPeriod == null ||
|
||
kpiUserEvaluation.kpiPeriod.endDate == null ||
|
||
kpiUserEvaluation.kpiPeriod.durationKPI != "APR"
|
||
? "-"
|
||
: Extension.ToThaiNumber(Extension.ToThaiFullDate(kpiUserEvaluation.kpiPeriod.endDate)),
|
||
startDateOct:
|
||
kpiUserEvaluation.kpiPeriod == null ||
|
||
kpiUserEvaluation.kpiPeriod.startDate == null ||
|
||
kpiUserEvaluation.kpiPeriod.durationKPI != "OCT"
|
||
? "-"
|
||
: Extension.ToThaiNumber(Extension.ToThaiFullDate(kpiUserEvaluation.kpiPeriod.startDate)),
|
||
endDateOct:
|
||
kpiUserEvaluation.kpiPeriod == null ||
|
||
kpiUserEvaluation.kpiPeriod.endDate == null ||
|
||
kpiUserEvaluation.kpiPeriod.durationKPI != "OCT"
|
||
? "-"
|
||
: Extension.ToThaiNumber(Extension.ToThaiFullDate(kpiUserEvaluation.kpiPeriod.endDate)),
|
||
|
||
evaluationResults: kpiUserEvaluation.evaluationResults,
|
||
totalPoint1: Extension.ToThaiNumber(kpiUserEvaluation.totalPoint1.toLocaleString()),
|
||
totalPoint2: Extension.ToThaiNumber(
|
||
(kpiUserEvaluation.totalPoint2_2 + kpiUserEvaluation.totalPoint2_1).toLocaleString(),
|
||
),
|
||
summaryPoint: Extension.ToThaiNumber(kpiUserEvaluation.summaryPoint.toLocaleString()),
|
||
weightPoint1: Extension.ToThaiNumber(kpiUserEvaluation.weightPoint1.toLocaleString()),
|
||
weightPoint2: Extension.ToThaiNumber(kpiUserEvaluation.weightPoint2.toLocaleString()),
|
||
summaryWeight: Extension.ToThaiNumber(kpiUserEvaluation.summaryWeight.toLocaleString()),
|
||
|
||
topicEvaluator: kpiUserEvaluation.topicEvaluator,
|
||
developEvaluator: kpiUserEvaluation.developEvaluator,
|
||
timeEvaluator: kpiUserEvaluation.timeEvaluator,
|
||
reasonEvaluator: kpiUserEvaluation.reasonEvaluator,
|
||
isReasonCommander: kpiUserEvaluation.isReasonCommander,
|
||
reasonCommander: kpiUserEvaluation.reasonCommander,
|
||
isReasonCommanderHigh: kpiUserEvaluation.isReasonCommanderHigh,
|
||
reasonCommanderHigh: kpiUserEvaluation.reasonCommanderHigh,
|
||
openDate:
|
||
kpiUserEvaluation.openDate == null
|
||
? null
|
||
: Extension.ToThaiNumber(Extension.ToThaiFullDate(kpiUserEvaluation.openDate)),
|
||
};
|
||
|
||
return new HttpSuccess({
|
||
template: "KPIUser",
|
||
reportName: "KPIUser",
|
||
data: formattedData,
|
||
});
|
||
}
|
||
}
|