/*
 * lg_settle_lines_dbio.pgc - lg 모듈 DB 접근 함수 (lg_settle_lines_dbio), archived into liblgdbio.a.
 * 정산연계(SETTLE) 전표에 속한 분개 라인 금액 합을 IN 서브쿼리로 구한다.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_settle_lines_dbio.h"

/* IN 서브쿼리: SETTLE 전표 소속 라인 금액 합. */
long lgqdb_settle_line_sum(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(amount),0) INTO :h_sum FROM lg_ledger_line
        WHERE voucher_id IN (SELECT voucher_id FROM lg_voucher
                             WHERE voucher_type = 'SETTLE' AND biz_date = :h_bizdate);
    if (sqlca.sqlcode < 0) { userlog("lgqdb_settle_line_sum FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return h_sum;
}
