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

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


namespace Cosmos.Service
{
    public class SaleReceipt : ISaleReceipt
    {
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private PosStatus m_cPosStatus = new PosStatus();                           //기본정보 참조
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private IMasterUs m_cMstService = null;                                     // 마스터 인터페이스
        private IDatabaseSQL m_cSqlDbService = null;                                // 데이터베이스 관리
        private TranStatus m_cTrnStatus = null;                                     // 거래정보 참조
        private ISaleCompleteUs m_cSaleComplete = null;                             // 거래 완료처리


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


        public SaleReceipt()
        {
            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_cSqlDbService = (IDatabaseSQL)sManager.InitServiceInstance(ServiceLists.AGENT_DATABASE.DLL, ServiceLists.AGENT_DATABASE.DATABASE_MSSQL);
            m_cDataCommon = (IDataCommonUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_COMMON);
            m_cSaleComplete = (ISaleCompleteUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.SALE_COMPLETE);
        }

        /// <summary>
        /// 거래조회
        /// </summary>
        /// <param name="sSaleDate"></param>
        /// <param name="sPosNo"></param>
        /// <param name="sTradeNo"></param>
        /// <param name="sPayTime"></param>
        /// <param name="sTradeType"></param>
        /// <returns></returns>
        public DataTable SearchSaleReceiptList(string sSaleDate, string sPosNo, string sTradeNo, string sPayTime, string sTradeType)
        {
            string sQuery = "";
            try
            {
                sQuery  = " DECLARE @CMP_CD  AS VARCHAR(5) ";
                sQuery += " DECLARE @STOR_CD AS VARCHAR(10) ";
                sQuery += " DECLARE @POS_NO  AS VARCHAR(2) ";
                sQuery += " DECLARE @SALE_DT AS VARCHAR(8) ";
                sQuery += " SET @CMP_CD  = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += " SET @STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                sQuery += " SET @POS_NO  = '" + sPosNo + "'";
                sQuery += " SET @SALE_DT = '" + sSaleDate + "'";

                if (m_cPosStatus.Base.TrainingFlag == "1")
                {
                    sQuery += " SELECT  A.SALE_DT ";
                    sQuery += "       , A.POS_NO ";
                    sQuery += "       , A.REG_NO AS TRADE_NO";
                    sQuery += "       , A.SYS_DT ";
                    sQuery += "       , A.PAY_TIME ";
                    sQuery += "       , A.NET_SALE_AMT ";
                    sQuery += "       , A.TRADE_DIV ";
                    sQuery += "       , A.TRADE_KINDPER ";
                    sQuery += "       , A.ORG_BILLDT + A.ORG_BILL_POSNO + A.ORG_BILL_NO AS ORG_INFO  ";
                    sQuery += "       , ISNULL(B.SALE_DT + B.POS_NO + B.TRADE_NO,'') AS RTN_INFO  ";
                    sQuery += "       , (CASE WHEN ISNULL(G.CASH_APP_AMT,0) > 0 AND C.TRADE_NO IS NULL AND  D.TRADE_NO IS NULL  THEN '0' ELSE '1' END) AF_CASH ";
                    sQuery += "       , (CASE WHEN (A.AF_ACCUM_POINT_AMT > 0 OR A.AF_ACCUM_POINT_INFO <> '') AND E.TRADE_NO IS NULL AND F.TRADE_NO IS NULL  THEN '0' ELSE '1' END) AF_POINT ";
                    sQuery += "       , ISNULL(A.AF_ACCUM_POINT_AMT,0)AF_ACCUM_POINT_AMT ";
                    sQuery += "       , ISNULL(A.AF_ACCUM_POINT_INFO,'')AF_ACCUM_POINT_INFO ";
                    sQuery += "       , ISNULL(G.CASH_APP_AMT,0) CASH_APP_AMT ";
                    sQuery += "       , ISNULL(A.NET_SALE_AMT,0) NET_SALE_AMT ";
                    sQuery += "       , ISNULL(A.TOT_VAT,0) TOT_VAT ";
                    sQuery += "       , ISNULL(A.AF_ACCUM_POINT_AMT,0) AF_ACCUM_POINT_AMT ";
                    sQuery += "       , ISNULL(A.AF_ACCUM_POINT_INFO,'') AF_ACCUM_POINT_INFO ";
                    sQuery += " FROM POSLOG..ETC_SALE_HEADER A ";
                    sQuery += "  LEFT JOIN (SELECT * FROM POSLOG..ETC_SALE_HEADER ";
                    sQuery += "              WHERE CMP_CD = @CMP_CD AND STOR_CD = @STOR_CD AND TRADE_DIV = '1' AND TRAIN_MODE_YN = '1' ";
                    sQuery += "         ) B ON A.SALE_DT = B.ORG_BILLDT AND A.POS_NO = B.ORG_BILL_POSNO AND A.REG_NO = B.ORG_BILL_NO ";
                    sQuery += "  LEFT JOIN (SELECT * FROM POSLOG..ETC_SALE_HEADER ";
                    sQuery += "              WHERE CMP_CD = @CMP_CD AND STOR_CD = @STOR_CD AND SALE_DT = @SALE_DT AND POS_NO = @POS_NO AND TRADE_DIV = '0' AND TRADE_KINDPER = '31' AND TRAIN_MODE_YN = '1' ";
                    sQuery += "         ) C ON A.SALE_DT = C.ORG_BILLDT AND A.POS_NO = C.ORG_BILL_POSNO AND A.REG_NO = C.ORG_BILL_NO ";
                    sQuery += "  LEFT JOIN (SELECT *  FROM POSLOG..ETC_SALE_ETC ";
                    sQuery += "              WHERE CMP_CD = @CMP_CD AND STOR_CD = @STOR_CD AND SALE_DT = @SALE_DT AND POS_NO = @POS_NO AND ETC_WAY_CD ='80' AND ETC_DTL_CD_01='00' AND CANCEL_DIV ='0' ";
                    sQuery += "         ) D ON A.SALE_DT = D.SALE_DT AND A.POS_NO = D.POS_NO AND A.REG_NO = D.REG_NO ";
                    sQuery += "  LEFT JOIN (SELECT * FROM POSLOG..TR_SALE_HEADER ";
                    sQuery += "              WHERE CMP_CD = @CMP_CD AND STOR_CD = @STOR_CD AND SALE_DT = @SALE_DT AND POS_NO = @POS_NO AND TRADE_DIV = '0' AND TRADE_KINDPER = '30' AND TRAIN_MODE_YN = '1' ";
                    sQuery += "         ) E ON A.SALE_DT = E.ORG_BILLDT AND A.POS_NO = E.ORG_BILL_POSNO AND A.REG_NO = E.ORG_BILL_NO ";
                    sQuery += "  LEFT JOIN (SELECT * FROM POSLOG..ETC_SALE_ETC ";
                    sQuery += "              WHERE CMP_CD = @CMP_CD AND STOR_CD = @STOR_CD AND SALE_DT = @SALE_DT AND POS_NO = @POS_NO AND ETC_WAY_CD ='80' AND ETC_DTL_CD_01='01' AND CANCEL_DIV ='0' ";
                    sQuery += "         ) F ON A.SALE_DT = F.SALE_DT AND A.POS_NO = F.POS_NO AND A.REG_NO = F.REG_NO ";
                    sQuery += " LEFT JOIN (SELECT CMP_CD , SALE_DT , STOR_CD , POS_NO , REG_NO , SUM(CASE WHEN PAY_WAY_CD + PAY_DTL_CD_01 = '0702' THEN AMT_ENTRY_07 ELSE PAY_AMT END) CASH_APP_AMT ";
                    sQuery += "              FROM POSLOG.dbo.ETC_SALE_PAY ";
                    sQuery += "             WHERE CMP_CD = @CMP_CD AND STOR_CD = @STOR_CD AND POS_NO = @POS_NO AND OCCUR_ENTRY_21 ='1' AND CANCEL_DIV = '0' ";
                    sQuery += "             GROUP BY CMP_CD, SALE_DT, STOR_CD, POS_NO, REG_NO ";
                    sQuery += "         ) G ON A.SALE_DT = G.SALE_DT AND A.POS_NO = G.POS_NO AND A.REG_NO = G.REG_NO ";
                    sQuery += "  WHERE A.CMP_CD  = @CMP_CD ";
                    sQuery += "    AND A.SALE_DT = @SALE_DT ";
                    sQuery += "    AND A.STOR_CD = @STOR_CD ";
                    sQuery += "    AND A.TRAIN_MODE_YN = '1' ";

                    if (sTradeNo.Trim().Length == 5)
                        sQuery += "    AND A.REG_NO = '" + sTradeNo + "' ";
                    if (sPayTime.Trim().Length == 6)
                        sQuery += "    AND A.PAY_TIME = '" + sPayTime + "' ";

                }
                else
                {
                    sQuery += " SELECT  A.SALE_DT ";
                    sQuery += "       , A.POS_NO ";
                    sQuery += "       , A.TRADE_NO ";
                    sQuery += "       , A.SYS_DT ";
                    sQuery += "       , A.PAY_TIME ";
                    sQuery += "       , A.NET_SALE_AMT ";
                    sQuery += "       , A.TRADE_DIV ";
                    sQuery += "       , A.TRADE_KINDPER ";
                    sQuery += "       , A.ORG_BILLDT + A.ORG_BILL_POSNO + A.ORG_BILL_NO ORG_INFO ";
                    sQuery += "       , ISNULL(B.SALE_DT + B.POS_NO + B.TRADE_NO,'') RTN_INFO ";
                    sQuery += "       , (CASE WHEN ISNULL(G.CASH_APP_AMT,0) > 0 AND C.TRADE_NO IS NULL AND  D.TRADE_NO IS NULL  THEN '0' ELSE '1' END) AF_CASH "; 	// 현금영수증 발행 가능 여부 
                    sQuery += "       , (CASE WHEN (A.AF_ACCUM_POINT_AMT > 0 OR A.AF_ACCUM_POINT_INFO <> '') AND E.TRADE_NO IS NULL AND F.TRADE_NO IS NULL  THEN '0' ELSE '1' END) AF_POINT "; // 포인트 발행 가능 여부
                    sQuery += "       , ISNULL(A.AF_ACCUM_POINT_AMT,0)AF_ACCUM_POINT_AMT ";
                    sQuery += "       , ISNULL(A.AF_ACCUM_POINT_INFO,'')AF_ACCUM_POINT_INFO ";

                    sQuery += "       , ISNULL(G.CASH_APP_AMT,0) CASH_APP_AMT ";
                    sQuery += "       , ISNULL(A.NET_SALE_AMT,0) NET_SALE_AMT ";
                    sQuery += "       , ISNULL(A.TOT_VAT,0) TOT_VAT ";
                    sQuery += "       , ISNULL(A.AF_ACCUM_POINT_AMT,0) AF_ACCUM_POINT_AMT ";
                    sQuery += "       , ISNULL(A.AF_ACCUM_POINT_INFO,'') AF_ACCUM_POINT_INFO ";

                    sQuery += " FROM POSLOG..TR_SALE_HEADER A ";
                    sQuery += "   LEFT JOIN (SELECT *  FROM POSLOG..TR_SALE_HEADER ";
                    sQuery += "               WHERE CMP_CD = @CMP_CD AND STOR_CD = @STOR_CD AND TRADE_DIV = '1' AND TRAIN_MODE_YN = '0') B ";
                    sQuery += "            ON A.SALE_DT = B.ORG_BILLDT ";
                    sQuery += "           AND A.POS_NO = B.ORG_BILL_POSNO ";
                    sQuery += "           AND A.TRADE_NO = B.ORG_BILL_NO ";
                    sQuery += "   LEFT JOIN (SELECT *  FROM POSLOG..ETC_SALE_HEADER ";
                    sQuery += "               WHERE CMP_CD = @CMP_CD AND STOR_CD = @STOR_CD AND SALE_DT = @SALE_DT AND POS_NO = @POS_NO AND TRADE_DIV = '0' AND TRAIN_MODE_YN = '0' AND TRADE_KINDPER = '31' ) C ";
                    sQuery += "            ON A.SALE_DT = C.ORG_BILLDT ";
                    sQuery += "           AND A.POS_NO = C.ORG_BILL_POSNO ";
                    sQuery += "           AND A.TRADE_NO = C.ORG_BILL_NO ";
                    sQuery += "   LEFT JOIN (SELECT *  FROM POSLOG..TR_SALE_ETC ";
                    sQuery += "                WHERE CMP_CD = @CMP_CD AND STOR_CD = @STOR_CD AND SALE_DT = @SALE_DT AND POS_NO = @POS_NO AND ETC_WAY_CD ='80' AND ETC_DTL_CD_01='00' AND CANCEL_DIV ='0' ) D ";
                    sQuery += "            ON A.SALE_DT = D.SALE_DT ";
                    sQuery += "           AND A.POS_NO = D.POS_NO ";
                    sQuery += "           AND A.TRADE_NO = D.TRADE_NO ";
                    sQuery += "   LEFT JOIN (SELECT *  FROM POSLOG..ETC_SALE_HEADER ";
                    sQuery += "               WHERE CMP_CD = @CMP_CD AND STOR_CD = @STOR_CD AND SALE_DT = @SALE_DT AND POS_NO = @POS_NO AND TRADE_DIV = '0' AND TRAIN_MODE_YN = '0' AND TRADE_KINDPER = '30' ) E ";
                    sQuery += "            ON A.SALE_DT = E.ORG_BILLDT ";
                    sQuery += "           AND A.POS_NO = E.ORG_BILL_POSNO ";
                    sQuery += "           AND A.TRADE_NO = E.ORG_BILL_NO ";
                    sQuery += "   LEFT JOIN (SELECT *  FROM POSLOG..TR_SALE_ETC ";
                    sQuery += "                WHERE CMP_CD = @CMP_CD AND STOR_CD = @STOR_CD AND SALE_DT = @SALE_DT AND POS_NO = @POS_NO AND ETC_WAY_CD ='80' AND ETC_DTL_CD_01='01' AND CANCEL_DIV ='0' ) F ";
                    sQuery += "            ON A.SALE_DT = F.SALE_DT ";
                    sQuery += "           AND A.POS_NO = F.POS_NO ";
                    sQuery += "           AND A.TRADE_NO = F.TRADE_NO ";
                    sQuery += "   LEFT JOIN (SELECT CMP_CD, SALE_DT, STOR_CD, POS_NO, TRADE_NO, SUM(CASE WHEN PAY_WAY_CD + PAY_DTL_CD_01 = '0702' THEN AMT_ENTRY_07 ELSE PAY_AMT END) CASH_APP_AMT ";
                    sQuery += "                FROM POSLOG.dbo.TR_SALE_PAY ";
                    sQuery += "               WHERE CMP_CD = @CMP_CD AND STOR_CD = @STOR_CD AND POS_NO = @POS_NO AND OCCUR_ENTRY_21 ='1' AND CANCEL_DIV = '0' ";
                    sQuery += "               GROUP BY CMP_CD, SALE_DT, STOR_CD, POS_NO, TRADE_NO ) G ";
                    sQuery += "            ON A.SALE_DT = G.SALE_DT ";
                    sQuery += "           AND A.POS_NO = G.POS_NO ";
                    sQuery += "           AND A.TRADE_NO = G.TRADE_NO ";
                    sQuery += " WHERE A.CMP_CD  = @CMP_CD ";
                    sQuery += "   AND A.SALE_DT = @SALE_DT ";
                    sQuery += "   AND A.STOR_CD = @STOR_CD ";
                    sQuery += "   AND A.TRAIN_MODE_YN = '0' ";
                    

                    if (sTradeNo.Trim().Length == 5)
                        sQuery += "    AND A.TRADE_NO = '" + sTradeNo + "' ";
                    if (sPayTime.Trim().Length == 6)
                        sQuery += "    AND A.PAY_TIME = '" + sPayTime + "' ";

                }


                if (sPosNo.Trim().Length == 2)
                    sQuery += "    AND A.POS_NO = '" + sPosNo + "' ";

                if (sTradeType == "1")
                {
                    sQuery += "    AND A.TRADE_DIV = '0' ";             //정상 
                }
                else if (sTradeType == "2")
                {
                    sQuery += "    AND A.TRADE_DIV = '1' ";             //반품 
                }
                else if (sTradeType == "3")
                {
                    
                    sQuery += "    AND A.TRADE_DIV = '0' ";
                    sQuery += "    AND ISNULL(B.TRADE_NO,'') = '' ";    //반품건 제외 
                }

                // 영수증반품 => 판매 데이터 만 대상
                // 수정 => 판매, 대외거래(o2o), 선불판매, 상품권판매, LOSS
                //sQuery += "    AND A.TRADE_KINDPER = '" + ItemConst.TRAN_KIND.NORMAL.SALE + "' ";
                sQuery += "    AND A.TRADE_KINDPER IN ( '" + ItemConst.TRAN_KIND.NORMAL.SALE + "','" + ItemConst.TRAN_KIND.FORIGN.O2O + "','"
                                                           + ItemConst.TRAN_KIND.CHARGESALE.PREPAID + "','" + ItemConst.TRAN_KIND.CHARGESALE.GIFT + "','"
                                                           + ItemConst.TRAN_KIND.ETC.LOSS + "' )";

                if (m_cPosStatus.Base.TrainingFlag == "1")
                {
                    sQuery += " ORDER BY A.REG_NO DESC ";
                }
                else
                {
                    sQuery += " ORDER BY A.TRADE_NO DESC ";
                }


                return m_cMstService.Select(new string[] { sQuery });
            }
            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="sPrtData"></param>
        /// <returns></returns>
        public bool PrintSaleReceipt(string sPrtData)
        {
            //// 영수증 출력
            return m_cSaleComplete.PosSaleRePrint(sPrtData);
        }

        /// <summary>
        /// 기념일 배송 주문정보 출력
        /// </summary>
        /// <param name="sPrtData"></param>
        /// <returns></returns>
        public bool PrintReceipt(string sPrtData)
        {
            //// 영수증 출력
            return m_cSaleComplete.PosRembOrderPrint(sPrtData);
        }

        ///// <summary>
        ///// 매출불러오기
        ///// </summary>
        ///// <param name="sSaleDate"></param>
        ///// <param name="sPosNo"></param>
        ///// <param name="sTradeNo"></param>
        ///// <returns></returns>
        //public string SetTrItem(string sSaleDate, string sPosNo, string sTradeNo)
        //{
        //    string sRet = UserCom.RST_ERR;


        //    try
        //    {
        //        string sItemCdoe = string.Empty;
        //        string sSubStoreCode = string.Empty;
                   

        //        //매출 아이템 조회
        //        DataTable dt = SearchSaleReceiptDetail(sSaleDate, sPosNo, sTradeNo);

        //        if (dt == null || dt.Rows.Count == 0) 
        //        {
        //            return sRet;
        //        }
        //        else
        //        {
        //            ArrayList m_alTrPluItem = (ArrayList)StateObject.GetItemObject(Column.TR_PLU.ITEM);

        //            foreach (DataRow dr in dt.Rows)
        //            {
        //                sItemCdoe = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.ITEM_PLU_CD);
        //                sSubStoreCode = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.SUB_SHOP_CD);

        //                // 상품 마스터 조회
        //                DataTable dtDataInfo = m_cDataCommon.SeletItemMstToItemCode(sSubStoreCode, sItemCdoe);
        //                if (dtDataInfo == null || dtDataInfo.Rows.Count == 0)
        //                {
        //                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0010);      // 등록되지 않은 상품 입니다
        //                    return sRet;
        //                }

        //                DataRow drPlu = dtDataInfo.Rows[0];

        //                // 상품 정보 생성
        //                Column.TR_PLU.DATA cPluItem = new Column.TR_PLU.DATA();

        //                cPluItem.ITEM_BRAND_CD = "";

        //                cPluItem.SUB_SHOP_CD = sSubStoreCode;
        //                cPluItem.ITEM_PLU_CD = sItemCdoe;
        //                cPluItem.ITEM_DIV = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.ITEM_DIV);
        //                cPluItem.SALE_QTY = CmUtil.GetDataRowLong(dr, PosTr.TR_SALE_DETAIL.DATA.SALE_QTY);


        //                //금액 과 세금 구분은 옵션으로 변경 해야함!!!!
        //                cPluItem.TAKE_IN_OUT_DIV = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.TAKE_IN_OUT_DIV);
        //                cPluItem.TAXRATE_CD = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.TAXRATE_CD);
        //                cPluItem.SALE_GIFT_NO = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.SALE_GIFT_NO);
        //                cPluItem.SALE_AMT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.SALE_AMT);
        //                cPluItem.FLAVOUR_DIV = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.FLAVOUR_DIV);
        //                cPluItem.FLAVOUR_QTY = CmUtil.GetDataRowLong(dr, PosTr.TR_SALE_DETAIL.DATA.FLAVOUR_QTY);
        //                cPluItem.FLAVOUR_STOCK = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.FLAVOUR_STOCK);
        //                cPluItem.ITEM_INPUT_DIV = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.ITEM_INPUT_DIV);
        //                cPluItem.ITEM_BAR_CD = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.ITEM_BAR_CD);
        //                cPluItem.ITEM_TAX_DIV = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.ITEM_TAX_DIV);
        //                cPluItem.TAX_AMT_INCLUDE_YN = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.TAX_AMT_INCLUDE_YN);
        //                cPluItem.APPLY_TAX_AMT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.APPLY_TAX_AMT);
        //                cPluItem.SVC_FEE_AMT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.SVC_FEE_AMT);
        //                cPluItem.SVC_FEE_VAT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.SVC_FEE_VAT);
        //                cPluItem.BILL_AMT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.BILL_AMT);
        //                cPluItem.DC_RATE = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.DC_RATE);
        //                cPluItem.ITEM_DC_AMT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.ITEM_DC_AMT);
        //                cPluItem.DC_DIV = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.DC_DIV);
        //                cPluItem.NONSALES_RSN_CD = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.NONSALES_RSN_CD);
        //                cPluItem.SUM_DC_AMT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.SUM_DC_AMT);
        //                cPluItem.CPI_DC_AMT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.CPI_DC_AMT);
        //                cPluItem.COOP_CARD_DC_AMT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.COOP_CARD_DC_AMT);
        //                cPluItem.SET_DC_AMT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.SET_DC_AMT);
        //                cPluItem.ETC_DC_DIV = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.ETC_DC_DIV);
        //                cPluItem.ETC_DC_AMT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.ETC_DC_AMT);


        //                cPluItem.EXCEP_DC_DIV = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.EXCEP_DC_DIV);
        //                cPluItem.EXCEP_DC_AMT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.EXCEP_DC_AMT);
                        
                        
                        
        //                cPluItem.PACK_DIV = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.PACK_DIV);
        //                cPluItem.SET_MENU_CD = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.SET_MENU_CD);
        //                cPluItem.CPI_PRESENT_DIV = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.CPI_PRESENT_DIV);
        //                cPluItem.BILLSPR_NO = CmUtil.GetDataRowLong(dr, PosTr.TR_SALE_DETAIL.DATA.BILLSPR_NO);
        //                cPluItem.CANCEL_DIV = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.CANCEL_DIV);
        //                cPluItem.CANCEL_DATE = "";

        //                cPluItem.SUB_SET_MNG_TYPE = CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.SUB_SET_MNG_TYPE);
        //                cPluItem.SALE_PRC = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.SALE_PRC);

        //                cPluItem.SET_MENU_CD = CmUtil.GetDataRowStr(dr, PosTr.TR_SALE_DETAIL.DATA.SET_MENU_CD);

        //                // 상품추가 정보
        //                if (cPluItem.ITEM_DIV == ItemConst.PLU_ITEM_DIV.OPT_MENU)
        //                {
        //                    cPluItem.ITEM_NAME = ">>" + CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.SHTCUT_ITEMNM);
        //                }
        //                else if (cPluItem.ITEM_DIV == ItemConst.PLU_ITEM_DIV.SET_DETL)
        //                {

        //                    cPluItem.ITEM_NAME = ">" + CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.SHTCUT_ITEMNM);

        //                }
        //                else if (cPluItem.FLAVOUR_DIV == ItemConst.PLU_FLAVOUR_DIV.FLAVOUR)
        //                {
        //                    cPluItem.ITEM_NAME = ">" + CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.SHTCUT_ITEMNM);
        //                }
        //                else
        //                {
        //                    cPluItem.ITEM_NAME = CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.SHTCUT_ITEMNM);
        //                }


        //                cPluItem.CANCEL_DIV_MAIN = "0";
        //                cPluItem.FREE_GIFT_YN = CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.FREE_GIFT_YN);
        //                cPluItem.DC_PRMT_YN = CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.DC_PRMT_YN);
        //                if (cPluItem.DC_PRMT_YN != "1") cPluItem.DC_PRMT_YN = "0";   // db에서 가져오지 않고 있어 임시 작업

        //                cPluItem.ACCUM_PRMT_YN = CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.ACCUM_PRMT_YN);
        //                cPluItem.SVC_FEE_TRGT_YN = CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.SVC_FEE_TRGT_YN);
        //                cPluItem.COOP_CARD_DC_NOADM_YN = CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.COOP_CARD_DC_NOADM_YN);

        //                cPluItem.TOT_DC_AMT = CmUtil.GetDataRowDouble(dr, PosTr.TR_SALE_DETAIL.DATA.TOT_DC_AMT);
        //                cPluItem.ORDER_OK_FLAG = PosConst.PLU_ORDER_OK_FLAG.ORDER_ING;
        //                cPluItem.L_CLSS = CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.L_CLSS);
        //                cPluItem.M_CLSS = CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.M_CLSS);
        //                cPluItem.S_CLSS = CmUtil.GetDataRowStr(drPlu, PosMst.MST_ITEM.DATA.S_CLSS);

        //                m_alTrPluItem.Add(cPluItem);                    // 등록 처리
        //            }


        //            ISalePluItemUs m_cPluService = (ISalePluItemUs)sManager.InitServiceInstance(ServiceLists.BSV_SALE.DLL, ServiceLists.BSV_SALE.SALE_PLU_ITEM);

        //            m_cPluService.ItemAmountExeccute(true, true);

        //            IFormSaleMainUs cFormSaleMain = (IFormSaleMainUs)FormManager.GetForm(FormManager.FORM_SAL_MAIN);
                    
        //            m_cPosStatus.Sale.SaleFlow = PosConst.SALE_FLOW.SALE_DOING;
                    
        //            cFormSaleMain.DisplayInit(false, true);

        //            cFormSaleMain.DisplayItem(0, false);

        //            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="sSaleDate"></param>
        /// <param name="sPosNo"></param>
        /// <param name="sTradeNo"></param>
        /// <returns></returns>
        public DataTable SearchPritSaleReceiptDetail(string sSaleDate, string sPosNo, string sTradeNo)
        {
            string sQuery = "";
            try
            {

                if (m_cPosStatus.Base.TrainingFlag == "1")
                {
                    sQuery = " SELECT JOURNAL_DATA";
                    sQuery += "   FROM POSLOG.dbo.ETC_SALE_JOURNAL ";
                    sQuery += "  WHERE 1 = 1 ";
                    sQuery += "    AND CMP_CD  = '" + m_cPosStatus.Base.CmpCd + "'";
                    sQuery += "    AND SALE_DT = '" + sSaleDate + "'";
                    sQuery += "    AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                    sQuery += "    AND POS_NO  = '" + sPosNo + "'";
                    sQuery += "    AND REG_NO  = '" + sTradeNo + "'";
                    sQuery += "    AND TRAIN_MODE_YN = '" + m_cPosStatus.Base.TrainingFlag + "'  ";
                }
                else
                {
                    sQuery = " SELECT JOURNAL_DATA";
                    sQuery += "   FROM POSLOG.dbo.TR_SALE_JOURNAL ";
                    sQuery += "  WHERE 1 = 1 ";
                    sQuery += "    AND CMP_CD  = '" + m_cPosStatus.Base.CmpCd + "'";
                    sQuery += "    AND SALE_DT = '" + sSaleDate + "'";
                    sQuery += "    AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                    sQuery += "    AND POS_NO  = '" + sPosNo + "'";
                    sQuery += "    AND TRADE_NO  = '" + sTradeNo + "'";
                    sQuery += "    AND TRAIN_MODE_YN = '" + m_cPosStatus.Base.TrainingFlag + "'  ";
                }

                
                return m_cMstService.Select(new string[] { sQuery });
            }
            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="sSaleDate"></param>
        /// <param name="sPosNo"></param>
        /// <param name="sTradeNo"></param>
        /// <returns></returns>
        private DataTable SearchSaleReceiptDetail(string sSaleDate, string sPosNo, string sTradeNo)
        {
            string sQuery = "";
            try
            {

                if (m_cPosStatus.Base.TrainingFlag == "1")
                {
                    sQuery = " SELECT *";
                    sQuery += "  FROM POSLOG..ETC_SALE_DETAIL ";
                    sQuery += "  WHERE CMP_CD  = '" + m_cPosStatus.Base.CmpCd + "'";
                    sQuery += "    AND SALE_DT = '" + sSaleDate + "'";
                    sQuery += "    AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                    sQuery += "    AND POS_NO  = '" + sPosNo + "'";
                    sQuery += "    AND REG_NO  = '" + sTradeNo + "'";
                    sQuery += "    AND CANCEL_DIV = '" + ItemConst.PAY_CANCEL_DIV.NORMAL + "'";
                }
                else
                {
                    sQuery = " SELECT *";
                    sQuery += "  FROM POSLOG..TR_SALE_DETAIL ";
                    sQuery += "  WHERE CMP_CD  = '" + m_cPosStatus.Base.CmpCd + "'";
                    sQuery += "    AND SALE_DT = '" + sSaleDate + "'";
                    sQuery += "    AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                    sQuery += "    AND POS_NO  = '" + sPosNo + "'";
                    sQuery += "    AND TRADE_NO  = '" + sTradeNo + "'";
                    sQuery += "    AND CANCEL_DIV = '" + ItemConst.PAY_CANCEL_DIV.NORMAL + "'";
                }

                

                return m_cMstService.Select(new string[] { sQuery });
            }
            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="sSaleDate"></param>
        /// <param name="sPosNo"></param>
        /// <param name="sTradeNo"></param>
        /// <returns></returns>
        public bool ReturnSaleReceipt(string sSaleDate, string sPosNo, string sTradeNo)
        {
            bool bRet = false;

            try
            {

                //반품 유무 조회
                DataTable dt = ReturnSearch(sSaleDate, sPosNo, sTradeNo);

                if (dt == null || dt.Rows.Count == 0)
                {
                    //반품 처리


                    bRet = true;

                }
                else
                {
                    //이미 반품된 거래 입니다.
                    WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0229));
                }
                
            }
            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 bRet;
        }


        /// <summary>
        /// 반품 거래 체크
        /// </summary>
        /// <param name="sSaleDate"></param>
        /// <param name="sPosNo"></param>
        /// <param name="sTradeNo"></param>
        /// <returns></returns>
        private DataTable ReturnSearch(string sSaleDate, string sPosNo, string sTradeNo)
        {
            string sQuery = "";
            try
            {


                if (m_cPosStatus.Base.TrainingFlag == "1")
                {
                    sQuery = " SELECT *";
                    sQuery += "   FROM POSLOG.dbo.ETC_SALE_JOURNAL ";
                    sQuery += "  WHERE 1 = 1 ";
                    sQuery += "   AND CMP_CD     = '" + m_cPosStatus.Base.CmpCd + "'";
                    sQuery += "   AND ORG_BILLDT = '" + sSaleDate + "'";
                    sQuery += "   AND STOR_CD    = '" + m_cPosStatus.Base.StoreNo + "'";
                    sQuery += "   AND ORG_BILL_POSNO  = '" + sTradeNo + "'";
                    sQuery += "   AND TRADE_DIV  = '1' ";
                    sQuery += "   AND TRAIN_MODE_YN = '" + m_cPosStatus.Base.TrainingFlag + "'  ";
                }
                else
                {
                    sQuery = " SELECT *";
                    sQuery += "   FROM POSLOG.dbo.TR_SALE_JOURNAL ";
                    sQuery += "  WHERE 1 = 1 ";
                    sQuery += "   AND CMP_CD     = '" + m_cPosStatus.Base.CmpCd + "'";
                    sQuery += "   AND ORG_BILLDT = '" + sSaleDate + "'";
                    sQuery += "   AND STOR_CD    = '" + m_cPosStatus.Base.StoreNo + "'";
                    sQuery += "   AND ORG_BILL_POSNO  = '" + sTradeNo + "'";
                    sQuery += "   AND TRADE_DIV  = '1' ";
                    sQuery += "   AND TRAIN_MODE_YN = '" + m_cPosStatus.Base.TrainingFlag + "'  ";
                }

                return m_cMstService.Select(new string[] { sQuery });
            }
            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;
        }


    }
}
