/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.ddns.grupotsergio;

import java.sql.*;

/**
 *
 * @author Edson
 */
public class cttosGS {
    public int chave = 0;
    public int chcli = 0;
    public String cttonr = null;
    public String dtini = null;
    public String dtfin = null;
    public String ativo = null;
    public String valor = null;
    public String auditoria = null;
    public int filiais_idfiliais = 0;
    public int empresa_idempresa = 0;

    private static PreparedStatement stm = null;
    private static ResultSet rs = null;
    private static Statement stmt;

    public cttosGS(){
    }

    public String getAtivo() {
        return ativo;
    }

    public void setAtivo(String ativo) {
        this.ativo = ativo;
    }

    public String getAuditoria() {
        return auditoria;
    }

    public void setAuditoria(String auditoria) {
        this.auditoria = auditoria;
    }

    public int getChave() {
        return chave;
    }

    public void setChave(int chave) {
        this.chave = chave;
    }

    public int getChcli() {
        return chcli;
    }

    public void setChcli(int chcli) {
        this.chcli = chcli;
    }

    public String getCttonr() {
        return cttonr;
    }

    public void setCttonr(String cttonr) {
        this.cttonr = cttonr;
    }

    public String getDtfin() {
        return dtfin;
    }

    public void setDtfin(String dtfin) {
        this.dtfin = dtfin;
    }

    public String getDtini() {
        return dtini;
    }

    public void setDtini(String dtini) {
        this.dtini = dtini;
    }

    public int getFiliais_idfiliais() {
        return filiais_idfiliais;
    }

    public void setFiliais_idfiliais(int filiais_idfiliais) {
        this.filiais_idfiliais = filiais_idfiliais;
    }

    public String getValor() {
        return valor;
    }

    public void setValor(String valor) {
        this.valor = valor;
    }

    public int getEmpresa_idempresa() {
        return empresa_idempresa;
    }

    public void setEmpresa_idempresa(int empresa_idempresa) {
        this.empresa_idempresa = empresa_idempresa;
    }

    public static boolean inserirCttos (cttosGS cttos){
        try{
            stm = Conexao.conectar().prepareStatement("insert into cttos(idcttos,"
                    + "idclifor, cttonr, dtini, dtfin, ativo, valor,"
                    + "auditoria, filiais_idfiliais, empresa_idempresa) "
                    + "values (?,?,?,?,?,?,?,?,?,?)");
            stm.setInt(1, cttos.getChave());
            stm.setInt(2, cttos.getChcli());
            stm.setString(3, cttos.getCttonr());
            stm.setString(4, cttos.getDtini());
            stm.setString(5, cttos.getDtfin());
            stm.setString(6, cttos.getAtivo());
            stm.setString(7, cttos.getValor());
            stm.setString(8, cttos.getAuditoria());
            stm.setInt(9, cttos.getFiliais_idfiliais());
            stm.setInt(10, cttos.getEmpresa_idempresa());
            stm.executeUpdate();
            stm.close();
            return true;
        }catch(Exception e){
            System.out.println("Erro ao inserir");
            return false;
        }
    }

    public static boolean alterarCttos (cttosGS cttos){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update cttos set "
                    + "idclifor=?, cttonr=?, dtini=?, dtfin=?, ativo=?, "
                    + "valor=?, auditoria=?, "
                    + "empresa_idempresa= ? "
                    + "where idcttos = ? "
                    + "and filiais_idfiliais = ?");
            stm.setInt(1, cttos.getChcli());
            stm.setString(2, cttos.getCttonr());
            stm.setString(3, cttos.getDtini());
            stm.setString(4, cttos.getDtfin());
            stm.setString(5, cttos.getAtivo());
            stm.setString(6, cttos.getValor());
            stm.setString(7, cttos.getAuditoria());
            stm.setInt(8, cttos.getEmpresa_idempresa());
            stm.setInt(9, cttos.getChave());
            stm.setInt(10, cttos.getFiliais_idfiliais());
            int executeUpdate = stm.executeUpdate();

            if(executeUpdate > 0)
                testa = true;
            else
                testa = false;
        }catch(Exception e){
            System.out.println("Erro ao alterar");
        }
        return testa;
    }

    public static boolean excluiCttos (cttosGS cttos){
        boolean testa = false;
        try {
            stm = Conexao.conectar().prepareStatement("delete from cttos "
                    + "where idcttos = ? "
                    + "and filiais_idfiliais = ?");
            stm.setInt(1, cttos.getChave());
            stm.setInt(2, cttos.getFiliais_idfiliais());
            int executeUpdate = stm.executeUpdate();

            if(executeUpdate > 0)
                testa = true;
            else
                testa = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return testa;
    }

    public void selCttos(String Id, String Idf) {
        try {
            cttosGS scid = null;
            stmt = Conexao.conectar().createStatement();
            rs = stmt.executeQuery("Select c.*, nome From cttos c, clifor o "
            + "Where c.idclifor=o.idclifor and idcttos ='"+Id+"' "
            + "and filiais_idfiliais = '"+Idf+"'");
            if (rs.next()) {
                scid = new cttosGS();
                scid.setChave(rs.getInt("chave"));
                scid.setChcli(rs.getInt("idclifor"));
                scid.setCttonr(rs.getString("cttonr"));
                scid.setDtini(rs.getString("dtini"));
                scid.setDtfin(rs.getString("dtfin"));
                scid.setAtivo(rs.getString("ativo"));
                scid.setValor(rs.getString("valor"));
                scid.setAuditoria(rs.getString("auditoria"));
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getAutoInc(String Idf) {
        int Codigo = 0;
        try {
            PreparedStatement pstmt = Conexao.conectar().prepareStatement(
                    "Select max(idcttos) From cttos "
                    + "where filiais_idfiliais = '"+Idf+"'");
            ResultSet rss = pstmt.executeQuery();
            rss.next();
            Codigo = rss.getInt(1) + 1;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return Codigo;
    }

    public void listaCttos(String Id){
        try{
            stmt = Conexao.conectar().createStatement();
            rs = stmt.executeQuery("select c.*, nome from cttos c, clifor o "
            + "where c.idclifor=o.idclifor "
            + "and c.empresa_idempresa=o.empresa_idempresa "
            + "and filiais_idfiliais = '"+Id+"' "
            + "order by nome, cttonr");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void listaCttoCli(String Id, String If){
        try{
            stmt = Conexao.conectar().createStatement();
            rs = stmt.executeQuery("select * from cttos "
            + "where filiais_idfiliais = '"+Id+"' "
            + "and idclifor = '"+If+"' order by cttonr");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public ResultSet getResultado(){
        return rs;
    }

}
