แก้ไขการคำนวนระยะเวลาครองตำแหน่ง

This commit is contained in:
Warunee Tamkoo 2026-05-18 20:56:20 +07:00
parent f1c546ba8f
commit d093953fbe
3 changed files with 227 additions and 143 deletions

View file

@ -23,7 +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 { normalizeDurationSumSimple } from "../utils/tenure";
import { TenurePositionOfficer } from "../entities/TenurePositionOfficer";
import { TenureLevelOfficer } from "../entities/TenureLevelOfficer";
import { TenurePositionEmployee } from "../entities/TenurePositionEmployee";
@ -78,33 +78,28 @@ export class ProfileSalaryController extends Controller {
_currentDate,
]);
const _position = position.length > 0 ? position[0] : [];
// Filter for current position and use SP's calculated values (calendar arithmetic)
const mapPosition =
_position.length > 1
? _position.slice(1).map((curr: any, index: number) => ({
days_diff: curr.days_diff,
positionName: _position[index]?.positionName,
// Use stored procedure's calculated values (calendar arithmetic)
year: curr.Years !== null && curr.Years !== undefined ? Math.floor(Number(curr.Years)) : 0,
month: curr.Months !== null && curr.Months !== undefined ? Math.floor(Number(curr.Months)) : 0,
day: curr.Days !== null && curr.Days !== undefined ? Math.floor(Number(curr.Days)) : 0,
}))
: [];
const calDayDiff = mapPosition
.filter((curr: any) => curr.positionName == x.position)
.reduce(
(acc: any, curr: any) => {
acc.days_diff += Number(curr.days_diff) || 0;
acc.positionName = curr.positionName;
return acc;
},
{ 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: year,
Months: month,
Days: day,
};
data.push(mapData);
const currentTenure = mapPosition.find((curr: any) => curr.positionName == x.position);
if (currentTenure) {
const mapData: any = {
profileId: x.id,
positionName: currentTenure.positionName,
year: currentTenure.year,
month: currentTenure.month,
day: currentTenure.day,
};
data.push(mapData);
}
}
await this.positionOfficerRepo.save(data);
@ -128,33 +123,28 @@ export class ProfileSalaryController extends Controller {
_currentDate,
]);
const _position = position.length > 0 ? position[0] : [];
// Filter for current position and use SP's calculated values (calendar arithmetic)
const mapPosition =
_position.length > 1
? _position.slice(1).map((curr: any, index: number) => ({
days_diff: curr.days_diff,
positionName: _position[index]?.positionName,
// Use stored procedure's calculated values (calendar arithmetic)
year: curr.Years !== null && curr.Years !== undefined ? Math.floor(Number(curr.Years)) : 0,
month: curr.Months !== null && curr.Months !== undefined ? Math.floor(Number(curr.Months)) : 0,
day: curr.Days !== null && curr.Days !== undefined ? Math.floor(Number(curr.Days)) : 0,
}))
: [];
const calDayDiff = mapPosition
.filter((curr: any) => curr.positionName == x.position)
.reduce(
(acc: any, curr: any) => {
acc.days_diff += Number(curr.days_diff) || 0;
acc.positionName = curr.positionName;
return acc;
},
{ 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: year,
Months: month,
Days: day,
};
data.push(mapData);
const currentTenure = mapPosition.find((curr: any) => curr.positionName == x.position);
if (currentTenure) {
const mapData: any = {
profileEmployeeId: x.id,
positionName: currentTenure.positionName,
year: currentTenure.year,
month: currentTenure.month,
day: currentTenure.day,
};
data.push(mapData);
}
}
await this.positionEmployeeRepo.save(data);
@ -203,16 +193,17 @@ export class ProfileSalaryController extends Controller {
},
{ days_diff: 0, positionType: null, positionLevel: null, positionCee: null },
);
const { year, month, day } = calculateTenure(calDayDiff.days_diff);
const years = calDayDiff.days_diff / 365;
const normalized = normalizeDurationSumSimple(years, 0, 0);
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 : year.toFixed(4),
Months: x.posLevel == null ? 0 : month.toFixed(4),
Days: x.posLevel == null ? 0 : day.toFixed(4),
Years: x.posLevel == null ? 0 : normalized.years.toFixed(4),
Months: x.posLevel == null ? 0 : normalized.months.toFixed(4),
Days: x.posLevel == null ? 0 : normalized.days.toFixed(4),
};
data.push(mapData);
}
@ -263,16 +254,17 @@ export class ProfileSalaryController extends Controller {
},
{ days_diff: 0, positionType: null, positionLevel: null, positionCee: null },
);
const { year, month, day } = calculateTenure(calDayDiff.days_diff);
const years = calDayDiff.days_diff / 365;
const normalized = normalizeDurationSumSimple(years, 0, 0);
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 : year.toFixed(4),
Months: x.posLevel == null ? 0 : month.toFixed(4),
Days: x.posLevel == null ? 0 : day.toFixed(4),
Years: x.posLevel == null ? 0 : normalized.years.toFixed(4),
Months: x.posLevel == null ? 0 : normalized.months.toFixed(4),
Days: x.posLevel == null ? 0 : normalized.days.toFixed(4),
};
data.push(mapData);
}
@ -337,14 +329,15 @@ export class ProfileSalaryController extends Controller {
},
{ days_diff: 0, positionExecutive: null },
);
const { year, month, day } = calculateTenure(calDayDiff.days_diff);
const years = calDayDiff.days_diff / 365;
const normalized = normalizeDurationSumSimple(years, 0, 0);
const mapData: any = {
profileId: x.id,
positionExecutiveName: calDayDiff.positionExecutive,
days_diff: calDayDiff.days_diff,
Years: year.toFixed(4),
Months: month.toFixed(4),
Days: day.toFixed(4),
Years: normalized.years.toFixed(4),
Months: normalized.months.toFixed(4),
Days: normalized.days.toFixed(4),
};
data.push(mapData);
}
@ -617,15 +610,15 @@ export class ProfileSalaryController extends Controller {
acc.push(existing);
}
// Normalize the summed values (convert excess days to months, months to years)
while (existing.day >= 30) {
existing.month += Math.floor(existing.day / 30);
existing.day = existing.day % 30;
}
while (existing.month >= 12) {
existing.year += Math.floor(existing.month / 12);
existing.month = existing.month % 12;
}
// Normalize the summed values using calendar arithmetic
const normalized = normalizeDurationSumSimple(
existing.year,
existing.month,
existing.day
);
existing.year = normalized.years;
existing.month = normalized.months;
existing.day = normalized.days;
return acc;
},
@ -668,15 +661,15 @@ export class ProfileSalaryController extends Controller {
acc.push(existing);
}
// Normalize the summed values (convert excess days to months, months to years)
while (existing.day >= 30) {
existing.month += Math.floor(existing.day / 30);
existing.day = existing.day % 30;
}
while (existing.month >= 12) {
existing.year += Math.floor(existing.month / 12);
existing.month = existing.month % 12;
}
// Normalize the summed values using calendar arithmetic
const normalized = normalizeDurationSumSimple(
existing.year,
existing.month,
existing.day
);
existing.year = normalized.years;
existing.month = normalized.months;
existing.day = normalized.days;
return acc;
},
@ -714,15 +707,15 @@ export class ProfileSalaryController extends Controller {
acc.push(existing);
}
// Normalize the summed values (convert excess days to months, months to years)
while (existing.day >= 30) {
existing.month += Math.floor(existing.day / 30);
existing.day = existing.day % 30;
}
while (existing.month >= 12) {
existing.year += Math.floor(existing.month / 12);
existing.month = existing.month % 12;
}
// Normalize the summed values using calendar arithmetic
const normalized = normalizeDurationSumSimple(
existing.year,
existing.month,
existing.day
);
existing.year = normalized.years;
existing.month = normalized.months;
existing.day = normalized.days;
return acc;
},
@ -787,15 +780,15 @@ export class ProfileSalaryController extends Controller {
acc.push(existing);
}
// Normalize the summed values (convert excess days to months, months to years)
while (existing.day >= 30) {
existing.month += Math.floor(existing.day / 30);
existing.day = existing.day % 30;
}
while (existing.month >= 12) {
existing.year += Math.floor(existing.month / 12);
existing.month = existing.month % 12;
}
// Normalize the summed values using calendar arithmetic
const normalized = normalizeDurationSumSimple(
existing.year,
existing.month,
existing.day
);
existing.year = normalized.years;
existing.month = normalized.months;
existing.day = normalized.days;
return acc;
},
@ -839,15 +832,15 @@ export class ProfileSalaryController extends Controller {
acc.push(existing);
}
// Normalize the summed values (convert excess days to months, months to years)
while (existing.day >= 30) {
existing.month += Math.floor(existing.day / 30);
existing.day = existing.day % 30;
}
while (existing.month >= 12) {
existing.year += Math.floor(existing.month / 12);
existing.month = existing.month % 12;
}
// Normalize the summed values using calendar arithmetic
const normalized = normalizeDurationSumSimple(
existing.year,
existing.month,
existing.day
);
existing.year = normalized.years;
existing.month = normalized.months;
existing.day = normalized.days;
return acc;
},
@ -885,15 +878,15 @@ export class ProfileSalaryController extends Controller {
acc.push(existing);
}
// Normalize the summed values (convert excess days to months, months to years)
while (existing.day >= 30) {
existing.month += Math.floor(existing.day / 30);
existing.day = existing.day % 30;
}
while (existing.month >= 12) {
existing.year += Math.floor(existing.month / 12);
existing.month = existing.month % 12;
}
// Normalize the summed values using calendar arithmetic
const normalized = normalizeDurationSumSimple(
existing.year,
existing.month,
existing.day
);
existing.year = normalized.years;
existing.month = normalized.months;
existing.day = normalized.days;
return acc;
},

View file

@ -27,7 +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 { normalizeDurationSumSimple } from "../utils/tenure";
import { Command } from "../entities/Command";
import { OrgRoot } from "../entities/OrgRoot";
import Extension from "../interfaces/extension";
@ -161,6 +161,14 @@ export class ProfileSalaryEmployeeController extends Controller {
_position.length > 1
? _position.slice(1).map((curr: any, index: number) => ({
days: curr.days_diff ? Number(curr.days_diff) : 0,
// Use stored procedure's calculated values (calendar arithmetic)
year:
curr.Years !== null && curr.Years !== undefined ? Math.floor(Number(curr.Years)) : 0,
month:
curr.Months !== null && curr.Months !== undefined
? Math.floor(Number(curr.Months))
: 0,
day: curr.Days !== null && curr.Days !== undefined ? Math.floor(Number(curr.Days)) : 0,
name: _position[index]?.positionName,
}))
: [];
@ -171,15 +179,25 @@ export class ProfileSalaryEmployeeController extends Controller {
if (existing) {
existing.days += curr.days;
existing.year += curr.year;
existing.month += curr.month;
existing.day += curr.day;
} else {
existing = { name: curr.name, days: curr.days };
existing = {
name: curr.name,
days: curr.days,
year: curr.year,
month: curr.month,
day: curr.day,
};
acc.push(existing);
}
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
// Normalize the summed values using calendar arithmetic
const normalized = normalizeDurationSumSimple(existing.year, existing.month, existing.day);
existing.year = normalized.years;
existing.month = normalized.months;
existing.day = normalized.days;
return acc;
},
@ -195,6 +213,14 @@ export class ProfileSalaryEmployeeController extends Controller {
_posLevel.length > 1
? _posLevel.slice(1).map((curr: any, index: number) => ({
days: curr.days_diff ? Number(curr.days_diff) : 0,
// Use stored procedure's calculated values (calendar arithmetic)
year:
curr.Years !== null && curr.Years !== undefined ? Math.floor(Number(curr.Years)) : 0,
month:
curr.Months !== null && curr.Months !== undefined
? Math.floor(Number(curr.Months))
: 0,
day: curr.Days !== null && curr.Days !== undefined ? Math.floor(Number(curr.Days)) : 0,
name:
!_posLevel[index]?.positionType && _posLevel[index]?.positionCee
? `ระดับ ${_posLevel[index]?.positionCee.trim()}`
@ -208,15 +234,25 @@ export class ProfileSalaryEmployeeController extends Controller {
if (existing) {
existing.days += curr.days;
existing.year += curr.year;
existing.month += curr.month;
existing.day += curr.day;
} else {
existing = { name: curr.name, days: curr.days };
existing = {
name: curr.name,
days: curr.days,
year: curr.year,
month: curr.month,
day: curr.day,
};
acc.push(existing);
}
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
// Normalize the summed values using calendar arithmetic
const normalized = normalizeDurationSumSimple(existing.year, existing.month, existing.day);
existing.year = normalized.years;
existing.month = normalized.months;
existing.day = normalized.days;
return acc;
},
@ -254,6 +290,14 @@ export class ProfileSalaryEmployeeController extends Controller {
_position.length > 1
? _position.slice(1).map((curr: any, index: number) => ({
days: curr.days_diff ? Number(curr.days_diff) : 0,
// Use stored procedure's calculated values (calendar arithmetic)
year:
curr.Years !== null && curr.Years !== undefined ? Math.floor(Number(curr.Years)) : 0,
month:
curr.Months !== null && curr.Months !== undefined
? Math.floor(Number(curr.Months))
: 0,
day: curr.Days !== null && curr.Days !== undefined ? Math.floor(Number(curr.Days)) : 0,
name: _position[index]?.positionName,
}))
: [];
@ -264,15 +308,25 @@ export class ProfileSalaryEmployeeController extends Controller {
if (existing) {
existing.days += curr.days;
existing.year += curr.year;
existing.month += curr.month;
existing.day += curr.day;
} else {
existing = { name: curr.name, days: curr.days };
existing = {
name: curr.name,
days: curr.days,
year: curr.year,
month: curr.month,
day: curr.day,
};
acc.push(existing);
}
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
// Normalize the summed values using calendar arithmetic
const normalized = normalizeDurationSumSimple(existing.year, existing.month, existing.day);
existing.year = normalized.years;
existing.month = normalized.months;
existing.day = normalized.days;
return acc;
},
@ -288,6 +342,14 @@ export class ProfileSalaryEmployeeController extends Controller {
_posLevel.length > 1
? _posLevel.slice(1).map((curr: any, index: number) => ({
days: curr.days_diff ? Number(curr.days_diff) : 0,
// Use stored procedure's calculated values (calendar arithmetic)
year:
curr.Years !== null && curr.Years !== undefined ? Math.floor(Number(curr.Years)) : 0,
month:
curr.Months !== null && curr.Months !== undefined
? Math.floor(Number(curr.Months))
: 0,
day: curr.Days !== null && curr.Days !== undefined ? Math.floor(Number(curr.Days)) : 0,
name:
!_posLevel[index]?.positionType && _posLevel[index]?.positionCee
? `ระดับ ${_posLevel[index]?.positionCee.trim()}`
@ -301,15 +363,25 @@ export class ProfileSalaryEmployeeController extends Controller {
if (existing) {
existing.days += curr.days;
existing.year += curr.year;
existing.month += curr.month;
existing.day += curr.day;
} else {
existing = { name: curr.name, days: curr.days };
existing = {
name: curr.name,
days: curr.days,
year: curr.year,
month: curr.month,
day: curr.day,
};
acc.push(existing);
}
const { year, month, day } = calculateTenure(existing.days);
existing.year = year;
existing.month = month;
existing.day = day;
// Normalize the summed values using calendar arithmetic
const normalized = normalizeDurationSumSimple(existing.year, existing.month, existing.day);
existing.year = normalized.years;
existing.month = normalized.months;
existing.day = normalized.days;
return acc;
},

View file

@ -1,18 +1,37 @@
/**
*
* Stored Procedure GetProfileSalaryPosition
* @param totalDays
* @returns { year, month, day }
* Normalize a duration sum using calendar arithmetic
* Converts excess days to months using average month length (30.4375 days)
* and excess months to years. Matches the logic used in stored procedures.
*
* @param years Total years from sum
* @param months Total months from sum
* @param days Total days from sum
* @returns Normalized { years, months, days }
*/
export function calculateTenure(totalDays: number) {
// Match stored procedure formula:
// days_diff / 365.2524 AS Years
// (days_diff / 30.4375) % 12 AS Months
// days_diff % 30.4375 AS Days
export function normalizeDurationSumSimple(
years: number,
months: number,
days: number,
): { years: number; months: number; days: number } {
const DAYS_PER_MONTH = 30.4375; // Average days per month in Gregorian calendar
const year = Math.floor(totalDays / 365.2524);
const month = Math.floor((totalDays / 30.4375) % 12);
const day = Math.floor(totalDays % 30.4375);
let totalMonths = months;
let totalDays = days;
return { year, month, day };
// Convert excess days to months
if (totalDays >= DAYS_PER_MONTH) {
const additionalMonths = Math.floor(totalDays / DAYS_PER_MONTH);
totalMonths += additionalMonths;
totalDays = totalDays - additionalMonths * DAYS_PER_MONTH;
}
// Convert excess months to years
let totalYears = years;
if (totalMonths >= 12) {
const additionalYears = Math.floor(totalMonths / 12);
totalYears += additionalYears;
totalMonths = totalMonths % 12;
}
return { years: totalYears, months: Math.floor(totalMonths), days: Math.floor(totalDays) };
}