/*
 * au_capture_join_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_capture_join_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: au_authorization a JOIN au_cancel c ON a.auth_id=c.auth_id 집계 (매입확정 취소액).
 */
#include <string.h>
#include <userlog.h>
#include "au_capture_join_dbio.h"

/* 매입확정(M)/정산반영(S) 승인에 대한 취소 금액 합계 (조인 기준 취소측 amount). */
long audb_cancelled_capture_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 au_authorization a JOIN au_cancel c ON a.auth_id = c.auth_id
        WHERE c.biz_date = :h_bizdate AND a.status IN ('M', 'S');
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
