import { Controller, Get, Route, Security, Tags, SuccessResponse, Response, Path, Query, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { In, LessThan, Brackets } from "typeorm"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { PosMaster } from "../entities/PosMaster"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import Extension from "../interfaces/extension"; import { LeaveType } from "../entities/LeaveType"; import HttpStatus from "../interfaces/http-status"; import { Profile } from "../entities/Profile"; import { viewRegistryOfficer } from "../entities/view/viewRegistryOfficer"; import { viewRegistryEmployee } from "../entities/view/viewRegistryEmployee"; import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster"; import { sendWebSocket } from "../services/webSocket"; @Route("api/v1/org/report") @Tags("Report") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) export class ReportController extends Controller { private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private orgRootRepository = AppDataSource.getRepository(OrgRoot); private child1Repository = AppDataSource.getRepository(OrgChild1); private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); private posTypepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); private posMasterRepository = AppDataSource.getRepository(PosMaster); private profileRepository = AppDataSource.getRepository(Profile); private empPosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); private empTempPosMasterRepository = AppDataSource.getRepository(EmployeeTempPosMaster); /** * API รายงานสถิติข้อมูลข้าราชการ กทม. สามัญ * * @summary รายงานสถิติข้อมูลข้าราชการ กทม. สามัญ * */ // @Get("registry-officer") // async registryOfficer( // @Query() rootId?: string, // @Query() year?: number, // @Query() ageMin?: number, // @Query() ageMax?: number, // ) { // if (ageMin && (ageMin < 18 || ageMin > 60)) { // throw new HttpError(HttpStatus.BAD_REQUEST, "ageMin must be between 18 and 60"); // } // if (ageMax && (ageMax < 18 || ageMax > 60)) { // throw new HttpError(HttpStatus.BAD_REQUEST, "ageMax must be between 18 and 60"); // } // const minAge = ageMin ?? 18; // const maxAge = ageMax ?? 60; // if (minAge > maxAge) { // throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax"); // } // const yearInAD = year ? year : null; // const currentRevision = await this.orgRevisionRepository.findOne({ // where: { // orgRevisionIsCurrent: true, // }, // }); // const rawdataProfile = await this.posMasterRepository // .createQueryBuilder('posMaster') // .leftJoinAndSelect('posMaster.current_holder', 'current_holder') // .leftJoinAndSelect('posMaster.positions', 'positions') // .leftJoinAndSelect('posMaster.orgRoot', 'orgRoot') // .leftJoinAndSelect('positions.posExecutive', 'posExecutive') // .leftJoinAndSelect('current_holder.posType', 'posType') // .leftJoinAndSelect('current_holder.posLevel', 'posLevel') // .leftJoinAndSelect('current_holder.profileEducations', 'profileEducations') // .where('posMaster.orgRevisionId = :currentRevisionId', { currentRevisionId: currentRevision?.id }) // .andWhere(rootId?'posMaster.orgRootId = :rootId': "1=1", { rootId: rootId }) // .andWhere('posMaster.current_holderId Is Not Null') // .andWhere('positions.positionIsSelected = :positionIsSelected', { positionIsSelected: true }) // .andWhere( yearInAD && yearInAD != null? 'YEAR(current_holder.dateAppoint) = :year': "1=1", { year: yearInAD }) //ตอนนี้ where ที่วันที่บรรจุ (รอ prove) ว่าจะ where ที่ไหน // .andWhere(` // TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) >= :minAge // AND TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) <= :maxAge // `, // { minAge, maxAge }, // ) // .orderBy("posType.posTypeName", "ASC") // .getMany(); // if (!rawdataProfile) { // throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); // } // const mapData = rawdataProfile.map((x) => { // const latestEducation = x.current_holder.profileEducations.sort( // (a: any, b: any) => b.endDate - a.startDate, // )[0]; // return { // name: x.current_holder.firstName + " " + x.current_holder.lastName, // affiliation: x.orgRoot.orgRootName ?? "-", // gender: x.current_holder.gender ?? "-", // positionName: x.positions[0] ? x.positions[0].positionName : "-", // status: x.current_holder.relationship ?? "-", // posType: x.current_holder.posType.posTypeName ?? "-", // posLevel: x.current_holder.posLevel.posLevelName ?? "-", // degree: latestEducation ? latestEducation.educationLevel : "-", // posExecutive: x.positions[0].posExecutive // ? x.positions[0].posExecutive.posExecutiveName // : "-", // currentPreiodPos: "-", // levelPeriodPos: "-", // }; // }); // const groupedData = mapData.reduce((acc: any, item) => { // const key = `${item.posType} - ${item.affiliation} - ${item.gender} - ${item.degree || "ไม่พบข้อมูล"} - ${item.status || "ไม่พบข้อมูล"} - ${item.positionName} - ${item.posLevel} - ${item.posExecutive || "ไม่พบข้อมูล"} `; // if (!acc[key]) { // acc[key] = { // posType: item.posType && item.posType != "" ? item.posType : "-", // affiliation: item.affiliation && item.affiliation != "" ? item.affiliation : "-", // gender: item.gender && item.gender != "" ? item.gender : "-", // degree: item.degree && item.degree != "" ? item.degree : "-", // status: item.status && item.status != "" ? item.status : "-", // positionName: item.positionName && item.positionName != "" ? item.positionName : "-", // posLevel: item.posLevel && item.posLevel != "" ? item.posLevel : "-", // posExecutive: item.posExecutive && item.posExecutive != "" ? item.posExecutive : "-", // currentPreiodPos: "-", // levelPeriodPos: "-", // count: 0, // }; // } // acc[key].count++; // return acc; // }, {}); // const result = Object.values(groupedData).map((item: any) => ({ // ...item, // count: Extension.ToThaiNumber(item.count.toString()), // })); // return new HttpSuccess({ // template: "registry-officer", // reportName: "xlsx-report", // data: { // year: year // ? Extension.ToThaiNumber((year + 543).toString()) // : Extension.ToThaiNumber((new Date().getFullYear() + 543).toString()), // date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())), // list: result, // }, // }); // } @Get("registry-officer") async registryOfficer( @Query() node?: number, @Query() nodeId?: string, @Query() posType?: string, @Query() posLevel?: string, @Query() position?: string, @Query() positionExecutive?: string, @Query() gender?: string, @Query() status?: string, @Query() education?: string, @Query() dateStart?: Date, @Query() dateEnd?: Date, @Query() ageMin?: number, @Query() ageMax?: number, @Query() isProbation?: boolean, @Query() isRetire?: boolean, @Query() isRetireLaw?: boolean, @Query() retireType?: string, @Query() tenureType?: string, @Query() tenureMin?: number, @Query() tenureMax?: number, @Query() sortBy: string = "posMasterNo", @Query() sort: "ASC" | "DESC" = "ASC", ) { const _null: any = null; if (!dateStart) { dateStart = _null; } if (!dateEnd) { dateEnd = _null; } if (ageMin && (ageMin < 18 || ageMin > 60)) { throw new HttpError(HttpStatus.NOT_FOUND, "ageMin must be between 18 and 60"); } if (ageMax && (ageMax < 18 || ageMax > 60)) { throw new HttpError(HttpStatus.NOT_FOUND, "ageMax must be between 18 and 60"); } if (ageMin && ageMax && ageMin > ageMax) { throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax"); } ageMin = ageMin ?? 18; ageMax = ageMax ?? 60; tenureMin = tenureMin ?? 0; tenureMax = tenureMax ?? 20; let nodeCondition = "1=1"; if (node === 0 && nodeId) { nodeCondition = "registryOfficer.orgRootId = :nodeId"; } else if (node === 1 && nodeId) { nodeCondition = "registryOfficer.orgChild1Id = :nodeId"; } else if (node === 2 && nodeId) { nodeCondition = "registryOfficer.orgChild2Id = :nodeId"; } else if (node === 3 && nodeId) { nodeCondition = "registryOfficer.orgChild3Id = :nodeId"; } else if (node === 4 && nodeId) { nodeCondition = "registryOfficer.orgChild4Id = :nodeId"; } let dateAppointCondition = "1=1"; if (dateStart && dateEnd) { dateAppointCondition = "DATE(registryOfficer.dateAppoint) >= :startDateAppoint AND DATE(registryOfficer.dateAppoint) <= :endDateAppoint"; } else if (dateStart) { dateAppointCondition = "DATE(registryOfficer.dateAppoint) >= :startDateAppoint"; } else if (dateEnd) { dateAppointCondition = "DATE(registryOfficer.dateAppoint) <= :endDateAppoint"; } const IsLeavecondition = ["registryOfficer.isLeave = :isLeave"]; const parameters: any = { isLeave: isRetire }; if (retireType && retireType.trim() !== "") { IsLeavecondition.push("registryOfficer.leaveType = :retireType"); parameters.retireType = retireType; } let retireLawCondition = "1=1"; if (isRetireLaw) { retireLawCondition = "DATE(registryOfficer.dateRetireLaw) >= :startDateRetireLaw AND DATE(registryOfficer.dateRetireLaw) <= :endDateRetireLaw"; } let tenureTypeCondition = "1=1"; if (tenureType != "" && tenureType == "position") { tenureTypeCondition = "registryOfficer.Years BETWEEN :tenureMin AND :tenureMax"; } else if (tenureType != "" && tenureType == "level") { tenureTypeCondition = "registryOfficer.levelYears BETWEEN :tenureMin AND :tenureMax"; } const [lists, total] = await AppDataSource.getRepository(viewRegistryOfficer) .createQueryBuilder("registryOfficer") .where(nodeCondition, { nodeId: nodeId, }) .andWhere(tenureTypeCondition, { tenureMin: tenureMin, tenureMax: tenureMax, }) .andWhere("registryOfficer.age BETWEEN :ageMin AND :ageMax", { ageMin, ageMax, }) .andWhere(dateAppointCondition, { startDateAppoint: dateStart?.toISOString().split("T")[0], endDateAppoint: dateEnd?.toISOString().split("T")[0], }) .andWhere(retireLawCondition, { startDateRetireLaw: new Date(new Date().getFullYear() - 1, 9, 1, 0, 0, 0, 0) ?.toISOString() .split("T")[0], endDateRetireLaw: new Date(new Date().getFullYear(), 8, 30, 23, 59, 59, 999) .toISOString() .split("T")[0], }) .andWhere("registryOfficer.isProbation = :isProbation", { isProbation: isProbation, }) .andWhere(IsLeavecondition.join(" AND "), parameters) .andWhere( posType != null && posType != "" ? "registryOfficer.posTypeName = :posTypeName" : "1=1", { posTypeName: `%${posType}%`, }, ) .andWhere( posLevel != null && posLevel != "" ? "registryOfficer.posLevelName = :posLevelName" : "1=1", { posLevelName: `%${posLevel}%`, }, ) .andWhere( position != null && position != "" ? "registryOfficer.position = :position" : "1=1", { position: `%${position}%`, }, ) .andWhere( positionExecutive != null && positionExecutive != "" ? "registryOfficer.posExecutiveName = :posExecutiveName" : "1=1", { posExecutiveName: `%${positionExecutive}%`, }, ) .andWhere(gender != null && gender != "" ? "registryOfficer.gender = :gender" : "1=1", { gender: `%${gender}%`, }) .andWhere( status != null && status != "" ? "registryOfficer.relationship = :relationship" : "1=1", { relationship: `%${status}%`, }, ) .andWhere( education != null && education != "" ? "registryOfficer.degree LIKE :degree" : "1=1", { degree: `%${education}%`, }, ) .orderBy(`registryOfficer.${sortBy}`, sort) .getManyAndCount(); const mapData = await Promise.all( lists.map(async (x) => { return { profileId: x.profileId, citizenId: x.citizenId, prefix: x.prefix, firstName: x.firstName, lastName: x.lastName, isProbation: x.isProbation, isLeave: x.isLeave, isRetirement: x.isRetirement, leaveType: x.leaveType, posMasterNo: x.posMasterNo, orgRootId: x.orgRootId, orgChild1Id: x.orgChild1Id, orgChild2Id: x.orgChild2Id, orgChild3Id: x.orgChild3Id, orgChild4Id: x.orgChild4Id, orgRootName: x.orgRootName, orgChild1Name: x.orgChild1Name, orgChild2Name: x.orgChild2Name, orgChild3Name: x.orgChild3Name, orgChild4Name: x.orgChild4Name, org: x.org, searchShortName: x.searchShortName, posExecutiveName: x.posExecutiveName, position: x.position, posTypeName: x.posTypeName, posLevelName: x.posLevelName, gender: x.gender, relationship: x.relationship, dateAppoint: x.dateAppoint, dateRetire: x.dateRetire, dateRetireLaw: x.dateRetireLaw, birthdate: x.birthdate, degree: x.degree, age: x.age, currentPosition: null, lengthPosition: null, positionDate: { Years: x.Years ? x.Years : 0, Months: x.Months ? x.Months : 0, Days: x.Days ? x.Days : 0, }, levelDate: { posExecutiveYears: x.levelYears, posExecutiveMonths: x.levelMonths, posExecutiveDays: x.levelDays, }, }; }), ); return new HttpSuccess({ data: mapData, total: total, }); } /** * API รายงานสถิติข้อมูลลูกจ้างประจำ กทม. * * @summary รายงานสถิติข้อมูลลูกจ้างประจำ กทม. * */ // @Get("registry-emp") // async registryEmp( // @Query() rootId?: string, // @Query() year?: number, // @Query() ageMin?: number, // @Query() ageMax?: number, // ) { // if (ageMin && (ageMin < 18 || ageMin > 60)) { // throw new HttpError(HttpStatus.BAD_REQUEST, "ageMin must be between 18 and 60"); // } // if (ageMax && (ageMax < 18 || ageMax > 60)) { // throw new HttpError(HttpStatus.BAD_REQUEST, "ageMax must be between 18 and 60"); // } // const minAge = ageMin ?? 18; // const maxAge = ageMax ?? 60; // if (minAge > maxAge) { // throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax"); // } // const yearInAD = year ? year : null; // const currentRevision = await this.orgRevisionRepository.findOne({ // where: { // orgRevisionIsCurrent: true, // }, // }); // const rawdataProfile = await this.empPosMasterRepository // .createQueryBuilder("posMaster") // .leftJoinAndSelect("posMaster.current_holder", "current_holder") // .leftJoinAndSelect("posMaster.positions", "positions") // .leftJoinAndSelect("posMaster.orgRoot", "orgRoot") // .leftJoinAndSelect("current_holder.posType", "posType") // .leftJoinAndSelect("current_holder.posLevel", "posLevel") // .leftJoinAndSelect("current_holder.profileEducations", "profileEducations") // .where("posMaster.orgRevisionId = :currentRevisionId", { // currentRevisionId: currentRevision?.id, // }) // .andWhere(rootId ? "posMaster.orgRootId = :rootId" : "1=1", { rootId: rootId }) // .andWhere("posMaster.current_holderId Is Not Null") // .andWhere("positions.positionIsSelected = :positionIsSelected", { positionIsSelected: true }) // .andWhere(yearInAD && yearInAD != null ? "YEAR(current_holder.dateAppoint) = :year" : "1=1", { // year: yearInAD, // }) // .andWhere( // ` // TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) >= :minAge // AND TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) <= :maxAge // `, // { minAge, maxAge }, // ) // .orderBy("posType.posTypeName", "ASC") // .getMany(); // if (!rawdataProfile) { // throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); // } // const mapData = rawdataProfile.map((x) => { // const latestEducation = x.current_holder.profileEducations.sort( // (a: any, b: any) => b.endDate - a.startDate, // )[0]; // return { // name: x.current_holder.firstName + " " + x.current_holder.lastName, // affiliation: x.orgRoot.orgRootName ?? "-", // gender: x.current_holder.gender ?? "-", // positionName: x.positions[0] ? x.positions[0].positionName : "-", // status: x.current_holder.relationship ?? "-", // posType: x.current_holder.posType.posTypeName ?? "-", // posLevel: x.current_holder.posLevel.posLevelName ?? "-", // degree: latestEducation ? latestEducation.educationLevel : "-", // period: "-", // }; // }); // const groupedData = mapData.reduce((acc: any, item) => { // const key = `${item.affiliation} - ${item.gender} - ${item.degree || "ไม่พบข้อมูล"} - ${item.status || "ไม่พบข้อมูล"} - ${item.posType} - ${item.positionName} - ${item.posLevel} // `; // if (!acc[key]) { // acc[key] = { // affiliation: item.affiliation && item.affiliation != "" ? item.affiliation : "-", // gender: item.gender && item.gender != "" ? item.gender : "-", // degree: item.degree && item.degree != "" ? item.degree : "-", // status: item.status && item.status != "" ? item.status : "-", // posType: item.posType && item.posType != "" ? item.posType : "-", // positionName: item.positionName && item.positionName != "" ? item.positionName : "-", // posLevel: Extension.ToThaiNumber(item.posLevel.toString()), // period: "-", // count: 0, // }; // } // acc[key].count++; // return acc; // }, {}); // const result = Object.values(groupedData).map((item: any) => ({ // ...item, // count: Extension.ToThaiNumber(item.count.toString()), // })); // return new HttpSuccess({ // template: "registry-emp", // reportName: "xlsx-report", // data: { // year: year // ? Extension.ToThaiNumber((year + 543).toString()) // : Extension.ToThaiNumber((new Date().getFullYear() + 543).toString()), // date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())), // list: result, // }, // }); // } @Get("registry-emp") async registryEmployee( @Query() node?: number, @Query() nodeId?: string, @Query() posType?: string, @Query() posLevel?: string, @Query() position?: string, @Query() gender?: string, @Query() status?: string, @Query() education?: string, @Query() dateStart?: Date, @Query() dateEnd?: Date, @Query() isProbation?: boolean, @Query() isRetire?: boolean, @Query() isRetireLaw?: boolean, @Query() retireType?: string, @Query() ageMin?: number, @Query() ageMax?: number, @Query() tenureType?: string, @Query() tenureMin?: number, @Query() tenureMax?: number, @Query() sortBy: string = "posMasterNo", @Query() sort: "ASC" | "DESC" = "ASC", ) { const _null: any = null; if (!dateStart) { dateStart = _null; } if (!dateEnd) { dateEnd = _null; } if (ageMin && (ageMin < 18 || ageMin > 60)) { throw new HttpError(HttpStatus.NOT_FOUND, "ageMin must be between 18 and 60"); } if (ageMax && (ageMax < 18 || ageMax > 60)) { throw new HttpError(HttpStatus.NOT_FOUND, "ageMax must be between 18 and 60"); } if (ageMin && ageMax && ageMin > ageMax) { throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax"); } ageMin = ageMin ?? 18; ageMax = ageMax ?? 60; tenureMin = tenureMin ?? 0; tenureMax = tenureMax ?? 20; let nodeCondition = "1=1"; if (node === 0 && nodeId) { nodeCondition = "registryEmployee.orgRootId = :nodeId"; } else if (node === 1 && nodeId) { nodeCondition = "registryEmployee.orgChild1Id = :nodeId"; } else if (node === 2 && nodeId) { nodeCondition = "registryEmployee.orgChild2Id = :nodeId"; } else if (node === 3 && nodeId) { nodeCondition = "registryEmployee.orgChild3Id = :nodeId"; } else if (node === 4 && nodeId) { nodeCondition = "registryEmployee.orgChild4Id = :nodeId"; } let dateAppointCondition = "1=1"; if (dateStart && dateEnd) { dateAppointCondition = "DATE(registryEmployee.dateAppoint) >= :startDateAppoint AND DATE(registryEmployee.dateAppoint) <= :endDateAppoint"; } else if (dateStart) { dateAppointCondition = "DATE(registryEmployee.dateAppoint) >= :startDateAppoint"; } else if (dateEnd) { dateAppointCondition = "DATE(registryEmployee.dateAppoint) <= :endDateAppoint"; } const IsLeavecondition = ["registryEmployee.isLeave = :isLeave"]; const parameters: any = { isLeave: isRetire }; if (retireType && retireType.trim() !== "") { IsLeavecondition.push("registryEmployee.leaveType = :retireType"); parameters.retireType = retireType; } let tenureTypeCondition = "1=1"; if (tenureType != "" && tenureType == "position") { tenureTypeCondition = "registryEmployee.Years BETWEEN :tenureMin AND :tenureMax"; } else if (tenureType != "" && tenureType == "level") { tenureTypeCondition = "registryEmployee.levelYears BETWEEN :tenureMin AND :tenureMax"; } let retireLawCondition = "1=1"; if (isRetireLaw) { retireLawCondition = "DATE(registryEmployee.dateRetireLaw) >= :startDateRetireLaw AND DATE(registryEmployee.dateRetireLaw) <= :endDateRetireLaw"; } const [lists, total] = await AppDataSource.getRepository(viewRegistryEmployee) .createQueryBuilder("registryEmployee") .where(nodeCondition, { nodeId: nodeId, }) .andWhere(tenureTypeCondition, { tenureMin: tenureMin, tenureMax: tenureMax, }) .andWhere("registryEmployee.age BETWEEN :ageMin AND :ageMax", { ageMin, ageMax, }) .andWhere(dateAppointCondition, { startDateAppoint: dateStart?.toISOString().split("T")[0], endDateAppoint: dateEnd?.toISOString().split("T")[0], }) .andWhere(retireLawCondition, { startDateRetireLaw: new Date(new Date().getFullYear() - 1, 9, 1, 0, 0, 0, 0) ?.toISOString() .split("T")[0], endDateRetireLaw: new Date(new Date().getFullYear(), 8, 30, 23, 59, 59, 999) .toISOString() .split("T")[0], }) .andWhere("registryEmployee.isProbation = :isProbation", { isProbation: isProbation, }) .andWhere(IsLeavecondition.join(" AND "), parameters) .andWhere("registryEmployee.employeeClass = 'PERM'") .andWhere( posType != null && posType != "" ? "registryEmployee.posTypeName LIKE :posTypeName" : "1=1", { posTypeName: `%${posType}%`, }, ) .andWhere( posLevel != null && posLevel != "" ? "registryEmployee.posLevelName LIKE :posLevelName" : "1=1", { posLevelName: `%${posLevel}%`, }, ) .andWhere( position != null && position != "" ? "registryEmployee.position LIKE :position" : "1=1", { position: `%${position}%`, }, ) .andWhere(gender != null && gender != "" ? "registryEmployee.gender LIKE :gender" : "1=1", { gender: `%${gender}%`, }) .andWhere( status != null && status != "" ? "registryEmployee.relationship LIKE :relationship" : "1=1", { relationship: `%${status}%`, }, ) .andWhere( education != null && education != "" ? "registryEmployee.degree LIKE :degree" : "1=1", { degree: `%${education}%`, }, ) .orderBy(`registryEmployee.${sortBy}`, sort) .getManyAndCount(); const mapData = await Promise.all( lists.map(async (x) => { return { profileId: x.profileEmployeeId, citizenId: x.citizenId, prefix: x.prefix, firstName: x.firstName, lastName: x.lastName, isProbation: x.isProbation, isLeave: x.isLeave, isRetirement: x.isRetirement, leaveType: x.leaveType, posMasterNo: x.posMasterNo, orgRootId: x.orgRootId, orgChild1Id: x.orgChild1Id, orgChild2Id: x.orgChild2Id, orgChild3Id: x.orgChild3Id, orgChild4Id: x.orgChild4Id, orgRootName: x.orgRootName, orgChild1Name: x.orgChild1Name, orgChild2Name: x.orgChild2Name, orgChild3Name: x.orgChild3Name, orgChild4Name: x.orgChild4Name, org: x.org, searchShortName: x.searchShortName, position: x.position, posTypeName: x.posTypeName, posLevelName: x.posLevelName, gender: x.gender, relationship: x.relationship, dateAppoint: x.dateAppoint, dateRetire: x.dateRetire, dateRetireLaw: x.dateRetireLaw, birthdate: x.birthdate, degree: x.degree, age: x.age, currentPosition: null, lengthPosition: null, positionDate: { Years: x.Years, Months: x.Months, Days: x.Days, }, levelDate: { posExecutiveYears: x.levelYears, posExecutiveMonths: x.levelMonths, posExecutiveDays: x.levelDays, }, }; }), ); return new HttpSuccess({ data: mapData, total: total, }); } /** * API Report1 * * @summary Report1 * */ @Get("report1/{rootId}") async findReport1(@Path() rootId: string) { // const orgRevision = await this.orgRevisionRepository.findOne({ // where: { orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, // relations: ["orgRoots"], // }); // if (!orgRevision) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); // } const orgRootData = await this.orgRootRepository.find({ where: { id: rootId, // orgRevisionId: orgRevision.id, }, order: { orgRootOrder: "ASC" }, relations: [ "posMasters", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; const orgChild1Data = await this.child1Repository.find({ where: { orgRootId: In(orgRootIds), }, order: { orgChild1Order: "ASC" }, relations: [ "posMasters", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id) || null; const orgChild2Data = await this.child2Repository.find({ where: { orgChild1: In(orgChild1Ids), }, order: { orgChild2Order: "ASC" }, relations: [ "posMasters", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids), }, order: { orgChild3Order: "ASC" }, relations: [ "posMasters", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids), }, order: { orgChild4Order: "ASC" }, relations: [ "posMasters", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); let data = new Array(); let _node: any; let no = 1; for (let orgRoot of orgRootData) { await Promise.all( orgRoot.posMasters .filter((x) => x.orgChild1Id == null) .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild1Id == null) { const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let node = { orgTreeName: orgRoot.orgRootName, orgTreeShortName: orgRoot.orgRootShortName, posMasterNo: posMaster.posMasterNo, positionName: positionName.join(" หรือ "), posType: posType.join(" หรือ "), posLevel: posLevel.join(" หรือ "), posExecutive: posExecutive.join(" หรือ "), reason: posMaster.reason == null ? "" : posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), positionName: Extension.ToThaiNumber(node.positionName.toString()), posType: Extension.ToThaiNumber(node.posType.toString()), posLevel: Extension.ToThaiNumber(node.posLevel.toString()), reason: Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), posExecutive: node.posExecutive == _node.posExecutive ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: Extension.ToThaiNumber(node.positionName.toString()), posType: Extension.ToThaiNumber(node.posType.toString()), posLevel: Extension.ToThaiNumber(node.posLevel.toString()), reason: Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild1 of orgChild1Data.filter( (orgChild1) => orgChild1.orgRootId === orgRoot.id, )) { await Promise.all( orgChild1.posMasters .filter((x) => x.orgChild2Id == null) .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild2Id == null) { const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let node = { orgTreeName: orgChild1.orgChild1Name, orgTreeShortName: orgChild1.orgChild1ShortName, posMasterNo: posMaster.posMasterNo, positionName: positionName.join(" หรือ "), posType: posType.join(" หรือ "), posLevel: posLevel.join(" หรือ "), posExecutive: posExecutive.join(" หรือ "), reason: posMaster.reason == null ? "" : posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), positionName: Extension.ToThaiNumber(node.positionName.toString()), posType: Extension.ToThaiNumber(node.posType.toString()), posLevel: Extension.ToThaiNumber(node.posLevel.toString()), reason: Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), posExecutive: node.posExecutive == _node.posExecutive ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: Extension.ToThaiNumber(node.positionName.toString()), posType: Extension.ToThaiNumber(node.posType.toString()), posLevel: Extension.ToThaiNumber(node.posLevel.toString()), reason: Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild2 of orgChild2Data.filter( (orgChild2) => orgChild2.orgChild1Id === orgChild1.id, )) { await Promise.all( orgChild2.posMasters .filter((x) => x.orgChild3Id == null) .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild3Id == null) { const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let node = { orgTreeName: orgChild2.orgChild2Name, orgTreeShortName: orgChild2.orgChild2ShortName, posMasterNo: posMaster.posMasterNo, posExecutive: posExecutive.join(" หรือ "), positionName: positionName.join(" หรือ "), posType: posType.join(" หรือ "), posLevel: posLevel.join(" หรือ "), reason: posMaster.reason == null ? "" : posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), positionName: Extension.ToThaiNumber(node.positionName.toString()), posType: Extension.ToThaiNumber(node.posType.toString()), posLevel: Extension.ToThaiNumber(node.posLevel.toString()), reason: Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), posExecutive: node.posExecutive == _node.posExecutive ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: Extension.ToThaiNumber(node.positionName.toString()), posType: Extension.ToThaiNumber(node.posType.toString()), posLevel: Extension.ToThaiNumber(node.posLevel.toString()), reason: Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild3 of orgChild3Data.filter( (orgChild3) => orgChild3.orgChild2Id === orgChild2.id, )) { await Promise.all( orgChild3.posMasters .filter((x) => x.orgChild4Id == null) .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild4Id == null) { const positionName = [ ...new Set(posMaster.positions.map((x) => x.positionName)), ]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let node = { orgTreeName: orgChild3.orgChild3Name, orgTreeShortName: orgChild3.orgChild3ShortName, posMasterNo: posMaster.posMasterNo, positionName: positionName.join(" หรือ "), posType: posType.join(" หรือ "), posLevel: posLevel.join(" หรือ "), posExecutive: posExecutive.join(" หรือ "), reason: posMaster.reason == null ? "" : posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), positionName: Extension.ToThaiNumber(node.positionName.toString()), posType: Extension.ToThaiNumber(node.posType.toString()), posLevel: Extension.ToThaiNumber(node.posLevel.toString()), reason: Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), posExecutive: node.posExecutive == _node.posExecutive ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: Extension.ToThaiNumber(node.positionName.toString()), posType: Extension.ToThaiNumber(node.posType.toString()), posLevel: Extension.ToThaiNumber(node.posLevel.toString()), reason: Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild4 of orgChild4Data.filter( (orgChild4) => orgChild4.orgChild3Id === orgChild3.id, )) { await Promise.all( orgChild4.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { const positionName = [ ...new Set(posMaster.positions.map((x) => x.positionName)), ]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let node = { orgTreeName: orgChild4.orgChild4Name, orgTreeShortName: orgChild4.orgChild4ShortName, posMasterNo: posMaster.posMasterNo, positionName: positionName.join(" หรือ "), posType: posType.join(" หรือ "), posLevel: posLevel.join(" หรือ "), posExecutive: posExecutive.join(" หรือ "), reason: posMaster.reason == null ? "" : posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), positionName: Extension.ToThaiNumber(node.positionName.toString()), posType: Extension.ToThaiNumber(node.posType.toString()), posLevel: Extension.ToThaiNumber(node.posLevel.toString()), reason: Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), posExecutive: node.posExecutive == _node.posExecutive ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: Extension.ToThaiNumber(node.posType.toString()), posLevel: Extension.ToThaiNumber(node.posLevel.toString()), reason: Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; }), ); _node = null; } } } } } return new HttpSuccess({ template: "report1", reportName: "report1", data: { data } }); } /** * API Report2 * * @summary Report2 * */ @Get("report2/{rootId}") async findReport2(@Path() rootId: string) { // const orgRevision = await this.orgRevisionRepository.findOne({ // where: { orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, // relations: ["orgRoots"], // }); // if (!orgRevision) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); // } const orgRootData = await this.orgRootRepository.find({ where: { id: rootId, // orgRevisionId: orgRevision.id, }, order: { orgRootOrder: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.next_holder", "posMasters.next_holder", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", "posMasters.next_holder.profileSalary", "posMasters.next_holder.profileEducations", "posMasters.next_holder.current_holders", "posMasters.next_holder.current_holders.positions", "posMasters.next_holder.current_holders.orgRoot", "posMasters.next_holder.current_holders.orgChild1", "posMasters.next_holder.current_holders.orgChild2", "posMasters.next_holder.current_holders.orgChild3", "posMasters.next_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; const orgChild1Data = await this.child1Repository.find({ where: { orgRootId: In(orgRootIds), }, order: { orgChild1Order: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", "posMasters.next_holder.profileSalary", "posMasters.next_holder.profileEducations", "posMasters.next_holder.current_holders", "posMasters.next_holder.current_holders.positions", "posMasters.next_holder.current_holders.orgRoot", "posMasters.next_holder.current_holders.orgChild1", "posMasters.next_holder.current_holders.orgChild2", "posMasters.next_holder.current_holders.orgChild3", "posMasters.next_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id) || null; const orgChild2Data = await this.child2Repository.find({ where: { orgChild1: In(orgChild1Ids), }, order: { orgChild2Order: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", "posMasters.next_holder.profileSalary", "posMasters.next_holder.profileEducations", "posMasters.next_holder.current_holders", "posMasters.next_holder.current_holders.positions", "posMasters.next_holder.current_holders.orgRoot", "posMasters.next_holder.current_holders.orgChild1", "posMasters.next_holder.current_holders.orgChild2", "posMasters.next_holder.current_holders.orgChild3", "posMasters.next_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids), }, order: { orgChild3Order: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", "posMasters.next_holder.profileSalary", "posMasters.next_holder.profileEducations", "posMasters.next_holder.current_holders", "posMasters.next_holder.current_holders.positions", "posMasters.next_holder.current_holders.orgRoot", "posMasters.next_holder.current_holders.orgChild1", "posMasters.next_holder.current_holders.orgChild2", "posMasters.next_holder.current_holders.orgChild3", "posMasters.next_holder.current_holders.orgChild4", "posMasters.next_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids), }, order: { orgChild4Order: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", "posMasters.next_holder.profileSalary", "posMasters.next_holder.profileEducations", "posMasters.next_holder.current_holders", "posMasters.next_holder.current_holders.positions", "posMasters.next_holder.current_holders.orgRoot", "posMasters.next_holder.current_holders.orgChild1", "posMasters.next_holder.current_holders.orgChild2", "posMasters.next_holder.current_holders.orgChild3", "posMasters.next_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); let orgRevisionActive: any = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, relations: [ "posMasters", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); if (orgRevisionActive == null) { const _orgRevisionActive = await this.orgRevisionRepository.find({ order: { createdAt: "DESC" }, skip: 1, relations: [ "posMasters", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); if (_orgRevisionActive.length > 0) orgRevisionActive = _orgRevisionActive[0]; } let data = new Array(); let _node: any; let no = 1; for (let orgRoot of orgRootData) { await Promise.all( orgRoot.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild1Id == null) { const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let positionMasterOld: any = null; let profilePositionName: any = []; let profilePosType: any = []; let profilePosLevel: any = []; let profilePosExecutive: any = []; if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { positionMasterOld = orgRevisionActive.posMasters.find( (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), ]; profilePosType = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posType != null) .map((x: any) => x.posType.posTypeName), ), ]; profilePosLevel = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posLevel != null) .map((x: any) => x.posLevel.posLevelName), ), ]; profilePosExecutive = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => x.posExecutive.posExecutiveName), ), ]; } let education: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileEducations != null && posMaster.next_holder.profileEducations.length > 0 ) { let _education: any = posMaster.next_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileSalary != null && posMaster.next_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.next_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // orgTreeName: orgRoot.orgRootName, orgTreeShortName: orgRoot.orgRootShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.next_holder == null ? positionName.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.next_holder.position, posType: posMaster.next_holder == null ? posType.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posType ?.posTypeName : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, posLevel: posMaster.next_holder == null ? posLevel.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posLevel ?.posLevelName : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, posExecutive: posMaster.next_holder == null ? posExecutive.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileOrgName: posMaster.next_holder == null ? orgRoot.orgRootName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4Name : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3Name : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2Name : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1Name : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootName : "-", profileOrgShortName: posMaster.next_holder == null ? orgRoot.orgRootShortName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4ShortName : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3ShortName : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2ShortName : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1ShortName : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootShortName : "-", profileFullname: posMaster.next_holder == null ? "- ว่าง -" : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null ? positionMasterOld == null ? "-" : positionMasterOld.posMasterNo : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.posMasterNo, profilePositionName: posMaster.next_holder == null ? positionMasterOld == null ? positionName.join(" หรือ ") : profilePositionName.join(" หรือ ") : posMaster.next_holder.position, profilePosType: posMaster.next_holder == null ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null ? posExecutive.join(" หรือ ") : profilePosExecutive.join(" หรือ ") : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, reason: posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName || node.profileOrgShortName != _node.profileOrgShortName || node.profileOrgName != _node.profileOrgName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", profilePosMasterNo: node.profileOrgShortName == _node.profileOrgShortName ? "" : node.profileOrgShortName, profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild1 of orgChild1Data.filter( (orgChild1) => orgChild1.orgRootId === orgRoot.id, )) { await Promise.all( orgChild1.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild2Id == null) { const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let positionMasterOld: any = null; let profilePositionName: any = []; let profilePosType: any = []; let profilePosLevel: any = []; let profilePosExecutive: any = []; if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { positionMasterOld = orgRevisionActive.posMasters.find( (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), ]; profilePosType = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posType != null) .map((x: any) => x.posType.posTypeName), ), ]; profilePosLevel = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posLevel != null) .map((x: any) => x.posLevel.posLevelName), ), ]; profilePosExecutive = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => x.posExecutive.posExecutiveName), ), ]; } let education: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileEducations != null && posMaster.next_holder.profileEducations.length > 0 ) { let _education: any = posMaster.next_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileSalary != null && posMaster.next_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.next_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // orgTreeName: orgChild1.orgChild1Name, orgTreeShortName: orgChild1.orgChild1ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.next_holder == null ? positionName.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.next_holder.position, posType: posMaster.next_holder == null ? posType.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, posLevel: posMaster.next_holder == null ? posLevel.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, posExecutive: posMaster.next_holder == null ? posExecutive.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileOrgName: posMaster.next_holder == null ? orgChild1.orgChild1Name : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4Name : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3Name : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2Name : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1Name : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootName : "-", profileOrgShortName: posMaster.next_holder == null ? orgChild1.orgChild1ShortName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4ShortName : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3ShortName : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2ShortName : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1ShortName : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootShortName : "-", profileFullname: posMaster.next_holder == null ? "- ว่าง -" : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null ? positionMasterOld == null ? "-" : positionMasterOld.posMasterNo : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.posMasterNo, profilePositionName: posMaster.next_holder == null ? positionMasterOld == null ? positionName.join(" หรือ ") : profilePositionName.join(" หรือ ") : posMaster.next_holder.position, profilePosType: posMaster.next_holder == null ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null ? posExecutive.join(" หรือ ") : profilePosExecutive.join(" หรือ ") : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, reason: posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName || node.profileOrgShortName != _node.profileOrgShortName || node.profileOrgName != _node.profileOrgName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", profilePosMasterNo: node.profileOrgShortName == _node.profileOrgShortName ? "" : node.profileOrgShortName, profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild2 of orgChild2Data.filter( (orgChild2) => orgChild2.orgChild1Id === orgChild1.id, )) { await Promise.all( orgChild2.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild3Id == null) { const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let positionMasterOld: any = null; let profilePositionName: any = []; let profilePosType: any = []; let profilePosLevel: any = []; let profilePosExecutive: any = []; if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { positionMasterOld = orgRevisionActive.posMasters.find( (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), ]; profilePosType = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posType != null) .map((x: any) => x.posType.posTypeName), ), ]; profilePosLevel = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posLevel != null) .map((x: any) => x.posLevel.posLevelName), ), ]; profilePosExecutive = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => x.posExecutive.posExecutiveName), ), ]; } let education: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileEducations != null && posMaster.next_holder.profileEducations.length > 0 ) { let _education: any = posMaster.next_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileSalary != null && posMaster.next_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.next_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // orgTreeName: orgChild2.orgChild2Name, orgTreeShortName: orgChild2.orgChild2ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.next_holder == null ? positionName.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.next_holder.position, posType: posMaster.next_holder == null ? posType.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, posLevel: posMaster.next_holder == null ? posLevel.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, posExecutive: posMaster.next_holder == null ? posExecutive.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileOrgName: posMaster.next_holder == null ? orgChild2.orgChild2Name : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4Name : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3Name : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2Name : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1Name : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootName : "-", profileOrgShortName: posMaster.next_holder == null ? orgChild2.orgChild2ShortName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4ShortName : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3ShortName : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2ShortName : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1ShortName : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootShortName : "-", profileFullname: posMaster.next_holder == null ? "- ว่าง -" : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null ? positionMasterOld == null ? "-" : positionMasterOld.posMasterNo : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.posMasterNo, profilePositionName: posMaster.next_holder == null ? positionMasterOld == null ? positionName.join(" หรือ ") : profilePositionName.join(" หรือ ") : posMaster.next_holder.position, profilePosType: posMaster.next_holder == null ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null ? posExecutive.join(" หรือ ") : profilePosExecutive.join(" หรือ ") : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, reason: posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", profilePosMasterNo: Extension.ToThaiNumber( node.profileOrgShortName.toString(), ), profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber( node.profilePosMasterNo.toString(), ), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName || node.profileOrgShortName != _node.profileOrgShortName || node.profileOrgName != _node.profileOrgName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", profilePosMasterNo: node.profileOrgShortName == _node.profileOrgShortName ? "" : node.profileOrgShortName, profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber( node.profilePosMasterNo.toString(), ), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild3 of orgChild3Data.filter( (orgChild3) => orgChild3.orgChild2Id === orgChild2.id, )) { await Promise.all( orgChild3.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild4Id == null) { const positionName = [ ...new Set(posMaster.positions.map((x) => x.positionName)), ]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let positionMasterOld: any = null; let profilePositionName: any = []; let profilePosType: any = []; let profilePosLevel: any = []; let profilePosExecutive: any = []; if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { positionMasterOld = orgRevisionActive.posMasters.find( (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), ]; profilePosType = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posType != null) .map((x: any) => x.posType.posTypeName), ), ]; profilePosLevel = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posLevel != null) .map((x: any) => x.posLevel.posLevelName), ), ]; profilePosExecutive = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => x.posExecutive.posExecutiveName), ), ]; } let education: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileEducations != null && posMaster.next_holder.profileEducations.length > 0 ) { let _education: any = posMaster.next_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileSalary != null && posMaster.next_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.next_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // orgTreeName: orgChild3.orgChild3Name, orgTreeShortName: orgChild3.orgChild3ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.next_holder == null ? positionName.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.next_holder.position, posType: posMaster.next_holder == null ? posType.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, posLevel: posMaster.next_holder == null ? posLevel.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, posExecutive: posMaster.next_holder == null ? posExecutive.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileOrgName: posMaster.next_holder == null ? orgChild3.orgChild3Name : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4Name : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3Name : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2Name : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1Name : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootName : "-", profileOrgShortName: posMaster.next_holder == null ? orgChild3.orgChild3ShortName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4ShortName : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3ShortName : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2ShortName : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1ShortName : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootShortName : "-", profileFullname: posMaster.next_holder == null ? "- ว่าง -" : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null ? positionMasterOld == null ? "-" : positionMasterOld.posMasterNo : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.posMasterNo, profilePositionName: posMaster.next_holder == null ? positionMasterOld == null ? positionName.join(" หรือ ") : profilePositionName.join(" หรือ ") : posMaster.next_holder.position, profilePosType: posMaster.next_holder == null ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null ? posExecutive.join(" หรือ ") : profilePosExecutive.join(" หรือ ") : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, reason: posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", profilePosMasterNo: Extension.ToThaiNumber( node.profileOrgShortName.toString(), ), profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber( node.profilePosMasterNo.toString(), ), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName || node.profileOrgShortName != _node.profileOrgShortName || node.profileOrgName != _node.profileOrgName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", profilePosMasterNo: node.profileOrgShortName == _node.profileOrgShortName ? "" : node.profileOrgShortName, profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber( node.profilePosMasterNo.toString(), ), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild4 of orgChild4Data.filter( (orgChild4) => orgChild4.orgChild3Id === orgChild3.id, )) { await Promise.all( orgChild4.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { const positionName = [ ...new Set(posMaster.positions.map((x) => x.positionName)), ]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let positionMasterOld: any = null; let profilePositionName: any = []; let profilePosType: any = []; let profilePosLevel: any = []; let profilePosExecutive: any = []; if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { positionMasterOld = orgRevisionActive.posMasters.find( (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), ]; profilePosType = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posType != null) .map((x: any) => x.posType.posTypeName), ), ]; profilePosLevel = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posLevel != null) .map((x: any) => x.posLevel.posLevelName), ), ]; profilePosExecutive = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => x.posExecutive.posExecutiveName), ), ]; } let education: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileEducations != null && posMaster.next_holder.profileEducations.length > 0 ) { let _education: any = posMaster.next_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileSalary != null && posMaster.next_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.next_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // orgTreeName: orgChild4.orgChild4Name, orgTreeShortName: orgChild4.orgChild4ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.next_holder == null ? positionName.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.next_holder.position, posType: posMaster.next_holder == null ? posType.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, posLevel: posMaster.next_holder == null ? posLevel.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, posExecutive: posMaster.next_holder == null ? posExecutive.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileOrgName: posMaster.next_holder == null ? orgChild4.orgChild4Name : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4Name : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3Name : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2Name : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1Name : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootName : "-", profileOrgShortName: posMaster.next_holder == null ? orgChild4.orgChild4ShortName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4ShortName : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3ShortName : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2ShortName : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1ShortName : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootShortName : "-", profileFullname: posMaster.next_holder == null ? "- ว่าง -" : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null ? positionMasterOld == null ? "-" : positionMasterOld.posMasterNo : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.posMasterNo, profilePositionName: posMaster.next_holder == null ? positionMasterOld == null ? positionName.join(" หรือ ") : profilePositionName.join(" หรือ ") : posMaster.next_holder.position, profilePosType: posMaster.next_holder == null ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null ? posExecutive.join(" หรือ ") : profilePosExecutive.join(" หรือ ") : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, reason: posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", profilePosMasterNo: Extension.ToThaiNumber( node.profileOrgShortName.toString(), ), profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber( node.profilePosMasterNo.toString(), ), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName || node.profileOrgShortName != _node.profileOrgShortName || node.profileOrgName != _node.profileOrgName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", profilePosMasterNo: node.profileOrgShortName == _node.profileOrgShortName ? "" : node.profileOrgShortName, profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber( node.profilePosMasterNo.toString(), ), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; }), ); _node = null; } } } } } const metaData = { template: "report2", reportName: "report2", data: { data } }; sendWebSocket(metaData) // return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); } /** * API Report2 * * @summary Report2 * */ @Get("report2-history/{rootId}") async findReport2History(@Path() rootId: string) { const orgRootData = await this.orgRootRepository.find({ where: { id: rootId, }, order: { orgRootOrder: "ASC" }, relations: [ "orgRevision", "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.current_holder", "posMasters.current_holder.posLevel", "posMasters.current_holder.posType", "posMasters.current_holder.profileSalary", "posMasters.current_holder.profileEducations", "posMasters.current_holder.current_holders", "posMasters.current_holder.current_holders.positions", "posMasters.current_holder.current_holders.orgRoot", "posMasters.current_holder.current_holders.orgChild1", "posMasters.current_holder.current_holders.orgChild2", "posMasters.current_holder.current_holders.orgChild3", "posMasters.current_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; const orgChild1Data = await this.child1Repository.find({ where: { orgRootId: In(orgRootIds), }, order: { orgChild1Order: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.current_holder", "posMasters.current_holder.posLevel", "posMasters.current_holder.posType", "posMasters.current_holder.profileSalary", "posMasters.current_holder.profileEducations", "posMasters.current_holder.current_holders", "posMasters.current_holder.current_holders.positions", "posMasters.current_holder.current_holders.orgRoot", "posMasters.current_holder.current_holders.orgChild1", "posMasters.current_holder.current_holders.orgChild2", "posMasters.current_holder.current_holders.orgChild3", "posMasters.current_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id) || null; const orgChild2Data = await this.child2Repository.find({ where: { orgChild1: In(orgChild1Ids), }, order: { orgChild2Order: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.current_holder", "posMasters.current_holder.posLevel", "posMasters.current_holder.posType", "posMasters.current_holder.profileSalary", "posMasters.current_holder.profileEducations", "posMasters.current_holder.current_holders", "posMasters.current_holder.current_holders.positions", "posMasters.current_holder.current_holders.orgRoot", "posMasters.current_holder.current_holders.orgChild1", "posMasters.current_holder.current_holders.orgChild2", "posMasters.current_holder.current_holders.orgChild3", "posMasters.current_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids), }, order: { orgChild3Order: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.current_holder", "posMasters.current_holder.posLevel", "posMasters.current_holder.posType", "posMasters.current_holder.profileSalary", "posMasters.current_holder.profileEducations", "posMasters.current_holder.current_holders", "posMasters.current_holder.current_holders.positions", "posMasters.current_holder.current_holders.orgRoot", "posMasters.current_holder.current_holders.orgChild1", "posMasters.current_holder.current_holders.orgChild2", "posMasters.current_holder.current_holders.orgChild3", "posMasters.current_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids), }, order: { orgChild4Order: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.current_holder", "posMasters.current_holder.posLevel", "posMasters.current_holder.posType", "posMasters.current_holder.profileSalary", "posMasters.current_holder.profileEducations", "posMasters.current_holder.current_holders", "posMasters.current_holder.current_holders.positions", "posMasters.current_holder.current_holders.orgRoot", "posMasters.current_holder.current_holders.orgChild1", "posMasters.current_holder.current_holders.orgChild2", "posMasters.current_holder.current_holders.orgChild3", "posMasters.current_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); if (orgRootData.length <= 0) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบโครงสร้าง"); const _orgRevisionActive = await this.orgRevisionRepository.find({ where: { createdAt: LessThan(orgRootData[0]?.createdAt ?? new Date()) }, order: { createdAt: "DESC" }, skip: 1, take: 2, relations: [ "posMasters", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); if (_orgRevisionActive.length <= 0) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประวัติโครงสร้าง"); } let orgRevisionActive = _orgRevisionActive[0]; let data = new Array(); let _node: any; let no = 1; for (let orgRoot of orgRootData) { await Promise.all( orgRoot.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild1Id == null) { const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let positionMasterProfileOld: any = null; if (posMaster.current_holder != null) { positionMasterProfileOld = posMaster.current_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let positionMasterOld: any = null; let profilePositionName: any = []; let profilePosType: any = []; let profilePosLevel: any = []; let profilePosExecutive: any = []; if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { positionMasterOld = orgRevisionActive.posMasters.find( (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), ]; profilePosType = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posType != null) .map((x: any) => x.posType.posTypeName), ), ]; profilePosLevel = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posLevel != null) .map((x: any) => x.posLevel.posLevelName), ), ]; profilePosExecutive = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => x.posExecutive.posExecutiveName), ), ]; } let education: any = ""; if ( posMaster.current_holder != null && posMaster.current_holder.profileEducations != null && posMaster.current_holder.profileEducations.length > 0 ) { let _education: any = posMaster.current_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.current_holder != null && posMaster.current_holder.profileSalary != null && posMaster.current_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.current_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // orgTreeName: orgRoot.orgRootName, orgTreeShortName: orgRoot.orgRootShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.current_holder == null ? positionName.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.current_holder.position, posType: posMaster.current_holder == null ? posType.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posType ?.posTypeName : posMaster.current_holder.posType == null ? "-" : posMaster.current_holder.posType.posTypeName, posLevel: posMaster.current_holder == null ? posLevel.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posLevel ?.posLevelName : posMaster.current_holder.posLevel == null ? "-" : posMaster.current_holder.posLevel.posLevelName, posExecutive: posMaster.current_holder == null ? posExecutive.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileOrgName: posMaster.current_holder == null ? orgRoot.orgRootName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4Name : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3Name : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2Name : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1Name : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootName : "-", profileOrgShortName: posMaster.current_holder == null ? orgRoot.orgRootShortName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4ShortName : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3ShortName : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2ShortName : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1ShortName : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootShortName : "-", profileFullname: posMaster.current_holder == null ? "- ว่าง -" : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, profilePosMasterNo: posMaster.current_holder == null ? positionMasterOld == null ? "-" : positionMasterOld.posMasterNo : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.posMasterNo, profilePositionName: posMaster.current_holder == null ? positionMasterOld == null ? positionName.join(" หรือ ") : profilePositionName.join(" หรือ ") : posMaster.current_holder.position, profilePosType: posMaster.current_holder == null ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") : posMaster.current_holder.posType == null ? "-" : posMaster.current_holder.posType.posTypeName, profilePosLevel: posMaster.current_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") : posMaster.current_holder.posLevel == null ? "-" : posMaster.current_holder.posLevel.posLevelName, profilePosExecutive: posMaster.current_holder == null ? positionMasterOld == null ? posExecutive.join(" หรือ ") : profilePosExecutive.join(" หรือ ") : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, reason: posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName || node.profileOrgShortName != _node.profileOrgShortName || node.profileOrgName != _node.profileOrgName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", profilePosMasterNo: node.profileOrgShortName == _node.profileOrgShortName ? "" : node.profileOrgShortName, profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild1 of orgChild1Data.filter( (orgChild1) => orgChild1.orgRootId === orgRoot.id, )) { await Promise.all( orgChild1.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild2Id == null) { const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let positionMasterProfileOld: any = null; if (posMaster.current_holder != null) { positionMasterProfileOld = posMaster.current_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let positionMasterOld: any = null; let profilePositionName: any = []; let profilePosType: any = []; let profilePosLevel: any = []; let profilePosExecutive: any = []; if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { positionMasterOld = orgRevisionActive.posMasters.find( (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), ]; profilePosType = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posType != null) .map((x: any) => x.posType.posTypeName), ), ]; profilePosLevel = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posLevel != null) .map((x: any) => x.posLevel.posLevelName), ), ]; profilePosExecutive = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => x.posExecutive.posExecutiveName), ), ]; } let education: any = ""; if ( posMaster.current_holder != null && posMaster.current_holder.profileEducations != null && posMaster.current_holder.profileEducations.length > 0 ) { let _education: any = posMaster.current_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.current_holder != null && posMaster.current_holder.profileSalary != null && posMaster.current_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.current_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // orgTreeName: orgChild1.orgChild1Name, orgTreeShortName: orgChild1.orgChild1ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.current_holder == null ? positionName.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.current_holder.position, posType: posMaster.current_holder == null ? posType.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName : posMaster.current_holder.posType == null ? "-" : posMaster.current_holder.posType.posTypeName, posLevel: posMaster.current_holder == null ? posLevel.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName : posMaster.current_holder.posLevel == null ? "-" : posMaster.current_holder.posLevel.posLevelName, posExecutive: posMaster.current_holder == null ? posExecutive.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileOrgName: posMaster.current_holder == null ? orgChild1.orgChild1Name : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4Name : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3Name : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2Name : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1Name : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootName : "-", profileOrgShortName: posMaster.current_holder == null ? orgChild1.orgChild1ShortName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4ShortName : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3ShortName : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2ShortName : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1ShortName : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootShortName : "-", profileFullname: posMaster.current_holder == null ? "- ว่าง -" : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, profilePosMasterNo: posMaster.current_holder == null ? positionMasterOld == null ? "-" : positionMasterOld.posMasterNo : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.posMasterNo, profilePositionName: posMaster.current_holder == null ? positionMasterOld == null ? positionName.join(" หรือ ") : profilePositionName.join(" หรือ ") : posMaster.current_holder.position, profilePosType: posMaster.current_holder == null ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") : posMaster.current_holder.posType == null ? "-" : posMaster.current_holder.posType.posTypeName, profilePosLevel: posMaster.current_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") : posMaster.current_holder.posLevel == null ? "-" : posMaster.current_holder.posLevel.posLevelName, profilePosExecutive: posMaster.current_holder == null ? positionMasterOld == null ? posExecutive.join(" หรือ ") : profilePosExecutive.join(" หรือ ") : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, reason: posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName || node.profileOrgShortName != _node.profileOrgShortName || node.profileOrgName != _node.profileOrgName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", profilePosMasterNo: node.profileOrgShortName == _node.profileOrgShortName ? "" : node.profileOrgShortName, profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild2 of orgChild2Data.filter( (orgChild2) => orgChild2.orgChild1Id === orgChild1.id, )) { await Promise.all( orgChild2.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild3Id == null) { const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let positionMasterProfileOld: any = null; if (posMaster.current_holder != null) { positionMasterProfileOld = posMaster.current_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let positionMasterOld: any = null; let profilePositionName: any = []; let profilePosType: any = []; let profilePosLevel: any = []; let profilePosExecutive: any = []; if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { positionMasterOld = orgRevisionActive.posMasters.find( (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), ]; profilePosType = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posType != null) .map((x: any) => x.posType.posTypeName), ), ]; profilePosLevel = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posLevel != null) .map((x: any) => x.posLevel.posLevelName), ), ]; profilePosExecutive = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => x.posExecutive.posExecutiveName), ), ]; } let education: any = ""; if ( posMaster.current_holder != null && posMaster.current_holder.profileEducations != null && posMaster.current_holder.profileEducations.length > 0 ) { let _education: any = posMaster.current_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.current_holder != null && posMaster.current_holder.profileSalary != null && posMaster.current_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.current_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // orgTreeName: orgChild2.orgChild2Name, orgTreeShortName: orgChild2.orgChild2ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.current_holder == null ? positionName.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.current_holder.position, posType: posMaster.current_holder == null ? posType.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName : posMaster.current_holder.posType == null ? "-" : posMaster.current_holder.posType.posTypeName, posLevel: posMaster.current_holder == null ? posLevel.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName : posMaster.current_holder.posLevel == null ? "-" : posMaster.current_holder.posLevel.posLevelName, posExecutive: posMaster.current_holder == null ? posExecutive.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileOrgName: posMaster.current_holder == null ? orgChild2.orgChild2Name : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4Name : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3Name : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2Name : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1Name : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootName : "-", profileOrgShortName: posMaster.current_holder == null ? orgChild2.orgChild2ShortName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4ShortName : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3ShortName : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2ShortName : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1ShortName : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootShortName : "-", profileFullname: posMaster.current_holder == null ? "- ว่าง -" : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, profilePosMasterNo: posMaster.current_holder == null ? positionMasterOld == null ? "-" : positionMasterOld.posMasterNo : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.posMasterNo, profilePositionName: posMaster.current_holder == null ? positionMasterOld == null ? positionName.join(" หรือ ") : profilePositionName.join(" หรือ ") : posMaster.current_holder.position, profilePosType: posMaster.current_holder == null ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") : posMaster.current_holder.posType == null ? "-" : posMaster.current_holder.posType.posTypeName, profilePosLevel: posMaster.current_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") : posMaster.current_holder.posLevel == null ? "-" : posMaster.current_holder.posLevel.posLevelName, profilePosExecutive: posMaster.current_holder == null ? positionMasterOld == null ? posExecutive.join(" หรือ ") : profilePosExecutive.join(" หรือ ") : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, reason: posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", profilePosMasterNo: Extension.ToThaiNumber( node.profileOrgShortName.toString(), ), profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber( node.profilePosMasterNo.toString(), ), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName || node.profileOrgShortName != _node.profileOrgShortName || node.profileOrgName != _node.profileOrgName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", profilePosMasterNo: node.profileOrgShortName == _node.profileOrgShortName ? "" : node.profileOrgShortName, profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber( node.profilePosMasterNo.toString(), ), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild3 of orgChild3Data.filter( (orgChild3) => orgChild3.orgChild2Id === orgChild2.id, )) { await Promise.all( orgChild3.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild4Id == null) { const positionName = [ ...new Set(posMaster.positions.map((x) => x.positionName)), ]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let positionMasterProfileOld: any = null; if (posMaster.current_holder != null) { positionMasterProfileOld = posMaster.current_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let positionMasterOld: any = null; let profilePositionName: any = []; let profilePosType: any = []; let profilePosLevel: any = []; let profilePosExecutive: any = []; if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { positionMasterOld = orgRevisionActive.posMasters.find( (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), ]; profilePosType = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posType != null) .map((x: any) => x.posType.posTypeName), ), ]; profilePosLevel = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posLevel != null) .map((x: any) => x.posLevel.posLevelName), ), ]; profilePosExecutive = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => x.posExecutive.posExecutiveName), ), ]; } let education: any = ""; if ( posMaster.current_holder != null && posMaster.current_holder.profileEducations != null && posMaster.current_holder.profileEducations.length > 0 ) { let _education: any = posMaster.current_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.current_holder != null && posMaster.current_holder.profileSalary != null && posMaster.current_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.current_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // orgTreeName: orgChild3.orgChild3Name, orgTreeShortName: orgChild3.orgChild3ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.current_holder == null ? positionName.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.current_holder.position, posType: posMaster.current_holder == null ? posType.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName : posMaster.current_holder.posType == null ? "-" : posMaster.current_holder.posType.posTypeName, posLevel: posMaster.current_holder == null ? posLevel.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName : posMaster.current_holder.posLevel == null ? "-" : posMaster.current_holder.posLevel.posLevelName, posExecutive: posMaster.current_holder == null ? posExecutive.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileOrgName: posMaster.current_holder == null ? orgChild3.orgChild3Name : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4Name : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3Name : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2Name : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1Name : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootName : "-", profileOrgShortName: posMaster.current_holder == null ? orgChild3.orgChild3ShortName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4ShortName : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3ShortName : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2ShortName : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1ShortName : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootShortName : "-", profileFullname: posMaster.current_holder == null ? "- ว่าง -" : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, profilePosMasterNo: posMaster.current_holder == null ? positionMasterOld == null ? "-" : positionMasterOld.posMasterNo : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.posMasterNo, profilePositionName: posMaster.current_holder == null ? positionMasterOld == null ? positionName.join(" หรือ ") : profilePositionName.join(" หรือ ") : posMaster.current_holder.position, profilePosType: posMaster.current_holder == null ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") : posMaster.current_holder.posType == null ? "-" : posMaster.current_holder.posType.posTypeName, profilePosLevel: posMaster.current_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") : posMaster.current_holder.posLevel == null ? "-" : posMaster.current_holder.posLevel.posLevelName, profilePosExecutive: posMaster.current_holder == null ? positionMasterOld == null ? posExecutive.join(" หรือ ") : profilePosExecutive.join(" หรือ ") : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, reason: posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", profilePosMasterNo: Extension.ToThaiNumber( node.profileOrgShortName.toString(), ), profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber( node.profilePosMasterNo.toString(), ), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName || node.profileOrgShortName != _node.profileOrgShortName || node.profileOrgName != _node.profileOrgName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", profilePosMasterNo: node.profileOrgShortName == _node.profileOrgShortName ? "" : node.profileOrgShortName, profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber( node.profilePosMasterNo.toString(), ), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild4 of orgChild4Data.filter( (orgChild4) => orgChild4.orgChild3Id === orgChild3.id, )) { await Promise.all( orgChild4.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { const positionName = [ ...new Set(posMaster.positions.map((x) => x.positionName)), ]; const posType = [ ...new Set( posMaster.positions .filter((x: any) => x.posType != null) .map((x) => x.posType.posTypeName), ), ]; const posLevel = [ ...new Set( posMaster.positions .filter((x: any) => x.posLevel != null) .map((x) => x.posLevel.posLevelName), ), ]; const posExecutive = [ ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x) => x.posExecutive.posExecutiveName), ), ]; let positionMasterProfileOld: any = null; if (posMaster.current_holder != null) { positionMasterProfileOld = posMaster.current_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let positionMasterOld: any = null; let profilePositionName: any = []; let profilePosType: any = []; let profilePosLevel: any = []; let profilePosExecutive: any = []; if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { positionMasterOld = orgRevisionActive.posMasters.find( (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), ]; profilePosType = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posType != null) .map((x: any) => x.posType.posTypeName), ), ]; profilePosLevel = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posLevel != null) .map((x: any) => x.posLevel.posLevelName), ), ]; profilePosExecutive = [ ...new Set( positionMasterOld.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => x.posExecutive.posExecutiveName), ), ]; } let education: any = ""; if ( posMaster.current_holder != null && posMaster.current_holder.profileEducations != null && posMaster.current_holder.profileEducations.length > 0 ) { let _education: any = posMaster.current_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.current_holder != null && posMaster.current_holder.profileSalary != null && posMaster.current_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.current_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // orgTreeName: orgChild4.orgChild4Name, orgTreeShortName: orgChild4.orgChild4ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.current_holder == null ? positionName.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.current_holder.position, posType: posMaster.current_holder == null ? posType.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName : posMaster.current_holder.posType == null ? "-" : posMaster.current_holder.posType.posTypeName, posLevel: posMaster.current_holder == null ? posLevel.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName : posMaster.current_holder.posLevel == null ? "-" : posMaster.current_holder.posLevel.posLevelName, posExecutive: posMaster.current_holder == null ? posExecutive.join(" หรือ ") : posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileOrgName: posMaster.current_holder == null ? orgChild4.orgChild4Name : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4Name : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3Name : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2Name : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1Name : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootName : "-", profileOrgShortName: posMaster.current_holder == null ? orgChild4.orgChild4ShortName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null ? positionMasterProfileOld.orgChild4.orgChild4ShortName : positionMasterProfileOld.orgChild3 != null ? positionMasterProfileOld.orgChild3.orgChild3ShortName : positionMasterProfileOld.orgChild2 != null ? positionMasterProfileOld.orgChild2.orgChild2ShortName : positionMasterProfileOld.orgChild1 != null ? positionMasterProfileOld.orgChild1.orgChild1ShortName : positionMasterProfileOld.orgRoot != null ? positionMasterProfileOld.orgRoot.orgRootShortName : "-", profileFullname: posMaster.current_holder == null ? "- ว่าง -" : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, profilePosMasterNo: posMaster.current_holder == null ? positionMasterOld == null ? "-" : positionMasterOld.posMasterNo : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.posMasterNo, profilePositionName: posMaster.current_holder == null ? positionMasterOld == null ? positionName.join(" หรือ ") : profilePositionName.join(" หรือ ") : posMaster.current_holder.position, profilePosType: posMaster.current_holder == null ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") : posMaster.current_holder.posType == null ? "-" : posMaster.current_holder.posType.posTypeName, profilePosLevel: posMaster.current_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") : posMaster.current_holder.posLevel == null ? "-" : posMaster.current_holder.posLevel.posLevelName, profilePosExecutive: posMaster.current_holder == null ? positionMasterOld == null ? posExecutive.join(" หรือ ") : profilePosExecutive.join(" หรือ ") : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, reason: posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", profilePosMasterNo: Extension.ToThaiNumber( node.profileOrgShortName.toString(), ), profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber( node.profilePosMasterNo.toString(), ), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName || node.profileOrgShortName != _node.profileOrgShortName || node.profileOrgName != _node.profileOrgName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", profilePosMasterNo: node.profileOrgShortName == _node.profileOrgShortName ? "" : node.profileOrgShortName, profilePosExecutive: "", profilePositionName: "", profilePosType: "", profilePosLevel: "", education: "", salary: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == null ? "" : Extension.ToThaiNumber(node.posExecutive.toString()), positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), profilePosMasterNo: Extension.ToThaiNumber( node.profilePosMasterNo.toString(), ), profilePosExecutive: node.profilePosExecutive == null ? "" : Extension.ToThaiNumber(node.profilePosExecutive.toString()), profilePositionName: node.profilePositionName == null ? "" : Extension.ToThaiNumber(node.profilePositionName.toString()), profilePosType: node.profilePosType == null ? "" : Extension.ToThaiNumber(node.profilePosType.toString()), profilePosLevel: node.profilePosLevel == null ? "" : Extension.ToThaiNumber(node.profilePosLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; }), ); _node = null; } } } } } return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); } /** * API Report3 * * @summary Report3 * */ @Get("report3/{rootId}") async findReport3(@Path() rootId: string) { // const orgRevision = await this.orgRevisionRepository.findOne({ // where: { orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, // relations: ["orgRoots"], // }); // if (!orgRevision) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); // } const orgRootData = await this.orgRootRepository.find({ where: { id: rootId, // orgRevisionId: orgRevision.id, }, order: { orgRootOrder: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.next_holder", "posMasters.next_holder", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", "posMasters.next_holder.profileSalary", "posMasters.next_holder.profileEducations", "posMasters.next_holder.current_holders", "posMasters.next_holder.current_holders.positions", "posMasters.next_holder.current_holders.orgRoot", "posMasters.next_holder.current_holders.orgChild1", "posMasters.next_holder.current_holders.orgChild2", "posMasters.next_holder.current_holders.orgChild3", "posMasters.next_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; const orgChild1Data = await this.child1Repository.find({ where: { orgRootId: In(orgRootIds), }, order: { orgChild1Order: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", "posMasters.next_holder.profileSalary", "posMasters.next_holder.profileEducations", "posMasters.next_holder.current_holders", "posMasters.next_holder.current_holders.positions", "posMasters.next_holder.current_holders.orgRoot", "posMasters.next_holder.current_holders.orgChild1", "posMasters.next_holder.current_holders.orgChild2", "posMasters.next_holder.current_holders.orgChild3", "posMasters.next_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id) || null; const orgChild2Data = await this.child2Repository.find({ where: { orgChild1: In(orgChild1Ids), }, order: { orgChild2Order: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", "posMasters.next_holder.profileSalary", "posMasters.next_holder.profileEducations", "posMasters.next_holder.current_holders", "posMasters.next_holder.current_holders.positions", "posMasters.next_holder.current_holders.orgRoot", "posMasters.next_holder.current_holders.orgChild1", "posMasters.next_holder.current_holders.orgChild2", "posMasters.next_holder.current_holders.orgChild3", "posMasters.next_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids), }, order: { orgChild3Order: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", "posMasters.next_holder.profileSalary", "posMasters.next_holder.profileEducations", "posMasters.next_holder.current_holders", "posMasters.next_holder.current_holders.positions", "posMasters.next_holder.current_holders.orgRoot", "posMasters.next_holder.current_holders.orgChild1", "posMasters.next_holder.current_holders.orgChild2", "posMasters.next_holder.current_holders.orgChild3", "posMasters.next_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids), }, order: { orgChild4Order: "ASC" }, relations: [ "posMasters", "posMasters.orgRoot", "posMasters.orgChild1", "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", "posMasters.next_holder.profileSalary", "posMasters.next_holder.profileEducations", "posMasters.next_holder.current_holders", "posMasters.next_holder.current_holders.positions", "posMasters.next_holder.current_holders.orgRoot", "posMasters.next_holder.current_holders.orgChild1", "posMasters.next_holder.current_holders.orgChild2", "posMasters.next_holder.current_holders.orgChild3", "posMasters.next_holder.current_holders.orgChild4", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); let orgRevisionActive: any = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, relations: [ "posMasters", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); if (orgRevisionActive == null) { const _orgRevisionActive = await this.orgRevisionRepository.find({ order: { createdAt: "DESC" }, skip: 1, relations: [ "posMasters", "posMasters.positions", "posMasters.positions.posLevel", "posMasters.positions.posType", "posMasters.positions.posExecutive", ], }); if (_orgRevisionActive.length > 0) orgRevisionActive = _orgRevisionActive[0]; } let data = new Array(); let _node: any; let no = 1; for (let orgRoot of orgRootData) { await Promise.all( orgRoot.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild1Id == null && posMaster.next_holder != null) { let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let education: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileEducations != null && posMaster.next_holder.profileEducations.length > 0 ) { let _education: any = posMaster.next_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileSalary != null && posMaster.next_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.next_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { orgTreeName: orgRoot.orgRootName, orgTreeShortName: orgRoot.orgRootShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.next_holder.position, posType: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posType ?.posTypeName : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, posLevel: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posLevel ?.posLevelName : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, posExecutive: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, positionSalaryAmount: salary == "" ? "" : salary.positionSalaryAmount, mouthSalaryAmount: salary == "" ? "" : salary.mouthSalaryAmount, reason: posMaster.reason == null ? "" : posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: "", posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", education: "", salary: "", positionSalaryAmount: "", mouthSalaryAmount: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == undefined ? "" : node.posExecutive, positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", positionSalaryAmount: node.positionSalaryAmount ? Extension.ToThaiNumber(node.positionSalaryAmount.toLocaleString()) : "", mouthSalaryAmount: node.mouthSalaryAmount ? Extension.ToThaiNumber(node.mouthSalaryAmount.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: "", posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", education: "", salary: "", positionSalaryAmount: "", mouthSalaryAmount: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == _node.posExecutive ? "" : node.posExecutive, positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", positionSalaryAmount: node.positionSalaryAmount ? Extension.ToThaiNumber(node.positionSalaryAmount.toLocaleString()) : "", mouthSalaryAmount: node.mouthSalaryAmount ? Extension.ToThaiNumber(node.mouthSalaryAmount.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild1 of orgChild1Data.filter( (orgChild1) => orgChild1.orgRootId === orgRoot.id, )) { await Promise.all( orgChild1.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild2Id == null && posMaster.next_holder != null) { let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let education: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileEducations != null && posMaster.next_holder.profileEducations.length > 0 ) { let _education: any = posMaster.next_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileSalary != null && posMaster.next_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.next_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { orgTreeName: orgChild1.orgChild1Name, orgTreeShortName: orgChild1.orgChild1ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.next_holder.position, posType: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posType ?.posTypeName : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, posLevel: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posLevel ?.posLevelName : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, posExecutive: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, positionSalaryAmount: salary == "" ? "" : salary.positionSalaryAmount, mouthSalaryAmount: salary == "" ? "" : salary.mouthSalaryAmount, reason: posMaster.reason == null ? "" : posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: "", posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", education: "", salary: "", positionSalaryAmount: "", mouthSalaryAmount: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive, positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", positionSalaryAmount: node.positionSalaryAmount ? Extension.ToThaiNumber(node.positionSalaryAmount.toLocaleString()) : "", mouthSalaryAmount: node.mouthSalaryAmount ? Extension.ToThaiNumber(node.mouthSalaryAmount.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: "", posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", education: "", salary: "", positionSalaryAmount: "", mouthSalaryAmount: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == _node.posExecutive ? "" : node.posExecutive, positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", positionSalaryAmount: node.positionSalaryAmount ? Extension.ToThaiNumber(node.positionSalaryAmount.toLocaleString()) : "", mouthSalaryAmount: node.mouthSalaryAmount ? Extension.ToThaiNumber(node.mouthSalaryAmount.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild2 of orgChild2Data.filter( (orgChild2) => orgChild2.orgChild1Id === orgChild1.id, )) { await Promise.all( orgChild2.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild3Id == null && posMaster.next_holder != null) { let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let education: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileEducations != null && posMaster.next_holder.profileEducations.length > 0 ) { let _education: any = posMaster.next_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileSalary != null && posMaster.next_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.next_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { orgTreeName: orgChild2.orgChild2Name, orgTreeShortName: orgChild2.orgChild2ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.next_holder.position, posType: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, posLevel: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, posExecutive: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, positionSalaryAmount: salary == "" ? "" : salary.positionSalaryAmount, mouthSalaryAmount: salary == "" ? "" : salary.mouthSalaryAmount, reason: posMaster.reason == null ? "" : posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: "", posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", education: "", salary: "", positionSalaryAmount: "", mouthSalaryAmount: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive, positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", positionSalaryAmount: node.positionSalaryAmount ? Extension.ToThaiNumber(node.positionSalaryAmount.toLocaleString()) : "", mouthSalaryAmount: node.mouthSalaryAmount ? Extension.ToThaiNumber(node.mouthSalaryAmount.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: "", posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", education: "", salary: "", positionSalaryAmount: "", mouthSalaryAmount: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == _node.posExecutive ? "" : node.posExecutive, positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", positionSalaryAmount: node.positionSalaryAmount ? Extension.ToThaiNumber(node.positionSalaryAmount.toLocaleString()) : "", mouthSalaryAmount: node.mouthSalaryAmount ? Extension.ToThaiNumber(node.mouthSalaryAmount.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild3 of orgChild3Data.filter( (orgChild3) => orgChild3.orgChild2Id === orgChild2.id, )) { await Promise.all( orgChild3.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild4Id == null && posMaster.next_holder != null) { let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let education: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileEducations != null && posMaster.next_holder.profileEducations.length > 0 ) { let _education: any = posMaster.next_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileSalary != null && posMaster.next_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.next_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { orgTreeName: orgChild3.orgChild3Name, orgTreeShortName: orgChild3.orgChild3ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.next_holder.position, posType: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, posLevel: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, posExecutive: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, positionSalaryAmount: salary == "" ? "" : salary.positionSalaryAmount, mouthSalaryAmount: salary == "" ? "" : salary.mouthSalaryAmount, reason: posMaster.reason == null ? "" : posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: "", posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", education: "", salary: "", positionSalaryAmount: "", mouthSalaryAmount: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive, positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", positionSalaryAmount: node.positionSalaryAmount ? Extension.ToThaiNumber(node.positionSalaryAmount.toLocaleString()) : "", mouthSalaryAmount: node.mouthSalaryAmount ? Extension.ToThaiNumber(node.mouthSalaryAmount.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: "", posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", education: "", salary: "", positionSalaryAmount: "", mouthSalaryAmount: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == _node.posExecutive ? "" : node.posExecutive, positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", positionSalaryAmount: node.positionSalaryAmount ? Extension.ToThaiNumber(node.positionSalaryAmount.toLocaleString()) : "", mouthSalaryAmount: node.mouthSalaryAmount ? Extension.ToThaiNumber(node.mouthSalaryAmount.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; for (let orgChild4 of orgChild4Data.filter( (orgChild4) => orgChild4.orgChild3Id === orgChild3.id, )) { await Promise.all( orgChild4.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.next_holder != null) { let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); } let education: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileEducations != null && posMaster.next_holder.profileEducations.length > 0 ) { let _education: any = posMaster.next_holder.profileEducations.sort( (a, b) => b.finishDate == null ? 0 : (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), ); if (_education.length > 0) { education = _education[0]; } } let salary: any = ""; if ( posMaster.next_holder != null && posMaster.next_holder.profileSalary != null && posMaster.next_holder.profileSalary.length > 0 ) { let _salary: any = posMaster.next_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), ); if (_salary.length > 0) { salary = _salary[0]; } } let node = { orgTreeName: orgChild4.orgChild4Name, orgTreeShortName: orgChild4.orgChild4ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.positionName : posMaster.next_holder.position, posType: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, posLevel: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, posExecutive: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, positionSalaryAmount: salary == "" ? "" : salary.positionSalaryAmount, mouthSalaryAmount: salary == "" ? "" : salary.mouthSalaryAmount, reason: posMaster.reason == null ? "" : posMaster.reason, }; if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), profileFullname: "", posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", education: "", salary: "", positionSalaryAmount: "", mouthSalaryAmount: "", reason: "", }; data.push(head); const _head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive, positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", positionSalaryAmount: node.positionSalaryAmount ? Extension.ToThaiNumber(node.positionSalaryAmount.toLocaleString()) : "", mouthSalaryAmount: node.mouthSalaryAmount ? Extension.ToThaiNumber(node.mouthSalaryAmount.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(_head); } else { if ( node.orgTreeShortName != _node.orgTreeShortName || node.orgTreeName != _node.orgTreeName ) { const head = { posMasterNo: node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, profileFullname: "", posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", education: "", salary: "", positionSalaryAmount: "", mouthSalaryAmount: "", reason: "", }; data.push(head); _node == null; } const head = { no: Extension.ToThaiNumber(no.toString()), posMasterNo: node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), profileFullname: node.profileFullname == null ? "" : Extension.ToThaiNumber(node.profileFullname.toString()), posExecutive: node.posExecutive == _node.posExecutive ? "" : node.posExecutive, positionName: node.positionName == null ? "" : Extension.ToThaiNumber(node.positionName.toString()), posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), posLevel: node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), education: node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", positionSalaryAmount: node.positionSalaryAmount ? Extension.ToThaiNumber(node.positionSalaryAmount.toLocaleString()) : "", mouthSalaryAmount: node.mouthSalaryAmount ? Extension.ToThaiNumber(node.mouthSalaryAmount.toLocaleString()) : "", reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), }; data.push(head); } no += 1; _node = node; } }), ); _node = null; } } } } } return new HttpSuccess({ template: "report3", reportName: "report3", data: { data } }); } @Get("report4/{rootId}") async findReport4(@Path() rootId: string) { const orgRootData = await this.orgRootRepository.findOne({ where: { id: rootId }, }); if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); const posMaster = await this.posMasterRepository .createQueryBuilder("posMaster") .leftJoinAndSelect("posMaster.positions", "position") .leftJoinAndSelect("position.posType", "posType") .leftJoinAndSelect("position.posLevel", "posLevel") .where("posMaster.orgRootId = :rootId", { rootId }) .orderBy("posType.posTypeRank", "ASC") .addOrderBy("posLevel.posLevelRank", "ASC") .getMany(); const _posMaster = posMaster.map((x) => ({ type: [...new Set(x.positions.flatMap((y) => y.posType.posTypeName))].join(","), typeRank: [...new Set(x.positions.flatMap((y) => y.posType.posTypeRank))].join(""), level: [...new Set(x.positions.flatMap((y) => y.posLevel.posLevelName))].join(","), levelRank: [ ...new Set( x.positions.flatMap((y) => `${y.posType.posTypeRank}${y.posLevel.posLevelRank}`), ), ].join(""), positions: [...new Set(x.positions.flatMap((y) => y.positionName))].join(""), })); const groupedData = _posMaster.reduce((acc: any, curr: any) => { const key = `${curr.type}|${curr.typeRank}|${curr.level}|${curr.levelRank}`; if (!acc[key]) { acc[key] = { ...curr, total: 1 }; } else { acc[key].total += 1; } return acc; }, {}); let result = Object.values(groupedData) .map((x: any) => ({ type: x.type, typeRank: parseInt(x.typeRank), level: x.level, levelRank: parseInt(x.levelRank), total: x.total, remark: x.positions, })) .sort((x, y) => { if (x.typeRank !== y.typeRank) { return x.typeRank - y.typeRank; } return x.levelRank - y.levelRank; }); let tmpType: string = ""; let allTotal: number = 0; let total: number = 0; let _total: number = 0; let _reslut = new Array(); result.forEach((x: any, idx: number) => { allTotal += x.total; total += x.total; if (x.type === tmpType) { _reslut.push({ ...x, type: "", }); } else { if (x.type !== tmpType && tmpType != "") { _total = total - x.total; _reslut.push({ type: "", typeRank: "", level: "รวม", levelRank: "", total: _total, remark: "", }); total = x.total; _total = 0; } _reslut.push({ ...x, }); } tmpType = x.type; }); _reslut.push({ type: "", typeRank: "", level: "รวม", levelRank: "", total: total, remark: "", }); _reslut.push({ type: "", typeRank: "", level: "รวมทั้งสิ้น", levelRank: "", total: allTotal, remark: "", }); return new HttpSuccess({ template: "report4", reportName: "report4", data: { dateCurrent: Extension.ToThaiShortDate(new Date()), rootName: orgRootData ? orgRootData.orgRootName : "-", data: _reslut, }, }); } @Get("report4-employee/{rootId}") async findReportEmp4(@Path() rootId: string) { const orgRootData = await this.orgRootRepository.findOne({ where: { id: rootId }, }); if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); const posMaster = await this.empPosMasterRepository .createQueryBuilder("posMaster") .leftJoinAndSelect("posMaster.positions", "position") .leftJoinAndSelect("position.posType", "posType") .leftJoinAndSelect("position.posLevel", "posLevel") .where("posMaster.orgRootId = :rootId", { rootId }) .orderBy("posType.posTypeRank", "ASC") .addOrderBy("posLevel.posLevelRank", "ASC") .getMany(); const _posMaster = posMaster.map((x) => ({ type: [...new Set(x.positions.flatMap((y) => y.posType.posTypeName))].join(","), typeRank: [...new Set(x.positions.flatMap((y) => y.posType.posTypeRank))].join(""), level: [...new Set(x.positions.flatMap((y) => y.posLevel.posLevelName))].join(","), levelRank: [ ...new Set( x.positions.flatMap((y) => `${y.posType.posTypeRank}${y.posLevel.posLevelRank}`), ), ].join(""), positions: [...new Set(x.positions.flatMap((y) => y.positionName))].join(""), })); const groupedData = _posMaster.reduce((acc: any, curr: any) => { const key = `${curr.type}|${curr.typeRank}|${curr.level}|${curr.levelRank}`; if (!acc[key]) { acc[key] = { ...curr, total: 1 }; } else { acc[key].total += 1; } return acc; }, {}); let result = Object.values(groupedData) .map((x: any) => ({ type: x.type, typeRank: parseInt(x.typeRank), level: x.level, levelRank: parseInt(x.levelRank), total: x.total, remark: x.positions, })) .sort((x, y) => { if (x.typeRank !== y.typeRank) { return x.typeRank - y.typeRank; } return x.levelRank - y.levelRank; }); let tmpType: string = ""; let allTotal: number = 0; let total: number = 0; let _total: number = 0; let _reslut = new Array(); result.forEach((x: any, idx: number) => { allTotal += x.total; total += x.total; if (x.type === tmpType) { _reslut.push({ ...x, type: "", }); } else { if (x.type !== tmpType && tmpType != "") { _total = total - x.total; _reslut.push({ type: "", typeRank: "", level: "รวม", levelRank: "", total: _total, remark: "", }); total = x.total; _total = 0; } _reslut.push({ ...x, }); } tmpType = x.type; }); _reslut.push({ type: "", typeRank: "", level: "รวม", levelRank: "", total: total, remark: "", }); _reslut.push({ type: "", typeRank: "", level: "รวมทั้งสิ้น", levelRank: "", total: allTotal, remark: "", }); return new HttpSuccess({ template: "report4", reportName: "report4", data: { dateCurrent: Extension.ToThaiShortDate(new Date()), rootName: orgRootData ? orgRootData.orgRootName : "-", data: _reslut, }, }); } @Get("report4-employee-temp/{rootId}") async findReportEmp4Temp(@Path() rootId: string) { const orgRootData = await this.orgRootRepository.findOne({ where: { id: rootId }, }); if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); const posMaster = await this.empTempPosMasterRepository .createQueryBuilder("posMaster") .leftJoinAndSelect("posMaster.positions", "position") .leftJoinAndSelect("position.posType", "posType") .leftJoinAndSelect("position.posLevel", "posLevel") .where("posMaster.orgRootId = :rootId", { rootId }) .orderBy("posType.posTypeRank", "ASC") .addOrderBy("posLevel.posLevelRank", "ASC") .getMany(); const _posMaster = posMaster.map((x) => ({ type: [...new Set(x.positions.flatMap((y) => y.posType.posTypeName))].join(","), typeRank: [...new Set(x.positions.flatMap((y) => y.posType.posTypeRank))].join(""), level: [...new Set(x.positions.flatMap((y) => y.posLevel.posLevelName))].join(","), levelRank: [ ...new Set( x.positions.flatMap((y) => `${y.posType.posTypeRank}${y.posLevel.posLevelRank}`), ), ].join(""), positions: [...new Set(x.positions.flatMap((y) => y.positionName))].join(""), })); const groupedData = _posMaster.reduce((acc: any, curr: any) => { const key = `${curr.type}|${curr.typeRank}|${curr.level}|${curr.levelRank}`; if (!acc[key]) { acc[key] = { ...curr, total: 1 }; } else { acc[key].total += 1; } return acc; }, {}); let result = Object.values(groupedData) .map((x: any) => ({ type: x.type, typeRank: parseInt(x.typeRank), level: x.level, levelRank: parseInt(x.levelRank), total: x.total, remark: x.positions, })) .sort((x, y) => { if (x.typeRank !== y.typeRank) { return x.typeRank - y.typeRank; } return x.levelRank - y.levelRank; }); let tmpType: string = ""; let allTotal: number = 0; let total: number = 0; let _total: number = 0; let _reslut = new Array(); result.forEach((x: any, idx: number) => { allTotal += x.total; total += x.total; if (x.type === tmpType) { _reslut.push({ ...x, type: "", }); } else { if (x.type !== tmpType && tmpType != "") { _total = total - x.total; _reslut.push({ type: "", typeRank: "", level: "รวม", levelRank: "", total: _total, remark: "", }); total = x.total; _total = 0; } _reslut.push({ ...x, }); } tmpType = x.type; }); _reslut.push({ type: "", typeRank: "", level: "รวม", levelRank: "", total: total, remark: "", }); _reslut.push({ type: "", typeRank: "", level: "รวมทั้งสิ้น", levelRank: "", total: allTotal, remark: "", }); return new HttpSuccess({ template: "report4", reportName: "report4", data: { dateCurrent: Extension.ToThaiShortDate(new Date()), rootName: orgRootData ? orgRootData.orgRootName : "-", data: _reslut, }, }); } }