using Cosmos.Common;
using Cosmos.CommonManager;
using Cosmos.UserFrame;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Data;

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 상품권
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Service
{
    class Gift : PaymentBase, IPaymentUs
    {

        public Gift()
        {
        }

        #region SearchPayment 결제 조회
        /// <summary>
        /// 결제 조회
        /// </summary>
        /// <param name="aParam"></param>
        /// <param name="aRet"></param>
        /// <returns></returns>
        public string SearchPayment(string[] aParam, ref string[] aRet)
        {
            string sRet = UserCom.RST_ERR;
            Hashtable htRecvData = new Hashtable();

            try
            {
                m_cPayItem = new Column.TR_PAYMENT.DATA();                  // 결과 저장 변수 생성

                string sPosMenuKey = aParam[0];                             // 전문구분
                m_cPayItem.PAY_WAY_CD = aParam[1];                          // 결제 그룹코드
                m_cPayItem.PAY_DTL_CD_01 = aParam[2];                       // 결제 상세코드
                string sTicketType = aParam[3];                             // 처리구분
                string sTicketNo = aParam[4];                               // 번호
                double nSaleAmt = CmUtil.DoubleParse(aParam[5]);            // 금액

                // 잔액 교환권 조회
                sRet = ExecuteHostIrt(PosConst.CANCEL_DIV.NORMAL, "10", new string[] { sPosMenuKey, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sTicketType, sTicketNo, nSaleAmt.ToString() }, ref htRecvData);
                if (sRet != UserCom.RST_OK) return sRet;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                   System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion

        #region GetPayment 결제 정보 획득
        /// <summary>
        /// 결제 정보 획득
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public object GetPayment(string[] aParam)
        {
            return m_cPayItem;
        }
        #endregion

        #region SetPayment 결제 등록
        /// <summary>
        /// 결제 등록
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public string SetPayment(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;

            try
            {
                m_cPayItem = new Column.TR_PAYMENT.DATA();                  // 결과 저장 변수 생성

                string sPosMenuKey = aParam[0];                             // 메뉴키
                m_cPayItem.PAY_WAY_CD = aParam[1];                          // 결제 그룹코드
                m_cPayItem.PAY_DTL_CD_01 = aParam[2];                       // 결제 상세코드
                m_cPayItem.CANCEL_DIV = ItemConst.PAY_CANCEL_DIV.NORMAL;    // 취소여부

                string sInqFlag = aParam[3];                                // 승인서버ID
                string sWcc = aParam[4];                                    // 입력구분
                string sGiftNo = aParam[5];                                 // 번호
                double nQty = CmUtil.DoubleParse(aParam[6]);                // 수량
                double nSaleAmt = CmUtil.DoubleParse(aParam[7]);            // 금액
                string sTicketType = aParam[8];                             // 티켓발행구분
                string sTicketNo = aParam[9];                               // 티켓번호
                double nTicketAmt = CmUtil.DoubleParse(aParam[10]);         // 티켓금액
                string sMakeType = aParam[11];                              // 티켓발생구분
                string sAppNo = aParam[12];                                 // 승인번호
                string sAppDate = aParam[13];                               // 승인일자
                double nChangeMoney = CmUtil.DoubleParse(aParam[14]);       // 거스름
                string sAppYN = aParam[15];                                 // 인증여부
                string sGiftType = aParam[16];                              // 권종
                string sGiftNM = aParam[17];                                // 상품권명
                string sCHANGE_PAYMNT_YN = aParam[18];                      // 거스름 가능여부

                // 승인요청
                sRet = ExecuteIrt(PosConst.CANCEL_DIV.NORMAL, ItemConst.GIFT_APPROVE_DIV.APPROVE, new string[] { sPosMenuKey, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sInqFlag, sWcc, sGiftNo, nQty.ToString()
                                    , nSaleAmt.ToString(), sTicketType, sTicketNo, nTicketAmt.ToString(), sMakeType, sAppNo, sAppDate, nChangeMoney.ToString(), sAppYN, "", "", sGiftType, sGiftNM, sCHANGE_PAYMNT_YN });
                if (sRet != UserCom.RST_OK)
                {
                    // 망취소
                    sRet = ExecuteIrt(PosConst.CANCEL_DIV.NORMAL, ItemConst.GIFT_APPROVE_DIV.NET_CANCEL, new string[] { sPosMenuKey, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sInqFlag, sWcc, sGiftNo, nQty.ToString()
                                    , nSaleAmt.ToString(), sTicketType, sTicketNo, nTicketAmt.ToString(), sMakeType, sAppNo, sAppDate, nChangeMoney.ToString(), sAppYN, "", "", sGiftType, sGiftNM, sCHANGE_PAYMNT_YN });
                    return sRet;
                }

                if (sCHANGE_PAYMNT_YN == "1" && nChangeMoney > 0)
                {
                    m_cPosStatus.Sale.ChangeCashAmt = nChangeMoney;             // 거스름
                }

                // 결제 아이템 추가
                ArrayList alPayItem = (ArrayList)StateObject.GetItemObject(Column.TR_PAYMENT.ITEM);     // 결제 내역 받아 오기
                m_cPayItem.SEQ = alPayItem.Count + 1;
                alPayItem.Add(m_cPayItem);

                m_cDataService.UpdatePluAmount();       // 상품 합계금액 계산(거래해더)
                m_cDataService.UpdatePayAmount();       // 결제 금액 재 계산 처리

                sRet = UserCom.RST_OK;
                //WinManager.ConfirmMessage(sPointType + " " + MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0067));
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                   System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion

        #region 결제 취소
        /// <summary>
        /// 결제 취소
        /// </summary>
        /// <returns></returns>
        public string CancelPayment(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;

            try
            {
                // ---------------------------------------------------------------------------------------------------------------------------------------
                // 결제 수단 취소
                // ---------------------------------------------------------------------------------------------------------------------------------------
                ArrayList alPayItem = (ArrayList)StateObject.GetItemObject(Column.TR_PAYMENT.ITEM);

                for (int iRow = 0; iRow < alPayItem.Count; iRow++)
                {
                    Column.TR_PAYMENT.DATA clPayItem = (Column.TR_PAYMENT.DATA)alPayItem[iRow];

                    // 취소구분
                    if (clPayItem.CANCEL_DIV == ItemConst.PAY_CANCEL_DIV.CANCEL) continue;

                    // 상품권
                    if (clPayItem.PAY_WAY_CD != ItemConst.TR_ITEM_ID.GIFT_ITEM) continue;

                    if (aParam != null)
                    {
                        if (aParam.Length > 2)
                        {
                            if (aParam[0].ToString() == clPayItem.OCCUR_ENTRY_01                // 상품권 번호
                                && aParam[1].ToString() == clPayItem.OCCUR_ENTRY_15             // 권종
                                && aParam[2].ToString() == clPayItem.OCCUR_ENTRY_02             // 승인번호
                                && aParam[3].ToString() == clPayItem.OCCUR_ENTRY_03             // 승인일자
                                && aParam[4].ToString() == clPayItem.OCCUR_ENTRY_04             // 승인시간
                                && m_cPayItem.CANCEL_DIV != PosConst.CANCEL_DIV.CANCEL
                                )
                            {
                                sRet = CancelPaymentSub(new string[] { iRow.ToString() });
                                if (sRet != UserCom.RST_OK) return sRet;
                            }
                            continue;
                        }
                        else
                        {
                            // 결제 수단 취소
                            sRet = CancelPaymentSub(new string[] { iRow.ToString() });
                            if (sRet != UserCom.RST_OK) return sRet;
                        }
                    }
                    else
                    {
                        // 결제 수단 취소
                        sRet = CancelPaymentSub(new string[] { iRow.ToString() });
                        if (sRet != UserCom.RST_OK) return sRet;
                    }
                }
                // ---------------------------------------------------------------------------------------------------------------------------------------

                sRet = UserCom.RST_OK;

            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            finally
            {
                m_cDataService.UpdatePluAmount();       // 상품 합계금액 계산(거래해더)
                m_cDataService.UpdatePayAmount();       // 결제 금액 재 계산 처리
            }
            return sRet;
        }
        #endregion

        #region 결제 반품
        /// <summary>
        /// 결제 반품
        /// </summary>
        /// <returns></returns>
        public string RefundPayment(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;

            try
            {
                //// ---------------------------------------------------------------------------------------------------------------------------------------
                //// 결제 수단 반품
                //// ---------------------------------------------------------------------------------------------------------------------------------------
                //ArrayList alPayItem = (ArrayList)StateObject.GetItemObject(Column.TR_PAYMENT.ITEM);

                //for (int iRow = 0; iRow < alPayItem.Count; iRow++)
                //{
                //    Column.TR_PAYMENT.DATA clPayItem = (Column.TR_PAYMENT.DATA)alPayItem[iRow];

                //    // 취소구분
                //    if (clPayItem.CANCEL_DIV == ItemConst.PAY_CANCEL_DIV.CANCEL) continue;

                //    // 상품권
                //    if (clPayItem.PAY_WAY_CD != ItemConst.TR_ITEM_ID.GIFT_ITEM) continue;

                    // 결제 수단 반품
                sRet = RefundPaymentSub(new string[] { aParam[0].ToString() });
                if (sRet != UserCom.RST_OK) return sRet;
                //}
                //// ---------------------------------------------------------------------------------------------------------------------------------------
                 

                sRet = UserCom.RST_OK;

            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion

        #region RefundPaymentSub 결제 반품
        /// <summary>
        /// 결제 반품
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public string RefundPaymentSub(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;
            Hashtable htRecvData = new Hashtable();

            try
            {
                //if (m_cTrnStatus.Head.TradeDiv != ItemConst.TRAN_DIV.REFUND) return sRet;

                int nPayRow = CmUtil.IntParse(aParam[0]); // 취소할 행번호

                // 결제 내역 받아 오기
                ArrayList alPayItem = (ArrayList)StateObject.GetItemObject(Column.TR_PAYMENT.ITEM);
                m_cPayItem = (Column.TR_PAYMENT.DATA)alPayItem[nPayRow];


                if (m_cPayItem.OCCUR_ENTRY_08.ToString() != ItemConst.PAY_APP_DIV.NORMAL)
                {
                    //임의 등록 이면 승인 없이 데이터 처리
                    // WinManager.ConfirmMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0240));
                    m_cPayItem.OCCUR_ENTRY_06 = m_cPayItem.OCCUR_ENTRY_02;
                    m_cPayItem.OCCUR_ENTRY_07 = m_cPayItem.OCCUR_ENTRY_03;
                }
                else
                {

                    string sInqFlag = m_cPayItem.OCCUR_ENTRY_13;                                    // 승인서버ID
                    string sWcc = m_cPayItem.OCCUR_ENTRY_05;                                        // 입력구분
                    string sGiftNo = m_cPayItem.OCCUR_ENTRY_01;                                    // 번호
                    double nQty = CmUtil.DoubleParse(m_cPayItem.QTY_ENTRY_01.ToString());           // 수량
                    double nSaleAmt = CmUtil.DoubleParse(m_cPayItem.AMT_ENTRY_01.ToString());       // 금액
                    string sTicketType = m_cPayItem.OCCUR_ENTRY_10;                                 // 티켓발행구분
                    string sTicketNo = m_cPayItem.OCCUR_ENTRY_12;                                   // 티켓번호
                    double nTicketAmt = CmUtil.DoubleParse(m_cPayItem.AMT_ENTRY_03.ToString());     // 티켓금액
                    string sMakeType = m_cPayItem.OCCUR_ENTRY_11;                                   // 티켓발생구분
                    string sAppNo = m_cPayItem.OCCUR_ENTRY_02;                                      // 승인번호
                    string sAppDate = m_cPayItem.OCCUR_ENTRY_03;                                    // 승인일자
                    double nChangeMoney = CmUtil.DoubleParse(m_cPayItem.AMT_ENTRY_02.ToString());   // 거스름
                    string sAppYN = m_cPayItem.OCCUR_ENTRY_14;                                      // 인증여부
                    string sOrgAppNo = m_cPayItem.OCCUR_ENTRY_02;                                   // 승인번호
                    string sOrgAppDate = m_cPayItem.OCCUR_ENTRY_03;                                 // 승인일자
                    string sGiftType = m_cPayItem.OCCUR_ENTRY_15;                                   // 권종
                    string sGiftNM = m_cPayItem.OCCUR_ENTRY_18;                                     // 상품권명
                    string sCHANGE_PAYMNT_YN = m_cPayItem.OCCUR_ENTRY_09;                           // 거스름 가능여부

                    // 잔액교환권 번호가 있을 경우 발행취소 요청
                    if (m_cPayItem.OCCUR_ENTRY_12.Trim().Length > 0)
                    {
                        sRet = ExecuteHostIrt(PosConst.CANCEL_DIV.CANCEL, "", new string[] { ItemConst.IRT_INQ_TYPE.EXCHANGE_TICKET_INQ, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, ItemConst.EXCHANGE_TICKET_ISSUE_TYPE.MAKE_CANCEL, sTicketNo, nTicketAmt.ToString() }, ref htRecvData);
                        if (sRet != UserCom.RST_OK) return sRet;
                    }

                    //// 취소
                    //if (m_cPosStatus.Base.SaleDate != sAppDate && sAppDate.Trim() != "")
                    //{
                    //    // 당일 취소가 아닌경우 현금대체
                    //    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0219);

                    //    m_cPayItem.PAY_WAY_CD = ItemConst.TR_ITEM_ID.CASH_ITEM;
                    //    m_cPayItem.PAY_DTL_CD_01 = ItemConst.TR_ITEM_ID.CASH.CASH_PAY;

                    //    sRet = ExecuteCashIrt(ItemConst.GIFT_APPROVE_DIV.CANCEL, new string[] { ItemConst.IRT_INQ_TYPE.GIFT_APPROVE_INQ, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sInqFlag, sWcc, sGiftNo, nQty.ToString()
                    //                        , nSaleAmt.ToString(), sTicketType, sTicketNo, nTicketAmt.ToString(), sMakeType, sAppNo, sAppDate, nChangeMoney.ToString(), sAppYN, sOrgAppNo, sOrgAppDate, sGiftType });
                    //}
                    //else
                    //{
                    // 당일 취소인 경우 
                    sRet = ExecuteIrt(PosConst.CANCEL_DIV.CANCEL, ItemConst.GIFT_APPROVE_DIV.CANCEL, new string[] { ItemConst.IRT_INQ_TYPE.GIFT_APPROVE_INQ, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sInqFlag, sWcc, sGiftNo, nQty.ToString()
                                        , nSaleAmt.ToString(), sTicketType, sTicketNo, nTicketAmt.ToString(), sMakeType, sAppNo, sAppDate, nChangeMoney.ToString(), sAppYN, sOrgAppNo, sOrgAppDate, sGiftType, sGiftNM, sCHANGE_PAYMNT_YN });
                    //}
                    if (sRet != UserCom.RST_OK)
                    {
                        sRet = ExecuteIrt(PosConst.CANCEL_DIV.CANCEL, ItemConst.GIFT_APPROVE_DIV.NET_CANCEL, new string[] { ItemConst.IRT_INQ_TYPE.GIFT_APPROVE_INQ, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sInqFlag, sWcc, sGiftNo, nQty.ToString()
                                        , nSaleAmt.ToString(), sTicketType, sTicketNo, nTicketAmt.ToString(), sMakeType, sAppNo, sAppDate, nChangeMoney.ToString(), sAppYN, sOrgAppNo, sOrgAppDate, sGiftType, sGiftNM, sCHANGE_PAYMNT_YN });
                        return sRet;
                    }
                    //m_cPayItem.CANCEL_DIV = PosConst.CANCEL_DIV.CANCEL;
                }
                sRet = UserCom.RST_OK;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                   System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion

        #region SetMenualPayment
        public string SetMenualPayment(string[] aParam)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region CancelPaymentSub 결제 취소
        /// <summary>
        /// 결제 취소
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public string CancelPaymentSub(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;
            Hashtable htRecvData = new Hashtable();

            try
            {
                if (m_cTrnStatus.Head.TradeDiv != ItemConst.TRAN_DIV.NORMAL) return sRet;

                int nPayRow = CmUtil.IntParse(aParam[0]); // 취소할 행번호

                // 결제 내역 받아 오기
                ArrayList alPayItem = (ArrayList)StateObject.GetItemObject(Column.TR_PAYMENT.ITEM);
                m_cPayItem = (Column.TR_PAYMENT.DATA)alPayItem[nPayRow];
                 
                string sInqFlag = m_cPayItem.OCCUR_ENTRY_13;                                    // 승인서버ID
                string sWcc = m_cPayItem.OCCUR_ENTRY_05;                                        // 입력구분
                string sGiftNo = m_cPayItem.OCCUR_ENTRY_01;                                     // 번호
                double nQty = CmUtil.DoubleParse(m_cPayItem.QTY_ENTRY_01.ToString());           // 수량
                double nSaleAmt = CmUtil.DoubleParse(m_cPayItem.AMT_ENTRY_01.ToString());       // 금액
                string sTicketType = m_cPayItem.OCCUR_ENTRY_10;                                 // 티켓발행구분
                string sTicketNo = m_cPayItem.OCCUR_ENTRY_12;                                   // 티켓번호
                double nTicketAmt = CmUtil.DoubleParse(m_cPayItem.AMT_ENTRY_03.ToString());     // 티켓금액
                string sMakeType = m_cPayItem.OCCUR_ENTRY_11;                                   // 티켓발생구분
                string sAppNo = m_cPayItem.OCCUR_ENTRY_02;                                      // 승인번호
                string sAppDate = m_cPayItem.OCCUR_ENTRY_03;                                    // 승인일자
                double nChangeMoney = CmUtil.DoubleParse(m_cPayItem.AMT_ENTRY_02.ToString());   // 거스름
                string sAppYN = m_cPayItem.OCCUR_ENTRY_14;                                      // 인증여부
                string sOrgAppNo = m_cPayItem.OCCUR_ENTRY_02;                                   // 승인번호
                string sOrgAppDate = m_cPayItem.OCCUR_ENTRY_03;                                 // 승인일자
                string sGiftType = m_cPayItem.OCCUR_ENTRY_15;                                   // 권종
                string sGiftNM = m_cPayItem.OCCUR_ENTRY_18;                                     // 상품권명
                string sCHANGE_PAYMNT_YN = m_cPayItem.OCCUR_ENTRY_09;                           // 거스름 가능여부

                // 잔액교환권 번호가 있을 경우 발행취소 요청
                if (m_cPayItem.OCCUR_ENTRY_12.Trim().Length > 0)
                {
                    sRet = ExecuteHostIrt(PosConst.CANCEL_DIV.CANCEL, "", new string[] { ItemConst.IRT_INQ_TYPE.EXCHANGE_TICKET_INQ, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, ItemConst.EXCHANGE_TICKET_ISSUE_TYPE.MAKE_CANCEL, sTicketNo, nTicketAmt.ToString() }, ref htRecvData);
                    if (sRet != UserCom.RST_OK) return sRet;
                }

                //// 취소
                //if (m_cPosStatus.Base.SaleDate != sAppDate && sAppDate.Trim() != "")
                //{
                //    // 당일 취소가 아닌경우 현금대체
                //    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0219);
                    
                //    m_cPayItem.PAY_WAY_CD = ItemConst.TR_ITEM_ID.CASH_ITEM;
                //    m_cPayItem.PAY_DTL_CD_01 = ItemConst.TR_ITEM_ID.CASH.CASH_PAY;

                //    sRet = ExecuteCashIrt(ItemConst.GIFT_APPROVE_DIV.CANCEL, new string[] { ItemConst.IRT_INQ_TYPE.GIFT_APPROVE_INQ, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sInqFlag, sWcc, sGiftNo, nQty.ToString()
                //                        , nSaleAmt.ToString(), sTicketType, sTicketNo, nTicketAmt.ToString(), sMakeType, sAppNo, sAppDate, nChangeMoney.ToString(), sAppYN, sOrgAppNo, sOrgAppDate, sGiftType });
                //}
                //else
                //{
                    // 당일 취소인 경우 
                sRet = ExecuteIrt(PosConst.CANCEL_DIV.CANCEL, ItemConst.GIFT_APPROVE_DIV.CANCEL, new string[] { ItemConst.IRT_INQ_TYPE.GIFT_APPROVE_INQ, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sInqFlag, sWcc, sGiftNo, nQty.ToString()
                                        , nSaleAmt.ToString(), sTicketType, sTicketNo, nTicketAmt.ToString(), sMakeType, sAppNo, sAppDate, nChangeMoney.ToString(), sAppYN, sOrgAppNo, sOrgAppDate, sGiftType, sGiftNM, sCHANGE_PAYMNT_YN });
                //}
                if (sRet != UserCom.RST_OK)
                {
                    // 망취소
                    sRet = ExecuteIrt(PosConst.CANCEL_DIV.CANCEL, ItemConst.GIFT_APPROVE_DIV.NET_CANCEL, new string[] { ItemConst.IRT_INQ_TYPE.GIFT_APPROVE_INQ, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sInqFlag, sWcc, sGiftNo, nQty.ToString()
                                        , nSaleAmt.ToString(), sTicketType, sTicketNo, nTicketAmt.ToString(), sMakeType, sAppNo, sAppDate, nChangeMoney.ToString(), sAppYN, sOrgAppNo, sOrgAppDate, sGiftType, sGiftNM, sCHANGE_PAYMNT_YN });
                    return sRet;
                }
                m_cPayItem.CANCEL_DIV = PosConst.CANCEL_DIV.CANCEL;

                //#20170830 거스름돈 발생시 결제취소 해도 거스름돈(AMT_ENTRY_02)이 취소가 안되어 계산함 start
                //현금, 잔액교환권, 외화, 상품권, 식권
                m_cPosStatus.Sale.ChangeCashAmt = CmUtil.DoubleSubtraction(m_cPosStatus.Sale.ChangeCashAmt, m_cPayItem.AMT_ENTRY_02);
                //cPayItem.AMT_ENTRY_01 = 0;
                //cPayItem.AMT_ENTRY_02 = 0;
                //#20170830 거스름돈 발생시 결제취소 해도 거스름돈(AMT_ENTRY_02)이 취소가 안되어 계산함 end

                sRet = UserCom.RST_OK;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                   System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion

        #region 조회 실행
        /// <summary>
        /// 조회 실행
        /// </summary>
        public string ExecuteIrt(string sTradeDiv, string sDealType, string[] aParam)
        {
            string sRet = UserCom.RST_ERR;
            Hashtable htRecvData = new Hashtable();

            try
            {
                string sPosMenuKey = aParam[0];                             // 메뉴키
                m_cPayItem.PAY_WAY_CD = aParam[1];                          // 결제 그룹코드
                m_cPayItem.PAY_DTL_CD_01 = aParam[2];                       // 결제 상세코드

                string sInqFlag = aParam[3];                                // 승인서버id
                string sWcc = aParam[4];                                    // 입력구분
                string sGiftNo = aParam[5];                                 // 번호
                double nQty = CmUtil.DoubleParse(aParam[6]);                // 수량
                double nSaleAmt = CmUtil.DoubleParse(aParam[7]);            // 금액
                string sTicketType = aParam[8];                             // 티켓발행구분
                string sTicketNo = aParam[9];                               // 티켓번호
                double nTicketAmt = CmUtil.DoubleParse(aParam[10]);         // 티켓금액
                string sMakeType = aParam[11];                              // 티켓발생구분
                string sAppNo = aParam[12];                                 // 승인번호
                string sAppDate = aParam[13];                               // 승인일자
                double nChangeMoney = CmUtil.DoubleParse(aParam[14]);       // 거스름
                string sAppYN = aParam[15];                                 // 인증여부
                string sOrgAppNo = aParam[16];                              // 원승인번호
                string sOrgAppDate = aParam[17];                            // 원승인일자
                string sGiftType = aParam[18];                              // 권종
                string sGiftNM = aParam[19];                                // 상품권명
                string sCHANGE_PAYMNT_YN = aParam[20];                      // 거스름 가능 여부

                // 연습모드이면 연습 데이터 설정
                if (m_cPosStatus.Base.TrainingFlag == "1" || m_cPosStatus.Mst.TestStorYn == "1" ) return SetTrainingData(new string[] { sPosMenuKey, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sInqFlag, sWcc, sGiftNo, nQty.ToString()
                                    , nSaleAmt.ToString(), sTicketType, sTicketNo, nTicketAmt.ToString(), sMakeType, sAppNo, sAppDate, nChangeMoney.ToString(), sAppYN, "", "", sGiftType, sGiftNM, sCHANGE_PAYMNT_YN });

                // 상품권 승인 처리
                if (aParam[15] == "1")
                {    
                    // 인증시 상품권 승인
                    sRet = ExecuteHostIrt(sTradeDiv, sDealType, new string[] { sPosMenuKey, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sInqFlag, sWcc, sGiftNo, sAppNo, sAppDate }, ref htRecvData);

                    if (sRet != UserCom.RST_OK) return sRet;

                    m_cPayItem.PAY_AMT = CmUtil.DoubleParse(htRecvData[Column.IQ_GIFT_APP_RSP.DATA.GIFT_AMT].ToString());
                    m_cPayItem.OCCUR_ENTRY_01 = htRecvData[Column.IQ_GIFT_APP_RSP.DATA.GIFT_NO].ToString();                 // 카드번호
                    m_cPayItem.OCCUR_ENTRY_02 = htRecvData[Column.IQ_GIFT_APP_RSP.DATA.APPROVE_NO].ToString();              // 승인번호
                    m_cPayItem.OCCUR_ENTRY_03 = htRecvData[Column.IQ_GIFT_APP_RSP.DATA.APPROVE_DATE].ToString().Substring(0, 8);    // 승인일자
                    m_cPayItem.OCCUR_ENTRY_04 = htRecvData[Column.IQ_GIFT_APP_RSP.DATA.APPROVE_DATE].ToString().Substring(8, 6);    // 승인시간
                }
                else
                {
                    m_cPayItem.PAY_AMT = nSaleAmt;
                    m_cPayItem.OCCUR_ENTRY_01 = sGiftNo;                                                                // 카드번호
                    m_cPayItem.OCCUR_ENTRY_02 = sAppNo;                                                                 // 승인번호
                    m_cPayItem.OCCUR_ENTRY_03 = sAppDate;                                                               // 승인일자
                    m_cPayItem.OCCUR_ENTRY_04 = "";                                                                     // 승인시간
                }

                // 결제구분 (1:결제, 2:할인)
                DataRow dr = PosMstManager.GetMstPayDc(new string[] { m_cPosStatus.Base.CmpCd, m_cPosStatus.Base.StoreNo, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01 });

                m_cPayItem.PAY_AMT = CmUtil.DoubleSubtraction(m_cPayItem.PAY_AMT, nChangeMoney);
                m_cPayItem.PAY_AMT = CmUtil.DoubleSubtraction(m_cPayItem.PAY_AMT, nTicketAmt);

                m_cPayItem.PAY_DTL_CD_02 = "";                                                                          // 캠페인코드
                m_cPayItem.PAY_DTL_CD_03 = "0";                                                                         // 할인금액 아이템 적용 여부 (0:미적용, 1:적용)
                m_cPayItem.PAY_DTL_CD_04 = "";                                                                          // 상품코드
                m_cPayItem.PAY_DTL_CD_05 = PosConst.PAY_DC_TYPE.PAY;                                                    // 할인결제구분 (1:결제, 2:할인)

                m_cPayItem.QTY_ENTRY_01 = nQty;                                                                         // 수량
                m_cPayItem.QTY_ENTRY_02 = 0;    
                m_cPayItem.QTY_ENTRY_03 = 0;
                m_cPayItem.QTY_ENTRY_04 = 0;
                m_cPayItem.QTY_ENTRY_05 = 0;

                if (sCHANGE_PAYMNT_YN == "1")
                {
                    // 거스름 가능인 경우
                    m_cPayItem.AMT_ENTRY_01 = nSaleAmt;                                                                 // 받은돈
                    m_cPayItem.AMT_ENTRY_02 = nChangeMoney;                                                             // 거스름
                    m_cPayItem.AMT_ENTRY_05 = 0;
                }
                else
                {
                    // 거스름 불가인 경우 받은금액은 결제금액으로 한다.
                    m_cPayItem.AMT_ENTRY_01 = m_cPayItem.PAY_AMT;                                                       // 받은돈
                    m_cPayItem.AMT_ENTRY_02 = 0;                                                                        // 거스름
                    m_cPayItem.AMT_ENTRY_05 = nSaleAmt;
                }
                //m_cPayItem.AMT_ENTRY_01 = m_cPayItem.PAY_AMT;                                                           // 받은돈
                m_cPayItem.AMT_ENTRY_03 = nTicketAmt;                                                                   // 잔액교환권 금액
                m_cPayItem.AMT_ENTRY_04 = 0;
                m_cPayItem.AMT_ENTRY_08 = 0;
                m_cPayItem.AMT_ENTRY_09 = 0;
                m_cPayItem.AMT_ENTRY_10 = 0;

                m_cPayItem.OCCUR_ENTRY_05 = sWcc;                                                                       // 입력 구분
                m_cPayItem.OCCUR_ENTRY_06 = sOrgAppNo;                                                                  // 원승인번호
                m_cPayItem.OCCUR_ENTRY_07 = sOrgAppDate;                                                                // 원승인일자
                m_cPayItem.OCCUR_ENTRY_09 = sCHANGE_PAYMNT_YN;                                                          // 거스름 가능 여부
                m_cPayItem.OCCUR_ENTRY_08 = ItemConst.PAY_APP_DIV.NORMAL;                                               // 승인구분
                m_cPayItem.OCCUR_ENTRY_10 = (sTicketNo.Trim() != "") ? "1" : "0";                                       // 잔액교환권 발행여부
                //잔액교환권 발생구분 수정(2017.07.24)
                //m_cPayItem.OCCUR_ENTRY_11 = sTicketType;                                                                // 잔액교환권 발생구분                
                m_cPayItem.OCCUR_ENTRY_11 = sMakeType;                                                                  // 잔액교환권 발생구분
                m_cPayItem.OCCUR_ENTRY_12 = sTicketNo;                                                                  // 잔액교환권 번호
                m_cPayItem.OCCUR_ENTRY_13 = sInqFlag;                                                                   // 상품권 승인서버 ID
                m_cPayItem.OCCUR_ENTRY_14 = sAppYN;                                                                     // 인증여부
                m_cPayItem.OCCUR_ENTRY_15 = sGiftType;                                                                  // 상품권 권종

                m_cPayItem.OCCUR_ENTRY_16 = CmUtil.GetDataRowStr(dr, PosMst.MST_PAY_DC.DATA.APPR_VEND_CD);              // VAN 구분
                m_cPayItem.OCCUR_ENTRY_18 = sGiftNM;                                                                    // 상품권 명
                m_cPayItem.OCCUR_ENTRY_20 = GetPayDtlCdToPayDtlName(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01);   // 결제수단명
                m_cPayItem.OCCUR_ENTRY_21 = CmUtil.GetDataRowStr(dr, PosMst.MST_PAY_DC.DATA.CASH_BILL_ISSUE_YN);        // 현금영수증 발행여부
                m_cPayItem.OCCUR_ENTRY_22 = "";
                m_cPayItem.BILLSPR_NO = m_cTrnStatus.Sale.BillSplitNo;                                                  // 빌분리 번호

                sRet = UserCom.RST_OK;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }

        /// <summary>
        /// 연습 데이터 설정
        /// </summary>
        public string SetTrainingData(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                // 연습모드이면 연습 데이터 설정

                string sPosMenuKey = aParam[0];                             // 메뉴키
                m_cPayItem.PAY_WAY_CD = aParam[1];                          // 결제 그룹코드
                m_cPayItem.PAY_DTL_CD_01 = aParam[2];                       // 결제 상세코드
                m_cPayItem.CANCEL_DIV = ItemConst.PAY_CANCEL_DIV.NORMAL;    // 취소여부

                string sInqFlag = aParam[3];                                // 승인서버ID
                string sWcc = aParam[4];                                    // 입력구분
                string sGiftNo = aParam[5];                                 // 번호
                double nQty = CmUtil.DoubleParse(aParam[6]);                // 수량
                double nSaleAmt = CmUtil.DoubleParse(aParam[7]);            // 금액
                string sTicketType = aParam[8];                             // 티켓발행구분
                string sTicketNo = aParam[9];                               // 티켓번호
                double nTicketAmt = CmUtil.DoubleParse(aParam[10]);         // 티켓금액
                string sMakeType = aParam[11];                              // 티켓발생구분
                string sAppNo = aParam[12];                                 // 승인번호
                string sAppDate = aParam[13];                               // 승인일자
                double nChangeMoney = CmUtil.DoubleParse(aParam[14]);       // 거스름
                string sAppYN = aParam[15];                                 // 인증여부
                string sGiftType = aParam[16];                              // 권종
                string sGiftNM = aParam[17];                                // 상품권명
                string sCHANGE_PAYMNT_YN = aParam[18];                      // 거스름 가능여부

                // 결제구분 (1:결제, 2:할인)
                DataRow dr = PosMstManager.GetMstPayDc(new string[] { m_cPosStatus.Base.CmpCd, m_cPosStatus.Base.StoreNo, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01 });

                m_cPayItem.PAY_AMT = nSaleAmt;

                m_cPayItem.PAY_AMT = CmUtil.DoubleSubtraction(m_cPayItem.PAY_AMT, nChangeMoney);
                m_cPayItem.PAY_AMT = CmUtil.DoubleSubtraction(m_cPayItem.PAY_AMT, nTicketAmt);

                m_cPayItem.PAY_DTL_CD_02 = "";                                                                          // 캠페인코드
                m_cPayItem.PAY_DTL_CD_03 = "0";                                                                         // 할인금액 아이템 적용 여부 (0:미적용, 1:적용)
                m_cPayItem.PAY_DTL_CD_04 = "";                                                                          // 상품코드
                m_cPayItem.PAY_DTL_CD_05 = PosConst.PAY_DC_TYPE.PAY;                                                    // 할인결제구분 (1:결제, 2:할인)

                m_cPayItem.QTY_ENTRY_01 = nQty;                                                                         // 수량
                m_cPayItem.QTY_ENTRY_02 = 0;
                m_cPayItem.QTY_ENTRY_03 = 0;
                m_cPayItem.QTY_ENTRY_04 = 0;
                m_cPayItem.QTY_ENTRY_05 = 0;

                if (sCHANGE_PAYMNT_YN == "1")
                {
                    // 거스름 가능인 경우
                    m_cPayItem.AMT_ENTRY_01 = nSaleAmt;                                                                 // 받은돈
                    m_cPayItem.AMT_ENTRY_02 = nChangeMoney;                                                             // 거스름
                    m_cPayItem.AMT_ENTRY_05 = 0;
                }
                else
                {
                    // 거스름 불가인 경우 받은금액은 결제금액으로 한다.
                    m_cPayItem.AMT_ENTRY_01 = m_cPayItem.PAY_AMT;                                                       // 받은돈
                    m_cPayItem.AMT_ENTRY_02 = 0;                                                                        // 거스름
                    m_cPayItem.AMT_ENTRY_05 = nSaleAmt;
                }
                //m_cPayItem.AMT_ENTRY_01 = m_cPayItem.PAY_AMT;                                                           // 받은돈
                m_cPayItem.AMT_ENTRY_03 = nTicketAmt;                                                                   // 잔액교환권 금액
                m_cPayItem.AMT_ENTRY_04 = 0; 
                m_cPayItem.AMT_ENTRY_08 = 0;
                m_cPayItem.AMT_ENTRY_09 = 0;
                m_cPayItem.AMT_ENTRY_10 = 0;

                m_cPayItem.OCCUR_ENTRY_01 = sGiftNo;                                                                    // 카드번호
                m_cPayItem.OCCUR_ENTRY_02 = "99999999";                                                                 // 승인번호
                m_cPayItem.OCCUR_ENTRY_03 = m_cPosStatus.Base.SaleDate;                                                 // 승인일자
                m_cPayItem.OCCUR_ENTRY_04 = DateTime.Now.ToString("HHmmss");                                            // 승인시간
                m_cPayItem.OCCUR_ENTRY_05 = sWcc;                                                                       // 입력 구분
                m_cPayItem.OCCUR_ENTRY_06 = "";                                                                         // 원승인번호
                m_cPayItem.OCCUR_ENTRY_07 = "";                                                                         // 원승인일자
                m_cPayItem.OCCUR_ENTRY_08 = ItemConst.PAY_APP_DIV.COMPULSION;                                           // 승인구분
                m_cPayItem.OCCUR_ENTRY_09 = sCHANGE_PAYMNT_YN;                                                          // 거스름 가능 여부
                m_cPayItem.OCCUR_ENTRY_10 = "0";                                                                        // 잔액교환권 발행여부
                m_cPayItem.OCCUR_ENTRY_11 = "";                                                                         // 잔액교환권 발생구분
                m_cPayItem.OCCUR_ENTRY_12 = "";                                                                         // 잔액교환권 번호
                m_cPayItem.OCCUR_ENTRY_13 = sInqFlag;                                                                   // 상품권 승인서버 ID
                m_cPayItem.OCCUR_ENTRY_14 = sAppYN;                                                                     // 인증여부
                m_cPayItem.OCCUR_ENTRY_15 = sGiftType;                                                                  // 상품권 권종
                m_cPayItem.OCCUR_ENTRY_16 = CmUtil.GetDataRowStr(dr, PosMst.MST_PAY_DC.DATA.APPR_VEND_CD);              // VAN 구분
                m_cPayItem.OCCUR_ENTRY_18 = sGiftNM;                                                                    // 상품권 명
                m_cPayItem.OCCUR_ENTRY_20 = GetPayDtlCdToPayDtlName(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01);   // 결제수단명
                m_cPayItem.OCCUR_ENTRY_21 = CmUtil.GetDataRowStr(dr, PosMst.MST_PAY_DC.DATA.CASH_BILL_ISSUE_YN);        // 현금영수증 발행여부
                m_cPayItem.OCCUR_ENTRY_22 = "";

                m_cPayItem.CANCEL_DIV = ItemConst.PAY_CANCEL_DIV.NORMAL;                                                // 취소여부
                m_cPayItem.BILLSPR_NO = m_cTrnStatus.Sale.BillSplitNo;                                                  // 빌분리 번호

                sRet = UserCom.RST_OK;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
         
        /// <summary>
        /// 현금 대체 실행
        /// </summary>
        public string ExecuteCashIrt(string sDealType, string[] aParam)
        {
            string sRet = UserCom.RST_ERR; 

            try
            {
                string sPosMenuKey = aParam[0];                             // 메뉴키
                m_cPayItem.PAY_WAY_CD = aParam[1];                          // 결제 그룹코드
                m_cPayItem.PAY_DTL_CD_01 = aParam[2];                       // 결제 상세코드
                m_cPayItem.CANCEL_DIV = ItemConst.PAY_CANCEL_DIV.NORMAL;    // 취소여부

                string sInqFlag = aParam[3];                                // 승인서버id
                string sWcc = aParam[4];                                    // 입력구분
                string sGiftNo = aParam[5];                                 // 번호
                double nQty = CmUtil.DoubleParse(aParam[6]);                // 수량
                double nSaleAmt = CmUtil.DoubleParse(aParam[7]);            // 금액
                string sTicketType = aParam[8];                             // 티켓발행구분
                string sTicketNo = aParam[9];                               // 티켓번호
                double nTicketAmt = CmUtil.DoubleParse(aParam[10]);         // 티켓금액
                string sMakeType = aParam[11];                              // 티켓발생구분
                string sAppNo = aParam[12];                                 // 승인번호
                string sAppDate = aParam[13];                               // 승인일자
                double nChangeMoney = CmUtil.DoubleParse(aParam[14]);       // 거스름
                string sAppYN = aParam[15];                                 // 인증여부
                string sOrgAppNo = aParam[16];                              // 원승인번호
                string sOrgAppDate = aParam[17];                            // 원승인일자

                // 연습모드이면 연습 데이터 설정
                if (m_cPosStatus.Base.TrainingFlag == "1" || m_cPosStatus.Mst.TestStorYn == "1" ) return SetTrainingData(new string[] { sPosMenuKey, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sInqFlag, sWcc, sGiftNo, nQty.ToString()
                                    , nSaleAmt.ToString(), sTicketType, sTicketNo, nTicketAmt.ToString(), sMakeType, sAppNo, sAppDate, nChangeMoney.ToString(), sAppYN, "", "" });

                // 상품권 현금 대체 승인 처리
                m_cPayItem.PAY_AMT = nSaleAmt;
                m_cPayItem.OCCUR_ENTRY_01 = "";                                                                         // 카드번호
                m_cPayItem.OCCUR_ENTRY_02 = "";                                                                         // 승인번호
                m_cPayItem.OCCUR_ENTRY_03 = "";                                                                         // 승인일자
                m_cPayItem.OCCUR_ENTRY_04 = "";                                                                         // 승인시간

                // 결제구분 (1:결제, 2:할인)
                DataRow dr = PosMstManager.GetMstPayDc(new string[] { m_cPosStatus.Base.CmpCd, m_cPosStatus.Base.StoreNo, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01 });

                m_cPayItem.PAY_AMT = CmUtil.DoubleSubtraction(m_cPayItem.PAY_AMT, nChangeMoney);

                m_cPayItem.PAY_AMT = CmUtil.DoubleSubtraction(m_cPayItem.PAY_AMT, nChangeMoney);
                m_cPayItem.PAY_AMT = CmUtil.DoubleSubtraction(m_cPayItem.PAY_AMT, nTicketAmt);

                m_cPayItem.PAY_DTL_CD_02 = "";                                                                          // 캠페인코드
                m_cPayItem.PAY_DTL_CD_03 = "0";                                                                         // 할인금액 아이템 적용 여부 (0:미적용, 1:적용)
                m_cPayItem.PAY_DTL_CD_04 = "";                                                                          // 상품코드
                m_cPayItem.PAY_DTL_CD_05 = PosConst.PAY_DC_TYPE.PAY;                                                    // 할인결제구분 (1:결제, 2:할인)

                m_cPayItem.QTY_ENTRY_01 = 0;                                                                            // 수량
                m_cPayItem.QTY_ENTRY_02 = 0;
                m_cPayItem.QTY_ENTRY_03 = 0;
                m_cPayItem.QTY_ENTRY_04 = 0;
                m_cPayItem.QTY_ENTRY_05 = 0;

                m_cPayItem.AMT_ENTRY_01 = m_cPayItem.PAY_AMT;                                                           // 받은돈
                m_cPayItem.AMT_ENTRY_02 = nChangeMoney;                                                                 // 거스름
                m_cPayItem.AMT_ENTRY_03 = 0;                                                                            // 
                m_cPayItem.AMT_ENTRY_04 = 0;
                m_cPayItem.AMT_ENTRY_05 = 0;
                m_cPayItem.AMT_ENTRY_08 = 0;
                m_cPayItem.AMT_ENTRY_09 = 0;
                m_cPayItem.AMT_ENTRY_10 = 0;

                m_cPayItem.OCCUR_ENTRY_05 = "";                                                                         // 입력 구분
                m_cPayItem.OCCUR_ENTRY_06 = "";                                                                         // 원승인번호
                m_cPayItem.OCCUR_ENTRY_07 = "";                                                                         // 원승인일자
                m_cPayItem.OCCUR_ENTRY_08 = ItemConst.PAY_APP_DIV.NORMAL;                                               // 승인구분
                m_cPayItem.OCCUR_ENTRY_10 = "";                                                                         // 잔액교환권 발행여부
                m_cPayItem.OCCUR_ENTRY_11 = "";                                                                         // 잔액교환권 발생구분                
                m_cPayItem.OCCUR_ENTRY_12 = "";                                                                         // 잔액교환권 번호
                m_cPayItem.OCCUR_ENTRY_13 = "";                                                                         // 상품권 승인서버 ID
                m_cPayItem.OCCUR_ENTRY_14 = "";                                                                         // 인증여부

                m_cPayItem.OCCUR_ENTRY_16 = CmUtil.GetDataRowStr(dr, PosMst.MST_PAY_DC.DATA.APPR_VEND_CD);              // VAN 구분
                m_cPayItem.OCCUR_ENTRY_20 = GetPayDtlCdToPayDtlName(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01);   // 결제수단명
                m_cPayItem.OCCUR_ENTRY_21 = CmUtil.GetDataRowStr(dr, PosMst.MST_PAY_DC.DATA.CASH_BILL_ISSUE_YN);        // 현금영수증 발행여부
                m_cPayItem.OCCUR_ENTRY_22 = "";
                m_cPayItem.BILLSPR_NO = m_cTrnStatus.Sale.BillSplitNo;                                                  // 빌분리 번호

                sRet = UserCom.RST_OK;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }

        #endregion

        #region Host 소켓 통신
        /// <summary>
        /// Host 소켓 통신
        /// </summary>
        private string ExecuteHostIrt(string sTradeDiv, string sDealType, string[] aParam, ref Hashtable htRecvData)
        {
            string sRet = UserCom.RST_ERR; 
            int iRecvTimeOut = 10000;
            Hashtable htSendData = new Hashtable();
            string sCode = "";
            double nAmt = 0; 

            try
            {
                // Body 생성
                sRet = GetMakeSendDataBody(sDealType, aParam, ref htSendData);
                if (sRet != UserCom.RST_OK) return sRet;

                if (aParam[0] == ItemConst.IRT_INQ_TYPE.EXCHANGE_TICKET_INQ)
                {
                    sCode = aParam[4];                               // 번호
                    nAmt = CmUtil.DoubleParse(aParam[5]);            // 금액
                }
                else if (aParam[0] == ItemConst.IRT_INQ_TYPE.GIFT_APPROVE_INQ)
                {
                    sCode = aParam[5];                               // 번호
                }
                else  
                {
                    sCode = aParam[5];                               // 번호
                }

                // 요청 - 승인로그 저장   (판매구분, [0]결제수단, [1]결제상세코드, [2]전문구분, [3]요청구분, [4]카드번호, [5]결제금액, [6]승인번호, [7]승인일자, [8]승인시간, [9]응답상태값, [10]응답메시지, [11]전문)
                m_cDataCommon.SetSaleApprLog(sTradeDiv, new string[] { m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sDealType, "S", sCode, nAmt.ToString(), "", "", "", "", "", JsonConvert.SerializeObject(htSendData) });

                switch (aParam[0])
                {
                    // ----------------------------------------------------------------------------------------------------------------------------------------------------------------
                    // 잔액교환권
                    // ----------------------------------------------------------------------------------------------------------------------------------------------------------------
                    case ItemConst.IRT_INQ_TYPE.EXCHANGE_TICKET_INQ:
                        {
                            // 전송
                            sRet = m_cDataCommon.ExecutePosIrt(ItemConst.COMM_MSG_TYPE.POSIRT, m_cPosStatus.Base.CommSvrIp, (int)m_cPosStatus.Base.PayInqPort, iRecvTimeOut, htSendData, ref htRecvData);
                            if (sRet != UserCom.RST_OK)
                            {
                                WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0005));
                                sRet = UserCom.RST_ERR;
                                break;
                            }

                            // 응답 - 승인로그 저장   (판매구분, [0]결제수단, [1]결제상세코드, [2]전문구분, [3]요청구분, [4]카드번호, [5]결제금액, [6]승인번호, [7]승인일자, [8]승인시간, [9]응답상태값, [10]응답메시지, [11]전문)
                            m_cDataCommon.SetSaleApprLog(sTradeDiv, new string[] { m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sDealType, "R"
                                                            , sCode, nAmt.ToString()
                                                            , "", DateTime.Now.ToString("yyyyMMdd"), DateTime.Now.ToString("HHmmss")
                                                            , htRecvData[Column.IQ_EXCHAGE_TICKET_RSP.DATA.RES_CD].ToString()
                                                            , htRecvData[Column.IQ_EXCHAGE_TICKET_RSP.DATA.RES_MSG].ToString() 
                                                            , JsonConvert.SerializeObject(htRecvData) });

                            // 응답 체크 
                            if (htRecvData[Column.IQ_EXCHAGE_TICKET_RSP.DATA.RES_CD].ToString() != "00")
                            {
                                WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0187) + "\n\r" + "(" + htRecvData[Column.IQ_EXCHAGE_TICKET_RSP.DATA.RES_CD].ToString() + ")" + htRecvData[Column.IQ_LOTTERY_NUMBERINQ_RSP.DATA.RES_MSG]);

                                sRet = UserCom.RST_ERR;
                                break;
                            }
                            break;
                        }
                    // ----------------------------------------------------------------------------------------------------------------------------------------------------------------

                    // ----------------------------------------------------------------------------------------------------------------------------------------------------------------
                    // 상품권
                    // ----------------------------------------------------------------------------------------------------------------------------------------------------------------
                    case ItemConst.IRT_INQ_TYPE.GIFT_APPROVE_INQ:
                        {
                            // 전송
                            sRet = m_cDataCommon.ExecutePosIrt(ItemConst.COMM_MSG_TYPE.POSIRT, m_cPosStatus.Base.CommSvrIp, (int)m_cPosStatus.Base.PayInqPort, iRecvTimeOut, htSendData, ref htRecvData);
                            if (sRet != UserCom.RST_OK)
                            {
                                WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0005));
                                sRet = UserCom.RST_ERR;
                                break;
                            }
                            // 응답 - 승인로그 저장   (판매구분, [0]결제수단, [1]결제상세코드, [2]전문구분, [3]요청구분, [4]카드번호, [5]결제금액, [6]승인번호, [7]승인일자, [8]승인시간, [9]응답상태값, [10]응답메시지, [11]전문)
                            m_cDataCommon.SetSaleApprLog(sTradeDiv, new string[] { m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, sDealType, "R"
                                                            , sCode, nAmt.ToString()
                                                            , htRecvData[Column.IQ_GIFT_APP_RSP.DATA.APPROVE_NO].ToString(), htRecvData[Column.IQ_GIFT_APP_RSP.DATA.APPROVE_DATE].ToString(), DateTime.Now.ToString("HHmmss")
                                                            , htRecvData[Column.IQ_GIFT_APP_RSP.DATA.RES_CD].ToString()
                                                            , htRecvData[Column.IQ_GIFT_APP_RSP.DATA.MESSAGE1].ToString() + " " +  htRecvData[Column.IQ_GIFT_APP_RSP.DATA.MESSAGE1].ToString()
                                                            , JsonConvert.SerializeObject(htRecvData) });

                            // 응답 체크 (0000:정상, 1100:기취소)
                            if (htRecvData[Column.IQ_GIFT_APP_RSP.DATA.RES_CD].ToString() != "0000" || htRecvData[Column.IQ_GIFT_APP_RSP.DATA.RES_CD].ToString() == "1100")
                            {
                                if (sDealType != ItemConst.GIFT_APPROVE_DIV.NET_CANCEL)
                                    WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0207) + "\n\r" + "(" + htRecvData[Column.IQ_GIFT_APP_RSP.DATA.RES_CD].ToString() + ")" + htRecvData[Column.IQ_GIFT_APP_RSP.DATA.MESSAGE1] + "\n\r" + htRecvData[Column.IQ_GIFT_APP_RSP.DATA.MESSAGE2]);

                                sRet = UserCom.RST_ERR;
                                break;
                            }
                            break;
                        }
                    // ----------------------------------------------------------------------------------------------------------------------------------------------------------------
                }

            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion

        #region Host Body 생성
        /// <summary>
        /// Host Body 생성
        /// </summary>
        private string GetMakeSendDataBody(string sDealType, string[] aParam, ref Hashtable htSendData)
        {
            string sRet = UserCom.RST_ERR;

            try
            {
                string sPosMenuKey = aParam[0];                                         // 전문구분
                m_cPayItem.PAY_WAY_CD = aParam[1];                                      // 결제 그룹코드
                m_cPayItem.PAY_DTL_CD_01 = aParam[2];                                   // 결제 상세코드

                switch (sPosMenuKey)
                {
                    case ItemConst.IRT_INQ_TYPE.EXCHANGE_TICKET_INQ:
                        {
                            string sTicketType = aParam[3];                             // 처리구분
                            string sTicketNo = aParam[4];                               // 번호
                            double nSaleAmt = CmUtil.DoubleParse(aParam[5]);            // 금액

                            // 잔액교환권 조회
                            htSendData.Add(Column.IQ_EXCHAGE_TICKET_REQ.DATA.INQ_TYPE, sPosMenuKey);
                            htSendData.Add(Column.IQ_EXCHAGE_TICKET_REQ.DATA.STOR_CD, m_cPosStatus.Base.StoreNo);
                            htSendData.Add(Column.IQ_EXCHAGE_TICKET_REQ.DATA.SALE_DT, m_cPosStatus.Base.SaleDate);
                            htSendData.Add(Column.IQ_EXCHAGE_TICKET_REQ.DATA.POS_NO, m_cPosStatus.Base.PosNo);
                            htSendData.Add(Column.IQ_EXCHAGE_TICKET_REQ.DATA.ISSUE_TYPE, sTicketType);
                            htSendData.Add(Column.IQ_EXCHAGE_TICKET_REQ.DATA.TICKET_NO, sTicketNo);
                            htSendData.Add(Column.IQ_EXCHAGE_TICKET_REQ.DATA.TICKET_AMT, nSaleAmt);
                            htSendData.Add(Column.IQ_EXCHAGE_TICKET_REQ.DATA.USER_ID, m_cPosStatus.Base.CashierNo);
                            htSendData.Add(Column.IQ_EXCHAGE_TICKET_REQ.DATA.RES_CD, "");
                            htSendData.Add(Column.IQ_EXCHAGE_TICKET_REQ.DATA.RES_MSG, "");

                            break;
                        }
                    case ItemConst.IRT_INQ_TYPE.GIFT_APPROVE_INQ:
                        {
                            // 상품권
                            string sInqFlag = aParam[3];                                // 처리구분
                            string sWcc = aParam[4];                                    // 입력구분
                            string sGiftCD = aParam[5];                                 // 번호
                            string sOrgAppNo = aParam[6];                               // 원승인번호
                            string sOrgAppDate = aParam[7];                             // 원승인일자

                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.INQ_TYPE, sPosMenuKey);
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.INQ_MODE, "");                            
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.DEAL_TYPE, sDealType);
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.INQ_FLAG, sInqFlag);
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.STOR_CD, m_cPosStatus.Base.StoreNo);
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.SALE_DT, m_cPosStatus.Base.SaleDate);
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.POS_NO, m_cPosStatus.Base.PosNo);
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.REG_NO, m_cPosStatus.Base.TradeNo);
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.OP_NO, m_cPosStatus.Base.CashierNo);
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.WCC, sWcc);

                            // 승인처리 경로
                            string sAppr_PAth = ItemConst.GIFT_APPROVE_PATH.STORE;
                            if (sDealType == ItemConst.GIFT_APPROVE_DIV.NET_CANCEL)
                                sAppr_PAth = ItemConst.GIFT_APPROVE_PATH.NET_CANCEL;
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.APPR_PATH, sAppr_PAth);
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.GIFT_NO, sGiftCD);
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.ORG_APPROVE_NO, sOrgAppNo);
                            htSendData.Add(Column.IQ_GIFT_APP_REQ.DATA.ORG_SALE_DT, sOrgAppDate);

                            break;
                        }
                }

                sRet = UserCom.RST_OK;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion
        
    }
}
