/*
 * lg_line_relabel_dbio.pgc - lg 모듈 DB 접근 함수 (lg_line_relabel_dbio), archived into liblgdbio.a.
 * 특정 전표 라인 계정과목 재분류 (update count).
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "lg_line_relabel_dbio.h"

/* 한 전표 안에서 계정과목 재분류(from->to), 재분류 라인 수 반환. */
long lgqdb_line_relabel(long vid, const char *from_acct, const char *to_acct)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_vid = vid;
    char h_from[32], h_to[32];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_from, from_acct, sizeof(h_from)-1); h_from[sizeof(h_from)-1] = 0;
    strncpy(h_to, to_acct, sizeof(h_to)-1); h_to[sizeof(h_to)-1] = 0;
    EXEC SQL UPDATE lg_ledger_line SET acct = :h_to
        WHERE voucher_id = :h_vid AND acct = :h_from AND status = 'posted';
    if (sqlca.sqlcode < 0) { userlog("lgqdb_line_relabel FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return sqlca.sqlerrd[2];
}
