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 CargarGrillaMoratorias()
 {
     MoratoriasImplement oMoratoriasImplement = new MoratoriasImplement();
     dgvMoratorias.DataSource = oMoratoriasImplement.GetAllByAnioDT(cmbAnio.SelectedValue.ToString());
 }
 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 CargarMoratoria()
        {
            moratorias oMoratorias = new moratorias();
                MoratoriasImplement oMoratoriasImplement = new MoratoriasImplement();
                oMoratorias = oMoratoriasImplement.Get(_idMoratoria);
                dtpFechaInicial.Value = (DateTime)oMoratorias.vigencia_desde;
                dtpFechafinal.Value = (DateTime)oMoratorias.vigencia_hasta;
                txtCantCuotas.Text = oMoratorias.cantidad_cuotas_max.ToString();
                chkLimitar.Checked = (bool)oMoratorias.limitar;

                DetallesMoratoriaImplement oDetallesMoratoriaImplement = new DetallesMoratoriaImplement();
                _dtDetallesMoratoria = oDetallesMoratoriaImplement.GetAllByIdMoratoria(_idMoratoria);
                dgvDetallesMoratoria.DataSource = _dtDetallesMoratoria;

                int indexUltimo = _dtDetallesMoratoria.Rows.Count - 1;
                _primeraCuota = int.Parse(_dtDetallesMoratoria.Rows[indexUltimo]["cuota_hasta"].ToString()) + 1;
                txtCuotaDesde.Text = _primeraCuota.ToString();
        }
 private void Inicializar()
 {
     MoratoriasImplement oMoratoriasImplement = new MoratoriasImplement();
     if (oMoratoriasImplement.ExisteMoratoria(DateTime.Today, DateTime.Today,0))
     {
         ((Control)tabConvenioPago).Enabled = false;
         ((Control)tabMoratoria).Enabled = true;
         tabConvenio.SelectTab(1);
         _convenioMora = true;
         InicializarConvenioMoratoria();
     }
     else {
         ((Control)tabConvenioPago).Enabled = true;
         ((Control)tabMoratoria).Enabled = false;
         tabConvenio.SelectTab(0);
         InicializarConvenioNuevo();
     }
 }
        private void CargarDatosMoratoria()
        {
            FacturasImplement oFacturasImplement = new FacturasImplement();
            CalculosConvenio oCalculosConvenio = new CalculosConvenio();

            _ConveniosNoCancelados = oCalculosConvenio.ObtenerImporteConvenioAdeudado(_idSocio);

            DataTable dtFacturas = oFacturasImplement.GetByIdSocioSinConvenioDT(_idSocio, 1, 1);
            txtFactAdeudadasMora.Text = dtFacturas.Rows.Count.ToString();
            Decimal valorTotalSinInteres = 0;
            Decimal valorInteresesMora = 0;
            foreach (DataRow dr in dtFacturas.Rows)
            {
                #region <Control Valores Vacios>
                decimal Neto = 0;
                decimal IVA = 0;
                decimal Cobrado = 0;
                decimal Recargo=0;
                decimal IVARecargo = 0;

                if (dr["Neto"] == null | dr["Neto"].ToString() == "")
                    Neto = 0;
                else
                    Neto = decimal.Parse(dr["Neto"].ToString());
                if (dr["IVA"] == null | dr["IVA"].ToString() == "")
                    IVA = 0;
                else
                    IVA = decimal.Parse(dr["IVA"].ToString());
                if (dr["Cobrado"] == null | dr["Cobrado"].ToString() == "")
                    Cobrado = 0;
                else
                    Cobrado = decimal.Parse(dr["Cobrado"].ToString());

                if (dr["Recargo"] == null | dr["Recargo"].ToString() == "")
                    Recargo = 0;
                else
                    Recargo = decimal.Parse(dr["Recargo"].ToString());
                if (dr["IVARecargo"] == null | dr["IVARecargo"].ToString() == "")
                    IVARecargo = 0;
                else
                    IVARecargo = decimal.Parse(dr["IVARecargo"].ToString());

                #endregion

                valorTotalSinInteres = valorTotalSinInteres + (Neto + IVA - Cobrado);
                valorInteresesMora = valorInteresesMora + (Recargo + IVARecargo);
            }

            Decimal ImporteTotalSinInteresMora =valorTotalSinInteres + _ConveniosNoCancelados;
            txtTotalSinInteresMora.Text =ImporteTotalSinInteresMora.ToString();
            Decimal ImporteInteresesMora = valorInteresesMora;
            txtImporteInteresesMora.Text = ImporteInteresesMora.ToString();
            txtTotalMoratoria.Text = (ImporteTotalSinInteresMora + ImporteInteresesMora).ToString();
            txtCuotasAbonadasMora.Text = "1";
            txtDescPagoContadoMora.Text = "5%";
            txtCantMaxCuotasMora.Text = "-";

            facturas oFacturas = new facturas();
            oFacturas = oFacturasImplement.GetUltimaFactura(_idSocio);
            DataTable oFacturasDetallesDT = new DataTable();
            FacturasDetallesImplement oFacturasDetallesImplements = new FacturasDetallesImplement();
            oFacturasDetallesDT = oFacturasDetallesImplements.GetAllByIdFacturaDT(oFacturas.id_factura);
            decimal valorUltimaFactura =0;
            foreach(DataRow dr in oFacturasDetallesDT.Rows)
            {
                int _idConcepto = int.Parse(dr["id_concepto"].ToString());
                if(_idConcepto==1 | _idConcepto==2 | _idConcepto==3 |_idConcepto==8)
                {
                    valorUltimaFactura = valorUltimaFactura + decimal.Parse(dr["importe"].ToString());
                }
            }

            decimal valorMinimoCuota = Math.Round((valorUltimaFactura / 2), 2);
            txtValorMinimoCuotaMora.Text=valorMinimoCuota.ToString();

            int maxCantCuotas = (int)((ImporteTotalSinInteresMora + ImporteInteresesMora) / valorMinimoCuota);
            //Pregunto por si la moratoria tiene limitacion, si no la tiene cambio la cant max de cuotas por minimo monto
            //por la max cant de cuotas por moratoria directamente

            MoratoriasImplement oMoratoriasImplement = new MoratoriasImplement();
            DataTable cuotasDT = new DataTable();
            DateTime fechaConv = dtpFechaConvenio.Value;

            cuotasDT = oMoratoriasImplement.GetTablaCuotasConvenioMoratoria(maxCantCuotas, fechaConv, ImporteTotalSinInteresMora, ImporteInteresesMora);
            txtCantMaxCuotasMora.Text = cuotasDT.Rows.Count.ToString();//esta tabla trae la cantidad de cuotas previendo limitacion con valor minimo

            dgvCuotasMora.DataSource = cuotasDT;
        }