package labsis.financeiro.servlets;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import labsis.financeiro.dao.DaoCheques;
import labsis.financeiro.dao.DaoContas;
import labsis.financeiro.dao.DaoFluxo;
import labsis.financeiro.dao.DaoFornecedor;
import labsis.financeiro.model.Cheques;
import labsis.financeiro.model.ContasAPagar;
import labsis.financeiro.model.ContasAReceber;
import labsis.financeiro.model.FluxoDeCaixa;
import labsis.financeiro.model.Fornecedor;
import labsis.vendas.dao.DaoCliente;
import labsis.vendas.model.Cliente;
/**
* Servlet implementation class Controller
*/
public class Controller extends HttpServlet {
private static Connection conn = null;
private ServletContext sc;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
sc = config.getServletContext();
try{
Class.forName(sc.getInitParameter("driver"));
conn = DriverManager.getConnection(sc.getInitParameter("url"), sc.getInitParameter("usuario"), sc.getInitParameter("senha"));
} catch(Exception e){
e.printStackTrace();
}
}
public static Connection conectar(){
return conn;
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String comm = request.getParameter("comm");
HttpSession session = request.getSession();
RequestDispatcher rd = null;
if (request.getParameter("comm") == null
|| request.getParameter("comm").equals("")) {
rd = request.getRequestDispatcher("/usuario/jsp/login.jsp");
rd.forward(request, response);
}
else if(comm.equals("pagcadastrarfornecedor")){
rd = request.getRequestDispatcher("/financeiro/jsp/cadastrarfornecedores.jsp");
rd.forward(request, response);
}
else if(comm.equals("cadastrarfornecedor")){
String s1 = request.getParameter("cnpj");
String s2 = request.getParameter("nomefornecedor");
String s3 = request.getParameter("enderecofornecedor");
String s4 = request.getParameter("telefonefornecedor");
String s5 = request.getParameter("emailfornecedor");
String s6 = request.getParameter("representante");
Fornecedor f = new Fornecedor();
f.setCnpj(s1);
f.setNomefornecedor(s2);
f.setEnderecofornecedor(s3);
f.setTelefonefornecedor(s4);
f.setEmailfornecedor(s5);
f.setNomeDoContato(s6);
try {
DaoFornecedor f2 = new DaoFornecedor();
f2.cadastrarfornecedores(f);
} catch (SQLException e) {
e.printStackTrace();
}
session.setAttribute("TABELA", "insereFornecedor");
rd = request.getRequestDispatcher("/financeiro/jsp/responsavel.jsp");
rd.forward(request, response);
}
else if(comm.equals("alteraFornecedor")){
String cnpj = request.getParameter("cnpjFornecedor");
String nome = request.getParameter("novoNome");
String end = request.getParameter("novoEnd");
String tel = request.getParameter("novoTel");
String email = request.getParameter("novoEmail");
String contato = request.getParameter("novoContato");
try {
DaoFornecedor f = new DaoFornecedor();
f.alteraFornecedor(cnpj, nome, end, tel, email, contato);
session.setAttribute("TABELA", "alteraFornecedor");
rd = request.getRequestDispatcher("/financeiro/jsp/responsavel.jsp");
rd.forward(request, response);
} catch(SQLException e){
e.printStackTrace();
}
}
else if(comm.equals("paglistarfornecedor")){
rd = request.getRequestDispatcher("/financeiro/jsp/listarfornecedores.jsp");
rd.forward(request, response);
}
else if(comm.equals("listarfornecedor")){
try {
DaoFornecedor f2 = new DaoFornecedor();
ArrayList<Fornecedor> lista = f2.listarfornecedores();
session.setAttribute("listar", lista);
rd = request.getRequestDispatcher("/financeiro/jsp/listarfornecedores.jsp");
rd.forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
else if(comm.equals("pesquisaClientes")){
String s1 = request.getParameter("cpf");
String s2 = request.getParameter("nome");
try {
DaoCliente c = new DaoCliente();
ArrayList<Cliente> lista = c.pesquisaCliente(s1, s2);
session.setAttribute("pesquisaClientes", lista);
rd = request.getRequestDispatcher("/financeiro/jsp/alteraCredito.jsp");
rd.forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(comm.equals("pagalterarCredito")){
rd = request.getRequestDispatcher("/financeiro/jsp/alteraCredito.jsp");
rd.forward(request, response);
}
else if(comm.equals("alteraCreditoCliente")){
float cred = Float.parseFloat(request.getParameter("novoCred"));
String cpf = request.getParameter("cpfCliente");
try {
DaoCliente c = new DaoCliente();
c.alteraCredito(cred, cpf);
session.setAttribute("TABELA", "alteraCredito");
rd = request.getRequestDispatcher("/financeiro/jsp/responsavel.jsp");
rd.forward(request, response);
} catch (SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(comm.equals("pagexcluirfornecedor")){
rd = request.getRequestDispatcher("/financeiro/jsp/excluirfornecedores.jsp");
}
else if(comm.equals("excluirfornecedor")){
String s1 = request.getParameter("cnpjf");
String s2 = request.getParameter("nomefornecedor");
try {
DaoFornecedor f2 = new DaoFornecedor();
boolean b = false;
if(s2.equals("")){
long l = Long.parseLong(s1);
b = f2.excluirfornecedoresc(l);
if(b){
session.setAttribute("TABELA", "excluiFornecedor");
rd = request.getRequestDispatcher("/financeiro/jsp/responsavel.jsp");
rd.forward(request, response);
}
else {
session.setAttribute("TABELA", "erexcluiFornecedor");
rd = request.getRequestDispatcher("/financeiro/jsp/responsavel.jsp");
rd.forward(request, response);
}
}
else{
b = f2.excluirfornecedoresn(s2);
if(b){
session.setAttribute("TABELA", "excluiFornecedor");
rd = request.getRequestDispatcher("/financeiro/jsp/responsavel.jsp");
rd.forward(request, response);
}
else {
session.setAttribute("TABELA", "erexcluiFornecedor");
rd = request.getRequestDispatcher("/financeiro/jsp/responsavel.jsp");
rd.forward(request, response);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
else if(comm.equals("paglistarContas")){
rd = request.getRequestDispatcher("/financeiro/jsp/listaContas.jsp");
rd.forward(request, response);
}
else if(comm.equals("listaContas")){
try {
if(request.getParameter("opcao").equals("Pagar")){
DaoContas d2 = new DaoContas();
ArrayList<ContasAPagar> lista = d2.listarContasAPagar();
session.setAttribute("listar", lista);
rd = request.getRequestDispatcher("/financeiro/jsp/listaContas.jsp");
rd.forward(request, response);
}
else {
DaoContas d2 = new DaoContas();
ArrayList<ContasAReceber> lista = d2.listarContasAReceber();
session.setAttribute("listar", lista);
rd = request.getRequestDispatcher("/financeiro/jsp/listaContas.jsp");
rd.forward(request, response);
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
else if(comm.equals("pagcadastrarCheque")){
rd = request.getRequestDispatcher("/financeiro/jsp/cadastrarCheques.jsp");
rd.forward(request, response);
}
else if(comm.equals("cadastrarCheque")){
String clienteCpf = request.getParameter("cpf");
String s = request.getParameter("data");
int dia, mes, ano;
dia = Integer.parseInt(s.substring(0, 2));
mes = Integer.parseInt(s.substring(3, 5));
ano = Integer.parseInt(s.substring(6, 10));
java.sql.Date dataParaDesconto = new java.sql.Date(ano-1900,mes-1,22);
float valor = Float.parseFloat(request.getParameter("valor"));
String banco = request.getParameter("banco");
String agencia = request.getParameter("agencia");
int numeroDaConta = Integer.parseInt(request.getParameter("numerodaconta"));
Cheques ch = new Cheques();
ch.setClientecpf(clienteCpf);
ch.setDataParaDesconto(dataParaDesconto);
ch.setValor(valor);
ch.setBanco(banco);
ch.setAgencia(agencia);
ch.setNumConta(numeroDaConta);
try {
DaoCheques dc = new DaoCheques();
dc.cadastrarCheque(ch);
session.setAttribute("TABELA", "cadastrarCheque");
rd = request.getRequestDispatcher("/financeiro/jsp/responsavel.jsp");
rd.forward(request, response);
}
catch (SQLException e) {
e.printStackTrace();
}
}
else if(comm.equals("paglistarChequeCliente")){
rd = request.getRequestDispatcher("/financeiro/jsp/listarCheques.jsp");
rd.forward(request, response);
}
else if(comm.equals("listarChequeCliente")){
try {
DaoCheques dc = new DaoCheques();
ArrayList<Cheques> lista = dc.listarCheques(request.getParameter("cpf"));
session.setAttribute("listar", lista);
rd = request.getRequestDispatcher("/financeiro/jsp/listarCheques.jsp");
rd.forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
else if(comm.equals("pagexcluirCheque")){
rd = request.getRequestDispatcher("/financeiro/jsp/excluirCheque.jsp");
rd.forward(request, response);
}
else if(comm.equals("excluirCheque")){
try{
DaoCheques dc = new DaoCheques();
dc.excluirCheques(Integer.parseInt(request.getParameter("codCheque")));
session.setAttribute("TABELA", "excluirCheque");
rd = request.getRequestDispatcher("/financeiro/jsp/responsavel.jsp");
rd.forward(request, response);
}
catch (SQLException e) {
e.printStackTrace();
}
}
else if(comm.equals("paglistarfluxo")){
rd = request.getRequestDispatcher("/financeiro/jsp/FluxoDeCaixa.jsp");
rd.forward(request, response);
}
else if(comm.equals("listarfluxo")){
try {
DaoFluxo f = new DaoFluxo();
ArrayList<FluxoDeCaixa> fluxo = f.listarFluxo();
session.setAttribute("listarFluxo", fluxo);
rd = request.getRequestDispatcher("/financeiro/jsp/FluxoDeCaixa");
rd.forward(request, response);
} catch (SQLException e){
e.printStackTrace();
}
}
}
}