Пример #1
0
        /// <summary>
        /// Inserta un alimento en la base de datos
        /// </summary>
        /// <returns>Si se ha insertado o no correctamente</returns>
        public bool Insert()
        {
            int idAlimento = Alimento.InsertarImagen(this.Imagen);

            using (SqlConnection conexion = new SqlConnection(Properties.Settings.Default.ConnectionString))
            {
                conexion.Open();
                int    rows = 0;
                string insert;

                if (idAlimento > 0)
                {
                    insert = "INSERT into dbo.Alimento(Nombre, Familia, Imagen, Descripcion) VALUES (@nombre, @familia, @imagen, @descripcion)";
                }
                else
                {
                    insert = "INSERT into dbo.Alimento(Nombre, Familia, Descripcion) VALUES (@nombre, @familia, @descripcion)";
                }

                using (SqlCommand query = new SqlCommand(insert))
                {
                    query.Connection = conexion;
                    query.Parameters.Add("@nombre", SqlDbType.VarChar, 50).Value = this.Nombre;
                    if (this.Familia != null)
                    {
                        query.Parameters.Add("@familia", SqlDbType.Int).Value = this.Familia.Id;
                    }
                    else
                    {
                        query.Parameters.Add("@familia", SqlDbType.Int).Value = DBNull.Value;
                    }
                    query.Parameters.Add("@descripcion", SqlDbType.VarChar, 200).Value = this.Descripcion;

                    if (idAlimento > 0)
                    {
                        query.Parameters.Add("@imagen", SqlDbType.Int).Value = idAlimento;
                    }
                    rows = query.ExecuteNonQuery();
                }
                conexion.Close();
                return(rows > 0);
            }
        }
Пример #2
0
        private void dataGridAlimentos_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int id = (int)dataGridAlimentos.CurrentRow.Cells["Id"].Value;
                this.alimento  = Alimento.Select(id);
                txtNombre.Text = this.alimento.Nombre;
                try
                {
                    txtFamilia.Text = this.alimento.Familia.Nombre;
                }
                catch (Exception ex) { txtFamilia.Text = ""; }

                txtDescripcion.Text = this.alimento.Descripcion;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "Se debe seleccionar un alimento", "Seleccione uno", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Пример #3
0
        /// <summary>
        /// Devuelve un Alimento dado su nombre
        /// </summary>
        /// <param name="Nombre">Nombre del alimento</param>
        /// <returns>El alimento</returns>
        public static Alimento SelectWhereNombreIs(string Nombre)
        {
            Alimento a = new Alimento();

            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString))
            {
                conn.Open();
                string selectString = "select * from Alimento where Nombre like @nombre";
                using (SqlCommand selectCommand = new SqlCommand(selectString, conn))
                {
                    selectCommand.Parameters.Add("@nombre", SqlDbType.VarChar, 50).Value = Nombre;
                    SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection);
                    while (reader.Read())
                    {
                        a.Id     = reader.GetInt32(0);
                        a.Nombre = reader.GetString(1);
                    }
                }
            }
            return(a);
        }
Пример #4
0
        /// <summary>
        /// Devuelve un ArrayList con los Productos que tienen una misma fecha de caducidad y almacén
        /// </summary>
        /// <param name="fechaCaducidad">La fecha de caducidad</param>
        /// <param name="almacen">El almacén</param>
        /// <returns></returns>
        public static ArrayList SelectGroupByFechaCadAlmacen(Alimento alimento, DateTime fechaCaducidad, Almacen almacen)
        {
            ArrayList productos = new ArrayList();

            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString))
            {
                conn.Open();
                string selectString = "select * from Producto where Alimento = @alimento and FechaCaducidad = @fechacaducidad and Almacen =  @almacen";
                using (SqlCommand selectCommand = new SqlCommand(selectString, conn))
                {
                    selectCommand.Parameters.Add("@alimento", SqlDbType.Int).Value            = alimento.Id;
                    selectCommand.Parameters.Add("@fechacaducidad", SqlDbType.DateTime).Value = fechaCaducidad;
                    selectCommand.Parameters.Add("@almacen", SqlDbType.Int).Value             = almacen.Id;
                    SqlDataReader reader = selectCommand.ExecuteReader();
                    while (reader.Read())
                    {
                        Producto p = new Producto();
                        p.Id                      = reader.GetInt32(0);
                        p.Alimento                = Alimento.Select(reader.GetInt32(1));
                        p.FechaEntrada            = reader.GetDateTime(2);
                        p.FechaCaducidad          = reader.GetDateTime(3);
                        p.FechaConsumirPreferente = reader.GetDateTime(4);
                        p.Proveedor               = reader.GetString(5);
                        p.Ubicacion               = reader.GetString(6);
                        p.Almacen                 = Almacen.Select(reader.GetInt32(7));
                        try
                        {
                            p.Entidad = Entidad.Select(reader.GetInt32(8));
                        }
                        catch (Exception ex)
                        {
                            p.Entidad = null;
                        }
                        productos.Add(p);
                    }
                    conn.Close();
                }
            }
            return(productos);
        }
Пример #5
0
        private void crearProducto_Load(object sender, EventArgs e)
        {
            ArrayList alimentos = Alimento.SelectAll();

            cmb_alimentos.Items.Clear();
            for (int i = 0; i < alimentos.Count; i++)
            {
                Alimento a = (Alimento)alimentos[i];
                cmb_alimentos.Items.Add(a.Nombre);
            }

            ArrayList almacenes = Almacen.SelectAll();

            cbxAlmacen.Items.Clear();
            for (int i = 0; i < almacenes.Count; i++)
            {
                Almacen a = (Almacen)almacenes[i];
                cbxAlmacen.Items.Add(a.Nombre);
            }

            ArrayList entidades = Entidad.SelectAll();

            cbxEntidades.Items.Clear();
            for (int i = 0; i < entidades.Count; i++)
            {
                Entidad en = (Entidad)entidades[i];
                if (en.Direccion != "")
                {
                    cbxEntidades.Items.Add(en.Nombre + "-" + en.Direccion);
                }
                else
                {
                    cbxEntidades.Items.Add(en.Nombre);
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Devuelve un alimento dado su id
        /// </summary>
        /// <param name="Id">Id del alimento</param>
        /// <returns>El alimento</returns>
        public static Alimento Select(int Id)
        {
            Alimento a = null;

            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString))
            {
                string selectString = "select * from Alimento where Id = @id";
                using (SqlCommand selectCommand = new SqlCommand(selectString, conn))
                {
                    conn.Open();
                    selectCommand.Parameters.Add("@id", SqlDbType.Int).Value = Id;
                    SqlDataReader reader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection);
                    while (reader.Read())
                    {
                        a        = new Alimento();
                        a.Id     = reader.GetInt32(0);
                        a.Nombre = reader.GetString(1);
                        try
                        {
                            a.Imagen = Alimento.GetImagen(reader.GetInt32(2));
                        }catch (Exception e)
                        {
                            a.Imagen = null;
                        }
                        try
                        {
                            a.Familia = Familia.Select(reader.GetInt32(3));
                        }
                        catch (Exception e) { a.Familia = null; }

                        a.Descripcion = reader.GetString(4);
                    }
                }
            }
            return(a);
        }
Пример #7
0
        private void btnAnadir_Click(object sender, EventArgs e)
        {
            if (cmb_alimentos.Text == "")
            {
                MessageBox.Show(this, "El campo Alimento es obligatorio", "Rellena los campos", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            if (!cmb_alimentos.Items.Contains(cmb_alimentos.Text))
            {
                MessageBox.Show(this, "El Alimento no existe", "Eliga un Alimento ya creado", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            if (comboBoxOrigen.Text == "")
            {
                MessageBox.Show(this, "El Origen no existe", "Eliga un Origen ya creado", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            if (cbxAlmacen.Text == "")
            {
                MessageBox.Show(this, "El Almacen no existe", "Eliga un Almacen ya creado", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            if (cbxEntidades.Text == "")
            {
                MessageBox.Show(this, "El Proveedor no existe", "Eliga un Proveedor ya creado", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            float cant = float.Parse(txtCantidad.Text);

            if (cant == 0)
            {
                MessageBox.Show(this, "Seleccione una cantidad superior a 0", "Escriba una cantidad", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if (MessageBox.Show(this, "Se va" + (cant == 1 ? "" : "n") + " a registrar " + cant + " producto" + (cant == 1 ? "" : "s") + " del alimento " + cmb_alimentos.Text + ". ¿Está seguro?", "Añadir Productos", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.No)
            {
                return;
            }



            Producto p = new Producto();

            p.Alimento                = Alimento.SelectWhereNombreIs(cmb_alimentos.Text);
            p.Almacen                 = Almacen.Select(cbxAlmacen.SelectedItem.ToString());
            p.FechaEntrada            = dateTimeFechaEn.Value;
            p.FechaCaducidad          = dateTimeFechaCad.Value;
            p.FechaConsumirPreferente = dateTimeFechaPref.Value;
            p.Proveedor               = comboBoxOrigen.SelectedItem.ToString();
            try
            {
                p.Entidad = Entidad.SelectByName((cbxEntidades.SelectedItem.ToString()).Split('-')[0], (cbxEntidades.SelectedItem.ToString()).Split('-')[1]);
            }
            catch (Exception ex) {
                p.Entidad = Entidad.SelectByName((cbxEntidades.SelectedItem.ToString()), "");
            }

            p.Cantidad = cant;
            bool ok = false;

            if (txtCantidad.Text != "")
            {
                ok = p.Insert();
            }

            // Feedback!
            if (!ok)
            {
                MessageBox.Show(this, "Se ha producido un error insertando el producto", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                //MessageBox.Show(this, "Se añadido un nuevo producto correctamente.", "Producto añadido", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cmb_alimentos.Text = "";
                txtCantidad.Text   = "0";
                this.fProductos.cargarDataGridView();
            }
        }
Пример #8
0
 public Salida()
 {
     this.Id          = -1;
     this.FechaSalida = DateTime.Now;
     this.Alimento    = null;
 }