/*
 * au_merchant_join_dbio.pgc - au 모듈 DB 접근 함수 세트 (au_merchant_join_dbio), archived into libaudbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: 2-table JOIN aggregate (승인원장 x 승인집계 조인 총액).
 */
#include <string.h>
#include <userlog.h>
#include "au_merchant_join_dbio.h"

/* au_summary 에 집계행이 존재하는 가맹점의 당일 승인 총액을 조인으로 집계. */
long audb_merchant_reconciled_gross(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(a.amount),0) INTO :h_v
        FROM au_authorization a
        JOIN au_summary s ON a.merchant_id = s.merchant_id AND a.biz_date = s.biz_date
        WHERE a.biz_date = :h_bizdate AND a.status = 'A';
    if (sqlca.sqlcode < 0) { userlog("audb_merchant_reconciled_gross FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}
