/*
 * st_confirmratio_dbio.pgc - st 모듈 DB 접근 함수 세트 (st_confirmratio_dbio), archived into libstdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 확정율(%) - CASE 로 SETTLED+CONFIRMED 분모 방지.
 */
#include <string.h>
#include <userlog.h>
#include "st_confirmratio_dbio.h"

long stdb_confirm_pct(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_conf = 0, h_all = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT
        count(*) FILTER (WHERE status = 'CONFIRMED'), count(*)
        INTO :h_conf, :h_all FROM settlement WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("stdb_confirm_pct FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_all > 0 ? (h_conf * 100 / h_all) : 0;
}
