diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 694e14e..75a60ad 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -75,11 +75,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) => @@ -89,11 +89,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, }; @@ -150,20 +150,20 @@ export class SalaryPeriodController extends Controller { if (!salaryProfile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลการขอเงินเดือนผู้ใช้งานนี้ในระบบ"); } - // หาจำนวน 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; salaryProfile.salaryOrg.remainQuota = calRemainQuota; await this.salaryOrgRepository.save(salaryProfile?.salaryOrg); } - + await this.salaryProfileRepository.remove(salaryProfile); return new HttpSuccess(); } @@ -233,9 +233,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; @@ -243,9 +243,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; @@ -253,29 +253,29 @@ 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; } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทการเลื่อนขึ้นเงินเดือนไม่ถูกต้อง"); } - // หาจำนวน 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; salaryProfile.salaryOrg.remainQuota = calRemainQuota; await this.salaryOrgRepository.save(salaryProfile?.salaryOrg); } - + await this.salaryProfileRepository.save(salaryProfile); return new HttpSuccess(); } @@ -304,12 +304,12 @@ export class SalaryPeriodController extends Controller { } //หาจำนวน Quota คงเหลือ - if(salaryOrg.snapshot == "SNAP1"){ + 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; @@ -386,9 +386,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; @@ -396,9 +396,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; @@ -406,9 +406,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; @@ -506,12 +506,12 @@ export class SalaryPeriodController extends Controller { } //หาจำนวน Quota คงเหลือ - if(salaryOrg.snapshot == "SNAP1"){ + 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; @@ -594,9 +594,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; @@ -604,9 +604,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; @@ -614,9 +614,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; @@ -841,10 +841,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: "", @@ -855,7 +854,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: "", @@ -865,7 +864,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); @@ -891,15 +890,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"; @@ -933,9 +923,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; @@ -1048,7 +1045,8 @@ export class SalaryPeriodController extends Controller { /** * Cronjob SalaryPeriod */ - async CronjobSalaryPeriod() { //bright + async CronjobSalaryPeriod() { + //bright const current = new Date(); let salaryPeriod: any; let request: any; @@ -1063,7 +1061,7 @@ export class SalaryPeriodController extends Controller { }, }); if (salaryPeriod) { - this.SnapshotSalarys("SNAP1", salaryPeriod.id, request) + this.SnapshotSalarys("SNAP1", salaryPeriod.id, request); } } else if (current.getDate() == 1 && current.getMonth() == 3) { salaryPeriod = await this.salaryPeriodRepository.findOne({ @@ -1074,7 +1072,7 @@ export class SalaryPeriodController extends Controller { }, }); if (salaryPeriod) { - this.SnapshotSalarys("SNAP2", salaryPeriod.id, request) + this.SnapshotSalarys("SNAP2", salaryPeriod.id, request); } } else if (current.getDate() == 1 && current.getMonth() == 8) { salaryPeriod = await this.salaryPeriodRepository.findOne({ @@ -1085,7 +1083,7 @@ export class SalaryPeriodController extends Controller { }, }); if (salaryPeriod) { - this.SnapshotSalarys("SNAP1", salaryPeriod.id, request) + this.SnapshotSalarys("SNAP1", salaryPeriod.id, request); } } else if (current.getDate() == 1 && current.getMonth() == 9) { salaryPeriod = await this.salaryPeriodRepository.findOne({ @@ -1096,16 +1094,12 @@ export class SalaryPeriodController extends Controller { }, }); if (salaryPeriod) { - this.SnapshotSalarys("SNAP2", salaryPeriod.id, request) + this.SnapshotSalarys("SNAP2", salaryPeriod.id, request); } } } - public async SnapshotSalarys( - snapshot: string, - salaryPeriodId: string, - request: any - ) { + public async SnapshotSalarys(snapshot: string, salaryPeriodId: string, request: any) { snapshot = snapshot.toLocaleUpperCase(); const salaryPeriod = await this.salaryPeriodRepository.findOne({ where: { id: salaryPeriodId }, @@ -1173,15 +1167,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"; @@ -1214,9 +1199,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 ? 0 : salaryOld.type; salaryProfileNew.amount = salaryOld == null ? 0 : salaryOld.amount; salaryProfileNew.amountSpecial = salaryOld == null ? 0 : salaryOld.amountSpecial; salaryProfileNew.amountUse = salaryOld == null ? 0 : salaryOld.amountUse; @@ -1232,13 +1224,95 @@ 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); + }), + ); + } } }