/*
 * ST_MONTHLY.pgc - st 모듈 서비스 ST_MONTHLY (one service = one file).
 * Moved verbatim from the former inline st_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by st_svr at tpsvrinit.
 */
#include "ST_MONTHLY.h"

/* 16. ST_MONTHLY - 월별 정산 net 합계 (biz_date LIKE 'YYYY-MM%'). */
void ST_MONTHLY(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char merch[64], ym[16], like[16];
    EXEC SQL BEGIN DECLARE SECTION;
    long h_sum;
    char h_merch[64], h_like[16];
    EXEC SQL END DECLARE SECTION;
    gets_(b, T_MERCHANT, merch, sizeof(merch));
    gets_(b, T_KEY1, ym, sizeof(ym)); if (ym[0] == 0) strcpy(ym, "2026-07");
    snprintf(like, sizeof(like), "%s%%", ym);
    strncpy(h_merch, merch, sizeof(h_merch)-1); h_merch[sizeof(h_merch)-1] = 0;
    strncpy(h_like, like, sizeof(h_like)-1); h_like[sizeof(h_like)-1] = 0;
    EXEC SQL SELECT coalesce(sum(net),0) INTO :h_sum FROM settlement
        WHERE merchant_id = :h_merch AND to_char(biz_date, 'YYYY-MM-DD') LIKE :h_like;
    if (sqlca.sqlcode < 0) FAIL(b);
    setl(b, T_GROSS, h_sum);
    OK(b);
}

/* vim: set ts=4 sw=4 et smartindent: */
