/*
 * lg_voucher_mix_dbio.pgc - lg 모듈 DB 접근 함수 (lg_voucher_mix_dbio), archived into liblgdbio.a.
 * 당일 전표 유형 가짓수와 역분개(REVERSE) 전표 수를 함께 산출한다.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_voucher_mix_dbio.h"

/* count DISTINCT + FILTER: 전표 유형 수와 역분개 전표 수. */
int lgqdb_voucher_type_mix(const char *bizdate, long *kinds, long *reverses)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_k = 0, h_r = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT count(DISTINCT voucher_type),
                    count(*) FILTER (WHERE voucher_type = 'REVERSE')
        INTO :h_k, :h_r FROM lg_voucher WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("lgqdb_voucher_type_mix FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    *kinds = h_k; *reverses = h_r;
    return 0;
}
