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

using Cosmos.BaseFrame;
using Cosmos.UserFrame;
using Cosmos.ServiceProvider;
using Cosmos.Common;
using Cosmos.CommonManager;

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 영수증 반품 처리
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Service
{
    class SaleRefund : SaleTranCallBase, ISaleRefundUs
    {
        /// <summary>
        /// 생성자
        /// </summary>
        public SaleRefund()
        {
         
        }

        /// <summary>
        /// 영수증 반품 대상 거래 데이터 리스트 조회
        /// </summary>
        /// <param name="sSaleDate"></param>
        /// <param name="sPosNo"></param>
        /// <param name="sTradeNo"></param>
        /// <param name="sTradeType"></param>
        /// <returns></returns>
        public DataTable SearchSaleReceiptList(string sSaleDate, string sPosNo, string sTradeNo, string sPayTime, string sTradeType)
        {
            string sQuery = "";
            try
            {
                m_cDataService.TranComplete();

                //#20171123 추후적립, 영수증반품, 영수증재발행 조회시 느린 증상 수정 start
                //아래 select 쿼리의 파라미터 변경

                sQuery += " DECLARE @CMP_CD  AS VARCHAR(5) ";
                sQuery += " DECLARE @STOR_CD AS VARCHAR(10) ";
                sQuery += " DECLARE @POS_NO  AS VARCHAR(2) ";
                sQuery += " DECLARE @SALE_DT AS VARCHAR(8) ";
                sQuery += " SET @CMP_CD  = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += " SET @STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                sQuery += " SET @POS_NO  = '" + sPosNo + "'";
                sQuery += " SET @SALE_DT = '" + sSaleDate + "'";

                //#20171123 추후적립, 영수증반품, 영수증재발행 조회시 느린 증상 수정 end

                if (m_cPosStatus.Base.TrainingFlag == "1")
                {
                    sQuery = "SELECT  A.SALE_DT, A.POS_NO, A.REG_NO AS TRADE_NO, A.SYS_DT, A.PAY_TIME, A.NET_SALE_AMT, A.TRADE_DIV, A.TRADE_KINDPER, ";
                    sQuery += "      A.ORG_BILLDT + A.ORG_BILL_POSNO + A.ORG_BILL_NO ORG_INFO, ISNULL(B.SALE_DT + B.POS_NO + B.TRADE_NO,'') RTN_INFO, A.AMT_DUTCHPAY_YN ";
                    sQuery += "  FROM POSLOG..ETC_SALE_HEADER A ";
                    sQuery += "  LEFT JOIN (SELECT * FROM POSLOG..ETC_SALE_HEADER ";
                    //#20170928 낱개 반품 한 영수증 반품 기능 start
                    //기존
                    //sQuery += "             WHERE CMP_CD = '" + m_cPosStatus.Base.CmpCd + "' AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "' AND TRADE_DIV = '1') B ";
                    //변경
                    sQuery += "             WHERE CMP_CD = '" + m_cPosStatus.Base.CmpCd + "' AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "') B ";
                    //#20170928 낱개 반품 한 영수증 반품 기능 end
                    sQuery += "       ON A.SALE_DT = B.ORG_BILLDT AND A.POS_NO = B.ORG_BILL_POSNO AND A.REG_NO = B.ORG_BILL_NO ";
                    sQuery += "  WHERE A.CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'  ";
                    sQuery += "    AND A.SALE_DT = '" + sSaleDate + "'  ";
                    sQuery += "    AND A.STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'  ";
                    sQuery += "    AND A.TRAIN_MODE_YN= '" + m_cPosStatus.Base.TrainingFlag + "'  ";

                    if (sTradeNo.Trim().Length == 5)
                        sQuery += "    AND A.REG_NO = '" + sTradeNo + "' ";
                    if (sPayTime.Trim().Length == 6)
                        sQuery += "    AND A.PAY_TIME = '" + sPayTime + "' ";
                }
                else
	            {
                    //#20171123 추후적립, 영수증반품, 영수증재발행 조회시 느린 증상 수정 start
                    //기존
                    /*
                    sQuery = "SELECT  A.SALE_DT, A.POS_NO, A.TRADE_NO, A.SYS_DT, A.PAY_TIME, A.NET_SALE_AMT, A.TRADE_DIV, A.TRADE_KINDPER, ";
                    sQuery += "      A.ORG_BILLDT + A.ORG_BILL_POSNO + A.ORG_BILL_NO ORG_INFO, ISNULL(B.SALE_DT + B.POS_NO + B.TRADE_NO,'') RTN_INFO, A.AMT_DUTCHPAY_YN ";
                    sQuery += "  FROM POSLOG..TR_SALE_HEADER A ";
                    sQuery += "  LEFT JOIN (SELECT * FROM POSLOG..TR_SALE_HEADER ";
                    //#20170928 낱개 반품 한 영수증 반품 기능 start
                    //기존
                    //sQuery += "             WHERE CMP_CD = '" + m_cPosStatus.Base.CmpCd + "' AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "' AND TRADE_DIV = '1') B ";
                    //변경
                    sQuery += "             WHERE CMP_CD = '" + m_cPosStatus.Base.CmpCd + "' AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "') B ";
                    //#20170928 낱개 반품 한 영수증 반품 기능 end
                    sQuery += "       ON A.SALE_DT = B.ORG_BILLDT AND A.POS_NO = B.ORG_BILL_POSNO AND A.TRADE_NO = B.ORG_BILL_NO ";
                    sQuery += "  WHERE A.CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'  ";
                    sQuery += "    AND A.SALE_DT = '" + sSaleDate + "'  ";
                    sQuery += "    AND A.STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'  ";
                    sQuery += "    AND A.TRAIN_MODE_YN= '" + m_cPosStatus.Base.TrainingFlag + "'  ";
                    */
                    //변경

                    sQuery += "SELECT  A.SALE_DT, A.POS_NO, A.TRADE_NO, A.SYS_DT, A.PAY_TIME, A.NET_SALE_AMT, A.TRADE_DIV, A.TRADE_KINDPER, ";
                    sQuery += "      A.ORG_BILLDT + A.ORG_BILL_POSNO + A.ORG_BILL_NO ORG_INFO, ISNULL(B.SALE_DT + B.POS_NO + B.TRADE_NO,'') RTN_INFO, A.AMT_DUTCHPAY_YN ";
                    sQuery += "  FROM POSLOG..TR_SALE_HEADER A ";
                    sQuery += "  LEFT JOIN (SELECT * FROM POSLOG..TR_SALE_HEADER ";
                    //#20170928 낱개 반품 한 영수증 반품 기능 start
                    //기존
                    //sQuery += "             WHERE CMP_CD = '" + m_cPosStatus.Base.CmpCd + "' AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "' AND TRADE_DIV = '1') B ";
                    //변경
                    sQuery += "             WHERE CMP_CD = @CMP_CD AND ORG_BILLDT = @SALE_DT AND STOR_CD = @STOR_CD) B ";
                    //#20170928 낱개 반품 한 영수증 반품 기능 end
                    sQuery += "       ON A.SALE_DT = B.ORG_BILLDT AND A.POS_NO = B.ORG_BILL_POSNO AND A.TRADE_NO = B.ORG_BILL_NO ";
                    sQuery += "  WHERE A.CMP_CD = @CMP_CD  ";
                    sQuery += "    AND A.SALE_DT = @SALE_DT  ";
                    sQuery += "    AND A.STOR_CD = @STOR_CD  ";
                    sQuery += "    AND A.TRAIN_MODE_YN= '" + m_cPosStatus.Base.TrainingFlag + "'  ";

                    //#20171123 추후적립, 영수증반품, 영수증재발행 조회시 느린 증상 수정 end

                    if (sTradeNo.Trim().Length == 5)
                        sQuery += "    AND A.TRADE_NO = '" + sTradeNo + "' ";
                    if (sPayTime.Trim().Length == 6)
                        sQuery += "    AND A.PAY_TIME = '" + sPayTime + "' ";
	            }

                if (sPosNo.Trim().Length == 2)
                    sQuery += "    AND A.POS_NO = '" + sPosNo + "' ";
                
                if (sTradeType == "1")
                {
                    sQuery += "    AND A.TRADE_DIV = '0' ";             //정상 
                }
                else if (sTradeType == "2")
                {
                    sQuery += "    AND A.TRADE_DIV = '1' ";             //반품 
                }
                else if (sTradeType == "3")
                {
                    sQuery += "    AND A.TRADE_DIV = '0' ";             
                    sQuery += "    AND ISNULL(B.TRADE_NO,'') = '' ";    //반품건 제외 
                }

                // 영수증반품 => 판매 데이터 만 대상
                // 수정 => 판매, 대외거래(o2o), 선불판매, 상품권판매, LOSS
                //sQuery += "    AND A.TRADE_KINDPER = '" + ItemConst.TRAN_KIND.NORMAL.SALE + "' ";
                sQuery += "    AND A.TRADE_KINDPER IN ( '" + ItemConst.TRAN_KIND.NORMAL.SALE + "','" + ItemConst.TRAN_KIND.FORIGN.O2O + "','" 
                                                           + ItemConst.TRAN_KIND.CHARGESALE.PREPAID + "','" + ItemConst.TRAN_KIND.CHARGESALE.GIFT + "','" 
                                                           + ItemConst.TRAN_KIND.ETC.LOSS + "' )";


                if (m_cPosStatus.Base.TrainingFlag == "1")
                {
                    sQuery += " ORDER BY A.REG_NO DESC ";
                }
                else
                {
                    sQuery += " ORDER BY A.TRADE_NO DESC ";
                }
                

                return m_cMstService.Select(new string[] { sQuery });
            }
            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 null;
        }

        /// <summary>
        /// 영수증 반품 대상 로컬 거래 데이터 로딩
        /// </summary>
        /// <param name="sSaleDate"></param>
        /// <param name="sPosNo"></param>
        /// <param name="sTradeNo"></param>
        /// <returns></returns>
        public bool LoadLocalTran(string sSaleDate, string sPosNo, string sTradeNo)
        {
            bool bRet = false;

            try
            {
                m_cDataService.TranComplete();

                // 해더정보 로딩
                if (LoadLocalTrHeader(sSaleDate, sPosNo, sTradeNo) == false)
                {
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0248);
                    m_cDataService.TranComplete();
                    return bRet;
                }

                // 상품정보 로딩
                LoadLocalTrDetail(sSaleDate, sPosNo, sTradeNo);

                // 결제정보 로딩
                LoadLocalTrPayment(sSaleDate, sPosNo, sTradeNo);

                // 기타정보 로딩
                LoadLocalTrEtc(sSaleDate, sPosNo, sTradeNo);

                // 캠페인정보 로딩
                LoadLocalTrCampaign(sSaleDate, sPosNo, sTradeNo);

                // 매출객수정보 로딩
                LoadLocalTrGuestCnt(sSaleDate, sPosNo, sTradeNo);

                bRet = true;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
                m_cDataService.TranComplete();
            }
            return bRet;
        }

        /// <summary>
        /// 영수증 반품 대상 거래 데이터 서버 조회
        /// </summary>
        /// <param name="sSaleDate"></param>
        /// <param name="sPosNo"></param>
        /// <param name="sTradeNo"></param>
        /// <param name="SaleTime"></param>
        /// <returns></returns>
        public string SearchServerTran(string sSaleDate, string sPosNo, string sTradeNo, string SaleTime)
        {
            string sStat = UserCom.RST_ERR;
            bool bRet = false;

            try
            {

                if (m_cPosStatus.Base.TrainingFlag == ItemConst.TRAINING_FLAG.TRAINING_YES) return sStat;

                m_cDataService.TranComplete();

                // 거래 데이터 서버 조회
                Hashtable htRecvData = new Hashtable();

                sStat = ExecServerTranCallIrt(ItemConst.IRT_INQ_TYPE.TRAN_CALL, sSaleDate, sPosNo, sTradeNo, SaleTime, ref htRecvData);
                if (sStat != UserCom.RST_OK) 
                {
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0259);
                    return sStat;
                }

                if (htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RES_CD].ToString() != "00")
                {
                    if (htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RES_CD].ToString() == "09")
                        WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0248);    // 해당거래 없음
                    else
                        WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0259) + "\n\r ERROR CODE => " + htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RES_CD]);
                    return UserCom.RST_ERR;
                }

                // 거래데이터
                string sTranData = htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.TRAN_DATA].ToString();

                m_cTrnStatus.Head.CmpCd = m_cPosStatus.Base.CmpCd;          // CMP_CD
                m_cTrnStatus.Head.StoreCd = m_cPosStatus.Base.StoreNo;      // STOR_CD
                m_cTrnStatus.Head.SaleDate = sSaleDate;                     // SALE_DT
                m_cTrnStatus.Head.PosNo = sPosNo;                           // POS_NO
                m_cTrnStatus.Head.TradeNo = htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.TRADE_NO].ToString();                       // TRADE_NO

                // 거래 분해 처리
                string[] aTranData = sTranData.Split(new string[] { "#~" }, StringSplitOptions.None);
                for (int i = 0; i < aTranData.Length; i++ )
                {
                    if (aTranData[i].StartsWith(ItemConst.IRT_TRAN_TYPE.TranHeader))
                        bRet = TranParserToHeader(aTranData[i]);
                    else if (aTranData[i].StartsWith(ItemConst.IRT_TRAN_TYPE.TranDetail))
                        bRet = TranParserToDetail(aTranData[i]);
                    else if (aTranData[i].StartsWith(ItemConst.IRT_TRAN_TYPE.TranPay))
                        bRet = TranParserToPayment(aTranData[i]);
                    else if (aTranData[i].StartsWith(ItemConst.IRT_TRAN_TYPE.TranCpiRslt))
                        bRet = TranParserToCampaign(aTranData[i]);
                    else if (aTranData[i].StartsWith(ItemConst.IRT_TRAN_TYPE.TranEtc))
                        bRet = TranParserToEtc(aTranData[i]);
                    //                    else if (aTranData[i].StartsWith(ItemConst.IRT_TRAN_TYPE.TranDpst))
                    //                        bRet = TranParserToGuestCnt(aTranData[i]);
                    //                    else if (aTranData[i].StartsWith(ItemConst.IRT_TRAN_TYPE.TranAdmt))
                    //                        bRet = TranParserToGuestCnt(aTranData[i]);
                    else if (aTranData[i].StartsWith(ItemConst.IRT_TRAN_TYPE.TranGuestCnt))
                        bRet = TranParserToGuestCnt(aTranData[i]);

                    if (bRet == false)
                    {
                        m_cDataService.TranComplete();
                        return sStat;
                    }
                }
                
                // 영수증 반품 체크
                string sRetDIV = htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RET_DIV].ToString();
                if (sRetDIV != "0") // 이미 반품된 거래
                {
                    m_cTrnStatus.Head.RefundInfo = htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RET_SALE_DT].ToString() + htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RET_POS_NO].ToString() + htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RET_TRADE_NO].ToString();
                }
                sStat = 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);
                m_cDataService.TranComplete();
            }
            return sStat;
        }

        /// <summary>
        /// 상품불러오기를 위한 거래 데이터 상품 정보 만 로딩
        /// </summary>
        /// <param name="sSaleDate"></param>
        /// <param name="sPosNo"></param>
        /// <param name="sTradeNo"></param>
        /// <returns></returns>
        public bool LoadLocalTranPlu(string sSaleDate, string sPosNo, string sTradeNo)
        {
            bool bRet = false;
            string AfterTradeDiv = string.Empty;
            string AfterTradeKind = string.Empty;
            try
            {
                ArrayList alTrPluItem = (ArrayList)StateObject.GetItemObject(Column.TR_PLU.ITEM);
                if (alTrPluItem.Count > 0) // 거래호출시 데이터 있음(다시로딩 안함)
                {
                    AfterTradeDiv = m_cTrnStatus.Head.TradeDiv;
                    AfterTradeKind = m_cTrnStatus.Head.TradeKind;

                    // 상품정보를 제외한 모든 정보 클리어
                    m_cDataService.InitTranHeader();

                    //  Loss 거래인지 확인.
                    if (AfterTradeDiv == ItemConst.TRAN_DIV.NORMAL && AfterTradeKind == ItemConst.TRAN_KIND.ETC.LOSS)
                    {                                               
                        // 로스 거래를 복사하는 거라면
                        m_cTrnStatus.Head.TradeDiv = ItemConst.TRAN_DIV.NORMAL;
                        m_cTrnStatus.Head.TradeKind = ItemConst.TRAN_KIND.ETC.LOSS;
                    }
                    
                    ArrayList alPayItem = (ArrayList)StateObject.GetItemObject(Column.TR_PAYMENT.ITEM);
                    alPayItem.Clear();
                    ArrayList alEtcItem = (ArrayList)StateObject.GetItemObject(Column.TR_ETC.ITEM);
                    alEtcItem.Clear();
                    ArrayList alTrCpiItem = (ArrayList)StateObject.GetItemObject(Column.TR_SALE_CPI_RSLT.ITEM);
                    alTrCpiItem.Clear();
                    ArrayList alTrGuest = (ArrayList)StateObject.GetItemObject(Column.TR_GUEST.ITEM);
                    alTrGuest.Clear();

                    return true;
                }
                m_cDataService.TranComplete();

                // 상품정보 로딩
                if (LoadLocalTrDetail(sSaleDate, sPosNo, sTradeNo) == false)
                {
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0248);
                    m_cDataService.TranComplete();
                    return bRet;
                }

                m_cTrnStatus.Head.FloorCd = m_cPosStatus.Sale.SelectFloorCd;
                m_cTrnStatus.Head.FloorNm = m_cPosStatus.Sale.SelectFloorNm;
                m_cTrnStatus.Head.TableNo = m_cPosStatus.Sale.SelectTableNo;
                m_cTrnStatus.Head.TableNm = m_cPosStatus.Sale.SelectTableNm;

                bRet = true;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
                m_cDataService.TranComplete();
            }
            return bRet;
        }


        /// <summary>
        /// 영수증 반품 가능 여부 조회
        /// </summary>
        /// <param name="sSaleDate"></param>
        /// <param name="sPosNo"></param>
        /// <param name="sTradeNo"></param>
        /// <returns></returns>
        public bool ChekcRefundTran(string sSaleDate, string sPosNo, string sTradeNo)
        {
            bool bRet = false;

            try
            {
                if (m_cPosStatus.Base.TrainingFlag == "1") return true;

                // 서버 IRT로 해당 거래 반품 가능여부 체크
                Hashtable htRecvData = new Hashtable();

                string sStat = ExecServerTranCallIrt(ItemConst.IRT_INQ_TYPE.TRAN_CALL, sSaleDate, sPosNo, sTradeNo, "", ref htRecvData, true);


                if (sStat == UserCom.RST_OK)
                {
                    if (htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RES_CD].ToString() == "00")
                    {
                        // 영수증 반품 가능여부 체크
                        string sRetDIV = htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RET_DIV].ToString();

                        if (sRetDIV != "0")
                        {
                            WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0229) + PosConst.CRLF
                                + htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RET_SALE_DT].ToString() + "-" + htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RET_POS_NO].ToString() + "-" + htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RET_TRADE_NO].ToString());


                            //#20171020 log 추가 
                            UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                                System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()",
                                "반품가능여부 : " + m_cTrnStatus.Head.TradeDiv + "," +
                                "반품가능여부 : " + htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RET_DIV].ToString() + "," +
                                "반품거래번호 : " + htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.TRADE_NO].ToString() + "," +
                                "반품거래정보-일자 : " + htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RET_SALE_DT].ToString() + "," +
                                "반품거래정보-POSNO : " + htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RET_POS_NO].ToString() + "," + 
                                "반품거래정보-거래번호 : " + htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RET_TRADE_NO].ToString()
                                );

                            return false;
                        }
                    }
                }

                return true;
            }
            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 bRet;
        }

        //#해피앱 추후적립 start, sskim
        /// <summary>
        /// 해피앱 추후적립 데이터 조회/상태변경
        /// </summary>
        /// <param name="sSaleDate"></param>
        /// <param name="sPosNo"></param>
        /// <param name="sTradeNo"></param>
        /// <param name="sVisibleMode"></param>
        /// <returns></returns>
        public string SearchServerHpAppSave(string sSaleDate, string sPosNo, string sTradeNo, bool sVisibleMode)
        {
            string sStat = UserCom.RST_ERR;

            if (m_cPosStatus.Base.TrainingFlag == ItemConst.TRAINING_FLAG.TRAINING_YES) return sStat;

            // 로컬 적립 데이터 유무 체크
            if (SearchPointSaveYN(sSaleDate, sPosNo, sTradeNo) == true) return sStat;

            // 로컬 추후 적립 데이터 유무 체크
            if (SearchPointAfterSaveYN(sSaleDate, sPosNo, sTradeNo) == true) return sStat;

            // 해피앱 추후적립 데이터 유무 체크(TR_HPAPP_AFTER_SAVE)
            if (SearchTrHpAppAfterSaveYN(sSaleDate, sPosNo, sTradeNo) == false) return sStat;
            
            // 거래 데이터 서버 조회
            try
            {
                Hashtable htSendData = new Hashtable();
                Hashtable htRecvData = new Hashtable();

                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.INQ_TYPE, ItemConst.IRT_INQ_TYPE.TRAN_CALL);
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.STOR_CD, m_cPosStatus.Base.StoreNo);
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.SALE_DT, sSaleDate);
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.POS_NO, sPosNo);
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.TRADE_NO, sTradeNo);
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.PAY_TIME, "");
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.RET_DIV, "");
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.ADD_POINT_DIV, "");
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.ADD_CASH_DIV, "");
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.RET_SALE_DT, "");
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.RET_POS_NO, "");
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.RET_TRADE_NO, "");
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.TRAN_LEN, "");
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.TRAN_DATA, "");
                htSendData.Add(Column.IQ_TRAN_CALL_REQ.DATA.RES_CD, "");

                sStat = m_cDataCommon.ExecutePosIrt(ItemConst.COMM_MSG_TYPE.POSIRT, m_cPosStatus.Base.CommSvrIp, (int)m_cPosStatus.Base.TranInqPort, 10000, htSendData, ref htRecvData, sVisibleMode);

                if (sStat != UserCom.RST_OK)
                {
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0259);
                    return sStat;
                }

                if (htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RES_CD].ToString() != "00")
                {
                    WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0259) + "\n\r ERROR CODE => " + htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.RES_CD]);
                    return UserCom.RST_ERR;
                }

                // 거래데이터
                string sTranData = htRecvData[Column.IQ_TRAN_CALL_REQ.DATA.TRAN_DATA].ToString();

                // 거래 분해 처리
                string[] aTranData = sTranData.Split(new string[] { "#~" }, StringSplitOptions.None);
                for (int i = 0; i < aTranData.Length; i++)
                {
                    if (aTranData[i].StartsWith(ItemConst.IRT_TRAN_TYPE.TranEtc))
                    {
                        bool bRet = InsertTR_SALE_ETC(sSaleDate, sPosNo, sTradeNo, aTranData[i]);
                    }

                }

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

        // #phj 해피앱 추후적립 start
        #region 조건체크- 적립 데이터 체크하기(TR_SALE_ETC)
        /// <summary>
        /// 해당 거래 적립 데이터 유무 확인 
        /// 적립 데이터 있으면 True , 없으면 false
        /// </summary>
        public bool SearchPointSaveYN(string sSaleDate, string sPosNo, string sTradeNo)
        {
            string sSql = string.Empty;

            try
            {
                sSql =  " SELECT *";
                sSql += "   FROM POSLOG..TR_SALE_ETC ";
                sSql += "  WHERE CMP_CD           = '" + m_cPosStatus.Base.CmpCd + "'";
                sSql += "    AND STOR_CD          = '" + m_cPosStatus.Base.StoreNo + "'";
                sSql += "    AND SALE_DT          = '" + sSaleDate + "'";
                sSql += "    AND POS_NO           = '" + sPosNo + "'";
                sSql += "    AND TRADE_NO         = '" + sTradeNo + "'";
                sSql += "    AND ETC_WAY_CD       = '80' ";
                sSql += "    AND cancel_div       = '0'  ";
                sSql += "    AND ETC_DTL_CD_01    = '01' ";

                // 선택한 그리드 값이 적립 되어 있는지 확인 
                DataTable dtTrEtc = m_cMstService.Select(new string[] { sSql });

                if (dtTrEtc == null || dtTrEtc.Rows.Count == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            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 false;
            }
        }
        #endregion 조건체크- 적립 데이터 체크하기

        #region 조건체크- 추후적립 체크하기(ETC_SALE_ETC)
        /// <summary>
        /// 해당 영수증이 추후적립 되어 있는지 체크 
        /// 추후적립 데이터 있으면 True , 없으면 false
        /// </summary>
        public bool SearchPointAfterSaveYN(string sSaleDate, string sPosNo, string sTradeNo)
        {
            try
            {
                string sSql = string.Empty;

                sSql =  " SELECT A.* ";
                sSql += "   FROM POSLOG..ETC_SALE_HEADER A LEFT JOIN POSLOG..ETC_SALE_ETC B ";
                sSql += "     ON (A.CMP_CD = B.CMP_CD AND A.SALE_DT = B.SALE_DT AND A.STOR_CD = B.STOR_CD AND A.POS_NO = B.POS_NO AND A.REG_NO = B.REG_NO) ";
                sSql += "  WHERE A.CMP_CD           = '" + m_cPosStatus.Base.CmpCd + "'";
                sSql += "    AND A.STOR_CD          = '" + m_cPosStatus.Base.StoreNo + "'";
                sSql += "    AND A.ORG_BILL_POSNO   = '" + sPosNo + "'";
                sSql += "    AND A.ORG_BILLDT       = '" + sSaleDate + "'";
                sSql += "    AND A.ORG_BILL_NO      = '" + sTradeNo + "'";
                sSql += "    AND A.TRADE_KINDPER    = '30' ";
                sSql += "    AND B.ETC_WAY_CD       = '80' ";
                sSql += "    AND B.ETC_DTL_CD_01    = '01' ";
                sSql += "    AND B.CANCEL_DIV       = '0'  ";


                // 선택한 그리드 값이 적립 되어 있는지 확인 
                DataTable dtTrETC = m_cMstService.Select(new string[] { sSql });

                if (dtTrETC == null || dtTrETC.Rows.Count == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            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 false;
            }
        }
        #endregion 조건체크- 추후적립 체크하기

        #region 조건체크- 해피앱 추후적립 체크하기(TR_HPAPP_AFTER_SAVE)
        /// <summary>
        /// 해당 영수증이 해피앱 추후적립 데이터 있는지 체크 
        /// 해피앱 추후적립 데이터 있으면 True , 없으면 false
        /// </summary>
        public bool SearchTrHpAppAfterSaveYN(string sSaleDate, string sPosNo, string sTradeNo)
        {
            try
            {
                string sSql = string.Empty;

                sSql = " SELECT *";
                sSql += "   FROM POSLOG..TR_HPAPP_AFTER_SAVE ";
                sSql += "  WHERE CMP_CD           = '" + m_cPosStatus.Base.CmpCd + "'";
                sSql += "    AND STOR_CD          = '" + m_cPosStatus.Base.StoreNo + "'";
                sSql += "    AND SALE_DT          = '" + sSaleDate + "'";
                sSql += "    AND POS_NO           = '" + sPosNo + "'";
                sSql += "    AND TRADE_NO         = '" + sTradeNo + "'";

                // 선택한 그리드 값이 적립 되어 있는지 확인 
                DataTable dtTrETC = m_cMstService.Select(new string[] { sSql });

                if (dtTrETC == null || dtTrETC.Rows.Count == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            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 false;
            }
        }
        #endregion 조건체크- 추후적립 체크하기

        //#해피앱 추후적립 end, sskim

        //2017-06-02 dkshin 해피앱 추후적립
        public bool Refund_HPAPP_After_Update_Tran(string sSaleDate, string sPosNo, string sTradeNo)
        {
            bool bRet = false;

            try
            {
                if (m_cPosStatus.Base.TrainingFlag == "1") return true;

                // 서버 IRT로 해당 거래 반품 가능여부 체크
                Hashtable htRecvData = new Hashtable();

                Refund_HPAPP_After_Save_Update_TranCallIrt(ItemConst.IRT_INQ_TYPE.HPAPP_AFTER_SAVE_UPDATE, sSaleDate, sPosNo, sTradeNo, "", ref htRecvData);

                return true;
            }
            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 bRet;
        }
    }


}
