/*
 * st_feecase_dbio.pgc - MDR/VAN 수수료 분리 합계 (st_feecase_dbio), libstdbio.a.
 * ECPG; sum(CASE WHEN fee_type=... THEN amount ELSE 0 END) 형태로 한 SELECT 에서 두 합계.
 */
#include <string.h>
#include <userlog.h>
#include "st_feecase_dbio.h"

long stdb_mdr_vs_van(const char *bizdate, long *van_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_mdr = 0, h_van = 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(CASE WHEN fee_type = 'MDR' THEN amount ELSE 0 END), 0),
                    coalesce(sum(CASE WHEN fee_type = 'VAN' THEN amount ELSE 0 END), 0)
        INTO :h_mdr, :h_van FROM st_settle_dtl WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("stdb_mdr_vs_van FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    if (van_out) *van_out = h_van;
    return h_mdr;
}
