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

namespace LLNet.Tienda.MainForms.Forms
{
    public partial class FrmCajaChica : Form
    {
        string Usuario = "";//Usuario que realiza el retiro de caja chica

        public FrmCajaChica(string usuario)
        {
            InitializeComponent();

            this.Usuario = usuario;
            Categorias();
            RetirosCH();
        }

        #region Categorias
        /// <summary>
        /// Obtener categorías de caja chica
        /// </summary>
        private void Categorias()
        {
            DataSet ds = GUI.CApp.bll.getDataSet("PDV", "ObtCategorias");
            this.rilueCategoria.DataSource = ds.Tables[0];
            this.rilueCategoria.DisplayMember = "RV_MEANING";
            this.rilueCategoria.ValueMember = "RV_LOW_VALUE";            
        }
        #endregion


        #region Categorias
        /// <summary>
        /// Obtener categorías de caja chica
        /// </summary>
        private string PorcentajeIVA(int pPiva)
        {
            string v_porcen = "";
            try
            {
                QueryParams qp = new QueryParams();
                qp.AddReplaceParam("PIVA", pPiva.ToString());
                v_porcen = Convert.ToString(GUI.CApp.bll.GetSingleValue("ObtIva", qp, "PDV"));

            }
            catch (Exception ex)
            {
                CApp.ShowMessage(ex.Message, "Caja Chica", true);
            }
                return v_porcen;
        }
        #endregion

        #region Subcategorias
        /// <summary>
        /// Obtiene las subcategorías de caja chica
        /// </summary>
        private void Subcategorias(string subc)
        {
            Utils.QueryParams qp = new QueryParams();
            qp.AddReplaceParam("SUBCATEGORIA", subc);
            DataSet ds = GUI.CApp.bll.getDataSet("PDV", "ObtSubcategorias", qp);

            //Verifica si tiene datos el dataset
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.rilueSubcategoria.ReadOnly = false;
                this.rilueSubcategoria.DataSource = ds.Tables[0];
                this.rilueSubcategoria.DisplayMember = "RV_MEANING";
                this.rilueSubcategoria.ValueMember = "RV_LOW_VALUE";
            }
            else
                this.rilueSubcategoria.ReadOnly = true;
        }
        #endregion


        #region RetirosCH
        //Obtiene los retiros de caja chica y llena el datagrid
        private void RetirosCH()
        {
            Utils.QueryParams qp = new QueryParams();
            qp.AddReplaceParam("JORNADA", Program.Jornada.ToString());

            DataSet ds = CApp.bll.getDataSet("PDV", "WMS_V_CAJACHICA", qp);
            this.gcRetirosCH.DataSource = null;
            this.gcRetirosCH.DataSource = ds.Tables[0];
        }
        #endregion

 #region AnularRetiroCH
        /// <summary>
        /// Anular un retiro de caja chica
        /// </summary>
        private void AnularRetiroCH()
        {
            if (this.gvRetirosCH.FocusedRowHandle >= 0)
            {
                string retiro = Convert.ToString(gvRetirosCH.GetRowCellValue(gvRetirosCH.FocusedRowHandle, "RETIRO"));
                Utils.QueryParams qp = new QueryParams();
                qp.AddReplaceParam("RETIRO", retiro);
                qp.AddReplaceParam("USUARIO", this.Usuario);
                GUI.CApp.bll.ExecuteSql("WMS_P_ANULCAJACH", qp, "PDV");

                this.gvRetirosCH.DeleteRow(gvRetirosCH.FocusedRowHandle);
            }
        }
        #endregion

        #region CargaFondoEfectivo 
          
        public decimal CargafondoEFE() 
        {
            decimal v_monto = 0;
            try
            {

            Utils.QueryParams pq = new LLNet.Utils.QueryParams();
            pq.AddReplaceParam("pJORNADA", Program.Jornada.ToString());
            
            
              v_monto = Convert.ToDecimal(GUI.CApp.bll.GetSingleValue("WU_FONDODISP", pq, "PDV"));
            }
            catch (Exception ex)
            { 
             CApp.ShowMessage(ex.Message,"Caja Chica",true);
            }
            return v_monto;
        }
        
        #endregion

        #region btnAgregarRetiro_Click
        private void btnAgregarRetiro_Click(object sender, EventArgs e)
        {
            


            try
            {

                if (this.rowNoDeposito.Properties.Value == null || this.rowNoDeposito.Properties.Value.ToString() == "")
                {
                    CApp.ShowMessage("Ingrese el número de depósito", "Caja Chica", true);
                }
                else if (this.rowDocumento.Properties.Value == null || this.rowDocumento.Properties.Value.ToString() == "")
                {
                    CApp.ShowMessage("Ingrese el número de documento", "Caja Chica", true);
                }
                else if (this.rowCategoria.Properties.Value == null || this.rowCategoria.Properties.Value.ToString() == "")
                {
                    CApp.ShowMessage("Seleccione una categoría", "Caja Chica", true);
                }
                else if (this.rilueSubcategoria.ReadOnly == false && (this.rowSubcategoria.Properties.Value == null || this.rowSubcategoria.Properties.Value.ToString() == ""))
                {
                    CApp.ShowMessage("Seleccione una subcategoría", "Caja Chica", true);
                }
                else if (this.rowSubTotal.Properties.Value == null || this.rowSubTotal.Properties.Value.ToString() == "")
                {
                    CApp.ShowMessage("Ingrese el subtotal", "Caja Chica", true);
                }
                else if ((this.rowIVA1.Properties.Value == null || this.rowIVA1.Properties.Value.ToString() == "") ||
                         (this.rowIVA13.Properties.Value == null || this.rowIVA13.Properties.Value.ToString() == "") ||
                         (this.rowIVA2.Properties.Value == null || this.rowIVA2.Properties.Value.ToString() == "") ||
                         (this.rowIVA4.Properties.Value == null || this.rowIVA4.Properties.Value.ToString() == "") )  
                {
                    CApp.ShowMessage("Ingrese el iva", "Caja Chica", true);
                }

                else if (this.rowTotal.Properties.Value == null || this.rowTotal.Properties.Value.ToString() == "" || decimal.Parse(this.rowTotal.Properties.Value.ToString()) == 0)
                {
                    CApp.ShowMessage("Ingrese el total", "Caja Chica", true);
                }
            
                else
                {
                    //Obtiene el idcajach
                    string retiro = GUI.CApp.GetSecuence("WMS_S_CAJACH").ToString();

                    Utils.QueryParams qp = new QueryParams();
                    qp.AddReplaceParam("IDCAJACH", retiro);
                    qp.AddReplaceParam("JORNADA", Program.Jornada.ToString());
                    qp.AddReplaceParam("CATEGORIA", this.rowCategoria.Properties.Value.ToString());
                    if (this.rilueSubcategoria.ReadOnly == true)
                        qp.AddReplaceParam("SUBCATEGORIA", "");
                    else

                        qp.AddReplaceParam("SUBCATEGORIA", this.rowSubcategoria.Properties.Value.ToString());
                    qp.AddReplaceParam("NO_DEPOSITO", this.rowNoDeposito.Properties.Value.ToString());
                    qp.AddReplaceParam("USUARIO", this.Usuario);
                    qp.AddReplaceParam("DOCUMENTO", this.rowDocumento.Properties.Value.ToString());
                    //Echo por Richard Vargas Martinez 03/10/2019
                    qp.AddReplaceParam("SUBTOTAL", this.rowSubTotal.Properties.Value.ToString());
                    qp.AddReplaceParam("MONTO", this.rowTotal.Properties.Value.ToString());

                    //Fin Richard Vargas Martinez 03/10/2019
                    GUI.CApp.bll.ExecuteSql("WMS_P_ALMCAJACH", qp, "PDV");

           //Echo por Richard Vargas Martinez 21/10/2019
            if(decimal.Parse(this.rowIVA13.Properties.Value.ToString()) > 0) 
            {
                 qp = new QueryParams();

                    qp.AddReplaceParam("IDCAJACH", retiro);
                    qp.AddReplaceParam("IVA",rowIVA13.Properties.Value.ToString());
                    qp.AddReplaceParam("PORCENTAJEIVA", PorcentajeIVA(1));
                    GUI.CApp.bll.ExecuteSql("WMS_P_IVA_CAJACHICA", qp, "PDV");
                

            }

            if(decimal.Parse(this.rowIVA4.Properties.Value.ToString()) > 0) 
            {
                qp = new QueryParams();

                    qp.AddReplaceParam("IDCAJACH", retiro);
                    qp.AddReplaceParam("IVA",rowIVA4.Properties.Value.ToString());
                    qp.AddReplaceParam("PORCENTAJEIVA", PorcentajeIVA(4));
                    GUI.CApp.bll.ExecuteSql("WMS_P_IVA_CAJACHICA", qp, "PDV");
            }
            if(decimal.Parse(this.rowIVA2.Properties.Value.ToString()) > 0) 
            {
                 qp = new QueryParams();

                    qp.AddReplaceParam("IDCAJACH", retiro);
                    qp.AddReplaceParam("IVA",rowIVA2.Properties.Value.ToString());
                    qp.AddReplaceParam("PORCENTAJEIVA", PorcentajeIVA(3));
                    GUI.CApp.bll.ExecuteSql("WMS_P_IVA_CAJACHICA", qp, "PDV");
            }
            if(decimal.Parse(this.rowIVA1.Properties.Value.ToString()) > 0) 
            {
                qp = new QueryParams();

                qp.AddReplaceParam("IDCAJACH", retiro);
                qp.AddReplaceParam("IVA", rowIVA1.Properties.Value.ToString());
                qp.AddReplaceParam("PORCENTAJEIVA", PorcentajeIVA(2));
                GUI.CApp.bll.ExecuteSql("WMS_P_IVA_CAJACHICA", qp, "PDV");
            }
            // Fin RVM


            this.rowNoDeposito.Properties.Value = null;
            this.rowDocumento.Properties.Value = null;
            this.rowCategoria.Properties.Value = null;
            this.rowSubcategoria.Properties.Value = null;
            this.rowSubTotal.Properties.Value = null;
            this.rowIVA1.Properties.Value = "0";
            this.rowIVA13.Properties.Value = "0";
            this.rowIVA2.Properties.Value = "0";
            this.rowIVA4.Properties.Value = "0"; 
            this.rowTotal.Properties.Value = null;
            this.rilueSubcategoria.ReadOnly = false;
                    //Se cargan los retiros
                    RetirosCH();

                    //Se muestra el reporte del retiro realizado
                    Reporte_Impreso___Retiros.report_viewer rv = new LLNet.Tienda.MainForms.Reporte_Impreso___Retiros.report_viewer(retiro);
                    rv.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                CApp.ShowMessage(ex.Message, "Caja Chica", true);
            }
        }
        #endregion

        #region btnCancelar_Click
        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        #endregion

        #region rilueCategoria_EditValueChanging
        private void rilueCategoria_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            try
            {
                this.rilueSubcategoria.DataSource = null;
                Subcategorias(e.NewValue.ToString());
                this.rowCategoria.Properties.Value = e.NewValue;
            }
            catch (Exception ex)
            {
                CApp.ShowMessage(ex.Message, "Caja Chica", true);
            }
        }
        #endregion

        #region FrmCajaChica_KeyDown
        private void FrmCajaChica_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    btnCancelar_Click(null, null);
                    break;
                case Keys.F4:
                    AnularRetiroCH();
                    break;
                case Keys.F5:
                    sbtnRConsolidado_Click(null, null);
                    break;
                case Keys.F6:
                    sbtnReimprimir_Click(null, null);
                    break;
                case Keys.F11:
                    btnAgregarRetiro_Click(null, null);
                    break;
            }
        }
        #endregion

        #region rilueSubcategoria_EditValueChanging
        private void rilueSubcategoria_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            try
            {
                this.rowSubcategoria.Properties.Value = e.NewValue;
            }
            catch (Exception ex)
            {
                CApp.ShowMessage(ex.Message, "Caja Chica", true);
            }
        }
        #endregion

  
        #region sbtnReimprimir_Click
        private void sbtnReimprimir_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.gvRetirosCH.FocusedRowHandle >= 0)
                {
                    string retiro = Convert.ToString(gvRetirosCH.GetRowCellValue(gvRetirosCH.FocusedRowHandle, "RETIRO"));
                    if (GUI.CApp.ShowMessage("¿Desea Re-Imprimir el Comprobante del Retiro #" + retiro + " ?", "Re-Imprimir Comprobantes Caja Chica", true, true) == DialogResult.OK)
                    {
                        Reporte_Impreso___Retiros.report_viewer rv = new LLNet.Tienda.MainForms.Reporte_Impreso___Retiros.report_viewer(retiro);
                        rv.ShowDialog();
                    }
                    else
                        return;                
                }
            }
            catch (Exception ex)
            {
                CApp.ShowMessage(ex.Message, "Caja Chica", true);
            }
        }
        #endregion

        #region sbtnRConsolidado_Click
        private void sbtnRConsolidado_Click(object sender, EventArgs e)
        {
            try
            {
                Reporte_Impreso___Retiros.report_viewer rv = new LLNet.Tienda.MainForms.Reporte_Impreso___Retiros.report_viewer("0");
                rv.ShowDialog();
            }
            catch (Exception ex)
            {
                CApp.ShowMessage(ex.Message, "Caja Chica", true);
            }
        }
        #endregion

        private void Vgc_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {


            if (this.rowSubTotal.Properties.Value != null && (this.rowIVA1.Properties.Value != "" &&
                this.rowIVA2.Properties.Value != "" && this.rowIVA4.Properties.Value != "" &&
                this.rowIVA13.Properties.Value != ""))
            {
               this.rowTotal.Properties.Value = decimal.Parse(this.rowSubTotal.Properties.Value.ToString()) +
                                                 decimal.Parse(this.rowIVA1.Properties.Value.ToString()) +
                                                 decimal.Parse(this.rowIVA13.Properties.Value.ToString()) +
                                                 decimal.Parse(this.rowIVA2.Properties.Value.ToString()) +
                                                 decimal.Parse(this.rowIVA4.Properties.Value.ToString());
            }
        }

    }
}