/*
 * VL_TOLERANCE.pgc - vl 모듈 서비스 VL_TOLERANCE (one service = one file).
 * 기능: 금액 허용오차(amount == fee+net ± tol) 검증 (RC 1=초과).
 * 기존 인라인 vl_svr.pgc에서 분리; caller의 XA 브랜치에서 실행(EXEC SQL CONNECT 없음).
 * vl_svr가 tpsvrinit에서 이 서비스를 tpadvertise 한다.
 */
#include "VL_TOLERANCE.h"

void VL_TOLERANCE(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    long rc = 0, tol, diff;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid, h_amount, h_fee, h_net;
    EXEC SQL END DECLARE SECTION;
    h_pid = getl(b, T_PURCHASE_ID);
    tol = getl(b, T_ARG1); if (tol < 0) tol = 0;
    EXEC SQL SELECT amount, fee, net INTO :h_amount, :h_fee, :h_net
        FROM purchase WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) { setl(b, T_RC, 3); OK(b); }
    diff = h_amount - (h_fee + h_net);
    if (diff < 0) diff = -diff;
    if (diff > tol) rc = 1;
    setl(b, T_DELTA, diff);
    setl(b, T_RC, rc);
    OK(b);
}
