/*
 * lg_credit_sum_dbio.pgc - lg 모듈 DB 접근 함수 (lg_credit_sum_dbio), archived into liblgdbio.a.
 * 계정과목 x 영업일 대변(C) 합계.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_credit_sum_dbio.h"

/* 계정 x 일자 대변합 (FILTER 사용). */
long lgqdb_credit_sum(const char *acct, const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_acct[32], h_bizdate[16];
    long h_cr = 0;
    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 coalesce(sum(amount) FILTER (WHERE dc = 'C'), 0) INTO :h_cr
        FROM lg_ledger_line WHERE acct = :h_acct AND biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("lgqdb_credit_sum FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    if (h_cr < 0) return -1;
    return h_cr;
}
