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

/* 6. RC_CNTDIFF - 건수 차이 대사: 매입 건수 vs 대사 로그 건수. */
void RC_CNTDIFF(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char bizdate[16];
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id, h_pcnt, h_mcnt, h_diff;
    char h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

    gets_(b, T_BIZDATE, bizdate, sizeof(bizdate));
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT count(*) INTO :h_pcnt FROM purchase WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) FAIL(b);
    EXEC SQL SELECT count(*) INTO :h_mcnt FROM rc_match_log WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) FAIL(b);
    h_diff = h_pcnt - h_mcnt;
    EXEC SQL SELECT nextval('rc_diff_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) FAIL(b);
    EXEC SQL INSERT INTO rc_diff (diff_id, purchase_id, expected_amt, actual_amt, diff_amt, diff_kind, biz_date)
        VALUES (:h_id, 0, :h_pcnt, :h_mcnt, :h_diff, 'CNT', :h_bizdate);
    if (sqlca.sqlcode < 0) FAIL(b);
    setl(b, T_COUNT, h_diff);
    OK(b);
}

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