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

void ACQ_DDC(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bizdate[16], ccy[8];
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id, h_pid, h_fx, h_krw, h_rate;
    char h_ccy[8], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

    h_pid = getl(b, T_PURCHASE_ID);
    h_fx  = getl(b, T_FX_AMT);
    gets_(b, T_CCY, ccy, sizeof(ccy)); if (ccy[0] == 0) strcpy(ccy, "USD");
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    h_rate = 1350;                       /* 13.50 KRW per unit (bps of 100) */
    h_krw  = h_fx * h_rate / 100;
    strncpy(h_ccy, ccy, sizeof(h_ccy)-1); h_ccy[sizeof(h_ccy)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;

    EXEC SQL SELECT nextval('ac_fx_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) FAIL(b);
    EXEC SQL INSERT INTO ac_fx (fx_id, purchase_id, ccy, fx_amount, krw_amount, rate_bps, biz_date)
        VALUES (:h_id, :h_pid, :h_ccy, :h_fx, :h_krw, :h_rate, :h_bizdate);
    if (sqlca.sqlcode < 0) { userlog("ACQ_DDC INSERT FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); FAIL(b); }
    setl(b, T_AMOUNT, h_krw);
    OK(b);
}
