diff --git a/src/interfaces/utils.ts b/src/interfaces/utils.ts index 7ab5583f..2a5dad07 100644 --- a/src/interfaces/utils.ts +++ b/src/interfaces/utils.ts @@ -71,7 +71,10 @@ export async function calculateGovAge(profileId: string, type: string) { }); // console.log("endDateFristRec", endDateFristRec.dateGovernment); - const calculateDuration = (startDate: any, endDate: any) => { + const calculateDuration = (startDate: any, endDate: any , inclusive = false) => { + if (inclusive) { + endDate = new Date(endDate.getTime() + 86400000); // +1 วัน + } let years = endDate.getFullYear() - startDate.getFullYear(); let months = endDate.getMonth() - startDate.getMonth(); let days = endDate.getDate() - startDate.getDate(); @@ -113,7 +116,7 @@ export async function calculateGovAge(profileId: string, type: string) { years: totalYears1, months: totalMonths1, days: totalDays1, - } = calculateDuration(firstStartDate, firstEndDate); + } = calculateDuration(firstStartDate, firstEndDate, true); const records_middle = await AppDataSource.getRepository(ProfileSalary).find({ where: { @@ -124,10 +127,11 @@ export async function calculateGovAge(profileId: string, type: string) { select: ["dateGovernment", "isGovernment"], order: { order: "ASC" }, }); - + //ถ้าไม่มีเข้าออกราชการหลายครั้ง if (!records_middle || records_middle.length === 0) { return { year: totalYears1, month: totalMonths1, day: totalDays1 }; } + let totalYears2 = 0, totalMonths2 = 0, totalDays2 = 0; @@ -141,8 +145,7 @@ export async function calculateGovAge(profileId: string, type: string) { if (current.isGovernment === true) { const startDate = new Date(current.dateGovernment); const endDate = next ? new Date(next.dateGovernment) : new Date(); - const adjustedEndDate = new Date(endDate.getTime() + 86400000) //เพิ่ม 1 วัน (วันปัจจุบัน) - const { years, months, days } = calculateDuration(startDate, adjustedEndDate); + const { years, months, days } = calculateDuration(startDate, endDate, true); totalYears2 += years; totalMonths2 += months; @@ -185,7 +188,9 @@ export async function calculateGovAge(profileId: string, type: string) { // console.log("sumDays", sumDays); const finalAdjusted = adjustTotal(sumYears, sumMonths, sumDays); - // console.log("finalAdjusted", finalAdjusted); + console.log("finalAdjusted", finalAdjusted); + console.log("finalAdjusted.days", finalAdjusted.days); + console.log("finalAdjusted.days+1", finalAdjusted.days + 1); return { year: finalAdjusted.years,