/*
 * lg_month_debit_dbio.pgc - lg 모듈 DB 접근 함수 (lg_month_debit_dbio), archived into liblgdbio.a.
 * 월(YYYY-MM) 계정 차변 합계.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_month_debit_dbio.h"

/* 월 프리픽스로 계정 차변 누계. */
long lgqdb_month_debit(const char *acct, const char *month)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_acct[32], h_month[16];
    long h_dr = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_acct, acct, sizeof(h_acct)-1); h_acct[sizeof(h_acct)-1] = 0;
    strncpy(h_month, month, sizeof(h_month)-1); h_month[sizeof(h_month)-1] = 0;
    EXEC SQL SELECT coalesce(sum(amount),0) INTO :h_dr FROM lg_ledger_line
        WHERE acct = :h_acct AND dc = 'D' AND to_char(biz_date,'YYYY-MM') = :h_month;
    if (sqlca.sqlcode < 0) { userlog("lgqdb_month_debit FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_dr;
}
