using System;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;

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

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 사은품 등록
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Service
{
    public partial class frmCampPresent : Form
    {

        #region 변수 선언

        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        private TranStatus m_cTrnStatus = new TranStatus();                         // 거래정보 참조        
        private DeviceStatus m_cDevStatus = new DeviceStatus();                     // 디바이스 관리

        private IDataProcessUs m_cDataService = null;
        private IDataCommonUs m_cDataCommon = null;                                 // POS 공통함수 인터페이스
        private IMasterUs m_cMstService = null;

        private bool bNotDoubleClik = false;                                        // 더블 클릭 방지용

        private string m_sCampaignCode;                                               
        /// <summary>
        /// 캠페인코드
        /// </summary>
        public string CampaignCode { set { m_sCampaignCode = value; } get { return m_sCampaignCode; } }
        
        private string m_sCampaignName;
        /// <summary>
        /// 캠페인명
        /// </summary>
        public string CampaignName { set { m_sCampaignName = value; } get { return m_sCampaignName; } }

        private string m_sCampaignPreQty;
        /// <summary>
        /// 캠페인 사은수량
        /// </summary>
        public string CampaignPreQty { set { m_sCampaignPreQty = value; } get { return m_sCampaignPreQty; } }

        private string m_sCampaignAddSaleYn;
        /// <summary>
        /// 캠페인 사은품 추가판매가능여부
        /// </summary>
        public string CampaignAddSaleYn { set { m_sCampaignAddSaleYn = value; } get { return m_sCampaignAddSaleYn; } }

        private string m_sCampaignPosOpInputDiv;
        /// <summary>
        /// 캠페인 사은품 등록 위치(상품,HP)
        /// </summary>
        public string CampaignPosOpInputDiv { set { m_sCampaignPosOpInputDiv = value; } get { return m_sCampaignPosOpInputDiv; } }

        /// <summary>
        /// 지급사은품 리스트 데이터
        /// </summary>
        private DataTable m_dtGridPresentDst;
        #endregion 변수 선언

        #region 생성자 & 소멸자 & 폼초기화
        public frmCampPresent()
        {
            InitializeComponent();

            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
            //this.UpdateStyles();

            m_cPosStatus = (PosStatus)StateObject.POS;          // POS 기본정보
            m_cTrnStatus = (TranStatus)StateObject.TRAN;        // POS 거래정보 
            m_cDataService = (IDataProcessUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_SERVICE);
            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);
        }

        private void frmCampPresent_Load(object sender, EventArgs e)
        {
            UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "");

            InitControl();
            InitializeGrid();
            DisplayGridSrc();
        }

        private void frmCampPresent_Activated(object sender, EventArgs e)
        { 
        }

        private void frmCampPresent_Deactivate(object sender, EventArgs e)
        {
        }

        private void frmCampPresent_FormClosing(object sender, FormClosingEventArgs e)
        {
            UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                 System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", lblTitle.Text);
        }

        /// <summary>
        /// 폼 컨트롤 초기화
        /// </summary>
        private void InitControl()
        {
            try
            {
                // 이미지 로딩 처리
                this.Size = new Size(800, 578);
                picBack.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.POP_SIZE_800X578);
                picBack.Location = new Point(0, 0);

                FormManager.SetFormAllControlFont(this, m_cPosStatus.Base.FONT);
                FormManager.MovePopUpForm(this, false, m_cPosStatus.Sale.ScreenSizeUser);

                btnComplete.BackColor = CmUtil.GetColorToString(m_cPosStatus.ScnMst.ThemeColor);

                btnExit.Image = ImageManager.GetImage(BaseCom.NxImgPath, ImageManager.BTN_CLOSE);
                if (btnExit.Image != null) btnExit.Text = "";

                // 타이틀
                lblTitle.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0749);

                lblCampName.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0399);
                lblPresentQty.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0750);
                lblAddSaleYn.Text = m_sCampaignAddSaleYn == "1" ? MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0751) : MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0752);

                lblTotQty.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0755);
                lblTotDis.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0759);
                lblTotAmt.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0756);
                        
                btnDelete.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0021);                        
                btnQty.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0017);                
                btnComplete.Text = MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0126);                      

                txtCampName.Text = m_sCampaignName;
                txtPresentQty.Text = CampaignPreQty;
                txtTotQty.Text = "0";
                txtTotDis.Text = "0";
                txtTotAmt.Text = "0";
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }

        /// <summary>
        /// 그리드 초기화
        /// </summary>
        private void InitializeGrid()
        {
            try
            {
                gridPresentDst.CsmGridColumnHeadersFont = new Font(m_cPosStatus.Base.FONT, 10, gridPresentDst.CsmGridColumnHeadersFont.Style);
                //DataGridView 자체의 컬럼 헤더 Visible 여부
                gridPresentDst.CsmGridColumnHeadersVisible = true;
                //DataGridView 자체의 컬럼 헤더 높이
                gridPresentDst.CsmGridColumnHeadersHeight = 38;
                gridPresentDst.CsmGridRowsHeight = 34;
                //그리드의 컬럼 수
                gridPresentDst.CsmGridColumnCount = 7;
                //그리드의 한 화면에 보이는 로우수
                gridPresentDst.CsmGridShowPageRowsCount = 3;

                gridPresentDst.CsmGridSetHeaderBackColor(236, 238, 239);                             //그리드 헤더 배경색
                gridPresentDst.CsmGridDefaultRowBackColor = Color.FromArgb(255, 255, 255);           //그리드 홀수(Default)행의 배경색 지정
                gridPresentDst.CsmGridAlternateRowBackColor = Color.FromArgb(251, 253, 255);         //그리드 짝수(Alternate)행의 배경색 지정
                gridPresentDst.CsmGridBackGroundColor = Color.FromArgb(251, 253, 255);               //기본 백그라운드 컬러
                gridPresentDst.CsmGridHighlightColor = Color.FromArgb(255, 251, 211);                //그리드 선택 백 컬러
                gridPresentDst.CsmGridHighlightTextColor = Color.Black;                              //그리드 선택 글자 컬러

                //각 컬럼별 이름 지정
                gridPresentDst.CsmGridColumnName(0, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0757));
                gridPresentDst.CsmGridColumnName(1, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0016));
                gridPresentDst.CsmGridColumnName(2, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0017));
                gridPresentDst.CsmGridColumnName(3, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0036));
                gridPresentDst.CsmGridColumnName(4, MessageManager.GetLabelMessage(POS_MESSAGE.LABEL.MSG_0019));
                gridPresentDst.CsmGridColumnName(5, "");
                gridPresentDst.CsmGridColumnName(6, "");

                //컬럼넓이 지정(406)
                gridPresentDst.CsmGridSetColumnWidth(new int[] { 250, 100, 65, 100, 100, 0, 0 });

                //컬럼 정렬 0:왼쪽, 1:가운데, 2:오른쪽
                gridPresentDst.CsmGridAlignment(new int[] { 0, 2, 2, 2, 2, 2, 2 });

                m_dtGridPresentDst = new DataTable("GRID_LIST_DST");
                m_dtGridPresentDst.Columns.Add(new DataColumn("DST_NAME", typeof(string)));
                m_dtGridPresentDst.Columns.Add(new DataColumn("DST_ORG_AMT", typeof(string)));
                m_dtGridPresentDst.Columns.Add(new DataColumn("DST_PRE_QTY", typeof(string)));
                m_dtGridPresentDst.Columns.Add(new DataColumn("DST_DIS_AMT", typeof(string)));
                m_dtGridPresentDst.Columns.Add(new DataColumn("DST_SALE_AMT", typeof(string)));
                m_dtGridPresentDst.Columns.Add(new DataColumn("DST_CODE", typeof(string)));
                m_dtGridPresentDst.Columns.Add(new DataColumn("DST_PRE_AMT", typeof(string)));
                m_dtGridPresentDst.Clear();
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }

        #endregion

        /// <summary>
        /// 전체 사은품 내역 화면 표시
        /// </summary>
        private void DisplayGridSrc()
        {
            int nRow = 0;

            try
            {
                // 캠페인 사은품 정보 획득
                string sQuery = " SELECT A.ITEM_CD, A.DC_RATE_AMT, B.SHTCUT_ITEMNM, B.TAKE_IN_SALE_AMT, B.TAKE_OUT_SALE_AMT ";
                sQuery += " FROM POSMST..MST_CPI_ITEM A ";
                sQuery += "   JOIN POSMST..MST_ITEM B ON A.CMP_CD=B.CMP_CD AND A.STOR_CD=B.STOR_CD AND A.SUB_STOR_CD=B.SUB_STOR_CD AND A.ITEM_CD=B.ITEM_CD ";
                sQuery += " WHERE A.CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += "   AND A.STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                sQuery += "   AND A.SUB_STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                sQuery += "   AND A.CPI_CD = '" + m_sCampaignCode + "'";
                sQuery += "   AND A.CPI_ITEM_GRP_CD = '11'";
                sQuery += "   AND A.USE_YN  = '" + PosConst.MST_USE_YN.YES + "'";

                btnMenu.SetMenuButtonCount(3, 5);
                btnMenu.BackColor = Color.LightGray;
                btnMenu.VisibleUpDown = false;
                btnMenu.BackColorUpDown = Color.White;
                btnMenu.ForeColorUpDown = Color.Black;

                btnMenu.ClearMenuButtonInfo();                     // 설정된 메뉴정보 초기화

                DataTable dtMstCpiItem = m_cMstService.Select(new string[] { sQuery });
                if (dtMstCpiItem != null && dtMstCpiItem.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtMstCpiItem.Rows)
                    {
                        Cosmos.UI.CsmMenu.MenuBtnInfo cMenuInfo = new Cosmos.UI.CsmMenu.MenuBtnInfo();

                        nRow++;
                        cMenuInfo.sClassCode = "00";                // 대분류
                        cMenuInfo.sGroupCode = "0000";              // 중분류코드
                        cMenuInfo.nSeqNo = nRow;                    // 표시순번

                        double nOrgAmt = 0;
                        if (PosMstManager.GetPosOption(POS_OPTION.OPT005) == "0")
                            nOrgAmt = CmUtil.GetDataRowDouble(dr, "TAKE_OUT_SALE_AMT");
                        else
                            nOrgAmt = CmUtil.GetDataRowDouble(dr, "TAKE_IN_SALE_AMT");

                        // 메뉴 키 값 = 메뉴코드(20) + 원단가(15) + 사은품단가(15) + 메뉴명(50) 
                        string sMenuCode = CmUtil.RPadH(CmUtil.GetDataRowStr(dr, "ITEM_CD"), 20)
                                         + CmUtil.RPadH(nOrgAmt.ToString(), 15)
                                         + CmUtil.RPadH(CmUtil.GetDataRowDouble(dr, "DC_RATE_AMT").ToString(), 15)
                                         + CmUtil.GetDataRowStr(dr, "SHTCUT_ITEMNM");
                        cMenuInfo.sMenuCode = sMenuCode;                                        // 메뉴 연결코드
                        cMenuInfo.sMenuName = CmUtil.GetDataRowStr(dr, "SHTCUT_ITEMNM");        // 메뉴 명
                        cMenuInfo.sMenuPrice = m_cPosStatus.Global.NumericTOCurrency(CmUtil.GetDataRowDouble(dr, "DC_RATE_AMT"));      // 메뉴 가격

                        cMenuInfo.cBackColor = Color.White;         // 버튼 백 컬러
                        cMenuInfo.cForeColorName = Color.Black;     // 메뉴명 컬러
                        cMenuInfo.cForeColorPrice = Color.Red;      // 메뉴가격 컬러
                        cMenuInfo.fFontSizeName = 12;               // 메뉴명 폰트 사이즈
                        cMenuInfo.fFontSizePrice = 12;              // 메뉴 가격 폰트 사이즈
                        cMenuInfo.nButtonSize = 0;                  // 메뉴버튼 사이즈(0:일반,1:가로확대,2:세로확대,3:가로세로확대)
                        cMenuInfo.cMenuImage = "";
                        
                        btnMenu.AddMenuButtonInfo(cMenuInfo);
                    }
                }

                // 메뉴 표시
                btnMenu.DisplayMenuButton("00", "0000");
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }


        #region 버튼 입력 처리
        /// <summary>
        /// 버튼 입력 처리
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProc_Click(object sender, EventArgs e)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                if (bNotDoubleClik) return;
                bNotDoubleClik = true;

                if (((Cosmos.UI.CsmButton)sender) == btnDelete)        // 삭제
                {
                    PresentDelAdd(true, 0, "");
                }
                else if (((Cosmos.UI.CsmButton)sender) == btnQty)           // 수량
                {
                    // 삭제, 수량
                    int nRow = gridPresentDst.CsmGridSelectedRowIndex;
                    if (nRow >= 0 && gridPresentDst.CsmGridRowsCount > 0)
                    {
                        frmInputDataMin fForm = new frmInputDataMin();
                        fForm.PosMenuKeyIn = "QTY";
                        if (fForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            string sQty = fForm.PosInPutData;
                            if (m_cDataService.LongParse(sQty) > 0) PresentDelAdd(false, m_cDataService.LongParse(sQty), "");
                        }
                    }
                }
                else if (((Cosmos.UI.CsmButton)sender) == btnComplete)      // 완료
                {
                    CompleteTxtInPut();
                }
                else if (((Cosmos.UI.CsmButton)sender) == btnUpDst)
                {
                    gridPresentDst.CsmGridScroll("UP");
                }
                else if (((Cosmos.UI.CsmButton)sender) == btnDnDst)
                {
                    gridPresentDst.CsmGridScroll("DOWN");
                }
                else if (((Cosmos.UI.CsmButton)sender) == btnExit)
                {
                    this.DialogResult = DialogResult.Cancel;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            bNotDoubleClik = false;
        }

        private void btnMenu_MenuClickHandler(object sender, string sMenuCode)
        {
            try
            {
                if (sMenuCode == "") return;

                if (m_sCampaignAddSaleYn == "0" && m_cDataService.LongParse(txtTotQty.Text) >= m_cDataService.LongParse(m_sCampaignPreQty))
                {
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0440);
                    return;
                }
                PresentDelAdd(false, 0 , sMenuCode);
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
        }

        /// <summary>
        /// 사은품 추가 및 삭제
        /// </summary>
        /// <returns></returns>
        private bool PresentDelAdd(bool bCancel, long nQty, string sMenuCode)
        {
            string sPluName = "", sOrgAmt = "", sPreAmt = "", sPluCode = "";

            try
            {
                if (bCancel == false && nQty == 0)
                {
                    // 추가 => 메뉴 키 값 = 메뉴코드(20) + 원단가(15) + 사은품단가(15) + 메뉴명(50) 
                    sPluCode = CmUtil.MidH(sMenuCode, 0 , 20).Trim();
                    sOrgAmt = CmUtil.MidH(sMenuCode, 20, 15).Trim();
                    sPreAmt = CmUtil.MidH(sMenuCode, 35, 15).Trim();
                    sPluName = CmUtil.MidH(sMenuCode, 50, 50).Trim();

                    if (CmUtil.DoubleParse(sOrgAmt) < CmUtil.DoubleParse(sPreAmt))
                    {
                        WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0616);
                        return false;
                    }
                }
                else
                {
                    // 삭제, 수량
                    int nRow = gridPresentDst.CsmGridSelectedRowIndex;
                    if (nRow < 0 || gridPresentDst.CsmGridRowsCount == 0) return false;

                    sPluCode = gridPresentDst.CsmGridGetCell(nRow, 5);
                }

                bool bExistPlu = false;
                for(int i = 0; i < m_dtGridPresentDst.Rows.Count;i++)
                {
                    DataRow dr = m_dtGridPresentDst.Rows[i];
                    if (CmUtil.GetDataRowStr(dr,"DST_CODE") == sPluCode)
                    {
                        if ( bCancel == true)
                        {
                            m_dtGridPresentDst.Rows.Remove(dr);
                        }
                        else
                        {
                            if (nQty == 0)
                                dr["DST_PRE_QTY"] = CmUtil.GetDataRowLong(dr, "DST_PRE_QTY") + 1;
                            else
                            {
                                if (m_cDataService.LongParse(dr["DST_PRE_QTY"].ToString()) < nQty)
                                {
                                    if (m_sCampaignAddSaleYn == "0" && m_cDataService.LongParse(txtTotQty.Text) + nQty - m_cDataService.LongParse(dr["DST_PRE_QTY"].ToString()) > m_cDataService.LongParse(m_sCampaignPreQty))
                                    {
                                        WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0440);
                                        return false;
                                    }
                                }
                                dr["DST_PRE_QTY"] = nQty;
                            }
                            bExistPlu = true;
                        }
                        break;
                    }
                }

                if (bCancel== false && bExistPlu == false)
                {
                    DataRow drNewRow = m_dtGridPresentDst.NewRow();
                    drNewRow["DST_NAME"] = sPluName;
                    drNewRow["DST_PRE_QTY"] = 1;
                    drNewRow["DST_CODE"] = sPluCode;
                    drNewRow["DST_ORG_AMT"] = m_cPosStatus.Global.NumericTOCurrency(CmUtil.DoubleParse(sOrgAmt));
                    drNewRow["DST_PRE_AMT"] = sPreAmt;

                    m_dtGridPresentDst.Rows.Add(drNewRow);
                }

                long nTotQty = 0;
                double nTotDisAmt = 0, nTotSaleAmt = 0;
                long nTotPreQty = 0;
                for (int i = 0; i < m_dtGridPresentDst.Rows.Count; i++)
                {
                    DataRow dr = m_dtGridPresentDst.Rows[i];

                    long nPreQty = CmUtil.GetDataRowLong(dr, "DST_PRE_QTY");     // 사은품 지급수량
                    long nOrgQty = 0;                                           // 사은품 판매수량
                    if (nTotPreQty + nPreQty > m_cDataService.LongParse(m_sCampaignPreQty))
                    {
                        nOrgQty = nTotPreQty + nPreQty - m_cDataService.LongParse(m_sCampaignPreQty);
                        nPreQty = m_cDataService.LongParse(m_sCampaignPreQty) - nTotPreQty;
                    }
                    nTotPreQty += nPreQty;

                    double nDisAmt = CmUtil.DoubleSubtraction(CmUtil.GetDataRowDouble(dr, "DST_ORG_AMT"), CmUtil.GetDataRowDouble(dr, "DST_PRE_AMT")) * nPreQty;
                    dr["DST_DIS_AMT"] = m_cPosStatus.Global.NumericTOCurrency(nDisAmt);

                    double nSaleAmt = CmUtil.DoubleAdd(CmUtil.GetDataRowDouble(dr, "DST_PRE_AMT") * nPreQty, CmUtil.GetDataRowDouble(dr, "DST_ORG_AMT") * nOrgQty);
                    dr["DST_SALE_AMT"] = m_cPosStatus.Global.NumericTOCurrency(nSaleAmt);

                    nTotQty += (nPreQty + nOrgQty);
                    nTotDisAmt = CmUtil.DoubleAdd(nTotDisAmt, nDisAmt);
                    nTotSaleAmt = CmUtil.DoubleAdd(nTotSaleAmt, nSaleAmt);
                }

                gridPresentDst.CsmGridDataSource = m_dtGridPresentDst;
                gridPresentDst.CsmGridSelectRow(0);

                txtTotQty.Text = m_cPosStatus.Global.NumericToQuantity(nTotQty);
                txtTotDis.Text = m_cPosStatus.Global.NumericTOCurrency(nTotDisAmt);
                txtTotAmt.Text = m_cPosStatus.Global.NumericTOCurrency(nTotSaleAmt);
            }
            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 false;
        }


        /// <summary>
        /// 사은품 등록 완료 처리
        /// </summary>
        /// <returns></returns>
        private bool CompleteTxtInPut()
        {
            try
            {
                if (m_cDataService.LongParse(txtTotQty.Text) <= 0)
                {
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0439);
                    return false;
                }
                if (m_sCampaignAddSaleYn == "0" && m_cDataService.LongParse(txtTotQty.Text) > m_cDataService.LongParse(m_sCampaignPreQty))
                {
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0440);
                    return false;
                }
                if (m_cDataService.LongParse(txtTotQty.Text) < m_cDataService.LongParse(m_sCampaignPreQty))
                {
                    // 
                }
                if (WinManager.QuestionMessage(POS_MESSAGE.ERROR.MSG_0449) == false) return false;

                ISalePluItemUs cPluService = (ISalePluItemUs)sManager.InitServiceInstance(ServiceLists.BSV_SALE.DLL, ServiceLists.BSV_SALE.SALE_PLU_ITEM);
                for (int i = 0; i < m_dtGridPresentDst.Rows.Count; i++)
                {
                    DataRow dr = m_dtGridPresentDst.Rows[i];

                    string sPluCode = CmUtil.GetDataRowStr(dr, "DST_CODE");
                    long   nPreQty = CmUtil.GetDataRowLong(dr, "DST_PRE_QTY");

                    // 사은상품 등록 처리
                    cPluService.SetItemCode("", m_sCampaignPosOpInputDiv, m_cPosStatus.Base.StoreNo, sPluCode, "", nPreQty.ToString());
                }

                // 사은품 상품 캠페인마스터 로딩 처리
                ICampaignUs cCampaingMain = (ICampaignUs)sManager.InitServiceInstance(ServiceLists.ASV_CAMPAIGN.DLL, ServiceLists.ASV_CAMPAIGN.CAMPAIGN_MAIN);
                cCampaingMain.RegisterCampaignPlu(false);

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            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 false;
        }
        #endregion

        

    }

}
