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

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

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 간이영수증 출력
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Service
{
    public class EtcReceipt : IDataServiceUs 
    {
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   //StateObject:StateServer Object(객체)
        private SManager sManager = new SManager();                                 //이 객체를 통해 업무 Service 호출

        private TranStatus m_cTrnStatus = new TranStatus();                         //거래정보 참조
        private IDataProcessUs m_cDataService = null;
        private IDataCommonUs cDataCommon = null;
        private ISaleCompleteUs m_cSaleComplete = null;                             // 거래완료처리            

        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조

        /// <summary>
        /// 생성자
        /// </summary>
        public EtcReceipt()
        {
            m_cTrnStatus = (TranStatus)StateObject.TRAN;    // POS 거래정보
            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            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);
            cDataCommon = (IDataCommonUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_COMMON);
        }

        /// <summary>
        /// 간이영수증 출력
        /// </summary>
        /// <param name="oParam"></param>
        /// <returns></returns>
        public string SetData(object oParam)
        {
            string sRet = UserCom.RST_ERR;
            double dTaxPluRate = 0;        //TakeOut 부가세 율

            DataTable aParam = (DataTable)oParam;

            // 아이템 정보에 쌓는다.
            ArrayList alTrPluItem = (ArrayList)StateObject.GetItemObject(Column.TR_PLU.ITEM);                   // 상품 아이템
            Column.TR_PLU.DATA cSaleItem = new Column.TR_PLU.DATA();

            if (aParam != null)
            {
                m_cTrnStatus.Head.TradeKind = ItemConst.TRAN_KIND.NORMAL.DELIMITER;

                double dTotAmount = 0;

                foreach (DataRow dr in aParam.Rows)
                {
                    cSaleItem = new Column.TR_PLU.DATA();

                    // 상품 내역
                    cSaleItem.ITEM_BAR_CD = dr["PLU"].ToString();
                    cSaleItem.SALE_AMT = CmUtil.DoubleParse(dr["AMOUNT"].ToString());
                    
                    dTotAmount = dTotAmount + CmUtil.DoubleParse(dr["AMOUNT"].ToString());

                    alTrPluItem.Add(cSaleItem);

                    m_cTrnStatus.Head.SysYmd = dr["SALEDT"].ToString();
                    m_cTrnStatus.Head.TotSaleAmt = CmUtil.DoubleAdd(m_cTrnStatus.Head.TotSaleAmt, cSaleItem.SALE_AMT);
                }

                // 헤더 부분 소계랑.. 총 매출액 등등을 출력해주기 위한 곳
                m_cTrnStatus.Head.TradeDiv = ItemConst.TRAN_DIV.NORMAL;
                // 합계
                m_cTrnStatus.Head.NetSaleAmt = dTotAmount;

                //부가세 계산
                //부가세 코드로 율 가져오기
                DataTable dt = cDataCommon.SeletCommonCode(PosConst.COMMON_CODE_GROUP.M0101, "");

                if (dt == null || dt.Rows.Count == 0)
                {
                    //부가세 값 없음!!
                    // 부가세
                    //m_cTrnStatus.Head.TotVatAmt = double.Parse(CmUtil.MathAmtPercent(dTotAmount, 10).ToString());
                }
                else
                {
                    DataRow[] adr = null;

                    adr = dt.Select(" " + PosMst.MST_COMMON.DATA.CMM_CD + " = '" + m_cPosStatus.Mst.BasicTakeOutTaxCd + "'");
                    if (adr.Length > 0) dTaxPluRate = CmUtil.GetDataRowDouble(adr[0], PosMst.MST_COMMON.DATA.NUM_VAL_TITLE_VAL_01);

                    if (dTotAmount > 0)
                    {
                        dTotAmount = dTotAmount - (dTotAmount / (1 + (dTaxPluRate / 100)));
                        dTotAmount = CmUtil.MathRounds(dTotAmount, m_cPosStatus.Mst.VatRudDwLocMethd, CmUtil.IntParse(m_cPosStatus.Mst.VatRudDwLoc));
                    }


                    // 부가세
                    m_cTrnStatus.Head.TotVatAmt = dTotAmount;
                }

                // 영수증 출력
                sRet = m_cSaleComplete.PosEtcReceiptExecute(aParam);
                if (sRet != UserCom.RST_OK) return sRet;

            }
            return sRet;
        }

        public object GetData(string[] aParam)
        {
            throw new NotImplementedException();
        }

        public string Execute(string[] aParam)
        {
            throw new NotImplementedException();
        }

    }
}
