diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index e1a6545..3918c47 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -29,6 +29,8 @@ import { Salarys } from "../entities/Salarys"; import { SalaryRanks } from "../entities/SalaryRanks"; import CallAPI from "../interfaces/call-api"; import * as express from "express"; +import { Int32 } from "typeorm/browser"; + @Route("api/v1/salary/period") @Tags("Salary") @Security("bearerAuth") @@ -138,11 +140,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(); } @@ -241,7 +258,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(); } @@ -268,15 +298,30 @@ 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(); } /** - * API แก้ไขเงินเดือน + * API แก้ไขขั้น * - * @summary SLR_025 - แก้ไขเงินเดือน #24 + * @summary SLR_025 - แก้ไขขั้น #24 * * @param {string} id profile Id * @param {string} type ประเภทการเลื่อน NONE->ไม่ได้เลื่อน HAFT->ครึ่งขั้น FULL->1ขั้น FULLHAFT->1.5ขั้น @@ -455,6 +500,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, @@ -479,7 +539,8 @@ export class SalaryPeriodController extends Controller { ); } - const salaryProfile = Object.assign(new SalaryProfile(), requestBody); + let salaryProfile: any = Object.assign(new SalaryProfile(), requestBody); + delete salaryProfile.id; salaryProfile.type = salaryProfile.type.toUpperCase(); //Type & Level const type = await this.posTypeRepository.findOne({ @@ -556,7 +617,9 @@ export class SalaryPeriodController extends Controller { : salaryRanks.salaryFullHalf - salaryProfile.amount; salaryProfile.positionSalaryAmount = salaryRanks == null ? 0 : salaryRanks.salaryFullHalf; } else { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทการเลื่อนขึ้นเงินเดือนไม่ถูกต้อง"); + salaryProfile.amountSpecial = 0; + salaryProfile.amountUse = 0; + salaryProfile.positionSalaryAmount = 0; } salaryProfile.salaryOrgId = salaryOrg.id; @@ -747,9 +810,9 @@ export class SalaryPeriodController extends Controller { } /** - * API รายการรอบเงินเดือน + * API snapshot salary * - * @summary SLR_020 - รายการรอบเงินเดือน #20 + * @summary snapshot salary * */ @Get("snapshot/{snapshot}/{salaryPeriodId}") @@ -772,27 +835,25 @@ export class SalaryPeriodController extends Controller { const salaryProfile = await this.salaryProfileRepository.find({ where: { salaryOrgId: In(salaryOrg.map((x) => x.id)) }, }); - await this.salaryOrgRepository.remove(salaryOrg); await this.salaryProfileRepository.remove(salaryProfile); + await this.salaryOrgRepository.remove(salaryOrg); let orgs = await new CallAPI().GetData(request, "org/active/root/id"); - let orgProfiles: any; - await new CallAPI() - .PostData(request, "org/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); + let total = 1000; + let _orgProfiles = await new CallAPI().PostData(request, "org/profile/salary/gen", { + page: 1, + pageSize: 1000, + keyword: "", + }); + let orgProfiles = _orgProfiles.data; + total = _orgProfiles.total; + if (total > 1000) { + const page = Math.ceil(total / 1000); for (let index = 2; index <= page; index++) { await new CallAPI() .PostData(request, "org/profile/salary/gen", { page: index, - pageSize: 100, + pageSize: 1000, keyword: "", }) .then((x) => { @@ -808,6 +869,7 @@ export class SalaryPeriodController extends Controller { await Promise.all( orgs.map(async (rootId: string) => { let salaryOrgNew = Object.assign(new SalaryOrg()); + delete salaryOrgNew.id; salaryOrgNew.salaryPeriodId = salaryPeriod.id; salaryOrgNew.status = "PENDING"; salaryOrgNew.rootId = rootId; @@ -858,6 +920,7 @@ export class SalaryPeriodController extends Controller { if (salaryOrgNew != null) { let salaryProfileNew = Object.assign(new SalaryProfile(), profile); + delete profile.id; salaryProfileNew.salaryOrgId = salaryOrgNew.id; salaryProfileNew.revisionId = salaryPeriod.revisionId; salaryProfileNew.createdUserId = request.user.sub; diff --git a/src/entities/SalaryOrg.ts b/src/entities/SalaryOrg.ts index 4ebad1d..eebc772 100644 --- a/src/entities/SalaryOrg.ts +++ b/src/entities/SalaryOrg.ts @@ -37,11 +37,13 @@ export class SalaryOrg extends EntityBase { snapshot: string; @Column({ + default: 0, comment: "จำนวนคนทั้งหมด", }) total: number; @Column({ + default: 0, comment: "15%ของจำนวนคน(จำนวนเต็ม)", }) fifteenPercent: number; @@ -59,6 +61,54 @@ export class SalaryOrg extends EntityBase { }) group: string; + @Column({ + comment: "เลือกไปแล้ว", + default: 0, + }) + quantityUsed: number; + + @Column({ + comment: "คงเหลือโควตา", + default: 0, + }) + remainQuota: number; + + @Column({ + comment: "จำนวนเงินคนครองปัจจุบัน", + type: "double", + default: 0, + }) + currentAmount: number; + + @Column({ + comment: "วงเงิน6%", + type: "double", + default: 0, + }) + sixPercentAmount: number; + + @Column({ + comment: "ยอดเงินที่ใช้ไป", + type: "double", + default: 0, + }) + spentAmount: number; + + @Column({ + comment: "ใช้ไปเท่าไหร่", + type: "double", + default: 0, + }) + useAmount: number; + + @Column({ + comment: "เหลือเท่าไหร่", + type: "double", + default: 0, + }) + remainingAmount: number; + + @ManyToOne(() => SalaryPeriod, (salaryPeriod) => salaryPeriod.salaryOrgs) @JoinColumn({ name: "salaryPeriodId" }) salaryPeriod: SalaryPeriod; diff --git a/src/entities/SalaryProfile.ts b/src/entities/SalaryProfile.ts index ff93597..3710588 100644 --- a/src/entities/SalaryProfile.ts +++ b/src/entities/SalaryProfile.ts @@ -238,31 +238,31 @@ export class SalaryProfile extends EntityBase { comment: "ผลการประเมิน", default: false, }) - isResult: boolean; + result: boolean; @Column({ comment: "ระยะเวลา", default: false, }) - isDuration: boolean; + duration: boolean; @Column({ comment: "การลงโทษ", default: false, }) - isPunish: boolean; + punish: boolean; @Column({ comment: "พักราชการ", default: false, }) - isRetired: boolean; + retired: boolean; @Column({ comment: "ขาดราชการ", default: false, }) - isRetired2: boolean; + retired2: boolean; @ManyToOne(() => SalaryOrg, (salaryOrg) => salaryOrg.salaryProfiles) @JoinColumn({ name: "salaryOrgId" }) @@ -289,16 +289,16 @@ export class CreateSalaryProfile { citizenId: string; @Column() - posMasterNoPrefix: string; + posMasterNoPrefix: string | null; @Column() posMasterNo: number; @Column() - posMasterNoSuffix: string; + posMasterNoSuffix: string | null; @Column() - orgShortName: string; + orgShortName: string | null; @Column() position: string; @@ -310,53 +310,53 @@ export class CreateSalaryProfile { posLevel: string; @Column() - posExecutive: string; + posExecutive: string | null; @Column() amount: number; @Column("uuid") - rootId: string; + rootId: string | null; @Column() - root: string; + root: string | null; @Column("uuid") - child1Id: string; + child1Id: string | null; @Column() - child1: string; + child1: string | null; @Column("uuid") - child2Id: string; + child2Id: string | null; @Column() - child2: string; + child2: string | null; @Column("uuid") - child3Id: string; + child3Id: string | null; @Column() - child3: string; + child3: string | null; @Column("uuid") - child4Id: string; + child4Id: string | null; @Column() - child4: string; + child4: string | null; @Column() - isResult: boolean; + result: boolean; @Column() - isDuration: boolean; + duration: boolean; @Column() - isPunish: boolean; + punish: boolean; @Column() - isRetired: boolean; + retired: boolean; @Column() - isRetired2: boolean; + retired2: boolean; } diff --git a/src/interfaces/call-api.ts b/src/interfaces/call-api.ts index 86ed525..8608230 100644 --- a/src/interfaces/call-api.ts +++ b/src/interfaces/call-api.ts @@ -35,17 +35,12 @@ class CallAPI { const token = request.headers.authorization; const url = process.env.API + path; try { - console.log("response"); - console.log(url); - console.log("response"); - const response = await axios.post(url, { + const response = await axios.post(url, sendData, { headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", }, - json: sendData, }); - console.log(response); return response.data.result; } catch (error) { throw error; diff --git a/src/migration/1709105846886-update_table_salaryProfile_add_revision3.ts b/src/migration/1709105846886-update_table_salaryProfile_add_revision3.ts new file mode 100644 index 0000000..cffaf25 --- /dev/null +++ b/src/migration/1709105846886-update_table_salaryProfile_add_revision3.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableSalaryProfileAddRevision31709105846886 implements MigrationInterface { + name = 'UpdateTableSalaryProfileAddRevision31709105846886' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryOrg\` CHANGE \`total\` \`total\` int NOT NULL COMMENT 'จำนวนคนทั้งหมด' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` CHANGE \`fifteenPercent\` \`fifteenPercent\` int NOT NULL COMMENT '15%ของจำนวนคน(จำนวนเต็ม)' DEFAULT '0'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryOrg\` CHANGE \`fifteenPercent\` \`fifteenPercent\` int NOT NULL COMMENT '15%ของจำนวนคน(จำนวนเต็ม)'`); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` CHANGE \`total\` \`total\` int NOT NULL COMMENT 'จำนวนคนทั้งหมด'`); + } + +} diff --git a/src/migration/1709112744213-update_table_salaryProfile_add_retired2.ts b/src/migration/1709112744213-update_table_salaryProfile_add_retired2.ts new file mode 100644 index 0000000..f2ae914 --- /dev/null +++ b/src/migration/1709112744213-update_table_salaryProfile_add_retired2.ts @@ -0,0 +1,46 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableSalaryProfileAddRetired21709112744213 implements MigrationInterface { + name = 'UpdateTableSalaryProfileAddRetired21709112744213' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`isDuration\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`isPunish\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`isResult\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`isRetired\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`isRetired2\``); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` ADD \`quantityUsed\` int NOT NULL COMMENT 'เลือกไปแล้ว' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` ADD \`remainQuota\` int NOT NULL COMMENT 'คงเหลือโควตา' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` ADD \`currentAmount\` double NOT NULL COMMENT 'จำนวนเงินคนครองปัจจุบัน' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` ADD \`sixPercentAmount\` double NOT NULL COMMENT 'วงเงิน6%' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` ADD \`spentAmount\` double NOT NULL COMMENT 'ยอดเงินที่ใช้ไป' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` ADD \`useAmount\` double NOT NULL COMMENT 'ใช้ไปเท่าไหร่' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` ADD \`remainingAmount\` double NOT NULL COMMENT 'เหลือเท่าไหร่' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`result\` tinyint NOT NULL COMMENT 'ผลการประเมิน' DEFAULT 0`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`duration\` tinyint NOT NULL COMMENT 'ระยะเวลา' DEFAULT 0`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`punish\` tinyint NOT NULL COMMENT 'การลงโทษ' DEFAULT 0`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`retired\` tinyint NOT NULL COMMENT 'พักราชการ' DEFAULT 0`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`retired2\` tinyint NOT NULL COMMENT 'ขาดราชการ' DEFAULT 0`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`retired2\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`retired\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`punish\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`duration\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` DROP COLUMN \`result\``); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` DROP COLUMN \`remainingAmount\``); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` DROP COLUMN \`useAmount\``); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` DROP COLUMN \`spentAmount\``); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` DROP COLUMN \`sixPercentAmount\``); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` DROP COLUMN \`currentAmount\``); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` DROP COLUMN \`remainQuota\``); + await queryRunner.query(`ALTER TABLE \`salaryOrg\` DROP COLUMN \`quantityUsed\``); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`isRetired2\` tinyint NOT NULL COMMENT 'ขาดราชการ' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`isRetired\` tinyint NOT NULL COMMENT 'พักราชการ' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`isResult\` tinyint NOT NULL COMMENT 'ผลการประเมิน' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`isPunish\` tinyint NOT NULL COMMENT 'การลงโทษ' DEFAULT '0'`); + await queryRunner.query(`ALTER TABLE \`salaryProfile\` ADD \`isDuration\` tinyint NOT NULL COMMENT 'ระยะเวลา' DEFAULT '0'`); + } + +}