diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 4e5f898e..68bd0588 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -1478,7 +1478,7 @@ export class CommandController extends Controller { return new HttpSuccess(); } - async cronjobUpdateRetirementStatus() { + async cronjobUpdateRetirementStatus(/*@Request() request: RequestWithUser*/) { let body = { client_id: "gettoken", client_secret: process.env.AUTH_ACCOUNT_SECRET, @@ -1499,6 +1499,7 @@ export class CommandController extends Controller { const today = new Date(); today.setUTCHours(0, 0, 0, 0); let type: string = "OFFICER"; + let _Date = new Date() try { const response_ = await axios.get( process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`, @@ -1510,23 +1511,46 @@ export class CommandController extends Controller { }, }, ); - if (response && response_.data.result.length > 0) { - let profiles: Profile[] = []; + if (response_ && response_.data.result) { + let signDate:string = "" + if (response_.data.result.signDate != null) { + signDate = Extension.ToThaiShortDate_noPrefix(new Date(response_.data.result.signDate)) + } + else { + signDate = Extension.ToThaiShortDate_noPrefix(_Date) + } + // let profiles: Profile[] = []; await Promise.all( - response_.data.result.map(async (x: any) => { + response_.data.result.profiles.map(async (x: any) => { const _profile = await this.profileRepository.findOneBy({ id: x.profileId }); if (_profile) { + // console.log("1. บันทึกลงประวัติตำแหน่ง") + await this.profileSalaryRetire(x.profileId, type, signDate, _Date); + // console.log("2. ปลดออกจากโครงสร้าง") + await this.posMasterRetire(x.profileId, type, _Date); + // console.log("3. แก้ไขสถานะในทะเบียนประวัติ") _profile.isRetirement = true; _profile.isLeave = true; + _profile.isActive = false _profile.leaveType = "RETIRE"; - _profile.leaveDate = new Date(); - _profile.dateLeave = new Date(); - _profile.lastUpdatedAt = new Date(); - profiles.push(_profile); + _profile.leaveReason = "เกษียณอายุราชการ"; + _profile.leaveDate = _Date; + _profile.dateLeave = _Date; + _profile.lastUpdatedAt = _Date; + if (_profile.keycloak != null && _profile.keycloak != "") { + // console.log("4. disable keycloak/authen") + const delUserKeycloak = await deleteUser(_profile.keycloak); + if (delUserKeycloak) { + _profile.keycloak = ""; + _profile.roleKeycloaks = []; + } + } + // profiles.push(_profile); + // console.log("5. save profile ",_profile) + await this.profileRepository.save(_profile); } }), ); - await this.profileRepository.save(profiles); } } catch {} @@ -1542,29 +1566,144 @@ export class CommandController extends Controller { }, }, ); - if (response && response_.data.result.length > 0) { - let profiles: ProfileEmployee[] = []; + if (response_ && response_.data.result) { + let signDate:string = "" + if (response_.data.result.signDate != null) { + signDate = Extension.ToThaiShortDate_noPrefix(new Date(response_.data.result.signDate)) + } + else { + signDate = Extension.ToThaiShortDate_noPrefix(_Date) + } + // let profiles: ProfileEmployee[] = []; await Promise.all( - response_.data.result.map(async (x: any) => { + response_.data.result.profiles.map(async (x: any) => { const _profileEmp = await this.profileEmployeeRepository.findOneBy({ id: x.profileId }); if (_profileEmp) { + // บันทึกลงประวัติตำแหน่ง + await this.profileSalaryRetire(x.profileId, type, signDate, _Date); + // ปลดออกจากโครงสร้าง + await this.posMasterRetire(x.profileId, type, _Date); + // แก้ไขสถานะในทะเบียนประวัติ _profileEmp.isRetirement = true; _profileEmp.isLeave = true; + _profileEmp.isActive = false _profileEmp.leaveType = "RETIRE"; - _profileEmp.leaveDate = new Date(); - _profileEmp.dateLeave = new Date(); - _profileEmp.lastUpdatedAt = new Date(); - profiles.push(_profileEmp); + _profileEmp.leaveReason = "เกษียณอายุราชการ"; + _profileEmp.leaveDate = _Date; + _profileEmp.dateLeave = _Date; + _profileEmp.lastUpdatedAt = _Date; + if (_profileEmp.keycloak != null && _profileEmp.keycloak != "") { + // disable keycloak/authen + const delUserKeycloak = await deleteUser(_profileEmp.keycloak); + if (delUserKeycloak) { + _profileEmp.keycloak = ""; + _profileEmp.roleKeycloaks = []; + } + } + // profiles.push(_profileEmp); + await this.profileEmployeeRepository.save(_profileEmp); } }), ); - await this.profileEmployeeRepository.save(profiles); } } catch {} return new HttpSuccess(); } + async profileSalaryRetire(profileId: string, type: string, signDate: string, _Date: Date) { + const whereKey = type == "OFFICER" + ? { profileId: profileId } + : { profileEmployeeId: profileId }; + const maxOrder = await this.salaryRepo.findOne({ + select: { order: true }, + where: whereKey, + order: { order: "DESC" }, + }); + const data: any = { + order: maxOrder ? maxOrder.order + 1 : 1, + amount: null, + positionSalaryAmount: null, + mouthSalaryAmount: null, + posNo: null, + positionExecutive: null, + positionType: null, + positionLevel: null, + amountSpecial: null, + orgRoot: null, + orgChild1: null, + orgChild2: null, + orgChild3: null, + orgChild4: null, + commandYear: _Date.getFullYear() + 543, + commandDateAffect: _Date, + commandCode: "16", + commandName: "พ้นจากราชการ", + posNoAbb: null, + isEntry: false, + positionName: "เกษียณอายุราชการ", + createdUserId: "", + createdFullName: "System Administrator", + lastUpdateUserId: "", + lastUpdateFullName: "System Administrator", + createdAt: _Date, + lastUpdatedAt: _Date, + remark: `ประกาศคณะอนุกรรมการสามัญข้าราชการกรุงเทพมหานครสามัญ ลว. ${signDate}`, // script เกษียณจริง ๆ ให้เอา “วันที่ประกาศเกษียณฉบับแรก” มาลงในเอกสารอ้างอิง + isGovernment: false, + }; + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + data.dateGovernment = _Date; + if (type == "OFFICER") { + data.profileId = profileId; + data.profileEmployeeId = null; + } + else if (type == "EMPLOYEE"){ + data.profileEmployeeId = profileId; + data.profileId = null; + } + const savedData = await this.salaryRepo.save(data); + history.profileSalaryId = savedData.id; + await this.salaryHistoryRepo.save(history); + } + + async posMasterRetire(profileId: string, type: string, _Date: Date) { + const orgRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false } + }); + if (orgRevision) { + let _posMaster:any = null; + if (type == "OFFICER") { + _posMaster = await this.posMasterRepository.findOne({ + where: { + orgRevisionId: orgRevision.id, + current_holderId: profileId, + }, + }); + if (_posMaster) { + _posMaster.current_holderId = null; + _posMaster.lastUpdateFullName = "System Administrator"; + _posMaster.lastUpdatedAt = _Date; + await this.posMasterRepository.save(_posMaster); + } + } + else if (type == "EMPLOYEE") { + _posMaster = await this.employeePosMasterRepository.findOne({ + where: { + orgRevisionId: orgRevision.id, + current_holderId: profileId, + }, + }); + if (_posMaster) { + _posMaster.current_holderId = null; + _posMaster.lastUpdateFullName = "System Administrator"; + _posMaster.lastUpdatedAt = _Date; + await this.employeePosMasterRepository.save(_posMaster); + } + } + } + } + @Get("cornjob/cronjobUpdateRetirementStatus") async runCronjobUpdateRetirementStatus() { await this.cronjobUpdateRetirementStatus(); diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 5ad7c11c..c12b1a72 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -1,3 +1,5 @@ +import { ProfileSalaryHistory } from "./../entities/ProfileSalaryHistory"; +import { ProfileSalary } from "./../entities/ProfileSalary"; import { Controller, Get, @@ -31,6 +33,7 @@ import { sendToQueueOrg, sendToQueueOrgDraft } from "../services/rabbitmq"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { PermissionOrg } from "../entities/PermissionOrg"; +import { deleteUser } from "../keycloak"; @Route("api/v1/org") @Tags("Organization") @@ -51,6 +54,8 @@ export class OrganizationController extends Controller { private posTypeRepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); private permissionOrgRepository = AppDataSource.getRepository(PermissionOrg); + private profileSalaryRepository = AppDataSource.getRepository(ProfileSalary); + private salaryHistoryRepo = AppDataSource.getRepository(ProfileSalaryHistory); /** * API ล้างข้อมูล @@ -7915,4 +7920,151 @@ export class OrganizationController extends Controller { return new HttpSuccess(); } + + /** + * API ลบคนในโครงสร้าง + * + * @summary - ลบคนในโครงสร้าง (ADMIN) + * + */ + @Get("delete/profile/org/{orgRevisionId}") + async deleteRetireInOrg(@Path() orgRevisionId: string, @Request() request: RequestWithUser) { + const posMasters = await this.posMasterRepository.find({ + where: { + orgRevisionId: orgRevisionId, + current_holder: { + isLeave: true, + }, + }, + }); + + let check = 0; + await Promise.all( + posMasters.map(async (posMaster) => { + posMaster.current_holderId = null; + await this.posMasterRepository.save(posMaster); + check += 1; + }), + ); + + // จำนวนคนที่ถูกลบออกจากโครงสร้าง + const total = posMasters.length; + return new HttpSuccess({ total, successAmount: check }); + } + + /** + * API บันทึกลงประวัติตำแหน่ง + * + * @summary - แก้ไขเหตุผลการลาออก และปลดจาก keycloak (ADMIN) + * + */ + @Get("save/profile/position-history") + async saveRetireToPositionHistory(@Request() request: RequestWithUser) { + const profileLeave = await this.profileRepo.find({ + where: { + isLeave: true, + leaveType: "RETIRE", + }, + }); + + let check: number = 0; + await Promise.all( + profileLeave.map(async (profile: any) => { + const dest_item = await this.profileSalaryRepository.findOne({ + where: { profileId: profile.id }, + order: { order: "DESC" }, + }); + const data: any = { + order: dest_item == null ? 1 : dest_item.order + 1, + amount: null, + positionSalaryAmount: null, + mouthSalaryAmount: null, + profileId: profile.id, + posNo: null, + positionExecutive: null, + positionType: null, + positionLevel: null, + amountSpecial: null, + orgRoot: null, + orgChild1: null, + orgChild2: null, + orgChild3: null, + orgChild4: null, + commandYear: new Date().getFullYear() + 543, + commandDateAffect: profile.dateLeave, + commandCode: "16", + commandName: "พ้นจากราชการ", + posNoAbb: null, + isEntry: false, + positionName: "เกษียณอายุราชการ", + createdUserId: request.user.sub, + createdFullName: request.user.name, + lastUpdateUserId: request.user.sub, + lastUpdateFullName: request.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + remark: "ประกาศคณะอนุกรรมการสามัญข้าราชการกรุงเทพมหานครสามัญ ลว. 31 มี.ค. 68", // script เกษียณจริง ๆ ให้เอา “วันที่ประกาศเกษียณฉบับแรก” มาลงในเอกสารอ้างอิง + isGovernment: false, + }; + + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + data.dateGovernment = profile.dateLeave; + const savedData = await this.profileSalaryRepository.save(data); + + history.profileSalaryId = savedData.id; + await this.salaryHistoryRepo.save(history); + + check += 1; + }), + ); + + // จำนวนคนที่บันทึกลงประวัติตำแหน่ง + const total = profileLeave.length; + // จำนวนคนที่ถูกบันทึกลงประวัติตำแหน่งสำเร็จ + return new HttpSuccess({ total, successAmount: check }); + } + + /** + * API แก้ไขเหตุผลการลาออก และปลดจาก keycloak + * + * @summary - แก้ไขเหตุผลการลาออก และปลดจาก keycloak (ADMIN) + * + */ + @Get("update/profile/leave-reason") + async updateRetireReason() { + const profileLeave = await this.profileRepo.find({ + where: { + isLeave: true, + leaveType: "RETIRE", + }, + }); + + let check: number = 0; + let notDelete: string[] = []; + await Promise.all( + profileLeave.map(async (profile) => { + profile.leaveReason = "เกษียณอายุราชการ"; + profile.isActive = false; + + if (profile.keycloak != null && profile.keycloak != "") { + const delUserKeycloak = await deleteUser(profile.keycloak); + if (delUserKeycloak) { + profile.keycloak = ""; + profile.roleKeycloaks = []; + check += 1; + } else { + // push array not delete + notDelete.push(profile.keycloak); + } + } + + await this.profileRepo.save(profile); + }), + ); + + // จำนวนคนที่ถูกแก้ไขเหตุผลการลาออก + const total = profileLeave.length; + return new HttpSuccess({ total, successAmount: check, notDelete }); + } } diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index 415759af..38e17b06 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -5521,7 +5521,7 @@ export class OrganizationDotnetController extends Controller { govAge: profile.dateAppoint ? `${Extension.CalculateGovAge(profile.dateAppoint, 0, 0)} ปี` : "-", - age: profile.birthDate ? Extension.CalculateAgeStrV2(profile.birthDate, 0, 0) : "-", + age: profile.birthDate ? Extension.CalculateAgeStrV2(profile.birthDate, 0, 0, "GET") : "-", dateAppoint: profile.dateAppoint, dateCurrent: new Date(), amount: profile.amount ?? "-", @@ -5636,7 +5636,7 @@ export class OrganizationDotnetController extends Controller { govAge: profile.dateAppoint ? `${Extension.CalculateGovAge(profile.dateAppoint, 0, 0)} ปี` : "-", - age: profile.birthDate ? Extension.CalculateAgeStrV2(profile.birthDate, 0, 0) : "-", + age: profile.birthDate ? Extension.CalculateAgeStrV2(profile.birthDate, 0, 0, "GET") : "-", dateAppoint: profile.dateAppoint, dateCurrent: new Date(), amount: profile.amount ?? "-", diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 33d33227..e6f4c1e7 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -5888,7 +5888,7 @@ export class ProfileController extends Controller { ) // .andWhere("profile.leaveCommandId Is NOT NULL") .andWhere( - "profileSalary.order = (SELECT MAX(ps.order) FROM profileSalary ps WHERE ps.profileId = profile.id)", + "profileSalary.order = (SELECT MAX(ps.order) FROM profileSalary ps WHERE ps.profileId = profile.id and ps.positionName != 'เกษียณอายุราชการ')", ) .andWhere( @@ -9781,6 +9781,8 @@ export class ProfileController extends Controller { rootId?: string; year: number; period: string; + sortBy?: string; + descending?: boolean; }, ) { const findRevision = await this.orgRevisionRepo.findOne({ @@ -9790,7 +9792,7 @@ export class ProfileController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } - const [findPosMaster, total] = await AppDataSource.getRepository(PosMaster) + let query = await AppDataSource.getRepository(PosMaster) .createQueryBuilder("posMaster") .leftJoinAndSelect("posMaster.current_holder", "current_holder") .leftJoinAndSelect("posMaster.orgRoot", "orgRoot") @@ -9923,7 +9925,37 @@ export class ProfileController extends Controller { ); }), ) - .orderBy("current_holder.citizenId", "ASC") + + if (body.sortBy) { + if(body.sortBy === "posType"){ + query = query.orderBy( + `posType.posTypeName`, + body.descending ? "DESC" : "ASC" + ); + }else if(body.sortBy === "posLevel"){ + query = query.orderBy( + `posLevel.posLevelName`, + body.descending ? "DESC" : "ASC" + ); + }else if(body.sortBy === "orgShortName" || body.sortBy === "posMasterNo"){ + query = query + .orderBy(`orgRoot.orgRootShortName`,body.descending ? "DESC" : "ASC") + .addOrderBy(`orgChild1.orgChild1ShortName`,body.descending ? "DESC" : "ASC") + .addOrderBy(`orgChild2.orgChild2ShortName`,body.descending ? "DESC" : "ASC") + .addOrderBy(`orgChild3.orgChild3ShortName`,body.descending ? "DESC" : "ASC") + .addOrderBy(`orgChild4.orgChild4ShortName`,body.descending ? "DESC" : "ASC") + .addOrderBy(`posMaster.posMasterNo`,body.descending ? "DESC" : "ASC") + }else{ + query = query.orderBy( + `current_holder.${body.sortBy}`, + body.descending ? "DESC" : "ASC" + ); + } + }else{ + query = query.orderBy("current_holder.citizenId", "ASC") + } + + const [findPosMaster, total] = await query .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 04b43f87..b217b3b3 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -2127,7 +2127,7 @@ export class ProfileEmployeeController extends Controller { posNo: _data.employeeClass == "TEMP" ? _data.posMasterNoTemp : shortName, employeeClass: _data.employeeClass == null ? null : _data.employeeClass, govAge: Extension.CalculateGovAge(_data.dateAppoint, 0, 0), - age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0), + age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0, "GET"), dateEmployment: dateEmployment, dateAppoint: _data.dateAppoint, dateStart: _data.dateStart, @@ -2786,7 +2786,7 @@ export class ProfileEmployeeController extends Controller { ) // .andWhere("profileEmployee.leaveCommandId Is NOT NULL") .andWhere( - "profileSalary.order = (SELECT MAX(ps.order) FROM profileSalary ps WHERE ps.profileEmployeeId = profileEmployee.id)", + "profileSalary.order = (SELECT MAX(ps.order) FROM profileSalary ps WHERE ps.profileEmployeeId = profileEmployee.id and ps.positionName != 'เกษียณอายุราชการ')", ) // .andWhere( @@ -2995,7 +2995,7 @@ export class ProfileEmployeeController extends Controller { : _data.profileSalary[0].posNo || "", employeeClass: _data.employeeClass == null ? null : _data.employeeClass, govAge: Extension.CalculateGovAge(_data.dateAppoint, 0, 0), - age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0), + age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0, "GET"), dateEmployment: dateEmployment, dateAppoint: _data.dateAppoint, dateStart: _data.dateStart, @@ -3417,7 +3417,7 @@ export class ProfileEmployeeController extends Controller { posNo: _data.employeeClass == "TEMP" ? _data.posMasterNoTemp : shortName, employeeClass: _data.employeeClass == null ? null : _data.employeeClass, govAge: Extension.CalculateGovAge(_data.dateAppoint, 0, 0), - age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0), + age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0, "GET"), dateEmployment: dateEmployment, dateAppoint: _data.dateAppoint, dateStart: _data.dateStart, @@ -4981,6 +4981,8 @@ export class ProfileEmployeeController extends Controller { rootId?: string; year: number; period: string; + sortBy?: string; + descending?: boolean; }, ) { const findRevision = await this.orgRevisionRepo.findOne({ @@ -4990,7 +4992,7 @@ export class ProfileEmployeeController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } - const [findPosMaster, total] = await AppDataSource.getRepository(EmployeePosMaster) + let query = await AppDataSource.getRepository(EmployeePosMaster) .createQueryBuilder("employeePosMaster") .leftJoinAndSelect("employeePosMaster.current_holder", "current_holder") .leftJoinAndSelect("employeePosMaster.orgRoot", "orgRoot") @@ -5122,10 +5124,40 @@ export class ProfileEmployeeController extends Controller { ); }), ) - .orderBy("current_holder.citizenId", "ASC") + + if (body.sortBy) { + if(body.sortBy === "posType"){ + query = query.orderBy( + `posType.posTypeName`, + body.descending ? "DESC" : "ASC" + ); + }else if(body.sortBy === "posLevel"){ + query = query + .orderBy(`posType.posTypeShortName`,body.descending ? "DESC" : "ASC") + .addOrderBy(`posLevel.posLevelName`,body.descending ? "DESC" : "ASC"); + }else if(body.sortBy === "orgShortName" || body.sortBy === "posMasterNo"){ + query = query + .orderBy(`orgRoot.orgRootShortName`,body.descending ? "DESC" : "ASC") + .addOrderBy(`orgChild1.orgChild1ShortName`,body.descending ? "DESC" : "ASC") + .addOrderBy(`orgChild2.orgChild2ShortName`,body.descending ? "DESC" : "ASC") + .addOrderBy(`orgChild3.orgChild3ShortName`,body.descending ? "DESC" : "ASC") + .addOrderBy(`orgChild4.orgChild4ShortName`,body.descending ? "DESC" : "ASC") + .addOrderBy(`employeePosMaster.posMasterNo`,body.descending ? "DESC" : "ASC") + }else{ + query = query.orderBy( + `current_holder.${body.sortBy}`, + body.descending ? "DESC" : "ASC" + ); + } + }else{ + query = query.orderBy("current_holder.citizenId", "ASC") + } + + const [findPosMaster, total] = await query .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); + if (!findPosMaster) { throw new HttpError(HttpStatus.NOT_FOUND, "not found. PosMaster"); } diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index e5d03e4a..12c65e10 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -1189,7 +1189,7 @@ export class ProfileEmployeeTempController extends Controller { posNo: _data.employeeClass == "TEMP" ? _data.posMasterNoTemp : shortName, employeeClass: _data.employeeClass == null ? null : _data.employeeClass, govAge: Extension.CalculateGovAge(_data.dateAppoint, 0, 0), - age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0), + age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0, "GET"), dateEmployment: dateEmployment, dateAppoint: _data.dateAppoint, dateStart: _data.dateStart, @@ -1724,7 +1724,7 @@ export class ProfileEmployeeTempController extends Controller { posNo: _data.employeeClass == "TEMP" ? _data.posMasterNoTemp : shortName, employeeClass: _data.employeeClass == null ? null : _data.employeeClass, govAge: Extension.CalculateGovAge(_data.dateAppoint, 0, 0), - age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0), + age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0, "GET"), dateEmployment: dateEmployment, dateAppoint: _data.dateAppoint, dateStart: _data.dateStart, diff --git a/src/controllers/ProfileGovernmentController.ts b/src/controllers/ProfileGovernmentController.ts index 6f63bd0e..f2acbaba 100644 --- a/src/controllers/ProfileGovernmentController.ts +++ b/src/controllers/ProfileGovernmentController.ts @@ -237,14 +237,37 @@ export class ProfileGovernmentHistoryController extends Controller { } } let _OrgLeave:any = [] + let _profileSalary:any = null; if (record?.isLeave && record?.profileSalary.length > 0) { - _OrgLeave = [ - record?.profileSalary[0].orgChild4 ? record?.profileSalary[0].orgChild4 : null, - record?.profileSalary[0].orgChild3 ? record?.profileSalary[0].orgChild3 : null, - record?.profileSalary[0].orgChild2 ? record?.profileSalary[0].orgChild2 : null, - record?.profileSalary[0].orgChild1 ? record?.profileSalary[0].orgChild1 : null, - record?.profileSalary[0].orgRoot ? record?.profileSalary[0].orgRoot : null, - ]; + // _OrgLeave = [ + // record?.profileSalary[0].orgChild4 ? record?.profileSalary[0].orgChild4 : null, + // record?.profileSalary[0].orgChild3 ? record?.profileSalary[0].orgChild3 : null, + // record?.profileSalary[0].orgChild2 ? record?.profileSalary[0].orgChild2 : null, + // record?.profileSalary[0].orgChild1 ? record?.profileSalary[0].orgChild1 : null, + // record?.profileSalary[0].orgRoot ? record?.profileSalary[0].orgRoot : null, + // ]; + if (record.leaveType == "RETIRE") { + _profileSalary = record?.profileSalary.length > 1 + ? record?.profileSalary[1] + : record?.profileSalary.length > 0 + ? record?.profileSalary[0] + : null; + } else { + _profileSalary = record?.profileSalary.length > 0 + ? record?.profileSalary[0] + : null; + } + if (_profileSalary) { + _OrgLeave = [ + _profileSalary.orgChild4 ?? null, + _profileSalary.orgChild3 ?? null, + _profileSalary.orgChild2 ?? null, + _profileSalary.orgChild1 ?? null, + _profileSalary.orgRoot ?? null, + ]; + } else { + _OrgLeave = []; + } } const orgLeave = _OrgLeave.filter((x:any) => x !== undefined && x !== null).join("\n"); const data = { @@ -254,8 +277,8 @@ export class ProfileGovernmentHistoryController extends Controller { posLevel: record?.posLevel == null ? null : record?.posLevel.posLevelName, //ระดับ posMasterNo: record?.isLeave == false ? posMaster == null ? null : `${orgShortName} ${posMaster.posMasterNo}` - : record && record?.profileSalary.length > 0 - ? `${record?.profileSalary[0].posNoAbb} ${record?.profileSalary[0].posNo}` + : _profileSalary != null + ? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}` : null, //เลขที่ตำแหน่ง posType: record?.posType == null ? null : record?.posType.posTypeName, //ประเภท posExecutive: @@ -373,14 +396,37 @@ export class ProfileGovernmentHistoryController extends Controller { } } let _OrgLeave:any = [] + let _profileSalary:any = null; if (record?.isLeave && record?.profileSalary.length > 0) { - _OrgLeave = [ - record?.profileSalary[0].orgChild4 ? record?.profileSalary[0].orgChild4 : null, - record?.profileSalary[0].orgChild3 ? record?.profileSalary[0].orgChild3 : null, - record?.profileSalary[0].orgChild2 ? record?.profileSalary[0].orgChild2 : null, - record?.profileSalary[0].orgChild1 ? record?.profileSalary[0].orgChild1 : null, - record?.profileSalary[0].orgRoot ? record?.profileSalary[0].orgRoot : null, - ]; + // _OrgLeave = [ + // record?.profileSalary[0].orgChild4 ? record?.profileSalary[0].orgChild4 : null, + // record?.profileSalary[0].orgChild3 ? record?.profileSalary[0].orgChild3 : null, + // record?.profileSalary[0].orgChild2 ? record?.profileSalary[0].orgChild2 : null, + // record?.profileSalary[0].orgChild1 ? record?.profileSalary[0].orgChild1 : null, + // record?.profileSalary[0].orgRoot ? record?.profileSalary[0].orgRoot : null, + // ]; + if (record.leaveType == "RETIRE") { + _profileSalary = record?.profileSalary.length > 1 + ? record?.profileSalary[1] + : record?.profileSalary.length > 0 + ? record?.profileSalary[0] + : null; + } else { + _profileSalary = record?.profileSalary.length > 0 + ? record?.profileSalary[0] + : null; + } + if (_profileSalary) { + _OrgLeave = [ + _profileSalary.orgChild4 ?? null, + _profileSalary.orgChild3 ?? null, + _profileSalary.orgChild2 ?? null, + _profileSalary.orgChild1 ?? null, + _profileSalary.orgRoot ?? null, + ]; + } else { + _OrgLeave = []; + } } const orgLeave = _OrgLeave.filter((x:any) => x !== undefined && x !== null).join("\n"); const data = { @@ -393,8 +439,8 @@ export class ProfileGovernmentHistoryController extends Controller { ? posMaster == null ? null : `${orgShortName} ${posMaster.posMasterNo}` - : record && record.profileSalary.length > 0 - ? `${record?.profileSalary[0].posNoAbb} ${record?.profileSalary[0].posNo}` + : _profileSalary != null + ? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}` : null, //เลขที่ตำแหน่ง posType: record?.posType == null ? null : record?.posType.posTypeName, //ประเภท posExecutive: diff --git a/src/controllers/ProfileGovernmentEmployeeController.ts b/src/controllers/ProfileGovernmentEmployeeController.ts index 0cfd43e3..ce170d17 100644 --- a/src/controllers/ProfileGovernmentEmployeeController.ts +++ b/src/controllers/ProfileGovernmentEmployeeController.ts @@ -220,6 +220,7 @@ export class ProfileGovernmentEmployeeController extends Controller { let _OrgLeave:any = [] let orgLeave:string = "" let posNoLeave:string = "" + let _profileSalary:any = null; if (record?.isLeave /*&& record?.profileSalary.length > 0*/) { const profileSalary = await this.salaryRepo.find({ select: [ @@ -255,16 +256,38 @@ export class ProfileGovernmentEmployeeController extends Controller { createdAt: "DESC" } }); - _OrgLeave = [ - profileSalary.length > 0 && profileSalary[0].orgChild4 ? profileSalary[0].orgChild4 : null, - profileSalary.length > 0 && profileSalary[0].orgChild3 ? profileSalary[0].orgChild3 : null, - profileSalary.length > 0 && profileSalary[0].orgChild2 ? profileSalary[0].orgChild2 : null, - profileSalary.length > 0 && profileSalary[0].orgChild1 ? profileSalary[0].orgChild1 : null, - profileSalary.length > 0 && profileSalary[0].orgRoot ? profileSalary[0].orgRoot : null, - ]; + // _OrgLeave = [ + // profileSalary.length > 0 && profileSalary[0].orgChild4 ? profileSalary[0].orgChild4 : null, + // profileSalary.length > 0 && profileSalary[0].orgChild3 ? profileSalary[0].orgChild3 : null, + // profileSalary.length > 0 && profileSalary[0].orgChild2 ? profileSalary[0].orgChild2 : null, + // profileSalary.length > 0 && profileSalary[0].orgChild1 ? profileSalary[0].orgChild1 : null, + // profileSalary.length > 0 && profileSalary[0].orgRoot ? profileSalary[0].orgRoot : null, + // ]; + if (record.leaveType == "RETIRE") { + _profileSalary = profileSalary.length > 1 + ? profileSalary[1] + : profileSalary.length > 0 + ? profileSalary[0] + : null; + } else { + _profileSalary = profileSalary.length > 0 + ? profileSalary[0] + : null + } + if (_profileSalary) { + _OrgLeave = [ + _profileSalary.orgChild4 ?? null, + _profileSalary.orgChild3 ?? null, + _profileSalary.orgChild2 ?? null, + _profileSalary.orgChild1 ?? null, + _profileSalary.orgRoot ?? null, + ]; + } else { + _OrgLeave = []; + } orgLeave = _OrgLeave.filter((x:any) => x !== undefined && x !== null).join("\n"); - posNoLeave = profileSalary.length > 0 - ? `${profileSalary[0].posNoAbb} ${profileSalary[0].posNo}` + posNoLeave = _profileSalary != null + ? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}` : "" } const data = { @@ -372,6 +395,7 @@ export class ProfileGovernmentEmployeeController extends Controller { let _OrgLeave:any = [] let orgLeave:string = "" let posNoLeave:string = "" + let _profileSalary:any = null; if (record?.isLeave /*&& record?.profileSalary.length > 0*/) { const profileSalary = await this.salaryRepo.find({ select: [ @@ -407,16 +431,38 @@ export class ProfileGovernmentEmployeeController extends Controller { createdAt: "DESC" } }); - _OrgLeave = [ - profileSalary.length > 0 && profileSalary[0].orgChild4 ? profileSalary[0].orgChild4 : null, - profileSalary.length > 0 && profileSalary[0].orgChild3 ? profileSalary[0].orgChild3 : null, - profileSalary.length > 0 && profileSalary[0].orgChild2 ? profileSalary[0].orgChild2 : null, - profileSalary.length > 0 && profileSalary[0].orgChild1 ? profileSalary[0].orgChild1 : null, - profileSalary.length > 0 && profileSalary[0].orgRoot ? profileSalary[0].orgRoot : null, - ]; + // _OrgLeave = [ + // profileSalary.length > 0 && profileSalary[0].orgChild4 ? profileSalary[0].orgChild4 : null, + // profileSalary.length > 0 && profileSalary[0].orgChild3 ? profileSalary[0].orgChild3 : null, + // profileSalary.length > 0 && profileSalary[0].orgChild2 ? profileSalary[0].orgChild2 : null, + // profileSalary.length > 0 && profileSalary[0].orgChild1 ? profileSalary[0].orgChild1 : null, + // profileSalary.length > 0 && profileSalary[0].orgRoot ? profileSalary[0].orgRoot : null, + // ]; + if (record.leaveType == "RETIRE") { + _profileSalary = profileSalary.length > 1 + ? profileSalary[1] + : profileSalary.length > 0 + ? profileSalary[0] + : null; + } else { + _profileSalary = profileSalary.length > 0 + ? profileSalary[0] + : null; + } + if (_profileSalary) { + _OrgLeave = [ + _profileSalary.orgChild4 ?? null, + _profileSalary.orgChild3 ?? null, + _profileSalary.orgChild2 ?? null, + _profileSalary.orgChild1 ?? null, + _profileSalary.orgRoot ?? null, + ]; + } else { + _OrgLeave = []; + } orgLeave = _OrgLeave.filter((x:any) => x !== undefined && x !== null).join("\n"); - posNoLeave = profileSalary.length > 0 - ? `${profileSalary[0].posNoAbb} ${profileSalary[0].posNo}` + posNoLeave = _profileSalary != null + ? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}` : "" } const data = { diff --git a/src/controllers/ProfileSalaryTempController.ts b/src/controllers/ProfileSalaryTempController.ts index b160f98d..5da8860e 100644 --- a/src/controllers/ProfileSalaryTempController.ts +++ b/src/controllers/ProfileSalaryTempController.ts @@ -878,7 +878,7 @@ export class ProfileSalaryTempController extends Controller { posNo: _data.employeeClass == "TEMP" ? _data.posMasterNoTemp : shortName, employeeClass: _data.employeeClass == null ? null : _data.employeeClass, govAge: Extension.CalculateGovAge(_data.dateAppoint, 0, 0), - age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0), + age: Extension.CalculateAgeStrV2(_data.birthDate, 0, 0, "GET"), dateEmployment: dateEmployment, dateAppoint: _data.dateAppoint, dateStart: _data.dateStart, diff --git a/src/interfaces/extension.ts b/src/interfaces/extension.ts index 26a12465..c81e66c0 100644 --- a/src/interfaces/extension.ts +++ b/src/interfaces/extension.ts @@ -309,11 +309,13 @@ class Extension { return years; } - public static CalculateAgeStrV2(date: Date, plusYear: number = 0, subtractYear: number = 0) { + public static CalculateAgeStrV2(date: Date, plusYear: number = 0, subtractYear: number = 0, method?:string) { if (date == null || date == undefined) return ""; const currentDate = new Date(); - if (date > currentDate) { + if (date > currentDate && method !== "GET") { throw new Error("วันเกิดต้องไม่มากกว่าวันที่ปัจจุบัน"); + }else if(date > currentDate && method === "GET"){ + return "" } let years = currentDate.getFullYear() - date.getFullYear();