/*
 * ACQ_EDI.pgc - ac 모듈 서비스 ACQ_EDI (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_EDI.h"

void ACQ_EDI(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char merch[64], docno[32], bizdate[16];
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id, h_amount;
    char h_merch[64], h_doc[32], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

    gets_(b, T_MERCHANT, merch, sizeof(merch));
    gets_(b, T_DOCNO, docno, sizeof(docno)); if (docno[0] == 0) strcpy(docno, "EDI-AUTO");
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    h_amount = getl(b, T_AMOUNT);
    strncpy(h_merch, merch, sizeof(h_merch)-1); h_merch[sizeof(h_merch)-1] = 0;
    strncpy(h_doc, docno, sizeof(h_doc)-1); h_doc[sizeof(h_doc)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;

    EXEC SQL SELECT nextval('ac_edi_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) FAIL(b);
    EXEC SQL INSERT INTO ac_edi (edi_id, merchant_id, doc_no, amount, biz_date)
        VALUES (:h_id, :h_merch, :h_doc, :h_amount, :h_bizdate);
    if (sqlca.sqlcode < 0) { userlog("ACQ_EDI INSERT FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); FAIL(b); }
    OK(b);
}
