/*
 * RECONCILE.pgc - rc 모듈 서비스 RECONCILE (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 "RECONCILE.h"

/* ========================================================================= */
void RECONCILE(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bizdate[16];
    long rlen = 0;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid, h_net;
    char h_bizdate[16];
    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));
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;

    /* own-branch write: recon ledger evidence only */
    EXEC SQL INSERT INTO ledger (ledger_id, purchase_id, entry_type, amount, biz_date)
        VALUES (nextval('ledger_seq'), :h_pid, 'RECON', :h_net, :h_bizdate);
    if (sqlca.sqlcode < 0) {
        userlog("RECONCILE INSERT ledger FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        FAIL(b);
    }
    Bchg(b, T_STATUS, 0, "M", 0L);
    userlog("RECONCILE pid=%ld 대사완료 -> SETTLE", h_pid);

    if (tpcall("SETTLE", (char *)b, 0L, (char **)&b, &rlen, 0L) < 0) {
        userlog("RECONCILE: tpcall(SETTLE) FAIL: %s", tpstrerror(tperrno));
        FAIL(b);
    }
    OK(b);
}

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