/*
 * lg_acct_asof_dbio.pgc - lg 모듈 DB 접근 함수 (lg_acct_asof_dbio), archived into liblgdbio.a.
 * 계정과목 잔액 조회 (as-of, account_bal).
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_acct_asof_dbio.h"

/* 계정 잔액/차변/대변 3개 반환 (account_bal). */
int lgqdb_acct_asof(const char *acct, const char *bizdate, long *bal, long *dr, long *cr)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_acct[32], h_bizdate[16];
    long h_bal, h_dr, h_cr;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_acct, acct, sizeof(h_acct)-1); h_acct[sizeof(h_acct)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT balance, debit_total, credit_total INTO :h_bal, :h_dr, :h_cr
        FROM lg_account_bal WHERE acct = :h_acct AND biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) { userlog("lgqdb_acct_asof miss [%d]", sqlca.sqlcode); return -1; }
    *bal = h_bal; *dr = h_dr; *cr = h_cr;
    return 0;
}
