update calGovAge

This commit is contained in:
AdisakKanthawilang 2025-07-18 11:47:58 +07:00
parent 1dc7badb4d
commit cbf3de9d1b

View file

@ -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,