diff --git a/src/interfaces/utils.ts b/src/interfaces/utils.ts index 6a49c1b7..431cde6a 100644 --- a/src/interfaces/utils.ts +++ b/src/interfaces/utils.ts @@ -69,6 +69,7 @@ export async function calculateGovAge(profileId: string, type: string) { select: ["dateGovernment", "order"], order: { order: "ASC" }, }); + // console.log("endDateFristRec", endDateFristRec.dateGovernment); const calculateDuration = (startDate: any, endDate: any) => { let years = endDate.getFullYear() - startDate.getFullYear(); @@ -104,6 +105,10 @@ export async function calculateGovAge(profileId: string, type: string) { // const firstStartDate = new Date(records[0].date); const firstStartDate = profile?.dateAppoint ? profile?.dateAppoint : new Date(); const firstEndDate = endDateFristRec ? new Date(endDateFristRec.dateGovernment) : new Date(); + + // console.log("firstStartDate1", firstStartDate); + // console.log("firstEndDate1", firstEndDate); + const { years: totalYears1, months: totalMonths1, @@ -129,26 +134,25 @@ export async function calculateGovAge(profileId: string, type: string) { // case ที่เข้าออกราชการหลายครั้ง for (let i = 0; i < records_middle.length; i++) { - const startDate = new Date(records_middle[i].dateGovernment); - const endDate = - i < records_middle.length - 1 ? new Date(records_middle[i + 1].dateGovernment) : new Date(); - - if ( - records_middle[i].isGovernment === false && - (i === records_middle.length - 1 || records_middle[i + 1].isGovernment === false) - ) { - break; - } - - const { - years: years_mid, - months: months_mid, - days: days_mid, - } = calculateDuration(startDate, endDate); - - totalYears2 += years_mid; - totalMonths2 += months_mid; - totalDays2 += days_mid; + const current = records_middle[i]; + const next = records_middle[i + 1]; + + // นับเฉพาะช่วงที่เป็นราชการ + if (current.isGovernment === true) { + const startDate = new Date(current.dateGovernment); + const endDate = next ? new Date(next.dateGovernment) : new Date(); + + const { years, months, days } = calculateDuration(startDate, endDate); + + totalYears2 += years; + totalMonths2 += months; + totalDays2 += days; + + // console.log(`✔ นับช่วง ${startDate.toISOString()} → ${endDate.toISOString()} ได้ ${years} ปี ${months} เดือน ${days} วัน`); + } + // else { + // console.log(`❌ ไม่รวมช่วง ${current.dateGovernment} เพราะ isGovernment = false`); + // } } //ตั้งแต่วันที่กลับมารับราขการไปจนถึงรวมถึงวันที่ปัจจุบัน @@ -167,14 +171,22 @@ export async function calculateGovAge(profileId: string, type: string) { return { years, months, days }; }; - const adjustedTotal = adjustTotal(totalYears2, totalMonths2, totalDays2); - - let sumYears = totalYears1 + adjustedTotal.years; - let sumMonths = totalMonths1 + adjustedTotal.months; - let sumDays = totalDays1 + adjustedTotal.days; - + let sumYears = totalYears1 + totalYears2; + let sumMonths = totalMonths1 + totalMonths2; + let sumDays = totalDays1 + totalDays2; + // console.log("totalYears1", totalYears1); + // console.log("totalMonths1", totalMonths1); + // console.log("totalDays1", totalDays1); + // console.log("totalYears2", totalYears2); + // console.log("totalMonths2", totalMonths2); + // console.log("totalDays2", totalDays2); + // console.log("sumYears", sumYears); + // console.log("sumMonths", sumMonths); + // console.log("sumDays", sumDays); const finalAdjusted = adjustTotal(sumYears, sumMonths, sumDays); + // console.log("finalAdjusted", finalAdjusted); + return { year: finalAdjusted.years, month: finalAdjusted.months,