diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index aaeac81..57ef9d1 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -32,221 +32,6 @@ export class SalaryPeriodController extends Controller { private salaryOrgRepository = AppDataSource.getRepository(SalaryOrg); private salaryProfileRepository = AppDataSource.getRepository(SalaryProfile); - /** - * API สร้างรอบเงินเดือน - * - * @summary SLR_016 - สร้างรอบเงินเดือน #16 - * - */ - @Post() - async create_salary_period( - @Body() requestBody: CreateSalaryPeriod, - @Request() request: { user: Record }, - ) { - const salaryPeriod = Object.assign(new SalaryPeriod(), requestBody); - - const chk_toUpper = ["SPECIAL", "APR", "OCT"]; - if (!chk_toUpper.includes(salaryPeriod.period.toUpperCase())) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผัง ไม่ถูกต้อง"); - } - - const startOfYear = new Date(salaryPeriod.effectiveDate.getFullYear(), 0, 1); - const endOfYear = new Date(salaryPeriod.effectiveDate.getFullYear(), 11, 31); - const chk_period = await this.salaryPeriodRepository.findOne({ - where: { - period: salaryPeriod.period, - effectiveDate: Between(startOfYear, endOfYear), - }, - }); - - if (chk_period) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ประเภทผังปี " + salaryPeriod.effectiveDate.getFullYear() + " ซ้ำ", - ); - } - - salaryPeriod.period = salaryPeriod.period.toUpperCase(); - salaryPeriod.createdUserId = request.user.sub; - salaryPeriod.createdFullName = request.user.name; - salaryPeriod.lastUpdateUserId = request.user.sub; - salaryPeriod.lastUpdateFullName = request.user.name; - await this.salaryPeriodRepository.save(salaryPeriod); - return new HttpSuccess(salaryPeriod.id); - } - - /** - * API แก้ไขรอบเงินเดือน - * - * @summary SLR_017 - แก้ไขรอบเงินเดือน #17 - * - * @param {string} id Guid, *Id รอบเงินเดือน - */ - @Put("{id}") - async update_salary_period( - @Path() id: string, - @Body() requestBody: UpdateSalaryPeriod, - @Request() request: { user: Record }, - ) { - const chk_SalaryPeriod = await this.salaryPeriodRepository.findOne({ - where: { id: id }, - }); - if (!chk_SalaryPeriod) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); - } - - const chk_toUpper = ["SPECIAL", "APR", "OCT"]; - if (!chk_toUpper.includes(requestBody.period.toUpperCase())) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผัง ไม่ถูกต้อง"); - } - - const startOfYear = new Date(requestBody.effectiveDate.getFullYear(), 0, 1); - const endOfYear = new Date(requestBody.effectiveDate.getFullYear(), 11, 31); - const chk_period = await this.salaryPeriodRepository.findOne({ - where: { - period: requestBody.period, - id: Not(id), - effectiveDate: Between(startOfYear, endOfYear), - }, - }); - - if (chk_period) { - throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ประเภทผังปี " + (requestBody.effectiveDate.getFullYear() + 543) + " ซ้ำ", - ); - } - - chk_SalaryPeriod.period = requestBody.period.toUpperCase(); - chk_SalaryPeriod.lastUpdateUserId = request.user.sub; - chk_SalaryPeriod.lastUpdateFullName = request.user.name; - chk_SalaryPeriod.lastUpdatedAt = new Date(); - this.salaryPeriodRepository.merge(chk_SalaryPeriod, requestBody); - await this.salaryPeriodRepository.save(chk_SalaryPeriod); - return new HttpSuccess(id); - } - - /** - * API ลบรอบเงินเดือน - * - * @summary SLR_018 - ลบรอบเงินเดือน #18 - * - * @param {string} id Guid, *Id รอบเงินเดือน - */ - @Delete("{id}") - async delete_salary_period(@Path() id: string) { - const SalaryPeriod = await this.salaryPeriodRepository.findOne({ - where: { id: id }, - }); - if (!SalaryPeriod) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); - } - await this.salaryPeriodRepository.remove(SalaryPeriod); - return new HttpSuccess(); - } - - /** - * API รายละเอียดรอบเงินเดือน - * - * @summary SLR_019 - รายละเอียดรอบเงินเดือน #19 - * - * @param {string} id Guid, *Id รอบเงินเดือน - */ - @Get("{id}") - async GetSalaryPeriod_ById(@Path() id: string) { - const salaryPeriod = await this.salaryPeriodRepository.findOne({ - where: { id: id }, - select: ["id", "period", "isActive", "effectiveDate", "isActive", "status"], - }); - if (!salaryPeriod) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); - } - return new HttpSuccess(salaryPeriod); - } - - /** - * API รายการรอบเงินเดือน - * - * @summary SLR_020 - รายการรอบเงินเดือน #20 - * - */ - @Get() - async GetListsSalaryPeriod( - @Query("page") page: number = 1, - @Query("pageSize") pageSize: number = 10, - @Query("keyword") keyword?: string, - @Query("year") year: number = 2024, - ) { - //ssss total ผิด - // let salaryPeriod: any; - // let total: any; - // if (year != 0) { - // const startOfYear = new Date(year, 0, 1); - // const endOfYear = new Date(year, 11, 31); - // [salaryPeriod, total] = await this.salaryPeriodRepository.findAndCount({ - // skip: (page - 1) * pageSize, - // take: pageSize, - // where: { - // effectiveDate: Between(startOfYear, endOfYear), - // }, - // }); - // } else { - // [salaryPeriod, total] = await this.salaryPeriodRepository.findAndCount({ - // skip: (page - 1) * pageSize, - // take: pageSize, - // }); - // } - - // if (keyword != undefined && keyword !== "") { - // const filteredSalaryPeriod = salaryPeriod.filter( - // (x: any) => - // x.period.toString().includes(keyword) || - // x.isActive.toString().includes(keyword) || - // x.effectiveDate.getFullYear().toString().includes(keyword), - // ); - - // const formattedData = filteredSalaryPeriod.map((item: any) => ({ - // id: item.id, - // period: item.period, - // isActive: item.isActive, - // effectiveDate: item.effectiveDate, - // status: item.status, - // })); - // return new HttpSuccess({ data: formattedData, total: formattedData.length }); - // } - - // const formattedData = salaryPeriod.map((item: any) => ({ - // id: item.id, - // period: item.period, - // isActive: item.isActive, - // effectiveDate: item.effectiveDate, - // status: item.status, - // })); - // return new HttpSuccess({ data: formattedData, total }); - - const [salaryPeriod, total] = await AppDataSource.getRepository(SalaryPeriod) - .createQueryBuilder("SalaryPeriod") - .andWhere(year != 0 ? "SalaryPeriod.year LIKE :year" : "1=1", {year: `%${year}%`,}) - .orWhere("SalaryPeriod.period LIKE :keyword", { keyword: `%${keyword}%` }) - .orWhere("SalaryPeriod.isActive LIKE :keyword", { keyword: `%${keyword}%` }) - .orWhere("SalaryPeriod.year LIKE :keyword", { keyword: `%${year}%` }) - .select([ - "SalaryPeriod.id", - "SalaryPeriod.period", - "SalaryPeriod.isActive", - "SalaryPeriod.effectiveDate", - "SalaryPeriod.status", - "SalaryPeriod.year", - ]) - .orderBy("SalaryPeriod.effectiveDate", "DESC") - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); - - return new HttpSuccess({ data: salaryPeriod, total }); - - } - /** * API รอบล่าสุด * @@ -552,4 +337,219 @@ export class SalaryPeriodController extends Controller { await this.salaryProfileRepository.save(salaryProfile); return new HttpSuccess(salaryProfile.id); } + + /** + * API สร้างรอบเงินเดือน + * + * @summary SLR_016 - สร้างรอบเงินเดือน #16 + * + */ + @Post() + async create_salary_period( + @Body() requestBody: CreateSalaryPeriod, + @Request() request: { user: Record }, + ) { + const salaryPeriod = Object.assign(new SalaryPeriod(), requestBody); + + const chk_toUpper = ["SPECIAL", "APR", "OCT"]; + if (!chk_toUpper.includes(salaryPeriod.period.toUpperCase())) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผัง ไม่ถูกต้อง"); + } + + const startOfYear = new Date(salaryPeriod.effectiveDate.getFullYear(), 0, 1); + const endOfYear = new Date(salaryPeriod.effectiveDate.getFullYear(), 11, 31); + const chk_period = await this.salaryPeriodRepository.findOne({ + where: { + period: salaryPeriod.period, + effectiveDate: Between(startOfYear, endOfYear), + }, + }); + + if (chk_period) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ประเภทผังปี " + salaryPeriod.effectiveDate.getFullYear() + " ซ้ำ", + ); + } + + salaryPeriod.period = salaryPeriod.period.toUpperCase(); + salaryPeriod.createdUserId = request.user.sub; + salaryPeriod.createdFullName = request.user.name; + salaryPeriod.lastUpdateUserId = request.user.sub; + salaryPeriod.lastUpdateFullName = request.user.name; + await this.salaryPeriodRepository.save(salaryPeriod); + return new HttpSuccess(salaryPeriod.id); + } + + /** + * API แก้ไขรอบเงินเดือน + * + * @summary SLR_017 - แก้ไขรอบเงินเดือน #17 + * + * @param {string} id Guid, *Id รอบเงินเดือน + */ + @Put("{id}") + async update_salary_period( + @Path() id: string, + @Body() requestBody: UpdateSalaryPeriod, + @Request() request: { user: Record }, + ) { + const chk_SalaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { id: id }, + }); + if (!chk_SalaryPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); + } + + const chk_toUpper = ["SPECIAL", "APR", "OCT"]; + if (!chk_toUpper.includes(requestBody.period.toUpperCase())) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผัง ไม่ถูกต้อง"); + } + + const startOfYear = new Date(requestBody.effectiveDate.getFullYear(), 0, 1); + const endOfYear = new Date(requestBody.effectiveDate.getFullYear(), 11, 31); + const chk_period = await this.salaryPeriodRepository.findOne({ + where: { + period: requestBody.period, + id: Not(id), + effectiveDate: Between(startOfYear, endOfYear), + }, + }); + + if (chk_period) { + throw new HttpError( + HttpStatusCode.NOT_FOUND, + "ประเภทผังปี " + (requestBody.effectiveDate.getFullYear() + 543) + " ซ้ำ", + ); + } + + chk_SalaryPeriod.period = requestBody.period.toUpperCase(); + chk_SalaryPeriod.lastUpdateUserId = request.user.sub; + chk_SalaryPeriod.lastUpdateFullName = request.user.name; + chk_SalaryPeriod.lastUpdatedAt = new Date(); + this.salaryPeriodRepository.merge(chk_SalaryPeriod, requestBody); + await this.salaryPeriodRepository.save(chk_SalaryPeriod); + return new HttpSuccess(id); + } + + /** + * API ลบรอบเงินเดือน + * + * @summary SLR_018 - ลบรอบเงินเดือน #18 + * + * @param {string} id Guid, *Id รอบเงินเดือน + */ + @Delete("{id}") + async delete_salary_period(@Path() id: string) { + const SalaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { id: id }, + }); + if (!SalaryPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); + } + await this.salaryPeriodRepository.remove(SalaryPeriod); + return new HttpSuccess(); + } + + /** + * API รายละเอียดรอบเงินเดือน + * + * @summary SLR_019 - รายละเอียดรอบเงินเดือน #19 + * + * @param {string} id Guid, *Id รอบเงินเดือน + */ + @Get("{id}") + async GetSalaryPeriod_ById(@Path() id: string) { + const salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { id: id }, + select: ["id", "period", "isActive", "effectiveDate", "isActive", "status"], + }); + if (!salaryPeriod) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); + } + return new HttpSuccess(salaryPeriod); + } + + /** + * API รายการรอบเงินเดือน + * + * @summary SLR_020 - รายการรอบเงินเดือน #20 + * + */ + @Get() + async GetListsSalaryPeriod( + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query("keyword") keyword?: string, + @Query("year") year: number = 2024, + ) { + //ssss total ผิด + // let salaryPeriod: any; + // let total: any; + // if (year != 0) { + // const startOfYear = new Date(year, 0, 1); + // const endOfYear = new Date(year, 11, 31); + // [salaryPeriod, total] = await this.salaryPeriodRepository.findAndCount({ + // skip: (page - 1) * pageSize, + // take: pageSize, + // where: { + // effectiveDate: Between(startOfYear, endOfYear), + // }, + // }); + // } else { + // [salaryPeriod, total] = await this.salaryPeriodRepository.findAndCount({ + // skip: (page - 1) * pageSize, + // take: pageSize, + // }); + // } + + // if (keyword != undefined && keyword !== "") { + // const filteredSalaryPeriod = salaryPeriod.filter( + // (x: any) => + // x.period.toString().includes(keyword) || + // x.isActive.toString().includes(keyword) || + // x.effectiveDate.getFullYear().toString().includes(keyword), + // ); + + // const formattedData = filteredSalaryPeriod.map((item: any) => ({ + // id: item.id, + // period: item.period, + // isActive: item.isActive, + // effectiveDate: item.effectiveDate, + // status: item.status, + // })); + // return new HttpSuccess({ data: formattedData, total: formattedData.length }); + // } + + // const formattedData = salaryPeriod.map((item: any) => ({ + // id: item.id, + // period: item.period, + // isActive: item.isActive, + // effectiveDate: item.effectiveDate, + // status: item.status, + // })); + // return new HttpSuccess({ data: formattedData, total }); + + const [salaryPeriod, total] = await AppDataSource.getRepository(SalaryPeriod) + .createQueryBuilder("SalaryPeriod") + .andWhere(year != 0 ? "SalaryPeriod.year LIKE :year" : "1=1", {year: `%${year}%`,}) + .orWhere("SalaryPeriod.period LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("SalaryPeriod.isActive LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("SalaryPeriod.year LIKE :keyword", { keyword: `%${year}%` }) + .select([ + "SalaryPeriod.id", + "SalaryPeriod.period", + "SalaryPeriod.isActive", + "SalaryPeriod.effectiveDate", + "SalaryPeriod.status", + "SalaryPeriod.year", + ]) + .orderBy("SalaryPeriod.effectiveDate", "DESC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + + return new HttpSuccess({ data: salaryPeriod, total }); + } + }