fixed calculate tenure (สูตรคำนวนอายุราชการจาก diff date)
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m6s

This commit is contained in:
Warunee Tamkoo 2026-04-20 18:20:20 +07:00
parent 5e52206987
commit 7e3982a96d
3 changed files with 85 additions and 57 deletions

View file

@ -23,6 +23,7 @@ import { ProfileEmployee } from "../entities/ProfileEmployee";
import { In, IsNull, LessThan, MoreThan, Not } from "typeorm";
import permission from "../interfaces/permission";
import { setLogDataDiff } from "../interfaces/utils";
import { calculateTenure } from "../utils/tenure";
import { TenurePositionOfficer } from "../entities/TenurePositionOfficer";
import { TenureLevelOfficer } from "../entities/TenureLevelOfficer";
import { TenurePositionEmployee } from "../entities/TenurePositionEmployee";
@ -92,16 +93,14 @@ export class ProfileSalaryController extends Controller {
},
{ days_diff: 0, positionName: null },
);
const { year, month, day } = calculateTenure(calDayDiff.days_diff);
const mapData: any = {
profileId: x.id,
positionName: calDayDiff.positionName,
days_diff: calDayDiff.days_diff,
// Years: (calDayDiff.days_diff / 365.2524).toFixed(4),
// Months: ((calDayDiff.days_diff / 30.4375) % 12).toFixed(4),
// Days: (calDayDiff.days_diff % 30.4375).toFixed(4),
Years: Math.floor(calDayDiff.days_diff / 365.2524),
Months: Math.floor((calDayDiff.days_diff / 30.4375) % 12),
Days: Math.floor(calDayDiff.days_diff % 30.4375),
Years: year,
Months: month,
Days: day,
};
// data.push(_mapData);
await this.positionOfficerRepo.save(mapData);
@ -143,16 +142,14 @@ export class ProfileSalaryController extends Controller {
},
{ days_diff: 0, positionName: null },
);
const { year, month, day } = calculateTenure(calDayDiff.days_diff);
const mapData: any = {
profileEmployeeId: x.id,
positionName: calDayDiff.positionName,
days_diff: calDayDiff.days_diff,
// Years: (calDayDiff.days_diff / 365.2524).toFixed(4),
// Months: ((calDayDiff.days_diff / 30.4375) % 12).toFixed(4),
// Days: (calDayDiff.days_diff % 30.4375).toFixed(4),
Years: Math.floor(calDayDiff.days_diff / 365.2524),
Months: Math.floor((calDayDiff.days_diff / 30.4375) % 12),
Days: Math.floor(calDayDiff.days_diff % 30.4375),
Years: year,
Months: month,
Days: day,
};
// data.push(_mapData);
await this.positionEmployeeRepo.save(mapData);
@ -202,15 +199,16 @@ export class ProfileSalaryController extends Controller {
},
{ days_diff: 0, positionType: null, positionLevel: null, positionCee: null },
);
const { year, month, day } = calculateTenure(calDayDiff.days_diff);
const mapData: any = {
profileId: x.id,
positionType: calDayDiff.positionType,
positionLevel: calDayDiff.positionLevel,
positionCee: calDayDiff.positionCee,
days_diff: calDayDiff.days_diff,
Years: x.posLevel == null ? 0 : (calDayDiff.days_diff / 365.2524).toFixed(4),
Months: x.posLevel == null ? 0 : ((calDayDiff.days_diff / 30.4375) % 12).toFixed(4),
Days: x.posLevel == null ? 0 : (calDayDiff.days_diff % 30.4375).toFixed(4),
Years: x.posLevel == null ? 0 : year.toFixed(4),
Months: x.posLevel == null ? 0 : month.toFixed(4),
Days: x.posLevel == null ? 0 : day.toFixed(4),
};
// data.push(_mapData);
await this.levelOfficerRepo.save(mapData);
@ -260,15 +258,16 @@ export class ProfileSalaryController extends Controller {
},
{ days_diff: 0, positionType: null, positionLevel: null, positionCee: null },
);
const { year, month, day } = calculateTenure(calDayDiff.days_diff);
const mapData: any = {
profileEmployeeId: x.id,
positionType: calDayDiff.positionType,
positionLevel: calDayDiff.positionLevel,
positionCee: calDayDiff.positionCee,
days_diff: calDayDiff.days_diff,
Years: x.posLevel == null ? 0 : (calDayDiff.days_diff / 365.2524).toFixed(4),
Months: x.posLevel == null ? 0 : ((calDayDiff.days_diff / 30.4375) % 12).toFixed(4),
Days: x.posLevel == null ? 0 : (calDayDiff.days_diff % 30.4375).toFixed(4),
Years: x.posLevel == null ? 0 : year.toFixed(4),
Months: x.posLevel == null ? 0 : month.toFixed(4),
Days: x.posLevel == null ? 0 : day.toFixed(4),
};
// data.push(_mapData);
await this.levelEmployeeRepo.save(mapData);
@ -331,13 +330,14 @@ export class ProfileSalaryController extends Controller {
},
{ days_diff: 0, positionExecutive: null },
);
const { year, month, day } = calculateTenure(calDayDiff.days_diff);
const mapData: any = {
profileId: x.id,
positionExecutiveName: calDayDiff.positionExecutive,
days_diff: calDayDiff.days_diff,
Years: (calDayDiff.days_diff / 365.2524).toFixed(4),
Months: ((calDayDiff.days_diff / 30.4375) % 12).toFixed(4),
Days: (calDayDiff.days_diff % 30.4375).toFixed(4),
Years: year.toFixed(4),
Months: month.toFixed(4),
Days: day.toFixed(4),
};
await this.positionExecutiveOfficerRepo.save(mapData);
}
@ -602,10 +602,10 @@ export class ProfileSalaryController extends Controller {
acc.push(existing);
}
// Recalculate year, month, and day
existing.year = Math.floor(existing.days / 365.2524);
existing.month = Math.floor((existing.days / 30.4375) % 12);
existing.day = Math.ceil(existing.days % 30.4375);
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
return acc;
},
@ -641,10 +641,10 @@ export class ProfileSalaryController extends Controller {
acc.push(existing);
}
// Recalculate year, month, and day
existing.year = Math.floor(existing.days / 365.2524);
existing.month = Math.floor((existing.days / 30.4375) % 12);
existing.day = Math.ceil(existing.days % 30.4375);
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
return acc;
},
@ -675,10 +675,10 @@ export class ProfileSalaryController extends Controller {
acc.push(existing);
}
// Recalculate year, month, and day
existing.year = Math.floor(existing.days / 365.2524);
existing.month = Math.floor((existing.days / 30.4375) % 12);
existing.day = Math.ceil(existing.days % 30.4375);
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
return acc;
},
@ -739,10 +739,10 @@ export class ProfileSalaryController extends Controller {
acc.push(existing);
}
// Recalculate year, month, and day
existing.year = Math.floor(existing.days / 365.2524);
existing.month = Math.floor((existing.days / 30.4375) % 12);
existing.day = Math.ceil(existing.days % 30.4375);
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
return acc;
},
@ -782,10 +782,10 @@ export class ProfileSalaryController extends Controller {
acc.push(existing);
}
// Recalculate year, month, and day
existing.year = Math.floor(existing.days / 365.2524);
existing.month = Math.floor((existing.days / 30.4375) % 12);
existing.day = Math.ceil(existing.days % 30.4375);
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
return acc;
},
@ -819,10 +819,10 @@ export class ProfileSalaryController extends Controller {
acc.push(existing);
}
// Recalculate year, month, and day
existing.year = Math.floor(existing.days / 365.2524);
existing.month = Math.floor((existing.days / 30.4375) % 12);
existing.day = Math.ceil(existing.days % 30.4375);
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
return acc;
},

View file

@ -27,6 +27,7 @@ import { Profile } from "../entities/Profile";
import { In, LessThan, IsNull, MoreThan } from "typeorm";
import permission from "../interfaces/permission";
import { setLogDataDiff } from "../interfaces/utils";
import { calculateTenure } from "../utils/tenure";
import { Command } from "../entities/Command";
import { OrgRoot } from "../entities/OrgRoot";
import Extension from "../interfaces/extension";
@ -175,9 +176,10 @@ export class ProfileSalaryEmployeeController extends Controller {
acc.push(existing);
}
existing.year = Math.floor(existing.days / 365.2524);
existing.month = Math.floor((existing.days / 30.4375) % 12);
existing.day = Math.ceil(existing.days % 30.4375);
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
return acc;
},
@ -211,9 +213,10 @@ export class ProfileSalaryEmployeeController extends Controller {
acc.push(existing);
}
existing.year = Math.floor(existing.days / 365.2524);
existing.month = Math.floor((existing.days / 30.4375) % 12);
existing.day = Math.ceil(existing.days % 30.4375);
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
return acc;
},
@ -266,9 +269,10 @@ export class ProfileSalaryEmployeeController extends Controller {
acc.push(existing);
}
existing.year = Math.floor(existing.days / 365.2524);
existing.month = Math.floor((existing.days / 30.4375) % 12);
existing.day = Math.ceil(existing.days % 30.4375);
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
return acc;
},
@ -302,9 +306,10 @@ export class ProfileSalaryEmployeeController extends Controller {
acc.push(existing);
}
existing.year = Math.floor(existing.days / 365.2524);
existing.month = Math.floor((existing.days / 30.4375) % 12);
existing.day = Math.ceil(existing.days % 30.4375);
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
return acc;
},

23
src/utils/tenure.ts Normal file
View file

@ -0,0 +1,23 @@
/**
*
* @param totalDays
* @returns { year, month, day }
*/
export function calculateTenure(totalDays: number) {
// 1. แปลงเป็น year เต็ม
const year = Math.floor(totalDays / 365.2524);
// 2. วันที่เหลือหลังหัก year ออก
const remainAfterYear = totalDays - year * 365.2524;
// 3. แปลงเป็น month เต็ม
const month = Math.floor(remainAfterYear / 30.4375);
// 4. วันที่เหลือหลังหัก month ออก
const remainAfterMonth = remainAfterYear - month * 30.4375;
// 5. ปัดลง เฉพาะวัน
const day = Math.floor(remainAfterMonth);
return { year, month, day };
}