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

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

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 기타 입출금 처리
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Service
{
    class AccEtcInout : IAccEtc
    {
        private PosStatus m_cPosStatus = new PosStatus();                           //기본정보 참조
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   //StateObject:StateServer Object(객체)

        private IMasterUs m_cMstService = null;                                     // 마스터 인터페이스
        private IDataProcessUs m_cDataService = null;
        private ISaleCompleteUs m_cSaleComplete = null;                             // 거래완료처리
        private TranStatus m_cTrnStatus = new TranStatus();                         //거래정보 참조

        /// <summary>
        /// 판매공통 모듈
        /// </summary>
        protected IDataCommonUs m_cDataCommon = null;

        /// <summary>
        /// 생성자
        /// </summary>
        public AccEtcInout()
        {
            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            m_cTrnStatus = (TranStatus)StateObject.TRAN;    // POS 거래정보
            m_cDataCommon = (IDataCommonUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_COMMON);

            m_cMstService = (IMasterUs)sManager.InitServiceInstance(ServiceLists.ASV_MASTER.DLL, ServiceLists.ASV_MASTER.POS_MASTER);
            m_cDataService = (IDataProcessUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_SERVICE);
            m_cSaleComplete = (ISaleCompleteUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.SALE_COMPLETE);
        }

        /// <summary>
        /// 기타 입출금 현황 조회
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public object GetData(string[] aParam)
        {
            string sQuery = "";
            try
            {
                sQuery += " SELECT ROW_NUMBER() OVER( ORDER BY B.SALE_DT, B.REG_NO ASC) AS NO ";
                sQuery += "       ,B.* ";
                sQuery += "   FROM ( ";
                sQuery += "          SELECT SDW.SALE_DT ";
                sQuery += "               , SDW.DPST_WTHDR_CD AS DPST_WTHDR_CD_NM ";
                sQuery += "               , DWA.DPST_WTHDR_ACC_NM ";
                sQuery += "               , CONVERT(VARCHAR, SDW.DPST_WTHDR_AMT) DPST_WTHDR_AMT ";
                sQuery += "               , SDW.ETC_REASON ";
                sQuery += "               , SDW.DPST_WTHDR_CD ";
                sQuery += "               , SDW.DPST_WTHDR_ACC_CD ";
                sQuery += "               , SDW.CUST_NO ";
                sQuery += "               , SDW.STOR_PAY_TYPE ";
                sQuery += "               , SDW.POS_NO ";
                sQuery += "               , SDW.REG_NO ";
                sQuery += "               , SDW.SLACK_FIELD_01 ";
                sQuery += "          FROM POSLOG.dbo.ETC_SALE_DPST_WTHDR AS SDW WITH(NOLOCK) ";
                sQuery += "              LEFT OUTER JOIN POSLOG.dbo.ETC_SALE_HEADER AS TSH WITH(NOLOCK) ";
                sQuery += "              ON SDW.CMP_CD = TSH.CMP_CD AND SDW.SALE_DT = TSH.SALE_DT AND SDW.STOR_CD = TSH.STOR_CD AND SDW.POS_NO = TSH.POS_NO AND SDW.REG_NO = TSH.REG_NO AND SDW.DPST_WTHDR_TYPE = TSH.TRADE_KINDPER ";
                sQuery += "              LEFT OUTER JOIN POSMST.dbo.MST_DPST_WTHDR_ACC AS DWA WITH(NOLOCK) ";
                sQuery += "              ON SDW.CMP_CD = DWA.CMP_CD  ";
                sQuery += "             AND DWA.DRT_FRCS_DIV = '" + m_cPosStatus.Mst.DrtFrcsDiv + "'";
                sQuery += "             AND SDW.DPST_WTHDR_ACC_CD = DWA.DPST_WTHDR_ACC_CD AND SDW.DPST_WTHDR_CD = DWA.DPST_WTHDR_TYPE ";
                sQuery += "         WHERE SDW.CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += "           AND SDW.STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                sQuery += "           AND SDW.POS_NO = '" + m_cPosStatus.Base.PosNo + "'";
                sQuery += "           AND SDW.DPST_WTHDR_TYPE = '82' ";
                sQuery += "           AND SDW.SALE_DT BETWEEN '" + aParam[0] + "'" + " AND '" + aParam[1] + "'";
                sQuery += "           AND TSH.TRADE_DIV = '0' ";
                sQuery += "         ) AS B ";
                sQuery += "    WHERE (B.SALE_DT + B.POS_NO + B.REG_NO NOT IN (SELECT ORG_BILLDT + ORG_BILL_POSNO + ORG_BILL_NO FROM [POSLOG].[dbo].[ETC_SALE_HEADER] ";
                sQuery += "                                                      WHERE CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += "                                                        AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                sQuery += "                                                        AND POS_NO = '" + m_cPosStatus.Base.PosNo + "'";
                sQuery += "                                                        AND TRADE_KINDPER = '82' ";
                sQuery += "                                                        AND SALE_DT BETWEEN '" + aParam[0] + "'" + " AND '" + aParam[1] + "'";
                sQuery += "                                                        AND TRADE_DIV = '1')) ";

                DataTable dtDataInfo = m_cMstService.Select(new string[] { sQuery });

                return dtDataInfo;
            }
            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="oParam"></param>
        /// <returns></returns>
        public string SetData(object oParam)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                string[] aParam = (string[])oParam;

                m_cDataService.SetTranBaseToHead();            //트란 헤더 정보 재설정

                if (aParam[0] == ItemConst.TRAN_DIV.NORMAL)
                {
                    m_cTrnStatus.Head.TradeKind = ItemConst.TRAN_KIND.STATEMENT.ETC_STATEMENT;
                    m_cTrnStatus.Head.TradeDiv = ItemConst.TRAN_DIV.NORMAL;
                    m_cTrnStatus.Head.TotSaleAmt = m_cDataService.DoubleParse(aParam[4]);
                }
                else if (aParam[0] == ItemConst.TRAN_DIV.REFUND)
                {
                    m_cTrnStatus.Head.TradeKind = ItemConst.TRAN_KIND.STATEMENT.ETC_STATEMENT;
                    m_cTrnStatus.Head.TradeDiv = ItemConst.TRAN_DIV.REFUND;
                    m_cTrnStatus.Head.TotSaleAmt = m_cDataService.DoubleParse(aParam[4]);

                    m_cTrnStatus.Head.OrgPosNo = aParam[10];
                    m_cTrnStatus.Head.OrgTradeNo = aParam[11];
                    m_cTrnStatus.Head.OrgSaleDate = aParam[1];
                }

                //결과 저장
                ArrayList aDpstWthdr = (ArrayList)StateObject.GetItemObject(Column.SALE_DPST_WTHDR.ITEM);
                Column.SALE_DPST_WTHDR.DATA cDpstWthdr = new Column.SALE_DPST_WTHDR.DATA();

                cDpstWthdr.SEQ = 0;
                cDpstWthdr.DPST_WTHDR_TYPE = ItemConst.TRAN_KIND.STATEMENT.ETC_STATEMENT;
                cDpstWthdr.DPST_WTHDR_ACC_CD = aParam[2];
                cDpstWthdr.DPST_WTHDR_CD = aParam[3];
                cDpstWthdr.DPST_WTHDR_AMT = m_cDataService.DoubleParse(aParam[4]);
                cDpstWthdr.DPST_WTHDR_NM = aParam[5];
                cDpstWthdr.CUST_NO = aParam[6];
                cDpstWthdr.SLACK_FIELD_01 = aParam[9];
                cDpstWthdr.STOR_PAY_TYPE = aParam[8];
                cDpstWthdr.ETC_REASON = aParam[7];

                aDpstWthdr.Add(cDpstWthdr);

                // 저장
                sRet = m_cSaleComplete.OpenCloseExecute(new string[] { "0" });
                if (sRet != UserCom.RST_OK) return sRet;
                
                bool bIncTradeNo = m_cDataCommon.TradeNoAddCheck("");
                m_cDataService.TranComplete();
                m_cDataService.SetMsgNoInc(true, !bIncTradeNo, bIncTradeNo);      // RegNo증가

                m_cTrnStatus.Head.RegNo = m_cPosStatus.Base.RegNo;      // REG_NO

                //현금 알람
                if (CmUtil.IsNull(PosMstManager.GetPosOption(POS_OPTION.OPT310), "0") != "0")
                {
                    if (cDpstWthdr.DPST_WTHDR_CD == "01")    //입금
                    {
                        m_cPosStatus.Mst.CashAlarmAmt = CmUtil.DoubleAdd(m_cPosStatus.Mst.CashAlarmAmt, cDpstWthdr.DPST_WTHDR_AMT);
                    }
                    else //출금
                    {
                        m_cPosStatus.Mst.CashAlarmAmt = CmUtil.DoubleSubtraction(m_cPosStatus.Mst.CashAlarmAmt, cDpstWthdr.DPST_WTHDR_AMT);
                    }
                }

                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>
        /// <param name="oParam">출력정보</param>
        /// <param name="sFromDate">조회일자</param>
        /// <param name="sToDate">조회일자</param>
        /// <returns></returns>
        public string AccEtcListPrt(object oParam, string sFromDate, string sToDate)
        {
            DataTable aParam = (DataTable)oParam;
            string sRet = UserCom.LOG_ERROR;
            try
            {
                if (aParam != null)
                {
                    m_cTrnStatus.Head.TradeKind = ItemConst.TRAN_KIND.STATEMENT.ETC_STATEMENT;

                    //결과 저장
                    ArrayList aDpstWthdr = (ArrayList)StateObject.GetItemObject(Column.SALE_DPST_WTHDR.ITEM);
                    Column.SALE_DPST_WTHDR.DATA cDpstWthdr = new Column.SALE_DPST_WTHDR.DATA();

                    int i = 1;
                    foreach (DataRow dr in aParam.Rows)
                    {
                        i++;

                        cDpstWthdr = new Column.SALE_DPST_WTHDR.DATA();

                        cDpstWthdr.SEQ = i;
                        cDpstWthdr.DPST_WTHDR_TYPE = ItemConst.TRAN_KIND.STATEMENT.ETC_STATEMENT;
                        cDpstWthdr.SALE_DT = dr["SALE_DT"].ToString();
                        cDpstWthdr.DPST_WTHDR_CD = dr["DPST_WTHDR_CD"].ToString();
                        cDpstWthdr.DPST_WTHDR_NM = dr["DPST_WTHDR_NM"].ToString();
                        cDpstWthdr.DPST_WTHDR_AMT = m_cDataService.DoubleParse(dr["DPST_WTHDR_AMT"].ToString());
                        cDpstWthdr.ETC_REASON = dr["ETC_REASON"].ToString();

                        aDpstWthdr.Add(cDpstWthdr);
                    }
                }

                // 영수증 출력
                sRet = m_cSaleComplete.AccEtcListReceipt(sFromDate, sToDate);
                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;
        }
    }
}
