/*
 * ACQ_RELEASE.pgc - ac 모듈 서비스 ACQ_RELEASE (one service = one file).
 * 보류 해제: 직전 상태를 조회한 뒤 H->A 전이, 이전 상태를 함께 회신.
 * Runs on the caller's XA branch (no EXEC SQL CONNECT). Advertised by ac_svr.
 */
#include "ACQ_RELEASE.h"

void ACQ_RELEASE(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid, h_aff;
    char h_prev[8];
    EXEC SQL END DECLARE SECTION;

    h_pid = getl(b, T_PURCHASE_ID);
    EXEC SQL SELECT status INTO :h_prev FROM purchase WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) FAIL(b);
    /* release only from HOLD; report whether the transition actually fired */
    EXEC SQL UPDATE purchase SET status = 'A', updated_at = now()
        WHERE purchase_id = :h_pid AND status = 'H';
    if (sqlca.sqlcode < 0) FAIL(b);
    h_aff = sqlca.sqlerrd[2];
    Bchg(b, T_STATUS, 0, h_aff > 0 ? "A" : h_prev, 0L);
    setl(b, T_RC, h_aff > 0 ? 0 : 1);
    OK(b);
}
