diff --git a/src/modules/04_registryPerson/components/Dialog/01_Information.vue b/src/modules/04_registryPerson/components/Dialog/01_Information.vue index 4923e6d08..1f139003c 100644 --- a/src/modules/04_registryPerson/components/Dialog/01_Information.vue +++ b/src/modules/04_registryPerson/components/Dialog/01_Information.vue @@ -5,7 +5,7 @@ import { useQuasar } from "quasar"; import http from "@/plugins/http"; import config from "@/app.config"; import { useCounterMixin } from "@/stores/mixin"; -import { useProfileDataStore } from "@/modules/04_registryPerson/stores/profile"; +import { calculateAge } from "@/utils/function"; import type { RequestObject } from "@/modules/04_registryPerson/interface/request/Profile"; @@ -17,8 +17,6 @@ const age = defineModel("age", { }); const $q = useQuasar(); -const storeProfile = useProfileDataStore(); -const { filterSelector, calculateAge } = storeProfile; const { date2Thai, messageError, dialogMessageNotify } = useCounterMixin(); const props = defineProps({ diff --git a/src/modules/04_registryPerson/components/Dialog/DialogUpdate.vue b/src/modules/04_registryPerson/components/Dialog/DialogUpdate.vue index ed9e592e4..04859e42c 100644 --- a/src/modules/04_registryPerson/components/Dialog/DialogUpdate.vue +++ b/src/modules/04_registryPerson/components/Dialog/DialogUpdate.vue @@ -9,14 +9,11 @@ import { useCounterMixin } from "@/stores/mixin"; import { useLinkageStore } from "@/stores/linkage"; import { useDataLinkCenter } from "@/modules/04_registryPerson/stores/LinkCenter"; import { useRequestEditStore } from "@/modules/04_registryPerson/stores/RequestEdit"; +import { calculateAge } from "@/utils/function"; import type { DataOption } from "@/modules/04_registryPerson/interface/index/Main"; import type { RequestregistrationAddressObject } from "@/modules/04_registryPerson/interface/request/Address"; import type { RequestObject } from "@/modules/04_registryPerson/interface/request/Profile"; -import type { - FormPerson, - FormChildren, -} from "@/modules/04_registryPerson/interface/index/family"; import Header from "@/components/DialogHeader.vue"; //ส่วนหัว popup import InformationPage from "@/modules/04_registryPerson/components/Dialog/01_Information.vue"; //form ข้อมูลส่วนตัว @@ -268,7 +265,7 @@ async function amiRequest() { formInformations.birthDate = data.value.dateOfBirth ? conventDateOfBirth(`${data.value.dateOfBirth}`) : null; - age.value = storeLinkCenter.calculateAge(data.value.age); + age.value = calculateAge(data.value.dateOfBirth); formInformations.gender = data.value.genderDesc; let registrationAddress = data.value.houseNo ? data.value.houseNo : ""; diff --git a/src/modules/04_registryPerson/components/detail/PersonalInformation/01_Profile.vue b/src/modules/04_registryPerson/components/detail/PersonalInformation/01_Profile.vue index 3ea865b78..7bdf10fe0 100644 --- a/src/modules/04_registryPerson/components/detail/PersonalInformation/01_Profile.vue +++ b/src/modules/04_registryPerson/components/detail/PersonalInformation/01_Profile.vue @@ -6,6 +6,7 @@ import { checkPermission } from "@/utils/permissions"; import { useRoute } from "vue-router"; import { useCounterMixin } from "@/stores/mixin"; import { useProfileDataStore } from "@/modules/04_registryPerson/stores/profile"; +import { calculateAge } from "@/utils/function"; import http from "@/plugins/http"; import config from "@/app.config"; @@ -22,7 +23,7 @@ import DialogHistory from "@/modules/04_registryPerson/components/detail/DialogH const $q = useQuasar(); const route = useRoute(); const store = useProfileDataStore(); -const { calculateAge, fetchPerson, filterSelector } = store; +const { fetchPerson, filterSelector } = store; const { success, showLoader, diff --git a/src/modules/04_registryPerson/stores/LinkCenter.ts b/src/modules/04_registryPerson/stores/LinkCenter.ts index b78d3d3d5..8f16056b6 100644 --- a/src/modules/04_registryPerson/stores/LinkCenter.ts +++ b/src/modules/04_registryPerson/stores/LinkCenter.ts @@ -16,7 +16,6 @@ const mixin = useCounterMixin(); const { showLoader, hideLoader } = mixin; export const useDataLinkCenter = defineStore("DataLinkCenter", () => { - const retireDate = ref(); const optionRelationshipMain = ref([]); const optionRelationship = ref([]); //รายการตัวเลือก @@ -70,32 +69,6 @@ export const useDataLinkCenter = defineStore("DataLinkCenter", () => { subdistrictCOps: [], }); - /** - * ฟังก์ชันคำนวนอายุด้วยวันเดิอนปีเกิด - * @param birthDate วันเกิด - * @returns อายุ - */ - function calculateAge(birthDate: Date | null) { - if (!birthDate) return null; - const birthDateTimeStamp = new Date(birthDate).getTime(); - const now = new Date(); - const diff = now.getTime() - birthDateTimeStamp; - - const ageDate = new Date(diff); - const years = ageDate.getUTCFullYear() - 1970; - const months = ageDate.getUTCMonth(); - const days = ageDate.getUTCDate() - 1; - const retire = new Date(birthDate); - retire.setFullYear(retire.getFullYear() + 60); - retireDate.value = retire; - - if (years > 60) { - return "อายุเกิน 60 ปี"; - } - - return `${years} ปี ${months} เดือน ${days} วัน`; - } - /** * ฟังก์ชันดึงข้อมูลตัวเลือกข้อมูลหลัก */ @@ -351,7 +324,7 @@ export const useDataLinkCenter = defineStore("DataLinkCenter", () => { OpsPerson, OpsAddress, OpsAddressFilter, - calculateAge, + fetchProvince, filterSelector, fetchDistrict, diff --git a/src/modules/04_registryPerson/stores/profile.ts b/src/modules/04_registryPerson/stores/profile.ts index 42c8073b5..6bda45bb5 100644 --- a/src/modules/04_registryPerson/stores/profile.ts +++ b/src/modules/04_registryPerson/stores/profile.ts @@ -15,8 +15,6 @@ const mixin = useCounterMixin(); const { showLoader, hideLoader } = mixin; export const useProfileDataStore = defineStore("profile", () => { - const retireDate = ref(); - //ข้อมูลส่วนตัว const defaultProfile: RequestObject = { bloodGroup: null, @@ -69,32 +67,6 @@ export const useProfileDataStore = defineStore("profile", () => { ], }); - /** - * ฟังก์ชันคำนวนอายุด้วยวันเดิอนปีเกิด - * @param birthDate วันเกิด - * @returns อายุ - */ - function calculateAge(birthDate: Date | null) { - if (!birthDate) return null; - const birthDateTimeStamp = new Date(birthDate).getTime(); - const now = new Date(); - const diff = now.getTime() - birthDateTimeStamp; - - const ageDate = new Date(diff); - const years = ageDate.getUTCFullYear() - 1970; - const months = ageDate.getUTCMonth(); - const days = ageDate.getUTCDate() - 1; - const retire = new Date(birthDate); - retire.setFullYear(retire.getFullYear() + 60); - retireDate.value = retire; - - if (years > 60) { - return "อายุเกิน 60 ปี"; - } - - return `${years} ปี ${months} เดือน ${days} วัน`; - } - /** * ฟังก์ชันดึงข้อมูลตัวเลือกข้อมูลหลัก */ @@ -247,11 +219,9 @@ export const useProfileDataStore = defineStore("profile", () => { return { defaultProfile, - retireDate, Ops, OpsFilter, - calculateAge, fetchPerson, filterSelector, }; diff --git a/src/modules/05_placement/components/Receive/ReceiveAddPerson.vue b/src/modules/05_placement/components/Receive/ReceiveAddPerson.vue index c305c14d1..e6d2da0ce 100644 --- a/src/modules/05_placement/components/Receive/ReceiveAddPerson.vue +++ b/src/modules/05_placement/components/Receive/ReceiveAddPerson.vue @@ -4,6 +4,7 @@ import { useQuasar } from "quasar"; import { useRouter } from "vue-router"; import { useCounterMixin } from "@/stores/mixin"; +import { calculateAge } from "@/utils/function"; import http from "@/plugins/http"; import config from "@/app.config"; @@ -16,7 +17,6 @@ import type { FormAddPerson, } from "@/modules/05_placement/interface/index/ProfileType"; -const retireDate = ref(); const router = useRouter(); const $q = useQuasar(); const mixin = useCounterMixin(); @@ -25,14 +25,12 @@ const { success, dateToISO, messageError, - dialogMessageNotify, showLoader, hideLoader, dialogConfirm, } = mixin; const age = ref(""); -const defaultCitizenData = ref(""); const informaData = ref({ prefix: "", firstName: "", @@ -283,32 +281,6 @@ function clickBack() { router.push("/placement/receive"); } -/** - * ฟังก์ชันคำนวนอายุ - * @param birthDate วันเดือยปีเกิด - * @returns อายุ แบบ ปี เดือน วัน - */ -function calculateAge(birthDate: Date | null) { - if (!birthDate) return null; - const birthDateTimeStamp = new Date(birthDate).getTime(); - const now = new Date(); - const diff = now.getTime() - birthDateTimeStamp; - - const ageDate = new Date(diff); - const years = ageDate.getUTCFullYear() - 1970; - const months = ageDate.getUTCMonth(); - const days = ageDate.getUTCDate() - 1; - const retire = new Date(birthDate); - retire.setFullYear(retire.getFullYear() + 60); - retireDate.value = retire; - - if (years > 60) { - return "อายุเกิน 60 ปี"; - } - - return `${years} ปี ${months} เดือน ${days} วัน`; -} - /** * ฟังก์ชันอัปเดทวันเดือนปีเกิด * @param v วันเดือยปีเกิด diff --git a/src/modules/05_placement/components/Receive/receiveDetail.vue b/src/modules/05_placement/components/Receive/receiveDetail.vue index a31636fad..2b680ec90 100644 --- a/src/modules/05_placement/components/Receive/receiveDetail.vue +++ b/src/modules/05_placement/components/Receive/receiveDetail.vue @@ -5,6 +5,7 @@ import { useQuasar } from "quasar"; import { useRoute, useRouter } from "vue-router"; import { useCounterMixin } from "@/stores/mixin"; import { checkPermission } from "@/utils/permissions"; +import { calculateAge } from "@/utils/function"; import http from "@/plugins/http"; import config from "@/app.config"; @@ -264,8 +265,6 @@ async function getData() { salary.value = data.amountOld ?? 0; reason.value = data.reason ?? ""; status.value = data.status ?? ""; - - calculateAge(data.dateOfBirth); }) .catch((e) => { messageError($q, e); @@ -510,31 +509,6 @@ function calculateMaxDate() { return today; } -/** - * ฟังก์ชันคำนวนอายุ - * @param birthDate วันเดือยปีเกิด - * @returns อายุ แบบ ปี เดือน วัน - */ -function calculateAge(birthDate: Date | null) { - if (!birthDate) return null; - const birthDateTimeStamp = new Date(birthDate).getTime(); - const now = new Date(); - const diff = now.getTime() - birthDateTimeStamp; - - const ageDate = new Date(diff); - const years = ageDate.getUTCFullYear() - 1970; - const months = ageDate.getUTCMonth(); - const days = ageDate.getUTCDate() - 1; - const retire = new Date(birthDate); - retire.setFullYear(retire.getFullYear() + 60); - - if (years > 60) { - return "อายุเกิน 60 ปี"; - } - - return `${years} ปี ${months} เดือน ${days} วัน`; -} - /** * ฟังก์ชันอัปเดทวันเดือนปีเกิด * @param v วันเดือยปีเกิด diff --git a/src/modules/08_registryEmployee/components/DialogAddEmployee.vue b/src/modules/08_registryEmployee/components/DialogAddEmployee.vue index f47d64b92..74698a63a 100644 --- a/src/modules/08_registryEmployee/components/DialogAddEmployee.vue +++ b/src/modules/08_registryEmployee/components/DialogAddEmployee.vue @@ -8,13 +8,13 @@ import config from "@/app.config"; import type { DataOption } from "@/modules/08_registryEmployee/interface/index/Main"; import type { FormDataEmployee } from "@/modules/08_registryEmployee/interface/request/Employee"; import type { ResOptionPerson } from "@/modules/08_registryEmployee/interface/response/Employee"; +import { calculateAge } from "@/utils/function"; /** importComponents*/ import DialogHeader from "@/components/DialogHeader.vue"; /** importStore*/ import { useCounterMixin } from "@/stores/mixin"; -import { useRegistryEmp } from "@/modules/08_registryEmployee/stores/registry-employee"; /** use*/ const $q = useQuasar(); @@ -27,7 +27,6 @@ const { dialogMessageNotify, date2Thai, } = useCounterMixin(); -const { calculateAge } = useRegistryEmp(); /** props*/ const modal = defineModel("modal", { required: true }); diff --git a/src/modules/08_registryEmployee/stores/registry-employee.ts b/src/modules/08_registryEmployee/stores/registry-employee.ts index fd1e00c00..e4ec4b071 100644 --- a/src/modules/08_registryEmployee/stores/registry-employee.ts +++ b/src/modules/08_registryEmployee/stores/registry-employee.ts @@ -1,32 +1,6 @@ import { defineStore } from "pinia"; export const useRegistryEmp = defineStore("registryEmployeeTemp", () => { - /** - * function คำนวณหาอายุ - * @param birthDate วันเกิด - * @returns อายุ - */ - function calculateAge(birthDate: Date | null) { - if (!birthDate) return null; - const birthDateTimeStamp = new Date(birthDate).getTime(); - const now = new Date(); - const diff = now.getTime() - birthDateTimeStamp; - - const ageDate = new Date(diff); - const years = ageDate.getUTCFullYear() - 1970; - const months = ageDate.getUTCMonth(); - const days = ageDate.getUTCDate() - 1; - const retire = new Date(birthDate); - retire.setFullYear(retire.getFullYear() + 60); - // retireDate.value = retire; - - if (years > 60) { - return "อายุเกิน 60 ปี"; - } - - return `${years} ปี ${months} เดือน ${days} วัน`; - } - /** * function convertstatus * @param val ค่าสภานะ @@ -53,7 +27,6 @@ export const useRegistryEmp = defineStore("registryEmployeeTemp", () => { } }; return { - calculateAge, statusText, }; }); diff --git a/src/utils/function.ts b/src/utils/function.ts index 445aaee30..d24194346 100644 --- a/src/utils/function.ts +++ b/src/utils/function.ts @@ -1,3 +1,5 @@ +import moment from "moment"; + /** * คำนวณหน้าที่จะแสดงหลังจากลบข้อมูล * @@ -28,3 +30,33 @@ export function calculateFiscalYear(date: Date) { const month = date.getMonth() + 1; return month >= 10 ? date.getFullYear() + 1 : date.getFullYear(); } + +/** + * คำนวณอายุจากวันเกิด + * @param birthDate วันเกิด + * @returns อายุในรูปแบบ ปี เดือน วัน หรือ ข้อความ "อายุเกิน 60 ปี" หรือ null หากวันเกิดเป็น null + */ +export function calculateAge(birthDate: Date | null) { + if (!birthDate) return null; + + // .startOf('day') จะเซ็ตเวลาเป็น 00:00:00.000 ของวันนั้นๆ + const birth = moment(birthDate).startOf("day"); + const now = moment().startOf("day"); + + if (!birth.isValid()) return "Invalid Date"; + + // คราวนี้การลบกันจะสนแค่ "วันที่" เท่านั้น + const years = now.diff(birth, "years"); + birth.add(years, "years"); + + const months = now.diff(birth, "months"); + birth.add(months, "months"); + + const days = now.diff(birth, "days"); + + if (years >= 60) { + return "อายุเกิน 60 ปี"; + } + + return `${years} ปี ${months} เดือน ${days} วัน`; +}