From 289b447b331d389d207a472685789736873c5a23 Mon Sep 17 00:00:00 2001 From: kittapath Date: Thu, 17 Oct 2024 22:11:54 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=A3=E0=B8=B2=E0=B8=A2=E0=B8=8A=E0=B8=B7?= =?UTF-8?q?=E0=B9=88=E0=B8=AD=E0=B8=95=E0=B8=B2=E0=B8=A1=E0=B8=81=E0=B8=A5?= =?UTF-8?q?=E0=B8=B8=E0=B9=88=E0=B8=A1=E0=B9=83=E0=B8=99=E0=B9=82=E0=B8=84?= =?UTF-8?q?=E0=B8=A3=E0=B8=87=E0=B8=AA=E0=B8=A3=E0=B9=89=E0=B8=B2=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 68 ++++++++++ src/controllers/OrganizationController.ts | 66 +++++----- src/controllers/PositionController.ts | 123 ++++++++++++++++-- src/entities/Command.ts | 4 + src/entities/PosMaster.ts | 7 + src/entities/ProfileSalary.ts | 13 ++ ...69375946-update_profilesalary_commandId.ts | 16 +++ ...2500950-update_profilesalary_commandId1.ts | 14 ++ 8 files changed, 269 insertions(+), 42 deletions(-) create mode 100644 src/migration/1729169375946-update_profilesalary_commandId.ts create mode 100644 src/migration/1729172500950-update_profilesalary_commandId1.ts diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index be46b076..357afdef 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -2747,6 +2747,62 @@ export class CommandController extends Controller { }[]; }, ) { + await Promise.all( + body.refIds.map(async (item) => { + const profile = await this.profileEmployeeRepository.findOne({ + where: { id: item.refId }, + relations: ["posType", "posLevel"], + }); + if (!profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); + } + + const dest_item = await this.salaryRepo.findOne({ + where: { profileEmployeeId: item.refId }, + order: { order: "DESC" }, + }); + const before = null; + const data = new ProfileSalary(); + + const meta = { + profileId: profile.id, + date: new Date(), + amount: item.amount, + positionSalaryAmount: item.positionSalaryAmount, + mouthSalaryAmount: item.mouthSalaryAmount, + posNo: "", + position: profile.position, + positionType: profile.posType?.posTypeName || null, + positionLevel: profile.posLevel?.posLevelName || null, + refCommandNo: `${item.commandNo}/${Extension.ToThaiYear(item.commandYear)}`, + templateDoc: item.templateDoc, + order: dest_item == null ? 1 : dest_item.order + 1, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(data, meta); + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + + await this.salaryRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); + history.profileSalaryId = data.id; + await this.salaryHistoryRepo.save(history, { data: req }); + }), + ); + const posMasters = await this.posMasterRepository.find({ + where: { id: In(body.refIds.map((x) => x.refId)) }, + }); + const data = posMasters.map((_data) => ({ + ..._data, + statusReport: "PENDING", + })); + await this.posMasterRepository.save(data); return new HttpSuccess(); } @Post("command38/officer/report") @@ -2757,6 +2813,12 @@ export class CommandController extends Controller { refIds: string[]; }, ) { + const posMasters = await this.posMasterRepository.find({ where: { id: In(body.refIds) } }); + const data = posMasters.map((_data) => ({ + ..._data, + statusReport: "REPORT", + })); + await this.posMasterRepository.save(data); return new HttpSuccess(); } @Post("command38/officer/report/delete") @@ -2776,6 +2838,12 @@ export class CommandController extends Controller { }[]; }, ) { + const posMasters = await this.posMasterRepository.find({ where: { id: In(body.refIds) } }); + const data = posMasters.map((_data) => ({ + ..._data, + statusReport: "PENDING", + })); + await this.posMasterRepository.save(data); return new HttpSuccess(); } diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 37339837..22ac4164 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -655,7 +655,7 @@ export class OrganizationController extends Controller { }); }); } - + const _orgRevisions = await this.orgRevisionRepository.find({ where: [{ orgRevisionIsDraft: true, id: Not(revision.id) }], }); @@ -3444,39 +3444,39 @@ export class OrganizationController extends Controller { profile.position = position?.positionName ?? _null; await this.profileRepo.save(profile); } - const profileSalary = await this.salaryRepository.findOne({ - where: { profileId: item.next_holderId }, - order: { createdAt: "DESC" }, - }); + // const profileSalary = await this.salaryRepository.findOne({ + // where: { profileId: item.next_holderId }, + // order: { createdAt: "DESC" }, + // }); - const shortName = - item != null && item.orgChild4 != null - ? `${item.orgChild4.orgChild4ShortName}${item.posMasterNo}` - : item != null && item?.orgChild3 != null - ? `${item.orgChild3.orgChild3ShortName}${item.posMasterNo}` - : item != null && item?.orgChild2 != null - ? `${item.orgChild2.orgChild2ShortName}${item.posMasterNo}` - : item != null && item?.orgChild1 != null - ? `${item.orgChild1.orgChild1ShortName}${item.posMasterNo}` - : item != null && item?.orgRoot != null - ? `${item.orgRoot.orgRootShortName}${item.posMasterNo}` - : null; - await new FunctionMain().newSalaryFunction(request, { - profileId: item.next_holderId, - date: new Date(), - amount: profileSalary?.amount ?? null, - positionSalaryAmount: profileSalary?.positionSalaryAmount ?? null, - mouthSalaryAmount: profileSalary?.mouthSalaryAmount ?? null, - posNo: shortName, - position: position?.positionName ?? _null, - positionLine: position?.positionField ?? _null, - positionPathSide: position?.positionArea ?? _null, - positionExecutive: position?.posExecutive?.posExecutiveName ?? _null, - positionType: position?.posType?.posTypeName ?? _null, - positionLevel: position?.posLevel?.posLevelName ?? _null, - refCommandNo: null, - templateDoc: "ปรับโครงสร้าง", - }); + // const shortName = + // item != null && item.orgChild4 != null + // ? `${item.orgChild4.orgChild4ShortName}${item.posMasterNo}` + // : item != null && item?.orgChild3 != null + // ? `${item.orgChild3.orgChild3ShortName}${item.posMasterNo}` + // : item != null && item?.orgChild2 != null + // ? `${item.orgChild2.orgChild2ShortName}${item.posMasterNo}` + // : item != null && item?.orgChild1 != null + // ? `${item.orgChild1.orgChild1ShortName}${item.posMasterNo}` + // : item != null && item?.orgRoot != null + // ? `${item.orgRoot.orgRootShortName}${item.posMasterNo}` + // : null; + // await new FunctionMain().newSalaryFunction(request, { + // profileId: item.next_holderId, + // date: new Date(), + // amount: profileSalary?.amount ?? null, + // positionSalaryAmount: profileSalary?.positionSalaryAmount ?? null, + // mouthSalaryAmount: profileSalary?.mouthSalaryAmount ?? null, + // posNo: shortName, + // position: position?.positionName ?? _null, + // positionLine: position?.positionField ?? _null, + // positionPathSide: position?.positionArea ?? _null, + // positionExecutive: position?.posExecutive?.posExecutiveName ?? _null, + // positionType: position?.posType?.posTypeName ?? _null, + // positionLevel: position?.posLevel?.posLevelName ?? _null, + // refCommandNo: null, + // templateDoc: "ปรับโครงสร้าง", + // }); } item.current_holderId = item.next_holderId; item.next_holderId = null; diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 74cfbc9f..ea22ef05 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -151,7 +151,7 @@ export class PositionController extends Controller { posDict.lastUpdateFullName = request.user.name; posDict.createdAt = new Date(); posDict.lastUpdatedAt = new Date(); - await this.posDictRepository.save(posDict, {data: request}); + await this.posDictRepository.save(posDict, { data: request }); setLogDataDiff(request, { before, after: posDict }); return new HttpSuccess(posDict.id); } @@ -223,7 +223,7 @@ export class PositionController extends Controller { where: { posMasterId: posMaster.id }, }); if (posMasterAssigns.length > 0) { - await this.posMasterAssignRepo.remove(posMasterAssigns, {data:request}); + await this.posMasterAssignRepo.remove(posMasterAssigns, { data: request }); } // await this.posMasterAssignRepo.delete({ posMasterId: posMaster.id }); return new HttpSuccess(); @@ -307,8 +307,8 @@ export class PositionController extends Controller { posExecutive.lastUpdateFullName = request.user.name; posExecutive.createdAt = new Date(); posExecutive.lastUpdatedAt = new Date(); - await this.posExecutiveRepository.save(posExecutive, {data:request}); - setLogDataDiff(request, {before, after: posExecutive}); + await this.posExecutiveRepository.save(posExecutive, { data: request }); + setLogDataDiff(request, { before, after: posExecutive }); } const rowRepeated = await this.posDictRepository.findOne({ @@ -334,8 +334,8 @@ export class PositionController extends Controller { posDict.lastUpdateFullName = request.user.name; posDict.createdAt = new Date(); posDict.lastUpdatedAt = new Date(); - await this.posDictRepository.save(posDict, {data:request}); - setLogDataDiff(request, {before, after: posDict}); + await this.posDictRepository.save(posDict, { data: request }); + setLogDataDiff(request, { before, after: posDict }); return new HttpSuccess(posDict.id); } @@ -433,8 +433,8 @@ export class PositionController extends Controller { posDict.posDictArea = requestBody.posDictArea ? requestBody.posDictArea : ""; posDict.isSpecial = requestBody.isSpecial; // this.posDictRepository.merge(posDict, requestBody); - await this.posDictRepository.save(posDict, {data:request}); - setLogDataDiff(request, {before, after: posDict}); + await this.posDictRepository.save(posDict, { data: request }); + setLogDataDiff(request, { before, after: posDict }); return new HttpSuccess(); } @@ -452,7 +452,7 @@ export class PositionController extends Controller { if (!delPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งในสายงานนี้"); } - await this.posDictRepository.remove(delPosDict, {data: request}); + await this.posDictRepository.remove(delPosDict, { data: request }); return new HttpSuccess(); } @@ -4053,4 +4053,109 @@ export class PositionController extends Controller { } return new HttpSuccess(); } + + /** + * API รายชื่อตามกลุ่มในโครงสร้าง + * + * @summary รายชื่อตามกลุ่มในโครงสร้าง + * + */ + @Post("report/draft") + async reportDraft( + @Body() + body: { + type: string; + rootId: string; + }, + ) { + let conditionGroup = ""; + if (body.type.trim().toUpperCase() == "GROUP1") { + conditionGroup = + "(posType.posTypeName = 'ทั่วไป' AND posLevel.posLevelName = 'ชำนาญงาน') OR (posType.posTypeName = 'ทั่วไป' AND posLevel.posLevelName = 'ปฏิบัติงาน') OR (posType.posTypeName = 'วิชาการ' AND posLevel.posLevelName = 'ปฏิบัติการ') OR (posType.posTypeName = 'วิชาการ' AND posLevel.posLevelName = 'ชำนาญการ')"; + } else if (body.type.trim().toUpperCase() == "GROUP2") { + conditionGroup = + "(posType.posTypeName = 'ทั่วไป' AND posLevel.posLevelName = 'อาวุโส') OR (posType.posTypeName = 'วิชาการ' AND posLevel.posLevelName = 'ชำนาญการพิเศษ') OR (posType.posTypeName = 'อำนวยการ' AND posLevel.posLevelName = 'ต้น')"; + } else if (body.type.trim().toUpperCase() == "GROUP3") { + conditionGroup = + "(posType.posTypeName = 'ทั่วไป' AND posLevel.posLevelName = 'ทักษะพิเศษ') OR (posType.posTypeName = 'วิชาการ' AND posLevel.posLevelName = 'เชี่ยวชาญ') OR (posType.posTypeName = 'วิชาการ' AND posLevel.posLevelName = 'ทรงคุณวุฒิ') OR (posType.posTypeName = 'อำนวยการ' AND posLevel.posLevelName = 'สูง') OR (posType.posTypeName = 'บริหาร' AND posLevel.posLevelName = 'ต้น') OR (posType.posTypeName = 'บริหาร' AND posLevel.posLevelName = 'สูง')"; + } else { + throw new HttpError(HttpStatusCode.NOT_FOUND, "กลุ่มเป้าหมายไม่ถูกต้อง"); + } + + let posMaster = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .leftJoinAndSelect("posMaster.orgRoot", "orgRoot") + .leftJoinAndSelect("posMaster.orgChild1", "orgChild1") + .leftJoinAndSelect("posMaster.orgChild2", "orgChild2") + .leftJoinAndSelect("posMaster.orgChild3", "orgChild3") + .leftJoinAndSelect("posMaster.orgChild4", "orgChild4") + .leftJoinAndSelect("posMaster.next_holder", "next_holder") + .leftJoinAndSelect("posMaster.positions", "positions") + .leftJoinAndSelect("positions.posType", "posType") + .leftJoinAndSelect("positions.posLevel", "posLevel") + .leftJoinAndSelect("posMaster.orgRevision", "orgRevision") + .andWhere("posMaster.orgRootId = :orgRootId", { orgRootId: body.rootId }) + .andWhere("posMaster.statusReport = :statusReport", { statusReport: "PENDING" }) + .andWhere("posMaster.next_holderId IS NOT NULL") + .andWhere("orgRevision.orgRevisionIsCurrent = :orgRevisionIsCurrent", { + orgRevisionIsCurrent: false, + }) + .andWhere("orgRevision.orgRevisionIsDraft = :orgRevisionIsDraft", { + orgRevisionIsDraft: true, + }) + .andWhere("positions.positionIsSelected = :isSelected", { isSelected: true }) + .andWhere( + new Brackets((qb) => { + qb.andWhere(conditionGroup); + }), + ) + .orderBy("posMaster.posMasterOrder", "ASC") + .select([ + "posMaster.id", + "posMaster.posMasterNo", + "positions.positionName", + "positions.positionIsSelected", + "posType.posTypeName", + "posLevel.posLevelName", + "orgRoot.orgRootShortName", + "orgChild1.orgChild1ShortName", + "orgChild2.orgChild2ShortName", + "orgChild3.orgChild3ShortName", + "orgChild4.orgChild4ShortName", + "next_holder.prefix", + "next_holder.firstName", + "next_holder.lastName", + ]) + .getMany(); + const _posMaster = posMaster.map((x) => { + const posMasterNo = + x.orgChild4 != null + ? `${x.orgChild4.orgChild4ShortName}${x.posMasterNo}` + : x != null && x?.orgChild3 != null + ? `${x.orgChild3.orgChild3ShortName}${x.posMasterNo}` + : x != null && x?.orgChild2 != null + ? `${x.orgChild2.orgChild2ShortName}${x.posMasterNo}` + : x != null && x?.orgChild1 != null + ? `${x.orgChild1.orgChild1ShortName}${x.posMasterNo}` + : x != null && x?.orgRoot != null + ? `${x.orgRoot.orgRootShortName}${x.posMasterNo}` + : null; + const position = + x.positions.filter((x) => x.positionIsSelected == true).length > 0 + ? x.positions.filter((x) => x.positionIsSelected == true)[0] + : null; + return { + id: x.id, + posMasterNo: posMasterNo, + positionName: position?.positionName || null, + posType: position?.posType?.posTypeName || null, + posLevel: position?.posLevel?.posLevelName || null, + prefix: x.next_holder?.prefix || null, + firstName: x.next_holder?.firstName || null, + lastName: x.next_holder?.lastName || null, + }; + }); + + return new HttpSuccess(_posMaster); + } } diff --git a/src/entities/Command.ts b/src/entities/Command.ts index 3254aac7..c33cce38 100644 --- a/src/entities/Command.ts +++ b/src/entities/Command.ts @@ -4,6 +4,7 @@ import { CommandType } from "./CommandType"; import { CommandSend } from "./CommandSend"; import { CommandSalary } from "./CommandSalary"; import { CommandRecive } from "./CommandRecive"; +import { ProfileSalary } from "./ProfileSalary"; @Entity("command") export class Command extends EntityBase { @@ -151,6 +152,9 @@ export class Command extends EntityBase { @OneToMany(() => CommandRecive, (commandRecive) => commandRecive.command) commandRecives: CommandRecive[]; + + @OneToMany(() => ProfileSalary, (profileSalary) => profileSalary.command) + profileSalarys: ProfileSalary[]; } export class CreateCommand { diff --git a/src/entities/PosMaster.ts b/src/entities/PosMaster.ts index 8aea9570..d32fc598 100644 --- a/src/entities/PosMaster.ts +++ b/src/entities/PosMaster.ts @@ -195,6 +195,13 @@ export class PosMaster extends EntityBase { }) authRoleId: string; + @Column({ + comment: "สถานะออกคำสั่ง", + default: "PENDING", + length: 20, + }) + statusReport: string; + @ManyToOne(() => AuthRole, (authRole) => authRole.posMasters) @JoinColumn({ name: "authRoleId" }) authRole: AuthRole; diff --git a/src/entities/ProfileSalary.ts b/src/entities/ProfileSalary.ts index 2103b6cd..f2a56217 100644 --- a/src/entities/ProfileSalary.ts +++ b/src/entities/ProfileSalary.ts @@ -3,6 +3,7 @@ import { EntityBase } from "./base/Base"; import { Profile } from "./Profile"; import { ProfileEmployee } from "./ProfileEmployee"; import { ProfileSalaryHistory } from "./ProfileSalaryHistory"; +import { Command } from "./Command"; @Entity("profileSalary") export class ProfileSalary extends EntityBase { @@ -155,6 +156,18 @@ export class ProfileSalary extends EntityBase { }) isGovernment: boolean; + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง command", + default: null, + }) + commandId: string; + + @ManyToOne(() => Command, (command) => command.profileSalarys) + @JoinColumn({ name: "commandId" }) + command: Command; + @OneToMany(() => ProfileSalaryHistory, (profileSalaryHistory) => profileSalaryHistory.histories) profileSalaryHistories: ProfileSalaryHistory[]; diff --git a/src/migration/1729169375946-update_profilesalary_commandId.ts b/src/migration/1729169375946-update_profilesalary_commandId.ts new file mode 100644 index 00000000..e76240d2 --- /dev/null +++ b/src/migration/1729169375946-update_profilesalary_commandId.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateProfilesalaryCommandId1729169375946 implements MigrationInterface { + name = 'UpdateProfilesalaryCommandId1729169375946' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileSalary\` ADD \`commandId\` varchar(40) NULL COMMENT 'คีย์นอก(FK)ของตาราง command'`); + await queryRunner.query(`ALTER TABLE \`profileSalary\` ADD CONSTRAINT \`FK_5aa33e13619d0c878032e2dc1bd\` FOREIGN KEY (\`commandId\`) REFERENCES \`command\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profileSalary\` DROP FOREIGN KEY \`FK_5aa33e13619d0c878032e2dc1bd\``); + await queryRunner.query(`ALTER TABLE \`profileSalary\` DROP COLUMN \`commandId\``); + } + +} diff --git a/src/migration/1729172500950-update_profilesalary_commandId1.ts b/src/migration/1729172500950-update_profilesalary_commandId1.ts new file mode 100644 index 00000000..5187c1d6 --- /dev/null +++ b/src/migration/1729172500950-update_profilesalary_commandId1.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateProfilesalaryCommandId11729172500950 implements MigrationInterface { + name = 'UpdateProfilesalaryCommandId11729172500950' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`posMaster\` ADD \`statusReport\` varchar(20) NOT NULL COMMENT 'สถานะออกคำสั่ง' DEFAULT 'PENDING'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`posMaster\` DROP COLUMN \`statusReport\``); + } + +}