/*
 * PAY_NETOFF.pgc - py 모듈 서비스 PAY_NETOFF (one service = one file).
 * 상계(net-off): 지급건의 net 을 조회해 차감액을 반영하고 갱신.
 * Moved verbatim from the former inline py_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by py_svr at tpsvrinit.
 */
#include "PAY_NETOFF.h"

void PAY_NETOFF(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pay, h_off, h_new;
    EXEC SQL END DECLARE SECTION;
    h_pay = getl(b, T_ID1);
    h_off = getl(b, T_AMOUNT);
    EXEC SQL SELECT net INTO :h_new FROM py_payout WHERE payout_id = :h_pay;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) FAIL(b);
    h_new = h_new - h_off;
    EXEC SQL UPDATE py_payout SET net = :h_new, updated_at = now() WHERE payout_id = :h_pay;
    if (sqlca.sqlcode < 0) FAIL(b);
    setl(b, T_NET, h_new);
    OK(b);
}
