/*
 * cl_period_sum_dbio.pgc - cl 모듈 DB 접근 함수 (cl_period_sum_dbio), archived into libcldbio.a.
 * 기간 프리픽스 마감 총액 합계.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "cl_period_sum_dbio.h"

/* period_key 프리픽스로 마감 total_amount 누계. */
long clqdb_period_sum(const char *prefix)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_prefix[16];
    long h_amt = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_prefix, prefix, sizeof(h_prefix)-1); h_prefix[sizeof(h_prefix)-1] = 0;
    EXEC SQL SELECT coalesce(sum(total_amount),0) INTO :h_amt FROM cl_close_log
        WHERE period_key LIKE :h_prefix || '%' AND status <> 'X';
    if (sqlca.sqlcode < 0) { userlog("clqdb_period_sum FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_amt;
}
