/*
 * st_joingross_dbio.pgc - st 모듈 DB 접근 함수 세트 (st_joingross_dbio), archived into libstdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 정산-매입 JOIN 총 매입금액(gross) 합계.
 */
#include <string.h>
#include <userlog.h>
#include "st_joingross_dbio.h"

long stdb_settle_gross_join(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(p.amount),0) INTO :h_v
        FROM settlement s JOIN purchase p ON p.purchase_id = s.purchase_id
        WHERE s.biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("stdb_settle_gross_join FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_v;
}
