/*
 * cl_verify_amt_dbio.pgc - cl 모듈 DB 접근 함수 (cl_verify_amt_dbio), archived into libcldbio.a.
 * 마감 저장총액 vs 실제 매입총액 검증.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "cl_verify_amt_dbio.h"

/* close_log total_amount 와 실제 매입 총액 대사. */
int clqdb_verify_amt(long cid, const char *bizdate, long *stored, long *actual)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cid = cid, h_stored, h_actual;
    char h_bizdate[16];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT total_amount INTO :h_stored FROM cl_close_log WHERE close_id = :h_cid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) return -1;
    EXEC SQL SELECT coalesce(sum(amount),0) INTO :h_actual FROM purchase WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    *stored = h_stored; *actual = h_actual;
    return 0;
}
