/*
 * PAY_ADJUST.pgc - py 모듈 서비스 PAY_ADJUST (one service = one file).
 * 지급 조정: py_adjust 에 증감(delta)을 기록하고 지급건 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_ADJUST.h"

void PAY_ADJUST(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bizdate[16], reason[128];
    long payout_id, delta, adj;
    EXEC SQL BEGIN DECLARE SECTION;
    long e_pay, e_delta;
    EXEC SQL END DECLARE SECTION;
    payout_id = getl(b, T_ID1);
    delta = getl(b, T_DELTA);
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    gets_(b, T_REASON, reason, sizeof(reason)); if (reason[0] == 0) strcpy(reason, "ADJ");
    adj = pydb_insert_adjust(payout_id, delta, reason, bizdate);
    if (adj < 0) FAIL(b);
    e_pay = payout_id; e_delta = delta;
    EXEC SQL UPDATE py_payout SET net = net + :e_delta, updated_at = now()
        WHERE payout_id = :e_pay;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) FAIL(b);
    setl(b, T_ADJ, adj);
    OK(b);
}
