/*
 * RC_APPR_ACQ.pgc - rc 모듈 서비스 RC_APPR_ACQ (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_APPR_ACQ.h"

/* 2. RC_APPR_ACQ - 승인-매입 대사 (승인 존재 여부 확인 후 대사 로그). */
void RC_APPR_ACQ(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bizdate[16];
    long mid;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid, h_cnt, h_net;
    EXEC SQL END DECLARE SECTION;

    h_pid = getl(b, T_PURCHASE_ID);
    h_net = getl(b, T_NET);
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    EXEC SQL SELECT count(*) INTO :h_cnt FROM approval WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0) FAIL(b);
    if (h_cnt > 0)
        mid = rcdb_insert_match(h_pid, "APPR_ACQ", h_net, "MATCHED", bizdate);
    else
        mid = rcdb_insert_match(h_pid, "APPR_ACQ", h_net, "EXCEPTION", bizdate);
    if (mid < 0) FAIL(b);
    setl(b, T_ID1, mid);
    setl(b, T_RC, h_cnt > 0 ? 0 : 1);
    OK(b);
}

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