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

/* 7. RC_3WAY - 3-way 대사: 승인+매입+원장 모두 존재 시 MATCHED. */
void RC_3WAY(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bizdate[16];
    long mid;
    EXEC SQL BEGIN DECLARE SECTION;
    long h_pid, h_appr, h_led, h_net;
    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 count(*) INTO :h_appr FROM approval WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0) FAIL(b);
    EXEC SQL SELECT count(*) INTO :h_led FROM ledger WHERE purchase_id = :h_pid;
    if (sqlca.sqlcode < 0) FAIL(b);
    mid = rcdb_insert_match(h_pid, "3WAY", h_net,
                            (h_appr > 0 && h_led > 0) ? "MATCHED" : "EXCEPTION", bizdate);
    if (mid < 0) FAIL(b);
    setl(b, T_RC, (h_appr > 0 && h_led > 0) ? 0 : 1);
    OK(b);
}

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