diff --git a/backend/app.py b/backend/app.py index 6d9815d..acfcd52 100644 --- a/backend/app.py +++ b/backend/app.py @@ -1387,7 +1387,68 @@ def admin_user_detail(user_id): print(f"[WARNING] MSSQL 조회 실패 (user {user_id}): {mssql_error}") purchases = [] - # 6. 응답 생성 + # 6. 조제 이력 조회 (전화번호 → CUSCODE → PS_main) + prescriptions = [] + pos_customer = None + if user['phone']: + try: + phone_clean = user['phone'].replace('-', '').replace(' ', '') + base_session = db_manager.get_session('PM_BASE') + pres_session = db_manager.get_session('PM_PRES') + + # 전화번호로 CUSCODE 조회 + cuscode_query = text(""" + SELECT TOP 1 CUSCODE, PANAME + FROM CD_PERSON + WHERE REPLACE(REPLACE(PHONE, '-', ''), ' ', '') = :phone + OR REPLACE(REPLACE(TEL_NO, '-', ''), ' ', '') = :phone + OR REPLACE(REPLACE(PHONE2, '-', ''), ' ', '') = :phone + """) + cus_row = base_session.execute(cuscode_query, {'phone': phone_clean}).fetchone() + + if cus_row: + cuscode = cus_row.CUSCODE + pos_customer = {'cuscode': cuscode, 'name': cus_row.PANAME} + + # 조제 이력 조회 + rx_query = text(""" + SELECT TOP 30 + P.PreSerial, P.Indate, P.Paname, P.Drname, P.OrderName, P.TDAYS + FROM PS_main P + WHERE P.CusCode = :cuscode + ORDER BY P.Indate DESC, P.PreSerial DESC + """) + rxs = pres_session.execute(rx_query, {'cuscode': cuscode}).fetchall() + + for rx in rxs: + # 처방 품목 조회 + items_query = text(""" + SELECT S.DrugCode, G.GoodsName, S.Days, S.QUAN, S.QUAN_TIME + FROM PS_sub_pharm S + LEFT JOIN PM_DRUG.dbo.CD_GOODS G ON S.DrugCode = G.DrugCode + WHERE S.PreSerial = :pre_serial + """) + items = pres_session.execute(items_query, {'pre_serial': rx.PreSerial}).fetchall() + + prescriptions.append({ + 'pre_serial': rx.PreSerial, + 'date': rx.Indate, + 'patient_name': rx.Paname, + 'doctor': rx.Drname, + 'hospital': rx.OrderName, + 'total_days': rx.TDAYS, + 'items': [{ + 'drug_code': item.DrugCode, + 'name': item.GoodsName or '알 수 없음', + 'days': float(item.Days) if item.Days else 0, + 'quantity': float(item.QUAN) if item.QUAN else 0, + 'times_per_day': float(item.QUAN_TIME) if item.QUAN_TIME else 0 + } for item in items] + }) + except Exception as rx_error: + logging.warning(f"조제 이력 조회 실패 (user {user_id}): {rx_error}") + + # 7. 응답 생성 return jsonify({ 'success': True, 'user': { @@ -1409,7 +1470,9 @@ def admin_user_detail(user_id): } for ml in mileage_history ], - 'purchases': purchases + 'purchases': purchases, + 'prescriptions': prescriptions, + 'pos_customer': pos_customer }) except Exception as e: diff --git a/backend/templates/admin.html b/backend/templates/admin.html index 8c29a67..91069ff 100644 --- a/backend/templates/admin.html +++ b/backend/templates/admin.html @@ -896,10 +896,13 @@
적립 이력이 없습니다.
'; } + html += ` + + + + `;