/*
 * lg_line_span_dbio.pgc - lg 모듈 DB 접근 함수 (lg_line_span_dbio), archived into liblgdbio.a.
 * 계정 x 영업일 분개액의 최소/최대를 한 번에 조회한다.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_line_span_dbio.h"

/* min/max: 계정 분개액 범위를 out 파라미터로 반환. */
int lgqdb_line_amount_span(const char *acct, const char *bizdate, long *mn, long *mx)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_acct[32], h_bizdate[16];
    long h_mn = 0, h_mx = 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(min(amount),0), coalesce(max(amount),0)
        INTO :h_mn, :h_mx FROM lg_ledger_line
        WHERE acct = :h_acct AND biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("lgqdb_line_amount_span FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    *mn = h_mn; *mx = h_mx;
    return 0;
}
