/*
 * ac_void_dbio.pgc - ac 모듈 DB 접근 함수 세트 (ac_void_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_void_dbio.h"

/* VOID 처리된 매입 중 원장 SETTLE 이 없는 건의 금액 합 (정합성 점검, NOT EXISTS). */
long acdb_void_unsettled_amount(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 coalesce(sum(c.amount),0) INTO :h_v FROM ac_cancel c
        WHERE c.biz_date = :h_bizdate AND c.cancel_type = 'VOID'
          AND NOT EXISTS (SELECT 1 FROM ledger l
                          WHERE l.purchase_id = c.purchase_id AND l.entry_type = 'SETTLE');
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
