/*
 * ac_settle_notexists_dbio.pgc - ac 모듈 DB 접근 함수 세트 (ac_settle_notexists_dbio), archived into libacdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: NOT EXISTS 상관 서브쿼리 - 대사완료(M) 이나 정산행이 없는 매입.
 */
#include <string.h>
#include <userlog.h>
#include "ac_settle_notexists_dbio.h"

/* 상태가 대사완료(M) 이지만 settlement 에 대응행이 없는 매입 건수. */
long acdb_unsettled_count(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 purchase p
        WHERE p.biz_date = :h_bizdate AND p.status = 'M'
          AND NOT EXISTS (SELECT 1 FROM settlement s WHERE s.purchase_id = p.purchase_id);
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
