From f0e6ca6a56af14c9c51e1671f76a65da74580201 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Wed, 28 Feb 2024 10:34:55 +0700 Subject: [PATCH] =?UTF-8?q?snap=20salary(=E0=B8=A2=E0=B8=B1=E0=B8=87?= =?UTF-8?q?=E0=B9=84=E0=B8=A1=E0=B9=88=E0=B9=80=E0=B8=AA=E0=B8=A3=E0=B9=87?= =?UTF-8?q?=E0=B8=88)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/SalaryPeriodController.ts | 490 +++++++++++++----- src/entities/SalaryOrg.ts | 16 +- src/entities/SalaryPeriod.ts | 7 + src/entities/SalaryProfile.ts | 22 +- ...pdate_table_salaryProfile_add_isresult1.ts | 30 ++ ...update_table_salaryProfile_add_revision.ts | 20 + ...pdate_table_salaryProfile_add_revision2.ts | 16 + 7 files changed, 476 insertions(+), 125 deletions(-) create mode 100644 src/migration/1709033553523-update_table_salaryProfile_add_isresult1.ts create mode 100644 src/migration/1709080437599-update_table_salaryProfile_add_revision.ts create mode 100644 src/migration/1709090519266-update_table_salaryProfile_add_revision2.ts diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index f39afd8..d59ed83 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -15,7 +15,7 @@ import { Query, } from "tsoa"; import { AppDataSource } from "../database/data-source"; -import { DeepPartial, In, IsNull, Not, Between, MoreThan } from "typeorm"; +import { DeepPartial, In, IsNull, Not, Between, MoreThan, Like, Brackets } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; @@ -46,14 +46,13 @@ export class SalaryPeriodController extends Controller { * @summary SLR_030 - รอบล่าสุด #29 * */ - @Get("latest/{id}") - async GetGroupSalaryPeriodLatest(@Path() id: string) { - //xxxx รอบเงินเดือนล่าสุดยังไม่แน่ใจเอาจากรอบไหน - //xxxx หาสังกัดคนนั้น - // const dateNow = new Date(); + @Post("latest") + async GetGroupSalaryPeriodLatest( + @Body() body: { rootId: string; salaryPeriodId: string; snapshot: string }, + ) { const salaryPeriod = await this.salaryPeriodRepository.findOne({ where: { - // effectiveDate: MoreThan(dateNow), + id: body.salaryPeriodId, isActive: true, }, order: { effectiveDate: "DESC" }, @@ -65,13 +64,33 @@ export class SalaryPeriodController extends Controller { const data = { group1id: - salaryPeriod.salaryOrgs.find((x) => x.group == "GROUP1" && x.rootId == id) == null + salaryPeriod.salaryOrgs.find( + (x) => + x.group == "GROUP1" && + x.rootId == body.rootId && + x.snapshot == body.snapshot.toLocaleUpperCase(), + ) == null ? null - : salaryPeriod.salaryOrgs.find((x) => x.group == "GROUP1" && x.rootId == id)?.id, + : salaryPeriod.salaryOrgs.find( + (x) => + x.group == "GROUP1" && + x.rootId == body.rootId && + x.snapshot == body.snapshot.toLocaleUpperCase(), + )?.id, group2id: - salaryPeriod.salaryOrgs.find((x) => x.group == "GROUP2" && x.rootId == id) == null + salaryPeriod.salaryOrgs.find( + (x) => + x.group == "GROUP2" && + x.rootId == body.rootId && + x.snapshot == body.snapshot.toLocaleUpperCase(), + ) == null ? null - : salaryPeriod.salaryOrgs.find((x) => x.group == "GROUP2" && x.rootId == id)?.id, + : salaryPeriod.salaryOrgs.find( + (x) => + x.group == "GROUP2" && + x.rootId == body.rootId && + x.snapshot == body.snapshot.toLocaleUpperCase(), + )?.id, effectiveDate: salaryPeriod.effectiveDate, period: salaryPeriod.period, }; @@ -164,36 +183,62 @@ export class SalaryPeriodController extends Controller { } //Salary const salarys = await this.salaryRepository.findOne({ - where: { - posTypeId : Level.posTypeId, - posLevelId : Level.id, - isActive : true - } + where: { + posTypeId: Level.posTypeId, + posLevelId: Level.id, + isActive: true, + }, }); - //SalaryRank - const SalaryRanks = await this.salaryRankRepository.findOne({ - where: { - salaryId: salarys?.id, - salary: salaryProfile.amount - } - }); - if (!SalaryRanks) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบผังเงินเดือน"); + if (!salarys) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบระดับตำแหน่ง"); + } + //SalaryRank + let salaryRanks: any = null; + if (salaryProfile.amount != null) { + salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: salaryProfile.amount, + }, + }); } - if (salaryProfile.type == "NONE") { + if (salaryProfile.type == "NONE") { + salaryProfile.amountSpecial = 0; + salaryProfile.amountUse = 0; + salaryProfile.positionSalaryAmount = salaryProfile.amount; } else if (salaryProfile.type == "HAFT") { - salaryProfile.amountSpecial = Number(SalaryRanks?.salaryHalfSpecial); - salaryProfile.amountUse = Number(SalaryRanks?.salaryHalf) - salaryProfile.amount; - salaryProfile.positionSalaryAmount = Number(SalaryRanks?.salaryHalf); + salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryHalfSpecial; + salaryProfile.amountUse = + salaryRanks == null || + salaryProfile == null || + salaryRanks.salaryHalf == null || + salaryProfile.amount == null + ? 0 + : salaryRanks.salaryHalf - salaryProfile.amount; + salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryHalf; } else if (salaryProfile.type == "FULL") { - salaryProfile.amountSpecial = Number(SalaryRanks?.salaryFullSpecial); - salaryProfile.amountUse = Number(SalaryRanks?.salaryFull) - salaryProfile.amount; - salaryProfile.positionSalaryAmount = Number(SalaryRanks?.salaryFull); + salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullSpecial; + salaryProfile.amountUse = + salaryRanks == null || + salaryProfile == null || + salaryRanks.salaryFull == null || + salaryProfile.amount == null + ? 0 + : salaryRanks.salaryFull - salaryProfile.amount; + salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFull; } else if (salaryProfile.type == "FULLHAFT") { - salaryProfile.amountSpecial = Number(SalaryRanks?.salaryFullHalfSpecial); - salaryProfile.amountUse = Number(SalaryRanks?.salaryFullHalf) - salaryProfile.amount; - salaryProfile.positionSalaryAmount = Number(SalaryRanks?.salaryFullHalf); + salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullHalfSpecial; + salaryProfile.amountUse = + salaryRanks == null || + salaryProfile == null || + salaryRanks.salaryFullHalf == null || + salaryProfile.amount == null + ? 0 + : salaryRanks.salaryFullHalf - salaryProfile.amount; + salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFullHalf; + } else { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทการเลื่อนขึ้นเงินเดือนไม่ถูกต้อง"); } await this.salaryProfileRepository.save(salaryProfile); @@ -264,34 +309,59 @@ export class SalaryPeriodController extends Controller { } //Salary const salarys = await this.salaryRepository.findOne({ - where: { - posTypeId : Level.posTypeId, - posLevelId : Level.id, - isActive : true - } + where: { + posTypeId: Level.posTypeId, + posLevelId: Level.id, + isActive: true, + }, }); + if (!salarys) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบระดับตำแหน่ง"); + } //SalaryRank - const SalaryRanks = await this.salaryRankRepository.findOne({ - where: { - salaryId: salarys?.id, - salary: salaryProfile.amount - } - }); - + let salaryRanks: any = null; + if (salaryProfile.amount != null) { + salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: salaryProfile.amount, + }, + }); + } if (body.type == "NONE") { - + salaryProfile.amountSpecial = 0; + salaryProfile.amountUse = 0; + salaryProfile.positionSalaryAmount = salaryProfile.amount; } else if (body.type == "HAFT") { - salaryProfile.amountSpecial = Number(SalaryRanks?.salaryHalfSpecial); - salaryProfile.amountUse = Number(SalaryRanks?.salaryHalf) - salaryProfile.amount; - salaryProfile.positionSalaryAmount = Number(SalaryRanks?.salaryHalf); + salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryHalfSpecial; + salaryProfile.amountUse = + salaryRanks == null || + salaryProfile == null || + salaryRanks.salaryHalf == null || + salaryProfile.amount == null + ? 0 + : salaryRanks.salaryHalf - salaryProfile.amount; + salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryHalf; } else if (body.type == "FULL") { - salaryProfile.amountSpecial = Number(SalaryRanks?.salaryFullSpecial); - salaryProfile.amountUse = Number(SalaryRanks?.salaryFull) - salaryProfile.amount; - salaryProfile.positionSalaryAmount = Number(SalaryRanks?.salaryFull); + salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullSpecial; + salaryProfile.amountUse = + salaryRanks == null || + salaryProfile == null || + salaryRanks.salaryFull == null || + salaryProfile.amount == null + ? 0 + : salaryRanks.salaryFull - salaryProfile.amount; + salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFull; } else if (body.type == "FULLHAFT") { - salaryProfile.amountSpecial = Number(SalaryRanks?.salaryFullHalfSpecial); - salaryProfile.amountUse = Number(SalaryRanks?.salaryFullHalf) - salaryProfile.amount; - salaryProfile.positionSalaryAmount = Number(SalaryRanks?.salaryFullHalf); + salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullHalfSpecial; + salaryProfile.amountUse = + salaryRanks == null || + salaryProfile == null || + salaryRanks.salaryFullHalf == null || + salaryProfile.amount == null + ? 0 + : salaryRanks.salaryFullHalf - salaryProfile.amount; + salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFullHalf; } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทการเลื่อนขึ้นเงินเดือนไม่ถูกต้อง"); } @@ -319,32 +389,44 @@ export class SalaryPeriodController extends Controller { if (!salaryOrg) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบรอบการขึ้นเงินเดือน"); } - const [salaryProfile, total] = await AppDataSource.getRepository(SalaryProfile) .createQueryBuilder("profile") - .andWhere({ - salaryOrgId: salaryOrg.id, - }) - .andWhere(body.type != null && body.type != "" ? "profile.type LIKE :type" : "1=1", { - type: `%${body.type}%`, - }) - //xxxx รอ fe ว่าแสดงค่าไหนบ่างใหเfilterเฉพาะค่านั้น - .orWhere("profile.posMasterNoPrefix LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("profile.posMasterNo LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("profile.posMasterNoSuffix LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("profile.orgShortName LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("profile.prefix LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("profile.firstName LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("profile.lastName LIKE :keyword", { keyword: `%${body.keyword}%` }) - // .orWhere("profile.citizenId LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("profile.position LIKE :keyword", { keyword: `%${body.keyword}%` }) - // .orWhere("profile.posType LIKE :keyword", { keyword: `%${body.keyword}%` }) - // .orWhere("profile.posLevel LIKE :keyword", { keyword: `%${body.keyword}%` }) - // .orWhere("profile.posExecutive LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("profile.amount LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("profile.amountSpecial LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("profile.amountUse LIKE :keyword", { keyword: `%${body.keyword}%` }) - .orWhere("profile.positionSalaryAmount LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere( + new Brackets((qb) => { + qb.andWhere(body.type != null && body.type != "" ? "profile.type LIKE :type" : "1=1", { + type: `%${body.type.toUpperCase()}%`, + }) + .andWhere({ + salaryOrgId: salaryOrg.id, + }) + .andWhere( + new Brackets((qb) => { + qb.orWhere("profile.posMasterNoPrefix LIKE :keyword", { + keyword: `%${body.keyword}%`, + }) + .orWhere("profile.posMasterNo LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("profile.posMasterNoSuffix LIKE :keyword", { + keyword: `%${body.keyword}%`, + }) + .orWhere("profile.orgShortName LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("profile.prefix LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("profile.firstName LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("profile.lastName LIKE :keyword", { keyword: `%${body.keyword}%` }) + // .orWhere("profile.citizenId LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("profile.position LIKE :keyword", { keyword: `%${body.keyword}%` }) + // .orWhere("profile.posType LIKE :keyword", { keyword: `%${body.keyword}%` }) + // .orWhere("profile.posLevel LIKE :keyword", { keyword: `%${body.keyword}%` }) + // .orWhere("profile.posExecutive LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("profile.amount LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("profile.amountSpecial LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("profile.amountUse LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("profile.positionSalaryAmount LIKE :keyword", { + keyword: `%${body.keyword}%`, + }); + }), + ); + }), + ) .orderBy("profile.citizenId", "ASC") .skip((body.page - 1) * body.pageSize) .take(body.pageSize) @@ -399,52 +481,79 @@ export class SalaryPeriodController extends Controller { const salaryProfile = Object.assign(new SalaryProfile(), requestBody); salaryProfile.type = salaryProfile.type.toUpperCase(); //Type & Level - const Type = await this.posTypeRepository.findOne({ + const type = await this.posTypeRepository.findOne({ where: { posTypeName: salaryProfile.posType, }, }); - if (!Type) { + if (!type) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทตำแหน่ง"); } - const Level = await this.posLevelRepository.findOne({ + const level = await this.posLevelRepository.findOne({ where: { - posTypeId: Type.id, + posTypeId: type.id, posLevelName: salaryProfile.posLevel, }, }); - if (!Level) { + if (!level) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบระดับตำแหน่ง"); } //Salary const salarys = await this.salaryRepository.findOne({ - where: { - posTypeId : Level.posTypeId, - posLevelId : Level.id, - isActive : true - } + where: { + posTypeId: level.posTypeId, + posLevelId: level.id, + isActive: true, + }, }); + if (!salarys) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบระดับตำแหน่ง"); + } //SalaryRank - const SalaryRanks = await this.salaryRankRepository.findOne({ - where: { - salaryId: salarys?.id, - salary: salaryProfile.amount - } - }); + let salaryRanks: any = null; + if (salaryProfile.amount != null) { + salaryRanks = await this.salaryRankRepository.findOne({ + where: { + salaryId: salarys.id, + salary: salaryProfile.amount, + }, + }); + } if (salaryProfile.type == "NONE") { + salaryProfile.amountSpecial = 0; + salaryProfile.amountUse = 0; + salaryProfile.positionSalaryAmount = salaryProfile.amount; } else if (salaryProfile.type == "HAFT") { - salaryProfile.amountSpecial = Number(SalaryRanks?.salaryHalfSpecial); - salaryProfile.amountUse = Number(SalaryRanks?.salaryHalf) - salaryProfile.amount; - salaryProfile.positionSalaryAmount = Number(SalaryRanks?.salaryHalf); + salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryHalfSpecial; + salaryProfile.amountUse = + salaryRanks == null || + salaryProfile == null || + salaryRanks.salaryHalf == null || + salaryProfile.amount == null + ? 0 + : salaryRanks.salaryHalf - salaryProfile.amount; + salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryHalf; } else if (salaryProfile.type == "FULL") { - salaryProfile.amountSpecial = Number(SalaryRanks?.salaryFullSpecial); - salaryProfile.amountUse = Number(SalaryRanks?.salaryFull) - salaryProfile.amount; - salaryProfile.positionSalaryAmount = Number(SalaryRanks?.salaryFull); + salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullSpecial; + salaryProfile.amountUse = + salaryRanks == null || + salaryProfile == null || + salaryRanks.salaryFull == null || + salaryProfile.amount == null + ? 0 + : salaryRanks.salaryFull - salaryProfile.amount; + salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFull; } else if (salaryProfile.type == "FULLHAFT") { - salaryProfile.amountSpecial = Number(SalaryRanks?.salaryFullHalfSpecial); - salaryProfile.amountUse = Number(SalaryRanks?.salaryFullHalf) - salaryProfile.amount; - salaryProfile.positionSalaryAmount = Number(SalaryRanks?.salaryFullHalf); + salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullHalfSpecial; + salaryProfile.amountUse = + salaryRanks == null || + salaryProfile == null || + salaryRanks.salaryFullHalf == null || + salaryProfile.amount == null + ? 0 + : salaryRanks.salaryFullHalf - salaryProfile.amount; + salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFullHalf; } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทการเลื่อนขึ้นเงินเดือนไม่ถูกต้อง"); } @@ -563,9 +672,9 @@ export class SalaryPeriodController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี"); } const SalaryOrg = await this.salaryOrgRepository.findOne({ - where: { salaryPeriodId: SalaryPeriod.id } - }) - if(SalaryOrg){ + where: { salaryPeriodId: SalaryPeriod.id }, + }); + if (SalaryOrg) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบได้"); } await this.salaryPeriodRepository.remove(SalaryPeriod); @@ -583,7 +692,16 @@ export class SalaryPeriodController extends Controller { async GetSalaryPeriod_ById(@Path() id: string) { const salaryPeriod = await this.salaryPeriodRepository.findOne({ where: { id: id }, - select: ["id", "period", "isActive", "effectiveDate", "isActive", "status", "year"], + select: [ + "id", + "period", + "isActive", + "effectiveDate", + "isActive", + "status", + "year", + "revisionId", + ], }); if (!salaryPeriod) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี"); @@ -605,24 +723,152 @@ export class SalaryPeriodController extends Controller { @Query("year") year: number = 2024, ) { const [salaryPeriod, total] = await AppDataSource.getRepository(SalaryPeriod) - .createQueryBuilder("SalaryPeriod") - .andWhere(year != 0 ? "SalaryPeriod.year LIKE :year" : "1=1", { year: `%${year}%` }) - .orWhere("SalaryPeriod.period LIKE :keyword", { keyword: `%${keyword}%` }) - .orWhere("SalaryPeriod.isActive LIKE :keyword", { keyword: `%${keyword}%` }) - .orWhere("SalaryPeriod.year LIKE :keyword", { keyword: `%${year}%` }) + .createQueryBuilder("salaryPeriod") + .andWhere(year != 0 ? "salaryPeriod.year LIKE :year" : "1=1", { year: `%${year}%` }) + .orWhere("salaryPeriod.period LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("salaryPeriod.isActive LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("salaryPeriod.year LIKE :keyword", { keyword: `%${year}%` }) .select([ - "SalaryPeriod.id", - "SalaryPeriod.period", - "SalaryPeriod.isActive", - "SalaryPeriod.effectiveDate", - "SalaryPeriod.status", - "SalaryPeriod.year", + "salaryPeriod.id", + "salaryPeriod.period", + "salaryPeriod.isActive", + "salaryPeriod.effectiveDate", + "salaryPeriod.status", + "salaryPeriod.year", + "salaryPeriod.revisionId", ]) - .orderBy("SalaryPeriod.effectiveDate", "DESC") + .orderBy("salaryPeriod.effectiveDate", "DESC") .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); return new HttpSuccess({ data: salaryPeriod, total }); } + + /** + * API รายการรอบเงินเดือน + * + * @summary SLR_020 - รายการรอบเงินเดือน #20 + * + */ + @Get("snapshot/{snaphot}/{salaryPeriodId}") + async SnapshotSalary( + @Path() snaphot: string, + salaryPeriodId: string, + @Request() request: { user: Record }, + ) { + snaphot = snaphot.toLocaleUpperCase(); + const salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { id: salaryPeriodId }, + }); + if (!salaryPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบรอบการสร้างเงินเดือน"); + } + + const salaryOrg = await this.salaryOrgRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: snaphot }, + }); + const salaryProfile = await this.salaryProfileRepository.find({ + where: { salaryOrgId: In(salaryOrg.map((x) => x.id)) }, + }); + await this.salaryOrgRepository.remove(salaryOrg); + await this.salaryProfileRepository.remove(salaryProfile); + + let orgs: any; + let orgProfiles: any; + let revisionId: any; + + salaryPeriod.revisionId = revisionId; + await this.salaryPeriodRepository.save(salaryPeriod); + + await Promise.all( + orgs.map(async (rootId: string) => { + let salaryOrgNew = Object.assign(new SalaryOrg()); + salaryOrgNew.salaryPeriodId = salaryPeriod.id; + salaryOrgNew.status = "PENDING"; + salaryOrgNew.rootId = rootId; + salaryOrgNew.revisionId = salaryPeriod.revisionId; + salaryOrgNew.snapshot = snaphot; + salaryOrgNew.group = "GROUP1"; + salaryOrgNew.createdUserId = request.user.sub; + salaryOrgNew.createdFullName = request.user.name; + salaryOrgNew.lastUpdateUserId = request.user.sub; + salaryOrgNew.lastUpdateFullName = request.user.name; + await this.salaryOrgRepository.save(salaryOrgNew); + delete salaryOrgNew.id; + salaryOrgNew.group = "GROUP2"; + await this.salaryOrgRepository.save(salaryOrgNew); + }), + ); + + let salaryProfileOld: SalaryProfile[] = []; + if (snaphot == "SNAP2") { + const salaryOrgOld = await this.salaryOrgRepository.findOne({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: snaphot }, + relations: ["salaryProfiles"], + }); + if (salaryOrgOld != null) salaryProfileOld = salaryOrgOld.salaryProfiles; + } + + await Promise.all( + orgProfiles.map(async (profile: any) => { + let group = "GROUP1"; + if ( + (profile.posType == "ทั่วไป" && profile.posLevel == "ทักษะพิเศษ") || + (profile.posType == "วิชาการ" && profile.posLevel == "เชี่ยวชาญ") || + (profile.posType == "วิชาการ" && profile.posLevel == "ทรงคุณวุฒิ") || + (profile.posType == "อำนวยการ" && profile.posLevel == "สูง") || + (profile.posType == "บริหาร" && profile.posLevel == "ต้น") || + (profile.posType == "บริหาร" && profile.posLevel == "สูง") + ) { + group = "GROUP2"; + } + const salaryOrgNew = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriod.id, + rootId: profile.rootId, + snapshot: snaphot, + group: group, + }, + }); + + if (salaryOrgNew != null) { + let salaryProfileNew = Object.assign(new SalaryProfile(), profile); + salaryProfileNew.salaryOrgId = salaryOrgNew.id; + salaryProfileNew.revisionId = salaryPeriod.revisionId; + salaryProfileNew.createdUserId = request.user.sub; + salaryProfileNew.createdFullName = request.user.name; + salaryProfileNew.lastUpdateUserId = request.user.sub; + salaryProfileNew.lastUpdateFullName = request.user.name; + + if (snaphot == "SNAP2") { + const salaryOld = salaryProfileOld.find( + (x) => x.citizenId == salaryProfileNew.citizenId, + ); + salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount; + salaryProfileNew.amountSpecial = salaryOld == null ? 0 : salaryOld.amountSpecial; + salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse; + salaryProfileNew.positionSalaryAmount = + salaryOld == null ? 0 : salaryOld.positionSalaryAmount; + } + await this.salaryProfileRepository.save(salaryProfileNew); + } + }), + ); + + const salaryOrgNew = await this.salaryOrgRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: snaphot }, + relations: ["salaryProfiles"], + }); + await Promise.all( + salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => { + _salaryOrg.total = _salaryOrg.salaryProfiles.length; + _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); + _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; + await this.salaryOrgRepository.save(_salaryOrg); + }), + ); + + return new HttpSuccess(); + } } diff --git a/src/entities/SalaryOrg.ts b/src/entities/SalaryOrg.ts index c114a62..4ebad1d 100644 --- a/src/entities/SalaryOrg.ts +++ b/src/entities/SalaryOrg.ts @@ -22,6 +22,20 @@ export class SalaryOrg extends EntityBase { }) rootId: string; + @Column({ + nullable: true, + comment: "id revision", + length: 40, + }) + revisionId: string; + + @Column({ + comment: "รอบการ Snapshot", + length: 20, + default: "SNAP1", + }) + snapshot: string; + @Column({ comment: "จำนวนคนทั้งหมด", }) @@ -37,7 +51,7 @@ export class SalaryOrg extends EntityBase { type: "double", default: 0, }) - fifteenPoint: Double; + fifteenPoint: number; @Column({ comment: "กลุ่ม GROUP1->กลุ่ม1 GROUP2->กลุ่ม2", diff --git a/src/entities/SalaryPeriod.ts b/src/entities/SalaryPeriod.ts index a35104b..d88a18d 100644 --- a/src/entities/SalaryPeriod.ts +++ b/src/entities/SalaryPeriod.ts @@ -36,6 +36,13 @@ export class SalaryPeriod extends EntityBase { }) year: number; + @Column({ + nullable: true, + comment: "id revision", + length: 40, + }) + revisionId: string; + @OneToMany(() => SalaryOrg, (salaryOrg) => salaryOrg.salaryPeriod) salaryOrgs: SalaryOrg[]; } diff --git a/src/entities/SalaryProfile.ts b/src/entities/SalaryProfile.ts index 04f3d56..41bfcdb 100644 --- a/src/entities/SalaryProfile.ts +++ b/src/entities/SalaryProfile.ts @@ -107,6 +107,7 @@ export class SalaryProfile extends EntityBase { @Column({ nullable: true, + type: "double", comment: "เงินเดือนฐาน", default: null, }) @@ -114,6 +115,7 @@ export class SalaryProfile extends EntityBase { @Column({ nullable: true, + type: "double", comment: "เงินพิเศษ", default: null, }) @@ -121,17 +123,19 @@ export class SalaryProfile extends EntityBase { @Column({ nullable: true, + type: "double", comment: "จำนวนเงินที่ใช้เลื่อน", default: null, }) - amountUse: number; + amountUse: number | null; @Column({ nullable: true, + type: "double", comment: "เงินเดือนหลังเลื่อน", default: null, }) - positionSalaryAmount: number; + positionSalaryAmount: number | null; @Column({ comment: @@ -141,6 +145,20 @@ export class SalaryProfile extends EntityBase { }) type: string; + @Column({ + comment: "สถานะ", + length: 20, + default: "PENDING", + }) + status: string; + + @Column({ + nullable: true, + comment: "id revision", + length: 40, + }) + revisionId: string; + @Column({ nullable: true, comment: "", diff --git a/src/migration/1709033553523-update_table_salaryProfile_add_isresult1.ts b/src/migration/1709033553523-update_table_salaryProfile_add_isresult1.ts new file mode 100644 index 0000000..a0c9628 --- /dev/null +++ b/src/migration/1709033553523-update_table_salaryProfile_add_isresult1.ts @@ -0,0 +1,30 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableSalaryProfileAddIsresult11709033553523 implements MigrationInterface { + name = 'UpdateTableSalaryProfileAddIsresult11709033553523' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryPeriod\` CHANGE \`year\` \`year\` int NULL COMMENT 'ปีงบประมาณ'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`amount\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`amount\` double NULL COMMENT 'เงินเดือนฐาน'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`amountSpecial\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`amountSpecial\` double NULL COMMENT 'เงินพิเศษ'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`amountUse\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`amountUse\` double NULL COMMENT 'จำนวนเงินที่ใช้เลื่อน'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`positionSalaryAmount\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`positionSalaryAmount\` double NULL COMMENT 'เงินเดือนหลังเลื่อน'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`positionSalaryAmount\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`positionSalaryAmount\` int NULL COMMENT 'เงินเดือนหลังเลื่อน'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`amountUse\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`amountUse\` int NULL COMMENT 'จำนวนเงินที่ใช้เลื่อน'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`amountSpecial\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`amountSpecial\` int NULL COMMENT 'เงินพิเศษ'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`amount\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`amount\` int NULL COMMENT 'เงินเดือนฐาน'`); + await queryRunner.query(`ALTER TABLE \`salaryPeriod\` CHANGE \`year\` \`year\` int NULL COMMENT 'ปีงบหระมาณ'`); + } + +} diff --git a/src/migration/1709080437599-update_table_salaryProfile_add_revision.ts b/src/migration/1709080437599-update_table_salaryProfile_add_revision.ts new file mode 100644 index 0000000..69d7a5f --- /dev/null +++ b/src/migration/1709080437599-update_table_salaryProfile_add_revision.ts @@ -0,0 +1,20 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableSalaryProfileAddRevision1709080437599 implements MigrationInterface { + name = 'UpdateTableSalaryProfileAddRevision1709080437599' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`revisionId\` varchar(40) NULL COMMENT 'id revision'`); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` ADD \`revisionId\` varchar(40) NULL COMMENT 'id revision'`); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` ADD \`snapshot\` varchar(20) NOT NULL COMMENT 'รอบการ Snapshot' DEFAULT 'SHAP1'`); + await queryRunner.query(`ALTER TABLE \`salaryPeriod\` ADD \`revisionId\` varchar(40) NULL COMMENT 'id revision'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryPeriod\` DROP COLUMN \`revisionId\``); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` DROP COLUMN \`snapshot\``); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` DROP COLUMN \`revisionId\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`revisionId\``); + } + +} diff --git a/src/migration/1709090519266-update_table_salaryProfile_add_revision2.ts b/src/migration/1709090519266-update_table_salaryProfile_add_revision2.ts new file mode 100644 index 0000000..9b75387 --- /dev/null +++ b/src/migration/1709090519266-update_table_salaryProfile_add_revision2.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableSalaryProfileAddRevision21709090519266 implements MigrationInterface { + name = 'UpdateTableSalaryProfileAddRevision21709090519266' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`status\` varchar(20) NOT NULL COMMENT 'สถานะ' DEFAULT 'PENDING'`); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` CHANGE \`snapshot\` \`snapshot\` varchar(20) NOT NULL COMMENT 'รอบการ Snapshot' DEFAULT 'SNAP1'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryOrg\` CHANGE \`snapshot\` \`snapshot\` varchar(20) NOT NULL COMMENT 'รอบการ Snapshot' DEFAULT 'SHAP1'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`status\``); + } + +}