示例#1
0
        public static void llenarDatosProduccion(Produccion produccion, List <Producto> actualizaStock)
        {
            bool correcto = false;

            //Primero los item de produccion
            // Proporciona la cadena de conexion a base de datos desde el archivo de configuracion
            string connectionString = ConfigurationManager.ConnectionStrings["TiendaConString"].ConnectionString;

            // Crear y abrir la conexión en un bloque using.
            // Esto asegura que todos los recursos serán cerrados y dispuestos cuando el código sale
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                // Crear el objeto Command.
                SqlCommand command = new SqlCommand("InsertarProducion", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("fecha", produccion.FechaProd);
                command.Parameters.AddWithValue("idEmpleado", produccion.IdEmpleado);

                //rejistrar el  parametro para salida
                command.Parameters.AddWithValue("idProduccion", 0);

                command.Parameters["idProduccion"].Direction = ParameterDirection.Output;

                try
                {
                    connection.Open();
                    //transacciones
                    SqlTransaction trato = connection.BeginTransaction();
                    command.Transaction = trato;

                    int fila = command.ExecuteNonQuery();

                    //verifica si se ingreso correctamente al cliente y luego ingresar los telefonos
                    if (fila != 0)
                    {
                        int idProduccion = Convert.ToInt32(command.Parameters["idProduccion"].Value);

                        foreach (DetalleProduccion item in produccion.getListaDetalle())
                        {
                            SqlCommand commandDetalle = new SqlCommand("InsertarDetalleProduccion", connection);
                            commandDetalle.CommandType = CommandType.StoredProcedure;

                            commandDetalle.Parameters.AddWithValue("cantidad", item.Cantidad);
                            commandDetalle.Parameters.AddWithValue("idPlato", item.Plato.Id);
                            commandDetalle.Parameters.AddWithValue("idProduccion", idProduccion);

                            commandDetalle.Transaction = trato;
                            fila = commandDetalle.ExecuteNonQuery();

                            if (fila != 0)
                            {
                                foreach (Ingrediente usado in item.Plato.getIngredientes())
                                {
                                    SqlCommand commandUsado = new SqlCommand("ActualizarCantidadProducto", connection);
                                    commandUsado.CommandType = CommandType.StoredProcedure;

                                    commandUsado.Parameters.AddWithValue("cantidad", usado.Cantidad);
                                    commandUsado.Parameters.AddWithValue("idProducto", usado.IdProducto);

                                    commandUsado.Transaction = trato;
                                    fila = commandUsado.ExecuteNonQuery();

                                    if (fila != 0)
                                    {
                                        correcto = true;
                                    }
                                    else
                                    {
                                        correcto = false;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                correcto = false;
                                break;
                            }
                        }
                    }

                    if (correcto)
                    {
                        trato.Commit();
                    }
                    else
                    {
                        trato.Rollback();
                    }
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
示例#2
0
        public static void llenarDatosProduccion(Produccion produccion, List<Producto> actualizaStock)
        {
            bool correcto = false;

            //Primero los item de produccion
            // Proporciona la cadena de conexion a base de datos desde el archivo de configuracion
            string connectionString = ConfigurationManager.ConnectionStrings["TiendaConString"].ConnectionString;

            // Crear y abrir la conexión en un bloque using.
            // Esto asegura que todos los recursos serán cerrados y dispuestos cuando el código sale
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                // Crear el objeto Command.
                SqlCommand command = new SqlCommand("InsertarProducion", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("fecha", produccion.FechaProd);
                command.Parameters.AddWithValue("idEmpleado", produccion.IdEmpleado);

                //rejistrar el  parametro para salida
                command.Parameters.AddWithValue("idProduccion", 0);

                command.Parameters["idProduccion"].Direction = ParameterDirection.Output;

                try
                {
                    connection.Open();
                    //transacciones
                    SqlTransaction trato = connection.BeginTransaction();
                    command.Transaction = trato;

                    int fila = command.ExecuteNonQuery();

                    //verifica si se ingreso correctamente al cliente y luego ingresar los telefonos
                    if (fila != 0)
                    {
                        int idProduccion = Convert.ToInt32(command.Parameters["idProduccion"].Value);

                        foreach (DetalleProduccion item in produccion.getListaDetalle())
                        {
                            SqlCommand commandDetalle = new SqlCommand("InsertarDetalleProduccion", connection);
                            commandDetalle.CommandType = CommandType.StoredProcedure;

                            commandDetalle.Parameters.AddWithValue("cantidad", item.Cantidad);
                            commandDetalle.Parameters.AddWithValue("idPlato", item.Plato.Id);
                            commandDetalle.Parameters.AddWithValue("idProduccion", idProduccion);

                            commandDetalle.Transaction = trato;
                            fila = commandDetalle.ExecuteNonQuery();

                            if (fila != 0)
                            {
                                foreach (Ingrediente usado in item.Plato.getIngredientes())
                                {
                                    SqlCommand commandUsado = new SqlCommand("ActualizarCantidadProducto", connection);
                                    commandUsado.CommandType = CommandType.StoredProcedure;

                                    commandUsado.Parameters.AddWithValue("cantidad", usado.Cantidad);
                                    commandUsado.Parameters.AddWithValue("idProducto", usado.IdProducto);

                                    commandUsado.Transaction = trato;
                                    fila = commandUsado.ExecuteNonQuery();

                                    if (fila != 0)
                                    {
                                        correcto = true;
                                    }
                                    else
                                    {
                                        correcto = false;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                correcto = false;
                                break;
                            }
                        }
                    }

                    if (correcto)
                    {
                        trato.Commit();
                    }
                    else
                    {
                        trato.Rollback();
                    }
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }