เช็คอายุราชการ #661

This commit is contained in:
AdisakKanthawilang 2024-10-29 16:59:57 +07:00
parent 89fbc0225f
commit 6b3b77337e
4 changed files with 85 additions and 118 deletions

View file

@ -40,158 +40,119 @@ export function calculateAge(start: Date, end = new Date()) {
return { year, month, day };
}
export async function calculateGovAge(profileId: string) {
export async function calculateGovAge(profileId: string , type: string) { // type = OFFICER , EMPLOYEE
const isEmployee = type === 'EMPLOYEE';
const records = await AppDataSource.getRepository(ProfileSalary).find({
where: {
profileId: profileId,
},
where: {
[isEmployee ? 'profileEmployeeId' : 'profileId']: profileId,
},
select: ["date", "dateGovernment", "isGovernment"],
order: { order: "ASC" },
}); // หา record ทั้งหมด
});
if (!records || records.length === 0) {
return null;
}
let sumYears = 0;
let sumMonths = 0;
let sumDays = 0;
let endDateFristRec: any;
endDateFristRec = await AppDataSource.getRepository(ProfileSalary).findOne({
let endDateFristRec: any = null;
endDateFristRec = await AppDataSource.getRepository(ProfileSalary).findOne({
where: {
profileId: profileId,
[isEmployee ? 'profileEmployeeId' : 'profileId']: profileId,
dateGovernment: Not(IsNull()),
isGovernment: false,
},
select: ["date", "dateGovernment", "isGovernment", "order"],
select: ["dateGovernment", "order"],
order: { order: "ASC" },
}); // หา isGovernment: false record แรก
});
let totalYears1 = 0;
let totalMonths1 = 0;
let totalDays1 = 0;
const calculateDuration = (startDate:any, endDate:any) => {
let years = endDate.getFullYear() - startDate.getFullYear();
let months = endDate.getMonth() - startDate.getMonth();
let days = endDate.getDate() - startDate.getDate();
if (days < 0) {
months--;
const lastMonthDays = new Date(endDate.getFullYear(), endDate.getMonth(), 0).getDate();
days += lastMonthDays;
}
if (months < 0) {
months += 12;
years--;
}
return { years, months, days };
};
const firstStartDate = new Date(records[0].date);
const firstEndDate = endDateFristRec?new Date(endDateFristRec.dateGovernment):new Date();
console.log("[firstStartDate]",firstStartDate);
console.log("[firstEndDate]",firstEndDate);
totalYears1 = firstEndDate.getFullYear() - firstStartDate.getFullYear();
totalMonths1 = firstEndDate.getMonth() - firstStartDate.getMonth();
totalDays1 = firstEndDate.getDate() - firstStartDate.getDate();
const firstEndDate = endDateFristRec ? new Date(endDateFristRec.dateGovernment) : new Date();
if (totalDays1 < 0) {
totalMonths1 -= 1; // หาวันในเดือนก่อนหน้า
const lastMonth = new Date(firstEndDate.getFullYear(), firstEndDate.getMonth(), 0).getDate();
totalDays1 += lastMonth;
}
if (totalMonths1 < 0) {
totalMonths1 += 12;
totalYears1 -= 1;
}
const { years: totalYears1, months: totalMonths1, days: totalDays1 } = calculateDuration(firstStartDate, firstEndDate);
const records_middle = await AppDataSource.getRepository(ProfileSalary).find({
where: {
profileId: profileId,
[isEmployee ? 'profileEmployeeId' : 'profileId']: profileId,
order: MoreThan(endDateFristRec?.order),
dateGovernment: Not(IsNull()),
},
select: ["date", "dateGovernment", "isGovernment", "order"],
select: ["dateGovernment", "isGovernment"],
order: { order: "ASC" },
}); // หา record order ที่ทำหลังจาก endDateFristRec
console.log("[Gov1]", totalYears1, totalMonths1, totalDays1);
if (!records_middle || records_middle.length === 0) { // ถ้าไม่เจอแปลว่ากำลังพักราชการอยู่หรือยังไม่เคยพักราชการ
sumYears = totalYears1,
sumMonths = totalMonths1,
sumDays = totalDays1
console.log("[SumGov1]", sumYears, sumMonths, sumDays);
return {
sumYears,
sumMonths,
sumDays,
};
});
if (!records_middle || records_middle.length === 0) {
return { year: totalYears1, month: totalMonths1, day: totalDays1 };
}
let totalYears2 = 0;
let totalMonths2 = 0;
let totalDays2 = 0;
let totalYears2 = 0, totalMonths2 = 0, totalDays2 = 0;
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 && records_middle[i + 1].isGovernment === false) {
? 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;
}
if (records_middle[i].isGovernment === false && i === records_middle.length - 1) {
break;
}
console.log("[SDate]", startDate);
console.log("[EDate]", endDate);
// คำนวณระยะเวลา
let years_mid = endDate.getFullYear() - startDate.getFullYear();
let months_mid = endDate.getMonth() - startDate.getMonth();
let days_mid = endDate.getDate() - startDate.getDate();
const { years: years_mid, months: months_mid, days: days_mid } = calculateDuration(startDate, endDate);
if (days_mid < 0) {
months_mid--;
const lastMonthDays = new Date(endDate.getFullYear(), endDate.getMonth(), 0).getDate();
days_mid += lastMonthDays; // ปรับวันให้ถูกต้อง
}
// การปรับเดือน
if (months_mid < 0) {
months_mid += 12;
years_mid--;
}
// รวมเข้ากับ total
totalYears2 += years_mid;
totalMonths2 += months_mid;
totalDays2 += days_mid;
}
// การปรับค่าหาก totalMonths2 เกิน 12
if (totalMonths2 >= 12) {
totalYears2 += Math.floor(totalMonths2 / 12);
totalMonths2 = totalMonths2 % 12;
}
const adjustTotal = (years: any, months: any, days: any) => {
if (days >= new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate()) {
months += Math.floor(days / new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate());
days %= new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate();
}
// การปรับค่าหาก totalDays2 เกินจำนวนวันในเดือน
const daysInCurrentMonth = new Date(
new Date().getFullYear(),
new Date().getMonth() + 1,
0,
).getDate();
if (totalDays2 >= daysInCurrentMonth) {
totalMonths2 += Math.floor(totalDays2 / daysInCurrentMonth);
totalDays2 = totalDays2 % daysInCurrentMonth;
}
if (months >= 12) {
years += Math.floor(months / 12);
months %= 12;
}
sumYears = totalYears1 + totalYears2;
sumMonths = totalMonths1 + totalMonths2;
sumDays = totalDays1 + totalDays2;
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;
if (sumMonths >= 12) {
sumYears += Math.floor(sumMonths / 12);
sumMonths = sumMonths % 12;
sumMonths %= 12;
}
console.log("[data1]",totalYears1+"/"+totalMonths1+"/"+totalDays1);
console.log("[data2]",totalYears2+"/"+totalMonths2+"/"+totalDays2);
console.log("[SumGovAge]", sumYears, sumMonths, sumDays);
return {
sumYears,
sumMonths,
sumDays,
};
return {year: sumYears, month: sumMonths, day: sumDays};
}
export function calculateRetireDate(birthDate: Date) {
// let _birthDate = birthDate;