feat: tkpurchase 시스템 Phase 1 - 협력업체 마스터 + 당일 방문 관리

신규 독립 시스템 tkpurchase (구매/방문 관리) 구축:
- 협력업체 CRUD + 소속 작업자 관리 (마스터 데이터 소유)
- 당일 방문 등록/체크인/체크아웃 + 일괄 마감
- 업체 자동완성, CSV 내보내기, 집계 통계
- 자정 자동 체크아웃 (node-cron)
- tkuser 협력업체 읽기 전용 탭 + 권한 그리드(tkpurchase-perms) 추가
- docker-compose에 tkpurchase-api/web 서비스 추가

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Hyungi Ahn
2026-03-12 15:45:37 +09:00
parent 5b1b89254c
commit 281f5d35d1
29 changed files with 2641 additions and 7 deletions

18
tkpurchase/api/Dockerfile Normal file
View File

@@ -0,0 +1,18 @@
FROM node:18-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --omit=dev
COPY . .
RUN chown -R node:node /usr/src/app
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s --start-period=20s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1); })"
CMD ["node", "index.js"]

View File

@@ -0,0 +1,136 @@
const dailyVisitModel = require('../models/dailyVisitModel');
const PURPOSE_LABELS = {
day_labor: '일용공', equipment_repair: '설비수리', inspection: '검사',
delivery: '납품/배송', safety_audit: '안전점검', client_audit: '고객심사',
construction: '공사', other: '기타'
};
async function today(req, res) {
try {
const [visits, stats] = await Promise.all([
dailyVisitModel.findToday(),
dailyVisitModel.getTodayStats()
]);
res.json({ success: true, data: { visits, stats } });
} catch (err) {
console.error('Today visits error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function list(req, res) {
try {
const rows = await dailyVisitModel.findAll(req.query);
res.json({ success: true, data: rows });
} catch (err) {
console.error('Visit list error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function create(req, res) {
try {
const { visitor_name, purpose, company_id, company_name } = req.body;
if (!visitor_name || !visitor_name.trim()) {
return res.status(400).json({ success: false, error: '방문자명은 필수입니다' });
}
if (!purpose) {
return res.status(400).json({ success: false, error: '방문 목적은 필수입니다' });
}
if (!company_id && (!company_name || !company_name.trim())) {
return res.status(400).json({ success: false, error: '업체를 선택하거나 업체명을 입력해주세요' });
}
const userId = req.user.user_id || req.user.id;
const visit = await dailyVisitModel.create({ ...req.body, registered_by: userId });
res.status(201).json({ success: true, data: visit });
} catch (err) {
console.error('Visit create error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function update(req, res) {
try {
const visit = await dailyVisitModel.update(req.params.id, req.body);
if (!visit) return res.status(404).json({ success: false, error: '방문 기록을 찾을 수 없습니다' });
res.json({ success: true, data: visit });
} catch (err) {
console.error('Visit update error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function checkout(req, res) {
try {
const visit = await dailyVisitModel.checkout(req.params.id, req.body.checkout_note);
if (!visit) return res.status(404).json({ success: false, error: '방문 기록을 찾을 수 없습니다' });
res.json({ success: true, data: visit });
} catch (err) {
console.error('Checkout error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function bulkCheckout(req, res) {
try {
const result = await dailyVisitModel.bulkCheckout();
res.json({ success: true, data: { affected: result.affectedRows } });
} catch (err) {
console.error('Bulk checkout error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function deleteVisit(req, res) {
try {
await dailyVisitModel.deleteVisit(req.params.id);
res.json({ success: true, message: '삭제 완료' });
} catch (err) {
console.error('Visit delete error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function stats(req, res) {
try {
const data = await dailyVisitModel.getStats(req.query);
res.json({ success: true, data });
} catch (err) {
console.error('Stats error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function exportCsv(req, res) {
try {
const rows = await dailyVisitModel.exportCsv(req.query);
const BOM = '\uFEFF';
const header = '방문일,업체,방문자,인원,목적,상세,작업장,안전교육,차량번호,체크인,체크아웃,상태,담당부서,비고';
const lines = rows.map(r => [
r.visit_date ? String(r.visit_date).substring(0, 10) : '',
`"${(r.company || '').replace(/"/g, '""')}"`,
`"${(r.visitor_name || '').replace(/"/g, '""')}"`,
r.visitor_count || 1,
PURPOSE_LABELS[r.purpose] || r.purpose,
`"${(r.purpose_detail || '').replace(/"/g, '""')}"`,
`"${(r.workplace_name || '').replace(/"/g, '""')}"`,
r.safety_education_yn ? 'Y' : 'N',
r.vehicle_number || '',
r.check_in_time || '',
r.check_out_time || '',
r.status,
r.managing_department || '',
`"${(r.notes || '').replace(/"/g, '""')}"`
].join(','));
res.setHeader('Content-Type', 'text/csv; charset=utf-8');
res.setHeader('Content-Disposition', 'attachment; filename=visits.csv');
res.send(BOM + header + '\n' + lines.join('\n'));
} catch (err) {
console.error('Export error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
module.exports = { today, list, create, update, checkout, bulkCheckout, deleteVisit, stats, exportCsv };

View File

@@ -0,0 +1,147 @@
const partnerModel = require('../models/partnerModel');
// 업체 목록
async function list(req, res) {
try {
const { search, is_active } = req.query;
const rows = await partnerModel.findAll({
search,
is_active: is_active !== undefined ? is_active === 'true' || is_active === '1' : undefined
});
res.json({ success: true, data: rows });
} catch (err) {
console.error('Partner list error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 업체 상세 (작업자 포함)
async function getById(req, res) {
try {
const company = await partnerModel.findById(req.params.id);
if (!company) return res.status(404).json({ success: false, error: '업체를 찾을 수 없습니다' });
const workers = await partnerModel.findWorkersByCompany(req.params.id);
res.json({ success: true, data: { ...company, workers } });
} catch (err) {
console.error('Partner get error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 업체 등록
async function create(req, res) {
try {
const { company_name } = req.body;
if (!company_name || !company_name.trim()) {
return res.status(400).json({ success: false, error: '업체명은 필수입니다' });
}
const company = await partnerModel.create(req.body);
res.status(201).json({ success: true, data: company });
} catch (err) {
if (err.code === 'ER_DUP_ENTRY') {
return res.status(400).json({ success: false, error: '이미 등록된 사업자번호입니다' });
}
console.error('Partner create error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 업체 수정
async function update(req, res) {
try {
const company = await partnerModel.update(req.params.id, req.body);
if (!company) return res.status(404).json({ success: false, error: '업체를 찾을 수 없습니다' });
res.json({ success: true, data: company });
} catch (err) {
if (err.code === 'ER_DUP_ENTRY') {
return res.status(400).json({ success: false, error: '이미 등록된 사업자번호입니다' });
}
console.error('Partner update error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 업체 비활성화
async function deactivate(req, res) {
try {
await partnerModel.deactivate(req.params.id);
res.json({ success: true, message: '비활성화 완료' });
} catch (err) {
console.error('Partner deactivate error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 업체 자동완성 검색
async function searchCompanies(req, res) {
try {
const q = req.query.q || '';
if (q.length < 1) return res.json({ success: true, data: [] });
const rows = await partnerModel.search(q);
res.json({ success: true, data: rows });
} catch (err) {
console.error('Partner search error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 소속 작업자 목록
async function listWorkers(req, res) {
try {
const rows = await partnerModel.findWorkersByCompany(req.params.id);
res.json({ success: true, data: rows });
} catch (err) {
console.error('Workers list error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 작업자 등록
async function createWorker(req, res) {
try {
const { worker_name, is_team_leader, phone } = req.body;
if (!worker_name || !worker_name.trim()) {
return res.status(400).json({ success: false, error: '작업자명은 필수입니다' });
}
if (is_team_leader && (!phone || !phone.trim())) {
return res.status(400).json({ success: false, error: '팀장급은 연락처 필수입니다' });
}
const worker = await partnerModel.createWorker(req.params.id, req.body);
res.status(201).json({ success: true, data: worker });
} catch (err) {
console.error('Worker create error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 작업자 수정
async function updateWorker(req, res) {
try {
const { is_team_leader, phone } = req.body;
if (is_team_leader && (!phone || !phone.trim())) {
return res.status(400).json({ success: false, error: '팀장급은 연락처 필수입니다' });
}
const worker = await partnerModel.updateWorker(req.params.id, req.body);
if (!worker) return res.status(404).json({ success: false, error: '작업자를 찾을 수 없습니다' });
res.json({ success: true, data: worker });
} catch (err) {
console.error('Worker update error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 작업자 비활성화
async function deactivateWorker(req, res) {
try {
await partnerModel.deactivateWorker(req.params.id);
res.json({ success: true, message: '비활성화 완료' });
} catch (err) {
console.error('Worker deactivate error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
module.exports = {
list, getById, create, update, deactivate, searchCompanies,
listWorkers, createWorker, updateWorker, deactivateWorker
};

67
tkpurchase/api/index.js Normal file
View File

@@ -0,0 +1,67 @@
const express = require('express');
const cors = require('cors');
const cron = require('node-cron');
const partnerRoutes = require('./routes/partnerRoutes');
const dailyVisitRoutes = require('./routes/dailyVisitRoutes');
const dailyVisitModel = require('./models/dailyVisitModel');
const app = express();
const PORT = process.env.PORT || 3000;
const allowedOrigins = [
'https://tkfb.technicalkorea.net',
'https://tkreport.technicalkorea.net',
'https://tkqc.technicalkorea.net',
'https://tkuser.technicalkorea.net',
'https://tkpurchase.technicalkorea.net',
];
if (process.env.NODE_ENV === 'development') {
allowedOrigins.push('http://localhost:30080', 'http://localhost:30480');
}
app.use(cors({
origin: function(origin, cb) {
if (!origin || allowedOrigins.includes(origin) || /^http:\/\/192\.168\.\d+\.\d+(:\d+)?$/.test(origin)) return cb(null, true);
cb(new Error('CORS blocked: ' + origin));
},
credentials: true
}));
app.use(express.json());
// Health check
app.get('/health', (req, res) => {
res.json({ status: 'ok', service: 'tkpurchase-api', timestamp: new Date().toISOString() });
});
// Routes
app.use('/api/partners', partnerRoutes);
app.use('/api/daily-visits', dailyVisitRoutes);
// 404
app.use((req, res) => {
res.status(404).json({ success: false, error: 'Not Found' });
});
// Error handler
app.use((err, req, res, next) => {
console.error('tkpurchase-api Error:', err.message);
res.status(err.status || 500).json({
success: false,
error: err.message || 'Internal Server Error'
});
});
// 자정 자동 체크아웃 (매일 23:59 KST)
cron.schedule('59 23 * * *', async () => {
try {
const result = await dailyVisitModel.autoCheckoutAll();
console.log(`Auto checkout: ${result.affectedRows} visits`);
} catch (e) {
console.error('Auto checkout failed:', e);
}
}, { timezone: 'Asia/Seoul' });
app.listen(PORT, () => {
console.log(`tkpurchase-api running on port ${PORT}`);
});
module.exports = app;

View File

@@ -0,0 +1,101 @@
const jwt = require('jsonwebtoken');
const mysql = require('mysql2/promise');
const JWT_SECRET = process.env.SSO_JWT_SECRET;
let pool;
function getPool() {
if (!pool) {
pool = mysql.createPool({
host: process.env.DB_HOST || 'mariadb',
port: parseInt(process.env.DB_PORT) || 3306,
user: process.env.DB_USER || 'hyungi_user',
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME || 'hyungi',
waitForConnections: true,
connectionLimit: 5,
queueLimit: 0
});
}
return pool;
}
function extractToken(req) {
const authHeader = req.headers['authorization'];
if (authHeader && authHeader.startsWith('Bearer ')) {
return authHeader.split(' ')[1];
}
if (req.cookies && req.cookies.sso_token) {
return req.cookies.sso_token;
}
return null;
}
function requireAuth(req, res, next) {
const token = extractToken(req);
if (!token) {
return res.status(401).json({ success: false, error: '인증이 필요합니다' });
}
try {
const decoded = jwt.verify(token, JWT_SECRET);
req.user = decoded;
next();
} catch {
return res.status(401).json({ success: false, error: '유효하지 않은 토큰입니다' });
}
}
function requireAdmin(req, res, next) {
const token = extractToken(req);
if (!token) {
return res.status(401).json({ success: false, error: '인증이 필요합니다' });
}
try {
const decoded = jwt.verify(token, JWT_SECRET);
if (!['admin', 'system'].includes((decoded.role || '').toLowerCase())) {
return res.status(403).json({ success: false, error: '관리자 권한이 필요합니다' });
}
req.user = decoded;
next();
} catch {
return res.status(401).json({ success: false, error: '유효하지 않은 토큰입니다' });
}
}
function requirePage(pageName) {
return async (req, res, next) => {
const userId = req.user.user_id || req.user.id;
const role = (req.user.role || '').toLowerCase();
if (role === 'admin' || role === 'system') return next();
try {
const db = getPool();
// 1. 개인 권한
const [rows] = await db.query(
'SELECT can_access FROM user_page_permissions WHERE user_id = ? AND page_name = ?',
[userId, pageName]
);
if (rows.length > 0) {
return rows[0].can_access ? next() : res.status(403).json({ success: false, error: '접근 권한이 없습니다' });
}
// 2. 부서 권한
const [userRows] = await db.query('SELECT department_id FROM sso_users WHERE user_id = ?', [userId]);
if (userRows.length > 0 && userRows[0].department_id) {
const [deptRows] = await db.query(
'SELECT can_access FROM department_page_permissions WHERE department_id = ? AND page_name = ?',
[userRows[0].department_id, pageName]
);
if (deptRows.length > 0) {
return deptRows[0].can_access ? next() : res.status(403).json({ success: false, error: '접근 권한이 없습니다' });
}
}
// 3. 기본 거부
return res.status(403).json({ success: false, error: '접근 권한이 없습니다' });
} catch (err) {
console.error('Permission check error:', err);
return res.status(500).json({ success: false, error: '권한 확인 실패' });
}
};
}
module.exports = { extractToken, requireAuth, requireAdmin, requirePage };

View File

@@ -0,0 +1,181 @@
const { getPool } = require('./partnerModel');
async function findToday() {
const db = getPool();
const [rows] = await db.query(
`SELECT dv.*, pc.company_name AS partner_company_name
FROM daily_visits dv
LEFT JOIN partner_companies pc ON dv.company_id = pc.id
WHERE dv.visit_date = CURDATE()
ORDER BY dv.check_in_time DESC`
);
return rows;
}
async function getTodayStats() {
const db = getPool();
const [rows] = await db.query(
`SELECT
COUNT(*) AS total,
SUM(CASE WHEN status = 'checked_in' THEN 1 ELSE 0 END) AS checked_in,
SUM(CASE WHEN status IN ('checked_out','auto_checkout') THEN 1 ELSE 0 END) AS checked_out,
SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled,
SUM(visitor_count) AS total_visitors
FROM daily_visits WHERE visit_date = CURDATE()`
);
return rows[0];
}
async function findAll({ visit_date, date_from, date_to, company_id, purpose, status, page = 1, limit = 50 } = {}) {
const db = getPool();
let sql = `SELECT dv.*, pc.company_name AS partner_company_name
FROM daily_visits dv
LEFT JOIN partner_companies pc ON dv.company_id = pc.id WHERE 1=1`;
const params = [];
if (visit_date) { sql += ' AND dv.visit_date = ?'; params.push(visit_date); }
if (date_from) { sql += ' AND dv.visit_date >= ?'; params.push(date_from); }
if (date_to) { sql += ' AND dv.visit_date <= ?'; params.push(date_to); }
if (company_id) { sql += ' AND dv.company_id = ?'; params.push(company_id); }
if (purpose) { sql += ' AND dv.purpose = ?'; params.push(purpose); }
if (status) { sql += ' AND dv.status = ?'; params.push(status); }
sql += ' ORDER BY dv.visit_date DESC, dv.check_in_time DESC';
const offset = (page - 1) * limit;
sql += ' LIMIT ? OFFSET ?';
params.push(limit, offset);
const [rows] = await db.query(sql, params);
return rows;
}
async function findById(id) {
const db = getPool();
const [rows] = await db.query(
`SELECT dv.*, pc.company_name AS partner_company_name
FROM daily_visits dv
LEFT JOIN partner_companies pc ON dv.company_id = pc.id
WHERE dv.id = ?`,
[id]
);
return rows[0] || null;
}
async function create(data) {
const db = getPool();
const [result] = await db.query(
`INSERT INTO daily_visits (visit_date, company_id, company_name, visitor_name, visitor_count,
purpose, purpose_detail, workplace_name, safety_education_yn, vehicle_number,
check_in_time, notes, managing_department, registered_by)
VALUES (CURDATE(), ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?, ?, ?)`,
[data.company_id || null, data.company_name || null, data.visitor_name, data.visitor_count || 1,
data.purpose, data.purpose_detail || null, data.workplace_name || null,
data.safety_education_yn || false, data.vehicle_number || null,
data.notes || null, data.managing_department || null, data.registered_by]
);
// 개별 인원 명단 (선택)
if (data.workers && data.workers.length > 0) {
for (const w of data.workers) {
await db.query(
'INSERT INTO daily_visit_workers (daily_visit_id, partner_worker_id, worker_name) VALUES (?, ?, ?)',
[result.insertId, w.partner_worker_id || null, w.worker_name]
);
}
}
return findById(result.insertId);
}
async function update(id, data) {
const db = getPool();
const fields = [];
const values = [];
if (data.company_id !== undefined) { fields.push('company_id = ?'); values.push(data.company_id || null); }
if (data.company_name !== undefined) { fields.push('company_name = ?'); values.push(data.company_name || null); }
if (data.visitor_name !== undefined) { fields.push('visitor_name = ?'); values.push(data.visitor_name); }
if (data.visitor_count !== undefined) { fields.push('visitor_count = ?'); values.push(data.visitor_count); }
if (data.purpose !== undefined) { fields.push('purpose = ?'); values.push(data.purpose); }
if (data.purpose_detail !== undefined) { fields.push('purpose_detail = ?'); values.push(data.purpose_detail || null); }
if (data.workplace_name !== undefined) { fields.push('workplace_name = ?'); values.push(data.workplace_name || null); }
if (data.safety_education_yn !== undefined) { fields.push('safety_education_yn = ?'); values.push(data.safety_education_yn); }
if (data.vehicle_number !== undefined) { fields.push('vehicle_number = ?'); values.push(data.vehicle_number || null); }
if (data.notes !== undefined) { fields.push('notes = ?'); values.push(data.notes || null); }
if (data.managing_department !== undefined) { fields.push('managing_department = ?'); values.push(data.managing_department || null); }
if (data.status !== undefined) { fields.push('status = ?'); values.push(data.status); }
if (fields.length === 0) return findById(id);
values.push(id);
await db.query(`UPDATE daily_visits SET ${fields.join(', ')} WHERE id = ?`, values);
return findById(id);
}
async function checkout(id, note) {
const db = getPool();
await db.query(
`UPDATE daily_visits SET status = 'checked_out', check_out_time = NOW(), checkout_note = ? WHERE id = ? AND status = 'checked_in'`,
[note || null, id]
);
return findById(id);
}
async function bulkCheckout() {
const db = getPool();
const [result] = await db.query(
`UPDATE daily_visits SET status = 'checked_out', check_out_time = NOW() WHERE visit_date = CURDATE() AND status = 'checked_in'`
);
return result;
}
async function autoCheckoutAll() {
const db = getPool();
const [result] = await db.query(
`UPDATE daily_visits SET status = 'auto_checkout', check_out_time = NOW() WHERE visit_date = CURDATE() AND status = 'checked_in'`
);
return result;
}
async function deleteVisit(id) {
const db = getPool();
await db.query('DELETE FROM daily_visit_workers WHERE daily_visit_id = ?', [id]);
await db.query('DELETE FROM daily_visits WHERE id = ?', [id]);
}
async function getStats({ date_from, date_to } = {}) {
const db = getPool();
const params = [];
let dateFilter = '';
if (date_from) { dateFilter += ' AND visit_date >= ?'; params.push(date_from); }
if (date_to) { dateFilter += ' AND visit_date <= ?'; params.push(date_to); }
const [byPurpose] = await db.query(
`SELECT purpose, COUNT(*) AS cnt, SUM(visitor_count) AS total_visitors FROM daily_visits WHERE 1=1 ${dateFilter} GROUP BY purpose ORDER BY cnt DESC`,
params
);
const [byCompany] = await db.query(
`SELECT COALESCE(pc.company_name, dv.company_name, '미등록') AS company, COUNT(*) AS cnt, SUM(dv.visitor_count) AS total_visitors
FROM daily_visits dv LEFT JOIN partner_companies pc ON dv.company_id = pc.id WHERE 1=1 ${dateFilter} GROUP BY company ORDER BY cnt DESC LIMIT 20`,
params
);
const [byDate] = await db.query(
`SELECT visit_date, COUNT(*) AS cnt, SUM(visitor_count) AS total_visitors FROM daily_visits WHERE 1=1 ${dateFilter} GROUP BY visit_date ORDER BY visit_date DESC LIMIT 30`,
params
);
return { byPurpose, byCompany, byDate };
}
async function exportCsv({ date_from, date_to, company_id, purpose } = {}) {
const db = getPool();
let sql = `SELECT dv.visit_date, COALESCE(pc.company_name, dv.company_name, '') AS company,
dv.visitor_name, dv.visitor_count, dv.purpose, dv.purpose_detail, dv.workplace_name,
dv.safety_education_yn, dv.vehicle_number, dv.check_in_time, dv.check_out_time,
dv.status, dv.managing_department, dv.notes
FROM daily_visits dv LEFT JOIN partner_companies pc ON dv.company_id = pc.id WHERE 1=1`;
const params = [];
if (date_from) { sql += ' AND dv.visit_date >= ?'; params.push(date_from); }
if (date_to) { sql += ' AND dv.visit_date <= ?'; params.push(date_to); }
if (company_id) { sql += ' AND dv.company_id = ?'; params.push(company_id); }
if (purpose) { sql += ' AND dv.purpose = ?'; params.push(purpose); }
sql += ' ORDER BY dv.visit_date DESC, dv.check_in_time DESC';
const [rows] = await db.query(sql, params);
return rows;
}
module.exports = {
findToday, getTodayStats, findAll, findById, create, update,
checkout, bulkCheckout, autoCheckoutAll, deleteVisit, getStats, exportCsv
};

View File

@@ -0,0 +1,141 @@
const mysql = require('mysql2/promise');
let pool;
function getPool() {
if (!pool) {
pool = mysql.createPool({
host: process.env.DB_HOST || 'mariadb',
port: parseInt(process.env.DB_PORT) || 3306,
user: process.env.DB_USER || 'hyungi_user',
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME || 'hyungi',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
}
return pool;
}
// ===== 협력업체 =====
async function findAll({ search, is_active } = {}) {
const db = getPool();
let sql = 'SELECT * FROM partner_companies WHERE 1=1';
const params = [];
if (is_active !== undefined) { sql += ' AND is_active = ?'; params.push(is_active); }
if (search) { sql += ' AND (company_name LIKE ? OR business_number LIKE ?)'; params.push(`%${search}%`, `%${search}%`); }
sql += ' ORDER BY company_name';
const [rows] = await db.query(sql, params);
return rows;
}
async function findById(id) {
const db = getPool();
const [rows] = await db.query('SELECT * FROM partner_companies WHERE id = ?', [id]);
return rows[0] || null;
}
async function search(q) {
const db = getPool();
const [rows] = await db.query(
'SELECT id, company_name, business_number FROM partner_companies WHERE is_active = TRUE AND company_name LIKE ? ORDER BY company_name LIMIT 20',
[`%${q}%`]
);
return rows;
}
async function create(data) {
const db = getPool();
const [result] = await db.query(
`INSERT INTO partner_companies (company_name, business_number, representative, contact_name, contact_phone, address, business_type, insurance_number, insurance_expiry, notes)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[data.company_name, data.business_number || null, data.representative || null,
data.contact_name || null, data.contact_phone || null, data.address || null,
data.business_type ? JSON.stringify(data.business_type) : null,
data.insurance_number || null, data.insurance_expiry || null, data.notes || null]
);
return findById(result.insertId);
}
async function update(id, data) {
const db = getPool();
const fields = [];
const values = [];
if (data.company_name !== undefined) { fields.push('company_name = ?'); values.push(data.company_name); }
if (data.business_number !== undefined) { fields.push('business_number = ?'); values.push(data.business_number || null); }
if (data.representative !== undefined) { fields.push('representative = ?'); values.push(data.representative || null); }
if (data.contact_name !== undefined) { fields.push('contact_name = ?'); values.push(data.contact_name || null); }
if (data.contact_phone !== undefined) { fields.push('contact_phone = ?'); values.push(data.contact_phone || null); }
if (data.address !== undefined) { fields.push('address = ?'); values.push(data.address || null); }
if (data.business_type !== undefined) { fields.push('business_type = ?'); values.push(data.business_type ? JSON.stringify(data.business_type) : null); }
if (data.insurance_number !== undefined) { fields.push('insurance_number = ?'); values.push(data.insurance_number || null); }
if (data.insurance_expiry !== undefined) { fields.push('insurance_expiry = ?'); values.push(data.insurance_expiry || null); }
if (data.notes !== undefined) { fields.push('notes = ?'); values.push(data.notes || null); }
if (data.is_active !== undefined) { fields.push('is_active = ?'); values.push(data.is_active); }
if (fields.length === 0) return findById(id);
values.push(id);
await db.query(`UPDATE partner_companies SET ${fields.join(', ')} WHERE id = ?`, values);
return findById(id);
}
async function deactivate(id) {
const db = getPool();
await db.query('UPDATE partner_companies SET is_active = FALSE WHERE id = ?', [id]);
}
// ===== 작업자 =====
async function findWorkersByCompany(companyId) {
const db = getPool();
const [rows] = await db.query(
'SELECT * FROM partner_workers WHERE company_id = ? ORDER BY is_team_leader DESC, worker_name',
[companyId]
);
return rows;
}
async function findWorkerById(id) {
const db = getPool();
const [rows] = await db.query('SELECT * FROM partner_workers WHERE id = ?', [id]);
return rows[0] || null;
}
async function createWorker(companyId, data) {
const db = getPool();
const [result] = await db.query(
`INSERT INTO partner_workers (company_id, worker_name, position, is_team_leader, phone, safety_training_date, notes)
VALUES (?, ?, ?, ?, ?, ?, ?)`,
[companyId, data.worker_name, data.position || null,
data.is_team_leader || false, data.phone || null,
data.safety_training_date || null, data.notes || null]
);
return findWorkerById(result.insertId);
}
async function updateWorker(id, data) {
const db = getPool();
const fields = [];
const values = [];
if (data.worker_name !== undefined) { fields.push('worker_name = ?'); values.push(data.worker_name); }
if (data.position !== undefined) { fields.push('position = ?'); values.push(data.position || null); }
if (data.is_team_leader !== undefined) { fields.push('is_team_leader = ?'); values.push(data.is_team_leader); }
if (data.phone !== undefined) { fields.push('phone = ?'); values.push(data.phone || null); }
if (data.safety_training_date !== undefined) { fields.push('safety_training_date = ?'); values.push(data.safety_training_date || null); }
if (data.notes !== undefined) { fields.push('notes = ?'); values.push(data.notes || null); }
if (data.is_active !== undefined) { fields.push('is_active = ?'); values.push(data.is_active); }
if (fields.length === 0) return findWorkerById(id);
values.push(id);
await db.query(`UPDATE partner_workers SET ${fields.join(', ')} WHERE id = ?`, values);
return findWorkerById(id);
}
async function deactivateWorker(id) {
const db = getPool();
await db.query('UPDATE partner_workers SET is_active = FALSE WHERE id = ?', [id]);
}
module.exports = {
getPool, findAll, findById, search, create, update, deactivate,
findWorkersByCompany, findWorkerById, createWorker, updateWorker, deactivateWorker
};

View File

@@ -0,0 +1,17 @@
{
"name": "tkpurchase-api",
"version": "1.0.0",
"description": "TK Factory Services - 구매/방문 관리 서비스",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "node --watch index.js"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.0",
"mysql2": "^3.14.1",
"node-cron": "^3.0.3"
}
}

View File

@@ -0,0 +1,18 @@
const express = require('express');
const router = express.Router();
const { requireAuth, requirePage } = require('../middleware/auth');
const ctrl = require('../controllers/dailyVisitController');
router.use(requireAuth);
router.get('/today', ctrl.today);
router.get('/export', ctrl.exportCsv);
router.get('/stats', ctrl.stats);
router.get('/', ctrl.list);
router.post('/', requirePage('purchasing_visit'), ctrl.create);
router.post('/bulk-checkout', requirePage('purchasing_visit'), ctrl.bulkCheckout);
router.put('/:id', requirePage('purchasing_visit'), ctrl.update);
router.put('/:id/checkout', requirePage('purchasing_visit'), ctrl.checkout);
router.delete('/:id', requirePage('purchasing_visit'), ctrl.deleteVisit);
module.exports = router;

View File

@@ -0,0 +1,20 @@
const express = require('express');
const router = express.Router();
const { requireAuth, requirePage } = require('../middleware/auth');
const ctrl = require('../controllers/partnerController');
router.use(requireAuth);
router.get('/search', ctrl.searchCompanies);
router.get('/', ctrl.list);
router.get('/:id', ctrl.getById);
router.post('/', requirePage('purchasing_partner'), ctrl.create);
router.put('/:id', requirePage('purchasing_partner'), ctrl.update);
router.delete('/:id', requirePage('purchasing_partner'), ctrl.deactivate);
router.get('/:id/workers', ctrl.listWorkers);
router.post('/:id/workers', requirePage('purchasing_partner'), ctrl.createWorker);
router.put('/workers/:id', requirePage('purchasing_partner'), ctrl.updateWorker);
router.delete('/workers/:id', requirePage('purchasing_partner'), ctrl.deactivateWorker);
module.exports = router;

View File

@@ -0,0 +1,75 @@
-- tkpurchase Phase 1: 협력업체 + 방문 관리 테이블
-- ① 협력업체
CREATE TABLE IF NOT EXISTS partner_companies (
id INT AUTO_INCREMENT PRIMARY KEY,
company_name VARCHAR(200) NOT NULL,
business_number VARCHAR(20) UNIQUE,
representative VARCHAR(100),
contact_name VARCHAR(100),
contact_phone VARCHAR(20),
address VARCHAR(500),
business_type JSON,
insurance_number VARCHAR(30),
insurance_expiry DATE,
notes TEXT,
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
-- ② 협력업체 소속 작업자
CREATE TABLE IF NOT EXISTS partner_workers (
id INT AUTO_INCREMENT PRIMARY KEY,
company_id INT NOT NULL,
worker_name VARCHAR(100) NOT NULL,
position VARCHAR(50),
is_team_leader BOOLEAN DEFAULT FALSE,
phone VARCHAR(20),
safety_training_date DATE,
notes TEXT,
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (company_id) REFERENCES partner_companies(id)
);
-- ③ 당일 방문 기록
CREATE TABLE IF NOT EXISTS daily_visits (
id INT AUTO_INCREMENT PRIMARY KEY,
visit_date DATE NOT NULL,
company_id INT,
company_name VARCHAR(200),
visitor_name VARCHAR(100) NOT NULL,
visitor_count INT DEFAULT 1,
purpose ENUM('day_labor','equipment_repair','inspection','delivery',
'safety_audit','client_audit','construction','other') NOT NULL,
purpose_detail VARCHAR(500),
workplace_name VARCHAR(200),
safety_education_yn BOOLEAN DEFAULT FALSE,
vehicle_number VARCHAR(20),
check_in_time DATETIME,
check_out_time DATETIME,
checkout_note VARCHAR(500),
notes TEXT,
status ENUM('checked_in','checked_out','auto_checkout','cancelled') DEFAULT 'checked_in',
managing_department VARCHAR(50),
registered_by INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_visit_date (visit_date),
INDEX idx_company (company_id),
INDEX idx_status (status),
FOREIGN KEY (company_id) REFERENCES partner_companies(id) ON DELETE SET NULL
);
-- ④ 방문 건별 개별 인원 명단
CREATE TABLE IF NOT EXISTS daily_visit_workers (
id INT AUTO_INCREMENT PRIMARY KEY,
daily_visit_id INT NOT NULL,
partner_worker_id INT,
worker_name VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (daily_visit_id) REFERENCES daily_visits(id) ON DELETE CASCADE,
FOREIGN KEY (partner_worker_id) REFERENCES partner_workers(id) ON DELETE SET NULL
);

10
tkpurchase/web/Dockerfile Normal file
View File

@@ -0,0 +1,10 @@
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY index.html /usr/share/nginx/html/index.html
COPY partner.html /usr/share/nginx/html/partner.html
COPY static/ /usr/share/nginx/html/static/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

266
tkpurchase/web/index.html Normal file
View File

@@ -0,0 +1,266 @@
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>방문 관리 - TK 구매관리</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet" href="/static/css/tkpurchase.css?v=20260312">
</head>
<body>
<!-- Header -->
<header class="bg-emerald-700 text-white sticky top-0 z-50">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between items-center h-14">
<div class="flex items-center gap-3">
<i class="fas fa-truck text-xl text-emerald-200"></i>
<h1 class="text-lg font-semibold">TK 구매관리</h1>
</div>
<div class="flex items-center gap-4">
<div id="headerUserName" class="text-sm font-medium hidden sm:block">-</div>
<div id="headerUserAvatar" class="w-8 h-8 bg-emerald-600 rounded-full flex items-center justify-center text-sm font-semibold">-</div>
<button onclick="doLogout()" class="text-emerald-200 hover:text-white" title="로그아웃"><i class="fas fa-sign-out-alt"></i></button>
</div>
</div>
</div>
</header>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4 fade-in">
<div class="flex gap-6">
<!-- Sidebar Nav -->
<nav id="sideNav" class="hidden lg:flex flex-col gap-1 w-48 flex-shrink-0 pt-2"></nav>
<!-- Main -->
<div class="flex-1 min-w-0">
<!-- 통계 카드 -->
<div class="grid grid-cols-2 sm:grid-cols-4 gap-3 mb-5">
<div class="stat-card">
<div class="stat-value text-emerald-600" id="statTotal">0</div>
<div class="stat-label">오늘 방문</div>
</div>
<div class="stat-card">
<div class="stat-value text-blue-600" id="statCheckedIn">0</div>
<div class="stat-label">체크인 중</div>
</div>
<div class="stat-card">
<div class="stat-value text-gray-600" id="statCheckedOut">0</div>
<div class="stat-label">체크아웃</div>
</div>
<div class="stat-card">
<div class="stat-value text-purple-600" id="statVisitors">0</div>
<div class="stat-label">총 인원</div>
</div>
</div>
<!-- 빠른 등록 폼 -->
<div class="bg-white rounded-xl shadow-sm p-5 mb-5">
<h2 class="text-base font-semibold text-gray-800 mb-4"><i class="fas fa-plus-circle text-emerald-500 mr-2"></i>방문 등록</h2>
<form id="visitForm">
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-3">
<!-- 업체 -->
<div class="sm:col-span-2 relative">
<label class="block text-xs font-medium text-gray-600 mb-1">업체</label>
<div class="flex items-center gap-2">
<div class="flex-1 relative">
<input type="text" id="companySearch" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="업체명 검색...">
<div id="companyDropdown" class="hidden absolute z-10 w-full mt-1 bg-white border rounded-lg shadow-lg max-h-48 overflow-y-auto"></div>
</div>
<div class="hidden flex-1">
<input type="text" id="manualCompanyName" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="업체명 직접입력">
</div>
<label class="flex items-center gap-1 text-xs text-gray-500 whitespace-nowrap cursor-pointer">
<input type="checkbox" id="manualCompanyToggle" class="rounded">
<span>직접입력</span>
</label>
</div>
</div>
<!-- 방문자명 -->
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">방문자명 <span class="text-red-400">*</span></label>
<input type="text" id="visitorName" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="대표 방문자" required>
</div>
<!-- 인원 -->
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">인원</label>
<div class="flex items-center gap-1">
<button type="button" id="countMinus" class="w-9 h-9 flex items-center justify-center border rounded-lg hover:bg-gray-50 text-gray-600"><i class="fas fa-minus text-xs"></i></button>
<input type="number" id="visitorCount" value="1" min="1" class="input-field w-14 text-center px-1 py-2 rounded-lg text-sm">
<button type="button" id="countPlus" class="w-9 h-9 flex items-center justify-center border rounded-lg hover:bg-gray-50 text-gray-600"><i class="fas fa-plus text-xs"></i></button>
</div>
</div>
<!-- 목적 -->
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">방문 목적 <span class="text-red-400">*</span></label>
<select id="visitPurpose" class="input-field w-full px-3 py-2 rounded-lg text-sm" required>
<option value="">선택</option>
<option value="day_labor">일용공</option>
<option value="equipment_repair">설비수리</option>
<option value="inspection">검사</option>
<option value="delivery">납품/배송</option>
<option value="safety_audit">안전점검</option>
<option value="client_audit">고객심사</option>
<option value="construction">공사</option>
<option value="other">기타</option>
</select>
</div>
<!-- 작업장 -->
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">작업장</label>
<input type="text" id="workplaceName" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="작업장소">
</div>
<!-- 안전교육 -->
<div class="flex items-end pb-1">
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" id="safetyCheck" class="h-5 w-5 text-emerald-500 rounded border-gray-300">
<span class="text-sm text-gray-700">안전교육 이수</span>
</label>
</div>
<!-- 목적 상세 -->
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">목적 상세</label>
<input type="text" id="purposeDetail" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="상세 내용">
</div>
</div>
<!-- 추가 정보 (접이식) -->
<div class="mt-3">
<button type="button" onclick="toggleExtra()" class="text-sm text-gray-500 hover:text-gray-700 flex items-center gap-1">
<i id="extraToggleIcon" class="fas fa-chevron-down text-xs"></i>추가 정보
</button>
<div id="extraFields" class="collapsible-content">
<div class="grid grid-cols-1 sm:grid-cols-3 gap-3 mt-2">
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">차량번호</label>
<input type="text" id="vehicleNumber" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="12가 3456">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">담당부서</label>
<select id="managingDept" class="input-field w-full px-3 py-2 rounded-lg text-sm">
<option value="">선택</option>
<option value="생산">생산</option>
<option value="품질">품질</option>
<option value="구매">구매</option>
<option value="설계">설계</option>
<option value="영업">영업</option>
</select>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">비고</label>
<input type="text" id="visitNotes" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="메모">
</div>
</div>
</div>
</div>
<div class="flex justify-end mt-4">
<button type="submit" class="px-6 py-2.5 bg-emerald-600 text-white rounded-lg hover:bg-emerald-700 text-sm font-medium">
<i class="fas fa-check mr-2"></i>등록
</button>
</div>
</form>
</div>
<!-- 오늘 방문 현황 -->
<div class="bg-white rounded-xl shadow-sm p-5">
<div class="flex items-center justify-between mb-4">
<h2 class="text-base font-semibold text-gray-800"><i class="fas fa-list text-emerald-500 mr-2"></i>오늘 방문 현황</h2>
<div class="flex gap-2">
<button onclick="exportVisits()" class="text-xs text-gray-500 hover:text-gray-700 border px-3 py-1.5 rounded-lg hover:bg-gray-50">
<i class="fas fa-download mr-1"></i>CSV
</button>
<button onclick="doBulkCheckout()" class="text-xs text-blue-600 hover:text-blue-800 border border-blue-200 px-3 py-1.5 rounded-lg hover:bg-blue-50">
<i class="fas fa-check-double mr-1"></i>전체 마감
</button>
</div>
</div>
<div class="overflow-x-auto">
<table class="visit-table">
<thead>
<tr>
<th>업체</th>
<th>방문자</th>
<th class="text-center">인원</th>
<th>목적</th>
<th class="hide-mobile">안전교육</th>
<th>체크인</th>
<th>상태</th>
<th class="text-right">관리</th>
</tr>
</thead>
<tbody id="visitTableBody">
<tr><td colspan="8" class="text-center text-gray-400 py-8">로딩 중...</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- 수정 모달 -->
<div id="editVisitModal" class="hidden modal-overlay" onclick="if(event.target===this)closeEditVisit()">
<div class="modal-content p-6">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold">방문 수정</h3>
<button onclick="closeEditVisit()" class="text-gray-400 hover:text-gray-600"><i class="fas fa-times"></i></button>
</div>
<form id="editVisitForm">
<div class="grid grid-cols-2 gap-3">
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">방문자명</label>
<input type="text" id="editVisitorName" class="input-field w-full px-3 py-2 rounded-lg text-sm" required>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">인원</label>
<input type="number" id="editVisitorCount" min="1" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">목적</label>
<select id="editPurpose" class="input-field w-full px-3 py-2 rounded-lg text-sm">
<option value="day_labor">일용공</option>
<option value="equipment_repair">설비수리</option>
<option value="inspection">검사</option>
<option value="delivery">납품/배송</option>
<option value="safety_audit">안전점검</option>
<option value="client_audit">고객심사</option>
<option value="construction">공사</option>
<option value="other">기타</option>
</select>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">목적 상세</label>
<input type="text" id="editPurposeDetail" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">작업장</label>
<input type="text" id="editWorkplace" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">차량번호</label>
<input type="text" id="editVehicle" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div class="flex items-end pb-1">
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" id="editSafetyCheck" class="h-5 w-5 text-emerald-500 rounded">
<span class="text-sm">안전교육 이수</span>
</label>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">비고</label>
<input type="text" id="editNotes" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
</div>
<div class="flex justify-end mt-4 gap-2">
<button type="button" onclick="closeEditVisit()" class="px-4 py-2 border rounded-lg text-sm hover:bg-gray-50">취소</button>
<button type="submit" class="px-4 py-2 bg-emerald-600 text-white rounded-lg text-sm hover:bg-emerald-700">저장</button>
</div>
</form>
</div>
</div>
<script src="/static/js/tkpurchase-core.js?v=20260312"></script>
<script src="/static/js/tkpurchase-visit.js?v=20260312"></script>
<script>initVisitPage();</script>
</body>
</html>

45
tkpurchase/web/nginx.conf Normal file
View File

@@ -0,0 +1,45 @@
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
gzip on;
gzip_types text/plain text/css application/javascript application/json;
gzip_min_length 1024;
location ~* \.html$ {
expires -1;
add_header Cache-Control "no-store, no-cache, must-revalidate";
}
location ~* \.(js|css)$ {
expires 1h;
add_header Cache-Control "public, no-transform";
}
location /api/ {
proxy_pass http://tkpurchase-api:3000/api/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /static/ {
expires 1h;
add_header Cache-Control "public, no-transform";
}
location / {
try_files $uri $uri/ /index.html;
}
location /health {
access_log off;
return 200 'ok';
add_header Content-Type text/plain;
}
}

298
tkpurchase/web/partner.html Normal file
View File

@@ -0,0 +1,298 @@
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>협력업체 관리 - TK 구매관리</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet" href="/static/css/tkpurchase.css?v=20260312">
</head>
<body>
<!-- Header -->
<header class="bg-emerald-700 text-white sticky top-0 z-50">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between items-center h-14">
<div class="flex items-center gap-3">
<i class="fas fa-truck text-xl text-emerald-200"></i>
<h1 class="text-lg font-semibold">TK 구매관리</h1>
</div>
<div class="flex items-center gap-4">
<div id="headerUserName" class="text-sm font-medium hidden sm:block">-</div>
<div id="headerUserAvatar" class="w-8 h-8 bg-emerald-600 rounded-full flex items-center justify-center text-sm font-semibold">-</div>
<button onclick="doLogout()" class="text-emerald-200 hover:text-white" title="로그아웃"><i class="fas fa-sign-out-alt"></i></button>
</div>
</div>
</div>
</header>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4 fade-in">
<div class="flex gap-6">
<!-- Sidebar Nav -->
<nav id="sideNav" class="hidden lg:flex flex-col gap-1 w-48 flex-shrink-0 pt-2"></nav>
<!-- Main -->
<div class="flex-1 min-w-0">
<div class="grid lg:grid-cols-5 gap-6">
<!-- 업체 목록 (좌측) -->
<div class="lg:col-span-2">
<div class="bg-white rounded-xl shadow-sm p-5">
<div class="flex items-center justify-between mb-4">
<h2 class="text-base font-semibold text-gray-800"><i class="fas fa-building text-emerald-500 mr-2"></i>협력업체</h2>
<button onclick="openAddPartner()" class="px-3 py-1.5 bg-emerald-600 text-white rounded-lg text-xs hover:bg-emerald-700">
<i class="fas fa-plus mr-1"></i>업체 등록
</button>
</div>
<!-- 필터 -->
<div class="flex gap-2 mb-3">
<input type="text" id="partnerSearch" class="input-field flex-1 px-3 py-1.5 rounded-lg text-sm" placeholder="업체명/사업자번호 검색">
<select id="partnerFilterActive" class="input-field px-2 py-1.5 rounded-lg text-sm">
<option value="true">활성</option>
<option value="">전체</option>
<option value="false">비활성</option>
</select>
</div>
<div id="partnerList" class="space-y-2 max-h-[70vh] overflow-y-auto">
<div class="text-gray-400 text-center py-8 text-sm">로딩 중...</div>
</div>
</div>
</div>
<!-- 업체 상세 (우측) -->
<div class="lg:col-span-3">
<div id="partnerDetail" class="hidden">
<div class="bg-white rounded-xl shadow-sm p-5 mb-4">
<h3 id="detailCompanyName" class="text-lg font-semibold text-gray-800 mb-3"></h3>
<div id="detailInfo"></div>
</div>
<!-- 작업자 목록 -->
<div class="bg-white rounded-xl shadow-sm p-5">
<div class="flex items-center justify-between mb-3">
<h3 class="text-base font-semibold text-gray-800"><i class="fas fa-users text-emerald-500 mr-2"></i>소속 작업자</h3>
<button onclick="openAddWorker()" class="px-3 py-1.5 bg-emerald-600 text-white rounded-lg text-xs hover:bg-emerald-700">
<i class="fas fa-user-plus mr-1"></i>작업자 등록
</button>
</div>
<div id="workerList" class="space-y-2"></div>
</div>
</div>
<div id="partnerEmpty" class="text-center text-gray-400 py-16">
<i class="fas fa-building text-4xl mb-3"></i>
<p>업체를 선택하면 상세 정보를 볼 수 있습니다</p>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- 업체 등록 모달 -->
<div id="addPartnerModal" class="hidden modal-overlay" onclick="if(event.target===this)closeAddPartner()">
<div class="modal-content p-6">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold">협력업체 등록</h3>
<button onclick="closeAddPartner()" class="text-gray-400 hover:text-gray-600"><i class="fas fa-times"></i></button>
</div>
<form id="addPartnerForm">
<div class="grid grid-cols-2 gap-3">
<div class="col-span-2">
<label class="block text-xs font-medium text-gray-600 mb-1">업체명 <span class="text-red-400">*</span></label>
<input type="text" id="newCompanyName" class="input-field w-full px-3 py-2 rounded-lg text-sm" required>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">사업자번호</label>
<input type="text" id="newBusinessNumber" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="000-00-00000">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">대표자</label>
<input type="text" id="newRepresentative" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">담당자명</label>
<input type="text" id="newContactName" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">담당자 연락처</label>
<input type="text" id="newContactPhone" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div class="col-span-2">
<label class="block text-xs font-medium text-gray-600 mb-1">주소</label>
<input type="text" id="newAddress" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">업종 (콤마 구분)</label>
<input type="text" id="newBusinessType" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="배관, 용접">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">산재보험 관리번호</label>
<input type="text" id="newInsuranceNumber" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">산재보험 만료일</label>
<input type="date" id="newInsuranceExpiry" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">비고</label>
<input type="text" id="newPartnerNotes" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
</div>
<div class="flex justify-end mt-4 gap-2">
<button type="button" onclick="closeAddPartner()" class="px-4 py-2 border rounded-lg text-sm hover:bg-gray-50">취소</button>
<button type="submit" class="px-4 py-2 bg-emerald-600 text-white rounded-lg text-sm hover:bg-emerald-700">등록</button>
</div>
</form>
</div>
</div>
<!-- 업체 수정 모달 -->
<div id="editPartnerModal" class="hidden modal-overlay" onclick="if(event.target===this)closeEditPartner()">
<div class="modal-content p-6">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold">협력업체 수정</h3>
<button onclick="closeEditPartner()" class="text-gray-400 hover:text-gray-600"><i class="fas fa-times"></i></button>
</div>
<form id="editPartnerForm">
<input type="hidden" id="editPartnerId">
<div class="grid grid-cols-2 gap-3">
<div class="col-span-2">
<label class="block text-xs font-medium text-gray-600 mb-1">업체명 <span class="text-red-400">*</span></label>
<input type="text" id="editCompanyName" class="input-field w-full px-3 py-2 rounded-lg text-sm" required>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">사업자번호</label>
<input type="text" id="editBusinessNumber" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">대표자</label>
<input type="text" id="editRepresentative" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">담당자명</label>
<input type="text" id="editContactName" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">담당자 연락처</label>
<input type="text" id="editContactPhone" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div class="col-span-2">
<label class="block text-xs font-medium text-gray-600 mb-1">주소</label>
<input type="text" id="editAddress" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">업종 (콤마 구분)</label>
<input type="text" id="editBusinessType" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">산재보험 관리번호</label>
<input type="text" id="editInsuranceNumber" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">산재보험 만료일</label>
<input type="date" id="editInsuranceExpiry" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">비고</label>
<input type="text" id="editPartnerNotes" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
</div>
<div class="flex justify-end mt-4 gap-2">
<button type="button" onclick="closeEditPartner()" class="px-4 py-2 border rounded-lg text-sm hover:bg-gray-50">취소</button>
<button type="submit" class="px-4 py-2 bg-emerald-600 text-white rounded-lg text-sm hover:bg-emerald-700">저장</button>
</div>
</form>
</div>
</div>
<!-- 작업자 등록 모달 -->
<div id="addWorkerModal" class="hidden modal-overlay" onclick="if(event.target===this)closeAddWorker()">
<div class="modal-content p-6">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold">작업자 등록</h3>
<button onclick="closeAddWorker()" class="text-gray-400 hover:text-gray-600"><i class="fas fa-times"></i></button>
</div>
<form id="addWorkerForm">
<div class="grid grid-cols-2 gap-3">
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">성명 <span class="text-red-400">*</span></label>
<input type="text" id="newWorkerName" class="input-field w-full px-3 py-2 rounded-lg text-sm" required>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">직위</label>
<input type="text" id="newWorkerPosition" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div class="flex items-end pb-1">
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" id="newWorkerIsLeader" class="h-4 w-4 text-emerald-500 rounded">
<span class="text-sm">팀장급</span>
</label>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">연락처</label>
<input type="text" id="newWorkerPhone" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="팀장급 필수">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">안전교육 이수일</label>
<input type="date" id="newWorkerSafetyDate" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">비고</label>
<input type="text" id="newWorkerNotes" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
</div>
<div class="flex justify-end mt-4 gap-2">
<button type="button" onclick="closeAddWorker()" class="px-4 py-2 border rounded-lg text-sm hover:bg-gray-50">취소</button>
<button type="submit" class="px-4 py-2 bg-emerald-600 text-white rounded-lg text-sm hover:bg-emerald-700">등록</button>
</div>
</form>
</div>
</div>
<!-- 작업자 수정 모달 -->
<div id="editWorkerModal" class="hidden modal-overlay" onclick="if(event.target===this)closeEditWorker()">
<div class="modal-content p-6">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold">작업자 수정</h3>
<button onclick="closeEditWorker()" class="text-gray-400 hover:text-gray-600"><i class="fas fa-times"></i></button>
</div>
<form id="editWorkerForm">
<div class="grid grid-cols-2 gap-3">
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">성명</label>
<input type="text" id="editWorkerName" class="input-field w-full px-3 py-2 rounded-lg text-sm" required>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">직위</label>
<input type="text" id="editWorkerPosition" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div class="flex items-end pb-1">
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" id="editWorkerIsLeader" class="h-4 w-4 text-emerald-500 rounded">
<span class="text-sm">팀장급</span>
</label>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">연락처</label>
<input type="text" id="editWorkerPhone" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">안전교육 이수일</label>
<input type="date" id="editWorkerSafetyDate" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">비고</label>
<input type="text" id="editWorkerNotes" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
</div>
<div class="flex justify-end mt-4 gap-2">
<button type="button" onclick="closeEditWorker()" class="px-4 py-2 border rounded-lg text-sm hover:bg-gray-50">취소</button>
<button type="submit" class="px-4 py-2 bg-emerald-600 text-white rounded-lg text-sm hover:bg-emerald-700">저장</button>
</div>
</form>
</div>
</div>
<script src="/static/js/tkpurchase-core.js?v=20260312"></script>
<script src="/static/js/tkpurchase-partner.js?v=20260312"></script>
<script>initPartnerPage();</script>
</body>
</html>

View File

@@ -0,0 +1,63 @@
/* tkpurchase global styles */
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: #f8fafc; margin: 0; }
.fade-in { opacity: 0; transition: opacity 0.3s; }
.fade-in.visible { opacity: 1; }
/* Input */
.input-field { border: 1px solid #e2e8f0; transition: border-color 0.15s; outline: none; }
.input-field:focus { border-color: #10b981; box-shadow: 0 0 0 3px rgba(16,185,129,0.1); }
/* Toast */
.toast-message { transition: opacity 0.3s; }
/* Nav active */
.nav-link.active { background: rgba(16,185,129,0.15); color: #059669; font-weight: 600; }
/* Stat card */
.stat-card { background: white; border-radius: 0.75rem; padding: 1.25rem; box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
.stat-card .stat-value { font-size: 1.75rem; font-weight: 700; line-height: 1.2; }
.stat-card .stat-label { font-size: 0.8rem; color: #6b7280; margin-top: 0.25rem; }
/* Table */
.visit-table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
.visit-table th { background: #f1f5f9; padding: 0.625rem 0.75rem; text-align: left; font-weight: 600; color: #475569; white-space: nowrap; border-bottom: 2px solid #e2e8f0; }
.visit-table td { padding: 0.625rem 0.75rem; border-bottom: 1px solid #f1f5f9; vertical-align: middle; }
.visit-table tr:hover { background: #f8fafc; }
/* Badge */
.badge { display: inline-flex; align-items: center; padding: 0.125rem 0.5rem; border-radius: 9999px; font-size: 0.75rem; font-weight: 500; }
.badge-green { background: #ecfdf5; color: #059669; }
.badge-blue { background: #eff6ff; color: #2563eb; }
.badge-amber { background: #fffbeb; color: #d97706; }
.badge-red { background: #fef2f2; color: #dc2626; }
.badge-gray { background: #f3f4f6; color: #6b7280; }
/* Purpose badges */
.purpose-day_labor { background: #dbeafe; color: #1d4ed8; }
.purpose-equipment_repair { background: #fef3c7; color: #92400e; }
.purpose-inspection { background: #ede9fe; color: #6d28d9; }
.purpose-delivery { background: #d1fae5; color: #065f46; }
.purpose-safety_audit { background: #fee2e2; color: #991b1b; }
.purpose-client_audit { background: #fce7f3; color: #9d174d; }
.purpose-construction { background: #e0e7ff; color: #3730a3; }
.purpose-other { background: #f3f4f6; color: #374151; }
/* Collapsible */
.collapsible-content { max-height: 0; overflow: hidden; transition: max-height 0.3s ease; }
.collapsible-content.open { max-height: 500px; }
/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.4); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 1rem; }
.modal-content { background: white; border-radius: 0.75rem; max-width: 40rem; width: 100%; max-height: 90vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,0.2); }
/* Safety warning */
.safety-warning { animation: pulse 2s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }
/* Responsive */
@media (max-width: 768px) {
.stat-card .stat-value { font-size: 1.25rem; }
.visit-table { font-size: 0.8rem; }
.visit-table th, .visit-table td { padding: 0.5rem; }
.hide-mobile { display: none; }
}

View 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 { return JSON.parse(atob(t.split('.')[1].replace(/-/g,'+').replace(/_/g,'/'))); } 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('[tkpurchase] 리다이렉트 루프 감지'); 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-emerald-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: '/partner.html', icon: 'fa-building', label: '협력업체', match: ['partner.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;
}

View File

@@ -0,0 +1,298 @@
/* ===== Partner Management ===== */
let partners = [];
let partnerWorkers = [];
let selectedPartnerId = null;
let editingWorkerId = null;
async function loadPartners() {
try {
const isActive = document.getElementById('partnerFilterActive')?.value;
const search = document.getElementById('partnerSearch')?.value?.trim() || '';
const params = new URLSearchParams();
if (isActive !== '' && isActive !== undefined) params.set('is_active', isActive);
if (search) params.set('search', search);
const r = await api('/partners/?' + params.toString());
partners = r.data || [];
renderPartnerList();
} catch (e) {
showToast('업체 목록 로드 실패: ' + e.message, 'error');
}
}
function renderPartnerList() {
const c = document.getElementById('partnerList');
if (!partners.length) {
c.innerHTML = '<div class="text-gray-400 text-center py-8 text-sm">등록된 협력업체가 없습니다</div>';
return;
}
c.innerHTML = partners.map(p => {
const types = tryParseJson(p.business_type) || [];
const typeStr = types.length ? types.map(t => `<span class="badge badge-blue text-xs">${escapeHtml(t)}</span>`).join(' ') : '';
const insuranceWarning = isInsuranceExpiringSoon(p.insurance_expiry);
return `<div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg hover:bg-gray-100 transition-colors cursor-pointer ${selectedPartnerId === p.id ? 'ring-2 ring-emerald-400' : ''}" onclick="selectPartner(${p.id})">
<div class="flex-1 min-w-0">
<div class="text-sm font-medium text-gray-800 flex items-center gap-2">
<i class="fas fa-building text-gray-400 text-xs"></i>
${escapeHtml(p.company_name)}
${!p.is_active ? '<span class="badge badge-gray">비활성</span>' : ''}
${insuranceWarning ? '<span class="badge badge-red"><i class="fas fa-exclamation-triangle mr-1"></i>보험만료</span>' : ''}
</div>
<div class="text-xs text-gray-500 mt-0.5 flex items-center gap-2 flex-wrap">
${p.business_number ? `<span>${p.business_number}</span>` : ''}
${p.representative ? `<span>${escapeHtml(p.representative)}</span>` : ''}
${typeStr}
</div>
</div>
<div class="flex gap-1 ml-2">
<button onclick="event.stopPropagation(); openEditPartner(${p.id})" class="p-1.5 text-slate-500 hover:text-slate-700 hover:bg-slate-200 rounded" title="수정"><i class="fas fa-pen text-xs"></i></button>
${p.is_active ? `<button onclick="event.stopPropagation(); deactivatePartner(${p.id}, '${escapeHtml(p.company_name).replace(/'/g, "\\'")}')" class="p-1.5 text-red-400 hover:text-red-600 hover:bg-red-100 rounded" title="비활성화"><i class="fas fa-ban text-xs"></i></button>` : ''}
</div>
</div>`;
}).join('');
}
function isInsuranceExpiringSoon(expiry) {
if (!expiry) return false;
const exp = new Date(expiry);
const now = new Date();
const diff = (exp - now) / (1000 * 60 * 60 * 24);
return diff <= 30 && diff >= 0;
}
function tryParseJson(val) {
if (!val) return null;
if (Array.isArray(val)) return val;
try { return JSON.parse(val); } catch { return null; }
}
/* ===== 업체 상세 + 작업자 ===== */
async function selectPartner(id) {
selectedPartnerId = id;
renderPartnerList(); // 하이라이트 갱신
try {
const r = await api(`/partners/${id}`);
const p = r.data;
partnerWorkers = p.workers || [];
renderPartnerDetail(p);
document.getElementById('partnerDetail').classList.remove('hidden');
document.getElementById('partnerEmpty').classList.add('hidden');
} catch (e) {
showToast('상세 조회 실패: ' + e.message, 'error');
}
}
function renderPartnerDetail(p) {
const types = tryParseJson(p.business_type) || [];
document.getElementById('detailCompanyName').textContent = p.company_name;
document.getElementById('detailInfo').innerHTML = `
<div class="grid grid-cols-2 gap-3 text-sm">
<div><span class="text-gray-500">사업자번호:</span> <span class="font-medium">${escapeHtml(p.business_number) || '-'}</span></div>
<div><span class="text-gray-500">대표자:</span> <span class="font-medium">${escapeHtml(p.representative) || '-'}</span></div>
<div><span class="text-gray-500">담당자:</span> <span class="font-medium">${escapeHtml(p.contact_name) || '-'}</span></div>
<div><span class="text-gray-500">연락처:</span> <span class="font-medium">${escapeHtml(p.contact_phone) || '-'}</span></div>
<div class="col-span-2"><span class="text-gray-500">주소:</span> <span class="font-medium">${escapeHtml(p.address) || '-'}</span></div>
<div><span class="text-gray-500">업종:</span> ${types.map(t => `<span class="badge badge-blue">${escapeHtml(t)}</span>`).join(' ') || '-'}</div>
<div><span class="text-gray-500">산재보험:</span> <span class="font-medium">${escapeHtml(p.insurance_number) || '-'}</span> ${p.insurance_expiry ? `(만료: ${formatDate(p.insurance_expiry)})` : ''}</div>
${p.notes ? `<div class="col-span-2"><span class="text-gray-500">비고:</span> ${escapeHtml(p.notes)}</div>` : ''}
</div>`;
renderWorkerList();
}
function renderWorkerList() {
const c = document.getElementById('workerList');
if (!partnerWorkers.length) {
c.innerHTML = '<div class="text-gray-400 text-center py-4 text-sm">등록된 작업자가 없습니다</div>';
return;
}
c.innerHTML = partnerWorkers.map(w => `
<div class="flex items-center justify-between p-2 bg-gray-50 rounded hover:bg-gray-100">
<div class="flex-1 min-w-0">
<div class="text-sm font-medium">${escapeHtml(w.worker_name)}
${w.is_team_leader ? '<span class="badge badge-amber ml-1">팀장</span>' : ''}
${!w.is_active ? '<span class="badge badge-gray ml-1">비활성</span>' : ''}
</div>
<div class="text-xs text-gray-500 flex gap-2 mt-0.5">
${w.position ? `<span>${escapeHtml(w.position)}</span>` : ''}
${w.phone ? `<span>${escapeHtml(w.phone)}</span>` : ''}
${w.safety_training_date ? `<span>안전교육: ${formatDate(w.safety_training_date)}</span>` : ''}
</div>
</div>
<div class="flex gap-1">
<button onclick="openEditWorker(${w.id})" class="p-1 text-slate-500 hover:text-slate-700 rounded" title="수정"><i class="fas fa-pen text-xs"></i></button>
${w.is_active ? `<button onclick="doDeactivateWorker(${w.id})" class="p-1 text-red-400 hover:text-red-600 rounded" title="비활성화"><i class="fas fa-ban text-xs"></i></button>` : ''}
</div>
</div>`).join('');
}
/* ===== 업체 등록 ===== */
function openAddPartner() { document.getElementById('addPartnerModal').classList.remove('hidden'); }
function closeAddPartner() { document.getElementById('addPartnerModal').classList.add('hidden'); document.getElementById('addPartnerForm').reset(); }
async function submitAddPartner(e) {
e.preventDefault();
const typesRaw = document.getElementById('newBusinessType').value.trim();
const data = {
company_name: document.getElementById('newCompanyName').value.trim(),
business_number: document.getElementById('newBusinessNumber').value.trim() || null,
representative: document.getElementById('newRepresentative').value.trim() || null,
contact_name: document.getElementById('newContactName').value.trim() || null,
contact_phone: document.getElementById('newContactPhone').value.trim() || null,
address: document.getElementById('newAddress').value.trim() || null,
business_type: typesRaw ? typesRaw.split(',').map(s => s.trim()).filter(Boolean) : null,
insurance_number: document.getElementById('newInsuranceNumber').value.trim() || null,
insurance_expiry: document.getElementById('newInsuranceExpiry').value || null,
notes: document.getElementById('newPartnerNotes').value.trim() || null,
};
if (!data.company_name) { showToast('업체명은 필수입니다', 'error'); return; }
try {
await api('/partners/', { method: 'POST', body: JSON.stringify(data) });
showToast('업체가 등록되었습니다');
closeAddPartner();
await loadPartners();
} catch (e) { showToast(e.message, 'error'); }
}
/* ===== 업체 수정 ===== */
function openEditPartner(id) {
const p = partners.find(x => x.id === id);
if (!p) return;
const types = tryParseJson(p.business_type) || [];
document.getElementById('editPartnerId').value = p.id;
document.getElementById('editCompanyName').value = p.company_name;
document.getElementById('editBusinessNumber').value = p.business_number || '';
document.getElementById('editRepresentative').value = p.representative || '';
document.getElementById('editContactName').value = p.contact_name || '';
document.getElementById('editContactPhone').value = p.contact_phone || '';
document.getElementById('editAddress').value = p.address || '';
document.getElementById('editBusinessType').value = types.join(', ');
document.getElementById('editInsuranceNumber').value = p.insurance_number || '';
document.getElementById('editInsuranceExpiry').value = p.insurance_expiry ? formatDate(p.insurance_expiry) : '';
document.getElementById('editPartnerNotes').value = p.notes || '';
document.getElementById('editPartnerModal').classList.remove('hidden');
}
function closeEditPartner() { document.getElementById('editPartnerModal').classList.add('hidden'); }
async function submitEditPartner(e) {
e.preventDefault();
const id = document.getElementById('editPartnerId').value;
const typesRaw = document.getElementById('editBusinessType').value.trim();
const data = {
company_name: document.getElementById('editCompanyName').value.trim(),
business_number: document.getElementById('editBusinessNumber').value.trim() || null,
representative: document.getElementById('editRepresentative').value.trim() || null,
contact_name: document.getElementById('editContactName').value.trim() || null,
contact_phone: document.getElementById('editContactPhone').value.trim() || null,
address: document.getElementById('editAddress').value.trim() || null,
business_type: typesRaw ? typesRaw.split(',').map(s => s.trim()).filter(Boolean) : null,
insurance_number: document.getElementById('editInsuranceNumber').value.trim() || null,
insurance_expiry: document.getElementById('editInsuranceExpiry').value || null,
notes: document.getElementById('editPartnerNotes').value.trim() || null,
};
try {
await api(`/partners/${id}`, { method: 'PUT', body: JSON.stringify(data) });
showToast('수정되었습니다');
closeEditPartner();
await loadPartners();
if (selectedPartnerId == id) selectPartner(id);
} catch (e) { showToast(e.message, 'error'); }
}
/* ===== 업체 비활성화 ===== */
async function deactivatePartner(id, name) {
if (!confirm(`"${name}" 업체를 비활성화하시겠습니까?`)) return;
try {
await api(`/partners/${id}`, { method: 'DELETE' });
showToast('비활성화 완료');
await loadPartners();
if (selectedPartnerId === id) {
document.getElementById('partnerDetail').classList.add('hidden');
document.getElementById('partnerEmpty').classList.remove('hidden');
selectedPartnerId = null;
}
} catch (e) { showToast(e.message, 'error'); }
}
/* ===== 작업자 등록 ===== */
function openAddWorker() {
if (!selectedPartnerId) { showToast('업체를 먼저 선택해주세요', 'error'); return; }
document.getElementById('addWorkerModal').classList.remove('hidden');
}
function closeAddWorker() { document.getElementById('addWorkerModal').classList.add('hidden'); document.getElementById('addWorkerForm').reset(); }
async function submitAddWorker(e) {
e.preventDefault();
const data = {
worker_name: document.getElementById('newWorkerName').value.trim(),
position: document.getElementById('newWorkerPosition').value.trim() || null,
is_team_leader: document.getElementById('newWorkerIsLeader').checked,
phone: document.getElementById('newWorkerPhone').value.trim() || null,
safety_training_date: document.getElementById('newWorkerSafetyDate').value || null,
notes: document.getElementById('newWorkerNotes').value.trim() || null,
};
if (!data.worker_name) { showToast('작업자명은 필수입니다', 'error'); return; }
try {
await api(`/partners/${selectedPartnerId}/workers`, { method: 'POST', body: JSON.stringify(data) });
showToast('작업자가 등록되었습니다');
closeAddWorker();
await selectPartner(selectedPartnerId);
} catch (e) { showToast(e.message, 'error'); }
}
/* ===== 작업자 수정 ===== */
function openEditWorker(id) {
const w = partnerWorkers.find(x => x.id === id);
if (!w) return;
editingWorkerId = id;
document.getElementById('editWorkerName').value = w.worker_name;
document.getElementById('editWorkerPosition').value = w.position || '';
document.getElementById('editWorkerIsLeader').checked = w.is_team_leader;
document.getElementById('editWorkerPhone').value = w.phone || '';
document.getElementById('editWorkerSafetyDate').value = w.safety_training_date ? formatDate(w.safety_training_date) : '';
document.getElementById('editWorkerNotes').value = w.notes || '';
document.getElementById('editWorkerModal').classList.remove('hidden');
}
function closeEditWorker() { document.getElementById('editWorkerModal').classList.add('hidden'); editingWorkerId = null; }
async function submitEditWorker(e) {
e.preventDefault();
if (!editingWorkerId) return;
const data = {
worker_name: document.getElementById('editWorkerName').value.trim(),
position: document.getElementById('editWorkerPosition').value.trim() || null,
is_team_leader: document.getElementById('editWorkerIsLeader').checked,
phone: document.getElementById('editWorkerPhone').value.trim() || null,
safety_training_date: document.getElementById('editWorkerSafetyDate').value || null,
notes: document.getElementById('editWorkerNotes').value.trim() || null,
};
try {
await api(`/partners/workers/${editingWorkerId}`, { method: 'PUT', body: JSON.stringify(data) });
showToast('수정되었습니다');
closeEditWorker();
await selectPartner(selectedPartnerId);
} catch (e) { showToast(e.message, 'error'); }
}
async function doDeactivateWorker(id) {
if (!confirm('이 작업자를 비활성화하시겠습니까?')) return;
try {
await api(`/partners/workers/${id}`, { method: 'DELETE' });
showToast('비활성화 완료');
await selectPartner(selectedPartnerId);
} catch (e) { showToast(e.message, 'error'); }
}
/* ===== Init ===== */
function initPartnerPage() {
if (!initAuth()) return;
document.getElementById('addPartnerForm').addEventListener('submit', submitAddPartner);
document.getElementById('editPartnerForm').addEventListener('submit', submitEditPartner);
document.getElementById('addWorkerForm').addEventListener('submit', submitAddWorker);
document.getElementById('editWorkerForm').addEventListener('submit', submitEditWorker);
document.getElementById('partnerSearch')?.addEventListener('input', debounce(loadPartners, 300));
document.getElementById('partnerFilterActive')?.addEventListener('change', loadPartners);
loadPartners();
}
function debounce(fn, ms) {
let t; return function(...args) { clearTimeout(t); t = setTimeout(() => fn.apply(this, args), ms); };
}

View 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-emerald-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-emerald-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();
}