示例#1
0
        public string Insertar(DatosApertura Apertura, List <DatosDetalleApertura> Detalle, DatosAperturaCierre AperturaCierre, ref int idAperturaCierre)
        {
            string          respuesta     = "";
            MySqlConnection MySqlConexion = new MySqlConnection(); //MySQL

            try
            {
                //MySql
                MySqlConexion.ConnectionString = ConexionMySQL.cadenaConexion;
                MySqlConexion.Open();
                MySqlTransaction MySqlTransaccion = MySqlConexion.BeginTransaction();
                MySqlCommand     ComandoMySql     = new MySqlCommand();
                ComandoMySql.Connection  = MySqlConexion;
                ComandoMySql.Transaction = MySqlTransaccion;
                ComandoMySql.CommandText = "spInsertarApertura";
                ComandoMySql.CommandType = CommandType.StoredProcedure;

                MySqlParameter parametroIdApertura = new MySqlParameter();
                parametroIdApertura.ParameterName = "parIdApertura";
                parametroIdApertura.MySqlDbType   = MySqlDbType.Int32;
                parametroIdApertura.Direction     = ParameterDirection.Output;
                ComandoMySql.Parameters.Add(parametroIdApertura);

                MySqlParameter parametroIdCaja = new MySqlParameter();
                parametroIdCaja.ParameterName = "parIdCaja";
                parametroIdCaja.MySqlDbType   = MySqlDbType.Int32;
                parametroIdCaja.Value         = Apertura.IdCaja;
                ComandoMySql.Parameters.Add(parametroIdCaja);

                MySqlParameter parametroIdEmpleado = new MySqlParameter();
                parametroIdEmpleado.ParameterName = "parIdEmpleado";
                parametroIdEmpleado.MySqlDbType   = MySqlDbType.Int32;
                parametroIdEmpleado.Value         = Apertura.IdEmpleado;
                ComandoMySql.Parameters.Add(parametroIdEmpleado);

                MySqlParameter parametroFechaHora = new MySqlParameter();
                parametroFechaHora.ParameterName = "parFechaHora";
                parametroFechaHora.MySqlDbType   = MySqlDbType.DateTime;
                parametroFechaHora.Value         = Apertura.FechaHora;
                ComandoMySql.Parameters.Add(parametroFechaHora);

                MySqlParameter parametroMontoInicial = new MySqlParameter();
                parametroMontoInicial.ParameterName = "parMontoInicial";
                parametroMontoInicial.MySqlDbType   = MySqlDbType.Decimal;
                parametroMontoInicial.Value         = Apertura.MontoInicial;
                ComandoMySql.Parameters.Add(parametroMontoInicial);

                //MySqlParameter parametroAperturaPorDefecto = new MySqlParameter();
                //parametroAperturaPorDefecto.ParameterName = "parAperturaPorDefecto";
                //parametroAperturaPorDefecto.MySqlDbType = MySqlDbType.Byte;
                //parametroAperturaPorDefecto.Value = Apertura.AperturaPorDefecto;
                //ComandoMySql.Parameters.Add(parametroAperturaPorDefecto);

                respuesta = ComandoMySql.ExecuteNonQuery() == 1 ? "OK" : "Ocurrió un error al intentar ingresar el registro. Intente nuevamente.";

                if (respuesta.Equals("OK"))
                {
                    IdApertura = Convert.ToInt32(ComandoMySql.Parameters["parIdApertura"].Value);
                    foreach (DatosDetalleApertura detalle in Detalle)
                    {
                        detalle.IdApertura = IdApertura;
                        respuesta          = detalle.Insertar(detalle, ref MySqlConexion, ref MySqlTransaccion);
                        if (!respuesta.Equals("OK"))
                        {
                            break;
                        }
                    }
                    if (respuesta.Equals("OK"))
                    {
                        //DatosAperturaCierre AperturaCierre = new DatosAperturaCierre();
                        AperturaCierre.IdApertura = IdApertura;
                        respuesta = AperturaCierre.Insertar(AperturaCierre, ref idAperturaCierre, ref MySqlConexion, ref MySqlTransaccion);
                    }
                }

                if (respuesta.Equals("OK"))
                {
                    DatosCaja Caja = new DatosCaja();
                    Caja.IdCaja = IdCaja;
                    respuesta   = Caja.Abrir(Caja, ref MySqlConexion, ref MySqlTransaccion);
                }

                if (respuesta.Equals("OK"))
                {
                    MySqlTransaccion.Commit();
                }
                else
                {
                    MySqlTransaccion.Rollback();
                }
            }
            catch (Exception ex)
            {
                respuesta = ex.Message;
            }
            finally
            {
                if (MySqlConexion.State == ConnectionState.Open)
                {
                    MySqlConexion.Close();
                }
            }
            return(respuesta);
        }