/*
 * PAY_APPROVE.pgc - py 모듈 서비스 PAY_APPROVE (one service = one file).
 * 지급 승인: 금액을 먼저 조회한 뒤 요청(R) 상태만 승인(A)으로 전이하고 금액을
 * 함께 회신. SELECT amount + guarded UPDATE(status='R') + affected-count.
 * Runs on the caller's XA branch (no EXEC SQL CONNECT). Advertised by py_svr.
 */
#include "PAY_APPROVE.h"

void PAY_APPROVE(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pay, h_amt, h_aff;
    EXEC SQL END DECLARE SECTION;

    h_pay = getl(b, T_ID1);
    EXEC SQL SELECT coalesce(amount,0) INTO :h_amt FROM py_payout WHERE payout_id = :h_pay;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) FAIL(b);
    EXEC SQL UPDATE py_payout SET status = 'A', updated_at = now()
        WHERE payout_id = :h_pay AND status = 'R';
    if (sqlca.sqlcode < 0) FAIL(b);
    h_aff = sqlca.sqlerrd[2];
    setl(b, T_AMOUNT, h_amt);
    if (h_aff > 0) Bchg(b, T_STATUS, 0, "A", 0L);
    setl(b, T_RC, h_aff > 0 ? 0 : 1);
    OK(b);
}
