/*
 * py_ledger_post_dbio.pgc - py 모듈 DB 접근 함수 세트 (py_ledger_post_dbio), archived into libpydbio.a.
 * 지급완료(P) 건을 지급원장 상세로 INSERT..SELECT 전기한다.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "py_ledger_post_dbio.h"

/* INSERT..SELECT: 지급완료 건을 OUT 엔트리로 원장상세에 적재, 적재 건수 반환. */
long pydb_ledger_post_paid(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    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 INSERT INTO py_ledger (ledger_id, payout_id, entry_type, amount, biz_date)
        SELECT nextval('py_ledger_seq'), payout_id, 'OUT', net, biz_date
        FROM py_payout WHERE biz_date = :h_bizdate AND status = 'P';
    if (sqlca.sqlcode < 0) { userlog("pydb_ledger_post_paid FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return sqlca.sqlerrd[2];
}
