feat: 구매/안전 시스템 전면 개편 — tkpurchase 개편 + tksafety 신규 + 권한 보강
Phase 1: tkuser 협력업체 CRUD 이관 (읽기전용 → 전체 CRUD) Phase 2: tkpurchase 개편 — 일용공 신청/확정, 작업일정, 업무현황, 계정관리, 협력업체 포털 Phase 3: tksafety 신규 시스템 — 방문관리 + 안전교육 신고 Phase 4: SSO 인증 보강 (partner_company_id JWT, 만료일 체크), 권한 테이블 기반 접근 제어 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
123
tksafety/web/static/js/tksafety-core.js
Normal file
123
tksafety/web/static/js/tksafety-core.js
Normal file
@@ -0,0 +1,123 @@
|
||||
/* ===== 서비스 워커 해제 ===== */
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.getRegistrations().then(function(regs) { regs.forEach(function(r) { r.unregister(); }); });
|
||||
if (typeof caches !== 'undefined') { caches.keys().then(function(ns) { ns.forEach(function(n) { caches.delete(n); }); }); }
|
||||
}
|
||||
|
||||
/* ===== Config ===== */
|
||||
const API_BASE = '/api';
|
||||
const PURPOSE_LABELS = {
|
||||
day_labor: '일용공', equipment_repair: '설비수리', inspection: '검사',
|
||||
delivery: '납품/배송', safety_audit: '안전점검', client_audit: '고객심사',
|
||||
construction: '공사', other: '기타'
|
||||
};
|
||||
|
||||
/* ===== Token ===== */
|
||||
function _cookieGet(n) { const m = document.cookie.match(new RegExp('(?:^|; )' + n + '=([^;]*)')); return m ? decodeURIComponent(m[1]) : null; }
|
||||
function _cookieRemove(n) { let c = n + '=; path=/; max-age=0'; if (location.hostname.includes('technicalkorea.net')) c += '; domain=.technicalkorea.net; secure; samesite=lax'; document.cookie = c; }
|
||||
function getToken() { return _cookieGet('sso_token') || localStorage.getItem('sso_token'); }
|
||||
function getLoginUrl() {
|
||||
const h = location.hostname;
|
||||
const t = Date.now();
|
||||
if (h.includes('technicalkorea.net')) return location.protocol + '//tkfb.technicalkorea.net/login?redirect=' + encodeURIComponent(location.href) + '&_t=' + t;
|
||||
return location.protocol + '//' + h + ':30000/login?redirect=' + encodeURIComponent(location.href) + '&_t=' + t;
|
||||
}
|
||||
function decodeToken(t) { try { const b = atob(t.split('.')[1].replace(/-/g,'+').replace(/_/g,'/')); return JSON.parse(new TextDecoder().decode(Uint8Array.from(b, c => c.charCodeAt(0)))); } catch { return null; } }
|
||||
|
||||
/* ===== 리다이렉트 루프 방지 ===== */
|
||||
const _REDIRECT_KEY = '_sso_redirect_ts';
|
||||
function _safeRedirect() {
|
||||
const last = parseInt(sessionStorage.getItem(_REDIRECT_KEY) || '0', 10);
|
||||
if (Date.now() - last < 5000) { console.warn('[tksafety] 리다이렉트 루프 감지'); return; }
|
||||
sessionStorage.setItem(_REDIRECT_KEY, String(Date.now()));
|
||||
location.href = getLoginUrl();
|
||||
}
|
||||
|
||||
/* ===== API ===== */
|
||||
async function api(path, opts = {}) {
|
||||
const token = getToken();
|
||||
const headers = { 'Authorization': token ? `Bearer ${token}` : '', ...(opts.headers||{}) };
|
||||
if (!(opts.body instanceof FormData)) headers['Content-Type'] = 'application/json';
|
||||
const res = await fetch(API_BASE + path, { ...opts, headers });
|
||||
if (res.status === 401) { _safeRedirect(); throw new Error('인증 만료'); }
|
||||
if (res.headers.get('content-type')?.includes('text/csv')) return res;
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || '요청 실패');
|
||||
return data;
|
||||
}
|
||||
|
||||
/* ===== Toast ===== */
|
||||
function showToast(msg, type = 'success') {
|
||||
document.querySelector('.toast-message')?.remove();
|
||||
const el = document.createElement('div');
|
||||
el.className = `toast-message fixed bottom-4 right-4 px-4 py-3 rounded-lg text-white z-[10000] shadow-lg ${type==='success'?'bg-orange-500':'bg-red-500'}`;
|
||||
el.innerHTML = `<i class="fas ${type==='success'?'fa-check-circle':'fa-exclamation-circle'} mr-2"></i>${escapeHtml(msg)}`;
|
||||
document.body.appendChild(el);
|
||||
setTimeout(() => { el.classList.add('opacity-0'); setTimeout(() => el.remove(), 300); }, 3000);
|
||||
}
|
||||
|
||||
/* ===== Escape ===== */
|
||||
function escapeHtml(str) { if (!str) return ''; const d = document.createElement('div'); d.textContent = str; return d.innerHTML; }
|
||||
|
||||
/* ===== Helpers ===== */
|
||||
function formatDate(d) { if (!d) return ''; return String(d).substring(0, 10); }
|
||||
function formatTime(d) { if (!d) return ''; return String(d).substring(11, 16); }
|
||||
function formatDateTime(d) { if (!d) return ''; return String(d).substring(0, 16).replace('T', ' '); }
|
||||
function purposeLabel(p) { return PURPOSE_LABELS[p] || p || ''; }
|
||||
function purposeBadge(p) { return `<span class="badge purpose-${p}">${purposeLabel(p)}</span>`; }
|
||||
function statusBadge(s) {
|
||||
const m = { checked_in: ['badge-green', '체크인'], checked_out: ['badge-blue', '체크아웃'], auto_checkout: ['badge-amber', '자동마감'], cancelled: ['badge-gray', '취소'] };
|
||||
const [cls, label] = m[s] || ['badge-gray', s];
|
||||
return `<span class="badge ${cls}">${label}</span>`;
|
||||
}
|
||||
|
||||
/* ===== Logout ===== */
|
||||
function doLogout() {
|
||||
if (!confirm('로그아웃?')) return;
|
||||
_cookieRemove('sso_token'); _cookieRemove('sso_user'); _cookieRemove('sso_refresh_token');
|
||||
['sso_token','sso_user','sso_refresh_token','token','user','access_token','currentUser','current_user','userInfo','userPageAccess'].forEach(k => localStorage.removeItem(k));
|
||||
location.href = getLoginUrl();
|
||||
}
|
||||
|
||||
/* ===== Navbar ===== */
|
||||
function renderNavbar() {
|
||||
const currentPage = location.pathname.replace(/\//g, '') || 'index.html';
|
||||
const links = [
|
||||
{ href: '/', icon: 'fa-door-open', label: '방문 관리', match: ['', 'index.html'] },
|
||||
{ href: '/education.html', icon: 'fa-graduation-cap', label: '안전교육', match: ['education.html'] },
|
||||
];
|
||||
const nav = document.getElementById('sideNav');
|
||||
if (!nav) return;
|
||||
nav.innerHTML = links.map(l => {
|
||||
const active = l.match.some(m => currentPage === m || currentPage.endsWith(m));
|
||||
return `<a href="${l.href}" class="nav-link flex items-center gap-3 px-4 py-2.5 rounded-lg text-sm transition-colors ${active ? 'active' : 'text-gray-600 hover:bg-gray-100'}">
|
||||
<i class="fas ${l.icon} w-5 text-center"></i><span>${l.label}</span></a>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
/* ===== State ===== */
|
||||
let currentUser = null;
|
||||
|
||||
/* ===== Init ===== */
|
||||
function initAuth() {
|
||||
const token = getToken();
|
||||
if (!token) { _safeRedirect(); return false; }
|
||||
const decoded = decodeToken(token);
|
||||
if (!decoded) { _safeRedirect(); return false; }
|
||||
sessionStorage.removeItem(_REDIRECT_KEY);
|
||||
if (!localStorage.getItem('sso_token')) localStorage.setItem('sso_token', token);
|
||||
currentUser = {
|
||||
id: decoded.user_id || decoded.id,
|
||||
username: decoded.username || decoded.sub,
|
||||
name: decoded.name || decoded.full_name,
|
||||
role: (decoded.role || decoded.access_level || '').toLowerCase()
|
||||
};
|
||||
const dn = currentUser.name || currentUser.username;
|
||||
const nameEl = document.getElementById('headerUserName');
|
||||
const avatarEl = document.getElementById('headerUserAvatar');
|
||||
if (nameEl) nameEl.textContent = dn;
|
||||
if (avatarEl) avatarEl.textContent = dn.charAt(0).toUpperCase();
|
||||
renderNavbar();
|
||||
setTimeout(() => document.querySelector('.fade-in')?.classList.add('visible'), 50);
|
||||
return true;
|
||||
}
|
||||
143
tksafety/web/static/js/tksafety-education.js
Normal file
143
tksafety/web/static/js/tksafety-education.js
Normal file
@@ -0,0 +1,143 @@
|
||||
/* ===== Education Management ===== */
|
||||
let educationList = [];
|
||||
let editingEducationId = null;
|
||||
|
||||
async function loadEducation() {
|
||||
try {
|
||||
const dateFrom = document.getElementById('eduDateFrom')?.value || '';
|
||||
const dateTo = document.getElementById('eduDateTo')?.value || '';
|
||||
const targetType = document.getElementById('eduTargetType')?.value || '';
|
||||
const params = new URLSearchParams();
|
||||
if (dateFrom) params.set('date_from', dateFrom);
|
||||
if (dateTo) params.set('date_to', dateTo);
|
||||
if (targetType) params.set('target_type', targetType);
|
||||
const r = await api('/education?' + params.toString());
|
||||
educationList = r.data || [];
|
||||
renderEducationList();
|
||||
} catch (e) {
|
||||
showToast('교육 목록 로드 실패: ' + e.message, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
function renderEducationList() {
|
||||
const tbody = document.getElementById('educationTableBody');
|
||||
if (!educationList.length) {
|
||||
tbody.innerHTML = '<tr><td colspan="7" class="text-center text-gray-400 py-8">등록된 안전교육이 없습니다</td></tr>';
|
||||
return;
|
||||
}
|
||||
const typeLabels = { day_labor: '일용공', partner_schedule: '협력업체', manual: '수동등록' };
|
||||
const statusLabels = { planned: '예정', completed: '완료', cancelled: '취소' };
|
||||
const statusColors = { planned: 'badge-amber', completed: 'badge-green', cancelled: 'badge-gray' };
|
||||
tbody.innerHTML = educationList.map(e => {
|
||||
const attendeeCount = e.attendees ? (typeof e.attendees === 'string' ? JSON.parse(e.attendees) : e.attendees).length : 0;
|
||||
return `<tr>
|
||||
<td>${formatDate(e.education_date)}</td>
|
||||
<td><span class="badge ${e.target_type === 'day_labor' ? 'badge-blue' : e.target_type === 'partner_schedule' ? 'badge-green' : 'badge-gray'}">${typeLabels[e.target_type] || e.target_type}</span></td>
|
||||
<td>${escapeHtml(e.educator) || '-'}</td>
|
||||
<td class="text-center">${attendeeCount}명</td>
|
||||
<td><span class="badge ${statusColors[e.status] || 'badge-gray'}">${statusLabels[e.status] || e.status}</span></td>
|
||||
<td class="hide-mobile">${escapeHtml(e.notes) || '-'}</td>
|
||||
<td class="text-right">
|
||||
<button onclick="openEditEducation(${e.id})" class="text-gray-400 hover:text-gray-600 text-xs" title="수정"><i class="fas fa-pen"></i></button>
|
||||
<button onclick="doDeleteEducation(${e.id})" class="text-gray-400 hover:text-red-500 text-xs ml-1" title="삭제"><i class="fas fa-trash"></i></button>
|
||||
</td>
|
||||
</tr>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
/* ===== Add education modal ===== */
|
||||
function openAddEducation() {
|
||||
document.getElementById('addEducationModal').classList.remove('hidden');
|
||||
}
|
||||
function closeAddEducation() {
|
||||
document.getElementById('addEducationModal').classList.add('hidden');
|
||||
document.getElementById('addEducationForm').reset();
|
||||
}
|
||||
|
||||
async function submitAddEducation(e) {
|
||||
e.preventDefault();
|
||||
const attendeesRaw = document.getElementById('newAttendees').value.trim();
|
||||
const attendees = attendeesRaw ? attendeesRaw.split('\n').map(line => {
|
||||
const parts = line.split(',').map(s => s.trim());
|
||||
return { name: parts[0] || '', company: parts[1] || '' };
|
||||
}).filter(a => a.name) : [];
|
||||
|
||||
const data = {
|
||||
target_type: document.getElementById('newTargetType').value,
|
||||
education_date: document.getElementById('newEducationDate').value,
|
||||
educator: document.getElementById('newEducator').value.trim() || null,
|
||||
attendees: attendees,
|
||||
status: document.getElementById('newEduStatus').value || 'planned',
|
||||
notes: document.getElementById('newEduNotes').value.trim() || null,
|
||||
};
|
||||
if (!data.education_date) { showToast('교육일은 필수입니다', 'error'); return; }
|
||||
try {
|
||||
await api('/education', { method: 'POST', body: JSON.stringify(data) });
|
||||
showToast('안전교육이 등록되었습니다');
|
||||
closeAddEducation();
|
||||
await loadEducation();
|
||||
} catch (e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
|
||||
/* ===== Edit education ===== */
|
||||
function openEditEducation(id) {
|
||||
const edu = educationList.find(x => x.id === id);
|
||||
if (!edu) return;
|
||||
editingEducationId = id;
|
||||
document.getElementById('editTargetType').value = edu.target_type;
|
||||
document.getElementById('editEducationDate').value = formatDate(edu.education_date);
|
||||
document.getElementById('editEducator').value = edu.educator || '';
|
||||
document.getElementById('editEduStatus').value = edu.status;
|
||||
document.getElementById('editEduNotes').value = edu.notes || '';
|
||||
const attendees = edu.attendees ? (typeof edu.attendees === 'string' ? JSON.parse(edu.attendees) : edu.attendees) : [];
|
||||
document.getElementById('editAttendees').value = attendees.map(a => `${a.name}${a.company ? ', ' + a.company : ''}`).join('\n');
|
||||
document.getElementById('editEducationModal').classList.remove('hidden');
|
||||
}
|
||||
function closeEditEducation() {
|
||||
document.getElementById('editEducationModal').classList.add('hidden');
|
||||
editingEducationId = null;
|
||||
}
|
||||
|
||||
async function submitEditEducation(e) {
|
||||
e.preventDefault();
|
||||
if (!editingEducationId) return;
|
||||
const attendeesRaw = document.getElementById('editAttendees').value.trim();
|
||||
const attendees = attendeesRaw ? attendeesRaw.split('\n').map(line => {
|
||||
const parts = line.split(',').map(s => s.trim());
|
||||
return { name: parts[0] || '', company: parts[1] || '' };
|
||||
}).filter(a => a.name) : [];
|
||||
|
||||
const data = {
|
||||
target_type: document.getElementById('editTargetType').value,
|
||||
education_date: document.getElementById('editEducationDate').value,
|
||||
educator: document.getElementById('editEducator').value.trim() || null,
|
||||
attendees: attendees,
|
||||
status: document.getElementById('editEduStatus').value,
|
||||
notes: document.getElementById('editEduNotes').value.trim() || null,
|
||||
};
|
||||
try {
|
||||
await api(`/education/${editingEducationId}`, { method: 'PUT', body: JSON.stringify(data) });
|
||||
showToast('수정되었습니다');
|
||||
closeEditEducation();
|
||||
await loadEducation();
|
||||
} catch (e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
|
||||
async function doDeleteEducation(id) {
|
||||
if (!confirm('이 교육 기록을 삭제하시겠습니까?')) return;
|
||||
try {
|
||||
await api(`/education/${id}`, { method: 'DELETE' });
|
||||
showToast('삭제되었습니다');
|
||||
await loadEducation();
|
||||
} catch (e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
|
||||
function initEducationPage() {
|
||||
if (!initAuth()) return;
|
||||
document.getElementById('addEducationForm').addEventListener('submit', submitAddEducation);
|
||||
document.getElementById('editEducationForm').addEventListener('submit', submitEditEducation);
|
||||
document.getElementById('eduDateFrom')?.addEventListener('change', loadEducation);
|
||||
document.getElementById('eduDateTo')?.addEventListener('change', loadEducation);
|
||||
document.getElementById('eduTargetType')?.addEventListener('change', loadEducation);
|
||||
loadEducation();
|
||||
}
|
||||
272
tksafety/web/static/js/tksafety-visit.js
Normal file
272
tksafety/web/static/js/tksafety-visit.js
Normal file
@@ -0,0 +1,272 @@
|
||||
/* ===== Visit Management ===== */
|
||||
let todayVisits = [];
|
||||
let editingVisitId = null;
|
||||
|
||||
async function loadTodayVisits() {
|
||||
try {
|
||||
const r = await api('/daily-visits/today');
|
||||
const { visits, stats } = r.data;
|
||||
todayVisits = visits;
|
||||
renderStats(stats);
|
||||
renderVisitTable(visits);
|
||||
} catch (e) {
|
||||
showToast('데이터 로드 실패: ' + e.message, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
function renderStats(s) {
|
||||
document.getElementById('statTotal').textContent = s.total || 0;
|
||||
document.getElementById('statCheckedIn').textContent = s.checked_in || 0;
|
||||
document.getElementById('statCheckedOut').textContent = s.checked_out || 0;
|
||||
document.getElementById('statVisitors').textContent = s.total_visitors || 0;
|
||||
}
|
||||
|
||||
function renderVisitTable(visits) {
|
||||
const tbody = document.getElementById('visitTableBody');
|
||||
if (!visits.length) {
|
||||
tbody.innerHTML = '<tr><td colspan="8" class="text-center text-gray-400 py-8">오늘 방문 기록이 없습니다</td></tr>';
|
||||
return;
|
||||
}
|
||||
tbody.innerHTML = visits.map(v => {
|
||||
const companyName = v.partner_company_name || v.company_name || '-';
|
||||
const safetyIcon = v.safety_education_yn
|
||||
? '<i class="fas fa-check-circle text-orange-500"></i>'
|
||||
: '<i class="fas fa-exclamation-triangle text-amber-500 safety-warning" title="안전교육 미이수"></i>';
|
||||
const actions = v.status === 'checked_in'
|
||||
? `<button onclick="doCheckout(${v.id})" class="text-blue-600 hover:text-blue-800 text-xs px-2 py-1 border border-blue-200 rounded hover:bg-blue-50">체크아웃</button>`
|
||||
: '';
|
||||
return `<tr>
|
||||
<td>${escapeHtml(companyName)}</td>
|
||||
<td>${escapeHtml(v.visitor_name)}</td>
|
||||
<td class="text-center">${v.visitor_count}</td>
|
||||
<td>${purposeBadge(v.purpose)}</td>
|
||||
<td class="hide-mobile">${safetyIcon}</td>
|
||||
<td>${formatTime(v.check_in_time)}</td>
|
||||
<td>${statusBadge(v.status)}</td>
|
||||
<td class="text-right">
|
||||
${actions}
|
||||
<button onclick="openEditVisit(${v.id})" class="text-gray-400 hover:text-gray-600 text-xs ml-1" title="수정"><i class="fas fa-pen"></i></button>
|
||||
<button onclick="doDeleteVisit(${v.id})" class="text-gray-400 hover:text-red-500 text-xs ml-1" title="삭제"><i class="fas fa-trash"></i></button>
|
||||
</td>
|
||||
</tr>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
/* ===== 업체 자동완성 ===== */
|
||||
let companySearchTimeout = null;
|
||||
let selectedCompanyId = null;
|
||||
|
||||
function initCompanySearch() {
|
||||
const input = document.getElementById('companySearch');
|
||||
const dropdown = document.getElementById('companyDropdown');
|
||||
const manualToggle = document.getElementById('manualCompanyToggle');
|
||||
const manualInput = document.getElementById('manualCompanyName');
|
||||
|
||||
input.addEventListener('input', () => {
|
||||
clearTimeout(companySearchTimeout);
|
||||
selectedCompanyId = null;
|
||||
const q = input.value.trim();
|
||||
if (q.length < 1) { dropdown.classList.add('hidden'); return; }
|
||||
companySearchTimeout = setTimeout(async () => {
|
||||
try {
|
||||
const r = await api('/partners/search?q=' + encodeURIComponent(q));
|
||||
const items = r.data || [];
|
||||
if (items.length === 0) {
|
||||
dropdown.innerHTML = '<div class="px-3 py-2 text-sm text-gray-400">검색 결과 없음</div>';
|
||||
} else {
|
||||
dropdown.innerHTML = items.map(c =>
|
||||
`<div class="px-3 py-2 text-sm hover:bg-orange-50 cursor-pointer" onclick="selectCompany(${c.id}, '${escapeHtml(c.company_name).replace(/'/g, "\\'")}')">
|
||||
<span class="font-medium">${escapeHtml(c.company_name)}</span>
|
||||
${c.business_number ? `<span class="text-gray-400 text-xs ml-2">${c.business_number}</span>` : ''}
|
||||
</div>`
|
||||
).join('');
|
||||
}
|
||||
dropdown.classList.remove('hidden');
|
||||
} catch (e) { dropdown.classList.add('hidden'); }
|
||||
}, 300);
|
||||
});
|
||||
|
||||
input.addEventListener('blur', () => setTimeout(() => dropdown.classList.add('hidden'), 200));
|
||||
|
||||
manualToggle.addEventListener('change', () => {
|
||||
if (manualToggle.checked) {
|
||||
input.parentElement.classList.add('hidden');
|
||||
manualInput.parentElement.classList.remove('hidden');
|
||||
selectedCompanyId = null;
|
||||
input.value = '';
|
||||
} else {
|
||||
input.parentElement.classList.remove('hidden');
|
||||
manualInput.parentElement.classList.add('hidden');
|
||||
manualInput.value = '';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function selectCompany(id, name) {
|
||||
selectedCompanyId = id;
|
||||
document.getElementById('companySearch').value = name;
|
||||
document.getElementById('companyDropdown').classList.add('hidden');
|
||||
}
|
||||
|
||||
/* ===== 인원수 +- ===== */
|
||||
function initCounterButtons() {
|
||||
document.getElementById('countMinus').addEventListener('click', () => {
|
||||
const el = document.getElementById('visitorCount');
|
||||
const v = parseInt(el.value) || 1;
|
||||
if (v > 1) el.value = v - 1;
|
||||
});
|
||||
document.getElementById('countPlus').addEventListener('click', () => {
|
||||
const el = document.getElementById('visitorCount');
|
||||
el.value = (parseInt(el.value) || 0) + 1;
|
||||
});
|
||||
}
|
||||
|
||||
/* ===== 추가정보 접이식 ===== */
|
||||
function toggleExtra() {
|
||||
document.getElementById('extraFields').classList.toggle('open');
|
||||
const icon = document.getElementById('extraToggleIcon');
|
||||
icon.classList.toggle('fa-chevron-down');
|
||||
icon.classList.toggle('fa-chevron-up');
|
||||
}
|
||||
|
||||
/* ===== 방문 등록 ===== */
|
||||
async function submitVisit(e) {
|
||||
e.preventDefault();
|
||||
const manualMode = document.getElementById('manualCompanyToggle').checked;
|
||||
const company_id = manualMode ? null : selectedCompanyId;
|
||||
const company_name = manualMode ? document.getElementById('manualCompanyName').value.trim() : null;
|
||||
|
||||
if (!company_id && !company_name) {
|
||||
showToast('업체를 선택하거나 입력해주세요', 'error'); return;
|
||||
}
|
||||
|
||||
const data = {
|
||||
company_id,
|
||||
company_name: company_name || document.getElementById('companySearch').value.trim(),
|
||||
visitor_name: document.getElementById('visitorName').value.trim(),
|
||||
visitor_count: parseInt(document.getElementById('visitorCount').value) || 1,
|
||||
purpose: document.getElementById('visitPurpose').value,
|
||||
purpose_detail: document.getElementById('purposeDetail').value.trim() || null,
|
||||
workplace_name: document.getElementById('workplaceName').value.trim() || null,
|
||||
safety_education_yn: document.getElementById('safetyCheck').checked,
|
||||
vehicle_number: document.getElementById('vehicleNumber').value.trim() || null,
|
||||
notes: document.getElementById('visitNotes').value.trim() || null,
|
||||
managing_department: document.getElementById('managingDept').value || null,
|
||||
};
|
||||
|
||||
if (!data.visitor_name) { showToast('방문자명을 입력해주세요', 'error'); return; }
|
||||
if (!data.purpose) { showToast('방문 목적을 선택해주세요', 'error'); return; }
|
||||
|
||||
try {
|
||||
await api('/daily-visits/', { method: 'POST', body: JSON.stringify(data) });
|
||||
showToast('방문이 등록되었습니다');
|
||||
document.getElementById('visitForm').reset();
|
||||
selectedCompanyId = null;
|
||||
document.getElementById('manualCompanyToggle').checked = false;
|
||||
document.getElementById('companySearch').parentElement.classList.remove('hidden');
|
||||
document.getElementById('manualCompanyName').parentElement.classList.add('hidden');
|
||||
document.getElementById('visitorCount').value = '1';
|
||||
await loadTodayVisits();
|
||||
} catch (e) {
|
||||
showToast(e.message, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
/* ===== 체크아웃 ===== */
|
||||
async function doCheckout(id) {
|
||||
try {
|
||||
await api(`/daily-visits/${id}/checkout`, { method: 'PUT', body: JSON.stringify({}) });
|
||||
showToast('체크아웃 완료');
|
||||
await loadTodayVisits();
|
||||
} catch (e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
|
||||
async function doBulkCheckout() {
|
||||
const checkedIn = todayVisits.filter(v => v.status === 'checked_in');
|
||||
if (checkedIn.length === 0) { showToast('체크인 중인 방문이 없습니다', 'error'); return; }
|
||||
if (!confirm(`체크인 중인 ${checkedIn.length}건을 모두 체크아웃 하시겠습니까?`)) return;
|
||||
try {
|
||||
const r = await api('/daily-visits/bulk-checkout', { method: 'POST', body: JSON.stringify({}) });
|
||||
showToast(`${r.data.affected}건 체크아웃 완료`);
|
||||
await loadTodayVisits();
|
||||
} catch (e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
|
||||
/* ===== 수정 ===== */
|
||||
function openEditVisit(id) {
|
||||
const v = todayVisits.find(x => x.id === id);
|
||||
if (!v) return;
|
||||
editingVisitId = id;
|
||||
document.getElementById('editVisitorName').value = v.visitor_name;
|
||||
document.getElementById('editVisitorCount').value = v.visitor_count;
|
||||
document.getElementById('editPurpose').value = v.purpose;
|
||||
document.getElementById('editPurposeDetail').value = v.purpose_detail || '';
|
||||
document.getElementById('editWorkplace').value = v.workplace_name || '';
|
||||
document.getElementById('editSafetyCheck').checked = v.safety_education_yn;
|
||||
document.getElementById('editVehicle').value = v.vehicle_number || '';
|
||||
document.getElementById('editNotes').value = v.notes || '';
|
||||
document.getElementById('editVisitModal').classList.remove('hidden');
|
||||
}
|
||||
|
||||
function closeEditVisit() {
|
||||
document.getElementById('editVisitModal').classList.add('hidden');
|
||||
editingVisitId = null;
|
||||
}
|
||||
|
||||
async function submitEditVisit(e) {
|
||||
e.preventDefault();
|
||||
if (!editingVisitId) return;
|
||||
const data = {
|
||||
visitor_name: document.getElementById('editVisitorName').value.trim(),
|
||||
visitor_count: parseInt(document.getElementById('editVisitorCount').value) || 1,
|
||||
purpose: document.getElementById('editPurpose').value,
|
||||
purpose_detail: document.getElementById('editPurposeDetail').value.trim() || null,
|
||||
workplace_name: document.getElementById('editWorkplace').value.trim() || null,
|
||||
safety_education_yn: document.getElementById('editSafetyCheck').checked,
|
||||
vehicle_number: document.getElementById('editVehicle').value.trim() || null,
|
||||
notes: document.getElementById('editNotes').value.trim() || null,
|
||||
};
|
||||
try {
|
||||
await api(`/daily-visits/${editingVisitId}`, { method: 'PUT', body: JSON.stringify(data) });
|
||||
showToast('수정되었습니다');
|
||||
closeEditVisit();
|
||||
await loadTodayVisits();
|
||||
} catch (e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
|
||||
/* ===== 삭제 ===== */
|
||||
async function doDeleteVisit(id) {
|
||||
if (!confirm('이 방문 기록을 삭제하시겠습니까?')) return;
|
||||
try {
|
||||
await api(`/daily-visits/${id}`, { method: 'DELETE' });
|
||||
showToast('삭제되었습니다');
|
||||
await loadTodayVisits();
|
||||
} catch (e) { showToast(e.message, 'error'); }
|
||||
}
|
||||
|
||||
/* ===== CSV 내보내기 ===== */
|
||||
async function exportVisits() {
|
||||
const token = getToken();
|
||||
const dateFrom = document.getElementById('exportDateFrom')?.value || '';
|
||||
const dateTo = document.getElementById('exportDateTo')?.value || '';
|
||||
let url = API_BASE + '/daily-visits/export?';
|
||||
if (dateFrom) url += 'date_from=' + dateFrom + '&';
|
||||
if (dateTo) url += 'date_to=' + dateTo + '&';
|
||||
const res = await fetch(url, { headers: { 'Authorization': `Bearer ${token}` } });
|
||||
if (!res.ok) { showToast('내보내기 실패', 'error'); return; }
|
||||
const blob = await res.blob();
|
||||
const a = document.createElement('a');
|
||||
a.href = URL.createObjectURL(blob);
|
||||
a.download = `visits_${dateFrom || 'all'}_${dateTo || 'all'}.csv`;
|
||||
a.click();
|
||||
}
|
||||
|
||||
/* ===== Init ===== */
|
||||
function initVisitPage() {
|
||||
if (!initAuth()) return;
|
||||
initCompanySearch();
|
||||
initCounterButtons();
|
||||
document.getElementById('visitForm').addEventListener('submit', submitVisit);
|
||||
document.getElementById('editVisitForm').addEventListener('submit', submitEditVisit);
|
||||
loadTodayVisits();
|
||||
}
|
||||
Reference in New Issue
Block a user