/*
 * lg_max_voucher_dbio.pgc - lg 모듈 DB 접근 함수 (lg_max_voucher_dbio), archived into liblgdbio.a.
 * 당일 최대 전표 ID.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_max_voucher_dbio.h"

/* 당일 채번된 최대 전표 ID (없으면 0). */
long lgqdb_max_voucher(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_max = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT coalesce(max(voucher_id), 0) INTO :h_max
        FROM lg_voucher WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("lgqdb_max_voucher FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_max;
}
