diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index e126509b..4f88c33f 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -13,6 +13,7 @@ import { OrgChild4 } from "../entities/OrgChild4"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { PosMaster } from "../entities/PosMaster"; +import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import Extension from "../interfaces/extension"; import { LeaveType } from "../entities/LeaveType"; import HttpStatus from "../interfaces/http-status"; @@ -33,6 +34,7 @@ export class ReportController extends Controller { private posTypepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); private posMasterRepository = AppDataSource.getRepository(PosMaster); + private empPosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); /** * API Report1 @@ -6246,7 +6248,6 @@ export class ReportController extends Controller { @Get("report4/{rootId}") async findReport4(@Path() rootId: string) { - const orgRootData = await this.orgRootRepository.findOne({ where: { id: rootId } }); @@ -6262,56 +6263,212 @@ export class ReportController extends Controller { .addOrderBy("posLevel.posLevelRank", "ASC") .getMany(); - const _posMaster = posMaster.map((x) => ({ - type: [...new Set(x.positions.flatMap((y) => y.posType.posTypeName))].join(","), - typeRank: [...new Set(x.positions.flatMap((y) => y.posType.posTypeRank))].join(""), - level: [...new Set(x.positions.flatMap((y) => y.posLevel.posLevelName))].join(","), - levelRank: [...new Set(x.positions.flatMap((y) => `${y.posType.posTypeRank}${y.posLevel.posLevelRank}`))].join(""), - })) - // .sort((x:any, y:any) => parseInt(x.typeRank) - parseInt(y.typeRank)) - // .sort((x:any, y:any) => parseInt(x.levelRank) - parseInt(y.levelRank)); + const _posMaster = posMaster + .map((x) => ({ + type: [...new Set(x.positions.flatMap((y) => y.posType.posTypeName))].join(","), + typeRank: [...new Set(x.positions.flatMap((y) => y.posType.posTypeRank))].join(""), + level: [...new Set(x.positions.flatMap((y) => y.posLevel.posLevelName))].join(","), + levelRank: [...new Set(x.positions.flatMap((y) => `${y.posType.posTypeRank}${y.posLevel.posLevelRank}`))].join(""), + })) - - // console.log("XXX: ",_posMaster) - const groupPosMaster = _posMaster.reduce((total: any, idx: any) => { - const sortedLevel = idx.level.split(",").sort().join(","); - const key = `${idx.type}-${sortedLevel}`; - - if (!total[key]) { - total[key] = { - type: idx.type, - typeRank: idx.typeRank, - level: sortedLevel, - LeaveType: idx.levelRank, - total: 0, - remark: "" - }; + const groupedData = _posMaster.reduce((acc:any, curr:any) => { + const key = `${curr.type}|${curr.typeRank}|${curr.level}|${curr.levelRank}`; + if (!acc[key]) { + acc[key] = { ...curr, total: 1 }; + } else { + acc[key].total += 1; } - total[key].total += 1; - return total; + return acc; }, {}); - - let result = Object.values(groupPosMaster) - .sort((x:any, y:any) => parseInt(x.typeRank) - parseInt(y.typeRank)) - .sort((x:any, y:any) => parseInt(x.levelRank) - parseInt(y.levelRank));; - // let _TypeTemp = ""; - // result = result.map((item: any) => { - // if (item.type === _TypeTemp) { - // return { ...item, type: "" }; - // } - // _TypeTemp = item.type; - // return item; - // }); + let result = Object.values(groupedData) + .map((x: any) => ({ + type: x.type, + typeRank: parseInt(x.typeRank), + level: x.level, + levelRank: parseInt(x.levelRank), + total: x.total, + remark: "", + })) + .sort((x, y) => { + if (x.typeRank !== y.typeRank) { + return x.typeRank - y.typeRank; + } + return x.levelRank - y.levelRank; + }); + let tmpType: string = ""; + let allTotal: number = 0; + let total: number = 0; + let _total: number = 0; + let _reslut = new Array(); + + result.forEach((x:any, idx:number) => { + allTotal += x.total; + total += x.total; + if(x.type === tmpType) { + _reslut.push({ + ...x, + type: "" + }) + }else { + if(x.type !== tmpType && tmpType != "") { + _total = total - x.total; + _reslut.push({ + type: "", + typeRank: "", + level: "รวม", + levelRank: "", + total: _total, + remark: "", + }) + total = x.total; + _total = 0; + } + _reslut.push({ + ...x + }) + } + tmpType = x.type; + }); + + _reslut.push({ + type: "", + typeRank: "", + level: "รวม", + levelRank: "", + total: total, + remark: "", + }) + _reslut.push({ + type: "", + typeRank: "", + level: "รวมทั้งสิ้น", + levelRank: "", + total: allTotal, + remark: "", + }); + return new HttpSuccess({ template: "report4", reportName: "report4", data: { dateCurrent: Extension.ToThaiShortDate(new Date()), rootName: orgRootData ? orgRootData.orgRootName : "-", - data: result + data: _reslut } }); + } + @Get("report4-employee/{rootId}") + async findReportEmp4(@Path() rootId: string) { + const orgRootData = await this.orgRootRepository.findOne({ + where: { id: rootId } + }); + if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + + const posMaster = await this.empPosMasterRepository + .createQueryBuilder("posMaster") + .leftJoinAndSelect("posMaster.positions", "position") + .leftJoinAndSelect("position.posType", "posType") + .leftJoinAndSelect("position.posLevel", "posLevel") + .where("posMaster.orgRootId = :rootId", { rootId }) + .orderBy("posType.posTypeRank", "ASC") + .addOrderBy("posLevel.posLevelRank", "ASC") + .getMany(); + + const _posMaster = posMaster + .map((x) => ({ + type: [...new Set(x.positions.flatMap((y) => y.posType.posTypeName))].join(","), + typeRank: [...new Set(x.positions.flatMap((y) => y.posType.posTypeRank))].join(""), + level: [...new Set(x.positions.flatMap((y) => y.posLevel.posLevelName))].join(","), + levelRank: [...new Set(x.positions.flatMap((y) => `${y.posType.posTypeRank}${y.posLevel.posLevelRank}`))].join(""), + })) + + const groupedData = _posMaster.reduce((acc:any, curr:any) => { + const key = `${curr.type}|${curr.typeRank}|${curr.level}|${curr.levelRank}`; + if (!acc[key]) { + acc[key] = { ...curr, total: 1 }; + } else { + acc[key].total += 1; + } + return acc; + }, {}); + + let result = Object.values(groupedData) + .map((x: any) => ({ + type: x.type, + typeRank: parseInt(x.typeRank), + level: x.level, + levelRank: parseInt(x.levelRank), + total: x.total, + remark: "", + })) + .sort((x, y) => { + if (x.typeRank !== y.typeRank) { + return x.typeRank - y.typeRank; + } + return x.levelRank - y.levelRank; + }); + let tmpType: string = ""; + let allTotal: number = 0; + let total: number = 0; + let _total: number = 0; + let _reslut = new Array(); + + result.forEach((x:any, idx:number) => { + allTotal += x.total; + total += x.total; + if(x.type === tmpType) { + _reslut.push({ + ...x, + type: "" + }) + }else { + if(x.type !== tmpType && tmpType != "") { + _total = total - x.total; + _reslut.push({ + type: "", + typeRank: "", + level: "รวม", + levelRank: "", + total: _total, + remark: "", + }) + total = x.total; + _total = 0; + } + _reslut.push({ + ...x + }) + } + tmpType = x.type; + }); + + _reslut.push({ + type: "", + typeRank: "", + level: "รวม", + levelRank: "", + total: total, + remark: "", + }) + _reslut.push({ + type: "", + typeRank: "", + level: "รวมทั้งสิ้น", + levelRank: "", + total: allTotal, + remark: "", + }); + + return new HttpSuccess({ + template: "report4", + reportName: "report4", + data: { + dateCurrent: Extension.ToThaiShortDate(new Date()), + rootName: orgRootData ? orgRootData.orgRootName : "-", + data: _reslut + } + }); } }