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(); }