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 Foreign : PaymentBase, IPaymentUs
    {

        public Foreign()
        {
        }

        #region 결제 조회
        /// <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;

            try
            {
               
                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 결제 정보 획득
        /// <summary>
        /// 결제 정보 획득
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public object GetPayment(string[] aParam)
        {
            return m_cPayItem;
        }
        #endregion

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

        #region 결제 등록
        /// <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];                                       // 결제 상세코드

                double nForeignAmt = CmUtil.DoubleParse(aParam[3].ToString());              // 받은금액(외화)
                double nPayAmt = CmUtil.DoubleParse(aParam[4].ToString());                  // 받은금액(원화)
                double nChangeAmt = CmUtil.DoubleParse(aParam[5].ToString());               // 거스름금액
                double nExchangeAmt = CmUtil.DoubleParse(aParam[6].ToString());             // 환율

                sRet = ExecuteIrt(sPosMenuKey, nForeignAmt, nPayAmt, nChangeAmt, nExchangeAmt);
                if (sRet != UserCom.RST_OK) return sRet;

                if (nChangeAmt > 0)
                    m_cPosStatus.Sale.ChangeCashAmt = nChangeAmt;         // 거스름
                //if (nChangeAmt > 0)
                //    m_cPosStatus.Sale.ChangeCashAmt =  nChangeAmt;                               // 거스름

                // 결제 아이템 추가
                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);
            }
            return sRet;
        }
        #endregion

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

            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 sPosMenuKey = PosKey.MENU_KEY.FOREIGN_CURRENCY;                  // 메뉴키

                double nForeignAmt = m_cPayItem.AMT_ENTRY_03;                           // 받은금액(외화)
                double nPayAmt = m_cPayItem.AMT_ENTRY_01;                               // 받은금액(원화)
                double nChangeAmt = m_cPayItem.AMT_ENTRY_02;                            // 거스름금액
                double nExchangeAmt = m_cPayItem.AMT_ENTRY_04;                          // 환율

                sRet = ExecuteIrt(sPosMenuKey, nForeignAmt, nPayAmt, nChangeAmt, nExchangeAmt);
                if (sRet != UserCom.RST_OK) 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

                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);
            }
            return sRet;
        }
        #endregion

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

            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 sPosMenuKey = PosKey.MENU_KEY.FOREIGN_CURRENCY;                  // 메뉴키

                double nForeignAmt = m_cPayItem.AMT_ENTRY_03;                           // 받은금액(외화)
                double nPayAmt = m_cPayItem.AMT_ENTRY_01;                               // 받은금액(원화)
                double nChangeAmt = m_cPayItem.AMT_ENTRY_02;                            // 거스름금액
                double nExchangeAmt = m_cPayItem.AMT_ENTRY_04;                          // 환율

                sRet = ExecuteIrt(sPosMenuKey, nForeignAmt, nPayAmt, nChangeAmt, nExchangeAmt); 
            }
            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 sPosMenuKey, double nForeignAmt, double nPayAmt, double nChangeAmt, double nExchangeAmt)
        {
            string sRet = UserCom.RST_ERR;
            Hashtable htRecvData = new Hashtable();

            try
            {
                // 연습모드이면 연습 데이터 설정
                if (m_cPosStatus.Base.TrainingFlag == "1" || m_cPosStatus.Mst.TestStorYn == "1" ) return SetTrainingData(sPosMenuKey, nForeignAmt, nPayAmt, nChangeAmt, nExchangeAmt);

                // 결제구분 (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(nPayAmt, nChangeAmt);

                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 = nPayAmt;                                                                      // 받은돈
                m_cPayItem.AMT_ENTRY_02 = nChangeAmt;                                                                   // 거스름
                m_cPayItem.AMT_ENTRY_03 = nForeignAmt;                                                                  // 외화 받은돈
                m_cPayItem.AMT_ENTRY_04 = nExchangeAmt;                                                                 // 환율
                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_01 = "";                                                                         // 카드번호
                m_cPayItem.OCCUR_ENTRY_02 = "";                                                                         // 승인번호
                m_cPayItem.OCCUR_ENTRY_03 = "";                                                                         // 승인일자
                m_cPayItem.OCCUR_ENTRY_04 = "";                                                                         // 승인시간
                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 = "";                                                                         //
                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_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 sPosMenuKey, double nForeignAmt, double nPayAmt, double nChangeAmt, double nExchangeAmt)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                // 연습모드이면 연습 데이터 설정

                // 결제구분 (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 = nPayAmt;

                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 = nPayAmt;                                                                      // 받은돈
                m_cPayItem.AMT_ENTRY_02 = nChangeAmt;                                                                   // 거스름
                m_cPayItem.AMT_ENTRY_03 = nForeignAmt;                                                                  // 외화 받은돈
                m_cPayItem.AMT_ENTRY_04 = nExchangeAmt;                                                                 // 환율
                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_01 = "";                                                                         // 카드번호
                m_cPayItem.OCCUR_ENTRY_02 = "";                                                                         // 승인번호
                m_cPayItem.OCCUR_ENTRY_03 = "";                                                                         // 승인일자
                m_cPayItem.OCCUR_ENTRY_04 = "";                                                                         // 승인시간
                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 = "";                                                                         //
                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_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
    }
}
