From b2205ff3ce516237a866809dfe928c74a9d4aed6 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 29 Feb 2024 14:52:54 +0700 Subject: [PATCH] cal sixPercent --- src/controllers/SalaryPeriodController.ts | 150 +++++++++++++++------- 1 file changed, 105 insertions(+), 45 deletions(-) diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 537e8cd..9042669 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -143,7 +143,7 @@ export class SalaryPeriodController extends Controller { @Delete("profile/{id}") async deleteSalaryProfile(@Path() id: string) { const salaryProfile = await this.salaryProfileRepository.findOne({ - relations: ["salaryOrg"], + relations: ["salaryOrg","salaryOrg.salaryPeriod"], where: { id: id }, }); if (!salaryProfile) { @@ -151,19 +151,34 @@ export class SalaryPeriodController extends Controller { } await this.salaryProfileRepository.remove(salaryProfile); - + // หาจำนวน Quota คงเหลือ if (salaryProfile.salaryOrg.snapshot == "SNAP1") { - 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); + 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; + salaryProfile.salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryProfile?.salaryOrg); + } } return new HttpSuccess(); @@ -272,17 +287,32 @@ export class SalaryPeriodController extends Controller { await this.salaryProfileRepository.save(salaryProfile); // หาจำนวน Quota คงเหลือ - if (salaryProfile?.salaryOrg.snapshot == "SNAP1") { - 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); + 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; + salaryProfile.salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryProfile?.salaryOrg); + } } return new HttpSuccess(); } @@ -313,18 +343,33 @@ export class SalaryPeriodController extends Controller { salaryProfile.salaryOrgId = salaryOrg.id; await this.salaryProfileRepository.save(salaryProfile); - //หาจำนวน Quota คงเหลือ + // หาจำนวน Quota คงเหลือ if (salaryOrg.snapshot == "SNAP1") { - 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); + 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; + salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryOrg); + } } return new HttpSuccess(); } @@ -634,18 +679,33 @@ export class SalaryPeriodController extends Controller { salaryProfile.lastUpdateFullName = request.user.name; await this.salaryProfileRepository.save(salaryProfile); - //หาจำนวน Quota คงเหลือ + // หาจำนวน Quota คงเหลือ if (salaryOrg.snapshot == "SNAP1") { - 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); + 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; + salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryOrg); + } } return new HttpSuccess(salaryProfile.id);