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

void ACQ_LIMITCHK(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char merch[64], bizdate[16];
    EXEC SQL BEGIN DECLARE SECTION;
    long h_sum, h_lim;
    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 coalesce(sum(amount),0) INTO :h_sum FROM purchase
        WHERE merchant_id = :h_merch AND biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) FAIL(b);
    EXEC SQL SELECT daily_limit INTO :h_lim FROM merchant WHERE merchant_id = :h_merch;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) FAIL(b);
    setl(b, T_GROSS, h_sum);
    setl(b, T_RC, h_sum > h_lim ? 1 : 0);
    OK(b);
}
