/*
 * lg_reverse_pair_dbio.pgc - lg 모듈 DB 접근 함수 (lg_reverse_pair_dbio), archived into liblgdbio.a.
 * 자신을 참조하는 역분개(REVERSE) 전표가 존재하는 전기 전표 수를 EXISTS 로 센다.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_reverse_pair_dbio.h"

/* EXISTS 상관: REVERSE 로 상쇄된 POST 전표 수. */
long lgqdb_reversed_posts(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_cnt = 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(*) INTO :h_cnt FROM lg_voucher v
        WHERE v.voucher_type = 'POST' AND v.biz_date = :h_bizdate
          AND EXISTS (SELECT 1 FROM lg_voucher r
                      WHERE r.voucher_type = 'REVERSE' AND r.ref_id = v.voucher_id);
    if (sqlca.sqlcode < 0) { userlog("lgqdb_reversed_posts FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_cnt;
}
