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

/* 22. RC_SUMMARY - 대사 요약 upsert (dbio). */
void RC_SUMMARY(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bizdate[16];
    long matched, unmatched;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_gross;
    char h_bizdate[16];
    EXEC SQL END DECLARE SECTION;
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    matched = rcdb_count_match(bizdate, "MATCHED");
    unmatched = rcdb_count_unmatched(bizdate);
    if (matched < 0 || unmatched < 0) FAIL(b);
    EXEC SQL SELECT coalesce(sum(net),0) INTO :h_gross FROM purchase WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) FAIL(b);
    if (rcdb_upsert_summary(bizdate, matched, unmatched, h_gross) < 0) FAIL(b);
    setl(b, T_COUNT, matched);
    setl(b, T_GROSS, h_gross);
    OK(b);
}

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