示例#1
0
        public bool registrarDocumento(Documento nuevoDocumento)
        {
            daoCliente aux = new daoCliente();

            if (!aux.clienteExiste(nuevoDocumento.Comprador.Rut))
            {
                aux.registrarCliente(nuevoDocumento.Comprador);
            }
            try
            {
                ConexionBD conexion = new ConexionBD();

                conexion.abrirConexion();

                string sqlQueryDocumento = "INSERT INTO documento (folio, estado, subtotal, iva, total, tipoPago, cliente_id, empresa_id, tipoDocumento_id, observacion, creadoPor) " +
                                           "values(@folio, @estado, @subtotal, @iva, @total, @tipoPago, @cliente_id, @empresa_id, @tipoDocumento_id, @observacion, @creadoPor)";

                SqlCommand commandSql = new SqlCommand(sqlQueryDocumento, conexion.Conexion);


                commandSql.Parameters.AddWithValue("@folio", SqlDbType.VarChar);
                commandSql.Parameters["@folio"].Value = nuevoDocumento.Folio;

                commandSql.Parameters.AddWithValue("@estado", SqlDbType.SmallInt);
                commandSql.Parameters["@estado"].Value = nuevoDocumento.EstadoEmitido;

                commandSql.Parameters.AddWithValue("@subtotal", SqlDbType.Int);
                commandSql.Parameters["@subtotal"].Value = nuevoDocumento.SubTotal;

                commandSql.Parameters.AddWithValue("@iva", SqlDbType.Int);
                commandSql.Parameters["@iva"].Value = nuevoDocumento.Iva;

                commandSql.Parameters.AddWithValue("@total", SqlDbType.Int);
                commandSql.Parameters["@total"].Value = nuevoDocumento.Total;

                commandSql.Parameters.AddWithValue("@tipoPago", SqlDbType.SmallInt);
                commandSql.Parameters["@tipoPago"].Value = nuevoDocumento.TipoPago;

                commandSql.Parameters.AddWithValue("@observacion", SqlDbType.VarChar);
                commandSql.Parameters["@observacion"].Value = "Creación de documento";

                commandSql.Parameters.AddWithValue("@creadoPor", SqlDbType.VarChar);
                commandSql.Parameters["@creadoPor"].Value = nuevoDocumento.CreadoPor;

                //ID de relacionados al documento
                commandSql.Parameters.AddWithValue("@cliente_id", SqlDbType.Int);
                commandSql.Parameters["@cliente_id"].Value = aux.getIdCliente(nuevoDocumento.Comprador.Rut);

                commandSql.Parameters.AddWithValue("@empresa_id", SqlDbType.Int);
                commandSql.Parameters["@empresa_id"].Value = nuevoDocumento.Vendedor.IdEmpresa;

                commandSql.Parameters.AddWithValue("@tipoDocumento_id", SqlDbType.Int);
                commandSql.Parameters["@tipoDocumento_id"].Value = nuevoDocumento.TipoDoc.IdTDocumento;


                //Ejecución de query
                int filasAfectadas = commandSql.ExecuteNonQuery();

                conexion.cerrarConexion();

                if (filasAfectadas > 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }