From d093953fbe44c7c774f674d85d2526e44370da0b Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Mon, 18 May 2026 20:56:20 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B9=84=E0=B8=82?= =?UTF-8?q?=E0=B8=81=E0=B8=B2=E0=B8=A3=E0=B8=84=E0=B8=B3=E0=B8=99=E0=B8=A7?= =?UTF-8?q?=E0=B8=99=E0=B8=A3=E0=B8=B0=E0=B8=A2=E0=B8=B0=E0=B9=80=E0=B8=A7?= =?UTF-8?q?=E0=B8=A5=E0=B8=B2=E0=B8=84=E0=B8=A3=E0=B8=AD=E0=B8=87=E0=B8=95?= =?UTF-8?q?=E0=B8=B3=E0=B9=81=E0=B8=AB=E0=B8=99=E0=B9=88=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileSalaryController.ts | 211 +++++++++--------- .../ProfileSalaryEmployeeController.ts | 114 ++++++++-- src/utils/tenure.ts | 45 ++-- 3 files changed, 227 insertions(+), 143 deletions(-) diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index 277ac081..ce477490 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -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; }, diff --git a/src/controllers/ProfileSalaryEmployeeController.ts b/src/controllers/ProfileSalaryEmployeeController.ts index 44b93a5d..5f2ea997 100644 --- a/src/controllers/ProfileSalaryEmployeeController.ts +++ b/src/controllers/ProfileSalaryEmployeeController.ts @@ -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; }, diff --git a/src/utils/tenure.ts b/src/utils/tenure.ts index dbdedbb3..1c97dff1 100644 --- a/src/utils/tenure.ts +++ b/src/utils/tenure.ts @@ -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) }; }