/*
 * ac_edi_intoledger_dbio.pgc - ac 모듈 DB 접근 함수 세트 (ac_edi_intoledger_dbio), archived into libacdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * Shape: INSERT INTO ledger SELECT (set-based 전기), 삽입 건수 반환.
 */
#include <string.h>
#include <userlog.h>
#include "ac_edi_intoledger_dbio.h"

/* 당일 EDI 문서를 RECON 원장 엔트리로 set-based 전기하고 삽입 건수를 반환. */
long acdb_edi_post_to_ledger(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long inserted = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL INSERT INTO ledger (ledger_id, purchase_id, entry_type, amount, biz_date)
        SELECT nextval('ledger_seq'), 0, 'RECON', e.amount, e.biz_date
        FROM ac_edi e WHERE e.biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) {
        userlog("acdb_edi_post_to_ledger FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    inserted = sqlca.sqlerrd[2];
    return inserted;
}
