diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index c342d9e..6cd374a 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -153,34 +153,33 @@ export class SalaryPeriodController extends Controller { // await this.salaryProfileRepository.remove(salaryProfile); // หาจำนวน 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); - } + // 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(); } @@ -288,34 +287,33 @@ export class SalaryPeriodController extends Controller { await this.salaryProfileRepository.save(salaryProfile); // หาจำนวน 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); - } + // 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(); } @@ -344,9 +342,9 @@ export class SalaryPeriodController extends Controller { salaryProfile.salaryOrgId = salaryOrg.id; await this.salaryProfileRepository.save(salaryProfile); - - // หาจำนวน Quota คงเหลือ - if (salaryOrg.snapshot == "SNAP1") { + if (salaryOrg) { + // หาจำนวน Quota คงเหลือ + if (salaryOrg.snapshot == "SNAP1") { if (salaryOrg.salaryPeriod.period == "APR") { const amountFullType = await this.salaryProfileRepository.count({ where: { @@ -372,6 +370,7 @@ export class SalaryPeriodController extends Controller { salaryOrg.remainingAmount = calRemainAmount; await this.salaryOrgRepository.save(salaryOrg); } + } } return new HttpSuccess(); } @@ -692,31 +691,31 @@ export class SalaryPeriodController extends Controller { // หาจำนวน 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.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(salaryProfile.id);