From 2d684725003f0aa1c22ada49edb6b61e3b69e7a2 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Thu, 4 Apr 2024 17:08:18 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=84=E0=B8=B3=E0=B8=99=E0=B8=A7=E0=B8=99?= =?UTF-8?q?=206=E0=B9=80=E0=B8=9B=E0=B8=AD=E0=B8=A3=E0=B9=80=E0=B8=8B?= =?UTF-8?q?=E0=B8=99=E0=B9=83=E0=B8=AB=E0=B8=A1=E0=B9=88=E0=B8=95=E0=B8=AD?= =?UTF-8?q?=E0=B8=99snap1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/SalaryPeriodController.ts | 587 +++++++++++++----- .../SalaryPeriodEmployeeController.ts | 575 ++++++++++++----- 2 files changed, 841 insertions(+), 321 deletions(-) diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 3c604cd..0cceaf4 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -295,37 +295,93 @@ export class SalaryPeriodController extends Controller { await this.salaryProfileRepository.remove(salaryProfile); + const _salaryProfile = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg", "salaryOrg.salaryPeriod"], + where: { id: salaryProfile.id }, + }); + // หาจำนวน 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: In(["HAFT", "FULL", "FULLHAFT"]), - }) - .getRawOne(); - const calRemainAmount = - salaryProfile.salaryOrg.sixPercentAmount - - sumAmountUse.totalAmount - - salaryProfile.salaryOrg.spentAmount; - salaryProfile.salaryOrg.useAmount = - sumAmountUse == null || sumAmountUse.totalAmount == null ? 0 : sumAmountUse.totalAmount; - salaryProfile.salaryOrg.remainingAmount = calRemainAmount; - await this.salaryOrgRepository.save(salaryProfile?.salaryOrg); + if (_salaryProfile != null) { + if (_salaryProfile.salaryOrg.snapshot == "SNAP1") { + if (_salaryProfile.salaryOrg.salaryPeriod.period == "APR") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const amountFullType = await this.salaryProfileRepository.count({ + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, + }); + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.fifteenPercent = Math.floor((salaryOrg.salaryProfiles.length * 15) / 100); + salaryOrg.fifteenPoint = (salaryOrg.salaryProfiles.length * 15) % 100; + salaryOrg.quantityUsed = amountFullType; + const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; + salaryOrg.remainQuota = calRemainQuota; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } else if (_salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const totalProfile = Extension.sumObjectValues(salaryOrg.salaryProfiles, "amount"); + salaryOrg.currentAmount = totalProfile; + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.sixPercentAmount = totalProfile * 0.06; + let totalAmount = 0; + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: _salaryProfile.salaryOrg.salaryPeriod.year, + period: "APR", + }, + }); + if (salaryPeriodAPROld != null) { + const salaryOrgSnap2Old: any = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriodAPROld.id, + rootId: salaryOrg.rootId, + group: salaryOrg.group, + snapshot: "SNAP2", + }, + relations: ["salaryProfiles"], + }); + totalAmount = + salaryOrgSnap2Old == null + ? 0 + : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); + } + salaryOrg.spentAmount = totalAmount; + const sumAmountUse = await AppDataSource.getRepository(SalaryProfile) + .createQueryBuilder("salaryProfile") + .select("SUM(salaryProfile.amountUse)", "totalAmount") + .where({ + salaryOrgId: salaryOrg.id, + type: In(["HAFT", "FULL", "FULLHAFT"]), + }) + .getRawOne(); + const calRemainAmount = + salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; + salaryOrg.useAmount = + sumAmountUse == null || sumAmountUse.totalAmount == null + ? 0 + : sumAmountUse.totalAmount; + salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } } + return new HttpSuccess(); } return new HttpSuccess(); @@ -515,41 +571,94 @@ export class SalaryPeriodController extends Controller { await this.salaryProfileRepository.save(salaryProfile); + const _salaryProfile = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg", "salaryOrg.salaryPeriod"], + where: { id: salaryProfile.id }, + }); + // หาจำนวน Quota คงเหลือ - if (salaryProfile.salaryOrg.snapshot == "SNAP1") { - if (salaryProfile.salaryOrg.salaryPeriod.period == "APR") { - if (salaryProfile.isReserve == false) { - 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 != null) { + if (_salaryProfile.salaryOrg.snapshot == "SNAP1") { + if (_salaryProfile.salaryOrg.salaryPeriod.period == "APR") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const amountFullType = await this.salaryProfileRepository.count({ + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, + }); + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.fifteenPercent = Math.floor((salaryOrg.salaryProfiles.length * 15) / 100); + salaryOrg.fifteenPoint = (salaryOrg.salaryProfiles.length * 15) % 100; + salaryOrg.quantityUsed = amountFullType; + const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; + salaryOrg.remainQuota = calRemainQuota; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } else if (_salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const totalProfile = Extension.sumObjectValues(salaryOrg.salaryProfiles, "amount"); + salaryOrg.currentAmount = totalProfile; + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.sixPercentAmount = totalProfile * 0.06; + let totalAmount = 0; + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: _salaryProfile.salaryOrg.salaryPeriod.year, + period: "APR", + }, + }); + if (salaryPeriodAPROld != null) { + const salaryOrgSnap2Old: any = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriodAPROld.id, + rootId: salaryOrg.rootId, + group: salaryOrg.group, + snapshot: "SNAP2", + }, + relations: ["salaryProfiles"], + }); + totalAmount = + salaryOrgSnap2Old == null + ? 0 + : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); + } + salaryOrg.spentAmount = totalAmount; + const sumAmountUse = await AppDataSource.getRepository(SalaryProfile) + .createQueryBuilder("salaryProfile") + .select("SUM(salaryProfile.amountUse)", "totalAmount") + .where({ + salaryOrgId: salaryOrg.id, + type: In(["HAFT", "FULL", "FULLHAFT"]), + }) + .getRawOne(); + const calRemainAmount = + salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; + salaryOrg.useAmount = + sumAmountUse == null || sumAmountUse.totalAmount == null + ? 0 + : sumAmountUse.totalAmount; + salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(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: In(["HAFT", "FULL", "FULLHAFT"]), - }) - .getRawOne(); - const calRemainAmount = - salaryProfile.salaryOrg.sixPercentAmount - - sumAmountUse.totalAmount - - salaryProfile.salaryOrg.spentAmount; - salaryProfile.salaryOrg.useAmount = - sumAmountUse == null || sumAmountUse.totalAmount == null ? 0 : sumAmountUse.totalAmount; - salaryProfile.salaryOrg.remainingAmount = calRemainAmount; - await this.salaryOrgRepository.save(salaryProfile?.salaryOrg); } + return new HttpSuccess(); } - return new HttpSuccess(); } /** @@ -579,39 +688,93 @@ export class SalaryPeriodController extends Controller { salaryProfile.salaryOrgId = salaryOrg.id; await this.salaryProfileRepository.save(salaryProfile); - if (salaryOrg) { - // หาจำนวน Quota คงเหลือ - if (salaryOrg.snapshot == "SNAP1") { - if (salaryOrg.salaryPeriod.period == "APR") { - if (salaryProfile.isReserve == false) { - const amountFullType = await this.salaryProfileRepository.count({ + const _salaryProfile = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg", "salaryOrg.salaryPeriod"], + where: { id: salaryProfile.id }, + }); + + // หาจำนวน Quota คงเหลือ + if (_salaryProfile != null) { + if (_salaryProfile.salaryOrg.snapshot == "SNAP1") { + if (_salaryProfile.salaryOrg.salaryPeriod.period == "APR") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ where: { - salaryOrgId: salaryOrg.id, - type: "FULL", + id: _salaryProfile.salaryOrg.id, }, }); - const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; - salaryOrg.quantityUsed = amountFullType; - salaryOrg.remainQuota = calRemainQuota; - await this.salaryOrgRepository.save(salaryOrg); + if (salaryOrg != null) { + const amountFullType = await this.salaryProfileRepository.count({ + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, + }); + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.fifteenPercent = Math.floor((salaryOrg.salaryProfiles.length * 15) / 100); + salaryOrg.fifteenPoint = (salaryOrg.salaryProfiles.length * 15) % 100; + salaryOrg.quantityUsed = amountFullType; + const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; + salaryOrg.remainQuota = calRemainQuota; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } else if (_salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const totalProfile = Extension.sumObjectValues(salaryOrg.salaryProfiles, "amount"); + salaryOrg.currentAmount = totalProfile; + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.sixPercentAmount = totalProfile * 0.06; + let totalAmount = 0; + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: _salaryProfile.salaryOrg.salaryPeriod.year, + period: "APR", + }, + }); + if (salaryPeriodAPROld != null) { + const salaryOrgSnap2Old: any = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriodAPROld.id, + rootId: salaryOrg.rootId, + group: salaryOrg.group, + snapshot: "SNAP2", + }, + relations: ["salaryProfiles"], + }); + totalAmount = + salaryOrgSnap2Old == null + ? 0 + : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); + } + salaryOrg.spentAmount = totalAmount; + const sumAmountUse = await AppDataSource.getRepository(SalaryProfile) + .createQueryBuilder("salaryProfile") + .select("SUM(salaryProfile.amountUse)", "totalAmount") + .where({ + salaryOrgId: salaryOrg.id, + type: In(["HAFT", "FULL", "FULLHAFT"]), + }) + .getRawOne(); + const calRemainAmount = + salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; + salaryOrg.useAmount = + sumAmountUse == null || sumAmountUse.totalAmount == null + ? 0 + : sumAmountUse.totalAmount; + salaryOrg.remainingAmount = calRemainAmount; + 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: salaryOrg.id, - type: In(["HAFT", "FULL", "FULLHAFT"]), - }) - .getRawOne(); - const calRemainAmount = - salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; - salaryOrg.useAmount = - sumAmountUse == null || sumAmountUse.totalAmount == null ? 0 : sumAmountUse.totalAmount; - salaryOrg.remainingAmount = calRemainAmount; - await this.salaryOrgRepository.save(salaryOrg); } } + return new HttpSuccess(); } return new HttpSuccess(); } @@ -659,7 +822,7 @@ export class SalaryPeriodController extends Controller { ); } } - } + } if (body.type == "FULL") { salaryProfile.isReserve = body.isReserve; } else { @@ -832,41 +995,93 @@ export class SalaryPeriodController extends Controller { } await this.salaryProfileRepository.save(salaryProfile); - if (salaryProfile.salaryOrg) { - // หาจำนวน Quota คงเหลือ - if (salaryProfile.salaryOrg.snapshot == "SNAP1") { - if (salaryProfile.salaryOrg.salaryPeriod.period == "APR") { - if (body.isReserve == false) { - const amountFullType = await this.salaryProfileRepository.count({ + const _salaryProfile = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg", "salaryOrg.salaryPeriod"], + where: { id: salaryProfile.id }, + }); + + // หาจำนวน Quota คงเหลือ + if (_salaryProfile != null) { + if (_salaryProfile.salaryOrg.snapshot == "SNAP1") { + if (_salaryProfile.salaryOrg.salaryPeriod.period == "APR") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ where: { - salaryOrgId: salaryProfile.salaryOrg.id, - type: "FULL", + id: _salaryProfile.salaryOrg.id, }, }); - const calRemainQuota = salaryProfile.salaryOrg.fifteenPercent - amountFullType; - salaryProfile.salaryOrg.quantityUsed = amountFullType; - salaryProfile.salaryOrg.remainQuota = calRemainQuota; - await this.salaryOrgRepository.save(salaryProfile.salaryOrg); + if (salaryOrg != null) { + const amountFullType = await this.salaryProfileRepository.count({ + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, + }); + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.fifteenPercent = Math.floor((salaryOrg.salaryProfiles.length * 15) / 100); + salaryOrg.fifteenPoint = (salaryOrg.salaryProfiles.length * 15) % 100; + salaryOrg.quantityUsed = amountFullType; + const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; + salaryOrg.remainQuota = calRemainQuota; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } else if (_salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const totalProfile = Extension.sumObjectValues(salaryOrg.salaryProfiles, "amount"); + salaryOrg.currentAmount = totalProfile; + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.sixPercentAmount = totalProfile * 0.06; + let totalAmount = 0; + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: _salaryProfile.salaryOrg.salaryPeriod.year, + period: "APR", + }, + }); + if (salaryPeriodAPROld != null) { + const salaryOrgSnap2Old: any = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriodAPROld.id, + rootId: salaryOrg.rootId, + group: salaryOrg.group, + snapshot: "SNAP2", + }, + relations: ["salaryProfiles"], + }); + totalAmount = + salaryOrgSnap2Old == null + ? 0 + : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); + } + salaryOrg.spentAmount = totalAmount; + const sumAmountUse = await AppDataSource.getRepository(SalaryProfile) + .createQueryBuilder("salaryProfile") + .select("SUM(salaryProfile.amountUse)", "totalAmount") + .where({ + salaryOrgId: salaryOrg.id, + type: In(["HAFT", "FULL", "FULLHAFT"]), + }) + .getRawOne(); + const calRemainAmount = + salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; + salaryOrg.useAmount = + sumAmountUse == null || sumAmountUse.totalAmount == null + ? 0 + : sumAmountUse.totalAmount; + salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(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: In(["HAFT", "FULL", "FULLHAFT"]), - }) - .getRawOne(); - const calRemainAmount = - salaryProfile.salaryOrg.sixPercentAmount - - sumAmountUse.totalAmount - - salaryProfile.salaryOrg.spentAmount; - salaryProfile.salaryOrg.useAmount = - sumAmountUse == null || sumAmountUse.totalAmount == null ? 0 : sumAmountUse.totalAmount; - salaryProfile.salaryOrg.remainingAmount = calRemainAmount; - await this.salaryOrgRepository.save(salaryProfile.salaryOrg); } } + return new HttpSuccess(); } return new HttpSuccess(); } @@ -1208,54 +1423,94 @@ export class SalaryPeriodController extends Controller { salaryProfile.lastUpdateFullName = request.user.name; await this.salaryProfileRepository.save(salaryProfile); + const _salaryProfile = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg", "salaryOrg.salaryPeriod"], + where: { id: salaryProfile.id }, + }); + // หาจำนวน Quota คงเหลือ - if (salaryOrg.snapshot == "SNAP1") { - const _salaryOrg = await this.salaryOrgRepository.findOne({ - where: { - id: salaryOrg.id, - }, - }); - if (!_salaryOrg) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบรอบการขึ้นเงินเดือน"); - } - if (salaryOrg.salaryPeriod.period == "APR") { - _salaryOrg.total = salaryOrg.salaryProfiles.length; - _salaryOrg.fifteenPercent = Math.floor((salaryOrg.salaryProfiles.length * 15) / 100); - _salaryOrg.fifteenPoint = (salaryOrg.salaryProfiles.length * 15) % 100; - - 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 totalProfile = Extension.sumObjectValues(salaryOrg.salaryProfiles, "amount"); - _salaryOrg.currentAmount = totalProfile; - _salaryOrg.total = salaryOrg.salaryProfiles.length; - _salaryOrg.sixPercentAmount = totalProfile * 0.06; - - const sumAmountUse = await AppDataSource.getRepository(SalaryProfile) - .createQueryBuilder("salaryProfile") - .select("SUM(salaryProfile.amountUse)", "totalAmount") - .where({ - salaryOrgId: salaryOrg.id, - type: In(["HAFT", "FULL", "FULLHAFT"]), - }) - .getRawOne(); - const calRemainAmount = - salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; - _salaryOrg.useAmount = - sumAmountUse == null || sumAmountUse.totalAmount == null ? 0 : sumAmountUse.totalAmount; - _salaryOrg.remainingAmount = calRemainAmount; - await this.salaryOrgRepository.save(_salaryOrg); + if (_salaryProfile != null) { + if (_salaryProfile.salaryOrg.snapshot == "SNAP1") { + if (_salaryProfile.salaryOrg.salaryPeriod.period == "APR") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const amountFullType = await this.salaryProfileRepository.count({ + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, + }); + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.fifteenPercent = Math.floor((salaryOrg.salaryProfiles.length * 15) / 100); + salaryOrg.fifteenPoint = (salaryOrg.salaryProfiles.length * 15) % 100; + salaryOrg.quantityUsed = amountFullType; + const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; + salaryOrg.remainQuota = calRemainQuota; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } else if (_salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const totalProfile = Extension.sumObjectValues(salaryOrg.salaryProfiles, "amount"); + salaryOrg.currentAmount = totalProfile; + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.sixPercentAmount = totalProfile * 0.06; + let totalAmount = 0; + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: _salaryProfile.salaryOrg.salaryPeriod.year, + period: "APR", + }, + }); + if (salaryPeriodAPROld != null) { + const salaryOrgSnap2Old: any = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriodAPROld.id, + rootId: salaryOrg.rootId, + group: salaryOrg.group, + snapshot: "SNAP2", + }, + relations: ["salaryProfiles"], + }); + totalAmount = + salaryOrgSnap2Old == null + ? 0 + : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); + } + salaryOrg.spentAmount = totalAmount; + const sumAmountUse = await AppDataSource.getRepository(SalaryProfile) + .createQueryBuilder("salaryProfile") + .select("SUM(salaryProfile.amountUse)", "totalAmount") + .where({ + salaryOrgId: salaryOrg.id, + type: In(["HAFT", "FULL", "FULLHAFT"]), + }) + .getRawOne(); + const calRemainAmount = + salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; + salaryOrg.useAmount = + sumAmountUse == null || sumAmountUse.totalAmount == null + ? 0 + : sumAmountUse.totalAmount; + salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } } + return new HttpSuccess(); } - return new HttpSuccess(salaryProfile.id); } @@ -1856,6 +2111,8 @@ export class SalaryPeriodController extends Controller { salaryProfileNew.positionSalaryAmount = salaryOld == null ? 0 : salaryOld.positionSalaryAmount; salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext; + salaryProfileNew.isSpecial = salaryOld == null ? false : salaryOld.isSpecial; + salaryProfileNew.isReserve = salaryOld == null ? false : salaryOld.isReserve; } await this.salaryProfileRepository.save(salaryProfileNew); } @@ -1898,6 +2155,8 @@ export class SalaryPeriodController extends Controller { salaryProfileNew.positionSalaryAmount = salaryOld == null ? 0 : salaryOld.positionSalaryAmount; salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext; + salaryProfileNew.isSpecial = salaryOld == null ? false : salaryOld.isSpecial; + salaryProfileNew.isReserve = salaryOld == null ? false : salaryOld.isReserve; } await this.salaryProfileEmployeeRepository.save(salaryProfileNew); } @@ -2342,6 +2601,8 @@ export class SalaryPeriodController extends Controller { salaryProfileNew.positionSalaryAmount = salaryOld == null ? 0 : salaryOld.positionSalaryAmount; salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext; + salaryProfileNew.type = salaryOld == null ? false : salaryOld.isSpecial; + salaryProfileNew.isReserve = salaryOld == null ? false : salaryOld.isReserve; } await this.salaryProfileRepository.save(salaryProfileNew); } @@ -2384,6 +2645,8 @@ export class SalaryPeriodController extends Controller { salaryProfileNew.positionSalaryAmount = salaryOld == null ? 0 : salaryOld.positionSalaryAmount; salaryProfileNew.isNext = salaryOld == null ? false : salaryOld.isNext; + salaryProfileNew.type = salaryOld == null ? false : salaryOld.isSpecial; + salaryProfileNew.isReserve = salaryOld == null ? false : salaryOld.isReserve; } await this.salaryProfileEmployeeRepository.save(salaryProfileNew); } diff --git a/src/controllers/SalaryPeriodEmployeeController.ts b/src/controllers/SalaryPeriodEmployeeController.ts index fe84fb6..5aa28d5 100644 --- a/src/controllers/SalaryPeriodEmployeeController.ts +++ b/src/controllers/SalaryPeriodEmployeeController.ts @@ -257,37 +257,93 @@ export class SalaryPeriodEmployeeController extends Controller { await this.salaryProfileRepository.remove(salaryProfile); + const _salaryProfile = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg", "salaryOrg.salaryPeriod"], + where: { id: salaryProfile.id }, + }); + // หาจำนวน 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(SalaryProfileEmployee) - .createQueryBuilder("salaryProfile") - .select("SUM(salaryProfile.amountUse)", "totalAmount") - .where({ - salaryOrgId: salaryProfile?.salaryOrg.id, - type: In(["HAFT", "FULL", "FULLHAFT"]), - }) - .getRawOne(); - const calRemainAmount = - salaryProfile.salaryOrg.sixPercentAmount - - sumAmountUse.totalAmount - - salaryProfile.salaryOrg.spentAmount; - salaryProfile.salaryOrg.useAmount = - sumAmountUse == null || sumAmountUse.totalAmount == null ? 0 : sumAmountUse.totalAmount; - salaryProfile.salaryOrg.remainingAmount = calRemainAmount; - await this.salaryOrgRepository.save(salaryProfile?.salaryOrg); + if (_salaryProfile != null) { + if (_salaryProfile.salaryOrg.snapshot == "SNAP1") { + if (_salaryProfile.salaryOrg.salaryPeriod.period == "APR") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const amountFullType = await this.salaryProfileRepository.count({ + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, + }); + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.fifteenPercent = Math.floor((salaryOrg.salaryProfiles.length * 15) / 100); + salaryOrg.fifteenPoint = (salaryOrg.salaryProfiles.length * 15) % 100; + salaryOrg.quantityUsed = amountFullType; + const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; + salaryOrg.remainQuota = calRemainQuota; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } else if (_salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const totalProfile = Extension.sumObjectValues(salaryOrg.salaryProfiles, "amount"); + salaryOrg.currentAmount = totalProfile; + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.sixPercentAmount = totalProfile * 0.06; + let totalAmount = 0; + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: _salaryProfile.salaryOrg.salaryPeriod.year, + period: "APR", + }, + }); + if (salaryPeriodAPROld != null) { + const salaryOrgSnap2Old: any = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriodAPROld.id, + rootId: salaryOrg.rootId, + group: salaryOrg.group, + snapshot: "SNAP2", + }, + relations: ["salaryProfiles"], + }); + totalAmount = + salaryOrgSnap2Old == null + ? 0 + : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); + } + salaryOrg.spentAmount = totalAmount; + const sumAmountUse = await AppDataSource.getRepository(SalaryProfileEmployee) + .createQueryBuilder("salaryProfileEmployee") + .select("SUM(salaryProfileEmployee.amountUse)", "totalAmount") + .where({ + salaryOrgId: salaryOrg.id, + type: In(["HAFT", "FULL", "FULLHAFT"]), + }) + .getRawOne(); + const calRemainAmount = + salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; + salaryOrg.useAmount = + sumAmountUse == null || sumAmountUse.totalAmount == null + ? 0 + : sumAmountUse.totalAmount; + salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } } + return new HttpSuccess(); } return new HttpSuccess(); @@ -649,39 +705,93 @@ export class SalaryPeriodEmployeeController extends Controller { await this.salaryProfileRepository.save(salaryProfile); + const _salaryProfile = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg", "salaryOrg.salaryPeriod"], + where: { id: salaryProfile.id }, + }); + // หาจำนวน Quota คงเหลือ - if (salaryProfile.salaryOrg.snapshot == "SNAP1") { - if (salaryProfile.salaryOrg.salaryPeriod.period == "APR") { - if (salaryProfile.isReserve == false) { - 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 != null) { + if (_salaryProfile.salaryOrg.snapshot == "SNAP1") { + if (_salaryProfile.salaryOrg.salaryPeriod.period == "APR") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const amountFullType = await this.salaryProfileRepository.count({ + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, + }); + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.fifteenPercent = Math.floor((salaryOrg.salaryProfiles.length * 15) / 100); + salaryOrg.fifteenPoint = (salaryOrg.salaryProfiles.length * 15) % 100; + salaryOrg.quantityUsed = amountFullType; + const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; + salaryOrg.remainQuota = calRemainQuota; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } else if (_salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const totalProfile = Extension.sumObjectValues(salaryOrg.salaryProfiles, "amount"); + salaryOrg.currentAmount = totalProfile; + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.sixPercentAmount = totalProfile * 0.06; + let totalAmount = 0; + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: _salaryProfile.salaryOrg.salaryPeriod.year, + period: "APR", + }, + }); + if (salaryPeriodAPROld != null) { + const salaryOrgSnap2Old: any = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriodAPROld.id, + rootId: salaryOrg.rootId, + group: salaryOrg.group, + snapshot: "SNAP2", + }, + relations: ["salaryProfiles"], + }); + totalAmount = + salaryOrgSnap2Old == null + ? 0 + : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); + } + salaryOrg.spentAmount = totalAmount; + const sumAmountUse = await AppDataSource.getRepository(SalaryProfileEmployee) + .createQueryBuilder("salaryProfileEmployee") + .select("SUM(salaryProfileEmployee.amountUse)", "totalAmount") + .where({ + salaryOrgId: salaryOrg.id, + type: In(["HAFT", "FULL", "FULLHAFT"]), + }) + .getRawOne(); + const calRemainAmount = + salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; + salaryOrg.useAmount = + sumAmountUse == null || sumAmountUse.totalAmount == null + ? 0 + : sumAmountUse.totalAmount; + salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryOrg); + } + } } - } else if (salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { - const sumAmountUse = await AppDataSource.getRepository(SalaryProfileEmployee) - .createQueryBuilder("salaryProfile") - .select("SUM(salaryProfile.amountUse)", "totalAmount") - .where({ - salaryOrgId: salaryProfile?.salaryOrg.id, - type: In(["HAFT", "FULL", "FULLHAFT"]), - }) - .getRawOne(); - const calRemainAmount = - salaryProfile.salaryOrg.sixPercentAmount - - sumAmountUse.totalAmount - - salaryProfile.salaryOrg.spentAmount; - salaryProfile.salaryOrg.useAmount = - sumAmountUse == null || sumAmountUse.totalAmount == null ? 0 : sumAmountUse.totalAmount; - salaryProfile.salaryOrg.remainingAmount = calRemainAmount; - await this.salaryOrgRepository.save(salaryProfile?.salaryOrg); } + return new HttpSuccess(); } return new HttpSuccess(); } @@ -713,39 +823,93 @@ export class SalaryPeriodEmployeeController extends Controller { salaryProfile.salaryOrgId = salaryOrg.id; await this.salaryProfileRepository.save(salaryProfile); - if (salaryOrg) { - // หาจำนวน Quota คงเหลือ - if (salaryOrg.snapshot == "SNAP1") { - if (salaryOrg.salaryPeriod.period == "APR") { - if (salaryProfile.isReserve == false) { - const amountFullType = await this.salaryProfileRepository.count({ + const _salaryProfile = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg", "salaryOrg.salaryPeriod"], + where: { id: salaryProfile.id }, + }); + + // หาจำนวน Quota คงเหลือ + if (_salaryProfile != null) { + if (_salaryProfile.salaryOrg.snapshot == "SNAP1") { + if (_salaryProfile.salaryOrg.salaryPeriod.period == "APR") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ where: { - salaryOrgId: salaryOrg.id, - type: "FULL", + id: _salaryProfile.salaryOrg.id, }, }); - const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; - salaryOrg.quantityUsed = amountFullType; - salaryOrg.remainQuota = calRemainQuota; - await this.salaryOrgRepository.save(salaryOrg); + if (salaryOrg != null) { + const amountFullType = await this.salaryProfileRepository.count({ + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, + }); + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.fifteenPercent = Math.floor((salaryOrg.salaryProfiles.length * 15) / 100); + salaryOrg.fifteenPoint = (salaryOrg.salaryProfiles.length * 15) % 100; + salaryOrg.quantityUsed = amountFullType; + const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; + salaryOrg.remainQuota = calRemainQuota; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } else if (_salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const totalProfile = Extension.sumObjectValues(salaryOrg.salaryProfiles, "amount"); + salaryOrg.currentAmount = totalProfile; + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.sixPercentAmount = totalProfile * 0.06; + let totalAmount = 0; + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: _salaryProfile.salaryOrg.salaryPeriod.year, + period: "APR", + }, + }); + if (salaryPeriodAPROld != null) { + const salaryOrgSnap2Old: any = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriodAPROld.id, + rootId: salaryOrg.rootId, + group: salaryOrg.group, + snapshot: "SNAP2", + }, + relations: ["salaryProfiles"], + }); + totalAmount = + salaryOrgSnap2Old == null + ? 0 + : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); + } + salaryOrg.spentAmount = totalAmount; + const sumAmountUse = await AppDataSource.getRepository(SalaryProfileEmployee) + .createQueryBuilder("salaryProfileEmployee") + .select("SUM(salaryProfileEmployee.amountUse)", "totalAmount") + .where({ + salaryOrgId: salaryOrg.id, + type: In(["HAFT", "FULL", "FULLHAFT"]), + }) + .getRawOne(); + const calRemainAmount = + salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; + salaryOrg.useAmount = + sumAmountUse == null || sumAmountUse.totalAmount == null + ? 0 + : sumAmountUse.totalAmount; + salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryOrg); + } } - } else if (salaryOrg.salaryPeriod.period == "OCT") { - const sumAmountUse = await AppDataSource.getRepository(SalaryProfileEmployee) - .createQueryBuilder("salaryProfile") - .select("SUM(salaryProfile.amountUse)", "totalAmount") - .where({ - salaryOrgId: salaryOrg.id, - type: In(["HAFT", "FULL", "FULLHAFT"]), - }) - .getRawOne(); - const calRemainAmount = - salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; - salaryOrg.useAmount = - sumAmountUse == null || sumAmountUse.totalAmount == null ? 0 : sumAmountUse.totalAmount; - salaryOrg.remainingAmount = calRemainAmount; - await this.salaryOrgRepository.save(salaryOrg); } } + return new HttpSuccess(); } return new HttpSuccess(); } @@ -1141,41 +1305,93 @@ export class SalaryPeriodEmployeeController extends Controller { } await this.salaryProfileRepository.save(salaryProfile); - if (salaryProfile.salaryOrg) { - // หาจำนวน Quota คงเหลือ - if (salaryProfile.salaryOrg.snapshot == "SNAP1") { - if (salaryProfile.salaryOrg.salaryPeriod.period == "APR") { - if (body.isReserve == false) { - const amountFullType = await this.salaryProfileRepository.count({ + const _salaryProfile = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg", "salaryOrg.salaryPeriod"], + where: { id: salaryProfile.id }, + }); + + // หาจำนวน Quota คงเหลือ + if (_salaryProfile != null) { + if (_salaryProfile.salaryOrg.snapshot == "SNAP1") { + if (_salaryProfile.salaryOrg.salaryPeriod.period == "APR") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ where: { - salaryOrgId: salaryProfile.salaryOrg.id, - type: "FULL", + id: _salaryProfile.salaryOrg.id, }, }); - const calRemainQuota = salaryProfile.salaryOrg.fifteenPercent - amountFullType; - salaryProfile.salaryOrg.quantityUsed = amountFullType; - salaryProfile.salaryOrg.remainQuota = calRemainQuota; - await this.salaryOrgRepository.save(salaryProfile.salaryOrg); + if (salaryOrg != null) { + const amountFullType = await this.salaryProfileRepository.count({ + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, + }); + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.fifteenPercent = Math.floor((salaryOrg.salaryProfiles.length * 15) / 100); + salaryOrg.fifteenPoint = (salaryOrg.salaryProfiles.length * 15) % 100; + salaryOrg.quantityUsed = amountFullType; + const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; + salaryOrg.remainQuota = calRemainQuota; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } else if (_salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const totalProfile = Extension.sumObjectValues(salaryOrg.salaryProfiles, "amount"); + salaryOrg.currentAmount = totalProfile; + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.sixPercentAmount = totalProfile * 0.06; + let totalAmount = 0; + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: _salaryProfile.salaryOrg.salaryPeriod.year, + period: "APR", + }, + }); + if (salaryPeriodAPROld != null) { + const salaryOrgSnap2Old: any = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriodAPROld.id, + rootId: salaryOrg.rootId, + group: salaryOrg.group, + snapshot: "SNAP2", + }, + relations: ["salaryProfiles"], + }); + totalAmount = + salaryOrgSnap2Old == null + ? 0 + : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); + } + salaryOrg.spentAmount = totalAmount; + const sumAmountUse = await AppDataSource.getRepository(SalaryProfileEmployee) + .createQueryBuilder("salaryProfileEmployee") + .select("SUM(salaryProfileEmployee.amountUse)", "totalAmount") + .where({ + salaryOrgId: salaryOrg.id, + type: In(["HAFT", "FULL", "FULLHAFT"]), + }) + .getRawOne(); + const calRemainAmount = + salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; + salaryOrg.useAmount = + sumAmountUse == null || sumAmountUse.totalAmount == null + ? 0 + : sumAmountUse.totalAmount; + salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryOrg); + } } - } else if (salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { - const sumAmountUse = await AppDataSource.getRepository(SalaryProfileEmployee) - .createQueryBuilder("salaryProfile") - .select("SUM(salaryProfile.amountUse)", "totalAmount") - .where({ - salaryOrgId: salaryProfile.salaryOrg.id, - type: In(["HAFT", "FULL", "FULLHAFT"]), - }) - .getRawOne(); - const calRemainAmount = - salaryProfile.salaryOrg.sixPercentAmount - - sumAmountUse.totalAmount - - salaryProfile.salaryOrg.spentAmount; - salaryProfile.salaryOrg.useAmount = - sumAmountUse == null || sumAmountUse.totalAmount == null ? 0 : sumAmountUse.totalAmount; - salaryProfile.salaryOrg.remainingAmount = calRemainAmount; - await this.salaryOrgRepository.save(salaryProfile.salaryOrg); } } + return new HttpSuccess(); } return new HttpSuccess(); } @@ -1742,52 +1958,93 @@ export class SalaryPeriodEmployeeController extends Controller { salaryProfile.lastUpdateFullName = request.user.name; await this.salaryProfileRepository.save(salaryProfile); + const _salaryProfile = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg", "salaryOrg.salaryPeriod"], + where: { id: salaryProfile.id }, + }); + // หาจำนวน Quota คงเหลือ - if (salaryOrg.snapshot == "SNAP1") { - const _salaryOrg = await this.salaryOrgRepository.findOne({ - where: { - id: salaryOrg.id, - }, - }); - if (!_salaryOrg) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบรอบการขึ้นเงินเดือน"); - } - if (salaryOrg.salaryPeriod.period == "APR") { - _salaryOrg.total = salaryOrg.salaryProfiles.length; - _salaryOrg.fifteenPercent = Math.floor((salaryOrg.salaryProfiles.length * 15) / 100); - _salaryOrg.fifteenPoint = (salaryOrg.salaryProfiles.length * 15) % 100; - - 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 totalProfile = Extension.sumObjectValues(salaryOrg.salaryProfiles, "amount"); - _salaryOrg.currentAmount = totalProfile; - _salaryOrg.total = salaryOrg.salaryProfiles.length; - _salaryOrg.sixPercentAmount = totalProfile * 0.06; - - const sumAmountUse = await AppDataSource.getRepository(SalaryProfileEmployee) - .createQueryBuilder("salaryProfile") - .select("SUM(salaryProfile.amountUse)", "totalAmount") - .where({ - salaryOrgId: salaryOrg.id, - type: In(["HAFT", "FULL", "FULLHAFT"]), - }) - .getRawOne(); - const calRemainAmount = - salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; - _salaryOrg.useAmount = - sumAmountUse == null || sumAmountUse.totalAmount == null ? 0 : sumAmountUse.totalAmount; - _salaryOrg.remainingAmount = calRemainAmount; - await this.salaryOrgRepository.save(_salaryOrg); + if (_salaryProfile != null) { + if (_salaryProfile.salaryOrg.snapshot == "SNAP1") { + if (_salaryProfile.salaryOrg.salaryPeriod.period == "APR") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const amountFullType = await this.salaryProfileRepository.count({ + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, + }); + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.fifteenPercent = Math.floor((salaryOrg.salaryProfiles.length * 15) / 100); + salaryOrg.fifteenPoint = (salaryOrg.salaryProfiles.length * 15) % 100; + salaryOrg.quantityUsed = amountFullType; + const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; + salaryOrg.remainQuota = calRemainQuota; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } else if (_salaryProfile.salaryOrg.salaryPeriod.period == "OCT") { + if (_salaryProfile != null) { + const salaryOrg = await this.salaryOrgRepository.findOne({ + where: { + id: _salaryProfile.salaryOrg.id, + }, + }); + if (salaryOrg != null) { + const totalProfile = Extension.sumObjectValues(salaryOrg.salaryProfiles, "amount"); + salaryOrg.currentAmount = totalProfile; + salaryOrg.total = salaryOrg.salaryProfiles.length; + salaryOrg.sixPercentAmount = totalProfile * 0.06; + let totalAmount = 0; + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: _salaryProfile.salaryOrg.salaryPeriod.year, + period: "APR", + }, + }); + if (salaryPeriodAPROld != null) { + const salaryOrgSnap2Old: any = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriodAPROld.id, + rootId: salaryOrg.rootId, + group: salaryOrg.group, + snapshot: "SNAP2", + }, + relations: ["salaryProfiles"], + }); + totalAmount = + salaryOrgSnap2Old == null + ? 0 + : Extension.sumObjectValues(salaryOrgSnap2Old.salaryProfiles, "amountUse"); + } + salaryOrg.spentAmount = totalAmount; + const sumAmountUse = await AppDataSource.getRepository(SalaryProfileEmployee) + .createQueryBuilder("salaryProfileEmployee") + .select("SUM(salaryProfileEmployee.amountUse)", "totalAmount") + .where({ + salaryOrgId: salaryOrg.id, + type: In(["HAFT", "FULL", "FULLHAFT"]), + }) + .getRawOne(); + const calRemainAmount = + salaryOrg.sixPercentAmount - sumAmountUse.totalAmount - salaryOrg.spentAmount; + salaryOrg.useAmount = + sumAmountUse == null || sumAmountUse.totalAmount == null + ? 0 + : sumAmountUse.totalAmount; + salaryOrg.remainingAmount = calRemainAmount; + await this.salaryOrgRepository.save(salaryOrg); + } + } + } } + return new HttpSuccess(); } return new HttpSuccess(salaryProfile.id);