/*
 * lg_posted_total_dbio.pgc - lg 모듈 DB 접근 함수 (lg_posted_total_dbio), archived into liblgdbio.a.
 * 전기(posted) 원장 라인 총 차변/대변.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_posted_total_dbio.h"

/* posted 라인의 총 차변/대변 (마감연계 전 잔량). */
int lgqdb_posted_total(const char *bizdate, long *dr, long *cr)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_dr, h_cr;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT coalesce(sum(CASE WHEN dc='D' THEN amount ELSE 0 END),0),
                    coalesce(sum(CASE WHEN dc='C' THEN amount ELSE 0 END),0)
        INTO :h_dr, :h_cr FROM lg_ledger_line
        WHERE biz_date = :h_bizdate AND status = 'posted';
    if (sqlca.sqlcode < 0) { userlog("lgqdb_posted_total FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    *dr = h_dr; *cr = h_cr;
    return 0;
}
