fix(tksupport): 휴가 차감 우선순위 적용 (이월→기본→추가→장기→회사)
단순 UPDATE → 트랜잭션 + 우선순위 순차 차감으로 변경. 이월 연차부터 먼저 소진되도록 보장. 복원도 역순(회사→장기→추가→기본→이월) 적용. 참조: system1-factory/api/models/vacationBalanceModel.js deductByPriority Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -61,24 +61,76 @@ const vacationBalanceModel = {
|
|||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 우선순위: 이월 → 기본연차 → 추가부여 → 장기근속 → 회사부여
|
||||||
async deductDays(userId, vacationTypeId, year, daysToDeduct, conn) {
|
async deductDays(userId, vacationTypeId, year, daysToDeduct, conn) {
|
||||||
const db = conn || getPool();
|
const db = conn || getPool();
|
||||||
const [result] = await db.query(`
|
const needRelease = !conn;
|
||||||
UPDATE sp_vacation_balances
|
const c = needRelease ? await db.getConnection() : db;
|
||||||
SET used_days = used_days + ?, updated_at = NOW()
|
try {
|
||||||
WHERE user_id = ? AND vacation_type_id = ? AND year = ?
|
if (needRelease) await c.beginTransaction();
|
||||||
`, [daysToDeduct, userId, vacationTypeId, year]);
|
|
||||||
return result;
|
const [balances] = await c.query(`
|
||||||
|
SELECT id, total_days, used_days, (total_days - used_days) AS remaining_days, balance_type
|
||||||
|
FROM sp_vacation_balances
|
||||||
|
WHERE user_id = ? AND year = ? AND (total_days - used_days) > 0
|
||||||
|
ORDER BY FIELD(balance_type, 'CARRY_OVER', 'AUTO', 'MANUAL', 'LONG_SERVICE', 'COMPANY_GRANT')
|
||||||
|
FOR UPDATE
|
||||||
|
`, [userId, year]);
|
||||||
|
|
||||||
|
let remaining = daysToDeduct;
|
||||||
|
for (const b of balances) {
|
||||||
|
if (remaining <= 0) break;
|
||||||
|
const toDeduct = Math.min(remaining, parseFloat(b.remaining_days));
|
||||||
|
if (toDeduct > 0) {
|
||||||
|
await c.query('UPDATE sp_vacation_balances SET used_days = used_days + ?, updated_at = NOW() WHERE id = ?', [toDeduct, b.id]);
|
||||||
|
remaining -= toDeduct;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needRelease) await c.commit();
|
||||||
|
return { affectedRows: balances.length };
|
||||||
|
} catch (err) {
|
||||||
|
if (needRelease) await c.rollback();
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
if (needRelease) c.release();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 복원: 역순 (회사부여 → 장기근속 → 추가부여 → 기본연차 → 이월)
|
||||||
async restoreDays(userId, vacationTypeId, year, daysToRestore, conn) {
|
async restoreDays(userId, vacationTypeId, year, daysToRestore, conn) {
|
||||||
const db = conn || getPool();
|
const db = conn || getPool();
|
||||||
const [result] = await db.query(`
|
const needRelease = !conn;
|
||||||
UPDATE sp_vacation_balances
|
const c = needRelease ? await db.getConnection() : db;
|
||||||
SET used_days = GREATEST(0, used_days - ?), updated_at = NOW()
|
try {
|
||||||
WHERE user_id = ? AND vacation_type_id = ? AND year = ?
|
if (needRelease) await c.beginTransaction();
|
||||||
`, [daysToRestore, userId, vacationTypeId, year]);
|
|
||||||
return result;
|
const [balances] = await c.query(`
|
||||||
|
SELECT id, used_days, balance_type
|
||||||
|
FROM sp_vacation_balances
|
||||||
|
WHERE user_id = ? AND year = ? AND used_days > 0
|
||||||
|
ORDER BY FIELD(balance_type, 'COMPANY_GRANT', 'LONG_SERVICE', 'MANUAL', 'AUTO', 'CARRY_OVER')
|
||||||
|
FOR UPDATE
|
||||||
|
`, [userId, year]);
|
||||||
|
|
||||||
|
let remaining = daysToRestore;
|
||||||
|
for (const b of balances) {
|
||||||
|
if (remaining <= 0) break;
|
||||||
|
const toRestore = Math.min(remaining, parseFloat(b.used_days));
|
||||||
|
if (toRestore > 0) {
|
||||||
|
await c.query('UPDATE sp_vacation_balances SET used_days = used_days - ?, updated_at = NOW() WHERE id = ?', [toRestore, b.id]);
|
||||||
|
remaining -= toRestore;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needRelease) await c.commit();
|
||||||
|
return { affectedRows: balances.length };
|
||||||
|
} catch (err) {
|
||||||
|
if (needRelease) await c.rollback();
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
if (needRelease) c.release();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
calculateAnnualLeaveDays(hireDate, targetYear) {
|
calculateAnnualLeaveDays(hireDate, targetYear) {
|
||||||
|
|||||||
Reference in New Issue
Block a user