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

        public CakeChange()
        {
        }

        #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
            {
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                   System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return sRet;
        }
        #endregion

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

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

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

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

                string sCakeChangeCD = aParam[3];                           // 코드
                double nPayAmt = CmUtil.DoubleParse(aParam[4]);             // 결제금액
                double nCakeChangeAmt = CmUtil.DoubleParse(aParam[5]);      // 금액  
                string siTemCD = aParam[6];                                 // 상품코드

                sRet = ExecuteIrt(sCakeChangeCD, nPayAmt, nCakeChangeAmt);
                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);

                if (m_cPayItem.PAY_DTL_CD_03 == "1")
                    SetPluDcAmount(m_cPayItem.PAY_WAY_CD + m_cPayItem.PAY_DTL_CD_01, siTemCD, nPayAmt);   // 상품아이템에 할인금액 설정

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

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

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

            try
            {
                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 sCakeChangeCD = m_cPayItem.OCCUR_ENTRY_01;           // 번호 
                double nPayAmt = m_cPayItem.AMT_ENTRY_01;                   // 대상금액
                double nCakeChangeAmt = m_cPayItem.AMT_ENTRY_03;            // 교환권금액

                sRet = ExecuteIrt(sCakeChangeCD, nPayAmt, nCakeChangeAmt);
                if (sRet != UserCom.RST_OK) return sRet;

                if (m_cPayItem.PAY_DTL_CD_03 == "1")
                    ClearPluDcAmount();        // 할인금액 삭제

                m_cPayItem.CANCEL_DIV = PosConst.CANCEL_DIV.CANCEL;

                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 sCakeChangeCD = m_cPayItem.OCCUR_ENTRY_01;           // 번호 
                double nPayAmt = m_cPayItem.AMT_ENTRY_01;                   // 대상금액
                double nCakeChangeAmt = m_cPayItem.AMT_ENTRY_03;            // 교환권금액

                sRet = ExecuteIrt(sCakeChangeCD, nPayAmt, nCakeChangeAmt);
                if (sRet != UserCom.RST_OK) return sRet;

                if (m_cPayItem.PAY_DTL_CD_03 == "1")
                    ClearPluDcAmount();        // 할인금액 삭제

            }
            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 조회 실행
        /// <summary>
        /// 조회 실행
        /// </summary>
        public string ExecuteIrt(string sCakeChangeCD, double nPayAmt, double nCakeChangeAmt)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                // 연습모드이면 연습 데이터 설정
                if (m_cPosStatus.Base.TrainingFlag == "1" || m_cPosStatus.Mst.TestStorYn == "1") return SetTrainingData(sCakeChangeCD, nPayAmt, nCakeChangeAmt);

                // 결제구분 (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_05 = PosConst.PAY_DC_TYPE.PAY;                                                    // 할인결제구분 (1:결제, 2:할인)
                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.PAY_DTL_CD_04 = ""; 
                m_cPayItem.QTY_ENTRY_01 = 0;                                                                            // 받을금액
                m_cPayItem.QTY_ENTRY_02 = 0;    
                m_cPayItem.QTY_ENTRY_03 = 0;
                m_cPayItem.QTY_ENTRY_04 = 0;
                m_cPayItem.QTY_ENTRY_05 = 0;

                m_cPayItem.AMT_ENTRY_01 = m_cPayItem.PAY_AMT;
                m_cPayItem.AMT_ENTRY_02 = 0;
                m_cPayItem.AMT_ENTRY_03 = nCakeChangeAmt;                                                               // 교환권 금액
                m_cPayItem.AMT_ENTRY_05 = 0;
                m_cPayItem.AMT_ENTRY_04 = 0;
                m_cPayItem.AMT_ENTRY_08 = 0;
                m_cPayItem.AMT_ENTRY_09 = 0;
                m_cPayItem.AMT_ENTRY_10 = 0;

                m_cPayItem.OCCUR_ENTRY_01 = sCakeChangeCD;                                                              // 카드번호
                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_09 = "";                                                                         // 
                m_cPayItem.OCCUR_ENTRY_10 = "";                                                                         // 
                m_cPayItem.OCCUR_ENTRY_11 = "";                                                                         // 
                m_cPayItem.OCCUR_ENTRY_12 = "";                                                                         // 
                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 = ItemConst.PAY_CASHBILL_DIV.NO;                                              // 현금영수증 발행여부
                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 sCakeChangeCD, double nPayAmt, double nCakeChangeAmt)
        {
            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_05 = PosConst.PAY_DC_TYPE.PAY;                                                    // 할인결제구분 (1:결제, 2:할인)
                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.PAY_DTL_CD_04 = "";
                m_cPayItem.QTY_ENTRY_01 = 0;                                                                            // 받을금액
                m_cPayItem.QTY_ENTRY_02 = 0;
                m_cPayItem.QTY_ENTRY_03 = 0;
                m_cPayItem.QTY_ENTRY_04 = 0;
                m_cPayItem.QTY_ENTRY_05 = 0;

                m_cPayItem.AMT_ENTRY_01 = m_cPayItem.PAY_AMT;
                m_cPayItem.AMT_ENTRY_02 = 0;
                m_cPayItem.AMT_ENTRY_03 = nCakeChangeAmt;                                                               // 교환권 금액
                m_cPayItem.AMT_ENTRY_05 = 0;
                m_cPayItem.AMT_ENTRY_04 = 0;
                m_cPayItem.AMT_ENTRY_08 = 0;
                m_cPayItem.AMT_ENTRY_09 = 0;
                m_cPayItem.AMT_ENTRY_10 = 0;

                m_cPayItem.OCCUR_ENTRY_01 = sCakeChangeCD;                                                              // 카드번호
                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.COMPULSION;                                           // 승인구분
                m_cPayItem.OCCUR_ENTRY_09 = "";                                                                         // 
                m_cPayItem.OCCUR_ENTRY_10 = "";                                                                         // 
                m_cPayItem.OCCUR_ENTRY_11 = "";                                                                         // 
                m_cPayItem.OCCUR_ENTRY_12 = "";                                                                         // 
                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 = ItemConst.PAY_CASHBILL_DIV.NO;                                              // 현금영수증 발행여부
                m_cPayItem.OCCUR_ENTRY_22 = "";
                m_cPayItem.BILLSPR_NO = m_cTrnStatus.Sale.BillSplitNo;                                                  // 빌분리 번호

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

        #region 할인 금액 설정
        /// <summary>
        /// 상품별 할인 금액 설정
        /// </summary>
        /// <param name="sCoopDcDiv"></param>
        /// <param name="siTemCD"></param>
        /// <param name="nTotDcAmt"></param>
        /// <returns></returns>
        public double SetPluDcAmount(string sCoopDcDiv, string siTemCD, double nTotDcAmt)
        { 
            try
            {
                ArrayList alSaleItem = (ArrayList)StateObject.GetItemObject(Column.TR_PLU.ITEM);

                for (int i = 0; i < alSaleItem.Count; i++)
                {
                    Column.TR_PLU.DATA cPluItem = (Column.TR_PLU.DATA)alSaleItem[i];

                    if (cPluItem.CANCEL_DIV == PosConst.CANCEL_DIV.CANCEL || cPluItem.CANCEL_DIV_MAIN == PosConst.CANCEL_DIV.CANCEL) continue;    // 지정취소
                    if (cPluItem.DC_PRMT_YN != "0") continue;
                    if (cPluItem.ITEM_PLU_CD != siTemCD) continue;

                    cPluItem.ITEM_DC_AMT = nTotDcAmt;
                    cPluItem.DC_DIV = sCoopDcDiv;
                    cPluItem.DC_TYPE = ItemConst.PLU_DC_TYPE.DC_ITEM;                       // 단품 할인 타입(0:없음, 1:소계할인, 2:단품할인)

                    m_cDataService.UpdatePluItemAmount(cPluItem); 
                }
                 
            }
            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 nTotDcAmt;
        }

        /// <summary>
        /// 상품별 할인 금액 삭제
        /// </summary>
        /// <returns></returns>
        public double ClearPluDcAmount()
        {
            try
            {
                ArrayList alSaleItem = (ArrayList)StateObject.GetItemObject(Column.TR_PLU.ITEM);

                for (int i = 0; i < alSaleItem.Count; i++)
                {
                    Column.TR_PLU.DATA cPluItem = (Column.TR_PLU.DATA)alSaleItem[i];

                    if (cPluItem.CANCEL_DIV == PosConst.CANCEL_DIV.CANCEL || cPluItem.CANCEL_DIV_MAIN == PosConst.CANCEL_DIV.CANCEL) continue;    // 지정취소
                    if (cPluItem.DC_PRMT_YN != "0") continue;
                    if (cPluItem.DC_TYPE != ItemConst.PLU_DC_TYPE.DC_ITEM) continue;

                    cPluItem.ITEM_DC_AMT = 0;
                    cPluItem.DC_DIV = ItemConst.PLU_DC_DIV.NORMAL;
                    cPluItem.DC_TYPE = ItemConst.PLU_DC_TYPE.NORMAL;

                    m_cDataService.UpdatePluItemAmount(cPluItem);
                }
            }
            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 0;
        }
        #endregion
    }
}
