feat: Implement daily attendance tracking system
- Backend: Auto-sync work reports with attendance records - Backend: Lazy initialization of daily active worker records - Frontend: Real-time attendance status on Group Leader Dashboard
This commit is contained in:
@@ -20,24 +20,151 @@ class AttendanceModel {
|
||||
LEFT JOIN vacation_types vt ON dar.vacation_type_id = vt.id
|
||||
WHERE dar.record_date = ?
|
||||
`;
|
||||
|
||||
|
||||
const params = [date];
|
||||
|
||||
|
||||
if (workerId) {
|
||||
query += ' AND dar.worker_id = ?';
|
||||
params.push(workerId);
|
||||
}
|
||||
|
||||
|
||||
query += ' ORDER BY w.worker_name';
|
||||
|
||||
|
||||
const [rows] = await db.execute(query, params);
|
||||
return rows;
|
||||
}
|
||||
|
||||
// 작업 보고서와 근태 기록 동기화 (시간 합산 및 상태 업데이트)
|
||||
static async syncWithWorkReports(workerId, date) {
|
||||
const db = await getDb();
|
||||
|
||||
// 1. 해당 날짜의 총 작업 시간 계산
|
||||
const [reportStats] = await db.execute(`
|
||||
SELECT
|
||||
COALESCE(SUM(work_hours), 0) as total_hours,
|
||||
COUNT(*) as report_count
|
||||
FROM daily_work_reports
|
||||
WHERE worker_id = ? AND report_date = ?
|
||||
`, [workerId, date]);
|
||||
|
||||
const totalHours = parseFloat(reportStats[0].total_hours || 0);
|
||||
const reportCount = reportStats[0].report_count;
|
||||
|
||||
// 2. 근태 유형 및 상태 결정
|
||||
// 기본 규칙: 0시간 -> incomplete, <8시간 -> partial, 8시간 -> complete, >8시간 -> overtime
|
||||
// (휴가는 별도 로직이지만 여기서 덮어쓰지 않도록 주의해야 함. 하지만 작업보고서가 추가되면 실 근무로 간주)
|
||||
|
||||
let status = 'incomplete';
|
||||
let typeCode = 'REGULAR'; // 기본값
|
||||
|
||||
if (totalHours === 0) {
|
||||
status = 'incomplete';
|
||||
} else if (totalHours < 8) {
|
||||
status = 'partial';
|
||||
typeCode = 'PARTIAL';
|
||||
} else if (totalHours === 8) {
|
||||
status = 'complete';
|
||||
typeCode = 'REGULAR';
|
||||
} else {
|
||||
status = 'overtime';
|
||||
typeCode = 'OVERTIME';
|
||||
}
|
||||
|
||||
// 근태 유형 ID 조회
|
||||
const [types] = await db.execute('SELECT id FROM work_attendance_types WHERE type_code = ?', [typeCode]);
|
||||
const typeId = types[0]?.id;
|
||||
|
||||
// 3. 기록 업데이트 (휴가 정보는 유지)
|
||||
// 기존 기록 조회
|
||||
const [existing] = await db.execute(
|
||||
'SELECT id, vacation_type_id FROM daily_attendance_records WHERE worker_id = ? AND record_date = ?',
|
||||
[workerId, date]
|
||||
);
|
||||
|
||||
if (existing.length > 0) {
|
||||
// 휴가가 설정되어 있고 시간이 0이면 휴가 상태 유지, 시간이 있으면 근무+휴가 복합 상태일 수 있음
|
||||
// 여기서는 단순화하여 근무 시간이 있으면 근무 상태로 업데이트 (단, vacation_type_id는 유지)
|
||||
|
||||
const recordId = existing[0].id;
|
||||
// 만약 기존 상태가 'vacation'이고 근무시간이 0이면 업데이트 건너뛸 수도 있지만,
|
||||
// 작업보고서가 삭제되어 0이 된 경우도 있으므로 업데이트는 수행해야 함.
|
||||
|
||||
await db.execute(`
|
||||
UPDATE daily_attendance_records
|
||||
SET
|
||||
total_work_hours = ?,
|
||||
attendance_type_id = ?,
|
||||
status = ?,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
`, [totalHours, typeId, status, recordId]);
|
||||
|
||||
return { synced: true, totalHours, status };
|
||||
} else {
|
||||
// 기록이 없으면 생성 (일반적으로는 initializeDailyRecords로 생성되어 있어야 함)
|
||||
// 생성자가 명확하지 않으므로 시스템(1) 또는 알 수 없음 처리
|
||||
await db.execute(`
|
||||
INSERT INTO daily_attendance_records
|
||||
(record_date, worker_id, total_work_hours, attendance_type_id, status, created_by)
|
||||
VALUES (?, ?, ?, ?, ?, 1)
|
||||
`, [date, workerId, totalHours, typeId, status]);
|
||||
|
||||
return { synced: true, totalHours, status, created: true };
|
||||
}
|
||||
}
|
||||
|
||||
// 일일 근태 기록 초기화 (모든 활성 작업자에 대한 기본 레코드 생성)
|
||||
static async initializeDailyRecords(date, createdBy) {
|
||||
const db = await getDb();
|
||||
|
||||
// 1. 활성 작업자 조회
|
||||
const [workers] = await db.execute(
|
||||
'SELECT worker_id FROM workers WHERE status = "active"' // is_active check not needed as status covers it based on previous fix? Wait, previous fix used status='active'.
|
||||
);
|
||||
|
||||
if (workers.length === 0) return { inserted: 0 };
|
||||
|
||||
// 2. 일일 근태 레코드 일괄 생성 (이미 존재하면 무시)
|
||||
// VALUES (...), (...), ...
|
||||
const values = workers.map(w => [date, w.worker_id, 'incomplete', createdBy]);
|
||||
|
||||
// Bulk INSERT IGNORE
|
||||
// Note: mysql2 execute doesn't support nested arrays for bulk insert easily with placeholder ?
|
||||
// We should build the query or use query method for pool?
|
||||
// Using simple loop for safety and compatibility or building string.
|
||||
|
||||
let insertedCount = 0;
|
||||
|
||||
// 트랜잭션 사용 권장
|
||||
const conn = await db.getConnection();
|
||||
try {
|
||||
await conn.beginTransaction();
|
||||
|
||||
for (const w of workers) {
|
||||
const [result] = await conn.execute(`
|
||||
INSERT IGNORE INTO daily_attendance_records
|
||||
(record_date, worker_id, status, created_by)
|
||||
VALUES (?, ?, 'incomplete', ?)
|
||||
`, [date, w.worker_id, createdBy]);
|
||||
|
||||
insertedCount += result.affectedRows;
|
||||
}
|
||||
|
||||
await conn.commit();
|
||||
} catch (err) {
|
||||
await conn.rollback();
|
||||
throw err;
|
||||
} finally {
|
||||
conn.release();
|
||||
}
|
||||
|
||||
return { inserted: insertedCount, total_active_workers: workers.length };
|
||||
}
|
||||
|
||||
// 근태 기록 생성 또는 업데이트
|
||||
static async upsertAttendanceRecord(recordData) {
|
||||
const db = await getDb();
|
||||
|
||||
|
||||
const {
|
||||
record_date,
|
||||
worker_id,
|
||||
@@ -72,7 +199,7 @@ class AttendanceModel {
|
||||
is_overtime_approved,
|
||||
existing[0].id
|
||||
]);
|
||||
|
||||
|
||||
return { id: existing[0].id, affected: result.affectedRows };
|
||||
} else {
|
||||
// 생성
|
||||
@@ -90,7 +217,7 @@ class AttendanceModel {
|
||||
is_overtime_approved,
|
||||
created_by
|
||||
]);
|
||||
|
||||
|
||||
return { id: result.insertId, affected: result.affectedRows };
|
||||
}
|
||||
}
|
||||
@@ -98,7 +225,7 @@ class AttendanceModel {
|
||||
// 작업자별 근태 현황 조회 (대시보드용)
|
||||
static async getWorkerAttendanceStatus(date) {
|
||||
const db = await getDb();
|
||||
|
||||
|
||||
// 모든 작업자와 해당 날짜의 근태 기록을 조회
|
||||
const [rows] = await db.execute(`
|
||||
SELECT
|
||||
@@ -127,51 +254,51 @@ class AttendanceModel {
|
||||
WHERE w.is_active = TRUE
|
||||
ORDER BY w.worker_name
|
||||
`, [date, date, date]);
|
||||
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
// 휴가 처리
|
||||
static async processVacation(workerId, date, vacationType, createdBy) {
|
||||
const db = await getDb();
|
||||
|
||||
|
||||
// 휴가 유형 정보 조회
|
||||
const [vacationTypes] = await db.execute(
|
||||
'SELECT id, type_code, type_name, hours_deduction, description, is_active, created_at, updated_at FROM vacation_types WHERE type_code = ?',
|
||||
[vacationType]
|
||||
);
|
||||
|
||||
|
||||
if (vacationTypes.length === 0) {
|
||||
throw new Error('유효하지 않은 휴가 유형입니다.');
|
||||
}
|
||||
|
||||
|
||||
const vacationTypeInfo = vacationTypes[0];
|
||||
|
||||
|
||||
// 현재 작업 시간 조회
|
||||
const [workHours] = await db.execute(`
|
||||
SELECT COALESCE(SUM(work_hours), 0) as total_hours
|
||||
FROM daily_work_reports
|
||||
WHERE worker_id = ? AND report_date = ?
|
||||
`, [workerId, date]);
|
||||
|
||||
|
||||
const currentHours = parseFloat(workHours[0].total_hours);
|
||||
const vacationHours = parseFloat(vacationTypeInfo.hours_deduction);
|
||||
const totalHours = currentHours + vacationHours;
|
||||
|
||||
|
||||
// 근로 유형 결정
|
||||
let attendanceTypeCode = 'VACATION';
|
||||
let status = 'vacation';
|
||||
|
||||
|
||||
if (totalHours >= 8) {
|
||||
attendanceTypeCode = totalHours > 8 ? 'OVERTIME' : 'REGULAR';
|
||||
status = totalHours > 8 ? 'overtime' : 'complete';
|
||||
}
|
||||
|
||||
|
||||
const [attendanceTypes] = await db.execute(
|
||||
'SELECT id FROM work_attendance_types WHERE type_code = ?',
|
||||
[attendanceTypeCode]
|
||||
);
|
||||
|
||||
|
||||
// 휴가 작업 기록 생성 (프로젝트 ID 13 = "연차/휴무", work_type_id 1 = 기본)
|
||||
await db.execute(`
|
||||
INSERT INTO daily_work_reports (
|
||||
@@ -185,7 +312,7 @@ class AttendanceModel {
|
||||
`${vacationTypeInfo.type_name} 처리`,
|
||||
createdBy
|
||||
]);
|
||||
|
||||
|
||||
// 근태 기록 업데이트
|
||||
const attendanceData = {
|
||||
record_date: date,
|
||||
@@ -196,14 +323,14 @@ class AttendanceModel {
|
||||
is_overtime_approved: false,
|
||||
created_by: createdBy
|
||||
};
|
||||
|
||||
|
||||
return await this.upsertAttendanceRecord(attendanceData);
|
||||
}
|
||||
|
||||
// 초과근무 승인
|
||||
static async approveOvertime(workerId, date, approvedBy) {
|
||||
const db = await getDb();
|
||||
|
||||
|
||||
const [result] = await db.execute(`
|
||||
UPDATE daily_attendance_records
|
||||
SET
|
||||
@@ -214,7 +341,7 @@ class AttendanceModel {
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE worker_id = ? AND record_date = ?
|
||||
`, [approvedBy, approvedBy, workerId, date]);
|
||||
|
||||
|
||||
return result.affectedRows > 0;
|
||||
}
|
||||
|
||||
@@ -240,19 +367,19 @@ class AttendanceModel {
|
||||
static async getWorkerVacationBalance(workerId, year = null) {
|
||||
const db = await getDb();
|
||||
const currentYear = year || new Date().getFullYear();
|
||||
|
||||
|
||||
const [rows] = await db.execute(`
|
||||
SELECT id, worker_id, year, total_annual_leave, used_annual_leave, notes, created_at, updated_at FROM worker_vacation_balance
|
||||
WHERE worker_id = ? AND year = ?
|
||||
`, [workerId, currentYear]);
|
||||
|
||||
|
||||
if (rows.length === 0) {
|
||||
// 기본 연차 생성 (15일)
|
||||
await db.execute(`
|
||||
INSERT INTO worker_vacation_balance (worker_id, year, total_annual_leave)
|
||||
VALUES (?, ?, 15.0)
|
||||
`, [workerId, currentYear]);
|
||||
|
||||
|
||||
return {
|
||||
worker_id: workerId,
|
||||
year: currentYear,
|
||||
@@ -261,14 +388,14 @@ class AttendanceModel {
|
||||
remaining_annual_leave: 15.0
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
return rows[0];
|
||||
}
|
||||
|
||||
// 월별 근태 통계
|
||||
static async getMonthlyAttendanceStats(year, month, workerId = null) {
|
||||
const db = await getDb();
|
||||
|
||||
|
||||
let query = `
|
||||
SELECT
|
||||
w.worker_id,
|
||||
@@ -285,16 +412,16 @@ class AttendanceModel {
|
||||
AND YEAR(dar.record_date) = ? AND MONTH(dar.record_date) = ?
|
||||
WHERE w.is_active = TRUE
|
||||
`;
|
||||
|
||||
|
||||
const params = [year, month];
|
||||
|
||||
|
||||
if (workerId) {
|
||||
query += ' AND w.worker_id = ?';
|
||||
params.push(workerId);
|
||||
}
|
||||
|
||||
|
||||
query += ' GROUP BY w.worker_id, w.worker_name ORDER BY w.worker_name';
|
||||
|
||||
|
||||
const [rows] = await db.execute(query, params);
|
||||
return rows;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user