/* @tier medium @module vl @transform validation-online */
/*
 * vl_ol_0011.pgc - 승인-매입 정합 검증 서비스 (VL_OL_0011)
 *
 * 승인번호를 기준으로 승인원장(vl_approval) 과 매입원장(vl_purchase) 을 각각
 * 조회하여 금액 합계와 건수를 대사한다. 승인/매입 금액 또는 건수가 불일치하면
 * 미대사(VL11xx) 로 판정한다.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "vl_core.h"

EXEC SQL INCLUDE sqlca;

TX_SERVICE(VL_OL_0011, ctx)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_appr_no[16];
    long h_appr_amt;
    long h_appr_cnt;
    long h_pur_amt;
    long h_pur_cnt;
    EXEC SQL END DECLARE SECTION;

    char approval[16];
    long amt_diff, cnt_diff;

    tx_log(TX_LOG_INFO, "[VL_OL_0011] 승인-매입 정합 검증 서비스 진입");

    if (tx_buf_get(ctx->in, "APPRNO", approval, sizeof(approval)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[VL_OL_0011] APPRNO 누락");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1100");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    strncpy(h_appr_no, approval, sizeof(h_appr_no) - 1);
    h_appr_no[sizeof(h_appr_no) - 1] = '\0';
    h_appr_amt = 0; h_appr_cnt = 0; h_pur_amt = 0; h_pur_cnt = 0;

    /* 1) 승인원장 집계 */
    EXEC SQL
        SELECT COALESCE(SUM(appr_amount), 0), COUNT(*)
          INTO :h_appr_amt, :h_appr_cnt
          FROM vl_approval
         WHERE approval_no = :h_appr_no;

    if (sqlca.sqlcode != TX_SQL_OK && sqlca.sqlcode != TX_SQL_NOTFOUND) {
        TX_DBIO_LOG_ERR("SELECT vl_approval");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1109");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    /* 2) 매입원장 집계 */
    EXEC SQL
        SELECT COALESCE(SUM(purchase_amount), 0), COUNT(*)
          INTO :h_pur_amt, :h_pur_cnt
          FROM vl_purchase
         WHERE approval_no = :h_appr_no;

    if (sqlca.sqlcode != TX_SQL_OK && sqlca.sqlcode != TX_SQL_NOTFOUND) {
        TX_DBIO_LOG_ERR("SELECT vl_purchase");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1119");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    /* 3) 대사 판정 */
    amt_diff = h_appr_amt - h_pur_amt;
    cnt_diff = h_appr_cnt - h_pur_cnt;

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "APPRNO", approval);
    tx_buf_setlong(ctx->out, "APPRAMT", h_appr_amt);
    tx_buf_setlong(ctx->out, "APPRCNT", h_appr_cnt);
    tx_buf_setlong(ctx->out, "PURAMT", h_pur_amt);
    tx_buf_setlong(ctx->out, "PURCNT", h_pur_cnt);
    tx_buf_setlong(ctx->out, "AMTDIFF", amt_diff);

    if (h_appr_cnt == 0 && h_pur_cnt == 0) {
        tx_log(TX_LOG_WARN, "[VL_OL_0011] 승인/매입 원장 없음 appr=%s", approval);
        tx_buf_sets(ctx->out, "RESULT", "MISMATCH");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1101");
        tx_return(ctx, TX_ENOENT, ctx->out);
        return;
    }
    if (amt_diff != 0 || cnt_diff != 0) {
        tx_log(TX_LOG_WARN, "[VL_OL_0011] 승인-매입 불일치 appr=%s amtdiff=%ld cntdiff=%ld",
               approval, amt_diff, cnt_diff);
        tx_buf_sets(ctx->out, "RESULT", "MISMATCH");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1103");
        tx_return(ctx, TX_OK, ctx->out);
        return;
    }

    tx_buf_sets(ctx->out, "RESULT", "MATCH");
    tx_buf_sets(ctx->out, "ERRCODE", RESP_OK);
    tx_log(TX_LOG_INFO, "[VL_OL_0011] 승인-매입 정합 appr=%s amt=%ld cnt=%ld",
           approval, h_appr_amt, h_appr_cnt);
    tx_return(ctx, TX_OK, ctx->out);
}
