/*
 * VL_FEE.pgc - vl 모듈 서비스 VL_FEE (one service = one file).
 * 기능: 수수료 재계산 정합 검증 (저장 fee vs MDR 재계산, RC 1=불일치).
 * 기존 인라인 vl_svr.pgc에서 분리; caller의 XA 브랜치에서 실행(EXEC SQL CONNECT 없음).
 * vl_svr가 tpsvrinit에서 이 서비스를 tpadvertise 한다.
 */
#include "VL_FEE.h"

void VL_FEE(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    long rc = 0, recompute;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid, h_amount, h_fee, h_bps;
    char h_merch[64];
    EXEC SQL END DECLARE SECTION;
    h_pid = getl(b, T_PURCHASE_ID);
    EXEC SQL SELECT amount, fee, merchant_id INTO :h_amount, :h_fee, :h_merch
        FROM purchase WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) { setl(b, T_RC, 3); OK(b); }
    EXEC SQL SELECT mdr_bps INTO :h_bps FROM merchant WHERE merchant_id = :h_merch;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) h_bps = 25;
    recompute = acq_fee(h_amount, h_bps);
    if (recompute != h_fee) rc = 1;
    setl(b, T_FEE, recompute);
    setl(b, T_DELTA, h_fee - recompute);
    setl(b, T_RC, rc);
    OK(b);
}
