import { defineStore } from "pinia";
import "moment/dist/locale/th";
import moment from "moment";
import CustomComponent from "@/components/CustomDialog.vue";
import { Loading, QSpinnerCube } from "quasar";
import { logout } from "@/plugins/auth";
import { format, utcToZonedTime } from "date-fns-tz";
moment.locale("th");
export const useCounterMixin = defineStore("mixin", () => {
/**
* ฟังก์ชันกลาง
*/
const calAge = (
srcDate: Date,
birthCal: Date = new Date(),
eng: boolean = false
) => {
const year = eng ? "years" : "ปี";
const month = eng ? "months" : "เดือน";
const day = eng ? "days" : "วัน";
if (srcDate == null) {
return `0 ${year} 0 ${month} 0 ${day}`;
}
const toDay = birthCal;
const birth = new Date(srcDate);
const yearNow = toDay.getFullYear();
const monthNow = toDay.getMonth();
const dateNow = toDay.getDate();
const yearDob = birth.getFullYear();
const monthDob = birth.getMonth();
const dateDob = birth.getDate();
const lastYear = 12;
const subtractDate: Object = moment().subtract(1, "months").endOf("month");
const lastMonths = new Date(subtractDate.toString()).getDate();
let yearAge = yearNow - yearDob;
let monthAge = 0;
let dateAge = 0;
if (monthNow >= monthDob) {
monthAge = monthNow - monthDob;
} else {
yearAge--;
monthAge = lastYear + monthNow - monthDob;
}
if (dateNow >= dateDob) {
dateAge = dateNow - dateDob;
} else {
monthAge--;
dateAge = lastMonths + dateNow - dateDob;
if (monthAge < 0) {
monthAge = 11;
yearAge--;
}
}
const age = {
years: yearAge,
months: monthAge,
days: dateAge,
};
return `${yearAge} ${year} ${monthAge} ${month} ${dateAge} ${day}`;
};
function date2Thai(
srcDate: Date | null,
isFullMonth: boolean = false,
isTime: boolean = false
) {
if (srcDate == null || !moment(srcDate).isValid()) return "";
const dateMoment = moment(srcDate);
const day = dateMoment.format("DD");
const month = dateMoment.format(isFullMonth ? "MMMM" : "MMM");
const year = +dateMoment.format("YYYY") + 543;
return `${day} ${month} ${year}${
isTime ? dateMoment.format(" HH:mm น.") : ""
}`;
}
function dateMonth2Thai(srcDate: Date, isFullMonth = false, isTime = false) {
if (!srcDate) return srcDate;
const date = new Date(srcDate);
const isValidDate = Boolean(+date);
if (!isValidDate) return srcDate;
if (isValidDate && date.getFullYear() < 1000) return srcDate;
const fullMonthThai = [
"มกราคม",
"กุมภาพันธ์",
"มีนาคม",
"เมษายน",
"พฤษภาคม",
"มิถุนายน",
"กรกฎาคม",
"สิงหาคม",
"กันยายน",
"ตุลาคม",
"พฤศจิกายน",
"ธันวาคม",
];
const abbrMonthThai = [
"ม.ค.",
"ก.พ.",
"มี.ค.",
"เม.ย.",
"พ.ค.",
"มิ.ย.",
"ก.ค.",
"ส.ค.",
"ก.ย.",
"ต.ค.",
"พ.ย.",
"ธ.ค.",
];
let dstYear = 0;
if (date.getFullYear() > 2500) {
dstYear = date.getFullYear();
} else {
dstYear = date.getFullYear() + 543;
}
let dstMonth = "";
if (isFullMonth) {
dstMonth = fullMonthThai[date.getMonth()];
} else {
dstMonth = abbrMonthThai[date.getMonth()];
}
let dstTime = "";
if (isTime) {
const H = date.getHours().toString().padStart(2, "0");
const M = date.getMinutes().toString().padStart(2, "0");
// const S = date.getSeconds().toString().length === 1 ? "0" + date.getSeconds() : date.getSeconds()
// dstTime = " " + H + ":" + M + ":" + S + " น."
dstTime = " " + H + ":" + M + " น.";
}
return date.getDate().toString().padStart(2, "0") + " " + dstMonth;
}
const calAgeYear = (srcDate: Date, birthCal: Date = new Date()) => {
const toDay = birthCal;
const birth = new Date(srcDate);
const yearNow = toDay.getFullYear();
const monthNow = toDay.getMonth();
const dateNow = toDay.getDate();
const yearDob = birth.getFullYear();
const monthDob = birth.getMonth();
const dateDob = birth.getDate();
const lastYear = 12;
const subtractDate: Object = moment().subtract(1, "months").endOf("month");
const lastMonths = new Date(subtractDate.toString()).getDate();
let yearAge = yearNow - yearDob;
let monthAge = 0;
let dateAge = 0;
if (monthNow >= monthDob) {
monthAge = monthNow - monthDob;
} else {
yearAge--;
monthAge = lastYear + monthNow - monthDob;
}
if (dateNow >= dateDob) {
dateAge = dateNow - dateDob;
} else {
monthAge--;
dateAge = lastMonths + dateNow - dateDob;
if (monthAge < 0) {
monthAge = 11;
yearAge--;
}
}
return yearAge;
};
function monthYear2Thai(month: number, year: number, isFullMonth = false) {
if (
month < 0 ||
month > 11 ||
!Number.isFinite(month) ||
!Number.isFinite(year)
)
return "";
const fullMonthThai = [
"มกราคม",
"กุมภาพันธ์",
"มีนาคม",
"เมษายน",
"พฤษภาคม",
"มิถุนายน",
"กรกฎาคม",
"สิงหาคม",
"กันยายน",
"ตุลาคม",
"พฤศจิกายน",
"ธันวาคม",
];
const abbrMonthThai = [
"ม.ค.",
"ก.พ.",
"มี.ค.",
"เม.ย.",
"พ.ค.",
"มิ.ย.",
"ก.ค.",
"ส.ค.",
"ก.ย.",
"ต.ค.",
"พ.ย.",
"ธ.ค.",
];
// assume year is in BE if > 2500
let dstYear = year > 2500 ? year : year + 543;
// month is already 0-based
let dstMonth = isFullMonth ? fullMonthThai[month] : abbrMonthThai[month];
return `${dstMonth} ${dstYear}`;
}
function dateToISO(date: Date) {
if (date != null) {
const srcDate = new Date(date);
return (
srcDate.getFullYear() +
"-" +
appendLeadingZeroes(srcDate.getMonth() + 1) +
"-" +
appendLeadingZeroes(srcDate.getDate())
);
} else {
return "";
}
}
const convertDate = (date: string) => {
if (date != null && date.length == 10) {
const srcDate = date.toString().split("/");
const dateVal =
Number(srcDate[2]) - 543 + "-" + srcDate[1] + "-" + srcDate[0];
const check = moment(dateVal).isValid();
if (check) {
return {
isValid: check,
value: dateVal,
};
} else {
return {
isValid: check,
value: "",
};
}
} else {
return {
isValid: false,
value: "",
};
}
};
const convertDateDisplay = (dateVal: Date) => {
const date = new Date(dateVal);
let dstYear = date.getFullYear() + 543;
let dstMonth = date.getMonth() + 1;
return (
date.getDate().toString().padStart(2, "0") +
"/" +
dstMonth.toString().padStart(2, "0") +
"/" +
dstYear
);
};
function appendLeadingZeroes(n: number) {
if (n <= 9) return "0" + n;
return n;
}
function textToPhone(n: string) {
const p = n.substr(0, 3) + "-" + n.substr(3, 3) + "-" + n.substr(6, 4);
return p;
}
function textToFax(n: string) {
const p = n.substr(0, 2) + "-" + n.substr(2, 3) + "-" + n.substr(5, 4);
return p;
}
const success = (q: any, val: string) => {
// useQuasar ไม่สามารถใช้นอกไฟล์ .vue
if (val !== "") {
return q.notify({
message: val,
color: "primary",
icon: "mdi-information",
position: "bottom-right",
multiLine: true,
timeout: 1000,
badgeColor: "positive",
classes: "my-notif-class",
});
}
};
function notify(q: any, val: string) {
if (val !== "") {
q.notify({
color: "teal-10",
message: val,
icon: "mdi-information",
position: "bottom-right",
multiLine: true,
timeout: 7000,
actions: [{ label: "ปิด", color: "white", handler: () => {} }],
});
}
}
function notifyError(q: any, val: string) {
if (val !== "") {
q.notify({
color: "negative",
message: val,
icon: "mdi-alert-circle",
position: "top",
multiLine: true,
timeout: 12000,
actions: [{ label: "ปิด", color: "white", handler: () => {} }],
});
}
}
function notifyWarring(q: any, val: string) {
if (val !== "") {
q.notify({
color: "warning",
message: val,
icon: "mdi-alert-circle",
position: "top",
multiLine: true,
timeout: 12000,
actions: [{ label: "ปิด", color: "white", handler: () => {} }],
});
}
}
const messageError = (q: any, e: any = "", msg: string = "") => {
if (e.response !== undefined) {
if (e.response.data.status !== undefined) {
if (e.response.data.status == 401) {
//invalid_token
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: `ล็อกอินหมดอายุ กรุณาล็อกอินใหม่อีกครั้ง`,
icon: "warning",
color: "red",
onlycancel: true,
},
}).onCancel(async () => {
showLoader();
await logout();
setTimeout(() => {
hideLoader();
}, 1000);
});
} else if (e.response.data.status == 400 && e.response.data.title) {
//validation errors
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: `${e.response.data.title}`,
icon: "warning",
color: "red",
onlycancel: true,
},
});
} else if (e.response.data.status != 403) {
const message = e.response.data.result ?? e.response.data.message;
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: `${message}`,
icon: "warning",
color: "red",
onlycancel: true,
},
});
}
} else {
if (e.response.status == 401) {
if (msg !== "") {
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: msg,
icon: "warning",
color: "red",
onlycancel: true,
},
}).onCancel(async () => {
showLoader();
await logout();
setTimeout(() => {
hideLoader();
}, 1000);
});
} else {
//invalid_token
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: `ล็อกอินหมดอายุ กรุณาล็อกอินใหม่อีกครั้ง`,
icon: "warning",
color: "red",
onlycancel: true,
},
}).onCancel(async () => {
showLoader();
await logout();
setTimeout(() => {
hideLoader();
}, 1000);
});
}
} else if (e.response.data.successful === false) {
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: e.response.data.message,
icon: "warning",
color: "red",
onlycancel: true,
},
});
} else {
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: `ข้อมูลผิดพลาดทำให้เกิดการไม่ตอบสนองต่อการเรียกใช้งานดูเว็บไซต์`,
icon: "warning",
color: "red",
onlycancel: true,
},
});
}
}
} else {
if (e.message && e.message !== "R is not a function") {
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: e.message,
icon: "warning",
color: "red",
onlycancel: true,
},
});
} else {
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: `ข้อมูลผิดพลาดทำให้เกิดการไม่ตอบสนองต่อการเรียกใช้งานดูเว็บไซต์`,
icon: "warning",
color: "red",
onlycancel: true,
},
});
}
}
};
const messageErrorLinkage = (q: any, e: any = "") => {
try {
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message:
e.message == "Failed to fetch"
? "ไม่สามารถเชื่อมต่อกับฐานข้อมูลกรมการปกครองได้"
: e.message,
icon: "warning",
color: "red",
onlycancel: true,
},
});
} catch (error) {
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: `ไม่สามารถเชื่อมต่อกับฐานข้อมูลกรมการปกครองได้`,
icon: "warning",
color: "red",
onlycancel: true,
},
});
}
};
const fails = (q: any, val: string) => {
// useQuasar ไม่สามารถใช้นอกไฟล์ .vue
if (val !== "") {
return q.notify({
message: val,
color: "red",
icon: "mdi-information",
position: "bottom-right",
multiLine: true,
timeout: 1000,
badgeColor: "positive",
classes: "my-notif-class",
});
}
};
const dialogMessage = (
// ไม่เอาใส่ undefined
q: any,
title: string | undefined,
message: string | undefined,
icon: string | undefined,
textOk: string | undefined,
color: string | undefined,
ok?: Function | undefined,
cancel?: Function | undefined,
onlycancel: Boolean = false
) => {
q.dialog({
component: CustomComponent,
componentProps: {
title: title,
message: message,
icon: icon,
color: color,
textOk: textOk,
onlycancel: onlycancel,
},
})
.onOk(() => {
if (ok != undefined) ok();
})
.onCancel(() => {
if (cancel != undefined) cancel();
});
};
//*** Dialog ***//
const dialogConfirm = (
q: any,
ok?: Function,
title?: string, // ถ้ามี cancel action ใส่เป็น null
desc?: string, // ถ้ามี cancel action ใส่เป็น null
cancel?: Function
) => {
q.dialog({
component: CustomComponent,
componentProps: {
title: title && title != null ? title : "ยืนยันการบันทึก",
message:
desc && desc != null
? desc
: "ต้องการยืนยันการบันทึกข้อมูลนี้ใช่หรือไม่?",
icon: "info",
color: "public",
textOk: "ตกลง",
onlycancel: false,
},
})
.onOk(() => {
if (ok) ok();
})
.onCancel(() => {
if (cancel) cancel();
});
};
const dialogRemove = (
q: any,
ok?: Function,
title?: string, // ถ้ามี cancel action ใส่เป็น null
desc?: string, // ถ้ามี cancel action ใส่เป็น null
cancel?: Function
) => {
q.dialog({
component: CustomComponent,
componentProps: {
title: title && title != null ? title : "ยืนยันการลบข้อมูล",
message:
desc && desc != null
? desc
: "ต้องการยืนยันการลบข้อมูลนี้ใช่หรือไม่?",
icon: "delete",
color: "red",
textOk: "ตกลง",
onlycancel: false,
},
})
.onOk(() => {
if (ok) ok();
})
.onCancel(() => {
if (cancel) cancel();
});
};
const dialogMessageNotify = (
q: any,
desc?: string, // ถ้ามี cancel action ใส่เป็น null
cancel?: Function
) => {
q.dialog({
component: CustomComponent,
componentProps: {
title: "ข้อความแจ้งเตือน",
message: desc && desc != null ? desc : "กรุณากรอกข้อมูลให้ครบ",
icon: "warning",
color: "orange",
textOk: "ตกลง",
onlycancel: true,
},
}).onCancel(() => {
if (cancel) cancel();
});
};
//*** END Dialog ***//
const showLoader = () => {
Loading.show({
spinner: QSpinnerCube,
spinnerSize: 140,
spinnerColor: "primary",
backgroundColor: "white",
});
};
const hideLoader = () => {
Loading.hide();
};
function modalDelete(
q: any,
title: string,
message: string,
ok: Function,
cancel?: Function
) {
q.dialog({
title: `${title}`,
message: `${message}`,
cancel: {
flat: true,
color: "grey-14",
},
ok: {
color: "red-6",
},
focus: "none",
persistent: true,
html: true,
})
.onOk(() => {
ok();
})
.onCancel(() => {
if (cancel != undefined) cancel();
})
.onDismiss(() => {});
}
function modalConfirm(
q: any,
title: string,
message: string,
ok: Function,
cancel?: Function
) {
q.dialog({
title: `${title}`,
message: `${message}`,
cancel: {
flat: true,
color: "grey",
},
ok: {
color: "primary",
},
focus: "none",
persistent: true,
html: true,
})
.onOk(() => {
ok();
})
.onCancel(() => {
if (cancel != undefined) cancel();
})
.onDismiss(() => {});
}
function modalWarning(q: any, title: string, message: string, ok?: Function) {
// q.dialog({
// title: `${title}`,
// message: `${message}`,
// ok: {
// push: true,
// color: "primary",
// },
// focus: "none",
// persistent: true,
// html: true,
// })
// .onOk(() => {
// if (ok != undefined) ok();
// })
// .onCancel(() => {})
// .onDismiss(() => {});
q.dialog({
component: CustomComponent,
componentProps: {
title: title,
message: message,
icon: "warning",
color: "warning",
onlycancel: true,
},
});
}
function modalError(q: any, title: string, message: string, ok?: Function) {
// q.dialog({
// title: `${title}`,
// message: `${message}`,
// ok: {
// push: true,
// color: "primary",
// },
// focus: "none",
// persistent: true,
// html: true,
// })
// .onOk(() => {
// if (ok != undefined) ok();
// })
// .onCancel(() => {})
// .onDismiss(() => {});
q.dialog({
component: CustomComponent,
componentProps: {
title: title,
message: message,
icon: "warning",
color: "red",
onlycancel: true,
},
});
}
const dateText = (val: Date) => {
if (val != null) {
return date2Thai(val);
} else {
return "-";
}
};
/**
* แปลงช่วงวันที่ถ้า2ค่าเป็นวันเดียวกันจะโชววันเดียวแต่ถ้าไม่เท่ากันจะแสดงเป็นช่วง
* @param val ช่วงวันที่
*/
const dateThaiRange = (val: [Date, Date]) => {
if (val === null) {
return "";
} else if (date2Thai(val[0]) === date2Thai(val[1])) {
return `${date2Thai(val[0])}`;
} else {
return `${date2Thai(val[0])} - ${date2Thai(val[1])}`;
}
};
const weekThai = (val: Number) => {
switch (val) {
case 0:
return "วันอาทิตย์";
case 1:
return "วันจันทร์";
case 2:
return "วันอังคาร";
case 3:
return "วันพุธ";
case 4:
return "วันพฤหัสบดี";
case 5:
return "วันศุกร์";
case 6:
return "วันเสาร์";
default:
return "-";
}
};
const genColor15 = (val: number) => {
val = val % 15;
switch (val) {
case 1:
return "pink";
case 2:
return "purple";
case 3:
return "deep-purple";
case 4:
return "indigo";
case 5:
return "blue";
case 6:
return "light-blue";
case 7:
return "cyan";
case 8:
return "teal";
case 9:
return "green";
case 10:
return "light-green";
case 11:
return "amber";
case 12:
return "orange";
case 13:
return "deep-orange";
case 14:
return "brown";
case 0:
return "blue-grey";
default:
return "";
}
};
const typeCategoryExam = (val: string) => {
switch (val) {
case "hygiene":
return "สำนักอนามัย";
case "physician":
return "สำนักการแพทย์";
case "city":
return "สำนักผังเมือง";
case "culture":
return "สำนักวัฒนธรรม กีฬา และการท่องเที่ยว";
default:
return "-";
}
};
const typeRetire = (val: string) => {
switch (val) {
case "retire":
return "เกษียณอายุราชการ";
case "resign":
return "ลาออก";
case "transfer":
return "ให้โอน";
case "death":
return "ถึงแก่กรรม";
case "layoff":
return "ให้ออก";
case "discharge":
return "ปลดออก";
case "dismiss":
return "ไล่ออก";
case "other":
return "อื่นๆ";
default:
return "-";
}
};
const typeChangeName = (val: string) => {
switch (val) {
case "prefix":
return "เปลี่ยนคำนำหน้าชื่อ";
case "firstName":
return "เปลี่ยนชื่อ";
case "lastName":
return "เปลี่ยนนามสกุล";
case "all":
return "เปลี่ยนคำนำหน้าชื่อ, ชื่อ-นามสกุล";
case "firstNameLastName":
return "เปลี่ยนชื่อ-นามสกุล";
case "prefixAndlastName":
return "เปลี่ยนคำนำหน้าชื่อ และนามสกุล";
default:
return "-";
}
};
const statusLeave = (val: string) => {
switch (val) {
case "waitting":
return "รออนุมัติ";
case "reject":
return "ไม่ผ่านการอนุมัติ";
case "approve":
return "ผ่านการอนุมัติ";
case "cancel":
return "ยกเลิก";
default:
return "-";
}
};
/**
* ฟังก์ชั่นคำนวนจำนวน ปี เดือน วัน
* @param startDate วันเริ่มต้น format MM-DD-YYYY"
* @param endDate วันสิ้นสุด format MM-DD-YYYY"
* @returns ผลการคำนวน ปี เดือน วัน ในรูปแบบ 1 ปี 10 เดือน 5 วัน
*/
function calculateDurationYmd(startDate: any, endDate: any) {
if (!startDate || !endDate) return "";
let start = moment(startDate).startOf("day");
let end = moment(endDate).startOf("day").add(1, "day");
// สลับค่าเพื่อให้ end มากกว่า start เสมอ
if (start > end) [start, end] = [end, start];
const years = end.diff(start, "years");
start.add(years, "years");
const months = end.diff(start, "months");
start.add(months, "months");
const days = end.diff(start, "days");
// การแสดงผล
const result = [];
if (years > 0) result.push(`${years} ปี`);
if (months > 0) result.push(`${months} เดือน`);
if (days > 0) result.push(`${days} วัน`);
return result.length > 0 ? result.join(" ") : "0 วัน";
}
function diffDay(startDate: any, endDate: any) {
if (!startDate || !endDate) return 0;
const start = moment(startDate).startOf("day");
const end = moment(endDate).startOf("day");
// คืนค่าความต่างเป็นจำนวนวัน (ตัวเลขจำนวนเต็ม)
return end.diff(start, "days");
}
function findOrgName(obj: any) {
if (obj) {
let name =
obj.child4 != null &&
obj.child4 !== "" &&
obj.child3 != null &&
obj.child3 !== ""
? obj.child4 + " "
: obj.child4 != null && obj.child4 !== ""
? obj.child4
: "";
name +=
obj.child3 != null &&
obj.child3 !== "" &&
obj.child2 != null &&
obj.child2 !== ""
? obj.child3 + " "
: obj.child3 != null && obj.child3 !== ""
? obj.child3
: "";
name +=
obj.child2 != null &&
obj.child2 !== "" &&
obj.child1 != null &&
obj.child1 !== ""
? obj.child2 + " "
: obj.child2 != null && obj.child2 !== ""
? obj.child2
: "";
name +=
obj.child1 != null &&
obj.child1 !== "" &&
obj.root != null &&
obj.root !== ""
? obj.child1 + " "
: obj.child1 != null && obj.child1 !== ""
? obj.child1
: "";
name += obj.root != null && obj.root !== "" ? obj.root : "";
return name == "" ? "-" : name;
} else {
return "";
}
}
function findOrgNameOld(obj: any) {
if (obj) {
let name =
obj.child4Old != null &&
obj.child4Old !== "" &&
obj.child3Old != null &&
obj.child3Old !== ""
? obj.child4Old + " "
: obj.child4Old != null && obj.child4Old !== ""
? obj.child4Old
: "";
name +=
obj.child3Old != null &&
obj.child3Old !== "" &&
obj.child2Old != null &&
obj.child2Old !== ""
? obj.child3Old + " "
: obj.child3Old != null && obj.child3Old !== ""
? obj.child3Old
: "";
name +=
obj.child2Old != null &&
obj.child2Old !== "" &&
obj.child1Old != null &&
obj.child1Old !== ""
? obj.child2Old + " "
: obj.child2Old != null && obj.child2Old !== ""
? obj.child2Old
: "";
name +=
obj.child1Old != null &&
obj.child1Old !== "" &&
obj.rootOld != null &&
obj.rootOld !== ""
? obj.child1Old + " "
: obj.child1Old != null && obj.child1Old !== ""
? obj.child1Old
: "";
name += obj.rootOld != null && obj.rootOld !== "" ? obj.rootOld : "";
return name == "" ? "-" : name;
} else {
return "";
}
}
function findOrgChildName(obj: any) {
if (obj) {
let name =
obj.orgChild4Name != null &&
obj.orgChild4Name !== "" &&
obj.orgChild3Name != null &&
obj.orgChild3Name !== ""
? obj.orgChild4Name + " "
: obj.orgChild4Name != null && obj.orgChild4Name !== ""
? obj.orgChild4Name
: "";
name +=
obj.orgChild3Name != null &&
obj.orgChild3Name !== "" &&
obj.orgChild2Name != null &&
obj.orgChild2Name !== ""
? obj.orgChild3Name + " "
: obj.orgChild3Name != null && obj.orgChild3Name !== ""
? obj.orgChild3Name
: "";
name +=
obj.orgChild2Name != null &&
obj.orgChild2Name !== "" &&
obj.orgChild1Name != null &&
obj.orgChild1Name !== ""
? obj.orgChild2Name + " "
: obj.orgChild2Name != null && obj.orgChild2Name !== ""
? obj.orgChild2Name
: "";
name +=
obj.orgChild1Name != null &&
obj.orgChild1Name !== "" &&
obj.orgRootName != null &&
obj.orgRootName !== ""
? obj.orgChild1Name + " "
: obj.orgChild1Name != null && obj.orgChild1Name !== ""
? obj.orgChild1Name
: "";
name +=
obj.orgRootName != null && obj.orgRootName !== ""
? obj.orgRootName
: "";
return name == "" ? "-" : name;
} else {
return "";
}
}
function findPosMasterNo(obj: any) {
if (obj) {
let shortName =
(obj.child4ShortName != null && obj.child4ShortName !== ""
? obj.child4ShortName
: obj.child3ShortName != null && obj.child3ShortName !== ""
? obj.child3ShortName
: obj.child2ShortName != null && obj.child2ShortName !== ""
? obj.child2ShortName
: obj.child1ShortName != null && obj.child1ShortName !== ""
? obj.child1ShortName
: obj.rootShortName != null && obj.rootShortName !== ""
? obj.rootShortName
: "") +
(obj.posMasterNo != null && obj.posMasterNo !== ""
? obj.posMasterNo
: "");
return shortName == "" ? "-" : shortName;
} else {
return "";
}
}
function findOrgNameHtml(obj: any) {
if (obj) {
let name =
obj.child4 != null &&
obj.child4 !== "" &&
obj.child3 != null &&
obj.child3 !== ""
? obj.child4 + (obj.child3 ? "\n" : "")
: obj.child4 != null && obj.child4 !== ""
? obj.child4
: "";
name +=
obj.child3 != null &&
obj.child3 !== "" &&
obj.child2 != null &&
obj.child2 !== ""
? obj.child3 + (obj.child2 ? "\n" : "")
: obj.child3 != null && obj.child3 !== ""
? obj.child3
: "";
name +=
obj.child2 != null &&
obj.child2 !== "" &&
obj.child1 != null &&
obj.child1 !== ""
? obj.child2 + (obj.child1 ? "\n" : "")
: obj.child2 != null && obj.child2 !== ""
? obj.child2
: "";
name +=
obj.child1 != null &&
obj.child1 !== "" &&
obj.root != null &&
obj.root !== ""
? obj.child1 + (obj.root ? "\n" : "")
: obj.child1 != null && obj.child1 !== ""
? obj.child1
: "";
name += obj.root != null && obj.root !== "" ? obj.root : "";
return name == "" ? "-" : name;
} else {
return "";
}
}
function findOrgNameOldHtml(obj: any) {
if (obj) {
let name =
obj.child4Old != null &&
obj.child4Old !== "" &&
obj.child3Old != null &&
obj.child3Old !== ""
? obj.child4Old + (obj.child3Old ? "\n" : "")
: obj.child4Old != null && obj.child4Old !== ""
? obj.child4Old
: "";
name +=
obj.child3Old != null &&
obj.child3Old !== "" &&
obj.child2Old != null &&
obj.child2Old !== ""
? obj.child3Old + (obj.child2Old ? "\n" : "")
: obj.child3Old != null && obj.child3Old !== ""
? obj.child3Old
: "";
name +=
obj.child2Old != null &&
obj.child2Old !== "" &&
obj.child1Old != null &&
obj.child1Old !== ""
? obj.child2Old + (obj.child1Old ? "\n" : "")
: obj.child2Old != null && obj.child2Old !== ""
? obj.child2Old
: "";
name +=
obj.child1Old != null &&
obj.child1Old !== "" &&
obj.rootOld != null &&
obj.rootOld !== ""
? obj.child1Old + (obj.rootOld ? "\n" : "")
: obj.child1Old != null && obj.child1Old !== ""
? obj.child1Old
: "";
name += obj.rootOld != null && obj.rootOld !== "" ? obj.rootOld : "";
return name == "" ? "-" : name;
} else {
return "";
}
}
function findOrgChildNameHtml(obj: any) {
if (obj) {
let name =
obj.orgChild4Name != null &&
obj.orgChild4Name !== "" &&
obj.orgChild3Name != null &&
obj.orgChild3Name !== ""
? obj.orgChild4Name + (obj.orgChild3Name ? "\n" : "")
: obj.orgChild4Name != null && obj.orgChild4Name !== ""
? obj.orgChild4Name
: "";
name +=
obj.orgChild3Name != null &&
obj.orgChild3Name !== "" &&
obj.orgChild2Name != null &&
obj.orgChild2Name !== ""
? obj.orgChild3Name + (obj.orgChild2Name ? "\n" : "")
: obj.orgChild3Name != null && obj.orgChild3Name !== ""
? obj.orgChild3Name
: "";
name +=
obj.orgChild2Name != null &&
obj.orgChild2Name !== "" &&
obj.orgChild1Name != null &&
obj.orgChild1Name !== ""
? obj.orgChild2Name + (obj.orgChild1Name ? "\n" : "")
: obj.orgChild2Name != null && obj.orgChild2Name !== ""
? obj.orgChild2Name
: "";
name +=
obj.orgChild1Name != null &&
obj.orgChild1Name !== "" &&
obj.orgRootName != null &&
obj.orgRootName !== ""
? obj.orgChild1Name + (obj.orgRootName ? "\n" : "")
: obj.orgChild1Name != null && obj.orgChild1Name !== ""
? obj.orgChild1Name
: "";
name +=
obj.orgRootName != null && obj.orgRootName !== ""
? obj.orgRootName
: "";
return name == "" ? "-" : name;
} else {
return "";
}
}
function findChildNameHtml(obj: any) {
if (obj) {
let name =
obj.child4 != null &&
obj.child4 !== "" &&
obj.child3 != null &&
obj.child3 !== ""
? obj.child4 + (obj.child3 ? "\n" : "")
: obj.child4 != null && obj.child4 !== ""
? obj.child4
: "";
name +=
obj.child3 != null &&
obj.child3 !== "" &&
obj.child2 != null &&
obj.child2 !== ""
? obj.child3 + (obj.child2 ? "\n" : "")
: obj.child3 != null && obj.child3 !== ""
? obj.child3
: "";
name +=
obj.child2 != null &&
obj.child2 !== "" &&
obj.child1 != null &&
obj.child1 !== ""
? obj.child2 + (obj.child1 ? "\n" : "")
: obj.child2 != null && obj.child2 !== ""
? obj.child2
: "";
name +=
obj.child1 != null &&
obj.child1 !== "" &&
obj.root != null &&
obj.root !== ""
? obj.child1 + (obj.root ? "\n" : "")
: obj.child1 != null && obj.child1 !== ""
? obj.child1
: "";
name += obj.root != null && obj.root !== "" ? obj.root : "";
return name == "" ? "-" : name;
} else {
return "";
}
}
function findPosMasterNoOld(obj: any) {
if (obj) {
let shortName =
(obj.child4ShortNameOld != null && obj.child4ShortNameOld !== ""
? obj.child4ShortNameOld
: obj.child3ShortNameOld != null && obj.child3ShortNameOld !== ""
? obj.child3ShortNameOld
: obj.child2ShortNameOld != null && obj.child2ShortNameOld !== ""
? obj.child2ShortNameOld
: obj.child1ShortNameOld != null && obj.child1ShortNameOld !== ""
? obj.child1ShortNameOld
: obj.rootShortNameOld != null && obj.rootShortNameOld !== ""
? obj.rootShortNameOld
: "") +
(obj.posMasterNoOld != null && obj.posMasterNoOld !== ""
? " " + obj.posMasterNoOld
: "");
return shortName == "" ? "-" : shortName;
} else {
return "";
}
}
function pathRegistryEmp(routeName: string) {
return routeName
? routeName === "registryNewByid"
? ""
: routeName === "registry-employeeId"
? "-temp"
: "-employee"
: "";
}
function downloadRenameFileByLink(link: string, fileName: string) {
fetch(link)
.then((response) => response.blob())
.then((blob) => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = fileName; // ชื่อไฟล์
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
})
.catch((error) => console.error("ดาวน์โหลดไฟล์ไม่สำเร็จ:", error));
}
function onSearchDataTable(keyword: string, data: any[], columns: any[]) {
const searchText = keyword.trim().toLowerCase();
if (!searchText) {
return data; // คืนค่าทั้งหมดถ้าไม่มีข้อความค้นหา
}
// คืนค่าข้อมูลที่กรองแล้ว
return data.filter((row: any) => {
return columns.some((col: any) => {
const rawValue = row[col.field];
const formattedValue = col.format
? col.format(rawValue, row) // ใช้ `format` ถ้ามี
: rawValue;
return String(formattedValue).toLowerCase().includes(searchText);
});
});
}
function formatDatePosition(year: string, month: string, day: string) {
const y = parseInt(year);
const m = parseInt(month);
const d = parseInt(day);
const parts = [];
if (y > 0) parts.push(`${y} ปี`);
if (m > 0) parts.push(`${m} เดือน`);
if (d > 0) parts.push(`${d} วัน`);
return parts.length > 0 ? parts.join(" ") : `${y} ปี ${m} เดือน ${d} วัน`; // กรณีที่ทั้งหมดเป็น 0
// return `${y} ปี ${m} เดือน ${d} วัน`;
}
function formatDatePositionReport(year: string, month: string, day: string) {
const y = parseInt(year);
const m = parseInt(month);
const d = parseInt(day);
const parts = [];
if (y > 0) parts.push(`${y} ปี`);
if (m > 0) parts.push(`${m} เดือน`);
if (d > 0) parts.push(`${d} วัน`);
return parts.length > 0 ? parts.join(" ") : ""; // กรณีที่ทั้งหมดเป็น 0
// return `${y} ปี ${m} เดือน ${d} วัน`;
}
// กรณีมีเฉพาะ date
function convertDateToAPI(date: Date | null) {
return date
? format(utcToZonedTime(date, "Asia/Bangkok"), "yyyy-MM-dd")
: null;
}
// กรณี datetime
function convertDatetimeToAPI(date: Date | null) {
return date
? format(utcToZonedTime(date, "Asia/Bangkok"), "yyyy-MM-dd HH:mm:ss")
: null;
}
return {
calAge,
date2Thai,
dateToISO,
notify,
notifyError,
calAgeYear,
dateText,
monthYear2Thai,
dateMonth2Thai,
success,
weekThai,
genColor15,
typeCategoryExam,
textToPhone,
textToFax,
dateThaiRange,
modalDelete,
modalConfirm,
modalError,
dialogMessage,
messageError,
messageErrorLinkage,
showLoader,
hideLoader,
typeRetire,
typeChangeName,
statusLeave,
modalWarning,
calculateDurationYmd,
// common dialog
dialogConfirm,
dialogRemove,
dialogMessageNotify,
fails,
convertDate,
convertDateDisplay,
diffDay,
findOrgName,
findOrgNameOld,
findOrgChildName,
findPosMasterNo,
findPosMasterNoOld,
pathRegistryEmp,
downloadRenameFileByLink,
onSearchDataTable,
formatDatePosition,
formatDatePositionReport,
convertDateToAPI,
convertDatetimeToAPI,
findOrgNameHtml,
findOrgNameOldHtml,
findOrgChildNameHtml,
findChildNameHtml,
notifyWarring,
};
});