/*
 * py_payout_subq_dbio.pgc - py 모듈 DB 접근 함수 세트 (py_payout_subq_dbio), archived into libpydbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "py_payout_subq_dbio.h"

/* 서브쿼리: 해당 실행일에 예약(S)된 지급건들의 순액 합. */
long pydb_payout_scheduled_sum(const char *rundate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_rundate[16];
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_rundate, rundate, sizeof(h_rundate)-1); h_rundate[sizeof(h_rundate)-1] = 0;
    EXEC SQL SELECT coalesce(sum(net),0) INTO :h_v FROM py_payout
        WHERE payout_id IN (SELECT payout_id FROM py_schedule
                            WHERE run_date = :h_rundate AND status = 'S');
    if (sqlca.sqlcode < 0) { userlog("pydb_payout_scheduled_sum FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}
