diff --git a/static/app.js b/static/app.js index 90e7ac0..b341acf 100644 --- a/static/app.js +++ b/static/app.js @@ -1294,13 +1294,15 @@ $(document).ready(function() { } // 재고 원장 보기 + let currentLedgerData = []; // 원본 데이터 저장 + function viewStockLedger(herbId, herbName) { const url = herbId ? `/api/stock-ledger?herb_id=${herbId}` : '/api/stock-ledger'; $.get(url, function(response) { if (response.success) { - const tbody = $('#stockLedgerList'); - tbody.empty(); + // 원본 데이터 저장 + currentLedgerData = response.ledger; // 헤더 업데이트 if (herbName) { @@ -1309,7 +1311,40 @@ $(document).ready(function() { $('#stockLedgerModal .modal-title').html(` 전체 입출고 원장`); } - response.ledger.forEach(entry => { + // 필터 적용하여 표시 + applyLedgerFilters(); + + // 약재 필터 옵션 업데이트 + const herbFilter = $('#ledgerHerbFilter'); + if (herbFilter.find('option').length <= 1) { + response.summary.forEach(herb => { + herbFilter.append(``); + }); + } + + $('#stockLedgerModal').modal('show'); + } + }).fail(function() { + alert('입출고 내역을 불러오는데 실패했습니다.'); + }); + } + + // 필터 적용 함수 + function applyLedgerFilters() { + const typeFilter = $('#ledgerTypeFilter').val(); + const tbody = $('#stockLedgerList'); + tbody.empty(); + + // 필터링된 데이터 + let filteredData = currentLedgerData; + + // 타입 필터 적용 + if (typeFilter) { + filteredData = currentLedgerData.filter(entry => entry.event_type === typeFilter); + } + + // 데이터 표시 + filteredData.forEach(entry => { let typeLabel = ''; let typeBadge = ''; switch(entry.event_type) { @@ -1349,21 +1384,16 @@ $(document).ready(function() { ${entry.reference_no || '-'} `); - }); - - // 약재 필터 옵션 업데이트 - const herbFilter = $('#ledgerHerbFilter'); - if (herbFilter.find('option').length <= 1) { - response.summary.forEach(herb => { - herbFilter.append(``); - }); - } - - $('#stockLedgerModal').modal('show'); - } - }).fail(function() { - alert('입출고 내역을 불러오는데 실패했습니다.'); }); + + // 데이터가 없는 경우 + if (filteredData.length === 0) { + tbody.append(` + + 데이터가 없습니다. + + `); + } } // 입출고 원장 모달 버튼 이벤트 @@ -1372,11 +1402,10 @@ $(document).ready(function() { }); // 필터 변경 이벤트 - $('#ledgerHerbFilter, #ledgerTypeFilter').on('change', function() { - const herbId = $('#ledgerHerbFilter').val(); - const typeFilter = $('#ledgerTypeFilter').val(); + $('#ledgerHerbFilter').on('change', function() { + const herbId = $(this).val(); - // 재로드 (필터 적용은 프론트엔드에서 처리) + // 약재 필터 변경 시 데이터 재로드 if (herbId) { const herbName = $('#ledgerHerbFilter option:selected').text(); viewStockLedger(herbId, herbName); @@ -1385,6 +1414,11 @@ $(document).ready(function() { } }); + // 타입 필터 변경 이벤트 (현재 데이터에서 필터링만) + $('#ledgerTypeFilter').on('change', function() { + applyLedgerFilters(); + }); + function formatCurrency(amount) { if (amount === null || amount === undefined) return '0원'; return new Intl.NumberFormat('ko-KR', {