/*
 * PREAUTH_COMPLETE.pgc - au 모듈 서비스 PREAUTH_COMPLETE (one service = one file).
 * 예비승인 확정: preauth 소진 + 본승인 생성.
 * Moved verbatim from the former inline au_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by au_svr at tpsvrinit.
 */
#include "PREAUTH_COMPLETE.h"

void PREAUTH_COMPLETE(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bizdate[16];
    long auth_id;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pre, h_amt;
    char h_card[24], h_merch[64], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;
    h_pre = getl(b, T_ID2);
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;

    EXEC SQL SELECT card_no, merchant_id, amount INTO :h_card, :h_merch, :h_amt
        FROM au_preauth WHERE preauth_id = :h_pre;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) FAIL(b);
    EXEC SQL UPDATE au_preauth SET status = 'C' WHERE preauth_id = :h_pre;
    if (sqlca.sqlcode < 0) FAIL(b);
    auth_id = audb_next_auth_id();
    if (auth_id < 0) FAIL(b);
    if (audb_insert_auth(auth_id, h_card, h_merch, h_amt, "PRECMP", "PREAUTH", "A", bizdate) < 0) FAIL(b);
    setl(b, T_ID1, auth_id);
    OK(b);
}
