hrms-api-kpi/src/controllers/ReportController.ts
2025-08-08 10:59:27 +07:00

2153 lines
92 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Controller, Get, Post, Route, Security, Tags, Body, Path, Request } from "tsoa";
import { AppDataSource } from "../database/data-source";
import { In, 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 Extension from "../interfaces/extension";
import { KpiUserDevelopment } from "../entities/kpiUserDevelopment";
import { KpiCapacity } from "../entities/kpiCapacity";
import { KpiPlan } from "../entities/kpiPlan";
import { KpiRole } from "../entities/kpiRole";
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 kpiUserDevelopmentRepository = AppDataSource.getRepository(KpiUserDevelopment);
private kpiCapacityRepository = AppDataSource.getRepository(KpiCapacity);
private kpiPlanRepository = AppDataSource.getRepository(KpiPlan);
private kpiRoleRepository = AppDataSource.getRepository(KpiRole);
@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 durationKpi_APR: string ="";
let durationKpi_OCT: string ="";
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 },
});
durationKpi_APR = data && data.durationKPI == "APR" ? `🗹` : ``
durationKpi_OCT = data && data.durationKPI == "OCT" ? `🗹` : ``
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 = `${durationKpi_APR} รอบที่ ๑ ตั้งแต่ ${Extension.ToThaiFullDate2(data.startDate)} ถึง ${Extension.ToThaiFullDate2(data.endDate)}`;
let _period2 = await this.kpiPeriodRepository.findOne({
where: {
year: data.year,
durationKPI: "OCT",
},
});
period2 = _period2
? `${durationKpi_OCT} รอบที่ ๒ ตั้งแต่ ${Extension.ToThaiFullDate2(_period2?.startDate)} ถึง ${Extension.ToThaiFullDate2(_period2?.endDate)}`
: `${durationKpi_OCT} รอบที่ ๒ ตั้งแต่ ๑ เมษายน ${Extension.ToThaiYear(data.year)} ถึง ๓๐ กันยายน ${Extension.ToThaiYear(data.year)}`;
}
else if (data.durationKPI == "OCT") {
period2 = `${durationKpi_OCT} รอบที่ ๒ ตั้งแต่ ${Extension.ToThaiFullDate2(data.startDate)} ถึง ${Extension.ToThaiFullDate2(data.endDate)}`;
let _period1 = await this.kpiPeriodRepository.findOne({
where: {
year: data.year,
durationKPI: "APR",
},
});
period1 = _period1
? `${durationKpi_APR} รอบที่ ๑ ตั้งแต่ ${Extension.ToThaiFullDate2(_period1?.startDate)} ถึง ${Extension.ToThaiFullDate2(_period1?.endDate)}`
: `${durationKpi_APR} รอบที่ ๑ ตั้งแต่ ๑ ตุลาคม ${Extension.ToThaiYear(data.year-1)} ถึง ๓๑ มีนาคม ${Extension.ToThaiYear(data.year)}`;
}
}
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 groupedEvaluations: any[] = [];
let fullNameCommander: string = "-";
let positionCommander: string = "-";
let posTypeNameCommander: string = "-";
let posLevelNameCommander: string = "-";
let orgCommander: string = "-";
let fullNameCommanderHigh: string = "-";
let positionCommanderHigh: string = "-";
let posTypeNameCommanderHigh: string = "-";
let posLevelNameCommanderHigh: string = "-";
let orgCommanderHigh: string = "-";
let idx:number=0
for (const x of data?.kpiUserEvaluations || []) {
let group = groupedEvaluations.find(
(g) =>
g.evaluatorId === x.evaluatorId &&
g.commanderId === x.commanderId &&
g.commanderHighId === x.commanderHighId
);
if (x.commanderId != "" && x.commanderId != null) {
await new CallAPI()
.GetData(request, "/org/profile/profileid/position/" + x.commanderId)
.then((res) => {
fullNameCommander = `${res.prefix}${res.firstName} ${res.lastName}`;
positionCommander = res.position;
posTypeNameCommander = res.posTypeName;
posLevelNameCommander = res.posLevelName;
orgCommander = res.root;
})
.catch(() => {});
}
if (x.commanderHighId != "" && x.commanderHighId != null) {
await new CallAPI()
.GetData(request, "/org/profile/profileid/position/" + x.commanderHighId)
.then((res) => {
fullNameCommanderHigh = `${res.prefix}${res.firstName} ${res.lastName}`;
positionCommanderHigh = res.position;
posTypeNameCommanderHigh = res.posTypeName;
posLevelNameCommanderHigh = res.posLevelName;
orgCommanderHigh = res.root;
})
.catch(() => {});
}
if (!group) {
group = {
evaluatorId: x.evaluatorId,
commanderId: x.commanderId,
commanderHighId: x.commanderHighId,
fullNameEvaluator: `${x.prefixEvaluator}${x.firstNameEvaluator} ${x.lastNameEvaluator}`,
positionEvaluator: x.positionEvaluator,
posTypeNameEvaluator: x.posTypeNameEvaluator,
posLevelNameEvaluator: x.posLevelNameEvaluator,
orgEvaluator: x.orgEvaluator,
fullNameCommander: fullNameCommander,
positionCommander: positionCommander,
posTypeNameCommander: posTypeNameCommander,
posLevelNameCommander: posLevelNameCommander,
orgCommander: orgCommander,
fullNameCommanderHigh: fullNameCommanderHigh,
positionCommanderHigh: positionCommanderHigh,
posTypeNameCommanderHigh: posTypeNameCommanderHigh,
posLevelNameCommanderHigh: posLevelNameCommanderHigh,
orgCommanderHigh: orgCommanderHigh,
evaluationResults: [],
};
groupedEvaluations.push(group);
}
group.evaluationResults.push({
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,
});
idx ++;
}
const _userEvaluations = groupedEvaluations.length > 0
? groupedEvaluations
: [
{
evaluatorId: "-",
commanderId: "-",
commanderHighId: "-",
fullNameEvaluator: "-",
positionEvaluator: "-",
posTypeNameEvaluator: "-",
posLevelNameEvaluator: "-",
orgEvaluator: "-",
fullNameCommander: "-",
positionCommander: "-",
posTypeNameCommander: "-",
posLevelNameCommander: "-",
orgCommander: "-",
fullNameCommanderHigh: "-",
positionCommanderHigh: "-",
posTypeNameCommanderHigh: "-",
posLevelNameCommanderHigh: "-",
orgCommanderHigh: "-",
evaluationResults: [
{
no: "-",
fullName: "-",
position: "-",
point1: "-",
point2: "-",
point3: "-",
point4: "-",
point5: "-",
remark: "-",
},
],
},
];
formattedData = {
year: data?.year ? Extension.ToThaiNumber((data.year + 543).toString()) : "-",
period1: Extension.ToThaiNumber(period1),
period2: Extension.ToThaiNumber(period2),
evaluations: _userEvaluations,
};
}
if (requestBody.type == "KPI4") {
templateName = "KPI4";
reportName = "KPI4";
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",
})
.groupBy("kpiUserEvaluation.kpiPeriodId")
// .select("MIN(kpiUserEvaluation.id) as id")
.select([
"kpiUserEvaluation.kpiPeriodId as kpiPeriodId",
"AVG(kpiUserEvaluation.summaryPoint) as avgSummaryPoint"
])
.getRawMany();
// const profileEvaluations = await this.kpiUserEvaluationRepository.find({
// relations: ["kpiPeriod"],
// where: { id: In(profileEvaluationNowYearIds.map((evaluation) => evaluation.id)) },
// });
const profileEvaluations = await this.kpiUserEvaluationRepository.find({
relations: ["kpiPeriod"],
where: {
kpiPeriodId: In(profileEvaluationNowYearIds.map((evaluation) => evaluation.kpiPeriodId)),
profileId: requestBody.profileId,
evaluationStatus: "KP7"
},
});
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: [],
};
}
const avgSummaryPoint = profileEvaluationNowYearIds.find(
(e) => e.kpiPeriodId === evaluation.kpiPeriodId
)?.avgSummaryPoint;
evaluation.summaryPoint = avgSummaryPoint;
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())
: "-";
data.summaryPointAPR2 =
evaluation.summaryPoint >= 80 && evaluation.summaryPoint < 90
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
: "-";
data.summaryPointAPR3 =
evaluation.summaryPoint >= 70 && evaluation.summaryPoint < 80
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
: "-";
data.summaryPointAPR4 =
evaluation.summaryPoint >= 60 && evaluation.summaryPoint < 70
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
: "-";
data.summaryPointAPR5 =
evaluation.summaryPoint < 60
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
: "-";
data.periodAPR = evaluation.kpiPeriod.durationKPI ?? "-";
}
else if (evaluation.kpiPeriod.durationKPI === "OCT") {
data.summaryPointOCT1 =
evaluation.summaryPoint >= 90
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
: "-";
data.summaryPointOCT2 =
evaluation.summaryPoint >= 80 && evaluation.summaryPoint < 90
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
: "-";
data.summaryPointOCT3 =
evaluation.summaryPoint >= 70 && evaluation.summaryPoint < 80
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
: "-";
data.summaryPointOCT4 =
evaluation.summaryPoint >= 60 && evaluation.summaryPoint < 70
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
: "-";
data.summaryPointOCT5 =
evaluation.summaryPoint < 60
? Extension.ToThaiNumber(evaluation.summaryPoint.toString())
: "-";
data.periodOCT = evaluation.kpiPeriod.durationKPI ?? "-";
}
});
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();
let _summaryPointApr: number[] = [];
let _summaryPointOct: number[] = [];
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();
for (let i = 0; i < 5; i++) {
let _calAvg = await this.kpiUserEvaluationRepository
.createQueryBuilder("evaluation")
.leftJoin("evaluation.kpiPeriod", "kpiPeriod")
.where("evaluation.profileId = :profileId", { profileId: requestBody.profileId })
.andWhere("evaluation.evaluationStatus = :status", { status: "KP7" })
.andWhere("kpiPeriod.year = :year", { year: yearNow - i })
.andWhere("kpiPeriod.durationKPI = :duration", { duration: "APR" })
.select("AVG(evaluation.summaryPoint)", "average")
.getRawOne();
_summaryPointApr.push(_calAvg.average);
}
for (let i = 0; i < 5; i++) {
let _calAvg = await this.kpiUserEvaluationRepository
.createQueryBuilder("evaluation")
.leftJoin("evaluation.kpiPeriod", "kpiPeriod")
.where("evaluation.profileId = :profileId", { profileId: requestBody.profileId })
.andWhere("evaluation.evaluationStatus = :status", { status: "KP7" })
.andWhere("kpiPeriod.year = :year", { year: yearNow - i })
.andWhere("kpiPeriod.durationKPI = :duration", { duration: "OCT" })
.select("AVG(evaluation.summaryPoint)", "average")
.getRawOne();
_summaryPointOct.push(_calAvg.average);
}
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 == "" || affiliation == null ? "-" : affiliation;
} else {
acc.fullName = "-";
acc.position = "-";
acc.posType = "-";
acc.posLevel = "-";
acc.affiliation = "-";
}
if (x.kpiPeriod.year === yearNow - 4 && x.kpiPeriod.durationKPI === "APR") {
acc.summaryPointAPR1 = _summaryPointApr.length > 4
? Extension.ToThaiNumber(_summaryPointApr[4].toString())
: null;
acc.textPointAPR1 = _summaryPointApr.length > 4 ? Extension.textPoint(_summaryPointApr[4]) : null;
acc.periodAPR1 = x.kpiPeriod.durationKPI;
acc.yearAPR1 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointAPR1 = "-";
acc.textPointAPR1 = "-";
acc.periodAPR1 = "-";
acc.yearAPR1 = "-";
}
if (x.kpiPeriod.year === yearNow - 4 && x.kpiPeriod.durationKPI === "OCT") {
acc.summaryPointOCT1 = _summaryPointOct.length > 4
? Extension.ToThaiNumber(_summaryPointOct[4].toString())
: null;
acc.textPointOCT1 = _summaryPointOct.length > 4 ? Extension.textPoint(_summaryPointOct[4]) : null;
acc.periodOCT1 = x.kpiPeriod.durationKPI;
acc.yearOCT1 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointOCT1 = "-";
acc.textPointOCT1 = "-";
acc.periodOCT1 = "-";
acc.yearOCT1 = "-";
}
if (x.kpiPeriod.year === yearNow - 3 && x.kpiPeriod.durationKPI === "APR") {
acc.summaryPointAPR2 = _summaryPointApr.length > 3
? Extension.ToThaiNumber(_summaryPointApr[3].toString())
: null;
acc.textPointAPR2 = _summaryPointApr.length > 3 ? Extension.textPoint(_summaryPointApr[3]) : null;
acc.periodAPR2 = x.kpiPeriod.durationKPI;
acc.yearAPR2 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointAPR2 = "-";
acc.textPointAPR2 = "-";
acc.periodAPR2 = "-";
acc.yearAPR2 = "-";
}
if (x.kpiPeriod.year === yearNow - 3 && x.kpiPeriod.durationKPI === "OCT") {
acc.summaryPointOCT2 = _summaryPointOct.length > 3
? Extension.ToThaiNumber(_summaryPointOct[3].toString())
: null;
acc.textPointOCT2 = _summaryPointOct.length > 3 ? Extension.textPoint(_summaryPointOct[3]) : null;
acc.periodOCT2 = x.kpiPeriod.durationKPI;
acc.yearOCT2 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointOCT2 = "-";
acc.textPointOCT2 = "-";
acc.periodOCT2 = "-";
acc.yearOCT2 = "-";
}
if (x.kpiPeriod.year === yearNow - 2 && x.kpiPeriod.durationKPI === "APR") {
acc.summaryPointAPR3 = _summaryPointApr.length > 2
? Extension.ToThaiNumber(_summaryPointApr[2].toString())
: null;
acc.textPointAPR3 = _summaryPointApr.length > 2 ? Extension.textPoint(_summaryPointApr[2]) : null;
acc.periodAPR3 = x.kpiPeriod.durationKPI;
acc.yearAPR3 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointAPR3 = "-";
acc.textPointAPR3 = "-";
acc.periodAPR3 = "-";
acc.yearAPR3 = "-";
}
if (x.kpiPeriod.year === yearNow - 2 && x.kpiPeriod.durationKPI === "OCT") {
acc.summaryPointOCT3 = _summaryPointOct.length > 2
? Extension.ToThaiNumber(_summaryPointOct[2].toString())
: null;
acc.textPointOCT3 = _summaryPointOct.length > 2 ? Extension.textPoint(_summaryPointOct[2]) : null;
acc.periodOCT3 = x.kpiPeriod.durationKPI;
acc.yearOCT3 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointOCT3 = "-";
acc.textPointOCT3 = "-";
acc.periodOCT3 = "-";
acc.yearOCT3 = "-";
}
if (x.kpiPeriod.year === yearNow - 1 && x.kpiPeriod.durationKPI === "APR") {
acc.summaryPointAPR4 = _summaryPointApr.length > 1
? Extension.ToThaiNumber(_summaryPointApr[1].toString())
: null;
acc.textPointAPR4 = _summaryPointApr.length > 1 ? Extension.textPoint(_summaryPointApr[1]) : null;
acc.periodAPR4 = x.kpiPeriod.durationKPI;
acc.yearAPR4 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointAPR4 = "-";
acc.textPointAPR4 = "-";
acc.periodAPR4 = "-";
acc.yearAPR4 = "-";
}
if (x.kpiPeriod.year === yearNow - 1 && x.kpiPeriod.durationKPI === "OCT") {
acc.summaryPointOCT4 = _summaryPointOct.length > 1
? Extension.ToThaiNumber(_summaryPointOct[1].toString())
: null;
acc.textPointOCT4 = _summaryPointOct.length > 1 ? Extension.textPoint(_summaryPointOct[1]) : null;
acc.periodOCT4 = x.kpiPeriod.durationKPI;
acc.yearOCT4 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointOCT4 = "-";
acc.textPointOCT4 = "-";
acc.periodOCT4 = "-";
acc.yearOCT4 = "-";
}
if (x.kpiPeriod.year === yearNow && x.kpiPeriod.durationKPI === "APR") {
acc.summaryPointAPR5 = _summaryPointApr.length > 0
? Extension.ToThaiNumber(_summaryPointApr[0].toString())
: null;
acc.textPointAPR5 = _summaryPointApr.length > 0 ? Extension.textPoint(_summaryPointApr[0]) : null;
acc.periodAPR5 = x.kpiPeriod.durationKPI;
acc.yearAPR5 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointAPR5 = "-";
acc.textPointAPR5 = "-";
acc.periodAPR5 = "-";
acc.yearAPR5 = "-";
}
if (x.kpiPeriod.year === yearNow && x.kpiPeriod.durationKPI === "OCT") {
acc.summaryPointOCT5 = _summaryPointOct.length > 0
? Extension.ToThaiNumber(_summaryPointOct[0].toString())
: null;
acc.textPointOCT5 = _summaryPointOct.length > 0 ? Extension.textPoint(_summaryPointOct[0]) : null;
acc.periodOCT5 = x.kpiPeriod.durationKPI;
acc.yearOCT5 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointOCT5 = "-";
acc.textPointOCT5 = "-";
acc.periodOCT5 = "-";
acc.yearOCT5 = "-";
}
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();
let _summaryPointApr: number[] = [];
let _summaryPointOct: number[] = [];
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();
for (let i = 0; i < 3; i++) {
let _calAvg = await this.kpiUserEvaluationRepository
.createQueryBuilder("evaluation")
.leftJoin("evaluation.kpiPeriod", "kpiPeriod")
.where("evaluation.profileId = :profileId", { profileId: requestBody.profileId })
.andWhere("evaluation.evaluationStatus = :status", { status: "KP7" })
.andWhere("kpiPeriod.year = :year", { year: yearNow - i })
.andWhere("kpiPeriod.durationKPI = :duration", { duration: "APR" })
.select("AVG(evaluation.summaryPoint)", "average")
.getRawOne();
_summaryPointApr.push(_calAvg.average);
}
for (let i = 0; i < 3; i++) {
let _calAvg = await this.kpiUserEvaluationRepository
.createQueryBuilder("evaluation")
.leftJoin("evaluation.kpiPeriod", "kpiPeriod")
.where("evaluation.profileId = :profileId", { profileId: requestBody.profileId })
.andWhere("evaluation.evaluationStatus = :status", { status: "KP7" })
.andWhere("kpiPeriod.year = :year", { year: yearNow - i })
.andWhere("kpiPeriod.durationKPI = :duration", { duration: "OCT" })
.select("AVG(evaluation.summaryPoint)", "average")
.getRawOne();
_summaryPointOct.push(_calAvg.average);
}
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 == "" || affiliation == null ? "-" : affiliation;
} else {
acc.fullName = "-";
acc.position = "-";
acc.posType = "-";
acc.posLevel = "-";
acc.affiliation = "-";
}
if (x.kpiPeriod.year === yearNow - 2 && x.kpiPeriod.durationKPI === "APR") {
acc.summaryPointAPR1 = _summaryPointApr.length > 2
? Extension.ToThaiNumber(_summaryPointApr[2].toString())
: null;
acc.textPointAPR1 = _summaryPointApr.length > 2 ? Extension.textPoint(_summaryPointApr[2]) : null;
acc.periodAPR1 = x.kpiPeriod.durationKPI;
acc.yearAPR1 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointAPR1 = "-";
acc.textPointAPR1 = "-";
acc.periodAPR1 = "-";
acc.yearAPR1 = "-";
}
if (x.kpiPeriod.year === yearNow - 2 && x.kpiPeriod.durationKPI === "OCT") {
acc.summaryPointOCT1 = _summaryPointOct.length > 2
? Extension.ToThaiNumber(_summaryPointOct[2].toString())
: null;
acc.textPointOCT1 = _summaryPointOct.length > 2 ? Extension.textPoint(_summaryPointOct[2]) : null;
acc.periodOCT1 = x.kpiPeriod.durationKPI;
acc.yearOCT1 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointOCT1 = "-";
acc.textPointOCT1 = "-";
acc.periodOCT1 = "-";
acc.yearOCT1 = "-";
}
if (x.kpiPeriod.year === yearNow - 1 && x.kpiPeriod.durationKPI === "APR") {
acc.summaryPointAPR2 = _summaryPointApr.length > 1
? Extension.ToThaiNumber(_summaryPointApr[1].toString())
: null;
acc.textPointAPR2 = _summaryPointApr.length > 1 ? Extension.textPoint(_summaryPointApr[1]) : null;
acc.periodAPR2 = x.kpiPeriod.durationKPI;
acc.yearAPR2 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointAPR2 = "-";
acc.textPointAPR2 = "-";
acc.periodAPR2 = "-";
acc.yearAPR2 = "-";
}
if (x.kpiPeriod.year === yearNow - 1 && x.kpiPeriod.durationKPI === "OCT") {
acc.summaryPointOCT2 = _summaryPointOct.length > 1
? Extension.ToThaiNumber(_summaryPointOct[1].toString())
: null;
acc.textPointOCT2 = _summaryPointOct.length > 1 ? Extension.textPoint(_summaryPointOct[1]) : null;
acc.periodOCT2 = x.kpiPeriod.durationKPI;
acc.yearOCT2 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointOCT2 = "-";
acc.textPointOCT2 = "-";
acc.periodOCT2 = "-";
acc.yearOCT2 = "-";
}
if (x.kpiPeriod.year === yearNow && x.kpiPeriod.durationKPI === "APR") {
acc.summaryPointAPR3 = _summaryPointApr.length > 0
? Extension.ToThaiNumber(_summaryPointApr[0].toString())
: null;
acc.textPointAPR3 = _summaryPointApr.length > 0 ? Extension.textPoint(_summaryPointApr[0]) : null;
acc.periodAPR3 = x.kpiPeriod.durationKPI;
acc.yearAPR3 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointAPR3 = "-";
acc.textPointAPR3 = "-";
acc.periodAPR3 = "-";
acc.yearAPR3 = "-";
}
if (x.kpiPeriod.year === yearNow && x.kpiPeriod.durationKPI === "OCT") {
acc.summaryPointOCT3 = _summaryPointOct.length > 0
? Extension.ToThaiNumber(_summaryPointOct[0].toString())
: null;
acc.textPointOCT3 = _summaryPointOct.length > 0 ? Extension.textPoint(_summaryPointOct[0]) : null;
acc.periodOCT3 = x.kpiPeriod.durationKPI;
acc.yearOCT3 = x.kpiPeriod.year
? Extension.ToThaiNumber(Extension.ToThaiYear(x.kpiPeriod.year).toString())
: null;
} else {
acc.summaryPointOCT3 = "-";
acc.textPointOCT3 = "-";
acc.periodOCT3 = "-";
acc.yearOCT3 = "-";
}
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) => {
const target_ = await this.kpiUserDevelopmentRepository.findOne({
where: { kpiUserEvaluationId: x.id },
select: ["id","kpiUserEvaluationId","target","achievement0","achievement5","achievement10"],
});
/*รอ 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: target_ ? target_.target: "-",
timeEvaluator: x.timeEvaluator ? Extension.ToThaiNumber(x.timeEvaluator) : "-",
developResults: target_
? [
target_.achievement10 ? `${target_.achievement10}(10)` : "",
target_.achievement5 ? `${target_.achievement5}(5)` : "",
target_.achievement0 ? `${target_.achievement0}(0)` : ""
]
.filter(Boolean)
.join("\n")
: "",
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) },
});
let durationKpi_APR = period_ && period_.durationKPI == "APR" ? `🗹` : ``
let durationKpi_OCT = period_ && period_.durationKPI == "OCT" ? `🗹` : ``
if (period_ && !period1 && !period2) {
if (period_?.durationKPI === "APR") {
period1 = `${durationKpi_APR} รอบที่ ๑ ตั้งแต่ ${Extension.ToThaiFullDate2(period_.startDate)} ถึง ${Extension.ToThaiFullDate2(period_.endDate)}`;
let _period2 = await this.kpiPeriodRepository.findOne({
where: {
year: period_.year,
durationKPI: "OCT",
},
});
period2 = _period2
? `${durationKpi_OCT} รอบที่ ๒ ตั้งแต่ ${Extension.ToThaiFullDate2(_period2?.startDate)} ถึง ${Extension.ToThaiFullDate2(_period2?.endDate)}`
: `${durationKpi_OCT} รอบที่ ๒ ตั้งแต่ ๑ เมษายน ${Extension.ToThaiYear(period_.year)} ถึง ๓๐ กันยายน ${Extension.ToThaiYear(period_.year)}`;
}
else if (period_?.durationKPI === "OCT") {
period2 = `${durationKpi_OCT} รอบที่ ๒ ตั้งแต่ ${Extension.ToThaiFullDate2(period_.startDate)} ถึง ${Extension.ToThaiFullDate2(period_.endDate)}`;
let _period1 = await this.kpiPeriodRepository.findOne({
where: {
year: period_.year,
durationKPI: "APR",
},
});
period1 = _period1
? `${durationKpi_APR} รอบที่ ๑ ตั้งแต่ ${Extension.ToThaiFullDate2(_period1?.startDate)} ถึง ${Extension.ToThaiFullDate2(_period1?.endDate)}`
: `${durationKpi_APR} รอบที่ ๑ ตั้งแต่ ๑ ตุลาคม ${Extension.ToThaiYear(period_.year-1)} ถึง ๓๑ มีนาคม ${Extension.ToThaiYear(period_.year)}`;
}
}
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
.createQueryBuilder("dev")
.leftJoinAndSelect("dev.kpiUserEvaluation", "eval")
.where("dev.kpiUserEvaluationId IN (:...ids)", { ids: profileEvaluationIds.map((x: any) => x.id) })
.select([
"dev.id",
"dev.name",
"dev.target",
"dev.summary",
"dev.point",
"dev.achievement0",
"dev.achievement5",
"dev.achievement10",
"dev.isDevelopment10",
"dev.isDevelopment20",
"dev.isDevelopment70",
"eval.developEvaluator",
"eval.timeEvaluator",
"eval.reasonEvaluator",
])
.getMany();
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()),
id: development.id,
name: development.name,
target: development.target,
summary: development.summary
? Extension.ToThaiNumber(development.summary.toString())
: null,
point: development.point
? Extension.ToThaiNumber(development.point.toString())
: null,
achievement0: development.achievement0,
achievement5: development.achievement5,
achievement10: development.achievement10,
developResults: development
? [
development.achievement10 ? `${development.achievement10}(10)` : "",
development.achievement5 ? `${development.achievement5}(5)` : "",
development.achievement0 ? `${development.achievement0}(0)` : ""
]
.filter(Boolean)
.join("\n")
: "",
isDevelopment10: development.isDevelopment10,
isDevelopment20: development.isDevelopment20,
isDevelopment70: development.isDevelopment70,
pointText:
development.point == 0
? development.achievement0
: development.point == 5
? development.achievement5
: development.point == 10
? development.achievement10
: null,
developEvaluator: development.kpiUserEvaluation?.developEvaluator ?? "-",
timeEvaluator: development.kpiUserEvaluation?.timeEvaluator ?? "-",
reasonEvaluator: development.kpiUserEvaluation?.reasonEvaluator ?? "-",
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 == "" || affiliation == null ? "-" : 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
: [
{
no: "-",
name: "-",
target: "-",
summary: "-",
},
],
};
}
if (requestBody.type == "KPI9") {
templateName = "KPI9";
reportName = "KPI9";
if (data && data.kpiUserEvaluations.length == 0) {
//แก้ไขกรณีมีผู้ประเมินที่มีผลการประเมินดีเด่น แต่ RootId ไม่ตรงกับหน่วยงานที่ filter
let userEvaInRoot: any;
let userEvaOutRoot: any[];
userEvaInRoot = await this.kpiUserEvaluationRepository.findOne({
select: ["org"],
where: {
kpiPeriodId: String(requestBody.periodId),
orgId: String(requestBody.root),
},
});
if (userEvaInRoot) {
userEvaOutRoot = await this.kpiUserEvaluationRepository.find({
where: {
kpiPeriodId: String(requestBody.periodId),
orgId: Not(String(requestBody.root)),
org: userEvaInRoot.org,
evaluationStatus: "KP7",
},
});
if (userEvaOutRoot.length > 0) {
data.kpiUserEvaluations = userEvaOutRoot;
}
}
}
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: "-",
},
];
const date = new Date();
formattedData = {
root: data && data.rootName != null ? data.rootName : "-",
period: data?.durationKPI == "APR" ? "๑" : data?.durationKPI == "OCT" ? "๒" : "-",
year: data.year ? Extension.ToThaiNumber((data.year + 543).toString()) : "-",
date: Extension.ToThaiNumber(Extension.ToThaiFullDate2(date).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),
fullnameCommander:
(kpiUserEvaluation.prefixCommander == null ? "" : kpiUserEvaluation.prefixCommander) +
"" +
(kpiUserEvaluation.firstNameCommander == null ? "" : kpiUserEvaluation.firstNameCommander) +
" " +
(kpiUserEvaluation.lastNameCommander == null ? "" : kpiUserEvaluation.lastNameCommander),
fullnameCommanderHigh:
(kpiUserEvaluation.prefixCommanderHigh == null
? ""
: kpiUserEvaluation.prefixCommanderHigh) +
"" +
(kpiUserEvaluation.firstNameCommanderHigh == null
? ""
: kpiUserEvaluation.firstNameCommanderHigh) +
" " +
(kpiUserEvaluation.lastNameCommanderHigh == null
? ""
: kpiUserEvaluation.lastNameCommanderHigh),
positionEvaluator: kpiUserEvaluation.positionEvaluator,
positionCommander: kpiUserEvaluation.positionCommander,
positionCommanderHigh: kpiUserEvaluation.positionCommanderHigh,
posTypeNameEvaluator: kpiUserEvaluation.posTypeNameEvaluator,
posLevelNameEvaluator: kpiUserEvaluation.posLevelNameEvaluator,
orgEvaluator: kpiUserEvaluation.orgEvaluator,
year: Extension.ToThaiNumber(String(Extension.ToThaiYear(kpiUserEvaluation.kpiPeriod.year))),
yearOld: Extension.ToThaiNumber(
String(Extension.ToThaiYear(kpiUserEvaluation.kpiPeriod.year + 1)),
),
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,
});
}
/**
* API Report แบบกำหนดข้อตกลงการประเมินผลสัมฤทธิ์ของงานและพฤติกรรมการปฏิบัติราชการ
*
* @summary Report แบบกำหนดข้อตกลงการประเมินผลสัมฤทธิ์ของงานและพฤติกรรมการปฏิบัติราชการ
*
* @param {string} id Guid, *Id รายการประเมินผลการปฏิบัติราชการระดับบุคคล
*/
@Get("list/{id}")
async GetReportKpiListGroup1(@Path() id?: string) {
const kpiUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
relations: [
"kpiPeriod",
"kpiUserPlanneds",
"kpiUserRoles",
"kpiUserSpecials",
"kpiUserCapacitys",
"kpiUserDevelopments"
],
where: {
id: id
},
order: {
createdAt: "DESC",
kpiUserPlanneds: {
createdAt: "DESC"
},
kpiUserSpecials: {
createdAt: "DESC"
},
kpiUserCapacitys: {
createdAt: "DESC"
}
}
});
if (!kpiUserEvaluation) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ประเมิน");
const isGroup2: boolean = ["ทั่วไป", "วิชาการ"].includes(kpiUserEvaluation.posTypeName);
const Plan = await this.kpiPlanRepository.find();
const Role = await this.kpiRoleRepository.find();
const Capacitys = await this.kpiCapacityRepository.find({
where: {
type: In(["HEAD", "EXECUTIVE", "GROUP"])
}
});
let durationKpi_APR = kpiUserEvaluation.kpiPeriod && kpiUserEvaluation.kpiPeriod.durationKPI == "APR" ? `🗹` : ``
let durationKpi_OCT = kpiUserEvaluation.kpiPeriod && kpiUserEvaluation.kpiPeriod.durationKPI == "OCT" ? `🗹` : ``
let period1:string="";
let period2:string="";
if(kpiUserEvaluation.kpiPeriod) {
if (kpiUserEvaluation.kpiPeriod.durationKPI === "APR") {
period1 = `${durationKpi_APR} รอบที่ ๑ ตั้งแต่ ${Extension.ToThaiFullDate2(kpiUserEvaluation.kpiPeriod.startDate)} ถึง ${Extension.ToThaiFullDate2(kpiUserEvaluation.kpiPeriod.endDate)}`;
let _period2 = await this.kpiPeriodRepository.findOne({
where: {
year: kpiUserEvaluation.kpiPeriod.year,
durationKPI: "OCT",
},
});
period2 = _period2
? `${durationKpi_OCT} รอบที่ ๒ ตั้งแต่ ${Extension.ToThaiFullDate2(_period2?.startDate)} ถึง ${Extension.ToThaiFullDate2(_period2?.endDate)}`
: `${durationKpi_OCT} รอบที่ ๒ ตั้งแต่ ๑ เมษายน ${Extension.ToThaiYear(kpiUserEvaluation.kpiPeriod.year)} ถึง ๓๐ กันยายน ${Extension.ToThaiYear(kpiUserEvaluation.kpiPeriod.year)}`;
}
else if (kpiUserEvaluation.kpiPeriod.durationKPI === "OCT") {
period2 = `${durationKpi_OCT} รอบที่ ๒ ตั้งแต่ ${Extension.ToThaiFullDate2(kpiUserEvaluation.kpiPeriod.startDate)} ถึง ${Extension.ToThaiFullDate2(kpiUserEvaluation.kpiPeriod.endDate)}`;
let _period1 = await this.kpiPeriodRepository.findOne({
where: {
year: kpiUserEvaluation.kpiPeriod.year,
durationKPI: "APR",
},
});
period1 = _period1
? `${durationKpi_APR} รอบที่ ๑ ตั้งแต่ ${Extension.ToThaiFullDate2(_period1?.startDate)} ถึง ${Extension.ToThaiFullDate2(_period1?.endDate)}`
: `${durationKpi_APR} รอบที่ ๑ ตั้งแต่ ๑ ตุลาคม ${Extension.ToThaiYear(kpiUserEvaluation.kpiPeriod.year-1)} ถึง ๓๑ มีนาคม ${Extension.ToThaiYear(kpiUserEvaluation.kpiPeriod.year)}`;
}
}
let mapData = {
year: kpiUserEvaluation.kpiPeriod
? Extension.ToThaiNumber((kpiUserEvaluation.kpiPeriod.year+543).toString())
: "-",
period: period1 != "" && period2 != ""
? Extension.ToThaiNumber(`${period1} ${period2}`)
: "-",
fullName: `${kpiUserEvaluation.prefix}${kpiUserEvaluation.firstName} ${kpiUserEvaluation.lastName}`,
position: kpiUserEvaluation.position ? kpiUserEvaluation.position : "-",
posTypeName: kpiUserEvaluation.posTypeName ? kpiUserEvaluation.posTypeName : "-",
posLevelName: kpiUserEvaluation.posLevelName ? kpiUserEvaluation.posLevelName : "-",
org: kpiUserEvaluation.org ? kpiUserEvaluation.org : "-",
fullnameEvaluator:
`${kpiUserEvaluation.prefixEvaluator}${kpiUserEvaluation.firstNameEvaluator} ${kpiUserEvaluation.lastNameEvaluator}`,
positionEvaluator: kpiUserEvaluation.positionEvaluator,
posTypeNameEvaluator: kpiUserEvaluation.posTypeNameEvaluator,
posLevelNameEvaluator: kpiUserEvaluation.posLevelNameEvaluator,
orgEvaluator: kpiUserEvaluation.orgEvaluator,
fullnameCommander: kpiUserEvaluation.prefixCommander != null && kpiUserEvaluation.firstNameCommander != null
&& kpiUserEvaluation.lastNameCommander != null
? `${kpiUserEvaluation.prefixCommander}${kpiUserEvaluation.firstNameCommander} ${kpiUserEvaluation.lastNameCommander}`
: ` `,
positionCommander: kpiUserEvaluation.positionCommander
? kpiUserEvaluation.positionCommander
: ` `,
fullnameCommanderHigh: kpiUserEvaluation.prefixCommanderHigh != null && kpiUserEvaluation.firstNameCommanderHigh != null
&& kpiUserEvaluation.lastNameCommanderHigh != null
? `${kpiUserEvaluation.prefixCommanderHigh}${kpiUserEvaluation.firstNameCommanderHigh} ${kpiUserEvaluation.lastNameCommanderHigh}`
: ` `,
positionCommanderHigh: kpiUserEvaluation.positionCommanderHigh
? kpiUserEvaluation.positionCommanderHigh
: ` `,
planneds: kpiUserEvaluation.kpiUserPlanneds.length != 0
? kpiUserEvaluation.kpiUserPlanneds
.map(
x => ({
name: Plan.find(y => y.id === x.kpiPlanId)?.includingName || "-",
target: x.target ? Extension.ToThaiNumber(x.target.toLocaleString()) : "-",
point1: x.point == 1 ? "🗹" : "☐",
point2: x.point == 2 ? "🗹" : "☐",
point3: x.point == 3 ? "🗹" : "☐",
point4: x.point == 4 ? "🗹" : "☐",
point5: x.point == 5 ? "🗹" : "☐",
weight: x.weight ? Extension.ToThaiNumber(x.weight.toLocaleString()) : "-",
achievement: x.point ? Extension.ToThaiNumber(`ระดับ ${x.point}`) : "-",
summary: x.summary ? Extension.ToThaiNumber(x.summary.toLocaleString()) : "-",
})
)
: [{
name: "-",
target: "-",
point1: "☐",
point2: "☐",
point3: "☐",
point4: "☐",
point5: "☐",
weight: "-",
achievement: "-",
summary: "-",
}],
roles: kpiUserEvaluation.kpiUserRoles.length != 0
? kpiUserEvaluation.kpiUserRoles
.map(
x => ({
name: Role.find(y => y.id === x.kpiRoleId)?.includingName || "-",
target: x.target ? Extension.ToThaiNumber(x.target.toLocaleString()) : "-",
point1: x.point == 1 ? "🗹" : "☐",
point2: x.point == 2 ? "🗹" : "☐",
point3: x.point == 3 ? "🗹" : "☐",
point4: x.point == 4 ? "🗹" : "☐",
point5: x.point == 5 ? "🗹" : "☐",
weight: x.weight ? Extension.ToThaiNumber(x.weight.toLocaleString()) : "-",
achievement: x.point ? Extension.ToThaiNumber(`ระดับ ${x.point}`) : "-",
summary: x.summary ? Extension.ToThaiNumber(x.summary.toLocaleString()) : "-",
})
)
: [{
name: "-",
target: "-",
point1: "☐",
point2: "☐",
point3: "☐",
point4: "☐",
point5: "☐",
weight: "-",
achievement: "-",
summary: "-",
}],
specials: kpiUserEvaluation.kpiUserSpecials.length != 0
? kpiUserEvaluation.kpiUserSpecials
.map(
x => ({
name: x.includingName ?? "-",
target: x.target ? Extension.ToThaiNumber(x.target.toLocaleString()) : "-",
point1: x.point == 1 ? "🗹" : "☐",
point2: x.point == 2 ? "🗹" : "☐",
point3: x.point == 3 ? "🗹" : "☐",
point4: x.point == 4 ? "🗹" : "☐",
point5: x.point == 5 ? "🗹" : "☐",
weight: x.weight ? Extension.ToThaiNumber(x.weight.toLocaleString()) : "-",
achievement: x.point ? Extension.ToThaiNumber(`ระดับ ${x.point}`) : "-",
summary: x.summary ? Extension.ToThaiNumber(x.summary.toLocaleString()) : "-",
})
)
: [{
name: "-",
target: "-",
point1: "☐",
point2: "☐",
point3: "☐",
point4: "☐",
point5: "☐",
weight: "-",
achievement: "-",
summary: "-",
}],
develops: kpiUserEvaluation.kpiUserDevelopments.length != 0
? kpiUserEvaluation.kpiUserDevelopments
.map(
x => ({
name: x.name ?? "-",
target: x.target ? Extension.ToThaiNumber(x.target.toLocaleString()) : "-",
achievement10: x.point == 10 ? `🗹 ${x.achievement10}` : `${x.achievement10}`,
achievement5: x.point == 5 ? `🗹 ${x.achievement5}` : `${x.achievement5}`,
achievement0: x.point == 0 ? `🗹 ${x.achievement0}` : `${x.achievement0}`,
isDevelopment70: x.isDevelopment70 == true ? `🗹 ๗๐ การลงมือปฏิบัติ(โดยผู้บังคับบัญชามอบหมาย)` : `☐ ๗๐ การลงมือปฏิบัติ(โดยผู้บังคับบัญชามอบหมาย)`,
isDevelopment20: x.isDevelopment20 == true ? `🗹 ๒๐ การเรียนรู้จากผู้อื่น Coach/Mentor/Consulting` : `☐ ๒๐ การเรียนรู้จากผู้อื่น Coach/Mentor/Consulting`,
isDevelopment10: x.isDevelopment10 == true ? `🗹 ๑๐ การฝึกอบรมอื่น ๆ` : `☐ ๑๐ การฝึกอบรมอื่น ๆ`,
point: x.point ? Extension.ToThaiNumber(x.point.toLocaleString()) : "-",
summary: x.summary ? Extension.ToThaiNumber(x.summary.toLocaleString()) : "-",
})
)
: [{
name: "-",
target: "-",
achievement10: "-",
isDevelopment70: "-",
point: "-",
summary: "-",
}],
headCaps: kpiUserEvaluation.kpiUserCapacitys.length != 0
&& kpiUserEvaluation.kpiUserCapacitys.filter(x => Capacitys.some(y => y.id === x.kpiCapacityId && y.type === "HEAD")).length != 0
? kpiUserEvaluation.kpiUserCapacitys
.filter(
x => Capacitys.some(y => y.id === x.kpiCapacityId && y.type === "HEAD")
)
.map(
x => ({
name: `- ${Capacitys.find(y => y.id === x.kpiCapacityId)?.name ?? ""}`,
level: x.level ? Extension.ToThaiNumber(x.level.toLocaleString()) : "-",
weight: x.weight ? Extension.ToThaiNumber(x.weight.toLocaleString()) : "-",
point1: x.point == 1 ? "🗹" : "☐",
point2: x.point == 2 ? "🗹" : "☐",
point3: x.point == 3 ? "🗹" : "☐",
point4: x.point == 4 ? "🗹" : "☐",
point5: x.point == 5 ? "🗹" : "☐",
summary: x.summary ? Extension.ToThaiNumber(x.summary.toLocaleString()) : "-",
})
)
: [{
name: "-",
level: "-",
weight: "-",
point1: "☐",
point2: "☐",
point3: "☐",
point4: "☐",
point5: "☐",
summary: "-",
}],
executiveCaps: kpiUserEvaluation.kpiUserCapacitys.length != 0
&& kpiUserEvaluation.kpiUserCapacitys.filter(x => Capacitys.some(y => y.id === x.kpiCapacityId && y.type === "EXECUTIVE")).length != 0
? kpiUserEvaluation.kpiUserCapacitys
.filter(
x => Capacitys.some(y => y.id === x.kpiCapacityId && y.type === "EXECUTIVE")
)
.map(
x => ({
name: `- ${Capacitys.find(y => y.id === x.kpiCapacityId)?.name ?? ""}`,
level: x.level ? Extension.ToThaiNumber(x.level.toLocaleString()) : "-",
weight: x.weight ? Extension.ToThaiNumber(x.weight.toLocaleString()) : "-",
point1: x.point == 1 ? "🗹" : "☐",
point2: x.point == 2 ? "🗹" : "☐",
point3: x.point == 3 ? "🗹" : "☐",
point4: x.point == 4 ? "🗹" : "☐",
point5: x.point == 5 ? "🗹" : "☐",
summary: x.summary ? Extension.ToThaiNumber(x.summary.toLocaleString()) : "-",
})
)
: [{
name: "-",
level: "-",
weight: "-",
point1: "☐",
point2: "☐",
point3: "☐",
point4: "☐",
point5: "☐",
summary: "-",
}],
groupCaps: kpiUserEvaluation.kpiUserCapacitys.length != 0
&& kpiUserEvaluation.kpiUserCapacitys.filter(x => Capacitys.some(y => y.id === x.kpiCapacityId && y.type === "GROUP")).length != 0
? kpiUserEvaluation.kpiUserCapacitys
.filter(
x => Capacitys.some(y => y.id === x.kpiCapacityId && y.type === "GROUP")
)
.map(
x => ({
name: `- ${Capacitys.find(y => y.id === x.kpiCapacityId)?.name ?? ""}`,
level: x.level ? Extension.ToThaiNumber(x.level.toLocaleString()) : "-",
weight: x.weight ? Extension.ToThaiNumber(x.weight.toLocaleString()) : "-",
point1: x.point == 1 ? "🗹" : "☐",
point2: x.point == 2 ? "🗹" : "☐",
point3: x.point == 3 ? "🗹" : "☐",
point4: x.point == 4 ? "🗹" : "☐",
point5: x.point == 5 ? "🗹" : "☐",
summary: x.summary ? Extension.ToThaiNumber(x.summary.toLocaleString()) : "-",
})
)
: [{
name: "-",
level: "-",
weight: "-",
point1: "☐",
point2: "☐",
point3: "☐",
point4: "☐",
point5: "☐",
summary: "-",
}],
//group1 only
summaryPlanneds: Extension.ToThaiNumber(kpiUserEvaluation.kpiUserPlanneds.reduce((sum, r) => sum + r.summary, 0).toLocaleString()),
totalPlan: kpiUserEvaluation.kpiUserPlanneds.reduce((sum, p) => sum + p.summary, 0) > 60
? Extension.ToThaiNumber((60).toLocaleString())
: Extension.ToThaiNumber(kpiUserEvaluation.kpiUserPlanneds.reduce((sum, p) => sum + p.summary, 0).toLocaleString()),
totalSpec: kpiUserEvaluation.kpiUserSpecials.reduce((sum, p) => sum + p.summary, 0) > 20
? Extension.ToThaiNumber((20).toLocaleString())
: Extension.ToThaiNumber(kpiUserEvaluation.kpiUserSpecials.reduce((sum, p) => sum + p.summary, 0).toLocaleString()),
//
summaryPoint1:
Extension.ToThaiNumber((kpiUserEvaluation.kpiUserPlanneds.reduce((sum, p) => sum + p.summary, 0)
+kpiUserEvaluation.kpiUserRoles.reduce((sum, r) => sum + r.summary, 0)
+kpiUserEvaluation.kpiUserSpecials.reduce((sum, s) => sum + s.summary, 0)).toLocaleString()),
totalPoint1: kpiUserEvaluation.totalPoint1
? Extension.ToThaiNumber(kpiUserEvaluation.totalPoint1.toLocaleString())
: "-",
totalPoint2_1: kpiUserEvaluation.totalPoint2_1
? Extension.ToThaiNumber(kpiUserEvaluation.totalPoint2_1.toLocaleString())
: "-",
totalPoint2_2: kpiUserEvaluation.totalPoint2_2
? Extension.ToThaiNumber(kpiUserEvaluation.totalPoint2_2.toLocaleString())
: "-",
totalPoint2: kpiUserEvaluation.totalPoint2_1 && kpiUserEvaluation.totalPoint2_2
? Extension.ToThaiNumber((kpiUserEvaluation.totalPoint2_1 + kpiUserEvaluation.totalPoint2_2).toLocaleString())
: "-",
summaryPoint: kpiUserEvaluation.summaryPoint
? Extension.ToThaiNumber(kpiUserEvaluation.summaryPoint.toLocaleString())
: "-",
};
return new HttpSuccess({
template: !isGroup2 ? "KPI_ListGroup1" : "KPI_ListGroup2&3",
reportName: !isGroup2 ? "KPI_ListGroup1" : "KPI_ListGroup2&3",
data: mapData,
});
}
}