using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LLNet.Utils;
using LLNet.GUI;


namespace LLNet.Tienda.MainForms.Forms
{
    public partial class frmRetiros : LLNet.GUI.FormBases.frmBase
    {
        private decimal EfectivoC = 0;
        private decimal TotalDep = 0;
        private decimal Deposito = 0;
        private Decimal IdRetiro = 0;
        private decimal MaxArqueo = 0; 
        
        /*/VARIABLE QUE TRAE LA VARIABLE CONSECUTIVA DE UN RETIRO/*/
        public static decimal id_retiro = 0;
        /*/FIN/*/

        public string v_set;
        public string id_ret;
        public frmRetiros()
        {
            InitializeComponent();
            Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsPDV1.GetType(), dsPDV1.TDA_V_MONEDAS.TableName), dsPDV1);

        }

        #region ElimRetiro
        /// <summary>
        /// Anula un retiro
        /// </summary>
        private void ElimRetiro()
        {
            string retiro = Convert.ToString(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "IDRETIRO"));

            if (retiro != "")
            {
                if (GUI.CApp.ValidateAccess("DeleteLine", "Eliminar Línea"))
                {
                    Utils.QueryParams qp = new QueryParams();
                    qp.AddReplaceParam("RETIRO", retiro);
                    qp.AddReplaceParam("JORNADA", Program.Jornada.ToString());
                    qp.AddReplaceParam("USUARIO", GUI.CApp.usuario);

                    GUI.CApp.bll.ExecuteSql("WMS_P_ELIMRETIRO", qp, "PDV");

                    frmRetiros_Load(null, null);
                }
            }
        }
        #endregion

        public override void Init(object[] pArgs)
        {
            base.Init(pArgs);
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Program.CloseWindow(this);
        }

        private void frmRetiros_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            { 
                case Keys.Escape:
                    btnCancelar_Click(null, null);
                    break;
                case Keys.F12:
                    btnAceptar_Click(null, null);
                    break;
                case Keys.F4:
                    //gridView1.DeleteRow(gridView1.FocusedRowHandle);
                    ElimRetiro();
                    btnAceptar_Click(null, null);
                    break;
                case Keys.F5:
                    sbtnCajaChica_Click(null, null);
                    break;
            }
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            GUI.CApp.bll.updateDataSet(dsRetiros1, dsRetiros1.WMS_RETIROS.TableName);
            //this.Close();
        }

        private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            //Decimal Retiro = Convert.ToDecimal(GUI.CApp.GetSequence("WMS_S_RETIRO"));
            //gridView1.SetRowCellValue(e.RowHandle, "IDRETIRO", Retiro);
            //gridView1.SetRowCellValue(e.RowHandle, "JORNADA", Program.Jornada.ToString());
            //gridView1.SetRowCellValue(e.RowHandle, "CAJA", Program.Caja.ToString());
            //gridView1.SetRowCellValue(e.RowHandle, "CREADO_POR", GUI.CApp.usuario);
            
        }

        private void frmRetiros_Load(object sender, EventArgs e)
        {
            IdRetiro = 0;
            Utils.QueryParams qp = new LLNet.Utils.QueryParams();
            qp.AddReplaceParam("JORNADA", Program.Jornada.ToString());
            qp.AddReplaceParam("CAJA", Program.Caja.ToString());
            dsRetiros1.WMS_RETIROS.Clear();
            Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsRetiros1.GetType(), dsRetiros1.WMS_RETIROS.TableName, qp), dsRetiros1);

        }

        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            gridView1.UpdateCurrentRow();
            gridView1.PostEditor();
           
        }

        private bool ValidarMonto(decimal  Monto)
        {
            Utils.QueryParams qp = new LLNet.Utils.QueryParams();
            qp.AddReplaceParam("JORNADA", Program.Jornada.ToString());
            qp.AddReplaceParam("CAJA", Program.Caja.ToString());
            EfectivoC = Convert.ToDecimal(GUI.CApp.bll.GetSingleValue("EfectivoCaja", qp, "PDV"));
            TotalDep = 0;
            foreach (DataRow row in dsRetiros1.WMS_RETIROS.Rows)
            {
                TotalDep += Convert.ToDecimal(Utils.Helpers.Nvl(row["DEPOSITO"], 0));
            }           
            if ((TotalDep + Monto) > EfectivoC)
            {
                return false;
            }
            return true;
        }


        private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            gridView1.SetRowCellValue(e.RowHandle, colMODIFICADO_POR, GUI.CApp.usuario);
        }

        private void btnAgregarRetiro_Click(object sender, EventArgs e)
        {
            //if (IdRetiro == 0)
            //{
                //Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                //qp.AddReplaceParam("JORNADA", Program.Jornada.ToString());
                //qp.AddReplaceParam("MONEDA", Program.Caja.ToString());
                //IdRetiro = Convert.ToDecimal(Utils.Helpers.Nvl(GUI.CApp.bll.GetSingleValue("RetiroJornada", qp, "PDV"),0));
                //if (IdRetiro == 0)
                    IdRetiro = Convert.ToDecimal(GUI.CApp.GetSequence("WMS_S_RETIRO"));
                    
                 

                    
            //}
            Vgc.PostEditor();
            Vgc.UpdateFocusedRecord();
            Vgc.FocusFirst();
            Model.PDV.dsRetiros.WMS_RETIROSRow row = dsRetiros1.WMS_RETIROS.NewWMS_RETIROSRow();
            row.CREADO_POR =  GUI.CApp.usuario;
            row.DEPOSITO = Convert.ToDecimal(rowMONTO.Properties.Value);
            row.NO_PLANILLA  = rowNoPlanilla.Properties.Value.ToString();
            row.NO_DEPOSITO = rowNoDeposito.Properties.Value.ToString();
            row.MONEDA = Convert.ToDecimal(rowMoneda.Properties.Value);
            row.IDRETIRO = IdRetiro;
            row.JORNADA = Convert.ToDecimal(Program.Jornada);
            //if (ValidarMonto(Convert.ToDecimal(rowMONTO.Properties.Value)) == true)
            //{

                dsRetiros1.WMS_RETIROS.AddWMS_RETIROSRow(row);
                rowNoDeposito.Properties.Value = null;
                rowMoneda.Properties.Value = null;
                rowMONTO.Properties.Value = null;
                
                id_ret = Convert.ToString(IdRetiro);
                v_set = row.NO_PLANILLA;


                rowNoPlanilla.Properties.Value = "";
            
                /*/ACTUALIZO Y GENERO - REP/*/

                GUI.CApp.bll.updateDataSet(dsRetiros1, dsRetiros1.WMS_RETIROS.TableName);

                QueryParams ParamsApart = new QueryParams();
                ParamsApart.AddReplaceParam("VALUE", v_set);
                ParamsApart.AddReplaceParam("IDRETIRO", id_ret);
                CApp.bll.ExecuteSql("SetNoPlanilla", ParamsApart, "PDV");

                ParamsApart.Clear();
                ParamsApart.AddReplaceParam("JORNADA", Program.Jornada.ToString());
                MaxArqueo = Convert.ToDecimal(Utils.Helpers.Nvl(GUI.CApp.bll.GetSingleValue("MaxArqueo", ParamsApart, "Reports"), 0));
                ParamsApart.AddReplaceParam("pARQUEO", MaxArqueo.ToString());
                ParamsApart.AddReplaceParam("pRETIRO", IdRetiro.ToString());
                ParamsApart.AddReplaceParam("pUSER", GUI.CApp.usuario);
                CApp.bll.ExecuteSql("GuardarArquRet", ParamsApart, "PDV");

                /*/ --- Genero Vista Previa Reporte de Retiros, Emanuel Carvajal Marìn ---  /*/

                id_retiro = IdRetiro;
                Reporte_Impreso___Retiros.report_viewer frm_1 = new LLNet.Tienda.MainForms.Reporte_Impreso___Retiros.report_viewer();
                frm_1.Show();

                /*/ -- FIN DE EDICIÒN -- /*/

                //btnAceptar_Click(null, null);
            
            //}
            //else
            //{
            //    GUI.CApp.ShowMessage("El Monto Ingresado Sobrepasa el Monto en Caja", "Retiros Efectivo", true);
                
            //}
             
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            id_retiro = 0;

            foreach (int handle in gridView1.GetSelectedRows())
            {
                id_retiro = Convert.ToDecimal(gridView1.GetRowCellValue(handle, colIDRETIRO));
            }

            if (id_retiro != 0)
            {

                if (GUI.CApp.ShowMessage("Desea Re-Imprimir el Comprobante del Retiro #" + id_retiro + " ?", "Re-Imprimir Comprobantes", true, true) == DialogResult.OK)
                {
                    Reporte_Impreso___Retiros.report_viewer frm_1 = new Reporte_Impreso___Retiros.report_viewer();
                    frm_1.Show();

                }
                else
                {
                    return;
                }

            }
            else
            {
                GUI.CApp.ShowMessage("Seleccione un Retiro a Re-Imprimir", "Error al Imprimir Comprobante", true);
                return;
            }
        }

        #region sbtnCajaChica_Click IMGF
        private void sbtnCajaChica_Click(object sender, EventArgs e)
        {
            if (GUI.CApp.ValidateAccess("CajaChica", "Caja chica"))
            {
                string supervisor = "";
                if (GUI.CApp.Sup != null && GUI.CApp.Sup != "")
                    supervisor = GUI.CApp.Sup;
                else
                    supervisor = GUI.CApp.usuario;

                FrmCajaChica fcc = new FrmCajaChica(supervisor);
                fcc.ShowDialog();
            }
        }
        #endregion

        #region Llamado de pantalla Arqueos
        /*
         * Pantalla de Arqueos Retiros Efectivos
         */
        private void repositoryItemLookUpEdit1_Leave(object sender, EventArgs e)
        {
            string parameter;
            int moneda;
            Vgc.PostEditor();
            Vgc.UpdateFocusedRecord();
            Vgc.FocusFirst();

            Utils.QueryParams qp = new LLNet.Utils.QueryParams();
            parameter = GUI.CApp.GetParam("APRE").ToString();
            moneda = Convert.ToInt32(rowMoneda.Properties.Value);


            if (parameter == "S")
            {
                if (moneda != 0)
                {   
                    rowMONTO.Enabled = false;
                    string Arqueo = GUI.CApp.GetSecuence("WMS_S_ARQUEO").ToString();

                    qp.AddReplaceParam("ARQUEO", Arqueo);
                    qp.AddReplaceParam("JORNADA", Program.Jornada.ToString());
                    qp.AddReplaceParam("USUARIO", GUI.CApp.usuario);
                    GUI.CApp.bll.ExecuteSql("NuevoArqueo", qp, "PDV");

                    CierreConsol.FrmRegsitroRetiro retiro = new CierreConsol.FrmRegsitroRetiro(Arqueo, moneda);
                    retiro.ShowDialog();

                    qp.AddReplaceParam("pMONEDA", moneda.ToString());
                    qp.AddReplaceParam("pARQUEO", Arqueo);
                    rowMONTO.Properties.Value = GUI.CApp.bll.GetSingleValue("MontoRetiro", qp, "PDV");
                }
                else
                {
                    GUI.CApp.ShowMessage("Seleccione una mondena antes de continuar", "Error en Retiro de Efectivo", true);
                    return;
                }
            }
            else
            {
                rowMONTO.Enabled = true;
            }

        }

        #endregion
    }
}

