From 97551dc0963addc79eb0eb130af4b86572f02742 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Wed, 28 Feb 2024 17:58:39 +0700 Subject: [PATCH 1/3] =?UTF-8?q?=E0=B8=84=E0=B8=B3=E0=B8=99=E0=B8=A7?= =?UTF-8?q?=E0=B8=99=E0=B9=80=E0=B8=87=E0=B8=B4=E0=B8=99=E0=B9=80=E0=B8=94?= =?UTF-8?q?=E0=B8=B7=E0=B8=AD=E0=B8=99=E0=B8=A2=E0=B9=89=E0=B8=AD=E0=B8=99?= =?UTF-8?q?=E0=B8=AB=E0=B8=A5=E0=B8=B1=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/SalaryPeriodController.ts | 110 +++++++++++++++-- src/entities/SalaryProfile.ts | 25 ++-- src/interfaces/extension.ts | 113 +++++++++++------- ...update_table_salaryProfile_add_retired3.ts | 32 +++++ 4 files changed, 215 insertions(+), 65 deletions(-) create mode 100644 src/migration/1709113262974-update_table_salaryProfile_add_retired3.ts diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 92140f8..f31d248 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -121,10 +121,14 @@ export class SalaryPeriodController extends Controller { const data = { total: salaryOrg.total, fifteenPercent: salaryOrg.fifteenPercent, - chosen: salaryOrg.salaryProfiles.filter((x) => x.posType == "FULL").length, - remaining: - salaryOrg.fifteenPercent - - salaryOrg.salaryProfiles.filter((x) => x.posType == "FULL").length, + chosen: salaryOrg.quantityUsed, + remaining: salaryOrg.remainQuota, + currentAmount: salaryOrg.currentAmount, + sixPercentAmount: salaryOrg.sixPercentAmount, + spentAmount: salaryOrg.spentAmount, + sixPercentSpentAmount: salaryOrg.sixPercentAmount - salaryOrg.spentAmount, + useAmount: salaryOrg.useAmount, + remainingAmount: salaryOrg.remainingAmount, }; return new HttpSuccess(data); } @@ -888,14 +892,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(); } diff --git a/src/entities/SalaryProfile.ts b/src/entities/SalaryProfile.ts index 3710588..47606f2 100644 --- a/src/entities/SalaryProfile.ts +++ b/src/entities/SalaryProfile.ts @@ -235,34 +235,39 @@ export class SalaryProfile extends EntityBase { child4: string; @Column({ + nullable: true, comment: "ผลการประเมิน", - default: false, + default: null, }) - result: boolean; + result: string; @Column({ + nullable: true, comment: "ระยะเวลา", - default: false, + default: null, }) - duration: boolean; + duration: string; @Column({ + nullable: true, comment: "การลงโทษ", - default: false, + default: null, }) - punish: boolean; + punish: string; @Column({ + nullable: true, comment: "พักราชการ", - default: false, + default: null, }) - retired: boolean; + retired: string; @Column({ + nullable: true, comment: "ขาดราชการ", - default: false, + default: null, }) - retired2: boolean; + retired2: string; @ManyToOne(() => SalaryOrg, (salaryOrg) => salaryOrg.salaryProfiles) @JoinColumn({ name: "salaryOrgId" }) diff --git a/src/interfaces/extension.ts b/src/interfaces/extension.ts index 25d8764..1355587 100644 --- a/src/interfaces/extension.ts +++ b/src/interfaces/extension.ts @@ -1,54 +1,81 @@ class Extension { - - public static ToThaiMonth(value: number) { - switch (value) { - case 1: return "มกราคม"; - case 2: return "กุมภาพันธ์"; - case 3: return "มีนาคม"; - case 4: return "เมษายน"; - case 5: return "พฤษภาคม"; - case 6: return "มิถุนายน"; - case 7: return "กรกฎาคม"; - case 8: return "สิงหาคม"; - case 9: return "กันยายน"; - case 10: return "ตุลาคม"; - case 11: return "พฤศจิกายน"; - case 12: return "ธันวาคม"; - default: return ""; - } + public static ToThaiMonth(value: number) { + switch (value) { + case 1: + return "มกราคม"; + case 2: + return "กุมภาพันธ์"; + case 3: + return "มีนาคม"; + case 4: + return "เมษายน"; + case 5: + return "พฤษภาคม"; + case 6: + return "มิถุนายน"; + case 7: + return "กรกฎาคม"; + case 8: + return "สิงหาคม"; + case 9: + return "กันยายน"; + case 10: + return "ตุลาคม"; + case 11: + return "พฤศจิกายน"; + case 12: + return "ธันวาคม"; + default: + return ""; } + } - public static ToThaiYear(value: number) { - if (value < 2400) - return value + 543; - else return value; - } + public static ToThaiYear(value: number) { + if (value < 2400) return value + 543; + else return value; + } - public static ToCeYear(value: number) { - if (value >= 2400) - return value - 543; - else return value; - } + public static ToCeYear(value: number) { + if (value >= 2400) return value - 543; + else return value; + } - public static ToThaiNumber(value: string) { - let arabicNumbers = "0123456789"; - let thaiNumbers = "๐๑๒๓๔๕๖๗๘๙"; - let result = ""; - for (let digit of value) { - let index = arabicNumbers.indexOf(digit); - if (index >= 0) { - result += thaiNumbers[index]; - } else { - result += digit; - } - } - return result; + public static ToThaiNumber(value: string) { + let arabicNumbers = "0123456789"; + let thaiNumbers = "๐๑๒๓๔๕๖๗๘๙"; + let result = ""; + for (let digit of value) { + let index = arabicNumbers.indexOf(digit); + if (index >= 0) { + result += thaiNumbers[index]; + } else { + result += digit; + } } + return result; + } - public static ToThaiFullDate(value: Date) { - let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear(); - return "วันที่ "+ value.getDate() +" เดือน "+ Extension.ToThaiMonth(value.getMonth() + 1)+ " พ.ศ. " + yy; + public static ToThaiFullDate(value: Date) { + let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear(); + return ( + "วันที่ " + + value.getDate() + + " เดือน " + + Extension.ToThaiMonth(value.getMonth() + 1) + + " พ.ศ. " + + yy + ); + } + + public static sumObjectValues(array: any, propertyName: any) { + let sum = 0; + for (let i = 0; i < array.length; i++) { + if (array[i][propertyName] !== undefined) { + sum += array[i][propertyName]; + } } + return sum; + } } export default Extension; diff --git a/src/migration/1709113262974-update_table_salaryProfile_add_retired3.ts b/src/migration/1709113262974-update_table_salaryProfile_add_retired3.ts new file mode 100644 index 0000000..5352514 --- /dev/null +++ b/src/migration/1709113262974-update_table_salaryProfile_add_retired3.ts @@ -0,0 +1,32 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableSalaryProfileAddRetired31709113262974 implements MigrationInterface { + name = 'UpdateTableSalaryProfileAddRetired31709113262974' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`result\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`result\` varchar(255) NULL COMMENT 'ผลการประเมิน'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`duration\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`duration\` varchar(255) NULL COMMENT 'ระยะเวลา'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`punish\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`punish\` varchar(255) NULL COMMENT 'การลงโทษ'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`retired\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`retired\` varchar(255) NULL COMMENT 'พักราชการ'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`retired2\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`retired2\` varchar(255) NULL COMMENT 'ขาดราชการ'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`retired2\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`retired2\` tinyint NOT NULL COMMENT 'ขาดราชการ' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`retired\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`retired\` tinyint NOT NULL COMMENT 'พักราชการ' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`punish\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`punish\` tinyint NOT NULL COMMENT 'การลงโทษ' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`duration\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`duration\` tinyint NOT NULL COMMENT 'ระยะเวลา' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`result\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`result\` tinyint NOT NULL COMMENT 'ผลการประเมิน' DEFAULT '0'`); + } + +} From 42acedebf583f37409ec0755a981f39616444af5 Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 28 Feb 2024 18:07:38 +0700 Subject: [PATCH 2/3] 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); + }), + ); + } } From ddd4eadfcf73100d257a77f5a1f6d4acf4296971 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Wed, 28 Feb 2024 20:41:27 +0700 Subject: [PATCH 3/3] no message --- src/controllers/SalaryPeriodController.ts | 290 ++++++++++++++-------- 1 file changed, 182 insertions(+), 108 deletions(-) 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); + }), + ); + } } }