/*
 * ac_unmatch_query_dbio.pgc - ac 모듈 DB 접근 함수 세트 (ac_unmatch_query_dbio), archived into libacdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "ac_unmatch_query_dbio.h"

/* 당일 이하 미매입 중 정산이 안 잡힌 누적 건수 (aging, NOT EXISTS settlement). */
long acdb_unmatch_backlog(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_v = 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_v FROM ac_unmatch u
        WHERE u.biz_date <= :h_bizdate
          AND NOT EXISTS (SELECT 1 FROM settlement s WHERE s.purchase_id = u.purchase_id);
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
