1544 lines
62 KiB
TypeScript
1544 lines
62 KiB
TypeScript
import { Controller, Get, Post, Route, Security, Tags, Body, Path, Request } from "tsoa";
|
||
import { AppDataSource } from "../database/data-source";
|
||
import { In } 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 Extension from "../interfaces/extension";
|
||
import { KpiRole } from "../entities/kpiRole";
|
||
import { KpiPlan } from "../entities/kpiPlan";
|
||
import { KpiUserDevelopment } from "../entities/kpiUserDevelopment";
|
||
import CallAPI from "../interfaces/call-api";
|
||
@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(
|
||
@Request() request: { user: Record<string, any> },
|
||
@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;
|
||
let rootName: any
|
||
if (requestBody.root && requestBody.periodId) {
|
||
data = await this.kpiPeriodRepository.findOne({
|
||
where: { id: requestBody.periodId },
|
||
});
|
||
dataKpiUserEvaluations = await this.kpiUserEvaluationRepository.find({
|
||
where: {
|
||
kpiPeriodId: requestBody.periodId,
|
||
orgId: requestBody.root,
|
||
evaluationStatus: "KP7",
|
||
},
|
||
});
|
||
await new CallAPI()
|
||
.GetData(request, `/org/root/${requestBody.root}`)
|
||
.then((x) => {
|
||
rootName = x.orgRootName;
|
||
});
|
||
data = {
|
||
id: data.id,
|
||
year: data.year,
|
||
durationKPI: data.durationKPI,
|
||
startDate: data.startDate,
|
||
endDate: data.endDate,
|
||
kpiUserEvaluations: dataKpiUserEvaluations,
|
||
rootName: rootName
|
||
};
|
||
|
||
if (data.durationKPI == "APR") {
|
||
period1 = `${Extension.ToThaiFullDate2(data.startDate)} ถึง ${Extension.ToThaiFullDate2(data.endDate)}`;
|
||
let _period2 = await this.kpiPeriodRepository.findOne({
|
||
where: {
|
||
year: data.year,
|
||
durationKPI: "OCT",
|
||
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),
|
||
kpiPeriodId: String(requestBody?.periodId),
|
||
},
|
||
});
|
||
const resultAll =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(userEvaluationOrg.length.toString())
|
||
: "๐";
|
||
const result =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(
|
||
userEvaluationOrg.filter((x: any) => x.evaluationStatus == "KP7").length.toString(),
|
||
)
|
||
: "๐";
|
||
const excellent =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(
|
||
userEvaluationOrg
|
||
.filter(
|
||
(x: any) => x.evaluationResults == "EXCELLENT" && x.evaluationStatus == "KP7",
|
||
)
|
||
.length.toString(),
|
||
)
|
||
: "๐";
|
||
const verygood =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(
|
||
userEvaluationOrg
|
||
.filter(
|
||
(x: any) => x.evaluationResults == "VERY_GOOD" && x.evaluationStatus == "KP7",
|
||
)
|
||
.length.toString(),
|
||
)
|
||
: "๐";
|
||
const good =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(
|
||
userEvaluationOrg
|
||
.filter((x: any) => x.evaluationResults == "GOOD" && x.evaluationStatus == "KP7")
|
||
.length.toString(),
|
||
)
|
||
: "๐";
|
||
const fair =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(
|
||
userEvaluationOrg
|
||
.filter((x: any) => x.evaluationResults == "FAIR" && x.evaluationStatus == "KP7")
|
||
.length.toString(),
|
||
)
|
||
: "๐";
|
||
const improvment =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(
|
||
userEvaluationOrg
|
||
.filter(
|
||
(x: any) => x.evaluationResults == "IMPROVEMENT" && x.evaluationStatus == "KP7",
|
||
)
|
||
.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 && data.rootName != null
|
||
? data.rootName
|
||
: "-",
|
||
userEvaluations: [
|
||
{
|
||
no: userEvaluationOrg.length > 0 ? "๑" : "-",
|
||
root: data && data.rootName != null ? data.rootName : "-",
|
||
resultAll: userEvaluationOrg.length > 0 ? resultAll : "-",
|
||
result: userEvaluationOrg.length > 0 ? result : "-",
|
||
excellent: userEvaluationOrg.length > 0 ? excellent : "-",
|
||
verygood: userEvaluationOrg.length > 0 ? verygood : "-",
|
||
good: userEvaluationOrg.length > 0 ? good : "-",
|
||
fair: userEvaluationOrg.length > 0 ? fair : "-",
|
||
improvment: userEvaluationOrg.length > 0 ? improvment : "-",
|
||
},
|
||
],
|
||
resultAll_: userEvaluationOrg.length > 0 ? resultAll : "-",
|
||
result_: userEvaluationOrg.length > 0 ? result : "-",
|
||
excellent_: userEvaluationOrg.length > 0 ? excellent : "-",
|
||
verygood_: userEvaluationOrg.length > 0 ? verygood : "-",
|
||
good_: userEvaluationOrg.length > 0 ? good : "-",
|
||
fair_: userEvaluationOrg.length > 0 ? fair : "-",
|
||
improvment_: userEvaluationOrg.length > 0 ? improvment : "-",
|
||
};
|
||
}
|
||
if (requestBody.type == "KPI2") {
|
||
templateName = "KPI2";
|
||
reportName = "KPI2";
|
||
const excellent =
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations
|
||
.filter((x: any) => x.evaluationResults == "EXCELLENT")
|
||
.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: x.prefix!=null&&x.firstName!=null&&x.lastName!=null
|
||
? `${x.prefix}${x.firstName} ${x.lastName}`
|
||
: "-",
|
||
position: x.position ? x.position : "-",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "-",
|
||
org: x.org ? x.org : "-",
|
||
}))
|
||
: [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
org: "-",
|
||
}];
|
||
const verygood =
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations
|
||
.filter((x: any) => x.evaluationResults == "VERY_GOOD")
|
||
.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: x.prefix!=null&&x.firstName!=null&&x.lastName!=null
|
||
? `${x.prefix}${x.firstName} ${x.lastName}`
|
||
: "-",
|
||
position: x.position ? x.position : "-",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "-",
|
||
org: x.org ? x.org : "-",
|
||
}))
|
||
: [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
org: "-",
|
||
}];
|
||
const good =
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations
|
||
.filter((x: any) => x.evaluationResults == "GOOD")
|
||
.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: x.prefix!=null&&x.firstName!=null&&x.lastName!=null
|
||
? `${x.prefix}${x.firstName} ${x.lastName}`
|
||
: "-",
|
||
position: x.position ? x.position : "-",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "-",
|
||
org: x.org ? x.org : "-",
|
||
}))
|
||
: [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
org: "-",
|
||
}];
|
||
const fair =
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations
|
||
.filter((x: any) => x.evaluationResults == "FAIR")
|
||
.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: x.prefix!=null&&x.firstName!=null&&x.lastName!=null
|
||
? `${x.prefix}${x.firstName} ${x.lastName}`
|
||
: "-",
|
||
position: x.position ? x.position : "-",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "-",
|
||
org: x.org ? x.org : "-",
|
||
}))
|
||
: [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
org: "-",
|
||
}];
|
||
const improvment =
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations
|
||
.filter((x: any) => x.evaluationResults == "IMPROVEMENT")
|
||
.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: x.prefix!=null&&x.firstName!=null&&x.lastName!=null
|
||
? `${x.prefix}${x.firstName} ${x.lastName}`
|
||
: "-",
|
||
position: x.position ? x.position : "-",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "-",
|
||
org: x.org ? x.org : "-",
|
||
}))
|
||
: [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
org: "-",
|
||
}];
|
||
formattedData = {
|
||
year: data?.year ? Extension.ToThaiNumber((data.year + 543).toString()) : "-",
|
||
period1: Extension.ToThaiNumber(period1),
|
||
period2: Extension.ToThaiNumber(period2),
|
||
durationKPI: data?.durationKPI,
|
||
root: data && data.rootName != null
|
||
? data.rootName
|
||
: "-",
|
||
excellents: {
|
||
count:
|
||
data.kpiUserEvaluations.length > 0
|
||
? Extension.ToThaiNumber(excellent.length.toString())
|
||
: "๐",
|
||
data: excellent.length > 0 ? excellent : [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
org: "-",
|
||
}],
|
||
},
|
||
verygoods: {
|
||
count:
|
||
data.kpiUserEvaluations.length > 0
|
||
? Extension.ToThaiNumber(verygood.length.toString())
|
||
: "๐",
|
||
data: verygood.length > 0 ? verygood : [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
org: "-",
|
||
}],
|
||
},
|
||
goods: {
|
||
count:
|
||
data.kpiUserEvaluations.length > 0
|
||
? Extension.ToThaiNumber(good.length.toString())
|
||
: "๐",
|
||
data: good.length > 0 ? good : [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
org: "-",
|
||
}],
|
||
},
|
||
fairs: {
|
||
count:
|
||
data.kpiUserEvaluations.length > 0
|
||
? Extension.ToThaiNumber(fair.length.toString())
|
||
: "๐",
|
||
data: fair.length > 0 ? fair : [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
org: "-",
|
||
}],
|
||
},
|
||
improvments: {
|
||
count:
|
||
data.kpiUserEvaluations.length > 0
|
||
? Extension.ToThaiNumber(improvment.length.toString())
|
||
: "๐",
|
||
data: improvment.length > 0 ? improvment : [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
org: "-",
|
||
}],
|
||
},
|
||
};
|
||
}
|
||
if (requestBody.type == "KPI3") {
|
||
templateName = "KPI3";
|
||
reportName = "KPI3";
|
||
const userEvaluations_ =
|
||
data?.kpiUserEvaluations?.length > 0
|
||
? data.kpiUserEvaluations.map((x: any, idx: number) => ({
|
||
no: idx != null ?Extension.ToThaiNumber((idx + 1).toString()) : "-",
|
||
fullName: x.prefix != null && x.firstName != null && x.lastName != null
|
||
? `${x.prefix}${x.firstName} ${x.lastName}`
|
||
: "-",
|
||
position: x.position ? x.position : "-",
|
||
point1:
|
||
x.summaryPoint >= 90.0
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: "-",
|
||
point2:
|
||
x.summaryPoint >= 80.0 && x.summaryPoint <= 89.99
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: "-",
|
||
point3:
|
||
x.summaryPoint >= 70.0 && x.summaryPoint <= 79.99
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: "-",
|
||
point4:
|
||
x.summaryPoint >= 60.0 && x.summaryPoint <= 69.99
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: "-",
|
||
point5:
|
||
x.summaryPoint < 60.0
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: "-",
|
||
remark: x.reasonEvaluator,
|
||
}))
|
||
: [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
point1: "-",
|
||
point2: "-",
|
||
point3: "-",
|
||
point4: "-",
|
||
point5: "-",
|
||
remark: "-",
|
||
}];
|
||
const prefixEvaluator_ =
|
||
data.kpiUserEvaluations.length > 0 ? data.kpiUserEvaluations[0].prefixEvaluator : "";
|
||
const firstNameEvaluator_ =
|
||
data.kpiUserEvaluations.length > 0 ? data.kpiUserEvaluations[0].firstNameEvaluator : "";
|
||
const lastNameEvaluator_ =
|
||
data.kpiUserEvaluations.length > 0 ? data.kpiUserEvaluations[0].lastNameEvaluator : "";
|
||
const fullNameEvaluator =
|
||
prefixEvaluator_ != "" && firstNameEvaluator_ != "" && lastNameEvaluator_ != ""
|
||
? `${prefixEvaluator_}${firstNameEvaluator_} ${lastNameEvaluator_}`
|
||
: "-";
|
||
const commanderId =
|
||
data.kpiUserEvaluations.length > 0 ? data.kpiUserEvaluations[0].commanderId : "";
|
||
let prefixCommander = "";
|
||
let firstNameCommander = "";
|
||
let lastNameCommander = "";
|
||
let positionCommander = "";
|
||
let posTypeNameCommander = "";
|
||
let posLevelNameCommander = "";
|
||
let orgCommander = "";
|
||
if (commanderId != "" && commanderId != null) {
|
||
await new CallAPI()
|
||
.GetData(request, "org/profile/profileid/position/" + commanderId)
|
||
.then((x) => {
|
||
prefixCommander = x.prefix;
|
||
firstNameCommander = x.firstName;
|
||
lastNameCommander = x.lastName;
|
||
positionCommander = x.position;
|
||
orgCommander = x.root;
|
||
posTypeNameCommander = x.posTypeName;
|
||
posLevelNameCommander = x.posLevelName;
|
||
})
|
||
.catch((x) => {
|
||
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ");
|
||
});
|
||
}
|
||
const fullNameCommander =
|
||
prefixCommander != "" && firstNameCommander != "" && firstNameCommander != ""
|
||
? `${prefixCommander}${firstNameCommander} ${lastNameCommander}`
|
||
: "-";
|
||
|
||
const commanderHighId =
|
||
data.kpiUserEvaluations.length > 0 ? data.kpiUserEvaluations[0].commanderHighId : "";
|
||
let prefixCommanderHigh = "";
|
||
let firstNameCommanderHigh = "";
|
||
let lastNameCommanderHigh = "";
|
||
let positionCommanderHigh = "";
|
||
let posTypeNameCommanderHigh = "";
|
||
let posLevelNameCommanderHigh = "";
|
||
let orgCommanderHigh;
|
||
if (commanderHighId != "" && commanderHighId != null) {
|
||
await new CallAPI()
|
||
.GetData(request, "org/profile/profileid/position/" + commanderHighId)
|
||
.then((x) => {
|
||
prefixCommanderHigh = x.prefix;
|
||
firstNameCommanderHigh = x.firstName;
|
||
lastNameCommanderHigh = x.lastName;
|
||
positionCommanderHigh = x.position;
|
||
orgCommanderHigh = x.root;
|
||
posTypeNameCommanderHigh = x.posTypeName;
|
||
posLevelNameCommanderHigh = x.posLevelName;
|
||
})
|
||
.catch((x) => {
|
||
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลในทะเบียนประวัติ");
|
||
});
|
||
}
|
||
const fullNameCommanderHigh =
|
||
prefixCommanderHigh != "" && firstNameCommanderHigh != "" && lastNameCommanderHigh != ""
|
||
? `${prefixCommanderHigh}${firstNameCommanderHigh} ${lastNameCommanderHigh}`
|
||
: "-";
|
||
|
||
formattedData = {
|
||
year: data?.year ? Extension.ToThaiNumber((data.year + 543).toString()) : "-",
|
||
period1: Extension.ToThaiNumber(period1),
|
||
period2: Extension.ToThaiNumber(period2),
|
||
durationKPI: data?.durationKPI,
|
||
root:
|
||
data && data.rootName != null
|
||
? data.rootName
|
||
: "-",
|
||
fullNameEvaluator: fullNameEvaluator,
|
||
positionEvaluator:
|
||
data.kpiUserEvaluations.length > 0 ? data.kpiUserEvaluations[0].positionEvaluator : "-",
|
||
posTypeNameEvaluator:
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations[0].posTypeNameEvaluator
|
||
: "-",
|
||
posLevelNameEvaluator:
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations[0].posLevelNameEvaluator
|
||
: "-",
|
||
orgEvaluator:
|
||
data.kpiUserEvaluations.length > 0 ? data.kpiUserEvaluations[0].orgEvaluator : "-",
|
||
fullNameCommander: fullNameCommander,
|
||
positionCommander:
|
||
positionCommander != "" && positionCommander != null ? positionCommander : "-",
|
||
posTypeNameCommander:
|
||
posTypeNameCommander != "" && posTypeNameCommander != null ? posTypeNameCommander : "-",
|
||
posLevelNameCommander:
|
||
posLevelNameCommander != "" && posLevelNameCommander != null
|
||
? posLevelNameCommander
|
||
: "-",
|
||
orgCommander: orgCommander != "" && orgCommander != null ? orgCommander : "-",
|
||
fullNameCommanderHigh: fullNameCommanderHigh,
|
||
positionCommanderHigh:
|
||
positionCommanderHigh != "" && positionCommanderHigh != null
|
||
? positionCommanderHigh
|
||
: "-",
|
||
posTypeNameCommanderHigh:
|
||
posTypeNameCommanderHigh != "" && posTypeNameCommanderHigh != null
|
||
? posTypeNameCommanderHigh
|
||
: "-",
|
||
posLevelNameCommanderHigh:
|
||
posLevelNameCommanderHigh != "" && posLevelNameCommanderHigh != null
|
||
? posLevelNameCommanderHigh
|
||
: "-",
|
||
orgCommanderHigh:
|
||
orgCommanderHigh != "" && orgCommanderHigh != null ? orgCommanderHigh : "-",
|
||
userEvaluations: userEvaluations_,
|
||
};
|
||
}
|
||
if (requestBody.type == "KPI4") {
|
||
templateName = "KPI4";
|
||
reportName = "KPI4";
|
||
const yearNow = new Date().getFullYear();
|
||
let combinedDatas: any;
|
||
if (requestBody.profileId) {
|
||
//ชั่วคราว
|
||
const profileEvaluationNowYearIds = await AppDataSource.getRepository(KpiUserEvaluation)
|
||
.createQueryBuilder("kpiUserEvaluation")
|
||
.leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod")
|
||
.where("kpiUserEvaluation.profileId = :profileId", { profileId: requestBody.profileId })
|
||
.andWhere("kpiUserEvaluation.evaluationStatus = :evaluationStatus", {
|
||
evaluationStatus: "KP7",
|
||
})
|
||
// .where("kpiPeriod.year = :year", { year: yearNow })
|
||
.groupBy("kpiUserEvaluation.kpiPeriodId")
|
||
.select("MIN(kpiUserEvaluation.id) as id")
|
||
.getRawMany();
|
||
|
||
// const profileEvaluationNextYearIds = await AppDataSource.getRepository(KpiUserEvaluation)
|
||
// .createQueryBuilder("kpiUserEvaluation")
|
||
// .leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod")
|
||
// .where("kpiUserEvaluation.profileId = :profileId", { profileId: requestBody.profileId })
|
||
// .where("kpiPeriod.year = :year", { year: yearNow + 1 })
|
||
// .groupBy("kpiUserEvaluation.kpiPeriodId")
|
||
// .select("MIN(kpiUserEvaluation.id) as id")
|
||
// .getRawMany();
|
||
// const profileEvaluationCombianIds = profileEvaluationNowYearIds.concat(profileEvaluationNextYearIds);
|
||
|
||
//ชั่วคราว
|
||
const profileEvaluations = await this.kpiUserEvaluationRepository.find({
|
||
relations: ["kpiPeriod"],
|
||
where: { id: In(profileEvaluationNowYearIds.map((evaluation) => evaluation.id)) },
|
||
});
|
||
|
||
const groupedEvaluations = profileEvaluations.reduce((acc: any, evaluation: any) => {
|
||
const year = evaluation.kpiPeriod.year;
|
||
const profileId = evaluation.profileId;
|
||
const key = `${profileId}-${year}`;
|
||
|
||
if (!acc[key]) {
|
||
acc[key] = {
|
||
fullName: evaluation.prefix + evaluation.firstName + " " + evaluation.lastName,
|
||
profileId: profileId,
|
||
year: year ? Extension.ToThaiNumber(Extension.ToThaiYear(year).toString()) : null,
|
||
evaluations: [],
|
||
};
|
||
}
|
||
|
||
acc[key].evaluations.push(evaluation);
|
||
return acc;
|
||
}, {});
|
||
|
||
// สร้าง formatData
|
||
combinedDatas = Object.values(groupedEvaluations).map((group: any) => {
|
||
const data: any = {
|
||
fullName: group.fullName ?? null,
|
||
year: group.year ?? null,
|
||
};
|
||
|
||
group.evaluations.forEach((evaluation: any) => {
|
||
if (evaluation.kpiPeriod.durationKPI === "APR") {
|
||
data.summaryPointAPR1 =
|
||
evaluation.summaryPoint >= 90
|
||
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
|
||
: null;
|
||
data.summaryPointAPR2 =
|
||
evaluation.summaryPoint >= 80 && evaluation.summaryPoint < 90
|
||
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
|
||
: null;
|
||
data.summaryPointAPR3 =
|
||
evaluation.summaryPoint >= 70 && evaluation.summaryPoint < 80
|
||
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
|
||
: null;
|
||
data.summaryPointAPR4 =
|
||
evaluation.summaryPoint >= 60 && evaluation.summaryPoint < 70
|
||
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
|
||
: null;
|
||
data.summaryPointAPR5 =
|
||
evaluation.summaryPoint < 60
|
||
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
|
||
: null;
|
||
data.periodAPR = evaluation.kpiPeriod.durationKPI;
|
||
} else if (evaluation.kpiPeriod.durationKPI === "OCT") {
|
||
data.summaryPointOCT1 =
|
||
evaluation.summaryPoint >= 90
|
||
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
|
||
: null;
|
||
data.summaryPointOCT2 =
|
||
evaluation.summaryPoint >= 80 && evaluation.summaryPoint < 90
|
||
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
|
||
: null;
|
||
data.summaryPointOCT3 =
|
||
evaluation.summaryPoint >= 70 && evaluation.summaryPoint < 80
|
||
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
|
||
: null;
|
||
data.summaryPointOCT4 =
|
||
evaluation.summaryPoint >= 60 && evaluation.summaryPoint < 70
|
||
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
|
||
: null;
|
||
data.summaryPointOCT5 =
|
||
evaluation.summaryPoint < 60
|
||
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
|
||
: null;
|
||
data.periodOCT = evaluation.kpiPeriod.durationKPI ?? null;
|
||
}
|
||
});
|
||
|
||
return data;
|
||
});
|
||
}
|
||
|
||
formattedData = {
|
||
combinedDatas: combinedDatas.length > 0 ? combinedDatas : [{}],
|
||
fullName: combinedDatas.length > 0 ? combinedDatas[0]["fullName"] : "-",
|
||
};
|
||
}
|
||
if (requestBody.type == "KPI5") {
|
||
templateName = "KPI5";
|
||
reportName = "KPI5";
|
||
interface KPIData {
|
||
fullName: string | null;
|
||
position: string | null;
|
||
posType: string | null;
|
||
posLevel: string | null;
|
||
affiliation: string | null;
|
||
summaryPointAPR1: string | null;
|
||
textPointAPR1: string | null;
|
||
periodAPR1: string | null;
|
||
yearAPR1: string | null;
|
||
summaryPointOCT1: string | null;
|
||
textPointOCT1: string | null;
|
||
periodOCT1: string | null;
|
||
yearOCT1: string | null;
|
||
summaryPointAPR2: string | null;
|
||
textPointAPR2: string | null;
|
||
periodAPR2: string | null;
|
||
yearAPR2: string | null;
|
||
summaryPointOCT2: string | null;
|
||
textPointOCT2: string | null;
|
||
periodOCT2: string | null;
|
||
yearOCT2: string | null;
|
||
summaryPointAPR3: string | null;
|
||
textPointAPR3: string | null;
|
||
periodAPR3: string | null;
|
||
yearAPR3: string | null;
|
||
summaryPointOCT3: string | null;
|
||
textPointOCT3: string | null;
|
||
periodOCT3: string | null;
|
||
yearOCT3: string | null;
|
||
summaryPointAPR4: string | null;
|
||
textPointAPR4: string | null;
|
||
periodAPR4: string | null;
|
||
yearAPR4: string | null;
|
||
summaryPointOCT4: string | null;
|
||
textPointOCT4: string | null;
|
||
periodOCT4: string | null;
|
||
yearOCT4: string | null;
|
||
summaryPointAPR5: string | null;
|
||
textPointAPR5: string | null;
|
||
periodAPR5: string | null;
|
||
yearAPR5: string | null;
|
||
summaryPointOCT5: string | null;
|
||
textPointOCT5: string | null;
|
||
periodOCT5: string | null;
|
||
yearOCT5: string | null;
|
||
year1: string | null;
|
||
year2: string | null;
|
||
year3: string | null;
|
||
year4: string | null;
|
||
year5: string | null;
|
||
}
|
||
const yearNow = new Date().getFullYear();
|
||
if (requestBody.profileId) {
|
||
const profileEvaluationIds = await AppDataSource.getRepository(KpiUserEvaluation)
|
||
.createQueryBuilder("kpiUserEvaluation")
|
||
.leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod")
|
||
.where("kpiUserEvaluation.profileId = :profileId", { profileId: requestBody.profileId })
|
||
.andWhere("kpiUserEvaluation.evaluationStatus = :evaluationStatus", {
|
||
evaluationStatus: "KP7",
|
||
})
|
||
.andWhere("kpiPeriod.year BETWEEN :startYear AND :endYear", {
|
||
startYear: yearNow - 4,
|
||
endYear: yearNow,
|
||
})
|
||
.groupBy("kpiUserEvaluation.kpiPeriodId")
|
||
.select("MIN(kpiUserEvaluation.id) as id")
|
||
.getRawMany();
|
||
// if (profileEvaluationIds.length > 0) {
|
||
const profileEvaluation = await this.kpiUserEvaluationRepository.find({
|
||
relations: ["kpiPeriod"],
|
||
where: { id: In(profileEvaluationIds.map((evaluation) => evaluation.id)) },
|
||
});
|
||
|
||
const combinedData: KPIData = profileEvaluation.reduce(
|
||
(acc: KPIData, x) => {
|
||
const fullNameParts = [x.child4, x.child3, x.child2, x.child1, x.org];
|
||
|
||
const affiliation = fullNameParts
|
||
.filter((part) => part !== undefined && part !== null)
|
||
.join("/");
|
||
|
||
if (!acc.fullName) {
|
||
acc.fullName = x.prefix + " " + x.firstName + " " + x.lastName;
|
||
acc.position = x.position;
|
||
acc.posType = x.posTypeName;
|
||
acc.posLevel = x.posLevelName;
|
||
acc.affiliation = affiliation;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow - 4 && x.kpiPeriod.durationKPI === "APR") {
|
||
acc.summaryPointAPR1 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointAPR1 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodAPR1 = x.kpiPeriod.durationKPI;
|
||
acc.yearAPR1 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow - 4 && x.kpiPeriod.durationKPI === "OCT") {
|
||
acc.summaryPointOCT1 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointOCT1 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodOCT1 = x.kpiPeriod.durationKPI;
|
||
acc.yearOCT1 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow - 3 && x.kpiPeriod.durationKPI === "APR") {
|
||
acc.summaryPointAPR2 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointAPR2 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodAPR2 = x.kpiPeriod.durationKPI;
|
||
acc.yearAPR2 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow - 3 && x.kpiPeriod.durationKPI === "OCT") {
|
||
acc.summaryPointOCT2 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointOCT2 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodOCT2 = x.kpiPeriod.durationKPI;
|
||
acc.yearOCT2 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow - 2 && x.kpiPeriod.durationKPI === "APR") {
|
||
acc.summaryPointAPR3 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointAPR3 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodAPR3 = x.kpiPeriod.durationKPI;
|
||
acc.yearAPR3 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow - 2 && x.kpiPeriod.durationKPI === "OCT") {
|
||
acc.summaryPointOCT3 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointOCT3 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodOCT3 = x.kpiPeriod.durationKPI;
|
||
acc.yearOCT3 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow - 1 && x.kpiPeriod.durationKPI === "APR") {
|
||
acc.summaryPointAPR4 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointAPR4 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodAPR4 = x.kpiPeriod.durationKPI;
|
||
acc.yearAPR4 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow - 1 && x.kpiPeriod.durationKPI === "OCT") {
|
||
acc.summaryPointOCT4 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointOCT4 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodOCT4 = x.kpiPeriod.durationKPI;
|
||
acc.yearOCT4 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow && x.kpiPeriod.durationKPI === "APR") {
|
||
acc.summaryPointAPR5 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointAPR5 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodAPR5 = x.kpiPeriod.durationKPI;
|
||
acc.yearAPR5 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow && x.kpiPeriod.durationKPI === "OCT") {
|
||
acc.summaryPointOCT5 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointOCT5 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodOCT5 = x.kpiPeriod.durationKPI;
|
||
acc.yearOCT5 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
return acc;
|
||
},
|
||
{
|
||
fullName: null,
|
||
position: null,
|
||
posType: null,
|
||
posLevel: null,
|
||
affiliation: null,
|
||
summaryPointAPR1: null,
|
||
textPointAPR1: null,
|
||
periodAPR1: null,
|
||
yearAPR1: null,
|
||
summaryPointOCT1: null,
|
||
textPointOCT1: null,
|
||
periodOCT1: null,
|
||
yearOCT1: null,
|
||
summaryPointAPR2: null,
|
||
textPointAPR2: null,
|
||
periodAPR2: null,
|
||
yearAPR2: null,
|
||
summaryPointOCT2: null,
|
||
textPointOCT2: null,
|
||
periodOCT2: null,
|
||
yearOCT2: null,
|
||
summaryPointAPR3: null,
|
||
textPointAPR3: null,
|
||
periodAPR3: null,
|
||
yearAPR3: null,
|
||
summaryPointOCT3: null,
|
||
textPointOCT3: null,
|
||
periodOCT3: null,
|
||
yearOCT3: null,
|
||
summaryPointAPR4: null,
|
||
textPointAPR4: null,
|
||
periodAPR4: null,
|
||
yearAPR4: null,
|
||
summaryPointOCT4: null,
|
||
textPointOCT4: null,
|
||
periodOCT4: null,
|
||
yearOCT4: null,
|
||
summaryPointAPR5: null,
|
||
textPointAPR5: null,
|
||
periodAPR5: null,
|
||
yearAPR5: null,
|
||
summaryPointOCT5: null,
|
||
textPointOCT5: null,
|
||
periodOCT5: null,
|
||
yearOCT5: null,
|
||
year1: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow - 4).toString()),
|
||
year2: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow - 3).toString()),
|
||
year3: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow - 2).toString()),
|
||
year4: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow - 1).toString()),
|
||
year5: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow).toString()),
|
||
},
|
||
);
|
||
formattedData = profileEvaluation.length > 0
|
||
? combinedData
|
||
: {
|
||
fullName: "-",
|
||
position: "-",
|
||
posType: "-",
|
||
posLevel: "-",
|
||
affiliation: "-",
|
||
summaryPointAPR1: "-",
|
||
textPointAPR1: "-",
|
||
periodAPR1: "-",
|
||
yearAPR1: "-",
|
||
summaryPointOCT1: "-",
|
||
textPointOCT1: "-",
|
||
periodOCT1: "-",
|
||
yearOCT1: "-",
|
||
summaryPointAPR2: "-",
|
||
textPointAPR2: "-",
|
||
periodAPR2: "-",
|
||
yearAPR2: "-",
|
||
summaryPointOCT2: "-",
|
||
textPointOCT2: "-",
|
||
periodOCT2: "-",
|
||
yearOCT2: "-",
|
||
summaryPointAPR3: "-",
|
||
textPointAPR3: "-",
|
||
periodAPR3: "-",
|
||
yearAPR3: "-",
|
||
summaryPointOCT3: "-",
|
||
textPointOCT3: "-",
|
||
periodOCT3: "-",
|
||
yearOCT3: "-",
|
||
summaryPointAPR4: "-",
|
||
textPointAPR4: "-",
|
||
periodAPR4: "-",
|
||
yearAPR4: "-",
|
||
summaryPointOCT4: "-",
|
||
textPointOCT4: "-",
|
||
periodOCT4: "-",
|
||
yearOCT4: "-",
|
||
summaryPointAPR5: "-",
|
||
textPointAPR5: "-",
|
||
periodAPR5: "-",
|
||
yearAPR5: "-",
|
||
summaryPointOCT5: "-",
|
||
textPointOCT5: "-",
|
||
periodOCT5: "-",
|
||
yearOCT5: "-",
|
||
year1: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow - 4).toString()),
|
||
year2: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow - 3).toString()),
|
||
year3: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow - 2).toString()),
|
||
year4: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow - 1).toString()),
|
||
year5: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow).toString()),
|
||
};
|
||
// }
|
||
}
|
||
}
|
||
if (requestBody.type == "KPI6") {
|
||
templateName = "KPI6";
|
||
reportName = "KPI6";
|
||
//use_filter
|
||
interface KPIData {
|
||
fullName: string | null;
|
||
position: string | null;
|
||
posType: string | null;
|
||
posLevel: string | null;
|
||
affiliation: string | null;
|
||
summaryPointAPR1: string | null;
|
||
textPointAPR1: string | null;
|
||
periodAPR1: string | null;
|
||
yearAPR1: string | null;
|
||
summaryPointOCT1: string | null;
|
||
textPointOCT1: string | null;
|
||
periodOCT1: string | null;
|
||
yearOCT1: string | null;
|
||
summaryPointAPR2: string | null;
|
||
textPointAPR2: string | null;
|
||
periodAPR2: string | null;
|
||
yearAPR2: string | null;
|
||
summaryPointOCT2: string | null;
|
||
textPointOCT2: string | null;
|
||
periodOCT2: string | null;
|
||
yearOCT2: string | null;
|
||
summaryPointAPR3: string | null;
|
||
textPointAPR3: string | null;
|
||
periodAPR3: string | null;
|
||
yearAPR3: string | null;
|
||
summaryPointOCT3: string | null;
|
||
textPointOCT3: string | null;
|
||
periodOCT3: string | null;
|
||
yearOCT3: string | null;
|
||
year1: string | null;
|
||
year2: string | null;
|
||
year3: string | null;
|
||
}
|
||
const yearNow = new Date().getFullYear();
|
||
if (requestBody.profileId) {
|
||
const profileEvaluationIds = await AppDataSource.getRepository(KpiUserEvaluation)
|
||
.createQueryBuilder("kpiUserEvaluation")
|
||
.leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod")
|
||
.where("kpiUserEvaluation.profileId = :profileId", { profileId: requestBody.profileId })
|
||
.andWhere("kpiUserEvaluation.evaluationStatus = :evaluationStatus", {
|
||
evaluationStatus: "KP7",
|
||
})
|
||
.andWhere("kpiPeriod.year BETWEEN :startYear AND :endYear", {
|
||
startYear: yearNow - 2,
|
||
endYear: yearNow,
|
||
})
|
||
.groupBy("kpiUserEvaluation.kpiPeriodId")
|
||
.select("MIN(kpiUserEvaluation.id) as id")
|
||
.getRawMany();
|
||
// if (profileEvaluationIds.length > 0) {
|
||
const profileEvaluation = await this.kpiUserEvaluationRepository.find({
|
||
relations: ["kpiPeriod"],
|
||
where: { id: In(profileEvaluationIds.map((evaluation) => evaluation.id)) },
|
||
});
|
||
|
||
const combinedData: KPIData = profileEvaluation.reduce(
|
||
(acc: KPIData, x) => {
|
||
const fullNameParts = [x.child4, x.child3, x.child2, x.child1, x.org];
|
||
|
||
const affiliation = fullNameParts
|
||
.filter((part) => part !== undefined && part !== null)
|
||
.join("/");
|
||
|
||
if (!acc.fullName) {
|
||
acc.fullName = x.prefix + " " + x.firstName + " " + x.lastName;
|
||
acc.position = x.position;
|
||
acc.posType = x.posTypeName;
|
||
acc.posLevel = x.posLevelName;
|
||
acc.affiliation = affiliation;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow - 2 && x.kpiPeriod.durationKPI === "APR") {
|
||
acc.summaryPointAPR1 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointAPR1 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodAPR1 = x.kpiPeriod.durationKPI;
|
||
acc.yearAPR1 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow - 2 && x.kpiPeriod.durationKPI === "OCT") {
|
||
acc.summaryPointOCT1 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointOCT1 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodOCT1 = x.kpiPeriod.durationKPI;
|
||
acc.yearOCT1 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow - 1 && x.kpiPeriod.durationKPI === "APR") {
|
||
acc.summaryPointAPR2 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointAPR2 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodAPR2 = x.kpiPeriod.durationKPI;
|
||
acc.yearAPR2 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow - 1 && x.kpiPeriod.durationKPI === "OCT") {
|
||
acc.summaryPointOCT2 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointOCT2 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodOCT2 = x.kpiPeriod.durationKPI;
|
||
acc.yearOCT2 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow && x.kpiPeriod.durationKPI === "APR") {
|
||
acc.summaryPointAPR3 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointAPR3 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodAPR3 = x.kpiPeriod.durationKPI;
|
||
acc.yearAPR3 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
if (x.kpiPeriod.year === yearNow && x.kpiPeriod.durationKPI === "OCT") {
|
||
acc.summaryPointOCT3 = x.summaryPoint
|
||
? Extension.ToThaiNumber(x.summaryPoint.toString())
|
||
: null;
|
||
acc.textPointOCT3 = x.summaryPoint ? Extension.textPoint(x.summaryPoint) : null;
|
||
acc.periodOCT3 = x.kpiPeriod.durationKPI;
|
||
acc.yearOCT3 = x.kpiPeriod.year
|
||
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
|
||
: null;
|
||
}
|
||
|
||
return acc;
|
||
},
|
||
{
|
||
fullName: null,
|
||
position: null,
|
||
posType: null,
|
||
posLevel: null,
|
||
affiliation: null,
|
||
summaryPointAPR1: null,
|
||
textPointAPR1: null,
|
||
periodAPR1: null,
|
||
yearAPR1: null,
|
||
summaryPointOCT1: null,
|
||
textPointOCT1: null,
|
||
periodOCT1: null,
|
||
yearOCT1: null,
|
||
summaryPointAPR2: null,
|
||
textPointAPR2: null,
|
||
periodAPR2: null,
|
||
yearAPR2: null,
|
||
summaryPointOCT2: null,
|
||
textPointOCT2: null,
|
||
periodOCT2: null,
|
||
yearOCT2: null,
|
||
summaryPointAPR3: null,
|
||
textPointAPR3: null,
|
||
periodAPR3: null,
|
||
yearAPR3: null,
|
||
summaryPointOCT3: null,
|
||
textPointOCT3: null,
|
||
periodOCT3: null,
|
||
yearOCT3: null,
|
||
year1: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow - 2).toString()),
|
||
year2: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow - 1).toString()),
|
||
year3: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow).toString()),
|
||
},
|
||
);
|
||
formattedData = profileEvaluation.length > 0
|
||
? combinedData
|
||
: {
|
||
fullName: "-",
|
||
position: "-",
|
||
posType: "-",
|
||
posLevel: "-",
|
||
affiliation: "-",
|
||
summaryPointAPR1: "-",
|
||
textPointAPR1: "-",
|
||
periodAPR1: "-",
|
||
yearAPR1: "-",
|
||
summaryPointOCT1: "-",
|
||
textPointOCT1: "-",
|
||
periodOCT1: "-",
|
||
yearOCT1: "-",
|
||
summaryPointAPR2: "-",
|
||
textPointAPR2: "-",
|
||
periodAPR2: "-",
|
||
yearAPR2: "-",
|
||
summaryPointOCT2: "-",
|
||
textPointOCT2: "-",
|
||
periodOCT2: "-",
|
||
yearOCT2: "-",
|
||
summaryPointAPR3: "-",
|
||
textPointAPR3: "-",
|
||
periodAPR3: "-",
|
||
yearAPR3: "-",
|
||
summaryPointOCT3: "-",
|
||
textPointOCT3: "-",
|
||
periodOCT3: "-",
|
||
yearOCT3: "-",
|
||
year1: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow - 2).toString()),
|
||
year2: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow - 1).toString()),
|
||
year3: Extension.ToThaiNumber(Extension.ToThaiYear(yearNow).toString()),
|
||
};
|
||
// }
|
||
}
|
||
}
|
||
if (requestBody.type == "KPI7") {
|
||
templateName = "KPI7";
|
||
reportName = "KPI7";
|
||
|
||
const userEvaluations_ = await Promise.all(
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations.map(async (x: any, idx: number) => {
|
||
/*รอ Fe เพิ่ม UI*/
|
||
// const target_ = await this.kpiUserDevelopmentRepository.findOne({
|
||
// where: { kpiUserEvaluationId: x.id },
|
||
// });
|
||
// const isDev70 = target_ && target_?.isDevelopment70 === true
|
||
// ? "🗹 70 การลงมือปฏิบัติ (โดยผู้บังคับบัญชามอบหมาย)"
|
||
// : ""
|
||
// const isDev20 = target_ && target_?.isDevelopment20 === true
|
||
// ? "🗹 20 การเรียนรู้จากผู้อื่น (Coach/Mentor/Consulting)"
|
||
// : ""
|
||
// const isDev10 = target_ && target_?.isDevelopment10 === true
|
||
// ? "🗹 10 การฝึกอบรมอื่นๆ"
|
||
// : ""
|
||
return {
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: x.prefix != null && x.firstName != null && x.lastName != null
|
||
? `${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 ? Extension.ToThaiNumber(x.timeEvaluator) : "-",
|
||
developResults: "-",
|
||
evaluationResults: x.evaluationResults
|
||
? Extension.EvaluationResult(x.evaluationResults)
|
||
: "-",
|
||
};
|
||
})
|
||
: [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
developName: "-",
|
||
developEvaluator: "-",
|
||
target: "-",
|
||
timeEvaluator: "-",
|
||
developResults: "-",
|
||
evaluationResults: "-"
|
||
}],
|
||
);
|
||
formattedData = {
|
||
year: data.year ? Extension.ToThaiNumber((data.year + 543).toString()) : "-",
|
||
period1: Extension.ToThaiNumber(period1),
|
||
period2: Extension.ToThaiNumber(period2),
|
||
durationKPI: data.durationKPI,
|
||
root:
|
||
data && data.rootName != null
|
||
? data.rootName
|
||
: "-",
|
||
userEvaluations: userEvaluations_.length > 0
|
||
? userEvaluations_
|
||
: [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
developName: "-",
|
||
developEvaluator: "-",
|
||
target: "-",
|
||
timeEvaluator: "-",
|
||
developResults: "-",
|
||
evaluationResults: "-"
|
||
}],
|
||
};
|
||
}
|
||
if (requestBody.type == "KPI8") {
|
||
templateName = "KPI8";
|
||
reportName = "KPI8";
|
||
//use_filter
|
||
let period_: any;
|
||
let formattedUserDevelopmentLists: any;
|
||
let userInfo: any;
|
||
let userDevelopmentLists: any;
|
||
let fullNameParts: any;
|
||
let affiliation: any;
|
||
if (requestBody.profileId && requestBody.periodId) {
|
||
period_ = await this.kpiPeriodRepository.findOne({
|
||
where: { id: String(requestBody.periodId) },
|
||
});
|
||
if (period_ && !period1 && !period2) {
|
||
if (period_?.durationKPI === "APR") {
|
||
period1 = `${Extension.ToThaiFullDate2(period_.startDate)} ถึง ${Extension.ToThaiFullDate2(period_.endDate)}`;
|
||
let _period2 = await this.kpiPeriodRepository.findOne({
|
||
where: {
|
||
year: period_.year,
|
||
durationKPI: "OCT",
|
||
isActive: true,
|
||
},
|
||
});
|
||
period2 = _period2
|
||
? `${Extension.ToThaiFullDate2(_period2?.startDate)} ถึง ${Extension.ToThaiFullDate2(_period2?.endDate)}`
|
||
: "";
|
||
} else if (period_?.durationKPI === "OCT") {
|
||
period2 = `${Extension.ToThaiFullDate2(period_.startDate)} ถึง ${Extension.ToThaiFullDate2(period_.endDate)}`;
|
||
let _period1 = await this.kpiPeriodRepository.findOne({
|
||
where: {
|
||
year: period_.year,
|
||
durationKPI: "APR",
|
||
isActive: true,
|
||
},
|
||
});
|
||
period1 = _period1
|
||
? `${Extension.ToThaiFullDate2(_period1?.startDate)} ถึง ${Extension.ToThaiFullDate2(_period1?.endDate)}`
|
||
: "";
|
||
}
|
||
}
|
||
const profileEvaluationIds = await AppDataSource.getRepository(KpiUserEvaluation)
|
||
.createQueryBuilder("kpiUserEvaluation")
|
||
.leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod")
|
||
.where("kpiUserEvaluation.profileId = :profileId", { profileId: requestBody.profileId })
|
||
.andWhere("kpiUserEvaluation.kpiPeriodId = :kpiPeriodId", {
|
||
kpiPeriodId: requestBody.periodId,
|
||
})
|
||
.andWhere("kpiUserEvaluation.evaluationStatus = 'KP7'")
|
||
.groupBy("kpiUserEvaluation.kpiPeriodId")
|
||
.select("MIN(kpiUserEvaluation.id) as id")
|
||
.getRawMany();
|
||
if (profileEvaluationIds.length > 0) {
|
||
userInfo = await this.kpiUserEvaluationRepository.find({
|
||
where: {
|
||
id: In(profileEvaluationIds.map((x: any) => x.id)),
|
||
evaluationStatus: "KP7",
|
||
},
|
||
});
|
||
userDevelopmentLists = await this.kpiUserDevelopmentRepository.find({
|
||
where: {
|
||
kpiUserEvaluationId: In(profileEvaluationIds.map((x: any) => x.id)),
|
||
},
|
||
select: [
|
||
"id",
|
||
"name",
|
||
"target",
|
||
"summary",
|
||
"point",
|
||
"achievement0",
|
||
"achievement5",
|
||
"achievement10",
|
||
"isDevelopment10",
|
||
"isDevelopment20",
|
||
"isDevelopment70",
|
||
],
|
||
});
|
||
const dev10text = "การลงมือปฏิบัติ (โดยผู้บังคับบัญชามอบหมาย)";
|
||
const dev20text = "การเรียนรู้จากผู้อื่น (Coach/Mentor/Consulting)";
|
||
const dev70text = "การลงมือปฏิบัติ (โดยผู้บังคับบัญชามอบหมาย)";
|
||
const combianText = [dev10text, dev20text, dev70text];
|
||
|
||
formattedUserDevelopmentLists = userDevelopmentLists.map(
|
||
(development: any, index: number) => ({
|
||
no: Extension.ToThaiNumber((index + 1).toString()),
|
||
...development,
|
||
summary: development.summary
|
||
? Extension.ToThaiNumber(development.summary.toString())
|
||
: null,
|
||
point: development.point
|
||
? Extension.ToThaiNumber(development.point.toString())
|
||
: null,
|
||
pointText: (development.point = 0
|
||
? development.achievement0
|
||
: (development.point = 5
|
||
? development.achievement5
|
||
: (development.point = 10 ? development.achievement10 : null))),
|
||
isDevelopmentText: [
|
||
development.isDevelopment10,
|
||
development.isDevelopment20,
|
||
development.isDevelopment70,
|
||
]
|
||
.map((x: any, index) => (x ? combianText[index] : null))
|
||
.filter((x: any) => x !== null)
|
||
.join("/"),
|
||
}),
|
||
);
|
||
|
||
fullNameParts = [
|
||
userInfo[0]?.child4,
|
||
userInfo[0]?.child3,
|
||
userInfo[0]?.child2,
|
||
userInfo[0]?.child1,
|
||
userInfo[0]?.org,
|
||
];
|
||
affiliation = fullNameParts
|
||
.filter((part: any) => part !== undefined && part !== null)
|
||
.join("/");
|
||
}
|
||
}
|
||
formattedData = {
|
||
year: period_ ? Extension.ToThaiNumber((period_.year + 543).toString()) : "-",
|
||
fullName: userInfo
|
||
? userInfo[0]?.prefix + userInfo[0]?.firstName + " " + userInfo[0]?.lastName
|
||
: "-",
|
||
affiliation: affiliation ?? "-",
|
||
durationKPI: period_ ? period_.durationKPI : "-",
|
||
position: userInfo ? userInfo[0]?.position : "-",
|
||
posType: userInfo ? userInfo[0]?.posTypeName : "-",
|
||
posLevel: userInfo ? userInfo[0]?.posLevelName : "-",
|
||
period1: period1 ? Extension.ToThaiNumber(period1) : "-",
|
||
period2: period2 ? Extension.ToThaiNumber(period2) : "-",
|
||
developments: formattedUserDevelopmentLists ? formattedUserDevelopmentLists : [{}],
|
||
};
|
||
}
|
||
if (requestBody.type == "KPI9") {
|
||
templateName = "KPI9";
|
||
reportName = "KPI9";
|
||
const userEvaluations_ =
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations
|
||
.filter(
|
||
(x: any) =>
|
||
x.evaluationResults == "EXCELLENT" || x.evaluationResults == "VERY_GOOD",
|
||
)
|
||
.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: x.prefix != null && x.firstName != null && x.lastName != null
|
||
? `${x.prefix}${x.firstName} ${x.lastName}`
|
||
: "-",
|
||
position: x.position ? x.position : "-",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "-",
|
||
evaluationResults: x.evaluationResults
|
||
? Extension.EvaluationResult(x.evaluationResults)
|
||
: "-",
|
||
}))
|
||
: [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
evaluationResults: "-",
|
||
}];
|
||
formattedData = {
|
||
root:
|
||
data && data.rootName != null
|
||
? data.rootName
|
||
: "-",
|
||
period: data?.durationKPI == "APR" ? "๑" : data?.durationKPI == "OCT" ? "๒" : "-",
|
||
year: data.year ? Extension.ToThaiNumber((data.year + 543).toString()) : "-",
|
||
userEvaluations: userEvaluations_.length > 0
|
||
? userEvaluations_
|
||
: [{
|
||
no: "-",
|
||
fullName: "-",
|
||
position: "-",
|
||
posLevelName: "-",
|
||
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,
|
||
});
|
||
}
|
||
}
|