/*
 * MG_SEND.pgc - mg 모듈 서비스 MG_SEND (one service = one file).
 * Split out of the former inline mg_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by mg_svr at tpsvrinit.
 */
#include "MG_SEND.h"

/* 2. MG_SEND - 전문 송신 (direction=OUT). */
void MG_SEND(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char chan[16], mti[8], raw[512], bizdate[16];
    long stan, msg_id, amount;

    gets_(b, T_CHANNEL, chan, sizeof(chan)); if (chan[0] == 0) strcpy(chan, "CH01");
    gets_(b, T_STR1, mti, sizeof(mti));      if (mti[0] == 0) strcpy(mti, "0210");
    gets_(b, T_STR3, raw, sizeof(raw));
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    amount = getl(b, T_AMOUNT);

    stan = mgdb_next_stan();       if (stan < 0) FAIL(b);
    msg_id = mgdb_next_msg_id();   if (msg_id < 0) FAIL(b);
    if (mgdb_insert_msg(msg_id, stan, mti, chan, "OUT", "000000", "", amount,
                        "", "00", raw, "S", bizdate) < 0) FAIL(b);
    setl(b, T_ARG1, stan);
    setl(b, T_ID1, msg_id);
    OK(b);
}
