/*
 * cl_snap_join_dbio.pgc - cl 모듈 DB 접근 함수 (cl_snap_join_dbio), archived into libcldbio.a.
 * 완료 마감(JOIN)에 속한 가맹점 gross 합계.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "cl_snap_join_dbio.h"

/* cl_snapshot 을 cl_close_log 와 조인해 status='C' 마감의 가맹점 gross 합. */
long clqdb_snap_join_gross(const char *merch)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch[64];
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_merch, merch, sizeof(h_merch)-1); h_merch[sizeof(h_merch)-1] = 0;
    EXEC SQL SELECT coalesce(sum(s.gross_amount),0) INTO :h_v
        FROM cl_snapshot s JOIN cl_close_log c ON s.close_id = c.close_id
        WHERE c.status = 'C' AND s.merchant_id = :h_merch;
    if (sqlca.sqlcode < 0) { userlog("clqdb_snap_join_gross FAIL [%d]", sqlca.sqlcode); return -1; }
    return h_v;
}
