/*
 * PAY_ADVANCE.pgc - py 모듈 서비스 PAY_ADVANCE (one service = one file).
 * 선지급(advance): 선지급 수수료(1%)를 공제한 지급건(ADVANCE 채널)을 생성.
 * Moved verbatim from the former inline py_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by py_svr at tpsvrinit.
 */
#include "PAY_ADVANCE.h"

void PAY_ADVANCE(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char merch[64], acct[32], bizdate[16];
    long amount, fee, net, payout_id;
    gets_(b, T_MERCHANT, merch, sizeof(merch));
    gets_(b, T_KEY1, acct, sizeof(acct));
    amount = getl(b, T_AMOUNT);
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    fee = acq_fee(amount, 100);          /* 선지급 수수료 1% */
    net = amount - fee;
    payout_id = pydb_next_payout_id();
    if (payout_id < 0) FAIL(b);
    if (pydb_insert_payout(payout_id, merch, acct, amount, fee, net, "A", "ADVANCE", bizdate) < 0) FAIL(b);
    setl(b, T_ID1, payout_id);
    setl(b, T_FEE, fee);
    setl(b, T_NET, net);
    OK(b);
}
