update report

This commit is contained in:
kittapath 2025-02-12 11:22:03 +07:00
parent a3167520af
commit 408a3a7644

View file

@ -1,4 +1,14 @@
import { Controller, Get, Route, Security, Tags, SuccessResponse, Response, Path, Query } from "tsoa"; import {
Controller,
Get,
Route,
Security,
Tags,
SuccessResponse,
Response,
Path,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source"; import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
@ -37,7 +47,7 @@ export class ReportController extends Controller {
private posMasterRepository = AppDataSource.getRepository(PosMaster); private posMasterRepository = AppDataSource.getRepository(PosMaster);
private profileRepository = AppDataSource.getRepository(Profile); private profileRepository = AppDataSource.getRepository(Profile);
private empPosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); private empPosMasterRepository = AppDataSource.getRepository(EmployeePosMaster);
/** /**
* API . * API .
* *
* @summary . * @summary .
@ -60,69 +70,79 @@ export class ReportController extends Controller {
const minAge = ageMin ?? 18; const minAge = ageMin ?? 18;
const maxAge = ageMax ?? 60; const maxAge = ageMax ?? 60;
if (minAge > maxAge) { if (minAge > maxAge) {
throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax"); throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax");
} }
const yearInAD = year?year:null; const yearInAD = year ? year : null;
const currentRevision = await this.orgRevisionRepository.findOne({ const currentRevision = await this.orgRevisionRepository.findOne({
where:{ where: {
orgRevisionIsCurrent: true orgRevisionIsCurrent: true,
} },
}); });
const rawdataProfile = await this.posMasterRepository const rawdataProfile = await this.posMasterRepository
.createQueryBuilder('posMaster') .createQueryBuilder("posMaster")
.leftJoinAndSelect('posMaster.current_holder', 'current_holder') .leftJoinAndSelect("posMaster.current_holder", "current_holder")
.leftJoinAndSelect('posMaster.positions', 'positions') .leftJoinAndSelect("posMaster.positions", "positions")
.leftJoinAndSelect('posMaster.orgRoot', 'orgRoot') .leftJoinAndSelect("posMaster.orgRoot", "orgRoot")
.leftJoinAndSelect('positions.posExecutive', 'posExecutive') .leftJoinAndSelect("positions.posExecutive", "posExecutive")
.leftJoinAndSelect('current_holder.posType', 'posType') .leftJoinAndSelect("current_holder.posType", "posType")
.leftJoinAndSelect('current_holder.posLevel', 'posLevel') .leftJoinAndSelect("current_holder.posLevel", "posLevel")
.leftJoinAndSelect('current_holder.profileEducations', 'profileEducations') .leftJoinAndSelect("current_holder.profileEducations", "profileEducations")
.where('posMaster.orgRevisionId = :currentRevisionId', { currentRevisionId: currentRevision?.id }) .where("posMaster.orgRevisionId = :currentRevisionId", {
.andWhere(rootId?'posMaster.orgRootId = :rootId': "1=1", { rootId: rootId }) currentRevisionId: currentRevision?.id,
.andWhere('posMaster.current_holderId Is Not Null') })
.andWhere('positions.positionIsSelected = :positionIsSelected', { positionIsSelected: true }) .andWhere(rootId ? "posMaster.orgRootId = :rootId" : "1=1", { rootId: rootId })
.andWhere( yearInAD && yearInAD != null? 'YEAR(current_holder.dateAppoint) = :year': "1=1", { year: yearInAD }) .andWhere("posMaster.current_holderId Is Not Null")
.andWhere(` .andWhere("positions.positionIsSelected = :positionIsSelected", { positionIsSelected: true })
.andWhere(yearInAD && yearInAD != null ? "YEAR(current_holder.dateAppoint) = :year" : "1=1", {
year: yearInAD,
})
.andWhere(
`
TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) >= :minAge TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) >= :minAge
AND TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) <= :maxAge AND TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) <= :maxAge
`, { minAge, maxAge }) `,
.orderBy("posType.posTypeName","ASC") { minAge, maxAge },
.getMany(); )
if(!rawdataProfile){ .orderBy("posType.posTypeName", "ASC")
.getMany();
if (!rawdataProfile) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
} }
const mapData = rawdataProfile const mapData = rawdataProfile.map((x) => {
.map((x) => { const latestEducation = x.current_holder.profileEducations.sort(
const latestEducation = x.current_holder.profileEducations.sort((a:any, b:any) => b.endDate - a.startDate)[0]; (a: any, b: any) => b.endDate - a.startDate,
)[0];
return { return {
name: x.current_holder.firstName + " " + x.current_holder.lastName, name: x.current_holder.firstName + " " + x.current_holder.lastName,
affiliation: x.orgRoot.orgRootName??"-", affiliation: x.orgRoot.orgRootName ?? "-",
gender: x.current_holder.gender??"-", gender: x.current_holder.gender ?? "-",
positionName: x.positions[0]?x.positions[0].positionName:"-", positionName: x.positions[0] ? x.positions[0].positionName : "-",
status: x.current_holder.relationship??"-", status: x.current_holder.relationship ?? "-",
posType: x.current_holder.posType.posTypeName??"-", posType: x.current_holder.posType.posTypeName ?? "-",
posLevel: x.current_holder.posLevel.posLevelName??"-", posLevel: x.current_holder.posLevel.posLevelName ?? "-",
degree: latestEducation ? latestEducation.educationLevel : "-", degree: latestEducation ? latestEducation.educationLevel : "-",
posExecutive: x.positions[0].posExecutive?x.positions[0].posExecutive.posExecutiveName:"-", posExecutive: x.positions[0].posExecutive
? x.positions[0].posExecutive.posExecutiveName
: "-",
currentPreiodPos: "-", currentPreiodPos: "-",
levelPeriodPos: "-", levelPeriodPos: "-",
}; };
}); });
const groupedData = mapData.reduce((acc:any, item) => { const groupedData = mapData.reduce((acc: any, item) => {
const key = `${item.posType} - ${item.affiliation} - ${item.gender} - ${item.degree || 'ไม่พบข้อมูล'} - ${item.status || 'ไม่พบข้อมูล'} - ${item.positionName} - ${item.posLevel} - ${item.posExecutive || 'ไม่พบข้อมูล'} `; const key = `${item.posType} - ${item.affiliation} - ${item.gender} - ${item.degree || "ไม่พบข้อมูล"} - ${item.status || "ไม่พบข้อมูล"} - ${item.positionName} - ${item.posLevel} - ${item.posExecutive || "ไม่พบข้อมูล"} `;
if (!acc[key]) { if (!acc[key]) {
acc[key] = { acc[key] = {
posType: item.posType && item.posType != "" ? item.posType: "-", posType: item.posType && item.posType != "" ? item.posType : "-",
affiliation: item.affiliation && item.affiliation != "" ? item.affiliation: "-", affiliation: item.affiliation && item.affiliation != "" ? item.affiliation : "-",
gender: item.gender && item.gender != "" ? item.gender: "-", gender: item.gender && item.gender != "" ? item.gender : "-",
degree: item.degree && item.degree != "" ? item.degree: "-", degree: item.degree && item.degree != "" ? item.degree : "-",
status: item.status && item.status != "" ? item.status: "-", status: item.status && item.status != "" ? item.status : "-",
positionName: item.positionName && item.positionName != "" ? item.positionName: "-", positionName: item.positionName && item.positionName != "" ? item.positionName : "-",
posLevel: item.posLevel && item.posLevel != "" ? item.posLevel: "-", posLevel: item.posLevel && item.posLevel != "" ? item.posLevel : "-",
posExecutive: item.posExecutive && item.posExecutive != "" ? item.posExecutive: "-", posExecutive: item.posExecutive && item.posExecutive != "" ? item.posExecutive : "-",
currentPreiodPos: "-", currentPreiodPos: "-",
levelPeriodPos: "-", levelPeriodPos: "-",
count: 0, count: 0,
@ -132,23 +152,25 @@ export class ReportController extends Controller {
return acc; return acc;
}, {}); }, {});
const result = Object.values(groupedData).map((item: any) => ({ const result = Object.values(groupedData).map((item: any) => ({
...item , ...item,
count: Extension.ToThaiNumber(item.count.toString()), count: Extension.ToThaiNumber(item.count.toString()),
})); }));
return new HttpSuccess({ return new HttpSuccess({
template: "registry-officer", template: "registry-officer",
reportName: "xlsx-report", reportName: "xlsx-report",
data: { data: {
year: year?Extension.ToThaiNumber(year.toString()):Extension.ToThaiNumber(new Date().getFullYear().toString()), year: year
? Extension.ToThaiNumber((year + 543).toString())
: Extension.ToThaiNumber((new Date().getFullYear() + 543).toString()),
date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())), date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())),
list: result list: result,
}, },
}); });
} }
/** /**
* API . * API .
* *
* @summary . * @summary .
@ -171,64 +193,73 @@ export class ReportController extends Controller {
const minAge = ageMin ?? 18; const minAge = ageMin ?? 18;
const maxAge = ageMax ?? 60; const maxAge = ageMax ?? 60;
if (minAge > maxAge) { if (minAge > maxAge) {
throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax"); throw new HttpError(HttpStatus.NOT_FOUND, "ageMin cannot be greater than ageMax");
} }
const yearInAD = year?year:null; const yearInAD = year ? year : null;
const currentRevision = await this.orgRevisionRepository.findOne({ const currentRevision = await this.orgRevisionRepository.findOne({
where:{ where: {
orgRevisionIsCurrent: true orgRevisionIsCurrent: true,
} },
}); });
const rawdataProfile = await this.empPosMasterRepository const rawdataProfile = await this.empPosMasterRepository
.createQueryBuilder('posMaster') .createQueryBuilder("posMaster")
.leftJoinAndSelect('posMaster.current_holder', 'current_holder') .leftJoinAndSelect("posMaster.current_holder", "current_holder")
.leftJoinAndSelect('posMaster.positions', 'positions') .leftJoinAndSelect("posMaster.positions", "positions")
.leftJoinAndSelect('posMaster.orgRoot', 'orgRoot') .leftJoinAndSelect("posMaster.orgRoot", "orgRoot")
.leftJoinAndSelect('current_holder.posType', 'posType') .leftJoinAndSelect("current_holder.posType", "posType")
.leftJoinAndSelect('current_holder.posLevel', 'posLevel') .leftJoinAndSelect("current_holder.posLevel", "posLevel")
.leftJoinAndSelect('current_holder.profileEducations', 'profileEducations') .leftJoinAndSelect("current_holder.profileEducations", "profileEducations")
.where('posMaster.orgRevisionId = :currentRevisionId', { currentRevisionId: currentRevision?.id }) .where("posMaster.orgRevisionId = :currentRevisionId", {
.andWhere(rootId?'posMaster.orgRootId = :rootId': "1=1", { rootId: rootId }) currentRevisionId: currentRevision?.id,
.andWhere('posMaster.current_holderId Is Not Null') })
.andWhere('positions.positionIsSelected = :positionIsSelected', { positionIsSelected: true }) .andWhere(rootId ? "posMaster.orgRootId = :rootId" : "1=1", { rootId: rootId })
.andWhere( yearInAD && yearInAD != null? 'YEAR(current_holder.dateAppoint) = :year': "1=1", { year: yearInAD }) .andWhere("posMaster.current_holderId Is Not Null")
.andWhere(` .andWhere("positions.positionIsSelected = :positionIsSelected", { positionIsSelected: true })
.andWhere(yearInAD && yearInAD != null ? "YEAR(current_holder.dateAppoint) = :year" : "1=1", {
year: yearInAD,
})
.andWhere(
`
TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) >= :minAge TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) >= :minAge
AND TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) <= :maxAge AND TIMESTAMPDIFF(YEAR, current_holder.birthDate, CURDATE()) <= :maxAge
`, { minAge, maxAge }) `,
.orderBy("posType.posTypeName","ASC") { minAge, maxAge },
.getMany(); )
if(!rawdataProfile){ .orderBy("posType.posTypeName", "ASC")
.getMany();
if (!rawdataProfile) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
} }
const mapData = rawdataProfile.map((x) => { const mapData = rawdataProfile.map((x) => {
const latestEducation = x.current_holder.profileEducations.sort((a:any, b:any) => b.endDate - a.startDate)[0]; const latestEducation = x.current_holder.profileEducations.sort(
(a: any, b: any) => b.endDate - a.startDate,
)[0];
return { return {
name: x.current_holder.firstName + " " + x.current_holder.lastName, name: x.current_holder.firstName + " " + x.current_holder.lastName,
affiliation: x.orgRoot.orgRootName??"-", affiliation: x.orgRoot.orgRootName ?? "-",
gender: x.current_holder.gender??"-", gender: x.current_holder.gender ?? "-",
positionName: x.positions[0]?x.positions[0].positionName: "-", positionName: x.positions[0] ? x.positions[0].positionName : "-",
status: x.current_holder.relationship??"-", status: x.current_holder.relationship ?? "-",
posType: x.current_holder.posType.posTypeName??"-", posType: x.current_holder.posType.posTypeName ?? "-",
posLevel: x.current_holder.posLevel.posLevelName??"-", posLevel: x.current_holder.posLevel.posLevelName ?? "-",
degree: latestEducation ? latestEducation.educationLevel : "-", degree: latestEducation ? latestEducation.educationLevel : "-",
period: "-", period: "-",
}; };
}); });
const groupedData = mapData.reduce((acc:any, item) => { const groupedData = mapData.reduce((acc: any, item) => {
const key = `${item.affiliation} - ${item.gender} - ${item.degree || 'ไม่พบข้อมูล'} - ${item.status || 'ไม่พบข้อมูล'} - ${item.posType} - ${item.positionName} - ${item.posLevel} const key = `${item.affiliation} - ${item.gender} - ${item.degree || "ไม่พบข้อมูล"} - ${item.status || "ไม่พบข้อมูล"} - ${item.posType} - ${item.positionName} - ${item.posLevel}
`; `;
if (!acc[key]) { if (!acc[key]) {
acc[key] = { acc[key] = {
affiliation: item.affiliation && item.affiliation != ""?item.affiliation:"-", affiliation: item.affiliation && item.affiliation != "" ? item.affiliation : "-",
gender: item.gender && item.gender != ""?item.gender:"-", gender: item.gender && item.gender != "" ? item.gender : "-",
degree: item.degree && item.degree != ""?item.degree:"-", degree: item.degree && item.degree != "" ? item.degree : "-",
status: item.status && item.status != ""?item.status:"-", status: item.status && item.status != "" ? item.status : "-",
posType: item.posType && item.posType != ""?item.posType:"-", posType: item.posType && item.posType != "" ? item.posType : "-",
positionName: item.positionName && item.positionName != ""?item.positionName:"-", positionName: item.positionName && item.positionName != "" ? item.positionName : "-",
posLevel: Extension.ToThaiNumber(item.posLevel.toString()), posLevel: Extension.ToThaiNumber(item.posLevel.toString()),
period: "-", period: "-",
count: 0, count: 0,
@ -240,20 +271,21 @@ export class ReportController extends Controller {
}, {}); }, {});
const result = Object.values(groupedData).map((item: any) => ({ const result = Object.values(groupedData).map((item: any) => ({
...item , ...item,
count: Extension.ToThaiNumber(item.count.toString()) count: Extension.ToThaiNumber(item.count.toString()),
})); }));
return new HttpSuccess({ return new HttpSuccess({
template: "registry-emp", template: "registry-emp",
reportName: "xlsx-report", reportName: "xlsx-report",
data: { data: {
year: year?Extension.ToThaiNumber(year.toString()):Extension.ToThaiNumber(new Date().getFullYear().toString()), year: year
? Extension.ToThaiNumber((year + 543).toString())
: Extension.ToThaiNumber((new Date().getFullYear() + 543).toString()),
date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())), date: Extension.ToThaiNumber(Extension.ToThaiShortDate(new Date())),
list: result list: result,
}, },
}); });
} }
/** /**
@ -6468,11 +6500,11 @@ export class ReportController extends Controller {
@Get("report4/{rootId}") @Get("report4/{rootId}")
async findReport4(@Path() rootId: string) { async findReport4(@Path() rootId: string) {
const orgRootData = await this.orgRootRepository.findOne({ const orgRootData = await this.orgRootRepository.findOne({
where: { id: rootId } where: { id: rootId },
}); });
if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
const posMaster = await this.posMasterRepository const posMaster = await this.posMasterRepository
.createQueryBuilder("posMaster") .createQueryBuilder("posMaster")
.leftJoinAndSelect("posMaster.positions", "position") .leftJoinAndSelect("posMaster.positions", "position")
@ -6482,17 +6514,20 @@ export class ReportController extends Controller {
.orderBy("posType.posTypeRank", "ASC") .orderBy("posType.posTypeRank", "ASC")
.addOrderBy("posLevel.posLevelRank", "ASC") .addOrderBy("posLevel.posLevelRank", "ASC")
.getMany(); .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(""),
positions: [...new Set(x.positions.flatMap((y) => y.positionName))].join(""),
}))
const groupedData = _posMaster.reduce((acc:any, curr:any) => { 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(""),
positions: [...new Set(x.positions.flatMap((y) => y.positionName))].join(""),
}));
const groupedData = _posMaster.reduce((acc: any, curr: any) => {
const key = `${curr.type}|${curr.typeRank}|${curr.level}|${curr.levelRank}`; const key = `${curr.type}|${curr.typeRank}|${curr.level}|${curr.levelRank}`;
if (!acc[key]) { if (!acc[key]) {
acc[key] = { ...curr, total: 1 }; acc[key] = { ...curr, total: 1 };
@ -6501,7 +6536,7 @@ export class ReportController extends Controller {
} }
return acc; return acc;
}, {}); }, {});
let result = Object.values(groupedData) let result = Object.values(groupedData)
.map((x: any) => ({ .map((x: any) => ({
type: x.type, type: x.type,
@ -6523,16 +6558,16 @@ export class ReportController extends Controller {
let _total: number = 0; let _total: number = 0;
let _reslut = new Array(); let _reslut = new Array();
result.forEach((x:any, idx:number) => { result.forEach((x: any, idx: number) => {
allTotal += x.total; allTotal += x.total;
total += x.total; total += x.total;
if(x.type === tmpType) { if (x.type === tmpType) {
_reslut.push({ _reslut.push({
...x, ...x,
type: "" type: "",
}) });
}else { } else {
if(x.type !== tmpType && tmpType != "") { if (x.type !== tmpType && tmpType != "") {
_total = total - x.total; _total = total - x.total;
_reslut.push({ _reslut.push({
type: "", type: "",
@ -6541,13 +6576,13 @@ export class ReportController extends Controller {
levelRank: "", levelRank: "",
total: _total, total: _total,
remark: "", remark: "",
}) });
total = x.total; total = x.total;
_total = 0; _total = 0;
} }
_reslut.push({ _reslut.push({
...x ...x,
}) });
} }
tmpType = x.type; tmpType = x.type;
}); });
@ -6559,7 +6594,7 @@ export class ReportController extends Controller {
levelRank: "", levelRank: "",
total: total, total: total,
remark: "", remark: "",
}) });
_reslut.push({ _reslut.push({
type: "", type: "",
typeRank: "", typeRank: "",
@ -6569,24 +6604,24 @@ export class ReportController extends Controller {
remark: "", remark: "",
}); });
return new HttpSuccess({ return new HttpSuccess({
template: "report4", template: "report4",
reportName: "report4", reportName: "report4",
data: { data: {
dateCurrent: Extension.ToThaiShortDate(new Date()), dateCurrent: Extension.ToThaiShortDate(new Date()),
rootName: orgRootData ? orgRootData.orgRootName : "-", rootName: orgRootData ? orgRootData.orgRootName : "-",
data: _reslut data: _reslut,
} },
}); });
} }
@Get("report4-employee/{rootId}") @Get("report4-employee/{rootId}")
async findReportEmp4(@Path() rootId: string) { async findReportEmp4(@Path() rootId: string) {
const orgRootData = await this.orgRootRepository.findOne({ const orgRootData = await this.orgRootRepository.findOne({
where: { id: rootId } where: { id: rootId },
}); });
if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
const posMaster = await this.empPosMasterRepository const posMaster = await this.empPosMasterRepository
.createQueryBuilder("posMaster") .createQueryBuilder("posMaster")
.leftJoinAndSelect("posMaster.positions", "position") .leftJoinAndSelect("posMaster.positions", "position")
@ -6596,17 +6631,20 @@ export class ReportController extends Controller {
.orderBy("posType.posTypeRank", "ASC") .orderBy("posType.posTypeRank", "ASC")
.addOrderBy("posLevel.posLevelRank", "ASC") .addOrderBy("posLevel.posLevelRank", "ASC")
.getMany(); .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(""),
positions: [...new Set(x.positions.flatMap((y) => y.positionName))].join(""),
}))
const groupedData = _posMaster.reduce((acc:any, curr:any) => { 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(""),
positions: [...new Set(x.positions.flatMap((y) => y.positionName))].join(""),
}));
const groupedData = _posMaster.reduce((acc: any, curr: any) => {
const key = `${curr.type}|${curr.typeRank}|${curr.level}|${curr.levelRank}`; const key = `${curr.type}|${curr.typeRank}|${curr.level}|${curr.levelRank}`;
if (!acc[key]) { if (!acc[key]) {
acc[key] = { ...curr, total: 1 }; acc[key] = { ...curr, total: 1 };
@ -6615,14 +6653,14 @@ export class ReportController extends Controller {
} }
return acc; return acc;
}, {}); }, {});
let result = Object.values(groupedData) let result = Object.values(groupedData)
.map((x: any) => ({ .map((x: any) => ({
type: x.type, type: x.type,
typeRank: parseInt(x.typeRank), typeRank: parseInt(x.typeRank),
level: x.level, level: x.level,
levelRank: parseInt(x.levelRank), levelRank: parseInt(x.levelRank),
total: x.total, total: x.total,
remark: x.positions, remark: x.positions,
})) }))
@ -6638,16 +6676,16 @@ export class ReportController extends Controller {
let _total: number = 0; let _total: number = 0;
let _reslut = new Array(); let _reslut = new Array();
result.forEach((x:any, idx:number) => { result.forEach((x: any, idx: number) => {
allTotal += x.total; allTotal += x.total;
total += x.total; total += x.total;
if(x.type === tmpType) { if (x.type === tmpType) {
_reslut.push({ _reslut.push({
...x, ...x,
type: "" type: "",
}) });
}else { } else {
if(x.type !== tmpType && tmpType != "") { if (x.type !== tmpType && tmpType != "") {
_total = total - x.total; _total = total - x.total;
_reslut.push({ _reslut.push({
type: "", type: "",
@ -6656,13 +6694,13 @@ export class ReportController extends Controller {
levelRank: "", levelRank: "",
total: _total, total: _total,
remark: "", remark: "",
}) });
total = x.total; total = x.total;
_total = 0; _total = 0;
} }
_reslut.push({ _reslut.push({
...x ...x,
}) });
} }
tmpType = x.type; tmpType = x.type;
}); });
@ -6674,7 +6712,7 @@ export class ReportController extends Controller {
levelRank: "", levelRank: "",
total: total, total: total,
remark: "", remark: "",
}) });
_reslut.push({ _reslut.push({
type: "", type: "",
typeRank: "", typeRank: "",
@ -6684,15 +6722,14 @@ export class ReportController extends Controller {
remark: "", remark: "",
}); });
return new HttpSuccess({ return new HttpSuccess({
template: "report4", template: "report4",
reportName: "report4", reportName: "report4",
data: { data: {
dateCurrent: Extension.ToThaiShortDate(new Date()), dateCurrent: Extension.ToThaiShortDate(new Date()),
rootName: orgRootData ? orgRootData.orgRootName : "-", rootName: orgRootData ? orgRootData.orgRootName : "-",
data: _reslut data: _reslut,
} },
}); });
} }
} }