/*
 * vl_ledger_query_dbio.pgc - 원장 합계 조회 (SETTLELINK) (libvldbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "vl_ledger_query_dbio.h"

long vldb_ledger_sum(long pid)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid = pid, h_sum;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT coalesce(sum(amount),0) INTO :h_sum FROM ledger
        WHERE purchase_id = :h_pid AND entry_type = 'SETTLELINK';
    if (sqlca.sqlcode < 0) { userlog("vldb_ledger_sum FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_sum;
}
