hrms-api-org/src/utils/tenure.ts
2026-05-12 17:24:15 +07:00

18 lines
725 B
TypeScript

/**
* คำนวณอายุงานจากจำนวนวันรวม
* ใช้สูตรเดียวกับ Stored Procedure GetProfileSalaryPosition
* @param totalDays จำนวนวันรวม
* @returns { year, month, day } ปี เดือน วัน
*/
export function calculateTenure(totalDays: number) {
// Match stored procedure formula:
// days_diff / 365.2524 AS Years
// (days_diff / 30.4375) % 12 AS Months
// days_diff % 30.4375 AS Days
const year = Math.floor(totalDays / 365.2524);
const month = Math.floor((totalDays / 30.4375) % 12);
const day = Math.floor(totalDays % 30.4375);
return { year, month, day };
}