private void bRealizarPrestamo_Click(object sender, EventArgs e)
        {
            // Comprobar si está seleccionado el lector y el libro:
            if (this.lectoresBindingSource.Position == -1 || this.librosBindingSource.Position == -1)
            {
                MessageBox.Show("Para realizar un préstamo es necesario seleccionar y lector y un libro");
                return;
            }
            // Comprobar si el libro está prestado:
            if (dS_Libros.libros[librosBindingSource.Position].prestado_sn_lib == "S")
            {
                MessageBox.Show("El libro seleccionado ya está prestado");
                return;
            }
            // Comprobar el límite de libros a prestar:
            if (dS_LibrosPrestados.LibrosPrestados.Rows.Count == 5)
            {
                MessageBox.Show("No se pueden prestar más de 5 libros a un lector");
                return;
            }
            // Comprobar si el lector no esta penalizado:
            // ¡Atención no se compara directamente con NULL sino que se utiliza el método específico Is___Null!
            if (!dS_Lectores.lectores[0].Isfecha_penalizacion_lecNull())
            {
                MessageBox.Show("No se puede prestar libros a un lector penalizado");
                return;
            }
            // Realizar el préstamo:
            try
            {
                var prestamos   = new DS_Prestamos.prestamosDataTable();
                var prestamosTa = new DS_PrestamosTableAdapters.prestamosTableAdapter();

                // Crear una nueva fila en la tabla prestamos:
                var fila = prestamos.NewprestamosRow();
                fila.id_lec_pre       = dS_Lectores.lectores[0].id_lec;
                fila.id_lib_pre       = dS_Libros.libros[librosBindingSource.Position].id_lib;
                fila.fecha_presta_pre = DateTime.Now;
                fila.fecha_devol_pre  = DateTime.Now.AddDays(configuracion[0].dias_prestamo_cnf);
                prestamos.AddprestamosRow(fila);
                prestamosTa.Update(prestamos);

                // Actualizar el libro como prestado:
                dS_Libros.libros[librosBindingSource.Position].prestado_sn_lib = "S";
                librosTableAdapter.Update(dS_Libros.libros);

                // Actualizar la visualización de libros prestados:
                var filaPrestados = dS_LibrosPrestados.LibrosPrestados.NewLibrosPrestadosRow();
                filaPrestados.id_lec_pre       = fila.id_lec_pre;
                filaPrestados.id_lib           = fila.id_lib_pre;
                filaPrestados.titulo_lib       = dS_Libros.libros[librosBindingSource.Position].titulo_lib;
                filaPrestados.fecha_presta_pre = fila.fecha_presta_pre;
                filaPrestados.fecha_devol_pre  = fila.fecha_devol_pre;
                dS_LibrosPrestados.LibrosPrestados.AddLibrosPrestadosRow(filaPrestados);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Se ha producido un error en la creación del préstamo:" + ex.Message);
            }
        }
Beispiel #2
0
        private void librosPrestadosDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int posicion = librosPrestadosBindingSource.Position;
            int id       = dS_LibrosPrestados.LibrosPrestados[posicion].id_lib;


            if (e.ColumnIndex == 4 && id_lecLabel1.Text != "")
            {
                DS_Configuracion.configuracionDataTable configuracion            = new DS_Configuracion.configuracionDataTable();
                DS_ConfiguracionTableAdapters.configuracionTableAdapter configTa = new DS_ConfiguracionTableAdapters.configuracionTableAdapter();
                configTa.Fill(configuracion);

                DS_Historico_prestamos.historico_prestamosDataTable historicoPrestamos          = new DS_Historico_prestamos.historico_prestamosDataTable();
                DS_Historico_prestamosTableAdapters.historico_prestamosTableAdapter historicoTa = new DS_Historico_prestamosTableAdapters.historico_prestamosTableAdapter();
                DS_Historico_prestamos.historico_prestamosRow fila = historicoPrestamos.Newhistorico_prestamosRow();
                fila.fecha_devol_his  = DateTime.Now;
                fila.fecha_presta_his = dS_LibrosPrestados.LibrosPrestados[posicion].fecha_presta_pre;
                fila.id_lec_his       = int.Parse(id_lecLabel1.Text);
                fila.id_lib_his       = dS_LibrosPrestados.LibrosPrestados[posicion].id_lib;
                historicoPrestamos.Addhistorico_prestamosRow(fila);
                historicoTa.Update(historicoPrestamos);


                DS_Prestamos.prestamosDataTable prestamos = new DS_Prestamos.prestamosDataTable();
                DS_PrestamosTableAdapters.prestamosTableAdapter prestamosTa = new DS_PrestamosTableAdapters.prestamosTableAdapter();
                prestamosTa.Fill(prestamos);


                if (DateTime.Now > dS_LibrosPrestados.LibrosPrestados[posicion].fecha_devol_pre)
                {
                    int numeroLectores = dS_Lectores.lectores.Count;
                    for (int i = 0; i < numeroLectores; i++)
                    {
                        if (dS_Lectores.lectores[i].id_lec == int.Parse(id_lecLabel1.Text))
                        {
                            DateTime fechaPenal = DateTime.Now.AddDays(DateTime.Now.DayOfYear - dS_LibrosPrestados.LibrosPrestados[posicion].fecha_devol_pre.DayOfYear);
                            if (fechaPenal.DayOfYear > configuracion[0].dias_penalizacion_cnf)
                            {
                                dS_Lectores.lectores[i].fecha_penalizacion_lec = DateTime.Now.AddDays(configuracion[0].dias_penalizacion_cnf);
                                lectoresBindingSource.EndEdit();
                                lectoresTableAdapter.Update(dS_Lectores.lectores);
                            }
                            else
                            {
                                dS_Lectores.lectores[i].fecha_penalizacion_lec = DateTime.Now.AddDays(fechaPenal.DayOfYear);
                            }
                            lectoresBindingSource.EndEdit();
                            lectoresTableAdapter.Update(dS_Lectores.lectores);
                        }
                    }
                }
                else
                {
                    fila.fecha_devol_his = DateTime.Now;
                }


                if (librosTableAdapter.FillById(dS_Libros.libros, dS_LibrosPrestados.LibrosPrestados[posicion].id_lib) == 1)
                {
                    dS_Libros.libros[0].prestado_sn_lib.Remove(0);
                    dS_Libros.libros[0].prestado_sn_lib = "N";
                    librosBindingSource.EndEdit();
                    librosTableAdapter.Update(dS_Libros.libros);
                }

                for (int i = 0; i < prestamos.Count; i++)
                {
                    if (prestamos[i].id_lib_pre == dS_Libros.libros[posicion].id_lib)
                    {
                        prestamos[i].Delete();
                        prestamosTa.Update(prestamos);
                        MessageBox.Show("El libro se ha devuelto correctamente");
                    }
                }
                librosPrestadosTableAdapter.FillById(dS_LibrosPrestados.LibrosPrestados, int.Parse(id_lecLabel1.Text));
            }
            else
            {
                MessageBox.Show("Busque un lector.");
            }
            id_lecLabel1.Focus();
        }
Beispiel #3
0
        private void bPrestamo_Click(object sender, EventArgs e)
        {
            int posicionLibros = librosBindingSource.Position;
            //int idLector = int.Parse(id_lecLabel1.Text);
            DateTime fechaActual = DateTime.Today;
            //DateTime? fechaPenalizacion = dS_Lectores.lectores[0].fecha_penalizacion_lec;
            string fecha = fechaActual.ToShortDateString();

            //int dias = dS_Configuracion.configuracion[0].dias_prestamo_cnf;
            Prestado = dS_Libros.libros[posicionLibros].prestado_sn_lib;

            if (id_lecLabel1.Text != "")
            {
                if (Prestado.Contains("N") && librosPrestadosDataGridView.RowCount <= 5)
                {
                    if (dS_Lectores.lectores[0].Isfecha_penalizacion_lecNull() || dS_Lectores.lectores[0].fecha_penalizacion_lec < DateTime.Today)
                    {
                        DS_Configuracion.configuracionDataTable configuracion = new DS_Configuracion.configuracionDataTable();
                        DS_ConfiguracionTableAdapters.configuracionTableAdapter configuracionTa = new DS_ConfiguracionTableAdapters.configuracionTableAdapter();
                        configuracionTa.Fill(configuracion);

                        int diasPrestamo = configuracion[0].dias_prestamo_cnf;
                        DS_Prestamos.prestamosDataTable prestamos = new DS_Prestamos.prestamosDataTable();
                        DS_PrestamosTableAdapters.prestamosTableAdapter prestamosTa = new DS_PrestamosTableAdapters.prestamosTableAdapter();
                        DS_Prestamos.prestamosRow fila = prestamos.NewprestamosRow();
                        fila.id_lec_pre       = dS_Lectores.lectores[0].id_lec;
                        fila.id_lib_pre       = dS_Libros.libros[posicionLibros].id_lib;
                        fila.fecha_presta_pre = DateTime.Now;
                        fila.fecha_devol_pre  = DateTime.Now.AddDays(configuracion[0].dias_prestamo_cnf);

                        prestamos.AddprestamosRow(fila);
                        prestamosTa.Update(prestamos);


                        dS_Libros.libros[posicionLibros].prestado_sn_lib.Remove(0, dS_Libros.libros[posicionLibros].prestado_sn_lib.Length);
                        dS_Libros.libros[posicionLibros].prestado_sn_lib = "S";
                        librosBindingSource.EndEdit();
                        librosTableAdapter.Update(dS_Libros.libros);
                        librosDataGridView.Update();

                        //dS_Lectores.lectores[0].Setfecha_penalizacion_lecNull();
                        //lectoresBindingSource.EndEdit();
                        //lectoresTableAdapter.Update(dS_Lectores.lectores);
                        //lectoresTableAdapter.FillById(dS_Lectores.lectores,int.Parse(tbIDLector.Text));

                        librosPrestadosTableAdapter.FillByIdLector(dS_LibrosPrestados.LibrosPrestados, int.Parse(tbIDLector.Text));

                        MessageBox.Show("El préstamo se ha realizado correctamente.");
                    }
                    else
                    {
                        MessageBox.Show("El usuario está penalizado y no puede realizar préstamos.");
                    }
                }
                else
                {
                    MessageBox.Show("No se puede prestar el libro porque ya está prestado o la persona tiene 5 libros prestados.");
                }
            }
            else
            {
                MessageBox.Show("Debes buscar primero un lector.");
            }
        }