From 97551dc0963addc79eb0eb130af4b86572f02742 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Wed, 28 Feb 2024 17:58:39 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=84=E0=B8=B3=E0=B8=99=E0=B8=A7=E0=B8=99?= =?UTF-8?q?=E0=B9=80=E0=B8=87=E0=B8=B4=E0=B8=99=E0=B9=80=E0=B8=94=E0=B8=B7?= =?UTF-8?q?=E0=B8=AD=E0=B8=99=E0=B8=A2=E0=B9=89=E0=B8=AD=E0=B8=99=E0=B8=AB?= =?UTF-8?q?=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'`); + } + +}