From aeb034a654916a774e0b56a811aaf8673b1a0af1 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 28 Feb 2024 18:03:36 +0700 Subject: [PATCH 1/2] =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88?= =?UTF-8?q?=E0=B8=A1=20call?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/SalaryPeriodController.ts | 60 ++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 92140f8..bd33cba 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -139,11 +139,26 @@ export class SalaryPeriodController extends Controller { @Delete("profile/{id}") async deleteSalaryProfile(@Path() id: string) { const salaryProfile = await this.salaryProfileRepository.findOne({ + relations: ["salaryOrg"], where: { id: id }, }); if (!salaryProfile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการขอเงินเดือนผู้ใช้งานนี้ในระบบ"); } + // หาจำนวน 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); + } + await this.salaryProfileRepository.remove(salaryProfile); return new HttpSuccess(); } @@ -242,7 +257,20 @@ export class SalaryPeriodController extends Controller { } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทการเลื่อนขึ้นเงินเดือนไม่ถูกต้อง"); } - + // หาจำนวน 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); + } + await this.salaryProfileRepository.save(salaryProfile); return new HttpSuccess(); } @@ -269,6 +297,21 @@ export class SalaryPeriodController extends Controller { if (!salaryOrg) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มการขอเลื่อนเงินเดือน"); } + + //หาจำนวน 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); + } + salaryProfile.salaryOrgId = salaryOrg.id; await this.salaryProfileRepository.save(salaryProfile); return new HttpSuccess(); @@ -456,6 +499,21 @@ export class SalaryPeriodController extends Controller { if (!salaryOrg) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบรอบการขึ้นเงินเดือน"); } + + //หาจำนวน 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); + } + const salaryOrgAll = await this.salaryOrgRepository.find({ where: { salaryPeriodId: salaryOrg.salaryPeriodId, From 42acedebf583f37409ec0755a981f39616444af5 Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 28 Feb 2024 18:07:38 +0700 Subject: [PATCH 2/2] no message --- src/controllers/SalaryPeriodController.ts | 244 +++++++++++++++++----- 1 file changed, 197 insertions(+), 47 deletions(-) diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index db17580..e1a6545 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -28,7 +28,7 @@ import { PosLevel } from "../entities/PosLevel"; import { Salarys } from "../entities/Salarys"; import { SalaryRanks } from "../entities/SalaryRanks"; import CallAPI from "../interfaces/call-api"; - +import * as express from "express"; @Route("api/v1/salary/period") @Tags("Salary") @Security("bearerAuth") @@ -73,11 +73,11 @@ export class SalaryPeriodController extends Controller { ) == null ? null : salaryPeriod.salaryOrgs.find( - (x) => - x.group == "GROUP1" && - x.rootId == body.rootId && - x.snapshot == body.snapshot.toLocaleUpperCase(), - )?.id, + (x) => + x.group == "GROUP1" && + x.rootId == body.rootId && + x.snapshot == body.snapshot.toLocaleUpperCase(), + )?.id, group2id: salaryPeriod.salaryOrgs.find( (x) => @@ -87,11 +87,11 @@ export class SalaryPeriodController extends Controller { ) == null ? null : salaryPeriod.salaryOrgs.find( - (x) => - x.group == "GROUP2" && - x.rootId == body.rootId && - x.snapshot == body.snapshot.toLocaleUpperCase(), - )?.id, + (x) => + x.group == "GROUP2" && + x.rootId == body.rootId && + x.snapshot == body.snapshot.toLocaleUpperCase(), + )?.id, effectiveDate: salaryPeriod.effectiveDate, period: salaryPeriod.period, }; @@ -212,9 +212,9 @@ export class SalaryPeriodController extends Controller { salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryHalfSpecial; salaryProfile.amountUse = salaryRanks == null || - salaryProfile == null || - salaryRanks.salaryHalf == null || - salaryProfile.amount == null + salaryProfile == null || + salaryRanks.salaryHalf == null || + salaryProfile.amount == null ? 0 : salaryRanks.salaryHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryHalf; @@ -222,9 +222,9 @@ export class SalaryPeriodController extends Controller { salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullSpecial; salaryProfile.amountUse = salaryRanks == null || - salaryProfile == null || - salaryRanks.salaryFull == null || - salaryProfile.amount == null + salaryProfile == null || + salaryRanks.salaryFull == null || + salaryProfile.amount == null ? 0 : salaryRanks.salaryFull - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFull; @@ -232,9 +232,9 @@ export class SalaryPeriodController extends Controller { salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullHalfSpecial; salaryProfile.amountUse = salaryRanks == null || - salaryProfile == null || - salaryRanks.salaryFullHalf == null || - salaryProfile.amount == null + salaryProfile == null || + salaryRanks.salaryFullHalf == null || + salaryProfile.amount == null ? 0 : salaryRanks.salaryFullHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFullHalf; @@ -337,9 +337,9 @@ export class SalaryPeriodController extends Controller { salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryHalfSpecial; salaryProfile.amountUse = salaryRanks == null || - salaryProfile == null || - salaryRanks.salaryHalf == null || - salaryProfile.amount == null + salaryProfile == null || + salaryRanks.salaryHalf == null || + salaryProfile.amount == null ? 0 : salaryRanks.salaryHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryHalf; @@ -347,9 +347,9 @@ export class SalaryPeriodController extends Controller { salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullSpecial; salaryProfile.amountUse = salaryRanks == null || - salaryProfile == null || - salaryRanks.salaryFull == null || - salaryProfile.amount == null + salaryProfile == null || + salaryRanks.salaryFull == null || + salaryProfile.amount == null ? 0 : salaryRanks.salaryFull - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFull; @@ -357,9 +357,9 @@ export class SalaryPeriodController extends Controller { salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullHalfSpecial; salaryProfile.amountUse = salaryRanks == null || - salaryProfile == null || - salaryRanks.salaryFullHalf == null || - salaryProfile.amount == null + salaryProfile == null || + salaryRanks.salaryFullHalf == null || + salaryProfile.amount == null ? 0 : salaryRanks.salaryFullHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFullHalf; @@ -529,9 +529,9 @@ export class SalaryPeriodController extends Controller { salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryHalfSpecial; salaryProfile.amountUse = salaryRanks == null || - salaryProfile == null || - salaryRanks.salaryHalf == null || - salaryProfile.amount == null + salaryProfile == null || + salaryRanks.salaryHalf == null || + salaryProfile.amount == null ? 0 : salaryRanks.salaryHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryHalf; @@ -539,9 +539,9 @@ export class SalaryPeriodController extends Controller { salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullSpecial; salaryProfile.amountUse = salaryRanks == null || - salaryProfile == null || - salaryRanks.salaryFull == null || - salaryProfile.amount == null + salaryProfile == null || + salaryRanks.salaryFull == null || + salaryProfile.amount == null ? 0 : salaryRanks.salaryFull - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFull; @@ -549,9 +549,9 @@ export class SalaryPeriodController extends Controller { salaryProfile.amountSpecial = salaryRanks == null ? 0 : salaryRanks.salaryFullHalfSpecial; salaryProfile.amountUse = salaryRanks == null || - salaryProfile == null || - salaryRanks.salaryFullHalf == null || - salaryProfile.amount == null + salaryProfile == null || + salaryRanks.salaryFullHalf == null || + salaryProfile.amount == null ? 0 : salaryRanks.salaryFullHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFullHalf; @@ -775,9 +775,7 @@ export class SalaryPeriodController extends Controller { await this.salaryOrgRepository.remove(salaryOrg); await this.salaryProfileRepository.remove(salaryProfile); - console.log("11111111111111111111111"); let orgs = await new CallAPI().GetData(request, "org/active/root/id"); - console.log("22222222222222222222222"); let orgProfiles: any; await new CallAPI() .PostData(request, "org/profile/salary/gen", { @@ -788,7 +786,6 @@ export class SalaryPeriodController extends Controller { .then((x) => { orgProfiles = x.data; }); - console.log("333333333333333333333"); if (orgProfiles.total > 100) { const page = Math.ceil(orgProfiles.total.length / 100); for (let index = 2; index <= page; index++) { @@ -803,9 +800,7 @@ export class SalaryPeriodController extends Controller { }); } } - console.log("44444444444444444444444"); let revisionId = await new CallAPI().GetData(request, "org/revision/latest"); - console.log("55555555555555555555555"); salaryPeriod.revisionId = revisionId; await this.salaryPeriodRepository.save(salaryPeriod); @@ -904,18 +899,23 @@ export class SalaryPeriodController extends Controller { /** * Cronjob SalaryPeriod */ - async CronjobSalaryPeriod() { + 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: { @@ -924,14 +924,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: { @@ -940,6 +946,150 @@ 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); + }), + ); + + 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"; + 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 salaryOld = salaryProfileOld.find( + (x) => x.citizenId == salaryProfileNew.citizenId, + ); + 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"], + }); + 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); + }), + ); + } }