From 77b8ef8a7dc47418cd93d3fbdbb732b87308fb1f Mon Sep 17 00:00:00 2001 From: Kittapath Date: Thu, 29 Feb 2024 11:01:35 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=20merge?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/SalaryPeriodController.ts | 423 +++++++++++++++++++--- 1 file changed, 367 insertions(+), 56 deletions(-) diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 266fcf2..017e40d 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -145,16 +145,16 @@ export class SalaryPeriodController extends Controller { if (!salaryProfile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการขอเงินเดือนผู้ใช้งานนี้ในระบบ"); } - + await this.salaryProfileRepository.remove(salaryProfile); - - // หาจำนวน Quota คงเหลือ - if(salaryProfile.salaryOrg.snapshot == "SNAP1"){ + + // หาจำนวน Quota คงเหลือ + if (salaryProfile.salaryOrg.snapshot == "SNAP1") { const amountFullType = await this.salaryProfileRepository.count({ - where:{ - salaryOrgId:salaryProfile?.salaryOrg.id, - type:"FULL" - } + where: { + salaryOrgId: salaryProfile?.salaryOrg.id, + type: "FULL", + }, }); const calRemainQuota = salaryProfile?.salaryOrg.fifteenPercent - amountFullType; salaryProfile.salaryOrg.quantityUsed = amountFullType; @@ -263,16 +263,16 @@ export class SalaryPeriodController extends Controller { } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทการเลื่อนขึ้นเงินเดือนไม่ถูกต้อง"); } - + await this.salaryProfileRepository.save(salaryProfile); - // หาจำนวน Quota คงเหลือ - if(salaryProfile?.salaryOrg.snapshot == "SNAP1"){ + // หาจำนวน Quota คงเหลือ + if (salaryProfile?.salaryOrg.snapshot == "SNAP1") { const amountFullType = await this.salaryProfileRepository.count({ - where:{ - salaryOrgId:salaryProfile?.salaryOrg.id, - type:"FULL" - } + where: { + salaryOrgId: salaryProfile?.salaryOrg.id, + type: "FULL", + }, }); const calRemainQuota = salaryProfile?.salaryOrg.fifteenPercent - amountFullType; salaryProfile.salaryOrg.quantityUsed = amountFullType; @@ -308,13 +308,13 @@ export class SalaryPeriodController extends Controller { salaryProfile.salaryOrgId = salaryOrg.id; await this.salaryProfileRepository.save(salaryProfile); - //หาจำนวน Quota คงเหลือ - if(salaryOrg.snapshot == "SNAP1"){ + //หาจำนวน Quota คงเหลือ + if (salaryOrg.snapshot == "SNAP1") { const amountFullType = await this.salaryProfileRepository.count({ - where:{ - salaryOrgId:salaryOrg.id, - type:"FULL" - } + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, }); const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; salaryOrg.quantityUsed = amountFullType; @@ -586,7 +586,7 @@ export class SalaryPeriodController extends Controller { salaryProfile.amountSpecial = 0; salaryProfile.amountUse = 0; salaryProfile.positionSalaryAmount = 0; - }else if (salaryProfile.type == "HAFT") { + } else if (salaryProfile.type == "HAFT") { salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryHalfSpecial; salaryProfile.amountUse = salaryRanks == null || @@ -629,13 +629,13 @@ export class SalaryPeriodController extends Controller { salaryProfile.lastUpdateFullName = request.user.name; await this.salaryProfileRepository.save(salaryProfile); - //หาจำนวน Quota คงเหลือ - if(salaryOrg.snapshot == "SNAP1"){ + //หาจำนวน Quota คงเหลือ + if (salaryOrg.snapshot == "SNAP1") { const amountFullType = await this.salaryProfileRepository.count({ - where:{ - salaryOrgId:salaryOrg.id, - type:"FULL" - } + where: { + salaryOrgId: salaryOrg.id, + type: "FULL", + }, }); const calRemainQuota = salaryOrg.fifteenPercent - amountFullType; salaryOrg.quantityUsed = amountFullType; @@ -852,10 +852,9 @@ export class SalaryPeriodController extends Controller { }); await this.salaryProfileRepository.remove(salaryProfile); await this.salaryOrgRepository.remove(salaryOrg); - - let orgs = await new CallAPI().GetData(request, "org/active/root/id"); + let orgs = await new CallAPI().GetData(request, "org/unauthorize/active/root/id"); let total = 1000; - let _orgProfiles = await new CallAPI().PostData(request, "org/profile/salary/gen", { + let _orgProfiles = await new CallAPI().PostData(request, "org/unauthorize/salary/gen", { page: 1, pageSize: 1000, keyword: "", @@ -866,7 +865,7 @@ export class SalaryPeriodController extends Controller { const page = Math.ceil(total / 1000); for (let index = 2; index <= page; index++) { await new CallAPI() - .PostData(request, "org/profile/salary/gen", { + .PostData(request, "org/unauthorize/profile/salary/gen", { page: index, pageSize: 1000, keyword: "", @@ -876,7 +875,7 @@ export class SalaryPeriodController extends Controller { }); } } - let revisionId = await new CallAPI().GetData(request, "org/revision/latest"); + let revisionId = await new CallAPI().GetData(request, "org/unauthorize/revision/latest"); salaryPeriod.revisionId = revisionId; await this.salaryPeriodRepository.save(salaryPeriod); @@ -902,15 +901,6 @@ export class SalaryPeriodController extends Controller { }), ); - let salaryProfileOld: SalaryProfile[] = []; - if (snapshot == "SNAP2") { - const salaryOrgOld = await this.salaryOrgRepository.findOne({ - where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" }, - relations: ["salaryProfiles"], - }); - if (salaryOrgOld != null) salaryProfileOld = salaryOrgOld.salaryProfiles; - } - await Promise.all( orgProfiles.map(async (profile: any) => { let group = "GROUP1"; @@ -944,9 +934,16 @@ export class SalaryPeriodController extends Controller { salaryProfileNew.lastUpdateFullName = request.user.name; if (snapshot == "SNAP2") { - const salaryOld = salaryProfileOld.find( - (x) => x.citizenId == salaryProfileNew.citizenId, - ); + const salaryOrgOld = await this.salaryOrgRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" }, + }); + const salaryOld = await this.salaryProfileRepository.findOne({ + where: { + citizenId: salaryProfileNew.citizenId, + salaryOrgId: In(salaryOrgOld.map((x) => x.id)), + }, + }); + salaryProfileNew.type = salaryOld == null ? "PENDING" : salaryOld.type; salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount; salaryProfileNew.amountSpecial = salaryOld == null ? 0 : salaryOld.amountSpecial; salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse; @@ -962,14 +959,96 @@ export class SalaryPeriodController extends Controller { where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, relations: ["salaryProfiles"], }); - await Promise.all( - salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => { - _salaryOrg.total = _salaryOrg.salaryProfiles.length; - _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); - _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; - await this.salaryOrgRepository.save(_salaryOrg); - }), - ); + if (salaryPeriod.period == "OCT") { + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: salaryPeriod.year, + period: "APR", + }, + }); + await Promise.all( + salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => { + let totalAmount = 0; + 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"); + } + + if (snapshot == "SNAP2") { + const salaryOrgSnap1 = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriod.id, + rootId: _salaryOrg.rootId, + group: _salaryOrg.group, + snapshot: "SNAP1", + }, + }); + if (salaryOrgSnap1 == null) { + const totalProfile = Extension.sumObjectValues(_salaryOrg.salaryProfiles, "amount"); + _salaryOrg.currentAmount = totalProfile; + _salaryOrg.sixPercentAmount = totalProfile * 0.06; + _salaryOrg.spentAmount = totalAmount; + } else { + _salaryOrg.currentAmount = salaryOrgSnap1.currentAmount; + _salaryOrg.sixPercentAmount = salaryOrgSnap1.sixPercentAmount; + _salaryOrg.spentAmount = salaryOrgSnap1.spentAmount; + _salaryOrg.useAmount = salaryOrgSnap1.useAmount; + _salaryOrg.remainingAmount = salaryOrgSnap1.remainingAmount; + } + } else { + const totalProfile = Extension.sumObjectValues(_salaryOrg.salaryProfiles, "amount"); + _salaryOrg.currentAmount = totalProfile; + _salaryOrg.sixPercentAmount = totalProfile * 0.06; + _salaryOrg.spentAmount = totalAmount; + } + + await this.salaryOrgRepository.save(_salaryOrg); + }), + ); + } else { + await Promise.all( + salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => { + if (snapshot == "SNAP2") { + const salaryOrgSnap1 = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriod.id, + rootId: _salaryOrg.rootId, + group: _salaryOrg.group, + snapshot: "SNAP1", + }, + }); + if (salaryOrgSnap1 == null) { + _salaryOrg.total = _salaryOrg.salaryProfiles.length; + _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); + _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; + } else { + _salaryOrg.total = salaryOrgSnap1.total; + _salaryOrg.fifteenPercent = salaryOrgSnap1.fifteenPercent; + _salaryOrg.fifteenPoint = salaryOrgSnap1.fifteenPoint; + _salaryOrg.quantityUsed = salaryOrgSnap1.quantityUsed; + _salaryOrg.remainQuota = salaryOrgSnap1.remainQuota; + } + } else { + _salaryOrg.total = _salaryOrg.salaryProfiles.length; + _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); + _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; + } + + await this.salaryOrgRepository.save(_salaryOrg); + }), + ); + } return new HttpSuccess(); } @@ -978,17 +1057,23 @@ export class SalaryPeriodController extends Controller { * Cronjob SalaryPeriod */ async CronjobSalaryPeriod() { + //bright const current = new Date(); let salaryPeriod: any; - // console.log(current.getDate(), current.getMonth() , current.getFullYear()) + let request: any; + // request = express.request; + // console.log("request: ", request); if (current.getDate() == 1 && current.getMonth() == 2) { salaryPeriod = await this.salaryPeriodRepository.findOne({ where: { year: current.getFullYear(), - period: "MAR", + period: "APR", isActive: true, }, }); + if (salaryPeriod) { + this.SnapshotSalarys("SNAP1", salaryPeriod.id, request); + } } else if (current.getDate() == 1 && current.getMonth() == 3) { salaryPeriod = await this.salaryPeriodRepository.findOne({ where: { @@ -997,14 +1082,20 @@ export class SalaryPeriodController extends Controller { isActive: true, }, }); + if (salaryPeriod) { + this.SnapshotSalarys("SNAP2", salaryPeriod.id, request); + } } else if (current.getDate() == 1 && current.getMonth() == 8) { salaryPeriod = await this.salaryPeriodRepository.findOne({ where: { year: current.getFullYear(), - period: "SEP", + period: "OCT", isActive: true, }, }); + if (salaryPeriod) { + this.SnapshotSalarys("SNAP1", salaryPeriod.id, request); + } } else if (current.getDate() == 1 && current.getMonth() == 9) { salaryPeriod = await this.salaryPeriodRepository.findOne({ where: { @@ -1013,6 +1104,226 @@ export class SalaryPeriodController extends Controller { isActive: true, }, }); + if (salaryPeriod) { + this.SnapshotSalarys("SNAP2", salaryPeriod.id, request); + } + } + } + + public async SnapshotSalarys(snapshot: string, salaryPeriodId: string, request: any) { + snapshot = snapshot.toLocaleUpperCase(); + const salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { id: salaryPeriodId }, + }); + if (!salaryPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบรอบการสร้างเงินเดือน"); + } + + const salaryOrg = await this.salaryOrgRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, + }); + const salaryProfile = await this.salaryProfileRepository.find({ + where: { salaryOrgId: In(salaryOrg.map((x) => x.id)) }, + }); + await this.salaryOrgRepository.remove(salaryOrg); + await this.salaryProfileRepository.remove(salaryProfile); + + let orgs = await new CallAPI().GetData(request, "org/unauthorize/active/root/id"); + let orgProfiles: any; + await new CallAPI() + .PostData(request, "org/unauthorize/profile/salary/gen", { + page: 1, + pageSize: 100, + keyword: "", + }) + .then((x) => { + orgProfiles = x.data; + }); + if (orgProfiles.total > 100) { + const page = Math.ceil(orgProfiles.total.length / 100); + for (let index = 2; index <= page; index++) { + await new CallAPI() + .PostData(request, "org/unauthorize/profile/salary/gen", { + page: index, + pageSize: 100, + keyword: "", + }) + .then((x) => { + Array.prototype.push.apply(orgProfiles, x.data); + }); + } + } + let revisionId = await new CallAPI().GetData(request, "org/unauthorize/revision/latest"); + + salaryPeriod.revisionId = revisionId; + await this.salaryPeriodRepository.save(salaryPeriod); + + await Promise.all( + orgs.map(async (rootId: string) => { + let salaryOrgNew = Object.assign(new SalaryOrg()); + salaryOrgNew.salaryPeriodId = salaryPeriod.id; + salaryOrgNew.status = "PENDING"; + salaryOrgNew.rootId = rootId; + salaryOrgNew.revisionId = salaryPeriod.revisionId; + salaryOrgNew.snapshot = snapshot; + salaryOrgNew.group = "GROUP1"; + salaryOrgNew.createdUserId = request.user.sub; + salaryOrgNew.createdFullName = request.user.name; + salaryOrgNew.lastUpdateUserId = request.user.sub; + salaryOrgNew.lastUpdateFullName = request.user.name; + await this.salaryOrgRepository.save(salaryOrgNew); + delete salaryOrgNew.id; + salaryOrgNew.group = "GROUP2"; + await this.salaryOrgRepository.save(salaryOrgNew); + }), + ); + + await Promise.all( + orgProfiles.map(async (profile: any) => { + let group = "GROUP1"; + if ( + (profile.posType == "ทั่วไป" && profile.posLevel == "ทักษะพิเศษ") || + (profile.posType == "วิชาการ" && profile.posLevel == "เชี่ยวชาญ") || + (profile.posType == "วิชาการ" && profile.posLevel == "ทรงคุณวุฒิ") || + (profile.posType == "อำนวยการ" && profile.posLevel == "สูง") || + (profile.posType == "บริหาร" && profile.posLevel == "ต้น") || + (profile.posType == "บริหาร" && profile.posLevel == "สูง") + ) { + group = "GROUP2"; + } + const salaryOrgNew = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriod.id, + rootId: profile.rootId, + snapshot: snapshot, + group: group, + }, + }); + + if (salaryOrgNew != null) { + let salaryProfileNew = Object.assign(new SalaryProfile(), profile); + salaryProfileNew.salaryOrgId = salaryOrgNew.id; + salaryProfileNew.revisionId = salaryPeriod.revisionId; + salaryProfileNew.createdUserId = request.user.sub; + salaryProfileNew.createdFullName = request.user.name; + salaryProfileNew.lastUpdateUserId = request.user.sub; + salaryProfileNew.lastUpdateFullName = request.user.name; + + if (snapshot == "SNAP2") { + const salaryOrgOld = await this.salaryOrgRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: "SNAP1" }, + }); + const salaryOld = await this.salaryProfileRepository.findOne({ + where: { + citizenId: salaryProfileNew.citizenId, + salaryOrgId: In(salaryOrgOld.map((x) => x.id)), + }, + }); + salaryProfileNew.type = salaryOld == null ? 0 : salaryOld.type; + salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount; + salaryProfileNew.amountSpecial = salaryOld == null ? 0 : salaryOld.amountSpecial; + salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse; + salaryProfileNew.positionSalaryAmount = + salaryOld == null ? 0 : salaryOld.positionSalaryAmount; + } + await this.salaryProfileRepository.save(salaryProfileNew); + } + }), + ); + + const salaryOrgNew = await this.salaryOrgRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, + relations: ["salaryProfiles"], + }); + if (salaryPeriod.period == "OCT") { + const salaryPeriodAPROld = await this.salaryPeriodRepository.findOne({ + where: { + year: salaryPeriod.year, + period: "APR", + }, + }); + await Promise.all( + salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => { + let totalAmount = 0; + 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"); + } + + if (snapshot == "SNAP2") { + const salaryOrgSnap1 = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriod.id, + rootId: _salaryOrg.rootId, + group: _salaryOrg.group, + snapshot: "SNAP1", + }, + }); + if (salaryOrgSnap1 == null) { + const totalProfile = Extension.sumObjectValues(_salaryOrg.salaryProfiles, "amount"); + _salaryOrg.currentAmount = totalProfile; + _salaryOrg.sixPercentAmount = totalProfile * 0.06; + _salaryOrg.spentAmount = totalAmount; + } else { + _salaryOrg.currentAmount = salaryOrgSnap1.currentAmount; + _salaryOrg.sixPercentAmount = salaryOrgSnap1.sixPercentAmount; + _salaryOrg.spentAmount = salaryOrgSnap1.spentAmount; + _salaryOrg.useAmount = salaryOrgSnap1.useAmount; + _salaryOrg.remainingAmount = salaryOrgSnap1.remainingAmount; + } + } else { + const totalProfile = Extension.sumObjectValues(_salaryOrg.salaryProfiles, "amount"); + _salaryOrg.currentAmount = totalProfile; + _salaryOrg.sixPercentAmount = totalProfile * 0.06; + _salaryOrg.spentAmount = totalAmount; + } + + await this.salaryOrgRepository.save(_salaryOrg); + }), + ); + } else { + await Promise.all( + salaryOrgNew.map(async (_salaryOrg: SalaryOrg) => { + if (snapshot == "SNAP2") { + const salaryOrgSnap1 = await this.salaryOrgRepository.findOne({ + where: { + salaryPeriodId: salaryPeriod.id, + rootId: _salaryOrg.rootId, + group: _salaryOrg.group, + snapshot: "SNAP1", + }, + }); + if (salaryOrgSnap1 == null) { + _salaryOrg.total = _salaryOrg.salaryProfiles.length; + _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); + _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; + } else { + _salaryOrg.total = salaryOrgSnap1.total; + _salaryOrg.fifteenPercent = salaryOrgSnap1.fifteenPercent; + _salaryOrg.fifteenPoint = salaryOrgSnap1.fifteenPoint; + _salaryOrg.quantityUsed = salaryOrgSnap1.quantityUsed; + _salaryOrg.remainQuota = salaryOrgSnap1.remainQuota; + } + } else { + _salaryOrg.total = _salaryOrg.salaryProfiles.length; + _salaryOrg.fifteenPercent = Math.floor((_salaryOrg.salaryProfiles.length * 15) / 100); + _salaryOrg.fifteenPoint = (_salaryOrg.salaryProfiles.length * 15) % 100; + } + + await this.salaryOrgRepository.save(_salaryOrg); + }), + ); } } }