/*
 * PREAUTH.pgc - au 모듈 서비스 PREAUTH (one service = one file).
 * 예비승인(pre-authorization) 기록.
 * 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.h"

void PREAUTH(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char card[24], merch[64], bizdate[16];
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id, h_amt;
    char h_card[24], h_merch[64], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;
    gets_(b, T_KEY1, card, sizeof(card));
    gets_(b, T_MERCHANT, merch, sizeof(merch));
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    h_amt = getl(b, T_AMOUNT);
    strncpy(h_card, card, sizeof(h_card)-1); h_card[sizeof(h_card)-1] = 0;
    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 nextval('au_preauth_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) FAIL(b);
    EXEC SQL INSERT INTO au_preauth (preauth_id, card_no, merchant_id, amount, status, biz_date)
        VALUES (:h_id, :h_card, :h_merch, :h_amt, 'P', :h_bizdate);
    if (sqlca.sqlcode < 0) FAIL(b);
    setl(b, T_ID2, h_id);
    OK(b);
}
