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

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

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 입출금/준비금/마감입금
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Service
{
    public class AccRemoney : IAccRemoney
    {
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private PosStatus m_cPosStatus = new PosStatus();                           //기본정보 참조
        private IMasterUs m_cMstService = null;                                     // 마스터 인터페이스
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private IDataProcessUs m_cDataService = null;
        private TranStatus m_cTrnStatus = new TranStatus();                         //거래정보 참조
        private IDataCommonUs m_cDataCommon = null;                                 // POS 공통함수 인터페이스
        private ISaleCompleteUs m_cSaleComplete = null;                             // 거래완료처리

        public AccRemoney()
        {
            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            m_cTrnStatus = (TranStatus)StateObject.TRAN;    // POS 거래정보

            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);
            m_cDataCommon = (IDataCommonUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_COMMON);
        }

        /// <summary>
        /// 준비금 조회
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public object GetData(string[] aParam)
        {
            string sQuery = "";
            try
            {
                sQuery = " SELECT ROW_NUMBER() OVER( ORDER BY A.REG_NO ASC) AS NO ";
                sQuery += "      , B.CASHIER_NM";
                sQuery += "      , CONVERT(VARCHAR, A.TOTSALE_AMT) TOTSALE_AMT ";
                sQuery += "      , LEFT(A.PAY_TIME, 2) + ':' + SUBSTRING(A.PAY_TIME, 3, 2) + ':' + RIGHT(A.PAY_TIME, 2) AS PAY_TIME";
                sQuery += " FROM POSLOG..ETC_SALE_HEADER AS A WITH(NOLOCK)";
                sQuery += "     LEFT OUTER JOIN POSMST..MST_USER AS B WITH(NOLOCK)";
                sQuery += "       ON A.CMP_CD = B.CMP_CD";
                sQuery += "      AND A.STOR_CD = B.STOR_CD";
                sQuery += "      AND A.PIC_NO = B.CASHIER_ID";
                sQuery += " WHERE A.CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += "  AND A.SALE_DT = '" + aParam[0] + "'";
                sQuery += "  AND A.STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                sQuery += "  AND A.POS_NO = '" + m_cPosStatus.Base.PosNo + "'";
                sQuery += "  AND A.TRADE_KINDPER = '" + ItemConst.TRAN_KIND.STATEMENT.RESERVE_FUND + "'";
                sQuery += "  ORDER BY A.REG_NO DESC ";

                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
            {
                DataTable aParam = (DataTable)oParam;

                if (aParam != null)
                {
                    m_cDataService.SetTranBaseToHead();            //트란 헤더 정보 재설정

                    m_cTrnStatus.Head.TradeKind = ItemConst.TRAN_KIND.STATEMENT.RESERVE_FUND;
                    m_cTrnStatus.Head.TradeDiv = ItemConst.TRAN_DIV.NORMAL;

                    //결과 저장
                    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)
                    {
                        if (dr["DPST_WTHDR_CD"].ToString() == PosConst.FRM_ACC_REMONEY_CODE.TOTAL_REMONEY)
                        {
                            m_cTrnStatus.Head.TotSaleAmt = m_cDataService.DoubleParse(dr["DPST_WTHDR_AMT"].ToString());
                        }

                        i++;

                        cDpstWthdr = new Column.SALE_DPST_WTHDR.DATA();

                        cDpstWthdr.SEQ = i;
                        cDpstWthdr.DPST_WTHDR_TYPE = ItemConst.TRAN_KIND.STATEMENT.RESERVE_FUND;
                        cDpstWthdr.DPST_WTHDR_CD = dr["DPST_WTHDR_CD"].ToString();
                        cDpstWthdr.DPST_WTHDR_AMT = m_cDataService.DoubleParse(dr["DPST_WTHDR_AMT"].ToString());
                        cDpstWthdr.DPST_WTHDR_NM = dr["DPST_WTHDR_NM"].ToString();
                        cDpstWthdr.DPST_WTHDR_QTY = m_cDataService.IntParse(dr["DPST_WTHDR_QTY"].ToString());
                        cDpstWthdr.CUST_NO = "";
                        cDpstWthdr.SLACK_FIELD_01 = "";

                        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
            }
            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 AccRemoneyListPrt(object oParam, string sFromDate)
        {
            string sRet = UserCom.RST_ERR;
            DataTable aParam = (DataTable)oParam;
            try
            {
                if (aParam != null)
                {
                    if (aParam.Rows.Count > 0)
                    {
                        // 영수증 출력
                        sRet = m_cSaleComplete.AccRemoneyListReceipt(aParam, sFromDate);
                        if (sRet != UserCom.RST_OK) return sRet;

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