/*
 * st_statusfilter_dbio.pgc - 지급 상태 구성 (st_statusfilter_dbio), libstdbio.a.
 * ECPG; count(*) FILTER (WHERE status=...) 3종으로 HELD/PAID/CANCELLED 를 동시에 집계.
 */
#include <string.h>
#include <userlog.h>
#include "st_statusfilter_dbio.h"

long stdb_pay_status_mix(const char *bizdate, long *paid_out, long *cancelled_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_held = 0, h_paid = 0, h_cancel = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT count(*) FILTER (WHERE status = 'HELD'),
                    count(*) FILTER (WHERE status = 'PAID'),
                    count(*) FILTER (WHERE status = 'CANCELLED')
        INTO :h_held, :h_paid, :h_cancel FROM st_payment WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("stdb_pay_status_mix FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    if (paid_out) *paid_out = h_paid;
    if (cancelled_out) *cancelled_out = h_cancel;
    return h_held;
}
