/*
 * PAY_MERCHSUM.pgc - py 모듈 서비스 PAY_MERCHSUM (one service = one file).
 * 가맹점 당일 지급 집계: py_payout 집계 후 dbio 로 py_summary upsert.
 * Moved verbatim from the former inline py_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by py_svr at tpsvrinit.
 */
#include "PAY_MERCHSUM.h"

void PAY_MERCHSUM(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char merch[64], bizdate[16];
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cnt, h_amt;
    char h_merch[64], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;
    gets_(b, T_MERCHANT, merch, sizeof(merch));
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    strncpy(h_merch, merch, sizeof(h_merch)-1); h_merch[sizeof(h_merch)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT count(*), coalesce(sum(net),0) INTO :h_cnt, :h_amt
        FROM py_payout WHERE merchant_id = :h_merch AND biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) FAIL(b);
    if (pydb_upsert_summary(merch, bizdate, h_cnt, h_amt) < 0) FAIL(b);
    setl(b, T_COUNT, h_cnt);
    setl(b, T_GROSS, h_amt);
    OK(b);
}
