feat(drug-usage): 단위 마스터 + 총사용량 표시 + 순차 API 호출
- drug_unit.py: SUNG_CODE 기반 단위 판별 함수 추가 - 조제 상세에 총사용량 + 단위 표시 (예: 1,230정) - API 순차 호출로 DB 세션 충돌 방지
This commit is contained in:
@@ -8905,6 +8905,8 @@ def api_drug_usage_imports(drug_code):
|
||||
@app.route('/api/drug-usage/<drug_code>/prescriptions')
|
||||
def api_drug_usage_prescriptions(drug_code):
|
||||
"""약품별 조제(매출) 상세 API"""
|
||||
from utils.drug_unit import get_drug_unit
|
||||
|
||||
start_date = request.args.get('start_date', '')
|
||||
end_date = request.args.get('end_date', '')
|
||||
|
||||
@@ -8913,6 +8915,16 @@ def api_drug_usage_prescriptions(drug_code):
|
||||
|
||||
try:
|
||||
pres_session = db_manager.get_session('PM_PRES')
|
||||
drug_session = db_manager.get_session('PM_DRUG')
|
||||
|
||||
# 약품 정보 조회 (단위 판별용)
|
||||
drug_info = drug_session.execute(text("""
|
||||
SELECT GoodsName, SUNG_CODE FROM CD_GOODS WHERE DrugCode = :drug_code
|
||||
"""), {'drug_code': drug_code}).fetchone()
|
||||
|
||||
goods_name = drug_info.GoodsName if drug_info else ''
|
||||
sung_code = drug_info.SUNG_CODE if drug_info else ''
|
||||
unit = get_drug_unit(goods_name, sung_code)
|
||||
|
||||
result = pres_session.execute(text("""
|
||||
SELECT
|
||||
@@ -8934,12 +8946,15 @@ def api_drug_usage_prescriptions(drug_code):
|
||||
items = []
|
||||
seen_patients = set()
|
||||
recent_patients = [] # 최근 조제받은 환자 (중복 제외, 최대 3명)
|
||||
total_usage = 0 # 총 사용량
|
||||
|
||||
for row in result:
|
||||
dosage = float(row.dosage) if row.dosage else 0
|
||||
freq = float(row.frequency) if row.frequency else 0
|
||||
days = int(row.days) if row.days else 0
|
||||
patient = row.patient_name or ''
|
||||
qty = dosage * freq * days
|
||||
total_usage += qty
|
||||
|
||||
# 중복 제외 환자 목록 (최근순, 최대 3명)
|
||||
if patient and patient not in seen_patients:
|
||||
@@ -8955,7 +8970,7 @@ def api_drug_usage_prescriptions(drug_code):
|
||||
'dosage': dosage,
|
||||
'frequency': freq,
|
||||
'days': days,
|
||||
'total_qty': dosage * freq * days
|
||||
'total_qty': qty
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
@@ -8964,6 +8979,8 @@ def api_drug_usage_prescriptions(drug_code):
|
||||
'total_count': len(items),
|
||||
'unique_patients': len(seen_patients),
|
||||
'recent_patients': recent_patients,
|
||||
'total_usage': total_usage,
|
||||
'unit': unit,
|
||||
'items': items
|
||||
})
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user