feat: 직접조제 및 원산지별 재고 관리 기능 강화

API 개선:
- /api/herbs, /api/inventory/summary에 효능 태그 추가
- 조제 시 원산지 선택 처리 로직 추가
- 원산지별 FIFO 또는 자동 선택 (저렴한 것부터)

UI 개선:
- 재고 목록에 효능 태그 표시 (녹색 배지)
- 처방 선택에 "직접조제" 옵션 추가
- 조제 시 원산지 선택 드롭다운 추가
- JavaScript 주석 블록 오류 수정

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
시골약사 2026-02-15 09:55:22 +00:00
parent 54af26e384
commit c834b5625f
3 changed files with 684 additions and 53 deletions

320
app.py
View File

@ -125,18 +125,36 @@ def create_patient():
@app.route('/api/herbs', methods=['GET'])
def get_herbs():
"""약재 목록 조회"""
"""약재 목록 조회 (효능 태그 포함)"""
try:
with get_db() as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT h.*, COALESCE(s.total_quantity, 0) as current_stock
SELECT
h.herb_item_id,
h.insurance_code,
h.herb_name,
h.is_active,
COALESCE(SUM(il.quantity_onhand), 0) as current_stock,
GROUP_CONCAT(DISTINCT et.tag_name) as efficacy_tags
FROM herb_items h
LEFT JOIN v_current_stock s ON h.herb_item_id = s.herb_item_id
LEFT JOIN inventory_lots il ON h.herb_item_id = il.herb_item_id
AND il.is_depleted = 0
LEFT JOIN herb_item_tags hit ON h.herb_item_id = hit.herb_item_id
LEFT JOIN herb_efficacy_tags et ON hit.tag_id = et.tag_id
WHERE h.is_active = 1
GROUP BY h.herb_item_id, h.insurance_code, h.herb_name, h.is_active
ORDER BY h.herb_name
""")
herbs = [dict(row) for row in cursor.fetchall()]
# 태그를 리스트로 변환
for herb in herbs:
if herb['efficacy_tags']:
herb['efficacy_tags'] = herb['efficacy_tags'].split(',')
else:
herb['efficacy_tags'] = []
return jsonify({'success': True, 'data': herbs})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
@ -227,6 +245,53 @@ def get_formula_ingredients(formula_id):
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
# ==================== 도매상 관리 API ====================
@app.route('/api/suppliers', methods=['GET'])
def get_suppliers():
"""도매상 목록 조회"""
try:
with get_db() as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT supplier_id, name, business_no, phone, address, is_active
FROM suppliers
WHERE is_active = 1
ORDER BY name
""")
suppliers = [dict(row) for row in cursor.fetchall()]
return jsonify({'success': True, 'data': suppliers})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
@app.route('/api/suppliers', methods=['POST'])
def create_supplier():
"""도매상 등록"""
try:
data = request.json
with get_db() as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO suppliers (name, business_no, contact_person, phone, address)
VALUES (?, ?, ?, ?, ?)
""", (
data['name'],
data.get('business_no'),
data.get('contact_person'),
data.get('phone'),
data.get('address')
))
supplier_id = cursor.lastrowid
return jsonify({
'success': True,
'message': '도매상이 등록되었습니다',
'supplier_id': supplier_id
})
except sqlite3.IntegrityError:
return jsonify({'success': False, 'error': '이미 등록된 도매상입니다'}), 400
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
# ==================== 입고 관리 API ====================
@app.route('/api/upload/purchase', methods=['POST'])
@ -243,6 +308,11 @@ def upload_purchase_excel():
if not allowed_file(file.filename):
return jsonify({'success': False, 'error': '허용되지 않는 파일 형식입니다'}), 400
# 도매상 ID 가져오기 (폼 데이터에서)
supplier_id = request.form.get('supplier_id')
if not supplier_id:
return jsonify({'success': False, 'error': '도매상을 선택해주세요'}), 400
# 파일 저장
filename = secure_filename(file.filename)
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
@ -281,21 +351,16 @@ def upload_purchase_excel():
processed_rows = 0
processed_items = set()
# 날짜별, 업체별로 그룹화
grouped = df.groupby(['receipt_date', 'supplier_name'])
# 도매상 정보 확인
cursor.execute("SELECT name FROM suppliers WHERE supplier_id = ?", (supplier_id,))
supplier_info = cursor.fetchone()
if not supplier_info:
return jsonify({'success': False, 'error': '유효하지 않은 도매상입니다'}), 400
for (receipt_date, supplier_name), group in grouped:
# 공급업체 확인/생성
cursor.execute("SELECT supplier_id FROM suppliers WHERE name = ?", (supplier_name,))
supplier = cursor.fetchone()
# 날짜별로 그룹화 (도매상은 이미 선택됨)
grouped = df.groupby(['receipt_date'])
if not supplier:
cursor.execute("""
INSERT INTO suppliers (name) VALUES (?)
""", (supplier_name,))
supplier_id = cursor.lastrowid
else:
supplier_id = supplier[0]
for receipt_date, group in grouped:
# 입고장 헤더 생성
total_amount = group['total_amount'].sum()
@ -399,13 +464,13 @@ def get_purchase_receipts():
pr.receipt_id,
pr.receipt_date,
pr.receipt_no,
pr.total_amount,
pr.source_file,
pr.created_at,
s.name as supplier_name,
s.supplier_id,
COUNT(prl.line_id) as line_count,
SUM(prl.quantity_g) as total_quantity
SUM(prl.quantity_g) as total_quantity,
SUM(prl.line_total) as total_amount
FROM purchase_receipts pr
JOIN suppliers s ON pr.supplier_id = s.supplier_id
LEFT JOIN purchase_receipt_lines prl ON pr.receipt_id = prl.receipt_id
@ -625,7 +690,19 @@ def delete_purchase_receipt(receipt_id):
'error': f'{usage["used_count"]}개 품목에서 {usage["used_quantity"]}g이 이미 사용되어 삭제할 수 없습니다'
}), 400
# 재고 로트 삭제
# 삭제 순서 중요: 참조하는 테이블부터 삭제
# 1. 재고 원장 기록 삭제 (lot_id를 참조)
cursor.execute("""
DELETE FROM stock_ledger
WHERE lot_id IN (
SELECT lot_id FROM inventory_lots
WHERE receipt_line_id IN (
SELECT line_id FROM purchase_receipt_lines WHERE receipt_id = ?
)
)
""", (receipt_id,))
# 2. 재고 로트 삭제 (receipt_line_id를 참조)
cursor.execute("""
DELETE FROM inventory_lots
WHERE receipt_line_id IN (
@ -633,16 +710,10 @@ def delete_purchase_receipt(receipt_id):
)
""", (receipt_id,))
# 재고 원장 기록
cursor.execute("""
DELETE FROM stock_ledger
WHERE reference_table = 'purchase_receipts' AND reference_id = ?
""", (receipt_id,))
# 입고장 라인 삭제
# 3. 입고장 라인 삭제 (receipt_id를 참조)
cursor.execute("DELETE FROM purchase_receipt_lines WHERE receipt_id = ?", (receipt_id,))
# 입고장 헤더 삭제
# 4. 입고장 헤더 삭제
cursor.execute("DELETE FROM purchase_receipts WHERE receipt_id = ?", (receipt_id,))
return jsonify({'success': True, 'message': '입고장이 삭제되었습니다'})
@ -686,6 +757,7 @@ def create_compound():
for ingredient in data['ingredients']:
herb_item_id = ingredient['herb_item_id']
total_grams = ingredient['total_grams']
origin_country = ingredient.get('origin_country') # 원산지 선택 정보
# 조제 약재 구성 기록
cursor.execute("""
@ -695,14 +767,26 @@ def create_compound():
""", (compound_id, herb_item_id,
ingredient['grams_per_cheop'], total_grams))
# 재고 차감 (FIFO 방식)
# 재고 차감 (FIFO 방식 - 원산지 지정 시 해당 원산지만)
remaining_qty = total_grams
cursor.execute("""
SELECT lot_id, quantity_onhand, unit_price_per_g
FROM inventory_lots
WHERE herb_item_id = ? AND is_depleted = 0 AND quantity_onhand > 0
ORDER BY received_date, lot_id
""", (herb_item_id,))
# 원산지가 지정된 경우 해당 원산지만, 아니면 전체에서 FIFO
if origin_country and origin_country != 'auto':
cursor.execute("""
SELECT lot_id, quantity_onhand, unit_price_per_g
FROM inventory_lots
WHERE herb_item_id = ? AND is_depleted = 0 AND quantity_onhand > 0
AND origin_country = ?
ORDER BY unit_price_per_g, received_date, lot_id
""", (herb_item_id, origin_country))
else:
# 자동 선택: 가격이 저렴한 것부터
cursor.execute("""
SELECT lot_id, quantity_onhand, unit_price_per_g
FROM inventory_lots
WHERE herb_item_id = ? AND is_depleted = 0 AND quantity_onhand > 0
ORDER BY unit_price_per_g, received_date, lot_id
""", (herb_item_id,))
lots = cursor.fetchall()
@ -761,11 +845,90 @@ def create_compound():
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
# ==================== 조제용 재고 조회 API ====================
@app.route('/api/herbs/<int:herb_item_id>/available-lots', methods=['GET'])
def get_available_lots(herb_item_id):
"""조제용 가용 로트 목록 - 원산지별로 그룹화"""
try:
with get_db() as conn:
cursor = conn.cursor()
# 약재 정보
cursor.execute("""
SELECT herb_name, insurance_code
FROM herb_items
WHERE herb_item_id = ?
""", (herb_item_id,))
herb = cursor.fetchone()
if not herb:
return jsonify({'success': False, 'error': '약재를 찾을 수 없습니다'}), 404
# 가용 로트 목록 (소진되지 않은 재고)
cursor.execute("""
SELECT
lot_id,
origin_country,
quantity_onhand,
unit_price_per_g,
received_date,
supplier_id
FROM inventory_lots
WHERE herb_item_id = ?
AND is_depleted = 0
AND quantity_onhand > 0
ORDER BY origin_country, unit_price_per_g, received_date
""", (herb_item_id,))
lots = []
for row in cursor.fetchall():
lots.append({
'lot_id': row[0],
'origin_country': row[1] or '미지정',
'quantity_onhand': row[2],
'unit_price_per_g': row[3],
'received_date': row[4],
'supplier_id': row[5]
})
# 원산지별 요약
origin_summary = {}
for lot in lots:
origin = lot['origin_country']
if origin not in origin_summary:
origin_summary[origin] = {
'origin_country': origin,
'total_quantity': 0,
'min_price': float('inf'),
'max_price': 0,
'lot_count': 0,
'lots': []
}
origin_summary[origin]['total_quantity'] += lot['quantity_onhand']
origin_summary[origin]['min_price'] = min(origin_summary[origin]['min_price'], lot['unit_price_per_g'])
origin_summary[origin]['max_price'] = max(origin_summary[origin]['max_price'], lot['unit_price_per_g'])
origin_summary[origin]['lot_count'] += 1
origin_summary[origin]['lots'].append(lot)
return jsonify({
'success': True,
'data': {
'herb_name': herb[0],
'insurance_code': herb[1],
'origins': list(origin_summary.values()),
'total_quantity': sum(lot['quantity_onhand'] for lot in lots)
}
})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
# ==================== 재고 현황 API ====================
@app.route('/api/inventory/summary', methods=['GET'])
def get_inventory_summary():
"""재고 현황 요약"""
"""재고 현황 요약 - 원산지별 구분 표시"""
try:
with get_db() as conn:
cursor = conn.cursor()
@ -776,16 +939,29 @@ def get_inventory_summary():
h.herb_name,
COALESCE(SUM(il.quantity_onhand), 0) as total_quantity,
COUNT(DISTINCT il.lot_id) as lot_count,
COUNT(DISTINCT il.origin_country) as origin_count,
AVG(il.unit_price_per_g) as avg_price,
COALESCE(SUM(il.quantity_onhand * il.unit_price_per_g), 0) as total_value
MIN(il.unit_price_per_g) as min_price,
MAX(il.unit_price_per_g) as max_price,
COALESCE(SUM(il.quantity_onhand * il.unit_price_per_g), 0) as total_value,
GROUP_CONCAT(DISTINCT et.tag_name) as efficacy_tags
FROM herb_items h
LEFT JOIN inventory_lots il ON h.herb_item_id = il.herb_item_id AND il.is_depleted = 0
LEFT JOIN herb_item_tags hit ON h.herb_item_id = hit.herb_item_id
LEFT JOIN herb_efficacy_tags et ON hit.tag_id = et.tag_id
GROUP BY h.herb_item_id, h.insurance_code, h.herb_name
HAVING total_quantity > 0
ORDER BY h.herb_name
""")
inventory = [dict(row) for row in cursor.fetchall()]
# 태그를 리스트로 변환
for item in inventory:
if item['efficacy_tags']:
item['efficacy_tags'] = item['efficacy_tags'].split(',')
else:
item['efficacy_tags'] = []
# 전체 요약
total_value = sum(item['total_value'] for item in inventory)
total_items = len(inventory)
@ -801,6 +977,78 @@ def get_inventory_summary():
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
@app.route('/api/inventory/detail/<int:herb_item_id>', methods=['GET'])
def get_inventory_detail(herb_item_id):
"""약재별 재고 상세 - 원산지별로 구분"""
try:
with get_db() as conn:
cursor = conn.cursor()
# 약재 기본 정보
cursor.execute("""
SELECT herb_item_id, insurance_code, herb_name
FROM herb_items
WHERE herb_item_id = ?
""", (herb_item_id,))
herb = cursor.fetchone()
if not herb:
return jsonify({'success': False, 'error': '약재를 찾을 수 없습니다'}), 404
herb_data = dict(herb)
# 원산지별 재고 정보
cursor.execute("""
SELECT
il.lot_id,
il.origin_country,
il.quantity_onhand,
il.unit_price_per_g,
il.received_date,
il.supplier_id,
s.name as supplier_name,
il.quantity_onhand * il.unit_price_per_g as lot_value
FROM inventory_lots il
LEFT JOIN suppliers s ON il.supplier_id = s.supplier_id
WHERE il.herb_item_id = ? AND il.is_depleted = 0
ORDER BY il.origin_country, il.unit_price_per_g, il.received_date
""", (herb_item_id,))
lots = [dict(row) for row in cursor.fetchall()]
# 원산지별 그룹화
by_origin = {}
for lot in lots:
origin = lot['origin_country'] or '미지정'
if origin not in by_origin:
by_origin[origin] = {
'origin_country': origin,
'lots': [],
'total_quantity': 0,
'total_value': 0,
'min_price': float('inf'),
'max_price': 0,
'avg_price': 0
}
by_origin[origin]['lots'].append(lot)
by_origin[origin]['total_quantity'] += lot['quantity_onhand']
by_origin[origin]['total_value'] += lot['lot_value']
by_origin[origin]['min_price'] = min(by_origin[origin]['min_price'], lot['unit_price_per_g'])
by_origin[origin]['max_price'] = max(by_origin[origin]['max_price'], lot['unit_price_per_g'])
# 평균 단가 계산
for origin_data in by_origin.values():
if origin_data['total_quantity'] > 0:
origin_data['avg_price'] = origin_data['total_value'] / origin_data['total_quantity']
herb_data['origins'] = list(by_origin.values())
herb_data['total_origins'] = len(by_origin)
return jsonify({'success': True, 'data': herb_data})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
# 서버 실행
if __name__ == '__main__':
# 데이터베이스 초기화

View File

@ -32,6 +32,7 @@ $(document).ready(function() {
break;
case 'purchase':
loadPurchaseReceipts();
loadSuppliersForSelect();
break;
case 'formulas':
loadFormulas();
@ -287,6 +288,15 @@ $(document).ready(function() {
return;
}
// 직접조제인 경우
if (formulaId === 'custom') {
$('#compoundIngredients').empty();
// 빈 행 하나 추가
addEmptyIngredientRow();
return;
}
// 등록된 처방인 경우
$.get(`/api/formulas/${formulaId}/ingredients`, function(response) {
if (response.success) {
$('#compoundIngredients').empty();
@ -303,6 +313,11 @@ $(document).ready(function() {
value="${ing.grams_per_cheop}" min="0.1" step="0.1">
</td>
<td class="total-grams">${totalGrams.toFixed(1)}</td>
<td class="origin-select-cell">
<select class="form-control form-control-sm origin-select" disabled>
<option value="">로딩중...</option>
</select>
</td>
<td class="stock-status">확인중...</td>
<td>
<button type="button" class="btn btn-sm btn-outline-danger remove-compound-ingredient">
@ -311,6 +326,9 @@ $(document).ready(function() {
</td>
</tr>
`);
// 각 약재별로 원산지별 재고 확인
loadOriginOptions(ing.herb_item_id, totalGrams);
});
// 재고 확인
@ -353,6 +371,77 @@ $(document).ready(function() {
}
// 조제 약재 추가
// 빈 약재 행 추가 함수
function addEmptyIngredientRow() {
const newRow = $(`
<tr>
<td>
<select class="form-control form-control-sm herb-select-compound">
<option value="">약재 선택</option>
</select>
</td>
<td>
<input type="number" class="form-control form-control-sm grams-per-cheop"
min="0.1" step="0.1" placeholder="0.0">
</td>
<td class="total-grams">0.0</td>
<td class="origin-select-cell">
<select class="form-control form-control-sm origin-select" disabled>
<option value="">약재 선택 표시</option>
</select>
</td>
<td class="stock-status">-</td>
<td>
<button type="button" class="btn btn-sm btn-outline-danger remove-compound-ingredient">
<i class="bi bi-x"></i>
</button>
</td>
</tr>
`);
$('#compoundIngredients').append(newRow);
// 약재 목록 로드
loadHerbsForSelect(newRow.find('.herb-select-compound'));
// 약재 선택 시 원산지 옵션 로드
newRow.find('.herb-select-compound').on('change', function() {
const herbId = $(this).val();
if (herbId) {
const row = $(this).closest('tr');
row.attr('data-herb-id', herbId);
const cheopTotal = parseFloat($('#cheopTotal').val()) || 0;
const gramsPerCheop = parseFloat(row.find('.grams-per-cheop').val()) || 0;
const totalGrams = gramsPerCheop * cheopTotal;
loadOriginOptions(herbId, totalGrams);
}
});
// 이벤트 바인딩
newRow.find('.grams-per-cheop').on('input', function() {
updateIngredientTotals();
// 원산지 옵션 다시 로드
const herbId = $(this).closest('tr').attr('data-herb-id');
if (herbId) {
const cheopTotal = parseFloat($('#cheopTotal').val()) || 0;
const gramsPerCheop = parseFloat($(this).val()) || 0;
const totalGrams = gramsPerCheop * cheopTotal;
loadOriginOptions(herbId, totalGrams);
}
});
newRow.find('.remove-compound-ingredient').on('click', function() {
$(this).closest('tr').remove();
updateIngredientTotals();
});
}
$('#addIngredientBtn').on('click', function() {
addEmptyIngredientRow();
});
// 기존 약재 추가 버튼 (기존 코드 삭제)
/*
$('#addIngredientBtn').on('click', function() {
const newRow = $(`
<tr>
@ -391,6 +480,7 @@ $(document).ready(function() {
updateIngredientTotals();
});
});
*/
// 조제 실행
$('#compoundEntryForm').on('submit', function(e) {
@ -401,12 +491,14 @@ $(document).ready(function() {
const herbId = $(this).data('herb-id');
const gramsPerCheop = parseFloat($(this).find('.grams-per-cheop').val());
const totalGrams = parseFloat($(this).find('.total-grams').text());
const originCountry = $(this).find('.origin-select').val();
if (herbId && gramsPerCheop) {
ingredients.push({
herb_item_id: parseInt(herbId),
grams_per_cheop: gramsPerCheop,
total_grams: totalGrams
total_grams: totalGrams,
origin_country: originCountry || null // 원산지 선택 정보 추가
});
}
});
@ -452,17 +544,140 @@ $(document).ready(function() {
tbody.empty();
response.data.forEach(item => {
// 원산지가 여러 개인 경우 표시
const originBadge = item.origin_count > 1
? `<span class="badge bg-info ms-2">${item.origin_count}개 원산지</span>`
: '';
// 효능 태그 표시
let efficacyTags = '';
if (item.efficacy_tags && item.efficacy_tags.length > 0) {
efficacyTags = item.efficacy_tags.map(tag =>
`<span class="badge bg-success ms-1">${tag}</span>`
).join('');
}
// 가격 범위 표시 (원산지가 여러 개이고 가격차가 있는 경우)
let priceDisplay = item.avg_price ? formatCurrency(item.avg_price) : '-';
if (item.origin_count > 1 && item.min_price && item.max_price && item.min_price !== item.max_price) {
priceDisplay = `${formatCurrency(item.min_price)} ~ ${formatCurrency(item.max_price)}`;
}
tbody.append(`
<tr>
<tr class="inventory-row" data-herb-id="${item.herb_item_id}" style="cursor: pointer;">
<td>${item.insurance_code || '-'}</td>
<td>${item.herb_name}</td>
<td>${item.herb_name}${originBadge}${efficacyTags}</td>
<td>${item.total_quantity.toFixed(1)}</td>
<td>${item.lot_count}</td>
<td>${item.avg_price ? formatCurrency(item.avg_price) : '-'}</td>
<td>${priceDisplay}</td>
<td>${formatCurrency(item.total_value)}</td>
</tr>
`);
});
// 클릭 이벤트 바인딩
$('.inventory-row').on('click', function() {
const herbId = $(this).data('herb-id');
showInventoryDetail(herbId);
});
}
});
}
// 재고 상세 모달 표시
function showInventoryDetail(herbId) {
$.get(`/api/inventory/detail/${herbId}`, function(response) {
if (response.success) {
const data = response.data;
// 원산지별 재고 정보 HTML 생성
let originsHtml = '';
data.origins.forEach(origin => {
originsHtml += `
<div class="card mb-3">
<div class="card-header bg-light">
<h6 class="mb-0">
<i class="bi bi-geo-alt"></i> ${origin.origin_country}
<span class="badge bg-primary float-end">${origin.total_quantity.toFixed(1)}g</span>
</h6>
</div>
<div class="card-body">
<div class="row mb-2">
<div class="col-6">
<small class="text-muted">평균 단가:</small><br>
<strong>${formatCurrency(origin.avg_price)}/g</strong>
</div>
<div class="col-6">
<small class="text-muted">재고 가치:</small><br>
<strong>${formatCurrency(origin.total_value)}</strong>
</div>
</div>
<table class="table table-sm">
<thead>
<tr>
<th>로트ID</th>
<th>수량</th>
<th>단가</th>
<th>입고일</th>
<th>도매상</th>
</tr>
</thead>
<tbody>`;
origin.lots.forEach(lot => {
originsHtml += `
<tr>
<td>#${lot.lot_id}</td>
<td>${lot.quantity_onhand.toFixed(1)}g</td>
<td>${formatCurrency(lot.unit_price_per_g)}</td>
<td>${lot.received_date}</td>
<td>${lot.supplier_name || '-'}</td>
</tr>`;
});
originsHtml += `
</tbody>
</table>
</div>
</div>`;
});
// 모달 생성 및 표시
const modalHtml = `
<div class="modal fade" id="inventoryDetailModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
${data.herb_name} 재고 상세
<small class="text-muted">(${data.insurance_code})</small>
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
${data.total_origins > 1
? `<div class="alert alert-info">
<i class="bi bi-info-circle"></i>
약재는 ${data.total_origins} 원산지의 재고가 있습니다.
조제 원산지를 선택할 있습니다.
</div>`
: ''}
${originsHtml}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">닫기</button>
</div>
</div>
</div>
</div>`;
// 기존 모달 제거
$('#inventoryDetailModal').remove();
$('body').append(modalHtml);
// 모달 표시
const modal = new bootstrap.Modal(document.getElementById('inventoryDetailModal'));
modal.show();
}
});
}
@ -520,8 +735,8 @@ $(document).ready(function() {
<td>${receipt.receipt_date}</td>
<td>${receipt.supplier_name}</td>
<td>${receipt.line_count}</td>
<td>${receipt.total_quantity ? receipt.total_quantity.toLocaleString() + 'g' : '-'}</td>
<td>${receipt.total_amount ? formatCurrency(receipt.total_amount) : '-'}</td>
<td class="fw-bold text-primary">${receipt.total_amount ? formatCurrency(receipt.total_amount) : '-'}</td>
<td class="text-muted small">${receipt.total_quantity ? receipt.total_quantity.toLocaleString() + 'g' : '-'}</td>
<td>${receipt.source_file || '-'}</td>
<td>
<button class="btn btn-sm btn-outline-info view-receipt" data-id="${receipt.receipt_id}">
@ -638,10 +853,71 @@ $(document).ready(function() {
loadPurchaseReceipts();
});
// 도매상 목록 로드 (셀렉트 박스용)
function loadSuppliersForSelect() {
$.get('/api/suppliers', function(response) {
if (response.success) {
const select = $('#uploadSupplier');
select.empty().append('<option value="">도매상을 선택하세요</option>');
response.data.forEach(supplier => {
select.append(`<option value="${supplier.supplier_id}">${supplier.name}</option>`);
});
// 필터용 셀렉트 박스도 업데이트
const filterSelect = $('#purchaseSupplier');
filterSelect.empty().append('<option value="">전체</option>');
response.data.forEach(supplier => {
filterSelect.append(`<option value="${supplier.supplier_id}">${supplier.name}</option>`);
});
}
});
}
// 도매상 등록
$('#saveSupplierBtn').on('click', function() {
const supplierData = {
name: $('#supplierName').val(),
business_no: $('#supplierBusinessNo').val(),
contact_person: $('#supplierContactPerson').val(),
phone: $('#supplierPhone').val(),
address: $('#supplierAddress').val()
};
if (!supplierData.name) {
alert('도매상명은 필수입니다.');
return;
}
$.ajax({
url: '/api/suppliers',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(supplierData),
success: function(response) {
if (response.success) {
alert('도매상이 등록되었습니다.');
$('#supplierModal').modal('hide');
$('#supplierForm')[0].reset();
loadSuppliersForSelect();
}
},
error: function(xhr) {
alert('오류: ' + xhr.responseJSON.error);
}
});
});
// 입고장 업로드
$('#purchaseUploadForm').on('submit', function(e) {
e.preventDefault();
const supplierId = $('#uploadSupplier').val();
if (!supplierId) {
alert('도매상을 선택해주세요.');
return;
}
const formData = new FormData();
const fileInput = $('#purchaseFile')[0];
@ -651,6 +927,7 @@ $(document).ready(function() {
}
formData.append('file', fileInput.files[0]);
formData.append('supplier_id', supplierId);
$('#uploadResult').html('<div class="alert alert-info">업로드 중...</div>');
@ -731,9 +1008,17 @@ $(document).ready(function() {
const select = $('#compoundFormula');
select.empty().append('<option value="">처방을 선택하세요</option>');
response.data.forEach(formula => {
select.append(`<option value="${formula.formula_id}">${formula.formula_name}</option>`);
});
// 직접조제 옵션 추가
select.append('<option value="custom">직접조제</option>');
// 등록된 처방 추가
if (response.data.length > 0) {
select.append('<optgroup label="등록된 처방">');
response.data.forEach(formula => {
select.append(`<option value="${formula.formula_id}">${formula.formula_name}</option>`);
});
select.append('</optgroup>');
}
}
});
}
@ -750,6 +1035,51 @@ $(document).ready(function() {
});
}
// 원산지별 재고 옵션 로드
function loadOriginOptions(herbId, requiredQty) {
$.get(`/api/herbs/${herbId}/available-lots`, function(response) {
if (response.success) {
const selectElement = $(`tr[data-herb-id="${herbId}"] .origin-select`);
selectElement.empty();
const origins = response.data.origins;
if (origins.length === 0) {
selectElement.append('<option value="">재고 없음</option>');
selectElement.prop('disabled', true);
$(`tr[data-herb-id="${herbId}"] .stock-status`)
.html('<span class="text-danger">재고 없음</span>');
} else {
selectElement.append('<option value="auto">자동 선택 (저렴한 것부터)</option>');
origins.forEach(origin => {
const stockStatus = origin.total_quantity >= requiredQty ? '' : ' (재고 부족)';
const priceInfo = `${formatCurrency(origin.min_price)}/g`;
const option = `<option value="${origin.origin_country}"
data-price="${origin.min_price}"
data-available="${origin.total_quantity}"
${origin.total_quantity < requiredQty ? 'disabled' : ''}>
${origin.origin_country} - ${priceInfo} (재고: ${origin.total_quantity.toFixed(1)}g)${stockStatus}
</option>`;
selectElement.append(option);
});
selectElement.prop('disabled', false);
// 재고 상태 업데이트
const totalAvailable = response.data.total_quantity;
const statusElement = $(`tr[data-herb-id="${herbId}"] .stock-status`);
if (totalAvailable >= requiredQty) {
statusElement.html(`<span class="text-success">충분 (${totalAvailable.toFixed(1)}g)</span>`);
} else {
statusElement.html(`<span class="text-danger">부족 (${totalAvailable.toFixed(1)}g)</span>`);
}
}
}
});
}
function formatCurrency(amount) {
if (amount === null || amount === undefined) return '0원';
return new Intl.NumberFormat('ko-KR', {

View File

@ -257,8 +257,8 @@
<th>입고일</th>
<th>공급업체</th>
<th>품목 수</th>
<th>총 수량</th>
<th>총 금액</th>
<th>총 수량</th>
<th>파일명</th>
<th>작업</th>
</tr>
@ -272,19 +272,31 @@
<!-- Excel 업로드 -->
<div class="card">
<div class="card-header">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0">새 입고 등록 (Excel 업로드)</h5>
<button class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#supplierModal">
<i class="bi bi-plus"></i> 도매상 등록
</button>
</div>
<div class="card-body">
<form id="purchaseUploadForm" enctype="multipart/form-data">
<div class="mb-3">
<label for="purchaseFile" class="form-label">입고 Excel 파일 선택</label>
<input type="file" class="form-control" id="purchaseFile" accept=".xlsx,.xls" required>
<div class="form-text">
지원 형식: 한의사랑, 한의정보 (자동 감지)
<div class="row">
<div class="col-md-6">
<label for="uploadSupplier" class="form-label">도매상 선택 *</label>
<select class="form-control" id="uploadSupplier" required>
<option value="">도매상을 선택하세요</option>
</select>
</div>
<div class="col-md-6">
<label for="purchaseFile" class="form-label">Excel 파일 선택 *</label>
<input type="file" class="form-control" id="purchaseFile" accept=".xlsx,.xls" required>
</div>
</div>
<button type="submit" class="btn btn-primary">
<div class="form-text mt-2">
<i class="bi bi-info-circle"></i> Excel 형식: 한의사랑, 한의정보 (자동 감지)<br>
<i class="bi bi-info-circle"></i> Excel 내 업체명은 제조사(제약사)로 저장됩니다
</div>
<button type="submit" class="btn btn-primary mt-3">
<i class="bi bi-upload"></i> 업로드 및 처리
</button>
</form>
@ -370,6 +382,7 @@
<th>약재명</th>
<th>1첩당 용량(g)</th>
<th>총 용량(g)</th>
<th>원산지 선택</th>
<th>재고</th>
<th>작업</th>
</tr>
@ -599,6 +612,46 @@
</div>
</div>
<!-- Supplier Modal -->
<div class="modal fade" id="supplierModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">도매상 등록</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<form id="supplierForm">
<div class="mb-3">
<label class="form-label">도매상명 *</label>
<input type="text" class="form-control" id="supplierName" required>
</div>
<div class="mb-3">
<label class="form-label">사업자번호</label>
<input type="text" class="form-control" id="supplierBusinessNo" placeholder="000-00-00000">
</div>
<div class="mb-3">
<label class="form-label">담당자</label>
<input type="text" class="form-control" id="supplierContactPerson">
</div>
<div class="mb-3">
<label class="form-label">전화번호</label>
<input type="tel" class="form-control" id="supplierPhone">
</div>
<div class="mb-3">
<label class="form-label">주소</label>
<input type="text" class="form-control" id="supplierAddress">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">취소</button>
<button type="button" class="btn btn-primary" id="saveSupplierBtn">저장</button>
</div>
</div>
</div>
</div>
<!-- Scripts -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>