/*
 * rc_ledgerfilter_dbio.pgc - rc 모듈 DB 접근 함수 세트 (rc_ledgerfilter_dbio), archived into librcdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 원장 RECON/SETTLE FILTER 분리 집계 - RECON 합 반환, SETTLE 는 로그.
 */
#include <string.h>
#include <userlog.h>
#include "rc_ledgerfilter_dbio.h"

long rcdb_ledger_recon_filter(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_recon = 0, h_settle = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT
        coalesce(sum(amount) FILTER (WHERE entry_type = 'RECON'), 0),
        coalesce(sum(amount) FILTER (WHERE entry_type = 'SETTLE'), 0)
        INTO :h_recon, :h_settle FROM ledger WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    userlog("rcdb_ledger_recon_filter settle=%ld", h_settle);
    return h_recon;
}
