/*
 * ACQ_AMTFIX.pgc - ac 모듈 서비스 ACQ_AMTFIX (one service = one file).
 * 금액 확정 정정: 직전 금액 조회 후 하우스 25bps 로 절대 재계산, 증감(delta) 회신.
 * Runs on the caller's XA branch (no EXEC SQL CONNECT). Advertised by ac_svr.
 */
#include "ACQ_AMTFIX.h"

void ACQ_AMTFIX(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    long amount, fee, net;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid, h_old = 0, h_amount, h_fee, h_net;
    EXEC SQL END DECLARE SECTION;

    h_pid = getl(b, T_PURCHASE_ID);
    amount = getl(b, T_AMOUNT);
    /* absolute fix: read prior amount, recompute fee at flat house 25bps (no MDR lookup) */
    EXEC SQL SELECT amount INTO :h_old FROM purchase WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) FAIL(b);
    fee = acq_fee(amount, 25);
    net = amount - fee;
    h_amount = amount; h_fee = fee; h_net = net;
    EXEC SQL UPDATE purchase SET amount = :h_amount, fee = :h_fee, net = :h_net, updated_at = now()
        WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) FAIL(b);
    setl(b, T_DELTA, amount - h_old);
    setl(b, T_FEE, fee);
    setl(b, T_NET, net);
    OK(b);
}
