hrms-mgt/src/utils/function.ts

76 lines
2.7 KiB
TypeScript
Raw Normal View History

2026-01-21 16:16:00 +07:00
import moment from "moment";
2025-07-07 18:08:57 +07:00
/**
*
*
* @param page
* @param maxPage
* @param currentPageItems
* @returns
*/
export async function updateCurrentPage(
page: number,
maxPage: number,
total: number
) {
// ถ้าหน้าปัจจุบันไม่ใช่หน้าแรก และเป็นหน้าสุดท้าย และมีข้อมูลเหลือ 1 รายการ ให้กลับไปหน้าก่อนหน้า
if (page > 1 && page === maxPage && total === 1) {
return page - 1;
}
return page;
}
2025-10-01 15:20:45 +07:00
/**
*
*
* @param date
* @returns
*/
export function calculateFiscalYear(date: Date) {
const month = date.getMonth() + 1;
return month >= 10 ? date.getFullYear() + 1 : date.getFullYear();
}
2026-01-21 16:16:00 +07:00
/**
*
* @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} วัน`;
}
2026-02-25 10:36:53 +07:00
/**
*
* @param col
* @param isAct
* @returns
*/
export function getColumnLabel(col: any, isAct: boolean) {
if (col.name === "posNo" && isAct) {
2026-02-25 10:43:31 +07:00
return `${col.label} (รักษาการแทน)`;
2026-02-25 10:36:53 +07:00
}
return col.label;
}