private void dgrid_Creditos_SelectionChanged(object sender, EventArgs e)
 {
     if (dgrid_Creditos.SelectedRows.Count > 0)
     {
         Cpago_credito objcred = ((Cpago_credito)dgrid_Creditos.CurrentRow.DataBoundItem);
         if (!proveedor)
         {
             decimal pagado = 0;
             foreach (Crecibo_debito aux in objcuenta.Debitos)
             {
                 if (aux.Creditos() != null)
                 {
                     if (aux.Creditos().Contains(objcred))
                     {
                         pagado += aux.Importe;
                     }
                 }
             }
             L_Incial.Text = objcred.Importe.ToString();
             L_Pagado.Text = pagado.ToString();
             L_APagar.Text = (objcred.Importe - pagado).ToString();
         }
         if (ModoModificacion)
         {
             if (LCreditos.Contains(objcred))
             {
                 dgrid_Creditos.CurrentRow.Cells["Concepto"].Style.BackColor = Color.FromArgb(64, 64, 64);
                 dgrid_Creditos.CurrentRow.Cells["Importe"].Style.BackColor  = Color.FromArgb(64, 64, 64);
                 txt_concepto.Text = txt_concepto.Text.Replace(objcred.Concepto + ", ", "");
                 if (LCreditos.Count == 2)
                 {
                     txt_importe.Enabled = true;
                 }
                 total           -= Convert.ToDecimal(L_APagar.Text);
                 txt_importe.Text = total.ToString();
                 LCreditos.Remove(objcred);
             }
             else
             {
                 dgrid_Creditos.CurrentRow.Cells["Concepto"].Style.BackColor = Color.FromArgb(240, 194, 70);
                 dgrid_Creditos.CurrentRow.Cells["Importe"].Style.BackColor  = Color.FromArgb(240, 194, 70);
                 txt_concepto.Text = txt_concepto.Text + objcred.Concepto + ", ";
                 if (LCreditos.Count < 1)
                 {
                     total            = Convert.ToDecimal(L_APagar.Text);
                     ApagarDelprimero = total;
                 }
                 else if (LCreditos.Count == 1)
                 {
                     txt_importe.Enabled = false;
                     total += Convert.ToDecimal(L_APagar.Text);
                 }
                 else
                 {
                     total += Convert.ToDecimal(L_APagar.Text);
                 }
                 LCreditos.Add(objcred);
                 txt_importe.Text = total.ToString();
             }
             dgrid_Creditos.ClearSelection();
         }
         else
         {
             dgrid_Debitos.ClearSelection();
             btn_Eliminar.Enabled  = true;
             btn_Modificar.Enabled = true;
             if (vertodos)
             {
                 if (!objcred.AltaOp())
                 {
                     btn_DarBaja.Enabled = true;
                 }
                 else
                 {
                     btn_DarBaja.Enabled = false;
                 }
             }
             else
             {
                 btn_DarBaja.Enabled = true;
             }
         }
     }
 }
        private void btn_Guardar_Click(object sender, EventArgs e)
        {
            try
            {
                if (objoperacion == null)
                {
                    Ctrl_conceptos admconceptos = Ctrl_conceptos.ClaseActiva();
                    if (rb_credito.Checked)
                    {
                        Cpago_credito objcredi = new Cpago_credito(txt_concepto.Text, Convert.ToDecimal(txt_importe.Text), DateTime.Today, true);
                        adm.AgregarOperacion(objcredi, objcuenta);
                        if (proveedor)
                        {
                            if (LRecibos.Count > 0)
                            {
                                bool darbaja = false;
                                if (LRecibos.Count > 1 || ApagarDelprimero == objcredi.Importe)
                                {
                                    adm.DarBajaoAlta(objcredi, objcuenta, false);
                                    darbaja = true;
                                }
                                objcredi.InstanciaRecibos();
                                int           seguidor;
                                int           tope;
                                Cpago_credito auxCredito;
                                foreach (Crecibo_debito auxDebito in LRecibos)
                                {
                                    adm.AgregarReferiaEntreOperacion(objcredi, auxDebito);
                                    objcredi.AgregarRecibo(auxDebito);
                                    if (darbaja)
                                    {
                                        adm.DarBajaoAlta(auxDebito, objcuenta, false);
                                        seguidor = 0;
                                        tope     = objcuenta.Creditos.Count;
                                        while (seguidor < tope)
                                        {
                                            auxCredito = objcuenta.Creditos[seguidor];
                                            if (auxCredito.Recibos() != null)
                                            {
                                                if (auxCredito.Recibos().Contains(auxDebito))
                                                {
                                                    adm.DarBajaoAlta(auxCredito, objcuenta, false);
                                                    tope--;
                                                    seguidor--;
                                                }
                                            }
                                            seguidor++;
                                        }
                                    }
                                }
                            }
                        }
                        if (cb_afecta_caja.Checked)
                        {
                            Cgasto objgasto = new Cgasto(admconceptos.BuscarPorID(1), objcredi.Importe, forma);
                            admcaja.AgregarOperacion(objgasto, ObjCajaDiaria);
                            adm.AgregarReferiaEntreOperacion(objcredi, objgasto);
                        }
                    }
                    else
                    {
                        Crecibo_debito objdebito = new Crecibo_debito(txt_concepto.Text, Convert.ToDecimal(txt_importe.Text), DateTime.Today, true);
                        adm.AgregarOperacion(objdebito, objcuenta);
                        if (!proveedor)
                        {
                            if (LCreditos.Count > 0)
                            {
                                bool darbaja = false;
                                if (LCreditos.Count > 1 || ApagarDelprimero == objdebito.Importe)
                                {
                                    adm.DarBajaoAlta(objdebito, objcuenta, false);
                                    darbaja = true;
                                }
                                objdebito.InstanciaCreditos();
                                int            seguidor;
                                int            tope;
                                Crecibo_debito auxDebito;
                                foreach (Cpago_credito auxCredito in LCreditos)
                                {
                                    adm.AgregarReferiaEntreOperacion(auxCredito, objdebito);
                                    objdebito.AgregarCreditos(auxCredito);

                                    if (darbaja)
                                    {
                                        adm.DarBajaoAlta(auxCredito, objcuenta, false);
                                        seguidor = 0;
                                        tope     = objcuenta.Debitos.Count;
                                        while (seguidor < tope)
                                        {
                                            auxDebito = objcuenta.Debitos[seguidor];
                                            if (auxDebito.Creditos() != null)
                                            {
                                                if (auxDebito.Creditos().Contains(auxCredito))
                                                {
                                                    adm.DarBajaoAlta(auxDebito, objcuenta, false);
                                                    tope--;
                                                    seguidor--;
                                                }
                                            }
                                            seguidor++;
                                        }
                                    }
                                }
                            }
                        }
                        if (cb_afecta_caja.Checked)
                        {
                            Ccobro objcobro = new Ccobro(admconceptos.BuscarPorID(2), objdebito.Importe, forma);
                            admcaja.AgregarOperacion(objcobro, ObjCajaDiaria);
                            adm.AgregarReferiaEntreOperacion(objdebito, objcobro);
                        }
                    }
                }
                else
                {
                    if (rb_credito.Checked)
                    {
                        Cpago_credito objcredito = (Cpago_credito)objoperacion;
                        objcredito.Concepto = txt_concepto.Text;
                        adm.ModificarOperacion(new Cpago_credito(objoperacion.Numero, txt_concepto.Text, Convert.ToDecimal(txt_importe.Text), Convert.ToDateTime(txt_dia.Text), objcredito.AltaOp()));
                        objcuenta.ModificarCredito(objcredito, Convert.ToDecimal(txt_importe.Text));
                    }
                    else
                    {
                        Crecibo_debito objdebito = (Crecibo_debito)objoperacion;
                        objdebito.Concepto = txt_concepto.Text;
                        adm.ModificarOperacion(new Crecibo_debito(objoperacion.Numero, txt_concepto.Text, Convert.ToDecimal(txt_importe.Text), Convert.ToDateTime(txt_dia.Text), objdebito.AltaOp()));
                        objcuenta.ModificarDebito(objdebito, Convert.ToDecimal(txt_importe.Text));
                    }
                }
                ActualizarGrillaCreditos();
                ActualizarGrillaDebitos();
                lbl_saldo.Text = objcuenta.Saldo.ToString();
                btn_Cancelar_Click(sender, e);
                adm.Modificar(objcuenta);
            }
            catch (Exception ex)
            {
                Frm_confirmacion frm = new Frm_confirmacion(ex.Message, "Bueno, esto es embarazoso. Pero lo solucionaremos :)", "Aceptar");
                frm.ShowDialog();
            }
        }