Esempio n. 1
0
        public Emprestimo read(string uidEmprestimo)
        {
            Emprestimo outEmprestimo;
            DataTable listaBrutaEmprestimo = new DataTable();

            SqlDataAdapter telefone = new SqlDataAdapter();
            SqlCommand pedido = new SqlCommand();
            SqlConnection ConexaoBD = new SqlConnection(_ligacao);

            pedido.CommandType = CommandType.Text;
            string comandoParaSQL = "SELECT* FROM emp WHERE uidEmprestimo = @uidEmprestimo";
            pedido.CommandText = comandoParaSQL;
            pedido.Parameters.AddWithValue("@uidEmprestimo", uidEmprestimo);
            telefone.SelectCommand = pedido;
            telefone.SelectCommand.Connection = ConexaoBD;
            ConexaoBD.Open();
            telefone.Fill(listaBrutaEmprestimo);
            ConexaoBD.Close();

            //Se existir, só pode haver uma fatura com o uid indicado
            if (listaBrutaEmprestimo.Rows.Count == 1)
            {
                //Criar um objecto Fatura a partir do registo da tabela
                DataRow linha = listaBrutaEmprestimo.Rows[0];
                outEmprestimo = new Emprestimo(linha["uidEmprestimo"].ToString());
                //Ler o Cliente completo dessa fatura
                Cliente_Helper_CRUD ch = new Cliente_Helper_CRUD(_ligacao);
                outEmprestimo.ClienteEmp = ch.read(linha["uidCliente"].ToString());
                Funcionario_Helper_CRUD fhc = new Funcionario_Helper_CRUD(_ligacao);
                outEmprestimo.FuncionarioEmp = fhc.read(linha["uidFuncionario"].ToString());
                Livro_Helper_CRUD lhc = new Livro_Helper_CRUD(_ligacao);
                outEmprestimo.LivroEmp = lhc.read(linha["uidLivro"].ToString());
                //E preencher o resto da informação
                outEmprestimo.Data = linha["dtRegisto"].ToString();
                outEmprestimo.DataEmp = Convert.ToDateTime(linha["dtEmprestimo"]);
                outEmprestimo.Estado = linha["estado"].ToString();
            }
            else
            {
                outEmprestimo = null;
            }
            return outEmprestimo;
        }
Esempio n. 2
0
        public List<Emprestimo> list(Cliente c, Livro l, Funcionario f, DateTime dt, int estado)
        {
            List<Emprestimo> listaADevolver = new List<Emprestimo>();
            DataTable listaBrutaEmp = new DataTable();

            SqlDataAdapter telefone = new SqlDataAdapter();
            SqlCommand pedido = new SqlCommand();
            SqlConnection numero = new SqlConnection(_ligacao);

            string est;

            string param1 = "estado = @estado3 ORDER BY dtEmprestimo DESC";
            string param2 = "estado = @estado1 OR estado = @estado2 ORDER BY dtEmprestimo DESC";

            if (estado == Convert.ToInt32(1))
            {
                est = param1;
            }
            else
            {
                est = param2;
            }

            

            #region: "Queries"
            pedido.CommandType = CommandType.Text;
            string pedidoParaSQL = "SELECT * FROM emp WHERE " + est ;

            if (c == null && l == null && f == null && dt == DateTime.Today.AddDays(1))
            {
                pedido.CommandText = pedidoParaSQL;
            }
            else if (dt != DateTime.Today.AddDays(1) && c != null)
            {
                pedidoParaSQL = "SELECT * FROM emp WHERE " +
                                "uidCliente = @uidCliente AND " +
                                "dtEmprestimo = @dtEmp AND " +
                                est;
                pedido.CommandText = pedidoParaSQL;
                pedido.Parameters.AddWithValue("@uidCliente", c.GuidPessoa);
                pedido.Parameters.AddWithValue("@dtEmp", dt);
            }
            else if (dt != DateTime.Today.AddDays(1) && f != null)
            {
                pedidoParaSQL = "SELECT * FROM emp WHERE " +
                                "uidFuncionario = @uidFuncionario AND " +
                                "dtEmprestimo = @dtEmp AND " +
                                est;
                pedido.CommandText = pedidoParaSQL;
                pedido.Parameters.AddWithValue("@uidFuncionario", f.GuidPessoa);
                pedido.Parameters.AddWithValue("@dtEmp", dt);
            }
            else if (dt != DateTime.Today.AddDays(1))
            {
                pedidoParaSQL = "SELECT * FROM emp WHERE " +
                                "dtEmprestimo = @dtEmp AND " +
                                est;
                pedido.CommandText = pedidoParaSQL;
                pedido.Parameters.AddWithValue("@dtEmp", dt);
            }
            else if (c != null && f != null)
            {
                pedidoParaSQL = "SELECT * FROM emp WHERE " +
                                "uidCliente = @uidCliente AND " +
                                "uidFuncionario = @uidFuncionario AND " +
                                est;
                pedido.CommandText = pedidoParaSQL;
                pedido.Parameters.AddWithValue("@uidCliente", c.GuidPessoa);
                pedido.Parameters.AddWithValue("@uidFuncionario", f.GuidPessoa);
            }
            else if (c != null)
            {
                pedidoParaSQL = "SELECT * FROM emp WHERE " +
                                "uidCliente = @uidCliente AND " +
                                est;
                pedido.CommandText = pedidoParaSQL;
                pedido.Parameters.AddWithValue("@uidCliente", c.GuidPessoa);
            }
            else if (l != null)
            {
                pedidoParaSQL = "SELECT * FROM emp WHERE " +
                                "uidLivro = @uidLivro AND " +
                                est;
                pedido.CommandText = pedidoParaSQL;
                pedido.Parameters.AddWithValue("@uidLivro", l.GuidLivro);
            }
            else if (f != null)
            {
                pedidoParaSQL = "SELECT * FROM emp WHERE " +
                                "uidFuncionario = @uidFuncionario AND " +
                                est;
                pedido.CommandText = pedidoParaSQL;
                pedido.Parameters.AddWithValue("@uidFuncionario", f.GuidPessoa);
            }
            #endregion

            pedido.Parameters.AddWithValue("@estado1", "Em dia.");
            pedido.Parameters.AddWithValue("@estado2", "Atrasado.");
            pedido.Parameters.AddWithValue("@estado3", "Devolvido.");
            telefone.SelectCommand = pedido;
            telefone.SelectCommand.Connection = numero;
            numero.Open();
            telefone.Fill(listaBrutaEmp);
            numero.Close();

            foreach (DataRow linha in listaBrutaEmp.Rows)
            {
                Emprestimo emp = new Emprestimo(linha["uidEmprestimo"].ToString());
                //Ler Clientes, Livros e Funcionarios do emprestimo
                Cliente_Helper_CRUD chc = new Cliente_Helper_CRUD(_ligacao);
                emp.ClienteEmp = chc.read(linha["uidCliente"].ToString());
                Funcionario_Helper_CRUD fhc = new Funcionario_Helper_CRUD(_ligacao);
                emp.FuncionarioEmp = fhc.read(linha["uidFuncionario"].ToString());
                Livro_Helper_CRUD lhc = new Livro_Helper_CRUD(_ligacao);
                emp.LivroEmp = lhc.read(linha["uidLivro"].ToString());
                //Resto da informacao
                emp.Data = linha["dtRegisto"].ToString();
                emp.DataEmp = Convert.ToDateTime(linha["dtEmprestimo"]);
                if (estado == Convert.ToInt32(1))
                {
                    emp.DataDevolucao = Convert.ToDateTime(linha["dtDevolucao"]);
                }
                
                emp.Estado = linha["estado"].ToString();
                //Adiciona a lista de saida
                listaADevolver.Add(emp);
            }
            return listaADevolver;
        }