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

/* 3. RC_ACQ_DEP - 매입-입금(원장) 대사: 원장 합계와 net 비교. */
void RC_ACQ_DEP(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bizdate[16];
    long mid;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid, h_net, h_dep;
    EXEC SQL END DECLARE SECTION;

    h_pid = getl(b, T_PURCHASE_ID);
    h_net = getl(b, T_NET);
    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    EXEC SQL SELECT coalesce(sum(amount),0) INTO :h_dep FROM ledger
        WHERE purchase_id = :h_pid AND entry_type IN ('RECON','SETTLE');
    if (sqlca.sqlcode < 0) FAIL(b);
    mid = rcdb_insert_match(h_pid, "ACQ_DEP", h_dep,
                            (h_dep == h_net) ? "MATCHED" : "EXCEPTION", bizdate);
    if (mid < 0) FAIL(b);
    setl(b, T_GROSS, h_dep);
    setl(b, T_RC, (h_dep == h_net) ? 0 : 1);
    OK(b);
}

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