/*
 * st_dtlinsert_dbio.pgc - 지급액으로부터 CAP 라인 생성 (st_dtlinsert_dbio), libstdbio.a.
 * ECPG; INSERT .. SELECT 로 st_payment 를 CAP 수수료 라인으로 st_settle_dtl 에 적재. 반환=행수.
 */
#include <string.h>
#include <userlog.h>
#include "st_dtlinsert_dbio.h"

long stdb_seed_cap_lines(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 st_settle_dtl (dtl_id, settlement_id, purchase_id, merchant_id, fee_type, amount, biz_date)
        SELECT nextval('st_dtl_seq'), 0, 0, merchant_id, 'CAP', net_amount / 100, biz_date
        FROM st_payment WHERE biz_date = :h_bizdate AND status = 'PAID';
    if (sqlca.sqlcode < 0) { userlog("stdb_seed_cap_lines FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return (long) sqlca.sqlerrd[2];
}
