/*
 * ST_DIFFCHK.pgc - st 모듈 서비스 ST_DIFFCHK (one service = one file).
 * Moved verbatim from the former inline st_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by st_svr at tpsvrinit.
 */
#include "ST_DIFFCHK.h"

/* 18. ST_DIFFCHK - 정산 net vs 매입 net 차이 점검. */
void ST_DIFFCHK(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid, h_snet, h_pnet, h_diff;
    EXEC SQL END DECLARE SECTION;
    h_pid = getl(b, T_PURCHASE_ID);
    EXEC SQL SELECT coalesce(sum(net),0) INTO :h_snet FROM settlement WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0) FAIL(b);
    EXEC SQL SELECT net INTO :h_pnet FROM purchase WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) FAIL(b);
    h_diff = h_pnet - h_snet;
    setl(b, T_DELTA, h_diff);
    setl(b, T_RC, h_diff == 0 ? 0 : 1);
    OK(b);
}

/* vim: set ts=4 sw=4 et smartindent: */
