From a1b0936bf609ea463976cd7400ac5b753ace8cc4 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 1 Mar 2024 14:46:06 +0700 Subject: [PATCH] fix --- src/controllers/SalaryPeriodController.ts | 55 ++++++++++++----------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 96af47e..9785393 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -343,33 +343,34 @@ export class SalaryPeriodController extends Controller { salaryProfile.salaryOrgId = salaryOrg.id; await this.salaryProfileRepository.save(salaryProfile); - - // หาจำนวน Quota คงเหลือ - if (salaryOrg.snapshot == "SNAP1") { - if(salaryOrg.salaryPeriod.period == "APR"){ - const amountFullType = await this.salaryProfileRepository.count({ - where: { - salaryOrgId: salaryOrg.id, - type: "FULL", - }, - }); - const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; - salaryOrg.quantityUsed = amountFullType; - salaryOrg.remainQuota = calRemainQuota; - await this.salaryOrgRepository.save(salaryOrg); - }else if(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 = salaryOrg.sixPercentAmount - sumAmountUse.totalAmount; - salaryOrg.useAmount = sumAmountUse.totalAmount; - salaryOrg.remainingAmount = calRemainAmount; - await this.salaryOrgRepository.save(salaryOrg); + if (salaryOrg) { + // หาจำนวน Quota คงเหลือ + if (salaryOrg.snapshot == "SNAP1") { + if(salaryOrg.salaryPeriod.period == "APR"){ + const amountFullType = await this.salaryProfileRepository.count({ + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, + }); + const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; + salaryOrg.quantityUsed = amountFullType; + salaryOrg.remainQuota = calRemainQuota; + await this.salaryOrgRepository.save(salaryOrg); + }else if(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 = salaryOrg.sixPercentAmount - sumAmountUse.totalAmount; + salaryOrg.useAmount = sumAmountUse.totalAmount; + salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryOrg); + } } } return new HttpSuccess();