/* Processed by ecpg (15.18 (Debian 15.18-0+deb12u1)) */
/* These include files are added by the preprocessor */
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
/* End of automatic include section */

#line 1 "app/online/vl_ol_0011.pgc"
/* @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"


#line 1 "/usr/include/postgresql/sqlca.h"
#ifndef POSTGRES_SQLCA_H
#define POSTGRES_SQLCA_H

#ifndef PGDLLIMPORT
#if  defined(WIN32) || defined(__CYGWIN__)
#define PGDLLIMPORT __declspec (dllimport)
#else
#define PGDLLIMPORT
#endif							/* __CYGWIN__ */
#endif							/* PGDLLIMPORT */

#define SQLERRMC_LEN	150

#ifdef __cplusplus
extern "C"
{
#endif

struct sqlca_t
{
	char		sqlcaid[8];
	long		sqlabc;
	long		sqlcode;
	struct
	{
		int			sqlerrml;
		char		sqlerrmc[SQLERRMC_LEN];
	}			sqlerrm;
	char		sqlerrp[8];
	long		sqlerrd[6];
	/* Element 0: empty						*/
	/* 1: OID of processed tuple if applicable			*/
	/* 2: number of rows processed				*/
	/* after an INSERT, UPDATE or				*/
	/* DELETE statement					*/
	/* 3: empty						*/
	/* 4: empty						*/
	/* 5: empty						*/
	char		sqlwarn[8];
	/* Element 0: set to 'W' if at least one other is 'W'	*/
	/* 1: if 'W' at least one character string		*/
	/* value was truncated when it was			*/
	/* stored into a host variable.             */

	/*
	 * 2: if 'W' a (hopefully) non-fatal notice occurred
	 */	/* 3: empty */
	/* 4: empty						*/
	/* 5: empty						*/
	/* 6: empty						*/
	/* 7: empty						*/

	char		sqlstate[5];
};

struct sqlca_t *ECPGget_sqlca(void);

#ifndef POSTGRES_ECPG_INTERNAL
#define sqlca (*ECPGget_sqlca())
#endif

#ifdef __cplusplus
}
#endif

#endif

#line 18 "app/online/vl_ol_0011.pgc"


TX_SERVICE(VL_OL_0011, ctx)
{
    /* exec sql begin declare section */
     
     
     
     
     
    
#line 23 "app/online/vl_ol_0011.pgc"
 char h_appr_no [ 16 ] ;
 
#line 24 "app/online/vl_ol_0011.pgc"
 long h_appr_amt ;
 
#line 25 "app/online/vl_ol_0011.pgc"
 long h_appr_cnt ;
 
#line 26 "app/online/vl_ol_0011.pgc"
 long h_pur_amt ;
 
#line 27 "app/online/vl_ol_0011.pgc"
 long h_pur_cnt ;
/* exec sql end declare section */
#line 28 "app/online/vl_ol_0011.pgc"


    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) 승인원장 집계 */
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select coalesce ( SUM ( appr_amount ) , 0 ) , count ( * ) from vl_approval where approval_no = $1 ", 
	ECPGt_char,(h_appr_no),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_long,&(h_appr_amt),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_appr_cnt),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 50 "app/online/vl_ol_0011.pgc"


    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) 매입원장 집계 */
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select coalesce ( SUM ( purchase_amount ) , 0 ) , count ( * ) from vl_purchase where approval_no = $1 ", 
	ECPGt_char,(h_appr_no),(long)16,(long)1,(16)*sizeof(char), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, 
	ECPGt_long,&(h_pur_amt),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, 
	ECPGt_long,&(h_pur_cnt),(long)1,(long)1,sizeof(long), 
	ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
#line 63 "app/online/vl_ol_0011.pgc"


    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);
}
