1257 lines
54 KiB
TypeScript
1257 lines
54 KiB
TypeScript
import {
|
||
Controller,
|
||
Get,
|
||
Post,
|
||
Put,
|
||
Delete,
|
||
Route,
|
||
Security,
|
||
Tags,
|
||
Body,
|
||
Path,
|
||
Request,
|
||
Query,
|
||
} from "tsoa";
|
||
import { AppDataSource } from "../database/data-source";
|
||
import { Brackets, IsNull, Not, 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 { off } from "process";
|
||
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;
|
||
|
||
if (requestBody.root && requestBody.periodId) {
|
||
// data = await this.kpiPeriodRepository.createQueryBuilder("period")
|
||
// .leftJoinAndSelect("period.kpiUserEvaluations", "kpiUserEvaluations")
|
||
// .where("period.id = :periodId", { periodId: requestBody.periodId })
|
||
// .andWhere("kpiUserEvaluations.orgId = :orgId", { orgId: requestBody.root })
|
||
// .getOne();
|
||
|
||
// if(data == null || data == undefined){
|
||
// throw new HttpError(HttpStatusCode.NOT_FOUND, `ไม่พบข้อมูลรายการประเมินผลการปฏิบัติราชการ ของหน่วยงานที่เลือก`);
|
||
// }
|
||
data = await this.kpiPeriodRepository.findOne({
|
||
where: { id: requestBody.periodId },
|
||
});
|
||
dataKpiUserEvaluations = await this.kpiUserEvaluationRepository.find({
|
||
where: {
|
||
kpiPeriodId: requestBody.periodId,
|
||
orgId: requestBody.root,
|
||
evaluationStatus: "KP7"
|
||
},
|
||
});
|
||
data = {
|
||
id: data.id,
|
||
year: data.year,
|
||
durationKPI: data.durationKPI,
|
||
startDate: data.startDate,
|
||
endDate: data.endDate,
|
||
kpiUserEvaluations: dataKpiUserEvaluations,
|
||
};
|
||
|
||
if (data.durationKPI == "APR") {
|
||
period1 = `${Extension.ToThaiFullDate2(data.startDate)} ถึง ${Extension.ToThaiFullDate2(data.endDate)}`;
|
||
let _period2 = await this.kpiPeriodRepository.findOne({
|
||
where: {
|
||
year: data.year,
|
||
durationKPI: "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),
|
||
// child1Id: "" || IsNull(),
|
||
// child2Id: "" || IsNull(),
|
||
// child3Id: "" || IsNull(),
|
||
// child4Id: "" || IsNull(),
|
||
},
|
||
});
|
||
const resultAll =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(userEvaluationOrg.length.toString())
|
||
: "๐";
|
||
const result =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(
|
||
userEvaluationOrg
|
||
.filter((x: any) => x.evaluationResults != "PENDING")
|
||
.length.toString(),
|
||
)
|
||
: "๐";
|
||
const excellent =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(
|
||
userEvaluationOrg
|
||
.filter((x: any) => x.evaluationResults == "EXCELLENT")
|
||
.length.toString(),
|
||
)
|
||
: "๐";
|
||
const verygood =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(
|
||
userEvaluationOrg
|
||
.filter((x: any) => x.evaluationResults == "VERY_GOOD")
|
||
.length.toString(),
|
||
)
|
||
: "๐";
|
||
const good =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(
|
||
userEvaluationOrg.filter((x: any) => x.evaluationResults == "GOOD").length.toString(),
|
||
)
|
||
: "๐";
|
||
const fair =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(
|
||
userEvaluationOrg.filter((x: any) => x.evaluationResults == "FAIR").length.toString(),
|
||
)
|
||
: "๐";
|
||
const improvment =
|
||
userEvaluationOrg.length > 0
|
||
? Extension.ToThaiNumber(
|
||
userEvaluationOrg
|
||
.filter((x: any) => x.evaluationResults == "IMPROVEMENT")
|
||
.length.toString(),
|
||
)
|
||
: "๐";
|
||
/*END ROOT*/
|
||
formattedData = {
|
||
year: data?.year ? Extension.ToThaiNumber((data.year + 543).toString()) : "-",
|
||
period1: Extension.ToThaiNumber(period1),
|
||
period2: Extension.ToThaiNumber(period2),
|
||
durationKPI: data?.durationKPI,
|
||
root:
|
||
data?.kpiUserEvaluations != null && data?.kpiUserEvaluations.length > 0
|
||
? data?.kpiUserEvaluations[0].orgEvaluator
|
||
: "-",
|
||
userEvaluations: [
|
||
{
|
||
no: userEvaluationOrg.length > 0 ? "๑" : "",
|
||
root: userEvaluationOrg.length > 0 ? userEvaluationOrg[0].orgEvaluator ?? "" : "-",
|
||
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
|
||
.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
org: x.org ? x.org : "",
|
||
}))
|
||
.filter((x: any) => x.evaluationResults == "EXCELLENT")
|
||
: [{}];
|
||
const verygood =
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations
|
||
.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
org: x.org ? x.org : "",
|
||
}))
|
||
.filter((x: any) => x.evaluationResults == "VERY_GOOD")
|
||
: [{}];
|
||
const good =
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations
|
||
.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
org: x.org ? x.org : "",
|
||
}))
|
||
.filter((x: any) => x.evaluationResults == "GOOD")
|
||
: [{}];
|
||
const fair =
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations
|
||
.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
org: x.org ? x.org : "",
|
||
}))
|
||
.filter((x: any) => x.evaluationResults == "FAIR")
|
||
: [{}];
|
||
const improvment =
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations
|
||
.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
org: x.org ? x.org : "",
|
||
}))
|
||
.filter((x: any) => x.evaluationResults == "IMPROVEMENT")
|
||
: [{}];
|
||
formattedData = {
|
||
year: data?.year ? Extension.ToThaiNumber((data.year + 543).toString()) : "-",
|
||
period1: Extension.ToThaiNumber(period1),
|
||
period2: Extension.ToThaiNumber(period2),
|
||
durationKPI: data?.durationKPI,
|
||
root:
|
||
data?.kpiUserEvaluations != null && data?.kpiUserEvaluations.length > 0
|
||
? data?.kpiUserEvaluations[0].orgEvaluator
|
||
: "-",
|
||
excellents: {
|
||
count:
|
||
data.kpiUserEvaluations.length > 0
|
||
? Extension.ToThaiNumber(excellent.length.toString())
|
||
: "",
|
||
data: excellent,
|
||
},
|
||
verygoods: {
|
||
count:
|
||
data.kpiUserEvaluations.length > 0
|
||
? Extension.ToThaiNumber(verygood.length.toString())
|
||
: "",
|
||
data: verygood,
|
||
},
|
||
goods: {
|
||
count:
|
||
data.kpiUserEvaluations.length > 0
|
||
? Extension.ToThaiNumber(good.length.toString())
|
||
: "",
|
||
data: good,
|
||
},
|
||
fairs: {
|
||
count:
|
||
data.kpiUserEvaluations.length > 0
|
||
? Extension.ToThaiNumber(fair.length.toString())
|
||
: "",
|
||
data: fair,
|
||
},
|
||
improvments: {
|
||
count:
|
||
data.kpiUserEvaluations.length > 0
|
||
? Extension.ToThaiNumber(improvment.length.toString())
|
||
: "",
|
||
data: improvment,
|
||
},
|
||
};
|
||
}
|
||
if (requestBody.type == "KPI3") {
|
||
templateName = "KPI3";
|
||
reportName = "KPI3";
|
||
const userEvaluations_ =
|
||
data?.kpiUserEvaluations?.length > 0
|
||
? data.kpiUserEvaluations.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
point1:
|
||
x.summaryPoint >= 90.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,
|
||
}))
|
||
: [{}];
|
||
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?.kpiUserEvaluations != null && data?.kpiUserEvaluations.length > 0
|
||
? data?.kpiUserEvaluations[0].orgEvaluator
|
||
: "-",
|
||
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();
|
||
if (requestBody.profileId) {
|
||
//ชั่วคราว
|
||
const profileEvaluationNowYearIds = await AppDataSource.getRepository(KpiUserEvaluation)
|
||
.createQueryBuilder("kpiUserEvaluation")
|
||
.leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod")
|
||
// .where("kpiUserEvaluation.profileId = :profileId", { profileId: requestBody.profileId })
|
||
// .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);
|
||
|
||
if (profileEvaluationNowYearIds.length > 0) {
|
||
//ชั่วคราว
|
||
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
|
||
const 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[0]["fullName"] ?? null,
|
||
};
|
||
}
|
||
}
|
||
}
|
||
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("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 = combinedData;
|
||
}
|
||
}
|
||
}
|
||
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("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 = combinedData;
|
||
}
|
||
}
|
||
}
|
||
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 },
|
||
});
|
||
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}${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: "", //วิธีการวัดผลการพัฒนา
|
||
isDev70,
|
||
isDev20,
|
||
isDev10,
|
||
evaluationResults: x.evaluationResults
|
||
? Extension.EvaluationResult(x.evaluationResults)
|
||
: "",
|
||
};
|
||
})
|
||
: [{}],
|
||
);
|
||
formattedData = {
|
||
year: data.year ? Extension.ToThaiNumber((data.year + 543).toString()) : "-",
|
||
period1: Extension.ToThaiNumber(period1),
|
||
period2: Extension.ToThaiNumber(period2),
|
||
durationKPI: data.durationKPI,
|
||
root:
|
||
data.kpiUserEvaluations != null && data.kpiUserEvaluations.length > 0
|
||
? data?.kpiUserEvaluations[0].orgEvaluator
|
||
: "-",
|
||
userEvaluations: userEvaluations_,
|
||
};
|
||
}
|
||
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",
|
||
],
|
||
});
|
||
|
||
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,
|
||
}),
|
||
);
|
||
|
||
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";
|
||
formattedData = {
|
||
root:
|
||
data?.kpiUserEvaluations != null && data?.kpiUserEvaluations.length > 0
|
||
? data?.kpiUserEvaluations[0].orgEvaluator
|
||
: "-",
|
||
period: data?.durationKPI == "APR" ? "๑" : data?.durationKPI == "OCT" ? "๒" : "-",
|
||
year: data.year ? Extension.ToThaiNumber((data.year + 543).toString()) : "-",
|
||
userEvaluations:
|
||
data.kpiUserEvaluations.length > 0
|
||
? data.kpiUserEvaluations.map((x: any, idx: number) => ({
|
||
no: Extension.ToThaiNumber((idx + 1).toString()),
|
||
fullName: `${x.prefix}${x.firstName} ${x.lastName}`,
|
||
position: x.position ? x.position : "",
|
||
posLevelName: x.posLevelName ? x.posLevelName : "",
|
||
evaluationResults: x.evaluationResults
|
||
? Extension.EvaluationResult(x.evaluationResults)
|
||
: "",
|
||
}))
|
||
: [{}],
|
||
};
|
||
}
|
||
|
||
return new HttpSuccess({
|
||
template: templateName,
|
||
reportName: reportName,
|
||
data: formattedData,
|
||
});
|
||
}
|
||
|
||
@Get("kpi-user/{id}")
|
||
async GetReportKpi9(@Path() id?: string) {
|
||
const kpiUserEvaluation = await this.kpiUserEvaluationRepository.findOne({
|
||
relations: ["kpiPeriod"],
|
||
where: { id: id },
|
||
});
|
||
if (!kpiUserEvaluation) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ประเมิน");
|
||
|
||
let formattedData = {
|
||
durationKPI: kpiUserEvaluation.kpiPeriod.durationKPI,
|
||
fullname:
|
||
(kpiUserEvaluation.prefix == null ? "" : kpiUserEvaluation.prefix) +
|
||
"" +
|
||
(kpiUserEvaluation.firstName == null ? "" : kpiUserEvaluation.firstName) +
|
||
" " +
|
||
(kpiUserEvaluation.lastName == null ? "" : kpiUserEvaluation.lastName),
|
||
position: kpiUserEvaluation.position,
|
||
posTypeName: kpiUserEvaluation.posTypeName,
|
||
posLevelName: kpiUserEvaluation.posLevelName,
|
||
org: kpiUserEvaluation.org,
|
||
|
||
fullnameEvaluator:
|
||
(kpiUserEvaluation.prefixEvaluator == null ? "" : kpiUserEvaluation.prefixEvaluator) +
|
||
"" +
|
||
(kpiUserEvaluation.firstNameEvaluator == null ? "" : kpiUserEvaluation.firstNameEvaluator) +
|
||
" " +
|
||
(kpiUserEvaluation.lastNameEvaluator == null ? "" : kpiUserEvaluation.lastNameEvaluator),
|
||
positionEvaluator: kpiUserEvaluation.positionEvaluator,
|
||
posTypeNameEvaluator: kpiUserEvaluation.posTypeNameEvaluator,
|
||
posLevelNameEvaluator: kpiUserEvaluation.posLevelNameEvaluator,
|
||
orgEvaluator: kpiUserEvaluation.orgEvaluator,
|
||
|
||
year: Extension.ToThaiNumber(String(Extension.ToThaiYear(kpiUserEvaluation.kpiPeriod.year))),
|
||
|
||
startDateApr:
|
||
kpiUserEvaluation.kpiPeriod == null ||
|
||
kpiUserEvaluation.kpiPeriod.startDate == null ||
|
||
kpiUserEvaluation.kpiPeriod.durationKPI != "APR"
|
||
? "-"
|
||
: Extension.ToThaiNumber(Extension.ToThaiFullDate(kpiUserEvaluation.kpiPeriod.startDate)),
|
||
endDateApr:
|
||
kpiUserEvaluation.kpiPeriod == null ||
|
||
kpiUserEvaluation.kpiPeriod.endDate == null ||
|
||
kpiUserEvaluation.kpiPeriod.durationKPI != "APR"
|
||
? "-"
|
||
: Extension.ToThaiNumber(Extension.ToThaiFullDate(kpiUserEvaluation.kpiPeriod.endDate)),
|
||
startDateOct:
|
||
kpiUserEvaluation.kpiPeriod == null ||
|
||
kpiUserEvaluation.kpiPeriod.startDate == null ||
|
||
kpiUserEvaluation.kpiPeriod.durationKPI != "OCT"
|
||
? "-"
|
||
: Extension.ToThaiNumber(Extension.ToThaiFullDate(kpiUserEvaluation.kpiPeriod.startDate)),
|
||
endDateOct:
|
||
kpiUserEvaluation.kpiPeriod == null ||
|
||
kpiUserEvaluation.kpiPeriod.endDate == null ||
|
||
kpiUserEvaluation.kpiPeriod.durationKPI != "OCT"
|
||
? "-"
|
||
: Extension.ToThaiNumber(Extension.ToThaiFullDate(kpiUserEvaluation.kpiPeriod.endDate)),
|
||
|
||
evaluationResults: kpiUserEvaluation.evaluationResults,
|
||
totalPoint1: Extension.ToThaiNumber(kpiUserEvaluation.totalPoint1.toLocaleString()),
|
||
totalPoint2: Extension.ToThaiNumber(
|
||
(kpiUserEvaluation.totalPoint2_2 + kpiUserEvaluation.totalPoint2_1).toLocaleString(),
|
||
),
|
||
summaryPoint: Extension.ToThaiNumber(kpiUserEvaluation.summaryPoint.toLocaleString()),
|
||
weightPoint1: Extension.ToThaiNumber(kpiUserEvaluation.weightPoint1.toLocaleString()),
|
||
weightPoint2: Extension.ToThaiNumber(kpiUserEvaluation.weightPoint2.toLocaleString()),
|
||
summaryWeight: Extension.ToThaiNumber(kpiUserEvaluation.summaryWeight.toLocaleString()),
|
||
|
||
topicEvaluator: kpiUserEvaluation.topicEvaluator,
|
||
developEvaluator: kpiUserEvaluation.developEvaluator,
|
||
timeEvaluator: kpiUserEvaluation.timeEvaluator,
|
||
reasonEvaluator: kpiUserEvaluation.reasonEvaluator,
|
||
isReasonCommander: kpiUserEvaluation.isReasonCommander,
|
||
reasonCommander: kpiUserEvaluation.reasonCommander,
|
||
isReasonCommanderHigh: kpiUserEvaluation.isReasonCommanderHigh,
|
||
reasonCommanderHigh: kpiUserEvaluation.reasonCommanderHigh,
|
||
openDate:
|
||
kpiUserEvaluation.openDate == null
|
||
? null
|
||
: Extension.ToThaiNumber(Extension.ToThaiFullDate(kpiUserEvaluation.openDate)),
|
||
};
|
||
|
||
return new HttpSuccess({
|
||
template: "KPIUser",
|
||
reportName: "KPIUser",
|
||
data: formattedData,
|
||
});
|
||
}
|
||
}
|