diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 36412e3..2f47d78 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -19,6 +19,9 @@ import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { KpiPeriod } from "../entities/kpiPeriod"; import { KpiUserEvaluation } from "../entities/kpiUserEvaluation"; +import { off } from "process"; +import Extension from "../interfaces/extension"; + @Route("api/v1/kpi/report") @Tags("Report") @Security("bearerAuth") @@ -37,27 +40,28 @@ export class ReportController extends Controller { const getPeriod = await this.kpiPeriodRepository.findOne({ where: { id: id }, }); - if (!getEvaluations || getEvaluations.length === 0) { - console.log("No evaluations found for the given kpiPeriodId: ", id); - return; - } if (!getPeriod) { - console.log("No period data found for the given id: ", id); - return; + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่มีรอบการประเมินนี้อยู่ในระบบ"); } - const formattedData = getEvaluations.map((evaluation) => ({ - periodId: requestBody.periodId, - root: requestBody.root, + const officer = getEvaluations.map((evaluation) => ({ fullName: `${evaluation.prefix} ${evaluation.firstName} ${evaluation.lastName}`, position: evaluation.position, posLevel: evaluation.posLevelName, - announceYear: getPeriod.year, result: "ดีเด่น", + })); + + const formattedData = { + periodId: requestBody.periodId, + root: requestBody.root, + authorizedFullName: "นาย สมหมาย นครชัยศรี", + authorizedPosition: "เจ้าหน้าที่พิเศษ", + announceYear: Extension.ToThaiNumber(getPeriod.year.toString()), oc: "test", organizationName: "test", - announceDate: "testDATE", - roundNo: "1", - })); + announceDate: "๑๒ สิงหาคม ๒๕๖๔", + roundNo: "๑ (๒๕๖๔) ", + officer: officer, + }; return new HttpSuccess({ template: "announce", diff --git a/src/interfaces/extension.ts b/src/interfaces/extension.ts new file mode 100644 index 0000000..d0f021e --- /dev/null +++ b/src/interfaces/extension.ts @@ -0,0 +1,248 @@ +import HttpStatus from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +class Extension { + public static ToThaiMonth(value: number) { + switch (value) { + case 1: + return "มกราคม"; + case 2: + return "กุมภาพันธ์"; + case 3: + return "มีนาคม"; + case 4: + return "เมษายน"; + case 5: + return "พฤษภาคม"; + case 6: + return "มิถุนายน"; + case 7: + return "กรกฎาคม"; + case 8: + return "สิงหาคม"; + case 9: + return "กันยายน"; + case 10: + return "ตุลาคม"; + case 11: + return "พฤศจิกายน"; + case 12: + return "ธันวาคม"; + default: + return ""; + } + } + + public static ToThaiShortMonth(value: number) { + switch (value) { + case 1: + return "ม.ค."; + case 2: + return "ก.พ."; + case 3: + return "มี.ค."; + case 4: + return "เม.ย."; + case 5: + return "พ.ค."; + case 6: + return "มิ.ย."; + case 7: + return "ก.ค."; + case 8: + return "ส.ค."; + case 9: + return "ก.ย."; + case 10: + return "ต.ค."; + case 11: + return "พ.ย."; + case 12: + return "ธ.ค."; + default: + return ""; + } + } + + public static ToThaiYear(value: number) { + if (value < 2400) return value + 543; + else return value; + } + + public static ToCeYear(value: number) { + if (value >= 2400) return value - 543; + else return value; + } + + public static ToThaiNumber(value: string) { + let arabicNumbers = "0123456789"; + let thaiNumbers = "๐๑๒๓๔๕๖๗๘๙"; + let result = ""; + for (let digit of value) { + let index = arabicNumbers.indexOf(digit); + if (index >= 0) { + result += thaiNumbers[index]; + } else { + result += digit; + } + } + return result; + } + + public static ToThaiFullDate(value: Date) { + let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear(); + return ( + "วันที่ " + + value.getDate() + + " เดือน " + + Extension.ToThaiMonth(value.getMonth() + 1) + + " พ.ศ. " + + yy + ); + } + + public static ToThaiShortDate(value: Date) { + let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear(); + return ( + "วันที่ " + + value.getDate() + + " " + + Extension.ToThaiShortMonth(value.getMonth() + 1) + + " " + + yy.toString().slice(-2) + ); + } + + public static ToThaiShortDate_noPrefix(value: Date) { + let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear(); + return ( + value.getDate() + + " " + + Extension.ToThaiShortMonth(value.getMonth() + 1) + + " " + + yy.toString().slice(-2) + ); + } + + public static ToThaiShortDate_monthYear(value: Date) { + let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear(); + return value.getDate() + " เดือน " + Extension.ToThaiMonth(value.getMonth() + 1) + " พ.ศ. " + yy; + } + + public static sumObjectValues(array: any, propertyName: any) { + let sum = 0; + for (let i = 0; i < array.length; i++) { + if (array[i][propertyName] !== undefined) { + sum += array[i][propertyName]; + } + } + return sum; + } + + public static CheckCitizen(value: string) { + let citizen = value + if (citizen.length !== 13) { + throw new HttpError(HttpStatus.NOT_FOUND, "กรุณากรอกข้อมูลรหัสบัตรประจำตัวประชาชนให้ครบ 13 หลัก",); + } + const citizenIdDigits = citizen.toString().split("").map(Number); + const cal = + citizenIdDigits[0] * 13 + + citizenIdDigits[1] * 12 + + citizenIdDigits[2] * 11 + + citizenIdDigits[3] * 10 + + citizenIdDigits[4] * 9 + + citizenIdDigits[5] * 8 + + citizenIdDigits[6] * 7 + + citizenIdDigits[7] * 6 + + citizenIdDigits[8] * 5 + + citizenIdDigits[9] * 4 + + citizenIdDigits[10] * 3 + + citizenIdDigits[11] * 2; + const calStp2 = cal % 11; + let chkDigit = 11 - calStp2; + if (chkDigit === 10) { + chkDigit = 1; + } + else if (chkDigit === 11) { + chkDigit = chkDigit % 10; + } + + if (citizenIdDigits[12] !== chkDigit) { + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } + return citizen; + } + + public static CalculateGovAge(appointDate: Date, plusYear: number = 0, subtractYear: number = 0): number { + if (appointDate == null || appointDate == undefined) return 0 + const now = new Date(); + if (now.getMonth() - appointDate.getMonth() >= 6) { + return (now.getFullYear() - appointDate.getFullYear()) + 1 + plusYear - subtractYear; + } + else { + return (now.getFullYear() - appointDate.getFullYear()) + plusYear - subtractYear; + } + } + + public static CalculateAge(appointDate: Date, plusYear: number = 0, subtractYear: number = 0) { + let currentDate = new Date().getTime(); + let appointDateTime = new Date(appointDate).getTime(); + let ageInMilliseconds = currentDate - appointDateTime; + let ageInDays = ageInMilliseconds / (1000 * 60 * 60 * 24); + let years = Math.floor(ageInDays / 365.25) + plusYear - subtractYear; + return years; + } + + public static CalculateAgeStrV2(date: Date, plusYear: number = 0, subtractYear: number = 0) { + if (date == null || date == undefined) return "" + const currentDate = new Date(); + if (date > currentDate) { + throw new Error("วันเกิดต้องไม่มากกว่าวันที่ปัจจุบัน"); + } + + let years = currentDate.getFullYear() - date.getFullYear(); + let months = 0; + let days = 0; + + if (currentDate.getMonth() < date.getMonth() || (currentDate.getMonth() === date.getMonth() && currentDate.getDate() < date.getDate())) { + years--; + months = 12 - date.getMonth() + currentDate.getMonth(); + + if (currentDate.getDate() < date.getDate()) { + months--; + let lastMonthDays = 0; + if (currentDate.getMonth() === 0) { + lastMonthDays = new Date(currentDate.getFullYear() - 1, 11, 0).getDate(); + } + else { + lastMonthDays = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0).getDate(); + days = lastMonthDays - date.getDate() + currentDate.getDate(); + } + } + else { + days = currentDate.getDate() - date.getDate(); + } + } + else { + months = currentDate.getMonth() - date.getMonth(); + + if (currentDate.getDate() < date.getDate()) { + months--; + let lastMonthDays = 0; + if (currentDate.getMonth() === 0) { + lastMonthDays = new Date(currentDate.getFullYear() - 1, 11, 0).getDate(); + } + else { + lastMonthDays = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0).getDate(); + days = lastMonthDays - date.getDate() + currentDate.getDate(); + } + } + else { + days = currentDate.getDate() - date.getDate(); + } + } + years += plusYear - subtractYear; + return `${years} ปี ${months} เดือน ${days} วัน`; + } +} + +export default Extension;