From a76bdbacbae07210e54ae3ca40bc9a5e1c0c4ff5 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 5 Mar 2024 09:38:05 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=95=E0=B8=A3=E0=B8=A7=E0=B8=88=E0=B8=AA?= =?UTF-8?q?=E0=B8=AD=E0=B8=9A=E0=B8=81=E0=B8=B2=E0=B8=A3=E0=B9=80=E0=B8=A5?= =?UTF-8?q?=E0=B8=B7=E0=B9=88=E0=B8=AD=E0=B8=99=E0=B8=82=E0=B8=B1=E0=B9=89?= =?UTF-8?q?=E0=B8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/SalaryPeriodController.ts | 57 +++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 617a9eb..2faf5fd 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -401,13 +401,39 @@ export class SalaryPeriodController extends Controller { */ @Post("change/type") async changeType(@Body() body: { profileId: string; type: string }) { + const salaryProfile = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg", "salaryOrg.salaryPeriod"], where: { id: body.profileId }, }); if (!salaryProfile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการขอเงินเดือนผู้ใช้งานนี้ในระบบ"); } body.type = body.type.toUpperCase(); + + //ตรวจสอบงวดเมษาว่าเลื่อนกี่ขั้น + if(body.type == "FULLHAFT"){ + if (salaryProfile?.salaryOrg?.salaryPeriod?.period === "OCT") { + const checkPreviousType = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg", "salaryOrg.salaryPeriod"], + where: { + id: body.profileId, + salaryOrg: { + salaryPeriod: { + period: "APR", + year: salaryProfile?.salaryOrg?.salaryPeriod?.year //ปีที่ตรงกันด้วย + }, + snapshot: "SNAP2" + }, + type: "FULL" + }, + }); + if(checkPreviousType){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถเลื่อนขั้นบุคลากรเกิน 2 ขั้นต่อปีได้"); + } + } + } + //Type & Level const Type = await this.posTypeRepository.findOne({ where: { @@ -502,6 +528,37 @@ export class SalaryPeriodController extends Controller { } salaryProfile.type = body.type; await this.salaryProfileRepository.save(salaryProfile); + + if (salaryProfile.salaryOrg) { + // หาจำนวน Quota คงเหลือ + if (salaryProfile.salaryOrg.snapshot == "SNAP1") { + if (salaryProfile.salaryOrg.salaryPeriod.period == "APR") { + const amountFullType = await this.salaryProfileRepository.count({ + where: { + salaryOrgId: salaryProfile?.salaryOrg.id, + type: "FULL", + }, + }); + const calRemainQuota = salaryProfile.salaryOrg.fifteenPercent - amountFullType; + salaryProfile.salaryOrg.quantityUsed = amountFullType; + salaryProfile.salaryOrg.remainQuota = calRemainQuota; + await this.salaryOrgRepository.save(salaryProfile?.salaryOrg); + } else if (salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { + const sumAmountUse = await AppDataSource.getRepository(SalaryProfile) + .createQueryBuilder("salaryProfile") + .select("SUM(salaryProfile.amountUse)", "totalAmount") + .where({ + salaryOrgId: salaryProfile?.salaryOrg.id, + type: "FULL", + }) + .getRawOne(); + const calRemainAmount = salaryProfile.salaryOrg.sixPercentAmount - sumAmountUse.totalAmount; + salaryProfile.salaryOrg.useAmount = sumAmountUse.totalAmount; + salaryProfile.salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryProfile?.salaryOrg); + } + } + } return new HttpSuccess(); }