/*
 * lg_posted_join_dbio.pgc - lg 모듈 DB 접근 함수 (lg_posted_join_dbio), archived into liblgdbio.a.
 * 전기완료(P) 전표와 조인해 차변(D) 라인 금액 합을 구한다.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_posted_join_dbio.h"

/* JOIN: 전기완료 전표의 차변 라인 합계. */
long lgqdb_posted_debit(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_sum = 0;
    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(l.amount),0) INTO :h_sum
        FROM lg_ledger_line l JOIN lg_voucher v ON l.voucher_id = v.voucher_id
        WHERE v.status = 'P' AND v.biz_date = :h_bizdate AND l.dc = 'D';
    if (sqlca.sqlcode < 0) { userlog("lgqdb_posted_debit FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_sum;
}
