/*
 * ST_RATEAPPLY.pgc - st 모듈 서비스 ST_RATEAPPLY (one service = one file).
 * Moved verbatim from the former inline st_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by st_svr at tpsvrinit.
 */
#include "ST_RATEAPPLY.h"

/* 20. ST_RATEAPPLY - 구간요율 적용 수수료 계산 + 정산 상세 기록 (dbio). */
void ST_RATEAPPLY(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char merch[64], bizdate[16], ftype[16];
    long sid = getl(b, T_SETTLE_ID), pid = getl(b, T_PURCHASE_ID);
    long amount = getl(b, T_AMOUNT), bps, fee, dtl;
    gets_(b, T_MERCHANT, merch, sizeof(merch));
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    gets_(b, T_STR1, ftype, sizeof(ftype)); if (ftype[0] == 0) strcpy(ftype, "MDR");
    bps = stdb_lookup_rate(ftype, amount);
    if (bps < 0) FAIL(b);
    fee = acq_fee(amount, bps);
    dtl = stdb_insert_fee_dtl(sid, pid, merch, ftype, fee, bizdate);
    if (dtl < 0) FAIL(b);
    setl(b, T_FEE, fee);
    setl(b, T_RC, bps);
    OK(b);
}

/* vim: set ts=4 sw=4 et smartindent: */
