diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index b9753f1a..67a27ccb 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3012,11 +3012,12 @@ export class ReportController extends Controller { ], }); - if (orgRootData.length < 0) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบโครงสร้าง"); + 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", @@ -3025,7 +3026,1973 @@ export class ReportController extends Controller { "posMasters.positions.posExecutive", ], }); - return new HttpSuccess(); + 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.date == null ? 0 : b.date.getTime()) - + (a.date == null ? 0 : a.date.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.date == null ? 0 : b.date.getTime()) - + (a.date == null ? 0 : a.date.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.date == null ? 0 : b.date.getTime()) - + (a.date == null ? 0 : a.date.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.date == null ? 0 : b.date.getTime()) - + (a.date == null ? 0 : a.date.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.date == null ? 0 : b.date.getTime()) - + (a.date == null ? 0 : a.date.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