feat(purchase): 구매 취소/반품 + 입고일 기준 월별 분석

- 상태 추가: cancelled(구매취소), returned(반품)
- API: PUT /:id/cancel, /:id/return, /:id/revert-cancel
- 데스크탑: 구매완료→취소 버튼, 입고완료→반품 버튼, 취소→되돌리기
- 분석 페이지: 구매일/입고일 기준 전환 토글, 입고일 기준 월간 분류 집계 + 입고 목록
- Settlement API: GET /received-summary, /received-list (입고일 기준)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Hyungi Ahn
2026-04-01 11:07:19 +09:00
parent 2c032bd9ea
commit 7c1369a1be
12 changed files with 352 additions and 37 deletions

View File

@@ -38,9 +38,13 @@
</div>
</div>
<!-- 월 선택 -->
<div class="flex items-center gap-3 mb-6">
<!-- 월 선택 + 기준일 전환 -->
<div class="flex items-center gap-3 mb-6 flex-wrap">
<input type="month" id="paMonth" class="px-3 py-2 border rounded-lg text-sm">
<div class="flex rounded-lg border overflow-hidden text-sm">
<button id="btnDatePurchase" onclick="setDateBasis('purchase')" class="px-3 py-2 bg-orange-600 text-white">구매일</button>
<button id="btnDateReceived" onclick="setDateBasis('received')" class="px-3 py-2 bg-white text-gray-600 hover:bg-gray-50">입고일</button>
</div>
<button onclick="loadAnalysis()" class="px-4 py-2 bg-orange-600 text-white rounded-lg text-sm hover:bg-orange-700">
<i class="fas fa-search mr-1"></i>조회
</button>
@@ -104,10 +108,34 @@
</div>
</div>
<!-- 입고 목록 (입고일 기준 모드에서만 표시) -->
<div id="paReceivedSection" class="hidden bg-white rounded-xl shadow-sm p-5 mt-6">
<h2 class="text-base font-semibold text-gray-800 mb-4"><i class="fas fa-box-open text-teal-500 mr-2"></i>월간 입고 내역</h2>
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead class="bg-gray-50 text-gray-600 text-xs uppercase">
<tr>
<th class="px-4 py-3 text-left">품목</th>
<th class="px-4 py-3 text-left">분류</th>
<th class="px-4 py-3 text-right">수량</th>
<th class="px-4 py-3 text-right">단가</th>
<th class="px-4 py-3 text-left">업체</th>
<th class="px-4 py-3 text-left">입고일</th>
<th class="px-4 py-3 text-left">보관위치</th>
<th class="px-4 py-3 text-left">상태</th>
</tr>
</thead>
<tbody id="paReceivedList" class="divide-y">
<tr><td colspan="8" class="px-4 py-8 text-center text-gray-400">-</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<script src="/static/js/tkfb-core.js?v=2026033108"></script>
<script src="/static/js/purchase-analysis.js?v=2026031602"></script>
<script src="/static/js/tkfb-core.js?v=2026040103"></script>
<script src="/static/js/purchase-analysis.js?v=2026040103"></script>
</body>
</html>

View File

@@ -117,6 +117,8 @@
<option value="grouped">구매진행중</option>
<option value="purchased">구매완료</option>
<option value="received">입고완료</option>
<option value="cancelled">취소</option>
<option value="returned">반품</option>
<option value="hold">보류</option>
</select>
<select id="prFilterCategory" class="px-3 py-2 border rounded-lg text-sm" onchange="loadRequests()">

View File

@@ -2,35 +2,76 @@
const CAT_LABELS = { consumable: '소모품', safety: '안전용품', repair: '수선비', equipment: '설비' };
const CAT_ICONS = { consumable: 'fa-box', safety: 'fa-hard-hat', repair: 'fa-wrench', equipment: 'fa-cogs' };
const CAT_BG = { consumable: 'bg-blue-50 text-blue-700', safety: 'bg-green-50 text-green-700', repair: 'bg-amber-50 text-amber-700', equipment: 'bg-purple-50 text-purple-700' };
const STATUS_LABELS = { received: '입고완료', returned: '반품' };
const STATUS_COLORS = { received: 'badge-teal', returned: 'badge-red' };
let currentYearMonth = '';
let dateBasis = 'purchase'; // 'purchase' 또는 'received'
function setDateBasis(basis) {
dateBasis = basis;
document.getElementById('btnDatePurchase').className = basis === 'purchase'
? 'px-3 py-2 bg-orange-600 text-white' : 'px-3 py-2 bg-white text-gray-600 hover:bg-gray-50';
document.getElementById('btnDateReceived').className = basis === 'received'
? 'px-3 py-2 bg-orange-600 text-white' : 'px-3 py-2 bg-white text-gray-600 hover:bg-gray-50';
}
async function loadAnalysis() {
currentYearMonth = document.getElementById('paMonth').value;
if (!currentYearMonth) { showToast('월을 선택해주세요.', 'error'); return; }
try {
const [summaryRes, purchasesRes, priceChangesRes] = await Promise.all([
api(`/settlements/summary?year_month=${currentYearMonth}`),
api(`/settlements/purchases?year_month=${currentYearMonth}`),
api(`/settlements/price-changes?year_month=${currentYearMonth}`)
]);
renderCategorySummary(summaryRes.data?.categorySummary || []);
renderVendorSummary(summaryRes.data?.vendorSummary || []);
renderPurchaseList(purchasesRes.data || []);
renderPriceChanges(priceChangesRes.data || []);
if (dateBasis === 'purchase') {
await loadPurchaseBasis();
} else {
await loadReceivedBasis();
}
} catch (e) {
showToast('데이터 로드 실패: ' + e.message, 'error');
}
}
/* ===== 구매일 기준 (기존) ===== */
async function loadPurchaseBasis() {
// 입고 섹션 숨김
document.getElementById('paReceivedSection').classList.add('hidden');
const [summaryRes, purchasesRes, priceChangesRes] = await Promise.all([
api(`/settlements/summary?year_month=${currentYearMonth}`),
api(`/settlements/purchases?year_month=${currentYearMonth}`),
api(`/settlements/price-changes?year_month=${currentYearMonth}`)
]);
renderCategorySummary(summaryRes.data?.categorySummary || []);
renderVendorSummary(summaryRes.data?.vendorSummary || []);
renderPurchaseList(purchasesRes.data || []);
renderPriceChanges(priceChangesRes.data || []);
}
/* ===== 입고일 기준 ===== */
async function loadReceivedBasis() {
const [summaryRes, listRes] = await Promise.all([
api(`/settlements/received-summary?year_month=${currentYearMonth}`),
api(`/settlements/received-list?year_month=${currentYearMonth}`)
]);
renderCategorySummary(summaryRes.data?.categorySummary || []);
// 업체/구매목록/가격변동은 빈 상태로
document.getElementById('paVendorSummary').innerHTML = '<tr><td colspan="5" class="px-4 py-4 text-center text-gray-400">입고일 기준에서는 업체별 정산이 표시되지 않습니다.</td></tr>';
document.getElementById('paPurchaseList').innerHTML = '<tr><td colspan="8" class="px-4 py-4 text-center text-gray-400">아래 입고 내역을 확인하세요.</td></tr>';
document.getElementById('paPriceChanges').innerHTML = '';
// 입고 섹션 표시
document.getElementById('paReceivedSection').classList.remove('hidden');
renderReceivedList(listRes.data || []);
}
/* ===== 렌더링 함수들 ===== */
function renderCategorySummary(data) {
const el = document.getElementById('paCategorySummary');
const allCategories = ['consumable', 'safety', 'repair', 'equipment'];
const dataMap = {};
data.forEach(d => { dataMap[d.category] = d; });
const totalAmount = data.reduce((sum, d) => sum + Number(d.total_amount || 0), 0);
el.innerHTML = allCategories.map(cat => {
@@ -48,7 +89,7 @@ function renderCategorySummary(data) {
</div>`;
}).join('') + `
<div class="col-span-2 lg:col-span-4 bg-orange-50 rounded-xl p-3 text-center">
<span class="text-sm text-orange-700 font-semibold">월 합계: ${totalAmount.toLocaleString()}원</span>
<span class="text-sm text-orange-700 font-semibold">${dateBasis === 'purchase' ? '구매' : '입고'} 월 합계: ${totalAmount.toLocaleString()}원</span>
</div>`;
}
@@ -60,21 +101,15 @@ function renderVendorSummary(data) {
}
tbody.innerHTML = data.map(v => {
const isCompleted = v.settlement_status === 'completed';
const statusBadge = isCompleted
? '<span class="badge badge-green">정산완료</span>'
: '<span class="badge badge-gray">미정산</span>';
const statusBadge = isCompleted ? '<span class="badge badge-green">정산완료</span>' : '<span class="badge badge-gray">미정산</span>';
const vendorName = v.vendor_name || '(업체 미지정)';
const vendorId = v.vendor_id || 0;
let actionBtn = '';
if (vendorId > 0) {
if (isCompleted) {
actionBtn = `<button onclick="cancelSettlement(${vendorId})" class="px-3 py-1 border border-gray-300 rounded text-xs text-gray-600 hover:bg-gray-50">정산 취소</button>`;
} else {
actionBtn = `<button onclick="completeSettlement(${vendorId})" class="px-3 py-1 bg-green-500 text-white rounded text-xs hover:bg-green-600">정산완료</button>`;
}
actionBtn = isCompleted
? `<button onclick="cancelSettlement(${vendorId})" class="px-3 py-1 border border-gray-300 rounded text-xs text-gray-600 hover:bg-gray-50">정산 취소</button>`
: `<button onclick="completeSettlement(${vendorId})" class="px-3 py-1 bg-green-500 text-white rounded text-xs hover:bg-green-600">정산완료</button>`;
}
return `<tr class="hover:bg-gray-50">
<td class="px-4 py-3 font-medium text-gray-800">${escapeHtml(vendorName)}</td>
<td class="px-4 py-3 text-right">${v.count}건</td>
@@ -99,7 +134,6 @@ function renderPurchaseList(data) {
const unitPrice = Number(p.unit_price || 0);
const hasPriceDiff = basePrice > 0 && unitPrice > 0 && basePrice !== unitPrice;
const priceDiffClass = hasPriceDiff ? (unitPrice > basePrice ? 'text-red-600 font-semibold' : 'text-blue-600 font-semibold') : '';
return `<tr class="hover:bg-gray-50 ${hasPriceDiff ? 'bg-yellow-50' : ''}">
<td class="px-4 py-3">
<div class="font-medium text-gray-800">${escapeHtml(p.item_name)}${p.spec ? ' <span class="text-gray-400">[' + escapeHtml(p.spec) + ']</span>' : ''}</div>
@@ -149,13 +183,39 @@ function renderPriceChanges(data) {
</table>`;
}
function renderReceivedList(data) {
const tbody = document.getElementById('paReceivedList');
if (!data.length) {
tbody.innerHTML = '<tr><td colspan="8" class="px-4 py-8 text-center text-gray-400">해당 월 입고 내역이 없습니다.</td></tr>';
return;
}
tbody.innerHTML = data.map(r => {
const catLabel = CAT_LABELS[r.category] || r.category || '-';
const catColor = CAT_BG[r.category] || '';
const statusLabel = STATUS_LABELS[r.status] || r.status;
const statusColor = STATUS_COLORS[r.status] || 'badge-gray';
return `<tr class="hover:bg-gray-50 ${r.status === 'returned' ? 'bg-red-50' : ''}">
<td class="px-4 py-3">
<div class="font-medium text-gray-800">${escapeHtml(r.item_name || '-')}${r.spec ? ' <span class="text-gray-400">[' + escapeHtml(r.spec) + ']</span>' : ''}</div>
<div class="text-xs text-gray-500">${escapeHtml(r.maker || '')} · ${escapeHtml(r.requester_name || '')}</div>
</td>
<td class="px-4 py-3"><span class="px-1.5 py-0.5 rounded text-xs ${catColor}">${catLabel}</span></td>
<td class="px-4 py-3 text-right">${r.quantity}</td>
<td class="px-4 py-3 text-right">${r.unit_price ? Number(r.unit_price).toLocaleString() + '원' : '-'}</td>
<td class="px-4 py-3 text-gray-600">${escapeHtml(r.vendor_name || '-')}</td>
<td class="px-4 py-3 text-gray-600">${formatDateTime(r.received_at)}</td>
<td class="px-4 py-3 text-gray-600 text-xs">${escapeHtml(r.received_location || '-')}</td>
<td class="px-4 py-3 text-center"><span class="badge ${statusColor}">${statusLabel}</span></td>
</tr>`;
}).join('');
}
/* ===== 정산 처리 ===== */
async function completeSettlement(vendorId) {
if (!confirm('이 업체의 정산을 완료 처리하시겠습니까?')) return;
try {
await api('/settlements/complete', {
method: 'POST',
body: JSON.stringify({ year_month: currentYearMonth, vendor_id: vendorId })
method: 'POST', body: JSON.stringify({ year_month: currentYearMonth, vendor_id: vendorId })
});
showToast('정산 완료 처리되었습니다.');
await loadAnalysis();
@@ -166,8 +226,7 @@ async function cancelSettlement(vendorId) {
if (!confirm('정산 완료를 취소하시겠습니까?')) return;
try {
await api('/settlements/cancel', {
method: 'POST',
body: JSON.stringify({ year_month: currentYearMonth, vendor_id: vendorId })
method: 'POST', body: JSON.stringify({ year_month: currentYearMonth, vendor_id: vendorId })
});
showToast('정산이 취소되었습니다.');
await loadAnalysis();
@@ -177,7 +236,6 @@ async function cancelSettlement(vendorId) {
/* ===== Init ===== */
(async function() {
if (!await initAuth()) return;
// 기본값: 현재 월
const now = new Date();
document.getElementById('paMonth').value = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`;
})();

View File

@@ -4,8 +4,8 @@ const TKUSER_BASE_URL = location.hostname.includes('technicalkorea.net')
: location.protocol + '//' + location.hostname + ':30180';
const CAT_LABELS = { consumable: '소모품', safety: '안전용품', repair: '수선비', equipment: '설비' };
const STATUS_LABELS = { pending: '대기', grouped: '구매진행중', purchased: '구매완료', received: '입고완료', hold: '보류' };
const STATUS_COLORS = { pending: 'badge-amber', grouped: 'badge-blue', purchased: 'badge-green', received: 'badge-teal', hold: 'badge-gray' };
const STATUS_LABELS = { pending: '대기', grouped: '구매진행중', purchased: '구매완료', received: '입고완료', cancelled: '취소', returned: '반품', hold: '보류' };
const STATUS_COLORS = { pending: 'badge-amber', grouped: 'badge-blue', purchased: 'badge-green', received: 'badge-teal', cancelled: 'badge-red', returned: 'badge-red', hold: 'badge-gray' };
const MATCH_LABELS = { exact: '정확', name: '이름', alias: '별칭', spec: '규격', chosung: '초성', chosung_alias: '초성' };
let currentPage = 1;

View File

@@ -7,8 +7,8 @@ const CAT_LABELS = { consumable: '소모품', safety: '안전용품', repair: '
const CAT_COLORS = { consumable: 'badge-blue', safety: 'badge-green', repair: 'badge-amber', equipment: 'badge-purple' };
const CAT_BG = { consumable: '#dbeafe', safety: '#dcfce7', repair: '#fef3c7', equipment: '#f3e8ff' };
const CAT_FG = { consumable: '#1e40af', safety: '#166534', repair: '#92400e', equipment: '#7e22ce' };
const STATUS_LABELS = { pending: '대기', grouped: '구매진행중', purchased: '구매완료', received: '입고완료', hold: '보류' };
const STATUS_COLORS = { pending: 'badge-amber', grouped: 'badge-blue', purchased: 'badge-green', received: 'badge-teal', hold: 'badge-gray' };
const STATUS_LABELS = { pending: '대기', grouped: '구매진행중', purchased: '구매완료', received: '입고완료', cancelled: '취소', returned: '반품', hold: '보류' };
const STATUS_COLORS = { pending: 'badge-amber', grouped: 'badge-blue', purchased: 'badge-green', received: 'badge-teal', cancelled: 'badge-red', returned: 'badge-red', hold: 'badge-gray' };
function _fmtSpec(spec) { return spec ? ' [' + spec + ']' : ''; }
@@ -359,7 +359,12 @@ function renderRequests() {
actions = `<button onclick="openPurchaseModal(${r.request_id})" class="px-2 py-1 bg-orange-500 text-white rounded text-xs hover:bg-orange-600 mr-1" title="구매 처리"><i class="fas fa-shopping-cart"></i></button>
<button onclick="openHoldModal(${r.request_id})" class="px-2 py-1 bg-gray-400 text-white rounded text-xs hover:bg-gray-500" title="보류"><i class="fas fa-pause"></i></button>`;
} else if (isAdmin && r.status === 'purchased') {
actions = `<button onclick="openReceiveModal(${r.request_id})" class="px-2 py-1 bg-teal-500 text-white rounded text-xs hover:bg-teal-600" title="입고 처리"><i class="fas fa-box-open"></i></button>`;
actions = `<button onclick="openReceiveModal(${r.request_id})" class="px-2 py-1 bg-teal-500 text-white rounded text-xs hover:bg-teal-600 mr-1" title="입고 처리"><i class="fas fa-box-open"></i></button>
<button onclick="cancelPurchase(${r.request_id})" class="px-2 py-1 bg-red-400 text-white rounded text-xs hover:bg-red-500" title="구매 취소"><i class="fas fa-times"></i></button>`;
} else if (isAdmin && r.status === 'received') {
actions = `<button onclick="returnItem(${r.request_id})" class="px-2 py-1 bg-red-500 text-white rounded text-xs hover:bg-red-600" title="반품"><i class="fas fa-undo-alt"></i></button>`;
} else if (isAdmin && r.status === 'cancelled') {
actions = `<button onclick="revertCancel(${r.request_id})" class="px-2 py-1 bg-blue-500 text-white rounded text-xs hover:bg-blue-600" title="대기로 되돌리기"><i class="fas fa-undo"></i></button>`;
} else if (isAdmin && r.status === 'hold') {
actions = `<button onclick="revertRequest(${r.request_id})" class="px-2 py-1 bg-blue-500 text-white rounded text-xs hover:bg-blue-600" title="대기로 되돌리기"><i class="fas fa-undo"></i></button>`;
}
@@ -660,6 +665,40 @@ async function batchReceive(batchId) {
} catch (e) { showToast(e.message, 'error'); }
}
/* ===== 구매 취소 / 반품 / 되돌리기 ===== */
async function cancelPurchase(requestId) {
const reason = prompt('취소 사유를 입력하세요 (선택):');
if (reason === null) return;
try {
await api(`/purchase-requests/${requestId}/cancel`, {
method: 'PUT', body: JSON.stringify({ cancel_reason: reason || null })
});
showToast('구매가 취소되었습니다.');
await loadRequests();
} catch (e) { showToast(e.message, 'error'); }
}
async function returnItem(requestId) {
const reason = prompt('반품 사유를 입력하세요:');
if (reason === null) return;
try {
await api(`/purchase-requests/${requestId}/return`, {
method: 'PUT', body: JSON.stringify({ cancel_reason: reason || null })
});
showToast('반품 처리되었습니다.');
await loadRequests();
} catch (e) { showToast(e.message, 'error'); }
}
async function revertCancel(requestId) {
if (!confirm('이 신청을 대기 상태로 되돌리시겠습니까?')) return;
try {
await api(`/purchase-requests/${requestId}/revert-cancel`, { method: 'PUT' });
showToast('대기 상태로 되돌렸습니다.');
await loadRequests();
} catch (e) { showToast(e.message, 'error'); }
}
/* ===== 입고 처리 모달 ===== */
let currentRequestForReceive = null;
let receivePhotoBase64 = null;