/*
 * mm_acct_dbio.pgc - 정산계좌 조회 + 미설정 수 (libmmdbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "mm_acct_dbio.h"

int mmdb_settle_acct(const char *merch, char *acct_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch[64], h_acct[64];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_merch, merch, sizeof(h_merch)-1); h_merch[sizeof(h_merch)-1] = 0;
    EXEC SQL SELECT coalesce(settle_acct, '') INTO :h_acct FROM mm_merchant_ext
        WHERE merchant_id = :h_merch;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) { userlog("mmdb_settle_acct FAIL [%d]", sqlca.sqlcode); return -1; }
    strncpy(acct_out, h_acct, 63); acct_out[63] = 0;
    return 0;
}

long mmdb_missing_acct_count(void)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cnt;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL SELECT count(*) INTO :h_cnt FROM mm_merchant_ext
        WHERE (settle_acct IS NULL OR settle_acct = '') AND status = 'ACTIVE';
    if (sqlca.sqlcode < 0) { userlog("mmdb_missing_acct_count FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_cnt;
}
