/*
 * st_feepct_dbio.pgc - st 모듈 DB 접근 함수 세트 (st_feepct_dbio), archived into libstdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 수수료율(%) = 수수료합/매입금액합 - 두 서브쿼리 비율.
 */
#include <string.h>
#include <userlog.h>
#include "st_feepct_dbio.h"

long stdb_fee_pct_of_gross(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_fee = 0, h_gross = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT
        (SELECT coalesce(sum(fee),0) FROM purchase WHERE biz_date = :h_bizdate),
        (SELECT coalesce(sum(amount),0) FROM purchase WHERE biz_date = :h_bizdate)
        INTO :h_fee, :h_gross;
    if (sqlca.sqlcode < 0) { userlog("stdb_fee_pct_of_gross FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_gross > 0 ? (h_fee * 10000 / h_gross) : 0;
}
