All files / models attendanceModel.js

0% Statements 0/65
0% Branches 0/21
0% Functions 0/9
0% Lines 0/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
const { getDb } = require('../dbPool');
 
class AttendanceModel {
  // 일일 근태 기록 조회
  static async getDailyAttendanceRecords(date, workerId = null) {
    const db = await getDb();
    let query = `
      SELECT 
        dar.*,
        w.worker_name,
        w.job_type,
        wat.type_name as attendance_type_name,
        wat.type_code as attendance_type_code,
        vt.type_name as vacation_type_name,
        vt.type_code as vacation_type_code,
        vt.hours_deduction as vacation_hours
      FROM daily_attendance_records dar
      LEFT JOIN workers w ON dar.worker_id = w.worker_id
      LEFT JOIN work_attendance_types wat ON dar.attendance_type_id = wat.id
      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 upsertAttendanceRecord(recordData) {
    const db = await getDb();
    
    const {
      record_date,
      worker_id,
      total_work_hours,
      work_attendance_type_id,
      vacation_type_id,
      is_overtime_approved,
      created_by
    } = recordData;
 
    // 기존 기록 확인
    const [existing] = await db.execute(
      'SELECT id FROM daily_attendance_records WHERE worker_id = ? AND record_date = ?',
      [worker_id, record_date]
    );
 
    if (existing.length > 0) {
      // 업데이트
      const [result] = await db.execute(`
        UPDATE daily_attendance_records 
        SET 
          total_work_hours = ?,
          work_attendance_type_id = ?,
          vacation_type_id = ?,
          is_overtime_approved = ?,
          updated_at = CURRENT_TIMESTAMP
        WHERE id = ?
      `, [
        total_work_hours,
        work_attendance_type_id,
        vacation_type_id,
        is_overtime_approved,
        existing[0].id
      ]);
      
      return { id: existing[0].id, affected: result.affectedRows };
    } else {
      // 생성
      const [result] = await db.execute(`
        INSERT INTO daily_attendance_records (
          record_date, worker_id, total_work_hours, work_attendance_type_id,
          vacation_type_id, is_overtime_approved, created_by
        ) VALUES (?, ?, ?, ?, ?, ?, ?)
      `, [
        record_date,
        worker_id,
        total_work_hours,
        work_attendance_type_id,
        vacation_type_id,
        is_overtime_approved,
        created_by
      ]);
      
      return { id: result.insertId, affected: result.affectedRows };
    }
  }
 
  // 작업자별 근태 현황 조회 (대시보드용)
  static async getWorkerAttendanceStatus(date) {
    const db = await getDb();
    
    // 모든 작업자와 해당 날짜의 근태 기록을 조회
    const [rows] = await db.execute(`
      SELECT 
        w.worker_id,
        w.worker_name,
        w.job_type,
        COALESCE(dar.total_work_hours, 0) as total_work_hours,
        COALESCE(dar.status, 'incomplete') as status,
        dar.is_vacation_processed,
        dar.overtime_approved,
        wat.type_name as attendance_type_name,
        wat.type_code as attendance_type_code,
        vt.type_name as vacation_type_name,
        vt.type_code as vacation_type_code,
        dar.notes,
        -- 작업 건수 계산
        (SELECT COUNT(*) FROM daily_work_reports dwr 
         WHERE dwr.worker_id = w.worker_id AND dwr.report_date = ?) as work_count,
        -- 오류 건수 계산
        (SELECT COUNT(*) FROM daily_work_reports dwr 
         WHERE dwr.worker_id = w.worker_id AND dwr.report_date = ? AND dwr.work_status_id = 2) as error_count
      FROM workers w
      LEFT JOIN daily_attendance_records dar ON w.worker_id = dar.worker_id AND dar.record_date = ?
      LEFT JOIN work_attendance_types wat ON dar.attendance_type_id = wat.id
      LEFT JOIN vacation_types vt ON dar.vacation_type_id = vt.id
      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 * 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 (
        report_date, worker_id, project_id, work_type_id, work_status_id,
        work_hours, description, created_by
      ) VALUES (?, ?, 13, 1, 1, ?, ?, ?)
    `, [
      date,
      workerId,
      vacationHours,
      `${vacationTypeInfo.type_name} 처리`,
      createdBy
    ]);
    
    // 근태 기록 업데이트
    const attendanceData = {
      record_date: date,
      worker_id: workerId,
      total_work_hours: totalHours,
      work_attendance_type_id: attendanceTypes[0]?.id,
      vacation_type_id: vacationTypeInfo.id,
      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 
        overtime_approved = TRUE,
        overtime_approved_by = ?,
        overtime_approved_at = CURRENT_TIMESTAMP,
        updated_by = ?,
        updated_at = CURRENT_TIMESTAMP
      WHERE worker_id = ? AND record_date = ?
    `, [approvedBy, approvedBy, workerId, date]);
    
    return result.affectedRows > 0;
  }
 
  // 근로 유형 목록 조회
  static async getAttendanceTypes() {
    const db = await getDb();
    const [rows] = await db.execute(
      'SELECT * FROM work_attendance_types WHERE is_active = TRUE ORDER BY id'
    );
    return rows;
  }
 
  // 휴가 유형 목록 조회
  static async getVacationTypes() {
    const db = await getDb();
    const [rows] = await db.execute(
      'SELECT * FROM vacation_types WHERE is_active = TRUE ORDER BY hours_deduction DESC'
    );
    return rows;
  }
 
  // 작업자 휴가 잔여 조회
  static async getWorkerVacationBalance(workerId, year = null) {
    const db = await getDb();
    const currentYear = year || new Date().getFullYear();
    
    const [rows] = await db.execute(`
      SELECT * 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,
        total_annual_leave: 15.0,
        used_annual_leave: 0,
        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,
        w.worker_name,
        COUNT(CASE WHEN dar.status = 'complete' THEN 1 END) as regular_days,
        COUNT(CASE WHEN dar.status = 'overtime' THEN 1 END) as overtime_days,
        COUNT(CASE WHEN dar.status = 'vacation' THEN 1 END) as vacation_days,
        COUNT(CASE WHEN dar.status = 'partial' THEN 1 END) as partial_days,
        COUNT(CASE WHEN dar.status = 'incomplete' THEN 1 END) as incomplete_days,
        SUM(dar.total_work_hours) as total_work_hours,
        AVG(dar.total_work_hours) as avg_work_hours
      FROM workers w
      LEFT JOIN daily_attendance_records dar ON w.worker_id = dar.worker_id
        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;
  }
}
 
module.exports = AttendanceModel;