private void btnAgregar_Click(object sender, EventArgs e)
        {
            #region <validacion vacio>
                string Mensaje;
                Mensaje = "";
                if (txtCuotaDesde.Text.Length == 0) { Mensaje += "Falta cuota inicial." + Environment.NewLine; };
                if (txtCuotaHasta.Text.Length == 0) { Mensaje += "Falta cuota final." + Environment.NewLine; };
                if (txtCantCuotas.Text.Length == 0) { Mensaje += "Falta cuota final." + Environment.NewLine; };

                if (Mensaje.Length == 0)
                {
                    if (int.Parse(txtCuotaDesde.Text) > int.Parse(txtCuotaHasta.Text))
                    {
                        Mensaje += "Cuota inicial debe ser menor a cuota final." + Environment.NewLine;
                    }
                    if (int.Parse(txtCuotaHasta.Text) > int.Parse(txtCantCuotas.Text))
                    {
                        Mensaje += "El limite de cuotas es " + txtCantCuotas.Text + Environment.NewLine;
                    }
                }

                if (txtDescuento.Text.Length == 0) { Mensaje += "Falta descuento." + Environment.NewLine; };

                #endregion
                if (Mensaje.Length > 0)
                {
                    frmVentanaInformativa ofrmVentanaInformativa = new frmVentanaInformativa(Mensaje);
                    ofrmVentanaInformativa.ShowDialog();
                }
                else
                {
                    AgregarDetalleMoratoria();
                }
        }
        private void GuardarMoratoria()
        {
            //verificar si hay una moratoria q cruce fechas
            MoratoriasImplement oMoratoriasImplement = new MoratoriasImplement();
            bool existeMoratoria = oMoratoriasImplement.ExisteMoratoria(DateTime.Parse(dtpFechaInicial.Text), DateTime.Parse(dtpFechafinal.Text), _idMoratoria);

            if (!existeMoratoria)
            {
                moratorias oMoratoria = new moratorias();
                oMoratoria.vigencia_desde      = DateTime.Parse(dtpFechaInicial.Text);
                oMoratoria.vigencia_hasta      = DateTime.Parse(dtpFechafinal.Text);
                oMoratoria.cantidad_cuotas_max = int.Parse(txtCantCuotas.Text);
                oMoratoria.limitar             = chkLimitar.Checked;
                if (_idMoratoria > 0)
                {
                    oMoratoria.idmoratorias = _idMoratoria;
                    oMoratoriasImplement.Update(oMoratoria);
                }
                else
                {
                    _idMoratoria = oMoratoriasImplement.Save(oMoratoria);
                }
                DetallesMoratoriaImplement oDetallesMoratoriaImplement = new DetallesMoratoriaImplement();
                int ultimaCuota = 1;
                foreach (DataRow dr in _dtDetallesMoratoria.Rows)
                {
                    if (int.Parse(dr["iddetalles_moratoria"].ToString()) == 0)
                    {
                        detalles_moratoria oDetallesMoratoria = new detalles_moratoria();
                        oDetallesMoratoria.idmoratoria = _idMoratoria;
                        oDetallesMoratoria.cuota_desde = int.Parse(dr["cuota_desde"].ToString());
                        oDetallesMoratoria.cuota_hasta = int.Parse(dr["cuota_hasta"].ToString());
                        oDetallesMoratoria.descuento   = decimal.Parse(dr["descuento"].ToString());
                        oDetallesMoratoriaImplement.Save(oDetallesMoratoria);
                        ultimaCuota = int.Parse(dr["cuota_hasta"].ToString());
                    }
                }
                int indexUltimo = _dtDetallesMoratoria.Rows.Count - 1;
                ultimaCuota = int.Parse(_dtDetallesMoratoria.Rows[indexUltimo]["cuota_hasta"].ToString());
                if (ultimaCuota < oMoratoria.cantidad_cuotas_max)
                {
                    detalles_moratoria oDetallesMoratoria = new detalles_moratoria();
                    oDetallesMoratoria.idmoratoria = _idMoratoria;
                    oDetallesMoratoria.cuota_desde = ultimaCuota + 1;
                    oDetallesMoratoria.cuota_hasta = oMoratoria.cantidad_cuotas_max;
                    oDetallesMoratoria.descuento   = 0;
                    oDetallesMoratoriaImplement.Save(oDetallesMoratoria);
                }
                Inicializar();
                DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                frmVentanaInformativa ofrmVentanaInformativa = new frmVentanaInformativa("Existe una moratoria que comprende alguna de las fechas ingresadas.");
                ofrmVentanaInformativa.ShowDialog();
            }
        }
        private void btnAgregar_Click(object sender, EventArgs e)
        {
            #region <validacion vacio>
            string Mensaje;
            Mensaje = "";
            if (txtCuotaDesde.Text.Length == 0)
            {
                Mensaje += "Falta cuota inicial." + Environment.NewLine;
            }
            ;
            if (txtCuotaHasta.Text.Length == 0)
            {
                Mensaje += "Falta cuota final." + Environment.NewLine;
            }
            ;
            if (txtCantCuotas.Text.Length == 0)
            {
                Mensaje += "Falta cuota final." + Environment.NewLine;
            }
            ;

            if (Mensaje.Length == 0)
            {
                if (int.Parse(txtCuotaDesde.Text) > int.Parse(txtCuotaHasta.Text))
                {
                    Mensaje += "Cuota inicial debe ser menor a cuota final." + Environment.NewLine;
                }
                if (int.Parse(txtCuotaHasta.Text) > int.Parse(txtCantCuotas.Text))
                {
                    Mensaje += "El limite de cuotas es " + txtCantCuotas.Text + Environment.NewLine;
                }
            }

            if (txtDescuento.Text.Length == 0)
            {
                Mensaje += "Falta descuento." + Environment.NewLine;
            }
            ;

            #endregion
            if (Mensaje.Length > 0)
            {
                frmVentanaInformativa ofrmVentanaInformativa = new frmVentanaInformativa(Mensaje);
                ofrmVentanaInformativa.ShowDialog();
            }
            else
            {
                AgregarDetalleMoratoria();
            }
        }
        private void tsbAceptar_Click(object sender, EventArgs e)
        {
            #region <validacion>

            string Mensaje;
            Mensaje = "";
            if (dtpFechaInicial.Text.Length == 0)
            {
                Mensaje += "Falta fecha inicial." + Environment.NewLine;
            }
            ;
            if (dtpFechafinal.Text.Length == 0)
            {
                Mensaje += "Falta fecha final." + Environment.NewLine;
            }
            ;

            if (Mensaje.Length == 0)
            {
                if (DateTime.Parse(dtpFechaInicial.Text) > DateTime.Parse(dtpFechafinal.Text))
                {
                    Mensaje += "Fecha inicial debe ser menor a fecha final." + Environment.NewLine;
                }
            }

            if (txtCantCuotas.Text.Length == 0)
            {
                Mensaje += "Falta cantidad cuotas." + Environment.NewLine;
            }
            ;

            if (_dtDetallesMoratoria.Rows.Count == 0)
            {
                Mensaje += "Debe ingresar algun detalle." + Environment.NewLine;
            }
            ;
            #endregion
            if (Mensaje.Length > 0)
            {
                frmVentanaInformativa ofrmVentanaInformativa = new frmVentanaInformativa(Mensaje);
                ofrmVentanaInformativa.ShowDialog();
            }
            else
            {
                GuardarMoratoria();
            }
        }
        private void btnEliminar_Click(object sender, EventArgs e)
        {
            MoratoriasImplement oMoratoriasImplement = new MoratoriasImplement();
            if (_idMoratoria > 0)
            {
                if (oMoratoriasImplement.TieneConvenios(_idMoratoria))
                {
                    frmVentanaInformativa oFrmVentanaInformativa = new frmVentanaInformativa("Esta moratoria no puede eliminarse por que ya cuenta con convenios realizados para la misma.");
                    oFrmVentanaInformativa.ShowDialog();
                }
                else
                {

                    DetallesMoratoriaImplement oDetallesMoratoriaImplement = new DetallesMoratoriaImplement();
                    oDetallesMoratoriaImplement.DeleteByIdMoratoria(_idMoratoria);
                    oMoratoriasImplement.Delete(_idMoratoria);
                    CargarGrillaMoratorias();
                }
            }
        }
        private void btnEliminar_Click(object sender, EventArgs e)
        {
            MoratoriasImplement oMoratoriasImplement = new MoratoriasImplement();

            if (_idMoratoria > 0)
            {
                if (oMoratoriasImplement.TieneConvenios(_idMoratoria))
                {
                    frmVentanaInformativa oFrmVentanaInformativa = new frmVentanaInformativa("Esta moratoria no puede eliminarse por que ya cuenta con convenios realizados para la misma.");
                    oFrmVentanaInformativa.ShowDialog();
                }
                else
                {
                    DetallesMoratoriaImplement oDetallesMoratoriaImplement = new DetallesMoratoriaImplement();
                    oDetallesMoratoriaImplement.DeleteByIdMoratoria(_idMoratoria);
                    oMoratoriasImplement.Delete(_idMoratoria);
                    CargarGrillaMoratorias();
                }
            }
        }
        private void btnEditar_Click(object sender, EventArgs e)
        {
            MoratoriasImplement oMoratoriasImplement = new MoratoriasImplement();
            if (_idMoratoria > 0)
            {
                if (oMoratoriasImplement.TieneConvenios(_idMoratoria))
                {
                    frmVentanaInformativa oFrmVentanaInformativa = new frmVentanaInformativa("Esta moratoria no puede editarse por que ya cuenta con convenios realizados para la misma.");
                    oFrmVentanaInformativa.ShowDialog();
                }
                else
                {
                    frmConfiguracionesPestanias oFrmConvenioMora = new frmConfiguracionesPestanias(_idMoratoria);
                    if (oFrmConvenioMora.ShowDialog() == DialogResult.OK)
                    {
                        CargarAnios();
                    }

                }
            }
        }
        private void btnEditar_Click(object sender, EventArgs e)
        {
            MoratoriasImplement oMoratoriasImplement = new MoratoriasImplement();

            if (_idMoratoria > 0)
            {
                if (oMoratoriasImplement.TieneConvenios(_idMoratoria))
                {
                    frmVentanaInformativa oFrmVentanaInformativa = new frmVentanaInformativa("Esta moratoria no puede editarse por que ya cuenta con convenios realizados para la misma.");
                    oFrmVentanaInformativa.ShowDialog();
                }
                else
                {
                    frmConfiguracionesPestanias oFrmConvenioMora = new frmConfiguracionesPestanias(_idMoratoria);
                    if (oFrmConvenioMora.ShowDialog() == DialogResult.OK)
                    {
                        CargarAnios();
                    }
                }
            }
        }
        private void tsbAceptar_Click(object sender, EventArgs e)
        {
            #region <validacion>

                string Mensaje;
                Mensaje = "";
                if (dtpFechaInicial.Text.Length == 0) { Mensaje += "Falta fecha inicial." + Environment.NewLine; };
                if (dtpFechafinal.Text.Length == 0) { Mensaje += "Falta fecha final." + Environment.NewLine; };

                if (Mensaje.Length == 0)
                {
                    if (DateTime.Parse(dtpFechaInicial.Text) > DateTime.Parse(dtpFechafinal.Text))
                    {
                        Mensaje += "Fecha inicial debe ser menor a fecha final." + Environment.NewLine;
                    }

                }

                if (txtCantCuotas.Text.Length == 0) { Mensaje += "Falta cantidad cuotas." + Environment.NewLine; };

                if (_dtDetallesMoratoria.Rows.Count == 0) { Mensaje += "Debe ingresar algun detalle." + Environment.NewLine; };
                #endregion
                if (Mensaje.Length > 0)
                {
                    frmVentanaInformativa ofrmVentanaInformativa = new frmVentanaInformativa(Mensaje);
                    ofrmVentanaInformativa.ShowDialog();
                }
                else
                {
                    GuardarMoratoria();

                }
        }
 private void GuardarMoratoria()
 {
     //verificar si hay una moratoria q cruce fechas
         MoratoriasImplement oMoratoriasImplement = new MoratoriasImplement();
         bool existeMoratoria = oMoratoriasImplement.ExisteMoratoria(DateTime.Parse(dtpFechaInicial.Text), DateTime.Parse(dtpFechafinal.Text), _idMoratoria);
         if (!existeMoratoria)
         {
             moratorias oMoratoria = new moratorias();
             oMoratoria.vigencia_desde = DateTime.Parse(dtpFechaInicial.Text);
             oMoratoria.vigencia_hasta = DateTime.Parse(dtpFechafinal.Text);
             oMoratoria.cantidad_cuotas_max = int.Parse(txtCantCuotas.Text);
             oMoratoria.limitar = chkLimitar.Checked;
             if (_idMoratoria > 0)
             {
                 oMoratoria.idmoratorias = _idMoratoria;
                 oMoratoriasImplement.Update(oMoratoria);
             }
             else
             {
                 _idMoratoria = oMoratoriasImplement.Save(oMoratoria);
             }
             DetallesMoratoriaImplement oDetallesMoratoriaImplement = new DetallesMoratoriaImplement();
             int ultimaCuota = 1;
             foreach(DataRow dr in _dtDetallesMoratoria.Rows)
             {
                 if (int.Parse(dr["iddetalles_moratoria"].ToString()) == 0)
                 {
                     detalles_moratoria oDetallesMoratoria = new detalles_moratoria();
                     oDetallesMoratoria.idmoratoria = _idMoratoria;
                     oDetallesMoratoria.cuota_desde = int.Parse(dr["cuota_desde"].ToString());
                     oDetallesMoratoria.cuota_hasta = int.Parse(dr["cuota_hasta"].ToString());
                     oDetallesMoratoria.descuento = decimal.Parse(dr["descuento"].ToString());
                     oDetallesMoratoriaImplement.Save(oDetallesMoratoria);
                     ultimaCuota = int.Parse(dr["cuota_hasta"].ToString());
                 }
             }
             int indexUltimo = _dtDetallesMoratoria.Rows.Count - 1;
             ultimaCuota = int.Parse(_dtDetallesMoratoria.Rows[indexUltimo]["cuota_hasta"].ToString());
             if (ultimaCuota < oMoratoria.cantidad_cuotas_max)
             {
                 detalles_moratoria oDetallesMoratoria = new detalles_moratoria();
                 oDetallesMoratoria.idmoratoria = _idMoratoria;
                 oDetallesMoratoria.cuota_desde = ultimaCuota+1;
                 oDetallesMoratoria.cuota_hasta = oMoratoria.cantidad_cuotas_max;
                 oDetallesMoratoria.descuento = 0;
                 oDetallesMoratoriaImplement.Save(oDetallesMoratoria);
             }
             Inicializar();
             DialogResult = DialogResult.OK;
             this.Close();
         }
         else {
             frmVentanaInformativa ofrmVentanaInformativa = new frmVentanaInformativa("Existe una moratoria que comprende alguna de las fechas ingresadas.");
             ofrmVentanaInformativa.ShowDialog();
         }
 }