/*
 * lg_settle_join_dbio.pgc - lg 모듈 DB 접근 함수 (lg_settle_join_dbio), archived into liblgdbio.a.
 * 매입(purchase) net 과 전기 원장 대변 정합 대사.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_settle_join_dbio.h"

/* purchase.net 합과 lg_ledger_line MERCH_PAY 대변합을 조인 없이 각각 집계해 대사. */
int lgqdb_settle_join(const char *bizdate, long *purch_net, long *ledger_pay)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_pn, h_lp;
    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(net),0) INTO :h_pn FROM purchase WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) return -1;
    EXEC SQL SELECT coalesce(sum(amount),0) INTO :h_lp FROM lg_ledger_line
        WHERE biz_date = :h_bizdate AND acct = 'MERCH_PAY' AND dc = 'C';
    if (sqlca.sqlcode < 0) return -1;
    *purch_net = h_pn; *ledger_pay = h_lp;
    return 0;
}
