From 20c5e39ff720c60d17e64343d4ba4ac0c8c451aa Mon Sep 17 00:00:00 2001 From: Kittapath Date: Thu, 7 Mar 2024 17:37:00 +0700 Subject: [PATCH] update_table_salaryProfile_add_isNext --- src/controllers/SalaryPeriodController.ts | 256 ++++++++++++++++-- src/entities/SalaryProfile.ts | 7 + ...2-update_table_salaryProfile_add_isNext.ts | 14 + 3 files changed, 250 insertions(+), 27 deletions(-) create mode 100644 src/migration/1709807736632-update_table_salaryProfile_add_isNext.ts diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index b3a47ac..a4edf1a 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -239,6 +239,7 @@ export class SalaryPeriodController extends Controller { if (!salarys) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบระดับตำแหน่ง"); } + let type = salaryProfile.type; //SalaryRank let salaryRanks: any = null; if (salaryProfile.amount != null) { @@ -246,19 +247,82 @@ export class SalaryPeriodController extends Controller { where: { salaryId: salarys.id, salary: salaryProfile.amount, + isNext: false, }, }); + if (salaryRanks == null) { + salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: MoreThan(salaryProfile.amount), + isNext: false, + }, + order: { salary: "DESC" }, + }); + } + if (salaryProfile.type == "HAFT") { + if (salaryRanks.salaryHalfSpecial != null && salaryRanks.salaryHalfSpecial > 0) { + let _salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: salaryProfile.amount, + isNext: true, + }, + }); + if (_salaryRanks != null) { + salaryRanks = _salaryRanks; + } + } + } else if (salaryProfile.type == "FULL") { + if (salaryRanks.salaryFullSpecial != null && salaryRanks.salaryFullSpecial > 0) { + let _salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: salaryProfile.amount, + isNext: true, + }, + }); + if (_salaryRanks != null) { + salaryRanks = _salaryRanks; + if (salaryRanks.salaryHalfSpecial == null && salaryRanks.salaryHalfSpecial == 0) { + type = "HAFT"; + } + } + } + } else if (salaryProfile.type == "FULLHAFT") { + if (salaryRanks.salaryFullHalfSpecial != null && salaryRanks.salaryFullHalfSpecial > 0) { + let _salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: salaryProfile.amount, + isNext: true, + }, + }); + if (_salaryRanks != null) { + salaryRanks = _salaryRanks; + if (salaryRanks.salaryFullSpecial == null && salaryRanks.salaryFullSpecial == 0) { + type = "HAFT"; + } else if ( + salaryRanks.salaryHalfSpecial == null && + salaryRanks.salaryHalfSpecial == 0 + ) { + type = "FULL"; + } + } + } + } } - if (salaryProfile.type == "NONE") { + salaryProfile.isNext = false; + if (type == "NONE") { salaryProfile.amountSpecial = 0; salaryProfile.amountUse = 0; salaryProfile.positionSalaryAmount = salaryProfile.amount == null ? 0 : salaryProfile.amount; - } else if (salaryProfile.type == "PENDING") { + } else if (type == "PENDING") { salaryProfile.amountSpecial = 0; salaryProfile.amountUse = 0; salaryProfile.positionSalaryAmount = 0; - } else if (salaryProfile.type == "HAFT") { + } else if (type == "HAFT") { salaryProfile.amountSpecial = salaryRanks == null || salaryRanks.salaryHalfSpecial == null ? 0 @@ -272,7 +336,8 @@ export class SalaryPeriodController extends Controller { : salaryRanks.salaryHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null || salaryRanks.salaryHalf == null ? 0 : salaryRanks.salaryHalf; - } else if (salaryProfile.type == "FULL") { + salaryProfile.isNext = salaryRanks == null ? 0 : salaryRanks.isNext; + } else if (type == "FULL") { salaryProfile.amountSpecial = salaryRanks == null || salaryRanks.salaryFullSpecial == null ? 0 @@ -286,7 +351,8 @@ export class SalaryPeriodController extends Controller { : salaryRanks.salaryFull - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null || salaryRanks.salaryFull == null ? 0 : salaryRanks.salaryFull; - } else if (salaryProfile.type == "FULLHAFT") { + salaryProfile.isNext = salaryRanks == null ? 0 : salaryRanks.isNext; + } else if (type == "FULLHAFT") { salaryProfile.amountSpecial = salaryRanks == null || salaryRanks.salaryFullHalfSpecial == null ? 0 @@ -300,6 +366,7 @@ export class SalaryPeriodController extends Controller { : salaryRanks.salaryFullHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null || salaryRanks.salaryFullHalf == null ? 0 : salaryRanks.salaryFullHalf; + salaryProfile.isNext = salaryRanks == null ? 0 : salaryRanks.isNext; } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทการเลื่อนขึ้นเงินเดือนไม่ถูกต้อง"); } @@ -479,6 +546,9 @@ export class SalaryPeriodController extends Controller { if (!salarys) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบระดับตำแหน่ง"); } + salaryProfile.type = body.type; + salaryProfile.isReserve = body.isReserve; + let type = salaryProfile.type; //SalaryRank let salaryRanks: any = null; if (salaryProfile.amount != null) { @@ -486,18 +556,82 @@ export class SalaryPeriodController extends Controller { where: { salaryId: salarys.id, salary: salaryProfile.amount, + isNext: false, }, }); + if (salaryRanks == null) { + salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: MoreThan(salaryProfile.amount), + isNext: false, + }, + order: { salary: "DESC" }, + }); + } + if (salaryProfile.type == "HAFT") { + if (salaryRanks.salaryHalfSpecial != null && salaryRanks.salaryHalfSpecial > 0) { + let _salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: salaryProfile.amount, + isNext: true, + }, + }); + if (_salaryRanks != null) { + salaryRanks = _salaryRanks; + } + } + } else if (salaryProfile.type == "FULL") { + if (salaryRanks.salaryFullSpecial != null && salaryRanks.salaryFullSpecial > 0) { + let _salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: salaryProfile.amount, + isNext: true, + }, + }); + if (_salaryRanks != null) { + salaryRanks = _salaryRanks; + if (salaryRanks.salaryHalfSpecial == null && salaryRanks.salaryHalfSpecial == 0) { + type = "HAFT"; + } + } + } + } else if (salaryProfile.type == "FULLHAFT") { + if (salaryRanks.salaryFullHalfSpecial != null && salaryRanks.salaryFullHalfSpecial > 0) { + let _salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: salaryProfile.amount, + isNext: true, + }, + }); + if (_salaryRanks != null) { + salaryRanks = _salaryRanks; + if (salaryRanks.salaryFullSpecial == null && salaryRanks.salaryFullSpecial == 0) { + type = "HAFT"; + } else if ( + salaryRanks.salaryHalfSpecial == null && + salaryRanks.salaryHalfSpecial == 0 + ) { + type = "FULL"; + } + } + } + } } - if (body.type == "NONE") { + + salaryProfile.isNext = false; + if (type == "NONE") { salaryProfile.amountSpecial = 0; salaryProfile.amountUse = 0; salaryProfile.positionSalaryAmount = salaryProfile.amount == null ? 0 : salaryProfile.amount; - } else if (body.type == "PENDING") { + } else if (type == "PENDING") { salaryProfile.amountSpecial = 0; salaryProfile.amountUse = 0; salaryProfile.positionSalaryAmount = 0; - } else if (body.type == "HAFT") { + } else if (type == "HAFT") { salaryProfile.amountSpecial = salaryRanks == null || salaryRanks.salaryHalfSpecial == null ? 0 @@ -511,7 +645,8 @@ export class SalaryPeriodController extends Controller { : salaryRanks.salaryHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null || salaryRanks.salaryHalf == null ? 0 : salaryRanks.salaryHalf; - } else if (body.type == "FULL") { + salaryProfile.isNext = salaryRanks == null ? 0 : salaryRanks.isNext; + } else if (type == "FULL") { salaryProfile.amountSpecial = salaryRanks == null || salaryRanks.salaryFullSpecial == null ? 0 @@ -525,7 +660,8 @@ export class SalaryPeriodController extends Controller { : salaryRanks.salaryFull - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null || salaryRanks.salaryFull == null ? 0 : salaryRanks.salaryFull; - } else if (body.type == "FULLHAFT") { + salaryProfile.isNext = salaryRanks == null ? 0 : salaryRanks.isNext; + } else if (type == "FULLHAFT") { salaryProfile.amountSpecial = salaryRanks == null || salaryRanks.salaryFullHalfSpecial == null ? 0 @@ -539,11 +675,10 @@ export class SalaryPeriodController extends Controller { : salaryRanks.salaryFullHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null || salaryRanks.salaryFullHalf == null ? 0 : salaryRanks.salaryFullHalf; + salaryProfile.isNext = salaryRanks == null ? 0 : salaryRanks.isNext; } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทการเลื่อนขึ้นเงินเดือนไม่ถูกต้อง"); } - salaryProfile.type = body.type; - salaryProfile.isReserve = body.isReserve; await this.salaryProfileRepository.save(salaryProfile); if (salaryProfile.salaryOrg) { @@ -744,34 +879,35 @@ export class SalaryPeriodController extends Controller { delete salaryProfile.id; salaryProfile.type = salaryProfile.type.toUpperCase(); //Type & Level - const type = await this.posTypeRepository.findOne({ + const posType = await this.posTypeRepository.findOne({ where: { posTypeName: salaryProfile.posType, }, }); - if (!type) { + if (!posType) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทตำแหน่ง"); } - const level = await this.posLevelRepository.findOne({ + const posLevel = await this.posLevelRepository.findOne({ where: { - posTypeId: type.id, + posTypeId: posType.id, posLevelName: salaryProfile.posLevel, }, }); - if (!level) { + if (!posLevel) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบระดับตำแหน่ง"); } //Salary const salarys = await this.salaryRepository.findOne({ where: { - posTypeId: level.posTypeId, - posLevelId: level.id, + posTypeId: posLevel.posTypeId, + posLevelId: posLevel.id, isActive: true, }, }); if (!salarys) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบระดับตำแหน่ง"); } + let type = salaryProfile.type; //SalaryRank let salaryRanks: any = null; if (salaryProfile.amount != null) { @@ -779,19 +915,82 @@ export class SalaryPeriodController extends Controller { where: { salaryId: salarys.id, salary: salaryProfile.amount, + isNext: false, }, }); + if (salaryRanks == null) { + salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: MoreThan(salaryProfile.amount), + isNext: false, + }, + order: { salary: "DESC" }, + }); + } + if (salaryProfile.type == "HAFT") { + if (salaryRanks.salaryHalfSpecial != null && salaryRanks.salaryHalfSpecial > 0) { + let _salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: salaryProfile.amount, + isNext: true, + }, + }); + if (_salaryRanks != null) { + salaryRanks = _salaryRanks; + } + } + } else if (salaryProfile.type == "FULL") { + if (salaryRanks.salaryFullSpecial != null && salaryRanks.salaryFullSpecial > 0) { + let _salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: salaryProfile.amount, + isNext: true, + }, + }); + if (_salaryRanks != null) { + salaryRanks = _salaryRanks; + if (salaryRanks.salaryHalfSpecial == null && salaryRanks.salaryHalfSpecial == 0) { + type = "HAFT"; + } + } + } + } else if (salaryProfile.type == "FULLHAFT") { + if (salaryRanks.salaryFullHalfSpecial != null && salaryRanks.salaryFullHalfSpecial > 0) { + let _salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: salaryProfile.amount, + isNext: true, + }, + }); + if (_salaryRanks != null) { + salaryRanks = _salaryRanks; + if (salaryRanks.salaryFullSpecial == null && salaryRanks.salaryFullSpecial == 0) { + type = "HAFT"; + } else if ( + salaryRanks.salaryHalfSpecial == null && + salaryRanks.salaryHalfSpecial == 0 + ) { + type = "FULL"; + } + } + } + } } - if (salaryProfile.type == "NONE") { + salaryProfile.isNext = false; + if (type == "NONE") { salaryProfile.amountSpecial = 0; salaryProfile.amountUse = 0; salaryProfile.positionSalaryAmount = salaryProfile.amount == null ? 0 : salaryProfile.amount; - } else if (salaryProfile.type == "PENDING") { + } else if (type == "PENDING") { salaryProfile.amountSpecial = 0; salaryProfile.amountUse = 0; salaryProfile.positionSalaryAmount = 0; - } else if (salaryProfile.type == "HAFT") { + } else if (type == "HAFT") { salaryProfile.amountSpecial = salaryRanks == null || salaryRanks.salaryHalfSpecial == null ? 0 @@ -805,7 +1004,8 @@ export class SalaryPeriodController extends Controller { : salaryRanks.salaryHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null || salaryRanks.salaryHalf == null ? 0 : salaryRanks.salaryHalf; - } else if (salaryProfile.type == "FULL") { + salaryProfile.isNext = salaryRanks == null ? 0 : salaryRanks.isNext; + } else if (type == "FULL") { salaryProfile.amountSpecial = salaryRanks == null || salaryRanks.salaryFullSpecial == null ? 0 @@ -819,7 +1019,8 @@ export class SalaryPeriodController extends Controller { : salaryRanks.salaryFull - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null || salaryRanks.salaryFull == null ? 0 : salaryRanks.salaryFull; - } else if (salaryProfile.type == "FULLHAFT") { + salaryProfile.isNext = salaryRanks == null ? 0 : salaryRanks.isNext; + } else if (type == "FULLHAFT") { salaryProfile.amountSpecial = salaryRanks == null || salaryRanks.salaryFullHalfSpecial == null ? 0 @@ -833,10 +1034,9 @@ export class SalaryPeriodController extends Controller { : salaryRanks.salaryFullHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null || salaryRanks.salaryFullHalf == null ? 0 : salaryRanks.salaryFullHalf; + salaryProfile.isNext = salaryRanks == null ? 0 : salaryRanks.isNext; } else { - salaryProfile.amountSpecial = 0; - salaryProfile.amountUse = 0; - salaryProfile.positionSalaryAmount = 0; + throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทการเลื่อนขึ้นเงินเดือนไม่ถูกต้อง"); } salaryProfile.salaryOrgId = salaryOrg.id; @@ -1210,6 +1410,7 @@ export class SalaryPeriodController extends Controller { salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse; salaryProfileNew.positionSalaryAmount = salaryOld == null ? 0 : salaryOld.positionSalaryAmount; + salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext; } await this.salaryProfileRepository.save(salaryProfileNew); } @@ -1492,6 +1693,7 @@ export class SalaryPeriodController extends Controller { salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse; salaryProfileNew.positionSalaryAmount = salaryOld == null ? 0 : salaryOld.positionSalaryAmount; + salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext; } await this.salaryProfileRepository.save(salaryProfileNew); } diff --git a/src/entities/SalaryProfile.ts b/src/entities/SalaryProfile.ts index 75f23c4..a7b74f2 100644 --- a/src/entities/SalaryProfile.ts +++ b/src/entities/SalaryProfile.ts @@ -287,6 +287,13 @@ export class SalaryProfile extends EntityBase { }) isReserve: boolean; + @Column({ + nullable: true, + comment: "ทะลุขั้น", + default: false, + }) + isNext: boolean; + @ManyToOne(() => SalaryOrg, (salaryOrg) => salaryOrg.salaryProfiles) @JoinColumn({ name: "salaryOrgId" }) salaryOrg: SalaryOrg; diff --git a/src/migration/1709807736632-update_table_salaryProfile_add_isNext.ts b/src/migration/1709807736632-update_table_salaryProfile_add_isNext.ts new file mode 100644 index 0000000..9cf5a33 --- /dev/null +++ b/src/migration/1709807736632-update_table_salaryProfile_add_isNext.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableSalaryProfileAddIsNext1709807736632 implements MigrationInterface { + name = 'UpdateTableSalaryProfileAddIsNext1709807736632' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`isNext\` tinyint NULL COMMENT 'ทะลุขั้น' DEFAULT 0`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`isNext\``); + } + +}