import pyodbc, sys
sys.stdout.reconfigure(encoding='utf-8')

conn = pyodbc.connect(
    r'DRIVER={ODBC Driver 17 for SQL Server};SERVER=192.168.0.4\PM2014;DATABASE=PM_PRES;UID=sa;PWD=tmddls214!%(;Encrypt=no;TrustServerCertificate=yes;'
)
cur = conn.cursor()

# 오늘 현금영수증 발행 건 확인
cur.execute("""
SELECT
    PRESERIAL,
    ETC_CASH, OTC_CASH, ETC_CARD, OTC_CARD,
    nCASHINMODE, nAPPROVAL_NUM, nCHK_GUBUN
FROM CD_SUNAB
WHERE INDATE = '20260225'
  AND nAPPROVAL_NUM IS NOT NULL AND nAPPROVAL_NUM != ''
ORDER BY PRESERIAL DESC
""")
rows = cur.fetchall()
print(f'=== 오늘 현금영수증 발행 건: {len(rows)}건 ===')
for r in rows:
    cash = (r[1] or 0) + (r[2] or 0)
    card = (r[3] or 0) + (r[4] or 0)
    pay = '카드' if card > 0 else '현금' if cash > 0 else '?'
    print(f'  주문={r[0]} | {pay} | 현금={cash:,} 카드={card:,} | 영수증모드={r[5]} | 승인번호={r[6]} | 구분={r[7]}')

# 오늘 전체 현금 결제 건 (영수증 무관)
cur.execute("""
SELECT COUNT(*) FROM CD_SUNAB
WHERE INDATE = '20260225'
  AND (ETC_CASH > 0 OR OTC_CASH > 0)
""")
r = cur.fetchone()
print(f'\n=== 오늘 현금 결제 건: {r[0]}건 ===')

# 오늘 nCASHINMODE가 있는 건 (영수증 입력 방식 있음)
cur.execute("""
SELECT nCASHINMODE, COUNT(*) as cnt
FROM CD_SUNAB
WHERE INDATE = '20260225'
  AND nCASHINMODE IS NOT NULL AND nCASHINMODE != ''
GROUP BY nCASHINMODE
""")
print(f'\n=== 오늘 nCASHINMODE 분포 ===')
for r in cur.fetchall():
    print(f'  모드={r[0]} → {r[1]}건')

conn.close()
