From 76f42d1a9618442245fcc3e1187bf3bc6f3a90b9 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Wed, 27 Mar 2024 16:39:45 +0700 Subject: [PATCH] =?UTF-8?q?dashboard=20=E0=B9=80=E0=B8=87=E0=B8=B4?= =?UTF-8?q?=E0=B8=99=E0=B9=80=E0=B8=94=E0=B8=B7=E0=B8=AD=E0=B8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/SalaryPeriodController.ts | 92 +++++++++++++--- .../SalaryPeriodEmployeeController.ts | 101 ++++++++++++++++++ src/entities/SalaryOrg.ts | 10 ++ 3 files changed, 191 insertions(+), 12 deletions(-) diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index f5f4bcd..b0762bb 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -1213,6 +1213,15 @@ export class SalaryPeriodController extends Controller { await this.salaryOrgRepository.remove(salaryOrg); await this.salaryProfileRepository.remove(salaryProfile); + const salaryOrgEmployee = await this.salaryOrgEmployeeRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, + }); + const salaryProfileEmployee = await this.salaryProfileEmployeeRepository.find({ + where: { salaryOrgId: In(salaryOrgEmployee.map((x) => x.id)) }, + }); + await this.salaryProfileEmployeeRepository.remove(salaryProfileEmployee); + await this.salaryOrgEmployeeRepository.remove(salaryOrgEmployee); + let orgs = await new CallAPI().GetData(request, "org/unauthorize/active/root/id"); let revisionId = await new CallAPI().GetData(request, "org/unauthorize/revision/latest"); @@ -1220,11 +1229,29 @@ export class SalaryPeriodController extends Controller { await this.salaryPeriodRepository.save(salaryPeriod); await Promise.all( - orgs.map(async (rootId: string) => { + orgs.map(async (root: any) => { let salaryOrgNew = Object.assign(new SalaryOrg()); salaryOrgNew.salaryPeriodId = salaryPeriod.id; salaryOrgNew.status = "PENDING"; - salaryOrgNew.rootId = rootId; + salaryOrgNew.rootId = root.rootId; + salaryOrgNew.root = root.root; + salaryOrgNew.revisionId = salaryPeriod.revisionId; + salaryOrgNew.snapshot = snapshot; + salaryOrgNew.createdUserId = request.user.sub; + salaryOrgNew.createdFullName = request.user.name; + salaryOrgNew.lastUpdateUserId = request.user.sub; + salaryOrgNew.lastUpdateFullName = request.user.name; + salaryOrgNew.group = "GROUP1"; + await this.salaryOrgRepository.save(salaryOrgNew); + }), + ); + await Promise.all( + orgs.map(async (root: any) => { + let salaryOrgNew = Object.assign(new SalaryOrgEmployee()); + salaryOrgNew.salaryPeriodId = salaryPeriod.id; + salaryOrgNew.status = "PENDING"; + salaryOrgNew.rootId = root.rootId; + salaryOrgNew.root = root.root; salaryOrgNew.revisionId = salaryPeriod.revisionId; salaryOrgNew.snapshot = snapshot; salaryOrgNew.createdUserId = request.user.sub; @@ -1295,6 +1322,15 @@ export class SalaryPeriodController extends Controller { await this.salaryOrgRepository.remove(salaryOrg); await this.salaryProfileRepository.remove(salaryProfile); + const salaryOrgEmployee = await this.salaryOrgEmployeeRepository.find({ + where: { salaryPeriodId: chk_SalaryPeriod.id, snapshot: snapshot }, + }); + const salaryProfileEmployee = await this.salaryProfileEmployeeRepository.find({ + where: { salaryOrgId: In(salaryOrgEmployee.map((x) => x.id)) }, + }); + await this.salaryProfileEmployeeRepository.remove(salaryProfileEmployee); + await this.salaryOrgEmployeeRepository.remove(salaryOrgEmployee); + let orgs = await new CallAPI().GetData(request, "org/unauthorize/active/root/id"); let revisionId = await new CallAPI().GetData(request, "org/unauthorize/revision/latest"); @@ -1302,11 +1338,30 @@ export class SalaryPeriodController extends Controller { await this.salaryPeriodRepository.save(chk_SalaryPeriod); await Promise.all( - orgs.map(async (rootId: string) => { + orgs.map(async (root: any) => { let salaryOrgNew = Object.assign(new SalaryOrg()); salaryOrgNew.salaryPeriodId = chk_SalaryPeriod.id; salaryOrgNew.status = "PENDING"; - salaryOrgNew.rootId = rootId; + salaryOrgNew.rootId = root.rootId; + salaryOrgNew.root = root.root; + salaryOrgNew.revisionId = chk_SalaryPeriod.revisionId; + salaryOrgNew.snapshot = snapshot; + salaryOrgNew.createdUserId = request.user.sub; + salaryOrgNew.createdFullName = request.user.name; + salaryOrgNew.lastUpdateUserId = request.user.sub; + salaryOrgNew.lastUpdateFullName = request.user.name; + salaryOrgNew.group = "GROUP1"; + await this.salaryOrgRepository.save(salaryOrgNew); + }), + ); + + await Promise.all( + orgs.map(async (root: any) => { + let salaryOrgNew = Object.assign(new SalaryOrgEmployee()); + salaryOrgNew.salaryPeriodId = chk_SalaryPeriod.id; + salaryOrgNew.status = "PENDING"; + salaryOrgNew.rootId = root.rootId; + salaryOrgNew.root = root.root; salaryOrgNew.revisionId = chk_SalaryPeriod.revisionId; salaryOrgNew.snapshot = snapshot; salaryOrgNew.createdUserId = request.user.sub; @@ -1591,12 +1646,13 @@ export class SalaryPeriodController extends Controller { await this.salaryPeriodRepository.save(salaryPeriod); await Promise.all( - orgs.map(async (rootId: string) => { + orgs.map(async (root: any) => { let salaryOrgNew = Object.assign(new SalaryOrg()); delete salaryOrgNew.id; salaryOrgNew.salaryPeriodId = salaryPeriod.id; salaryOrgNew.status = "PENDING"; - salaryOrgNew.rootId = rootId; + salaryOrgNew.rootId = root.rootId; + salaryOrgNew.root = root.root; salaryOrgNew.revisionId = salaryPeriod.revisionId; salaryOrgNew.snapshot = snapshot; salaryOrgNew.createdUserId = request.user.sub; @@ -1614,11 +1670,12 @@ export class SalaryPeriodController extends Controller { ); await Promise.all( - orgs.map(async (rootId: string) => { + orgs.map(async (root: any) => { let salaryOrgNew = Object.assign(new SalaryOrgEmployee()); salaryOrgNew.salaryPeriodId = salaryPeriod.id; salaryOrgNew.status = "PENDING"; - salaryOrgNew.rootId = rootId; + salaryOrgNew.rootId = root.rootId; + salaryOrgNew.root = root.root; salaryOrgNew.revisionId = salaryPeriod.revisionId; salaryOrgNew.snapshot = snapshot; salaryOrgNew.createdUserId = request.user.sub; @@ -2004,6 +2061,15 @@ export class SalaryPeriodController extends Controller { await this.salaryOrgRepository.remove(salaryOrg); await this.salaryProfileRepository.remove(salaryProfile); + const salaryOrgEmployee = await this.salaryOrgEmployeeRepository.find({ + where: { salaryPeriodId: salaryPeriod.id, snapshot: snapshot }, + }); + const salaryProfileEmployee = await this.salaryProfileEmployeeRepository.find({ + where: { salaryOrgId: In(salaryOrgEmployee.map((x) => x.id)) }, + }); + await this.salaryProfileEmployeeRepository.remove(salaryProfileEmployee); + await this.salaryOrgEmployeeRepository.remove(salaryOrgEmployee); + let orgs = await new CallAPI().GetData(request, "org/unauthorize/active/root/id"); let total = 1000; let _orgProfiles = await new CallAPI().PostData(request, "org/unauthorize/salary/gen", { @@ -2067,11 +2133,12 @@ export class SalaryPeriodController extends Controller { await this.salaryPeriodRepository.save(salaryPeriod); await Promise.all( - orgs.map(async (rootId: string) => { + orgs.map(async (root: any) => { let salaryOrgNew = Object.assign(new SalaryOrg()); salaryOrgNew.salaryPeriodId = salaryPeriod.id; salaryOrgNew.status = "PENDING"; - salaryOrgNew.rootId = rootId; + salaryOrgNew.rootId = root.rootId; + salaryOrgNew.root = root.root; salaryOrgNew.revisionId = salaryPeriod.revisionId; salaryOrgNew.snapshot = snapshot; salaryOrgNew.createdUserId = request.user.sub; @@ -2089,11 +2156,12 @@ export class SalaryPeriodController extends Controller { ); await Promise.all( - orgs.map(async (rootId: string) => { + orgs.map(async (root: any) => { let salaryOrgNew = Object.assign(new SalaryOrgEmployee()); salaryOrgNew.salaryPeriodId = salaryPeriod.id; salaryOrgNew.status = "PENDING"; - salaryOrgNew.rootId = rootId; + salaryOrgNew.rootId = root.rootId; + salaryOrgNew.root = root.root; salaryOrgNew.revisionId = salaryPeriod.revisionId; salaryOrgNew.snapshot = snapshot; salaryOrgNew.createdUserId = request.user.sub; diff --git a/src/controllers/SalaryPeriodEmployeeController.ts b/src/controllers/SalaryPeriodEmployeeController.ts index 933651f..e49373a 100644 --- a/src/controllers/SalaryPeriodEmployeeController.ts +++ b/src/controllers/SalaryPeriodEmployeeController.ts @@ -101,6 +101,107 @@ export class SalaryPeriodEmployeeController extends Controller { return new HttpSuccess(data); } + /** + * API ยอดใช้งานทั้งหมด + * + * @summary ยอดใช้งานทั้งหมด + * + * @param {string} id + * @param {string} group + * @param {string} period + * @param {string} snapshot + */ + @Post("summary/all") + async summaryUseSalary( + @Body() + body: { + year: number; + group: string; + period: string; + snapshot: string; + }, + ) { + const _salaryOrg = await this.salaryOrgRepository.find({ + relations: ["salaryPeriod", "salaryProfiles"], + where: { + group: body.group, + snapshot: body.snapshot.trim().toUpperCase(), + salaryPeriod: { period: body.period.trim().toUpperCase(), year: body.year }, + }, + }); + const salaryOrg: any[] = []; + await Promise.all( + _salaryOrg.map(async (item) => { + const fullNameParts = [ + item == null || item.salaryProfiles.length == 0 || item.salaryProfiles[0].root == null + ? null + : item.salaryProfiles[0].root, + ]; + const org = fullNameParts.filter((part) => part !== undefined && part !== null).join("/"); + const sum = item.salaryProfiles.reduce((accumulator, object) => { + return accumulator + object.amountSpecial; + }, 0); + const data = { + org: org, + total: item.total, + fifteenPercent: item.fifteenPercent, + chosen: item.quantityUsed, + remaining: item.remainQuota, + currentAmount: item.currentAmount, + sixPercentAmount: item.sixPercentAmount, + spentAmount: item.spentAmount, + sixPercentSpentAmount: item.sixPercentAmount - item.spentAmount, + useAmount: item.useAmount, + remainingAmount: item.remainingAmount, + totalAmountSpecial: sum, + totalBackup: item.salaryProfiles.filter((x) => x.isReserve == true).length, + }; + salaryOrg.push(data); + }), + ); + return new HttpSuccess({ + salaryOrg, + dashboard: { + total: salaryOrg.reduce((accumulator, object) => { + return accumulator + object.total; + }, 0), + fifteenPercent: salaryOrg.reduce((accumulator, object) => { + return accumulator + object.fifteenPercent; + }, 0), + chosen: salaryOrg.reduce((accumulator, object) => { + return accumulator + object.chosen; + }, 0), + remaining: salaryOrg.reduce((accumulator, object) => { + return accumulator + object.remaining; + }, 0), + currentAmount: salaryOrg.reduce((accumulator, object) => { + return accumulator + object.currentAmount; + }, 0), + sixPercentAmount: salaryOrg.reduce((accumulator, object) => { + return accumulator + object.sixPercentAmount; + }, 0), + spentAmount: salaryOrg.reduce((accumulator, object) => { + return accumulator + object.spentAmount; + }, 0), + sixPercentSpentAmount: salaryOrg.reduce((accumulator, object) => { + return accumulator + object.sixPercentSpentAmount; + }, 0), + useAmount: salaryOrg.reduce((accumulator, object) => { + return accumulator + object.useAmount; + }, 0), + remainingAmount: salaryOrg.reduce((accumulator, object) => { + return accumulator + object.remainingAmount; + }, 0), + totalAmountSpecial: salaryOrg.reduce((accumulator, object) => { + return accumulator + object.totalAmountSpecial; + }, 0), + totalBackup: salaryOrg.reduce((accumulator, object) => { + return accumulator + object.totalBackup; + }, 0), + }, + }); + } + /** * API จำนวนโควตา * diff --git a/src/entities/SalaryOrg.ts b/src/entities/SalaryOrg.ts index fd87af3..0e575d9 100644 --- a/src/entities/SalaryOrg.ts +++ b/src/entities/SalaryOrg.ts @@ -22,6 +22,13 @@ export class SalaryOrg extends EntityBase { }) rootId: string; + @Column({ + nullable: true, + comment: "orgRoot", + length: 255, + }) + root: string; + @Column({ nullable: true, comment: "คีย์นอก(FK)ของตาราง orgRevision", @@ -140,6 +147,9 @@ export class CreateSalaryOrg { @Column("uuid") rootId: string; + @Column() + root: string; + @Column() total: number;