/*
 * lg_ledger_dbio.pgc - 전표/원장라인 DB 접근 (part of liblgdbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_dbio.h"

long lgdb_next_voucher_id(void)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id;
    EXEC SQL END DECLARE SECTION;

    EXEC SQL SELECT nextval('lg_voucher_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) {
        userlog("lgdb_next_voucher_id FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return h_id;
}

long lgdb_next_line_id(void)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id;
    EXEC SQL END DECLARE SECTION;

    EXEC SQL SELECT nextval('lg_line_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) {
        userlog("lgdb_next_line_id FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return h_id;
}

int lgdb_insert_voucher(long vid, const char *vtype, long ref_id,
                        const char *status, const char *bizdate, const char *memo)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_vid = vid, h_ref = ref_id;
    char h_type[16], h_status[8], h_bizdate[16], h_memo[128];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_type, vtype, sizeof(h_type) - 1);       h_type[sizeof(h_type)-1] = 0;
    strncpy(h_status, status, sizeof(h_status) - 1);  h_status[sizeof(h_status)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1);  h_bizdate[sizeof(h_bizdate)-1] = 0;
    strncpy(h_memo, memo, sizeof(h_memo) - 1);        h_memo[sizeof(h_memo)-1] = 0;

    EXEC SQL INSERT INTO lg_voucher (voucher_id, voucher_type, ref_id, biz_date, status, memo)
        VALUES (:h_vid, :h_type, :h_ref, :h_bizdate, :h_status, :h_memo);
    if (sqlca.sqlcode < 0) {
        userlog("lgdb_insert_voucher FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

int lgdb_update_voucher_status(long vid, const char *status)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_vid = vid;
    char h_status[8];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_status, status, sizeof(h_status) - 1); h_status[sizeof(h_status)-1] = 0;

    EXEC SQL UPDATE lg_voucher SET status = :h_status, updated_at = now()
        WHERE voucher_id = :h_vid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("lgdb_update_voucher_status FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

int lgdb_get_voucher_status(long vid, char *out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_vid = vid;
    char h_status[8];
    EXEC SQL END DECLARE SECTION;

    EXEC SQL SELECT status INTO :h_status FROM lg_voucher WHERE voucher_id = :h_vid;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("lgdb_get_voucher_status FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    out[0] = h_status[0];
    out[1] = 0;
    return 0;
}

int lgdb_insert_line(long vid, const char *acct, const char *dc, long amount,
                     const char *merch, const char *bizdate, const char *status)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id, h_vid = vid, h_amount = amount;
    char h_acct[32], h_dc[4], h_merch[64], h_bizdate[16], h_status[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_acct, acct, sizeof(h_acct) - 1);        h_acct[sizeof(h_acct)-1] = 0;
    strncpy(h_dc, dc, sizeof(h_dc) - 1);              h_dc[sizeof(h_dc)-1] = 0;
    strncpy(h_merch, merch, sizeof(h_merch) - 1);     h_merch[sizeof(h_merch)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1);  h_bizdate[sizeof(h_bizdate)-1] = 0;
    strncpy(h_status, status, sizeof(h_status) - 1);  h_status[sizeof(h_status)-1] = 0;

    EXEC SQL SELECT nextval('lg_line_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) return -1;

    EXEC SQL INSERT INTO lg_ledger_line
        (line_id, voucher_id, acct, dc, amount, merchant_id, biz_date, status)
        VALUES (:h_id, :h_vid, :h_acct, :h_dc, :h_amount, :h_merch, :h_bizdate, :h_status);
    if (sqlca.sqlcode < 0) {
        userlog("lgdb_insert_line FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}
