/*
 * lg_status_flip_dbio.pgc - lg 모듈 DB 접근 함수 (lg_status_flip_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_status_flip_dbio.h"

/* 당일 from_st -> to_st 전표 일괄 전이, 반영 건수 반환. */
long lgqdb_status_flip(const char *bizdate, const char *from_st, const char *to_st)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16], h_from[8], h_to[8];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    strncpy(h_from, from_st, sizeof(h_from)-1); h_from[sizeof(h_from)-1] = 0;
    strncpy(h_to, to_st, sizeof(h_to)-1); h_to[sizeof(h_to)-1] = 0;
    EXEC SQL UPDATE lg_voucher SET status = :h_to, updated_at = now()
        WHERE biz_date = :h_bizdate AND status = :h_from;
    if (sqlca.sqlcode < 0) { userlog("lgqdb_status_flip FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return sqlca.sqlerrd[2];
}
