CI / build (push) Successful in 29s
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
207 lines
8.9 KiB
JavaScript
207 lines
8.9 KiB
JavaScript
const HR_SOURCE = 'hr-sync';
|
||
|
||
function text(value) {
|
||
return typeof value === 'string' ? value.trim() : '';
|
||
}
|
||
|
||
function month(value, minimum, maximum) {
|
||
const number = Number(value);
|
||
return Number.isInteger(number) && number >= minimum && number <= maximum ? number : '';
|
||
}
|
||
|
||
function has(input, key) {
|
||
return Object.prototype.hasOwnProperty.call(input, key);
|
||
}
|
||
|
||
export function isCostRole(role) {
|
||
return typeof role === 'string' && /^(P|A|G)\s*-/.test(role);
|
||
}
|
||
|
||
export function normalizeOverride(input = {}) {
|
||
const output = {};
|
||
if (has(input, 'role')) {
|
||
const role = text(input.role);
|
||
output.role = /^(P|A|G)\s*-/.test(role) ? role : '';
|
||
}
|
||
if (has(input, 'isManager')) output.isManager = input.isManager === true;
|
||
if (has(input, 'dept')) output.dept = text(input.dept).slice(0, 80);
|
||
if (has(input, 'group')) output.group = text(input.group).slice(0, 80);
|
||
if (has(input, 'weeklyWorkDays')) output.weeklyWorkDays = Math.min(5, Math.max(1, Number(input.weeklyWorkDays) || 5));
|
||
if (has(input, 'h1Start')) output.h1Start = month(input.h1Start, 1, 6);
|
||
if (has(input, 'h1End')) output.h1End = month(input.h1End, 1, 6);
|
||
if (has(input, 'h2Start')) output.h2Start = month(input.h2Start, 7, 12);
|
||
if (has(input, 'h2End')) output.h2End = month(input.h2End, 7, 12);
|
||
return output;
|
||
}
|
||
|
||
// Department and group follow HR. A manual value only fills a gap HR leaves empty; once HR
|
||
// provides the field, HR wins and the manual value is kept but no longer used.
|
||
// A manual group only applies inside the department it was chosen for.
|
||
export function placement(person, saved) {
|
||
const hrDept = text(person.departmentName);
|
||
const hrGroup = text(person.groupName);
|
||
const manualDept = has(saved, 'dept') ? saved.dept : '';
|
||
const manualGroup = has(saved, 'group') ? saved.group : '';
|
||
const dept = hrDept || manualDept;
|
||
const deptSource = hrDept ? 'hr' : manualDept ? 'manual' : 'none';
|
||
const manualGroupUsable = manualGroup && !hrGroup && (!manualDept || manualDept === dept);
|
||
const group = hrGroup || (manualGroupUsable ? manualGroup : '');
|
||
const groupSource = hrGroup ? 'hr' : manualGroupUsable ? 'manual' : 'none';
|
||
return {
|
||
dept, group, deptSource, groupSource,
|
||
manualDeptIgnored: Boolean(manualDept && hrDept && manualDept !== hrDept),
|
||
manualGroupIgnored: Boolean(manualGroup && !manualGroupUsable && manualGroup !== group),
|
||
};
|
||
}
|
||
|
||
// 工讀生:HR 員工子群組 B01。算法同常態型外包(O),日薪 = 時薪 × 8。
|
||
export const PART_TIME_SUBGROUP = 'B01';
|
||
export const PART_TIME_HOURS_PER_DAY = 8;
|
||
|
||
export function isPartTime(person) {
|
||
return text(person.employeeSubGroupCode) === PART_TIME_SUBGROUP;
|
||
}
|
||
|
||
// 到職日、離職日落在試算年度內時,在職月份從到職月開始、到離職月結束(HR 為準)。
|
||
// 到職在年度之後或離職在年度之前,整年不計。手動設定的月份只能更窄,不能超出 HR 的在職期間。
|
||
export function boundMonths(months, hireDate, leaveDate, year) {
|
||
const parse = (value) => {
|
||
const match = /^(\d{4})-(\d{2})/.exec(text(value));
|
||
return match ? { year: Number(match[1]), month: Number(match[2]) } : null;
|
||
};
|
||
const hire = parse(hireDate);
|
||
const leave = parse(leaveDate);
|
||
if (!year || (!hire && !leave)) return { ...months, bounded: false };
|
||
const first = !hire || hire.year < year ? 1 : hire.year > year ? 13 : hire.month;
|
||
const last = !leave || leave.year > year ? 12 : leave.year < year ? 0 : leave.month;
|
||
if (first === 1 && last === 12) return { ...months, bounded: false };
|
||
const out = { ...months, bounded: true };
|
||
for (const [startKey, endKey] of [['h1Start', 'h1End'], ['h2Start', 'h2End']]) {
|
||
if (out[startKey] === '' || out[endKey] === '') continue;
|
||
const start = Math.max(Number(out[startKey]), first);
|
||
const end = Math.min(Number(out[endKey]), last);
|
||
if (start > end) { out[startKey] = ''; out[endKey] = ''; } else { out[startKey] = start; out[endKey] = end; }
|
||
}
|
||
return out;
|
||
}
|
||
|
||
export function toCostPerson(person, salary, override = {}, { year, fullYearDefault = false } = {}) {
|
||
const saved = normalizeOverride(override);
|
||
const months = boundMonths({
|
||
h1Start: has(saved, 'h1Start') ? saved.h1Start : 1,
|
||
h1End: has(saved, 'h1End') ? saved.h1End : 6,
|
||
// 2028 起預設全年(再依 HR 到職/離職限縮);2027 維持原本的預設(H2 留空)。
|
||
h2Start: has(saved, 'h2Start') ? saved.h2Start : (fullYearDefault ? 7 : ''),
|
||
h2End: has(saved, 'h2End') ? saved.h2End : (fullYearDefault ? 12 : ''),
|
||
}, person.hireDate, person.leaveDate, year);
|
||
const place = placement(person, saved);
|
||
const salaryType = salary?.salaryType === 'hourly' ? 'hourly' : 'monthly';
|
||
const amount = Number(salary?.amount);
|
||
const hasSalary = Number.isFinite(amount) && amount >= 0;
|
||
const hasRole = isCostRole(saved.role);
|
||
if (isPartTime(person)) return toPartTimePerson(person, salary, saved, place, salaryType, amount, hasSalary, months);
|
||
|
||
return {
|
||
row: String(person.employeeNumber),
|
||
employeeNumber: String(person.employeeNumber),
|
||
source: HR_SOURCE,
|
||
name: text(person.name),
|
||
title: text(person.positionName),
|
||
company: text(person.corporationName),
|
||
dept: place.dept,
|
||
group: place.group,
|
||
deptSource: place.deptSource,
|
||
groupSource: place.groupSource,
|
||
manualDeptIgnored: place.manualDeptIgnored,
|
||
manualGroupIgnored: place.manualGroupIgnored,
|
||
role: has(saved, 'role') ? saved.role || '待分類' : '待分類',
|
||
// 管理職完全以 HR 組織資料為準(主要與次要主管都算),不再吃手動覆寫。
|
||
isManager: Array.isArray(person.supervisorOf) && person.supervisorOf.length > 0,
|
||
managerClassification: 'hr',
|
||
supervisorOf: Array.isArray(person.supervisorOf) ? person.supervisorOf : [],
|
||
salary: salaryType === 'monthly' && hasSalary ? amount : 0,
|
||
hourlyRate: salaryType === 'hourly' && hasSalary ? amount : 0,
|
||
salaryType: hasSalary ? salaryType : 'unknown',
|
||
weeklyWorkDays: has(saved, 'weeklyWorkDays') ? saved.weeklyWorkDays : 5,
|
||
h1Start: months.h1Start,
|
||
h1End: months.h1End,
|
||
h2Start: months.h2Start,
|
||
h2End: months.h2End,
|
||
employmentBounded: months.bounded,
|
||
hireDate: text(person.hireDate) || null,
|
||
leaveDate: text(person.leaveDate) || null,
|
||
workRateUsed: 1,
|
||
hrStatus: person.status,
|
||
salaryEffectiveDate: salary?.effectiveDate || null,
|
||
hrFields: {
|
||
name: text(person.name), title: text(person.positionName),
|
||
company: text(person.corporationName), dept: text(person.departmentName), group: text(person.groupName),
|
||
},
|
||
calculationReady: hasSalary && hasRole,
|
||
calculationIssue: !hasSalary ? 'HR 尚未提供可用薪資' : !hasRole ? '待設定 P/A 職能' : '',
|
||
};
|
||
}
|
||
|
||
// 工讀生一律當 O:P/A/G 的手動設定不套用。HR 給月薪時不猜換算,標成待確認、不計成本。
|
||
function toPartTimePerson(person, salary, saved, place, salaryType, amount, hasSalary, months) {
|
||
const hourly = hasSalary && salaryType === 'hourly';
|
||
const issue = !hasSalary ? 'HR 尚未提供可用薪資'
|
||
: !hourly ? '工讀薪資待確認:HR 提供的是月薪,不是時薪' : '';
|
||
return {
|
||
row: String(person.employeeNumber),
|
||
employeeNumber: String(person.employeeNumber),
|
||
source: HR_SOURCE,
|
||
name: text(person.name),
|
||
title: text(person.positionName),
|
||
company: text(person.corporationName),
|
||
dept: place.dept,
|
||
group: place.group,
|
||
deptSource: place.deptSource,
|
||
groupSource: place.groupSource,
|
||
manualDeptIgnored: place.manualDeptIgnored,
|
||
manualGroupIgnored: place.manualGroupIgnored,
|
||
role: 'O - 外包',
|
||
partTime: true,
|
||
isManager: false,
|
||
managerClassification: 'hr',
|
||
supervisorOf: [],
|
||
salary: 0,
|
||
hourlyRate: 0,
|
||
partTimeHourlyRate: hourly ? amount : 0,
|
||
reportedSalary: hasSalary ? { type: salaryType, amount } : null,
|
||
dayrate: hourly ? amount * PART_TIME_HOURS_PER_DAY : 0,
|
||
salaryType: hasSalary ? 'daily' : 'unknown',
|
||
weeklyWorkDays: 5,
|
||
h1Start: months.h1Start,
|
||
h1End: months.h1End,
|
||
h2Start: months.h2Start,
|
||
h2End: months.h2End,
|
||
employmentBounded: months.bounded,
|
||
hireDate: text(person.hireDate) || null,
|
||
leaveDate: text(person.leaveDate) || null,
|
||
hrStatus: person.status,
|
||
salaryEffectiveDate: salary?.effectiveDate || null,
|
||
hrFields: {
|
||
name: text(person.name), title: text(person.positionName),
|
||
company: text(person.corporationName), dept: text(person.departmentName), group: text(person.groupName),
|
||
},
|
||
calculationReady: !issue,
|
||
calculationIssue: issue,
|
||
};
|
||
}
|
||
|
||
export function weekdaysInMonth(year, month) {
|
||
let count = 0;
|
||
const date = new Date(Date.UTC(year, month - 1, 1));
|
||
while (date.getUTCMonth() === month - 1) {
|
||
const day = date.getUTCDay();
|
||
if (day >= 1 && day <= 5) count += 1;
|
||
date.setUTCDate(date.getUTCDate() + 1);
|
||
}
|
||
return count;
|
||
}
|
||
|
||
export function hourlyMonthlyAmount(person, year, month) {
|
||
return weekdaysInMonth(year, month) * 8 * (Number(person.hourlyRate) || 0) * ((Number(person.weeklyWorkDays) || 5) / 5);
|
||
}
|