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

        public iNStorDC()
        {
        }

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

            try
            {
                string sPosMenuKey = aParam[0];                     // 메뉴키
                string sInPutType = aParam[1];                      // 입력구분
                string sInPutData = aParam[2];                      // 입력데이터
                string sCardData = aParam[3];                       // 카드데이터
                double nPayAmt = CmUtil.DoubleParse(aParam[4]);     // 대상금액
            }
            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
            {
                string sPosMenuKey = aParam[0];                     // 메뉴키
                string sInPutType = aParam[1];                      // 입력구분
                string sInPutData = aParam[2];                      // 입력데이터
                string sCardData = aParam[3];                       // 카드데이터
                double nPayAmt = CmUtil.DoubleParse(aParam[4]);     // 대상금액
                double nSaleAmt = CmUtil.DoubleParse(aParam[5]);    // 받을금액
                string sCouponGroup = aParam[6];                    // 쿠폰그룹
                string sCouponNm = aParam[7];                       // 쿠폰명칭

                string sTrType = "";
                string sPointType = "";

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

                m_cPayItem.PAY_WAY_CD = GetMenuKeyToPayWayCd(sPosMenuKey);
                m_cPayItem.PAY_DTL_CD_01 = GetMenuKeyToPayDtlCd(sPosMenuKey);

                sRet = ExecuteIrt(sTrType, sPointType, sInPutType, sInPutData, sCardData, nSaleAmt, nPayAmt, sCouponGroup, "", "", sCouponNm);
                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();       // 결제 금액 재 계산 처리

                //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);
                Column.TR_PAYMENT.DATA cPayItem = (Column.TR_PAYMENT.DATA)alPayItem[CmUtil.IntParse(aParam[0])];
                
                CancelPaymentSub(new string[] { aParam[0] });

                //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.USER_DC_ITEM) continue;

                //    // 결제 수단 취소
                //    CancelPaymentSub(new string[] { iRow.ToString() });
                //}

                // ---------------------------------------------------------------------------------------------------------------------------------------

                if (PosMstManager.GetMstPayDc(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, PosMst.MST_PAY_DC.DATA.PAY_DC_TYPE) == PosConst.PAY_DC_TYPE.DC)
                {
                    // ---------------------------------------------------------------------------------------------------------------------------------------
                    // 상품정보
                    // ---------------------------------------------------------------------------------------------------------------------------------------
                    ArrayList aSaleItem = (ArrayList)StateObject.GetItemObject(Column.TR_PLU.ITEM);
                    for (int iRow = 0; iRow < aSaleItem.Count; iRow++)
                    {
                        Column.TR_PLU.DATA cSaleItem = (Column.TR_PLU.DATA)aSaleItem[iRow];
                        if (cSaleItem.CANCEL_DIV == PosConst.CANCEL_DIV.CANCEL || cSaleItem.CANCEL_DIV_MAIN == PosConst.CANCEL_DIV.CANCEL) continue;// 지정취소

                        if (cPayItem.PAY_DTL_CD_01 == ItemConst.TR_ITEM_ID.USER_DC.TOTAL_ADDDC_AMT)
                        {
                            // 상품할인 적용 취소
                            cSaleItem.ETC_DC_AMT = 0;
                            cSaleItem.ETC_DC_DIV = ItemConst.PLU_DC_DIV.NORMAL;
                        }
                        else
                        {
                            // 상품할인 적용 취소
                            cSaleItem.ITEM_DC_AMT = 0;
                            cSaleItem.DC_DIV = ItemConst.PLU_DC_DIV.NORMAL;
                            cSaleItem.DC_TYPE = ItemConst.PLU_DC_TYPE.NORMAL;                       // 단품 할인 타입(0:없음, 1:소계할인, 2:단품할인)
                        }
                        // 상품행별 영수 금액 재계산 처리
                        m_cDataService.UpdatePluItemAmount(cSaleItem);
                    }
                }
                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 RefundPayment 결제 반품
        /// <summary>
        /// 결제 반품
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public string RefundPayment(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                int nPayRow = CmUtil.IntParse(aParam[0]); // 취소할 행번호

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

                string sInPutType = m_cPayItem.OCCUR_ENTRY_05;          // 입력구분
                string sInPutData = m_cPayItem.OCCUR_ENTRY_01;          // 입력데이터
                string sCardData = m_cPayItem.OCCUR_ENTRY_22;           // 카드데이터
                double nPayAmt = m_cPayItem.AMT_ENTRY_01;               // 대상금액
                double nSaleAmt = m_cPayItem.AMT_ENTRY_03;              // 받을금액
                string sCouponGroup = m_cPayItem.OCCUR_ENTRY_09;        // 쿠폰그룹코드
                string sCouponNm = m_cPayItem.OCCUR_ENTRY_10;           // 쿠폰명

                string sTrType = PosConst.POS_VAN_MASTER.TRADE_ID.VAN_POT_DIS_C;
                string sPointType = "";

                sRet = ExecuteIrt(sTrType, sPointType, sInPutType, sInPutData, sCardData, nSaleAmt, nPayAmt, sCouponGroup, "", "", sCouponNm);
 
            }
            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;

            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 sInPutType = m_cPayItem.OCCUR_ENTRY_05;          // 입력구분
                string sInPutData = m_cPayItem.OCCUR_ENTRY_01;          // 입력데이터
                string sCardData = m_cPayItem.OCCUR_ENTRY_22;           // 카드데이터
                double nPayAmt = m_cPayItem.AMT_ENTRY_01;               // 대상금액
                double nSaleAmt = m_cPayItem.AMT_ENTRY_03;              // 받을금액
                string sCouponGroup = m_cPayItem.OCCUR_ENTRY_09;        // 쿠폰그룹코드
                string sCouponNm = m_cPayItem.OCCUR_ENTRY_10;           // 쿠폰명

                string sTrType = PosConst.POS_VAN_MASTER.TRADE_ID.VAN_POT_DIS_C;
                string sPointType = "";

                sRet = ExecuteIrt(sTrType, sPointType, sInPutType, sInPutData, sCardData, nSaleAmt, nPayAmt, sCouponGroup, "", "", sCouponNm);
                if (sRet != UserCom.RST_OK) return sRet;

                m_cPayItem.CANCEL_DIV = PosConst.CANCEL_DIV.CANCEL;

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

                //WinManager.ConfirmMessage(sPointType + " " + MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0068));

                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 sTrType, string sPointType, string sInPutType, string sInPutData, string sCardData, double nSaleAmt, double nPayAmt, string sCouponGroup, string sApprDate, string sApprNo, string sCouponNm)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                // 연습모드이면 연습 데이터 설정
                if (m_cPosStatus.Base.TrainingFlag == "1" || m_cPosStatus.Mst.TestStorYn == "1") return SetTrainingData(sTrType, sPointType, sInPutType, sInPutData, sCardData, nSaleAmt, nPayAmt, sCouponGroup, sApprDate, sApprNo, sCouponNm);

                // 결제구분 (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.DoubleParse(nPayAmt.ToString());   

                m_cPayItem.PAY_DTL_CD_02 = "";                                                                          // 캠페인코드
                m_cPayItem.PAY_DTL_CD_04 = "";                                                                          // 상품코드

                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 = CmUtil.DoubleParse(nPayAmt.ToString());                                       // 받은돈
                m_cPayItem.AMT_ENTRY_02 = 0;
                if (PosMstManager.GetMstPayDc(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, PosMst.MST_PAY_DC.DATA.PAY_DC_TYPE) == PosConst.PAY_DC_TYPE.DC)
                {
                    m_cPayItem.PAY_DTL_CD_03 = "1";                             // 할인금액아이템적용여부 (0:미적용, 1:적용)
                    m_cPayItem.PAY_DTL_CD_05 = PosConst.PAY_DC_TYPE.DC;         // 할인결제구분 (1:결제, 2:할인)
                    m_cPayItem.AMT_ENTRY_02 = CmUtil.DoubleParse(nPayAmt.ToString());                                   // 할인금액
                }

                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_03 = nSaleAmt;                                                                     // 받을금액
                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_01 = sInPutData;                                                                 // 카드번호
                m_cPayItem.OCCUR_ENTRY_02 = "";                                                                         // 승인번호
                m_cPayItem.OCCUR_ENTRY_03 = "";                                                                         // 승인일자
                m_cPayItem.OCCUR_ENTRY_04 = "";                                                                         // 승인시간
                m_cPayItem.OCCUR_ENTRY_05 = sInPutType;                                                                 // 입력 구분
                m_cPayItem.OCCUR_ENTRY_06 = "";                                                                         // 원승인번호
                m_cPayItem.OCCUR_ENTRY_07 = "";                                                                         // 원승인일자
                m_cPayItem.OCCUR_ENTRY_08 = ItemConst.PAY_APP_DIV.NORMAL;                                               // 승인구분
                m_cPayItem.OCCUR_ENTRY_09 = sCouponGroup;                                                               // 쿠폰그룹코드
                m_cPayItem.OCCUR_ENTRY_10 = sCouponNm;                                                                  // 쿠폰명
                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.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 SetTrainingData(string sTrType, string sPointType, string sInPutType, string sInPutData, string sCardData, double nSaleAmt, double nPayAmt, string sCouponGroup, string sApprDate, string sApprNo, string sCouponNm)
        {
            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_04 = "";                                                                          // 상품코드

                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 = nPayAmt;                                                                      // 받은돈
                m_cPayItem.AMT_ENTRY_02 = 0;                                                                            // 할인금액
                if (PosMstManager.GetMstPayDc(m_cPayItem.PAY_WAY_CD, m_cPayItem.PAY_DTL_CD_01, PosMst.MST_PAY_DC.DATA.PAY_DC_TYPE) == PosConst.PAY_DC_TYPE.DC)
                {
                    m_cPayItem.PAY_DTL_CD_03 = "1";                             // 할인금액아이템적용여부 (0:미적용, 1:적용)
                    m_cPayItem.PAY_DTL_CD_05 = PosConst.PAY_DC_TYPE.DC;         // 할인결제구분 (1:결제, 2:할인)
                    m_cPayItem.AMT_ENTRY_02 = nPayAmt;                                                                  // 할인금액
                }

                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_03 = nSaleAmt;                                                                     // 받을금액
                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_01 = sInPutData;                                                                 // 카드번호
                m_cPayItem.OCCUR_ENTRY_02 = "";                                                                         // 승인번호
                m_cPayItem.OCCUR_ENTRY_03 = "";                                                                         // 승인일자
                m_cPayItem.OCCUR_ENTRY_04 = "";                                                                         // 승인시간
                m_cPayItem.OCCUR_ENTRY_05 = sInPutType;                                                                 // 입력 구분
                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 = sCouponGroup;                                                               // 쿠폰그룹코드
                m_cPayItem.OCCUR_ENTRY_10 = sCouponNm;                                                                  // 쿠폰명
                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.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;
        }
        #endregion
    }
}
