feat(tkpurchase): 업무현황 다건 입력 + 작업자 시간 추적 + 종합 페이지
- DB: 유니크 제약 제거, report_seq 컬럼, work_report_workers 테이블 - API: 트랜잭션 기반 다건 생성/수정, 작업자 CRUD, 요약/엑셀 엔드포인트 - 협력업체 포탈: 다건 보고 UI, 작업자+시간 입력(자동완성), 수정 기능 - 업무현황 페이지: 보고순번/작업자 상세 표시 - 종합 페이지(NEW): 업체별/프로젝트별 취합, 엑셀 추출 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const workReportModel = require('../models/workReportModel');
|
||||
const checkinModel = require('../models/checkinModel');
|
||||
const ExcelJS = require('exceljs');
|
||||
|
||||
// 작업보고 목록
|
||||
async function list(req, res) {
|
||||
@@ -58,7 +59,7 @@ async function myReports(req, res) {
|
||||
// 작업보고 등록
|
||||
async function create(req, res) {
|
||||
try {
|
||||
const { checkin_id, schedule_id, company_id, report_date } = req.body;
|
||||
const { checkin_id, schedule_id, company_id, report_date, workers } = req.body;
|
||||
|
||||
if (!report_date) {
|
||||
return res.status(400).json({ success: false, error: '보고일은 필수입니다' });
|
||||
@@ -85,7 +86,8 @@ async function create(req, res) {
|
||||
const data = {
|
||||
...req.body,
|
||||
company_id: resolvedCompanyId,
|
||||
reporter_id: req.user.user_id || req.user.id
|
||||
reporter_id: req.user.user_id || req.user.id,
|
||||
workers: workers || []
|
||||
};
|
||||
const row = await workReportModel.create(data);
|
||||
res.status(201).json({ success: true, data: row });
|
||||
@@ -98,8 +100,22 @@ async function create(req, res) {
|
||||
// 작업보고 수정
|
||||
async function update(req, res) {
|
||||
try {
|
||||
const existing = await workReportModel.findById(req.params.id);
|
||||
if (!existing) {
|
||||
return res.status(404).json({ success: false, error: '작업보고를 찾을 수 없습니다' });
|
||||
}
|
||||
|
||||
// 소유권 검증 (협력업체 포탈에서 호출 시)
|
||||
if (req.user.partner_company_id && existing.reporter_id !== (req.user.user_id || req.user.id)) {
|
||||
return res.status(403).json({ success: false, error: '본인이 작성한 보고만 수정할 수 있습니다' });
|
||||
}
|
||||
|
||||
// 확인 완료된 보고 수정 불가
|
||||
if (existing.confirmed_by) {
|
||||
return res.status(400).json({ success: false, error: '확인 완료된 보고는 수정할 수 없습니다' });
|
||||
}
|
||||
|
||||
const row = await workReportModel.update(req.params.id, req.body);
|
||||
if (!row) return res.status(404).json({ success: false, error: '작업보고를 찾을 수 없습니다' });
|
||||
res.json({ success: true, data: row });
|
||||
} catch (err) {
|
||||
console.error('WorkReport update error:', err);
|
||||
@@ -120,4 +136,120 @@ async function confirm(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { list, getById, myReports, create, update, confirm };
|
||||
// 종합 요약
|
||||
async function summary(req, res) {
|
||||
try {
|
||||
const { company_id, schedule_id, date_from, date_to } = req.query;
|
||||
|
||||
if (!date_from || !date_to) {
|
||||
return res.status(400).json({ success: false, error: '기간(date_from, date_to)은 필수입니다' });
|
||||
}
|
||||
|
||||
// 최대 3개월 검증
|
||||
const from = new Date(date_from);
|
||||
const to = new Date(date_to);
|
||||
const diffMs = to - from;
|
||||
if (diffMs < 0) {
|
||||
return res.status(400).json({ success: false, error: '시작일이 종료일보다 늦을 수 없습니다' });
|
||||
}
|
||||
if (diffMs > 92 * 24 * 60 * 60 * 1000) {
|
||||
return res.status(400).json({ success: false, error: '조회 기간은 최대 3개월입니다' });
|
||||
}
|
||||
|
||||
const rows = await workReportModel.findAllAggregated({
|
||||
company_id: company_id ? parseInt(company_id) : undefined,
|
||||
schedule_id: schedule_id ? parseInt(schedule_id) : undefined,
|
||||
date_from,
|
||||
date_to
|
||||
});
|
||||
res.json({ success: true, data: rows });
|
||||
} catch (err) {
|
||||
console.error('WorkReport summary error:', err);
|
||||
res.status(500).json({ success: false, error: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
// 엑셀 추출
|
||||
async function exportExcel(req, res) {
|
||||
try {
|
||||
const { company_id, schedule_id, date_from, date_to } = req.query;
|
||||
|
||||
if (!date_from || !date_to) {
|
||||
return res.status(400).json({ success: false, error: '기간(date_from, date_to)은 필수입니다' });
|
||||
}
|
||||
|
||||
const from = new Date(date_from);
|
||||
const to = new Date(date_to);
|
||||
const diffMs = to - from;
|
||||
if (diffMs < 0) {
|
||||
return res.status(400).json({ success: false, error: '시작일이 종료일보다 늦을 수 없습니다' });
|
||||
}
|
||||
if (diffMs > 92 * 24 * 60 * 60 * 1000) {
|
||||
return res.status(400).json({ success: false, error: '조회 기간은 최대 3개월입니다' });
|
||||
}
|
||||
|
||||
const rows = await workReportModel.exportData({
|
||||
company_id: company_id ? parseInt(company_id) : undefined,
|
||||
schedule_id: schedule_id ? parseInt(schedule_id) : undefined,
|
||||
date_from,
|
||||
date_to
|
||||
});
|
||||
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
const sheet = workbook.addWorksheet('업무현황');
|
||||
|
||||
sheet.columns = [
|
||||
{ header: '보고일', key: 'report_date', width: 12 },
|
||||
{ header: '순번', key: 'report_seq', width: 6 },
|
||||
{ header: '업체', key: 'company_name', width: 18 },
|
||||
{ header: '작업장', key: 'workplace_name', width: 15 },
|
||||
{ header: '작업내용', key: 'schedule_description', width: 20 },
|
||||
{ header: '보고내용', key: 'work_content', width: 30 },
|
||||
{ header: '진행률(%)', key: 'progress_rate', width: 10 },
|
||||
{ header: '작업자', key: 'worker_name', width: 12 },
|
||||
{ header: '투입시간', key: 'hours_worked', width: 10 },
|
||||
{ header: '이슈사항', key: 'issues', width: 25 },
|
||||
{ header: '향후계획', key: 'next_plan', width: 25 },
|
||||
{ header: '보고자', key: 'reporter_name', width: 10 },
|
||||
{ header: '확인상태', key: 'confirm_status', width: 8 },
|
||||
{ header: '확인자', key: 'confirmed_by_name', width: 10 },
|
||||
];
|
||||
|
||||
// 헤더 스타일
|
||||
sheet.getRow(1).eachCell(cell => {
|
||||
cell.fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: 'FF059669' } };
|
||||
cell.font = { color: { argb: 'FFFFFFFF' }, bold: true };
|
||||
cell.alignment = { horizontal: 'center', vertical: 'middle' };
|
||||
});
|
||||
|
||||
rows.forEach(r => {
|
||||
sheet.addRow({
|
||||
report_date: r.report_date ? String(r.report_date).substring(0, 10) : '',
|
||||
report_seq: r.report_seq,
|
||||
company_name: r.company_name || '',
|
||||
workplace_name: r.workplace_name || '',
|
||||
schedule_description: r.schedule_description || '',
|
||||
work_content: r.work_content || '',
|
||||
progress_rate: r.progress_rate || 0,
|
||||
worker_name: r.worker_name || '',
|
||||
hours_worked: r.hours_worked != null ? Number(r.hours_worked) : '',
|
||||
issues: r.issues || '',
|
||||
next_plan: r.next_plan || '',
|
||||
reporter_name: r.reporter_name || '',
|
||||
confirm_status: r.confirm_status || '',
|
||||
confirmed_by_name: r.confirmed_by_name || '',
|
||||
});
|
||||
});
|
||||
|
||||
const filename = `업무현황_${date_from}_${date_to}.xlsx`;
|
||||
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||||
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(filename)}`);
|
||||
await workbook.xlsx.write(res);
|
||||
res.end();
|
||||
} catch (err) {
|
||||
console.error('WorkReport exportExcel error:', err);
|
||||
res.status(500).json({ success: false, error: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { list, getById, myReports, create, update, confirm, summary, exportExcel };
|
||||
|
||||
Reference in New Issue
Block a user