hrms-api-salary/src/controllers/ReportController.ts

114 lines
4.5 KiB
TypeScript
Raw Normal View History

import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { In, IsNull, Not } from "typeorm";
import { Salarys } from "../entities/Salarys";
import { SalaryRanks } from "../entities/SalaryRanks";
import { PosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
import Extension from "../interfaces/extension";
@Route("api/v1/salary/report")
@Tags("Report")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class ReportController extends Controller {
private salaryRepository = AppDataSource.getRepository(Salarys);
private salaryRankRepository = AppDataSource.getRepository(SalaryRanks);
private poTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
/**
* API
*
* @summary
*
* @param {string} id Guid, *Id
*/
@Get("{id}")
async SalaryReport(@Path() id: string) {
const salarys = await this.salaryRepository.findOne({
where: { id: id }
});
if (!salarys) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id);
}
const posType = await this.poTypeRepository.findOne({
where: { id: salarys.posTypeId },
});
const posLevel = await this.posLevelRepository.findOne({
where: { id: salarys.posLevelId },
});
const salaryRank = await this.salaryRankRepository.find({
where: {
salaryId: salarys.id,
},
select: [
"id",
"salary",
"salaryHalf",
"salaryHalfSpecial",
"salaryFull",
"salaryFullSpecial",
"salaryFullHalf",
"salaryFullHalfSpecial",
],
order: {
salary: "DESC",
salaryHalf: "DESC",
}
});
const mapSalaryRank = salaryRank.map((item, index) => ({
// no: index + 1,
// id: item.id,
salary: item.salary == null || 0 ? "" : item.salary.toLocaleString() ,
salaryHalf: item.salaryHalf == null || 0 ? "" : item.salaryHalf.toLocaleString(),
salaryHalfSpecial: item.salaryHalfSpecial == null || 0 ? "" : item.salaryHalfSpecial.toLocaleString(),
salaryFull: item.salaryFull == null || 0 ? "" : item.salaryFull.toLocaleString(),
salaryFullSpecial: item.salaryFullSpecial == null || 0 ? "" : item.salaryFullSpecial.toLocaleString(),
salaryFullHalf: item.salaryFullHalf == null || 0 ? "" : item.salaryFullHalf.toLocaleString(),
salaryFullHalfSpecial: item.salaryFullHalfSpecial == null || 0 ? "" : item.salaryFullHalfSpecial.toLocaleString(),
}));
2024-02-19 15:31:07 +07:00
return new HttpSuccess({ template: "salaryReport", reportName: "salaryReport",
data: {
nameType: salarys.salaryType == "OFFICER" ? "ผังข้าราชการกรุงเทพมหานครสามัญ" :
salarys.salaryType == "EMPLOYEE" ? "ผังลูกจ้างประจำกรุงเทพมหานคร" : "",
level: posLevel?.posLevelName == null ? "" : posLevel?.posLevelName,
type: posType?.posTypeName == null ? "" : posType?.posTypeName,
date: salarys.date == null ? "" :
salarys.date.getDate() + " " + Extension.ToThaiMonth(salarys.date.getMonth()+1) + " " + Extension.ToThaiYear(salarys.date.getFullYear()),
startDate: salarys.startDate == null ? "" :
salarys.startDate.getDate() + " " + Extension.ToThaiMonth(salarys.startDate.getMonth()+1) + " " + Extension.ToThaiYear(salarys.startDate.getFullYear()),
endDate: salarys.endDate == null ? "" :
salarys.endDate.getDate() + " " + Extension.ToThaiMonth(salarys.endDate.getMonth()+1) + " " + Extension.ToThaiYear(salarys.endDate.getFullYear()),
details: salarys.details == null ? "" : salarys.details,
salaryRanks: mapSalaryRank
}
});
}
}