using System;
using System.Collections;
using System.Text;

using Cosmos.UserFrame;
using Cosmos.ServiceProvider;
using Cosmos.Common;
using Cosmos.CommonManager;
using System.Data;

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 해피포인트 - 중국
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Service
{
    class PointHappyPointUse_China : PaymentBase, IPaymentUs
    {

        public PointHappyPointUse_China()
        {
        }

        #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;
            string sPosMenuKey = "", sWcc = "", siNqType = "", sCardData = "", sTranType = "";
            double nPayAmt = 0;
            try
            {
                sPosMenuKey = aParam[0];                        // 메뉴키
                sWcc = aParam[1];                               // 카드번호
                sCardData = aParam[2];                          // 입력구분
                nPayAmt = CmUtil.DoubleParse(aParam[3]);        // 금액
                
                m_cPayItem = new Column.TR_PAYMENT.DATA();      // 결과 저장 변수 생성
                m_cPayItem.PAY_WAY_CD = GetMenuKeyToPayWayCd(sPosMenuKey);
                m_cPayItem.PAY_DTL_CD_01 = GetMenuKeyToPayDtlCd(sPosMenuKey);

                // 해피포인트 적립카드로 조회된 내역이 있는지 체크
                ArrayList alEtcItem = (ArrayList)StateObject.GetItemObject(Column.TR_ETC.ITEM);     // 기타 내역 받아 오기
                foreach (Column.TR_ETC.DATA cEtcItem in alEtcItem)
                {
                    if (cEtcItem.CANCEL_DIV == PosConst.CANCEL_DIV.CANCEL) continue;

                    if (cEtcItem.ETC_WAY_CD == ItemConst.TR_ITEM_ID.ETC_INFO_ITEM && cEtcItem.ETC_DTL_CD_01 == ItemConst.TR_ITEM_ID.ETC_INFO.POINT_SAVE_CHINA)
                    {
                        if (cEtcItem.CanFiller1 == sCardData)
                        {
                            m_cPayItem.AMT_ENTRY_03 = cEtcItem.AMT_ENTRY_03;            // 총포인트
                            m_cPayItem.AMT_ENTRY_04 = cEtcItem.AMT_ENTRY_04;            // 가용포인트
                            m_cPayItem.AMT_ENTRY_05 = cEtcItem.AMT_ENTRY_05;            // 잔여포인트

                            //m_cPayItem.HP_ADD_INFO_SCH = cEtcItem.HP_ADD_INFO_SCH;      // 추가응답정보
                            //m_cPayItem.HP_RESERVED = cEtcItem.HP_RESERVED;              // 예약필드
                            //m_cPayItem.HP_CAMPAIGN_INFO = cEtcItem.HP_CAMPAIGN_INFO;    // 캠페인정보

                            //m_cPayItem.HP_EXT_POINT = cEtcItem.HP_EXT_POINT;            // 소멸예정포인트

                            return UserCom.RST_OK;
                        }
                    }
                }

                siNqType = PosConst.POS_POINT_INQ_TYPE_CHINA.SEARCH;
                if (sPosMenuKey == PosKey.MENU_KEY.HAPPY_POINT_USE_CHINA)
                    // 포인트 사용 조회
                    sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.SEARCH_POINT;
                else if (sPosMenuKey == PosKey.MENU_KEY.HAPPY_POINT_SAVE_CHINA)
                    // 포인트 적립 조회
                    sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.SEARCH_POINT;
                else if (sPosMenuKey == PosKey.MENU_KEY.PREPAID_CARDS_HPJOY)
                    // 선불 조회
                    sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.SEARCH_PP;

                sRet = ExecuteHappyPointUseIrt(PosConst.CANCEL_DIV.NORMAL, siNqType, sTranType, sWcc, sCardData, nPayAmt, "", "" );
                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);
            }
            finally
            {
                //여전법 대응!
                //////////////////////////////////////////////////////////////////////////////////
                CmUtil.ZeroFillClear(ref sCardData); 
                CmUtil.ZeroFillClear(ref aParam);
                //////////////////////////////////////////////////////////////////////////////////
            }
            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;
            string sPosMenuKey = "", sWcc = "", siNqType = "", sCardData = "", sTranType = "";
            double nPayAmt = 0;

            try
            {
                sPosMenuKey = aParam[0];                        // 메뉴키
                sWcc = aParam[1];                               // 카드번호
                sCardData = aParam[2];                          // 입력구분
                nPayAmt = CmUtil.DoubleParse(aParam[3]);        // 금액

                //m_cPayItem = new Column.TR_PAYMENT.DATA();      // 결과 저장 변수 생성
                ArrayList alEtcItem = (ArrayList)StateObject.GetItemObject(Column.TR_ETC.ITEM);     // 기타 내역 받아 오기
                m_cPayItem.PAY_WAY_CD = GetMenuKeyToPayWayCd(sPosMenuKey);
                m_cPayItem.PAY_DTL_CD_01 = GetMenuKeyToPayDtlCd(sPosMenuKey);

                if (sPosMenuKey == PosKey.MENU_KEY.HAPPY_POINT_USE_CHINA)
                {
                    // 포인트 사용
                    siNqType = PosConst.POS_POINT_INQ_TYPE_CHINA.USE;
                    sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_POINT_APPROVAL;
                }
                else if (sPosMenuKey == PosKey.MENU_KEY.HAPPY_POINT_SAVE_CHINA)
                {
                    // 포인트 적립
                    siNqType = PosConst.POS_POINT_INQ_TYPE_CHINA.SAVE;
                    sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.SAVE_PP_APPROVAL;
                }
                else if (sPosMenuKey == PosKey.MENU_KEY.PREPAID_CARDS_HPJOY)
                {
                    // 선불 조회
                    siNqType = PosConst.POS_POINT_INQ_TYPE_CHINA.USE;
                    sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_PP_APPROVAL;
                }
                sRet = ExecuteHappyPointUseIrt(PosConst.CANCEL_DIV.NORMAL, siNqType, sTranType, sWcc, sCardData, nPayAmt, "", "");
                if (sRet != UserCom.RST_OK) return sRet;
                 
                // 결제 아이템 추가
                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; 
            }
            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
            {
                //여전법 대응!
                //////////////////////////////////////////////////////////////////////////////////
                CmUtil.ZeroFillClear(ref sCardData); 
                CmUtil.ZeroFillClear(ref aParam);
                //////////////////////////////////////////////////////////////////////////////////
            }
            return sRet;
        }
        #endregion

        #region CancelPayment 결제 취소
        /// <summary>
        /// 결제 취소
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public string CancelPayment(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;
            string sWcc = "", siNqType = "", sCardData = "", sTranType = "";
            string sPAY_WAY_CD = "";
            string sPAY_DTL_CD_01 = "";
            double nPayAmt = 0;

            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];

                sPAY_WAY_CD = m_cPayItem.PAY_WAY_CD;
                sPAY_DTL_CD_01 = m_cPayItem.PAY_DTL_CD_01;

                sWcc = m_cPayItem.OCCUR_ENTRY_05;                       // 카드번호
                sCardData = m_cPayItem.OCCUR_ENTRY_01;                  // 입력구분
                nPayAmt = m_cPayItem.PAY_AMT;                           // 금액

                string sApprDate = m_cPayItem.OCCUR_ENTRY_03;           // 승인일자
                string sApprNo = m_cPayItem.OCCUR_ENTRY_02;             // 승인번호

                if (sPAY_WAY_CD == ItemConst.TR_ITEM_ID.POINT_ITEM)
                {
                    // 포인트 사용
                    siNqType = PosConst.POS_POINT_INQ_TYPE_CHINA.USE;
                    sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_POINT_REFUND;
                }
                else if (sPAY_WAY_CD == ItemConst.TR_ITEM_ID.ETC_INFO_ITEM)
                {
                    // 포인트 적립
                    siNqType = PosConst.POS_POINT_INQ_TYPE_CHINA.SAVE;
                    sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.SAVE_POINT_REFUND;
                }
                else if (sPAY_WAY_CD == ItemConst.TR_ITEM_ID.PPCARD_ITEM)
                {
                    // 선불  
                    siNqType = PosConst.POS_POINT_INQ_TYPE_CHINA.USE;
                    sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_PP_REFUND;
                }

                sRet = ExecuteHappyPointUseIrt(PosConst.CANCEL_DIV.CANCEL, siNqType, sTranType, sWcc, sCardData, nPayAmt, sApprDate, sApprNo);
                if (sRet != UserCom.RST_OK)
                {      
                    if (siNqType == PosConst.POS_POINT_INQ_TYPE_CHINA.SAVE && sTranType == PosConst.POS_POINT_TRAN_TYPE_CHINA.SAVE_POINT_REFUND)
                        sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.SAVE_POINT_NETREFUND;
                    else if (siNqType == PosConst.POS_POINT_INQ_TYPE_CHINA.SAVE && sTranType == PosConst.POS_POINT_TRAN_TYPE_CHINA.SAVE_PP_REFUND)
                        sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.SAVE_PP_NETREFUND;

                    else if (siNqType == PosConst.POS_POINT_INQ_TYPE_CHINA.USE && sTranType == PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_POINT_REFUND)
                        sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_POINT_NETREFUND;
                    else if (siNqType == PosConst.POS_POINT_INQ_TYPE_CHINA.USE && sTranType == PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_PP_REFUND)
                        sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_PP_NETREFUND;

                    sRet = ExecuteHappyPointUseIrt(PosConst.CANCEL_DIV.CANCEL, siNqType, sTranType, sWcc, sCardData, nPayAmt, sApprDate, sApprNo);
                    if (sRet != UserCom.RST_OK) return sRet;
                }

                m_cPayItem.CANCEL_DIV = PosConst.CANCEL_DIV.CANCEL;

                ICampaignUs cCampaingMain = (ICampaignUs)sManager.InitServiceInstance(ServiceLists.ASV_CAMPAIGN.DLL, ServiceLists.ASV_CAMPAIGN.CAMPAIGN_MAIN);
                // 캠페인 취소 처리
                cCampaingMain.CancelCampaignPayment(aParam[0], "");

                m_cDataService.UpdatePluAmount();       // 상품 합계금액 계산(거래해더)
                m_cDataService.UpdatePayAmount();       // 결제 금액 재 계산 처리
                 
                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
            {
                //여전법 대응!
                //////////////////////////////////////////////////////////////////////////////////
                CmUtil.ZeroFillClear(ref sCardData); 
                CmUtil.ZeroFillClear(ref aParam);
                //////////////////////////////////////////////////////////////////////////////////
            }
            return sRet;
        }
        #endregion

        #region RefundPayment 결제 반품
        /// <summary>
        /// 결제 반품
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public string RefundPayment(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;
            string sWcc = "", siNqType = "", sCardData = "", sTranType = "";
            double nPayAmt = 0;
            string sPAY_WAY_CD = "";
            string sPAY_DTL_CD_01 = "";

            try
            {
                int nPayRow = CmUtil.IntParse(aParam[0]); // 취소할 행번호
                
                // 결제 내역 받아 오기
                ArrayList alPayItem = (ArrayList)StateObject.GetItemObject(Column.TR_PAYMENT.ITEM);
                m_cPayItem = (Column.TR_PAYMENT.DATA)alPayItem[nPayRow];

                sPAY_WAY_CD = m_cPayItem.PAY_WAY_CD;
                sPAY_DTL_CD_01 = m_cPayItem.PAY_DTL_CD_01;

                sWcc = m_cPayItem.OCCUR_ENTRY_05;                       // 카드번호
                sCardData = m_cPayItem.OCCUR_ENTRY_01;                  // 입력구분
                nPayAmt = m_cPayItem.PAY_AMT;                           // 금액

                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 sApprDate = m_cPayItem.OCCUR_ENTRY_03;           // 승인일자
                    string sApprNo = m_cPayItem.OCCUR_ENTRY_02;             // 승인번호

                    if (sPAY_WAY_CD == ItemConst.TR_ITEM_ID.POINT_ITEM)
                    {
                        // 포인트 사용
                        siNqType = PosConst.POS_POINT_INQ_TYPE_CHINA.USE;
                        sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_POINT_REFUND;
                    }
                    else if (sPAY_WAY_CD == ItemConst.TR_ITEM_ID.ETC_INFO_ITEM)
                    {
                        // 포인트 적립
                        siNqType = PosConst.POS_POINT_INQ_TYPE_CHINA.SAVE;
                        sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.SAVE_POINT_REFUND;
                    }
                    else if (sPAY_WAY_CD == ItemConst.TR_ITEM_ID.PPCARD_ITEM)
                    {
                        // 선불  
                        siNqType = PosConst.POS_POINT_INQ_TYPE_CHINA.USE;
                        sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_PP_REFUND;
                    }

                    sRet = ExecuteHappyPointUseIrt(PosConst.CANCEL_DIV.CANCEL, siNqType, sTranType, sWcc, sCardData, nPayAmt, sApprDate, sApprNo);
                    if (sRet != UserCom.RST_OK)
                    {
                        if (siNqType == PosConst.POS_POINT_INQ_TYPE_CHINA.SAVE && sTranType == PosConst.POS_POINT_TRAN_TYPE_CHINA.SAVE_POINT_REFUND)
                            sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.SAVE_POINT_NETREFUND;
                        else if (siNqType == PosConst.POS_POINT_INQ_TYPE_CHINA.SAVE && sTranType == PosConst.POS_POINT_TRAN_TYPE_CHINA.SAVE_PP_REFUND)
                            sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.SAVE_PP_NETREFUND;

                        else if (siNqType == PosConst.POS_POINT_INQ_TYPE_CHINA.USE && sTranType == PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_POINT_REFUND)
                            sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_POINT_NETREFUND;
                        else if (siNqType == PosConst.POS_POINT_INQ_TYPE_CHINA.USE && sTranType == PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_PP_REFUND)
                            sTranType = PosConst.POS_POINT_TRAN_TYPE_CHINA.USE_PP_NETREFUND;

                        sRet = ExecuteHappyPointUseIrt(PosConst.CANCEL_DIV.CANCEL, siNqType, sTranType, sWcc, sCardData, nPayAmt, sApprDate, sApprNo);
                        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
            {
                //여전법 대응!
                //////////////////////////////////////////////////////////////////////////////////
                CmUtil.ZeroFillClear(ref sCardData); 
                CmUtil.ZeroFillClear(ref aParam);
                //////////////////////////////////////////////////////////////////////////////////
            }
            return sRet;
        }
        #endregion

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

        #region 해피포인트 조회,사용
        /// <summary>
        /// 해피포인트 조회,사용
        /// </summary>
        public string ExecuteHappyPointUseIrt(string sTradeDiv, string siNqType, string sTranType, string sWcc, string sCardData, double nPayAmt, string sApprDate, string sApprNo)
        {
            string sRet = UserCom.RST_ERR;
            try
            { 
                // 연습모드이면 연습 데이터 설정
                if (m_cPosStatus.Base.TrainingFlag == "1" || m_cPosStatus.Mst.TestStorYn == "1") return SetTrainingData(sTradeDiv, siNqType, sTranType, sWcc, sCardData, nPayAmt, sApprDate, sApprNo);
                 
                string sVanCD = GetPayDtlCdToVanCode(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01);    // 승인업체 코드 조회
                if (sVanCD == "")
                {
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0563);
                    return sRet;
                }

                // 포인트 조회,사용,사용취소
                string[] aIrtRsp = null;
                string[] aIrtHdrRsp = null;
                sRet = ExecuteIrt(sTradeDiv, m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, siNqType, sTranType, sWcc, sCardData, nPayAmt, sApprDate, sApprNo, ref aIrtHdrRsp, ref aIrtRsp);
                if (sRet != UserCom.RST_OK) return sRet;

                if (sTradeDiv == PosConst.CANCEL_DIV.NORMAL)
                {
                    m_cPayItem.PAY_AMT = nPayAmt;

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

                    m_cPayItem.AMT_ENTRY_01 = m_cPayItem.PAY_AMT;
                    m_cPayItem.AMT_ENTRY_02 = 0;
                    m_cPayItem.AMT_ENTRY_03 = 0;
                    m_cPayItem.AMT_ENTRY_04 = 0;
                    m_cPayItem.AMT_ENTRY_05 = 0; 
                    if (siNqType == PosConst.POS_POINT_INQ_TYPE_CHINA.SEARCH)
                    {
                        // 포인트 소숫점 처리(2017.06.26)
                        m_cPayItem.AMT_ENTRY_04 = CmUtil.DoubleParse(aIrtRsp[Column.POS_REQ_SEL_POINT_CHINA.SEQ.SaleAmt1] + "." + aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.SaleAmt2]);    // 가용포인트
                        //m_cPayItem.AMT_ENTRY_04 = CmUtil.DoubleParse(aIrtRsp[Column.POS_REQ_SEL_POINT_CHINA.SEQ.SaleAmt1]);  // 가용포인트
                    }
                    else
                    {
                        // 카카통 결제금액 응답 받은 실제 결제금액으로 설정(2017.07.17)
                        m_cPayItem.PAY_AMT = CmUtil.DoubleParse(aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.SaleAmt1] + "." + aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.SaleAmt2]);             // 결제금액
                        m_cPayItem.AMT_ENTRY_01 = m_cPayItem.PAY_AMT;

                        // 포인트 소숫점 처리(2017.06.26)
                        m_cPayItem.AMT_ENTRY_03 = CmUtil.DoubleParse(aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.BalanceAmt1] + "." + aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.BalanceAmt2]);  // 거래후잔액 
                        //m_cPayItem.AMT_ENTRY_04 = CmUtil.DoubleParse(aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.BalanceAmt1]);  // 거래후잔액
                        m_cPayItem.OCCUR_ENTRY_02 = aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.ApprNo].Trim();                  // 승인번호
                        m_cPayItem.OCCUR_ENTRY_17 = aIrtHdrRsp[Column.POS_POINT_HEADER_CHINA.SEQ.UniqueID].Trim();          // 거래고유번호                                                                     // 가맹점번호
                    }

                    m_cPayItem.OCCUR_ENTRY_01 = sCardData;                                                                  // 카드번호
                    m_cPayItem.OCCUR_ENTRY_03 = DateTime.Now.ToString("yyMMdd");                                            // 승인일자
                    m_cPayItem.OCCUR_ENTRY_04 = DateTime.Now.ToString("HHmmss");                                            // 승인시간
                    m_cPayItem.OCCUR_ENTRY_05 = sWcc;                                                                       // 입력 구분
                    m_cPayItem.OCCUR_ENTRY_06 = sApprNo;                                                                    // 원승인번호
                    m_cPayItem.OCCUR_ENTRY_07 = sApprDate;                                                                  // 원승인일자
                    m_cPayItem.OCCUR_ENTRY_08 = ItemConst.PAY_APP_DIV.NORMAL;                                               // 승인구분

                    m_cPayItem.OCCUR_ENTRY_16 = sVanCD;                                                                     // VAN 구분
                    m_cPayItem.OCCUR_ENTRY_18 = "";                                                                         // 가맹점번호
                    m_cPayItem.OCCUR_ENTRY_20 = GetPayDtlCdToPayDtlName(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01);   // 결제수단명
                    m_cPayItem.OCCUR_ENTRY_21 = GetPayDtlCdToCashBillYn(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01);   // 현금영수증 발행여부

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

                    m_cPayItem.CanFiller1 = "";

                    m_cPayItem.HP_RESERVED = "";                                                                            // 예약필드
                    m_cPayItem.HP_CAMPAIGN_INFO = "";                                                                       // 캠페인정보
                }
                else
                {
                    // 포인트 소숫점 처리(2017.06.26)
                    m_cPayItem.AMT_ENTRY_03 = CmUtil.DoubleParse(aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.BalanceAmt1] + "." + aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.BalanceAmt2]);  // 거래후잔액 

                    m_cPayItem.OCCUR_ENTRY_01 = sCardData;                                                                  // 카드번호
                    m_cPayItem.OCCUR_ENTRY_03 = DateTime.Now.ToString("yyMMdd");                                            // 승인일자
                    m_cPayItem.OCCUR_ENTRY_04 = DateTime.Now.ToString("HHmmss");                                            // 승인시간
                    m_cPayItem.OCCUR_ENTRY_05 = sWcc;                                                                       // 입력 구분
                    m_cPayItem.OCCUR_ENTRY_06 = sApprNo;                                                                    // 원승인번호
                    m_cPayItem.OCCUR_ENTRY_07 = sApprDate;                                                                  // 원승인일자
                }
                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
            {
                //여전법 대응!
                //////////////////////////////////////////////////////////////////////////////////
                CmUtil.ZeroFillClear(ref sCardData);
                CmUtil.ZeroFillClear(ref sCardData);
                //////////////////////////////////////////////////////////////////////////////////
            }
            return sRet;
        }

        /// <summary>
        /// 연습 데이터 설정
        /// </summary>
        public string SetTrainingData(string sTradeDiv, string siNqType, string sTranType, string sWcc, string sCardData, double nPayAmt, string sApprDate, string sApprNo)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                // 연습모드이면 연습 데이터 설정
                string sVanCD = GetPayDtlCdToVanCode(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01);    // 승인업체 코드 조회
                if (sVanCD == "")
                {
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0563);
                    return sRet;
                }
                
                m_cPayItem.PAY_AMT = nPayAmt;

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

                m_cPayItem.AMT_ENTRY_01 = m_cPayItem.PAY_AMT;                   // 사용포인트
                m_cPayItem.AMT_ENTRY_03 = 99990;                                // 총포인트
                m_cPayItem.AMT_ENTRY_04 = 99990;                                // 가용포인트
                m_cPayItem.AMT_ENTRY_05 = 99990;                                // 잔여포인트

                m_cPayItem.OCCUR_ENTRY_01 = sCardData;                          // 카드번호
                m_cPayItem.OCCUR_ENTRY_02 = "99999999";                         // 승인번호
                m_cPayItem.OCCUR_ENTRY_03 = DateTime.Now.ToString("yyMMdd");    // 승인일자
                m_cPayItem.OCCUR_ENTRY_04 = DateTime.Now.ToString("HHmm");      // 승인시간
                m_cPayItem.OCCUR_ENTRY_05 = sWcc;                               // 입력 구분
                m_cPayItem.OCCUR_ENTRY_06 = sApprNo;                            // 원승인번호
                m_cPayItem.OCCUR_ENTRY_07 = sApprDate;                          // 원승인일자
                m_cPayItem.OCCUR_ENTRY_08 = ItemConst.PAY_APP_DIV.COMPULSION;   //승인구분
                
                m_cPayItem.OCCUR_ENTRY_16 = sVanCD;                             // VAN 구분
                m_cPayItem.OCCUR_ENTRY_18 = "";                                 // 가맹점번호
                m_cPayItem.OCCUR_ENTRY_20 = GetPayDtlCdToPayDtlName(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01);   // 결제수단명
                m_cPayItem.OCCUR_ENTRY_21 = GetPayDtlCdToCashBillYn(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01);   // 현금영수증 발행여부

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

                m_cPayItem.CanFiller1 = "";

                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
            {
                //여전법 대응!
                //////////////////////////////////////////////////////////////////////////////////
                CmUtil.ZeroFillClear(ref sCardData);
                CmUtil.ZeroFillClear(ref sCardData);
                //////////////////////////////////////////////////////////////////////////////////
            }
            return sRet;
        }
        #endregion

        #region 포인트 조회 실행
        /// <summary>
        /// 포인트 조회 실행
        /// </summary>
        private string ExecuteIrt(string sTradeDiv, string sPAY_WAY_CD, string sPAY_DTL_CD_01, string siNqType, string sTranType, string sWcc, string sCardData, double nPayAmt, string sApprDate, string sApprNo, ref string[] aIrtHdrRsp, ref string[] aIrtRsp)
        {
            string sSendData = "", sRecvData = "";
            int nNowPos = 0;
            string sRet = UserCom.RST_ERR;

            string sServerIP = "";
            string sServerPort = "";
            int iRecvTimeOut = 10000;

            try
            {
                // VAN 정보 조회
                if (m_cPayItem.OCCUR_ENTRY_16.Trim().Length != 0)
                {
                    // VAN_ID가 있는 경우 VAN_ID로 VAN 정보 조회
                    sServerIP = PosMstManager.GetMstVan(m_cPayItem.OCCUR_ENTRY_16.Trim(), PosMst.MST_VAN.DATA.IP);
                    sServerPort = PosMstManager.GetMstVan(m_cPayItem.OCCUR_ENTRY_16.Trim(), PosMst.MST_VAN.DATA.PORT);
                }
                else
                {
                    // VAN_ID가 미정인 경우 결제수단으로 VAN 정보 조회
                    sServerIP = PosMstManager.GetMstVan(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, PosMst.MST_VAN.DATA.IP);
                    sServerPort = PosMstManager.GetMstVan(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, PosMst.MST_VAN.DATA.PORT);
                }

                if (sServerIP.Trim() == "")
                {
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0565);
                    return sRet;
                }

                // 포인트 전문 생성                
                sSendData = MakePoint(siNqType, sTranType, sWcc, sCardData, nPayAmt, sApprDate, sApprNo);

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

                // 전문 송수신
                sRet = m_cDataCommon.ExecuteLenIrt(sServerIP, CmUtil.IntParse(sServerPort), iRecvTimeOut, sSendData, ref sRecvData, 4, 0);
                if (sRet != UserCom.RST_OK)
                {
                    WinManager.ErrorMessage(sRet);
                    return sRet;
                }

                byte[] bytes = Encoding.Default.GetBytes(sRecvData);
                aIrtHdrRsp = new string[Column.POS_POINT_HEADER_CHINA.LEN.Length];
                ItemColumn.ParseMessage(Column.POS_POINT_HEADER_CHINA.LEN, Column.POS_POINT_HEADER_CHINA.TYPE, bytes, ref nNowPos, ref aIrtHdrRsp);
                if (siNqType == PosConst.POS_POINT_INQ_TYPE_CHINA.SEARCH)
                {
                    aIrtRsp = new string[Column.POS_REQ_SEL_POINT_CHINA.LEN.Length];
                    ItemColumn.ParseMessage(Column.POS_REQ_SEL_POINT_CHINA.LEN, Column.POS_REQ_SEL_POINT_CHINA.TYPE, bytes, ref nNowPos, ref aIrtRsp);

                    // 응답 - 승인로그 저장   (판매구분, [0]결제수단, [1]결제상세코드, [2]전문구분, [3]요청구분, [4]카드번호, [5]결제금액, [6]승인번호, [7]승인일자, [8]승인시간, [9]응답상태값, [10]응답메시지, [11]전문)
                    m_cDataCommon.SetSaleApprLog(sTradeDiv, new string[] { sPAY_WAY_CD, sPAY_DTL_CD_01, siNqType, "R"
                                                            , sCardData, CmUtil.DoubleParse(aIrtRsp[Column.POS_REQ_SEL_POINT_CHINA.SEQ.SaleAmt1]).ToString()
                                                            , aIrtRsp[Column.POS_REQ_SEL_POINT_CHINA.SEQ.ApprNo].Trim(), CmUtil.MidH(aIrtHdrRsp[Column.POS_POINT_HEADER_CHINA.SEQ.SaleDate], 0, 6), CmUtil.MidH(aIrtHdrRsp[Column.POS_POINT_HEADER_CHINA.SEQ.SaleTime], 6, 4)
                                                            , aIrtHdrRsp[Column.POS_POINT_HEADER_CHINA.SEQ.ResCD].Trim()
                                                            , aIrtRsp[Column.POS_REQ_SEL_POINT_CHINA.SEQ.Message1].Trim() + aIrtRsp[Column.POS_REQ_SEL_POINT_CHINA.SEQ.Message2].Trim()
                                                            , sRecvData });
                    // - '0000O' : 승인 '그외' : 거절
                    if (aIrtHdrRsp[Column.POS_POINT_HEADER_CHINA.SEQ.ResCD] != "00000")
                    {
                        if (aIrtRsp[Column.POS_REQ_SEL_POINT_CHINA.SEQ.Message1] == "" && aIrtRsp[Column.POS_REQ_SEL_POINT_CHINA.SEQ.Message2].Trim() == "")
                            sRet = MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0158) + "[" + aIrtHdrRsp[Column.POS_POINT_HEADER_CHINA.SEQ.ResCD] + "]";
                        else
                            sRet = aIrtRsp[Column.POS_REQ_SEL_POINT_CHINA.SEQ.Message1].Trim() + "\n" + aIrtRsp[Column.POS_REQ_SEL_POINT_CHINA.SEQ.Message2].Trim();
                        if (m_bErrMsgShow == true) WinManager.ErrorMessage(sRet);
                        return sRet;
                    }
                }
                else
                {
                    aIrtRsp = new string[Column.POS_REQ_POINT_CHINA.LEN.Length];
                    ItemColumn.ParseMessage(Column.POS_REQ_POINT_CHINA.LEN, Column.POS_REQ_POINT_CHINA.TYPE, bytes, ref nNowPos, ref aIrtRsp);

                    // 응답 - 승인로그 저장   (판매구분, [0]결제수단, [1]결제상세코드, [2]전문구분, [3]요청구분, [4]카드번호, [5]결제금액, [6]승인번호, [7]승인일자, [8]승인시간, [9]응답상태값, [10]응답메시지, [11]전문)
                    m_cDataCommon.SetSaleApprLog(sTradeDiv, new string[] { sPAY_WAY_CD, sPAY_DTL_CD_01, siNqType, "R"
                                                            , sCardData, CmUtil.DoubleParse(aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.SaleAmt1]).ToString()
                                                            , aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.ApprNo].Trim(), CmUtil.MidH(aIrtHdrRsp[Column.POS_POINT_HEADER_CHINA.SEQ.SaleDate], 0, 6), CmUtil.MidH(aIrtHdrRsp[Column.POS_POINT_HEADER_CHINA.SEQ.SaleTime], 6, 4)
                                                            , aIrtHdrRsp[Column.POS_POINT_HEADER_CHINA.SEQ.ResCD].Trim()
                                                            , aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.Message1].Trim() + aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.Message2].Trim()
                                                            , sRecvData });
                    // - '0000O' : 승인 '그외' : 거절
                    if (aIrtHdrRsp[Column.POS_POINT_HEADER_CHINA.SEQ.ResCD] != "00000")
                    {
                        if (aIrtRsp[Column.POS_REQ_SEL_POINT_CHINA.SEQ.Message1] == "" && aIrtRsp[Column.POS_REQ_SEL_POINT_CHINA.SEQ.Message2].Trim() == "")
                            sRet = MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0158) + "[" + aIrtHdrRsp[Column.POS_POINT_HEADER_CHINA.SEQ.ResCD] + "]";
                        else
                            sRet = aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.Message1].Trim() + "\n" + aIrtRsp[Column.POS_REQ_POINT_CHINA.SEQ.Message2].Trim();
                        if (m_bErrMsgShow == true) WinManager.ErrorMessage(sRet);
                        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 포인트 요청 전문 생성
        /// <summary>
        /// 포인트 요청 전문 생성
        /// </summary>
        /// <param name="sPointType"></param>
        /// <param name="sInPutType"></param>
        /// <param name="sCardData"></param>
        /// <param name="nPayAmt"></param>
        /// <param name="sApprDate"></param>
        /// <param name="sApprNo"></param>
        /// <returns></returns>
        private string MakePoint(string siNqType, string sTranType, string sWcc, string sCardData, double nPayAmt, string sApprDate, string sApprNo)
        {
            string sSendData = string.Empty;
            string sSendBodyData = string.Empty;
            int nDataLen = 0; 
            try
            {
                // 카드번호 = 이하 자름
                if (sCardData.IndexOf("=") > 0) sCardData = CmUtil.MidH(sCardData, 0, sCardData.IndexOf("="));

                // 가맹점 정보 획득(회사승인ID,점포승인ID)
                string sApprID = PosMstManager.GetMstVan(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, PosMst.MST_VAN.DATA.APPR_ID);

                if (siNqType == PosConst.POS_POINT_INQ_TYPE_CHINA.SEARCH)
                {
                    // 조회
                    string[] aIrtBody = new string[Column.POS_RES_SEL_POINT_CHINA.LEN.Length];
                    // Body 길이
                    nDataLen = ItemColumn.LenMessage(Column.POS_RES_SEL_POINT_CHINA.LEN);

                    aIrtBody[Column.POS_RES_SEL_POINT_CHINA.SEQ.CardNo] = sCardData;
                    sSendBodyData = ItemColumn.MakeMessage(aIrtBody, Column.POS_RES_SEL_POINT_CHINA.LEN, Column.POS_RES_SEL_POINT_CHINA.TYPE);
                }
                else
                {
                    // 사용/적립/충전
                    string[] aIrtBody = new string[Column.POS_RES_POINT_CHINA.LEN.Length];
                    // Body 길이
                    nDataLen = ItemColumn.LenMessage(Column.POS_RES_POINT_CHINA.LEN);

                    aIrtBody[Column.POS_RES_POINT_CHINA.SEQ.CardNo] = sCardData;
                    // 포인트 소숫점 처리(2017.06.26)
                    aIrtBody[Column.POS_RES_POINT_CHINA.SEQ.SaleAmt1] = ((long)nPayAmt).ToString();
                    aIrtBody[Column.POS_RES_POINT_CHINA.SEQ.SaleAmt2] = (CmUtil.DoubleMultiplication(CmUtil.DoubleSubtraction(nPayAmt,(long)nPayAmt), 100)).ToString(); 
                    //aIrtBody[Column.POS_RES_POINT_CHINA.SEQ.SaleAmt1] = nPayAmt.ToString();
                    //aIrtBody[Column.POS_RES_POINT_CHINA.SEQ.SaleAmt2] = "0";
                    aIrtBody[Column.POS_RES_POINT_CHINA.SEQ.OrgApprNo] = sApprNo;
                    aIrtBody[Column.POS_RES_POINT_CHINA.SEQ.OrgSaleDate] = sApprDate;
                    sSendBodyData = ItemColumn.MakeMessage(aIrtBody, Column.POS_RES_POINT_CHINA.LEN, Column.POS_RES_POINT_CHINA.TYPE);
                }                
                
                // 헤더
                string[] aIrtHeader = new string[Column.POS_POINT_HEADER_CHINA.LEN.Length];
                // 헤더 길이
                nDataLen += ItemColumn.LenMessage(Column.POS_POINT_HEADER_CHINA.LEN) - 4;

                aIrtHeader[Column.POS_POINT_HEADER_CHINA.SEQ.InqLen] = nDataLen.ToString();
                aIrtHeader[Column.POS_POINT_HEADER_CHINA.SEQ.InqType] = siNqType;
                aIrtHeader[Column.POS_POINT_HEADER_CHINA.SEQ.TranType] = sTranType;
                aIrtHeader[Column.POS_POINT_HEADER_CHINA.SEQ.TeminalID] = sApprID;
                aIrtHeader[Column.POS_POINT_HEADER_CHINA.SEQ.SaleDate] = DateTime.Now.ToString("yyyyMMdd");
                aIrtHeader[Column.POS_POINT_HEADER_CHINA.SEQ.SaleTime] = DateTime.Now.ToString("HHmmss");
                aIrtHeader[Column.POS_POINT_HEADER_CHINA.SEQ.UniqueID] = m_cPosStatus.Base.SaleDate + m_cPosStatus.Base.PosNo + m_cPosStatus.Base.TradeNo + string.Format("{0:00000}", DateTime.Now.ToString("ssfff"));
                aIrtHeader[Column.POS_POINT_HEADER_CHINA.SEQ.ResCD] =  "";
                aIrtHeader[Column.POS_POINT_HEADER_CHINA.SEQ.WCC] = sWcc;
                aIrtHeader[Column.POS_POINT_HEADER_CHINA.SEQ.Filler] = "";                 

                // 포인트 요청 전문 생성
                sSendData = ItemColumn.MakeMessage(aIrtHeader, Column.POS_POINT_HEADER_CHINA.LEN, Column.POS_POINT_HEADER_CHINA.TYPE);
                sSendData += sSendBodyData;
            }
            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 sSendData;
        }
        #endregion
    }
}
