/* @tier easy @module rc @transform reconciliation-batch */ /* * rc_bt_0012.pgc - 대사 결과 집계(원장반영) 배치 (RC_BT_0012) * * 매입확정('M') 건을 커서로 순회하며 원장 반영액을 산정하고 정산 전이한다. * 업무규칙: 원장 반영액 gl_amt = amount - fee. * gl_amt > 0 이면 정산완료('S') 로 전이하고 반영합계/수수료합계를 * 누계한다. gl_amt <= 0 이면 반영 스킵(상태 유지). * GL 반영 요약을 리포트한다. * * 실행: rc_bt_0012 [db@host] */ #include #include #include #include "txcore.h" #include "txcore_dbio.h" #include "acq_util.h" #include "rc_core.h" EXEC SQL INCLUDE sqlca; static int run_rc_bt_0012(const char *biz_date) { EXEC SQL BEGIN DECLARE SECTION; char h_biz_date[9]; char h_key[16]; long h_amount; long h_fee; char h_new_status[2]; EXEC SQL END DECLARE SECTION; long total = 0, posted = 0, skipped = 0, errcnt = 0; long gl_total = 0, fee_total = 0; int rc; strncpy(h_biz_date, biz_date, sizeof(h_biz_date) - 1); h_biz_date[sizeof(h_biz_date) - 1] = '\0'; EXEC SQL DECLARE cur_rc_bt_0012 CURSOR FOR SELECT key, amount, fee FROM rc_ledger WHERE biz_date = :h_biz_date AND status = 'M' ORDER BY key; EXEC SQL OPEN cur_rc_bt_0012; if (sqlca.sqlcode != TX_SQL_OK) { TX_DBIO_LOG_ERR("OPEN cur_rc_bt_0012"); return TX_EDB; } if (tx_begin() != TX_OK) tx_log(TX_LOG_WARN, "[rc_bt_0012] tx_begin 경고"); for (;;) { long gl_amt; EXEC SQL FETCH cur_rc_bt_0012 INTO :h_key, :h_amount, :h_fee; if (sqlca.sqlcode == TX_SQL_NOTFOUND) break; if (sqlca.sqlcode != TX_SQL_OK) { TX_DBIO_LOG_ERR("FETCH cur_rc_bt_0012"); errcnt++; break; } total++; /* 원장 반영액 = 매입금액 - 수수료 */ gl_amt = h_amount - h_fee; if (gl_amt > 0) { strncpy(h_new_status, RC_ST_SETTLED, sizeof(h_new_status)); rc = rc_rec_update_status(h_key, h_new_status); if (rc != TX_OK) { tx_log(TX_LOG_ERROR, "[rc_bt_0012] 정산전이 실패 key=%s rc=%d", h_key, rc); errcnt++; continue; } posted++; gl_total += gl_amt; fee_total += h_fee; } else { skipped++; } } EXEC SQL CLOSE cur_rc_bt_0012; if (errcnt == 0) tx_commit(); else tx_abort(); tx_log(TX_LOG_INFO, "[rc_bt_0012] 요약 date=%s 대상=%ld 반영=%ld 스킵=%ld 반영합=%ld", biz_date, total, posted, skipped, gl_total); printf("======= 대사 결과 집계(원장반영) 배치 [rc_bt_0012] =======\n"); printf(" 영업일 : %s\n", biz_date); printf(" 대상 건수 : %ld\n", total); printf(" 원장반영(S) : %ld\n", posted); printf(" 반영 스킵 : %ld\n", skipped); printf(" 원장반영 합계 : %ld\n", gl_total); printf(" 수수료 합계 : %ld\n", fee_total); printf(" 오류 건수 : %ld\n", errcnt); printf("=======================================================\n"); return (errcnt == 0) ? TX_OK : TX_FAIL; } int main(int argc, char **argv) { const char *biz_date; const char *target; int rc; tx_set_loglevel(TX_LOG_INFO); if (argc < 2) { fprintf(stderr, "사용법: %s [db@host]\n", argv[0]); return 2; } biz_date = argv[1]; target = (argc >= 3) ? argv[2] : NULL; if (!date_is_valid(biz_date)) { fprintf(stderr, "오류: 유효하지 않은 영업일 '%s'\n", biz_date); return 2; } tx_log(TX_LOG_INFO, "[rc_bt_0012] 배치 시작 date=%s", biz_date); rc = rc_dbio_connect(target); if (rc != TX_OK) { tx_log(TX_LOG_ERROR, "[rc_bt_0012] DB 접속 실패 rc=%d", rc); return 1; } rc = run_rc_bt_0012(biz_date); rc_dbio_disconnect(); tx_log(TX_LOG_INFO, "[rc_bt_0012] 배치 종료 rc=%d(%s)", rc, tx_strerror(rc)); return (rc == TX_OK) ? 0 : 1; }