/*
 * 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 reldespesaGS {
    int chave = 0;
    int idctto = 0;
    String datarel = null;
    String reembolsar = null;
    String formareelb = null;
    String dtsaldoant = null;
    String saldoant = null;
    String debito = null;
    String credito = null;
    String saldo = null;
    String reelbolso = null;
    String ffixo = null;
    String auditoria = null;
    int filiais_idfiliais = 0;
    int idusuario = 0;

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

    public reldespesaGS(){
    }

    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 String getCredito() {
        return credito;
    }

    public void setCredito(String credito) {
        this.credito = credito;
    }

    public String getDatarel() {
        return datarel;
    }

    public void setDatarel(String datarel) {
        this.datarel = datarel;
    }

    public String getDebito() {
        return debito;
    }

    public void setDebito(String debito) {
        this.debito = debito;
    }

    public String getDtsaldoant() {
        return dtsaldoant;
    }

    public void setDtsaldoant(String dtsaldoant) {
        this.dtsaldoant = dtsaldoant;
    }

    public String getFfixo() {
        return ffixo;
    }

    public void setFfixo(String ffixo) {
        this.ffixo = ffixo;
    }

    public int getFiliais_idfiliais() {
        return filiais_idfiliais;
    }

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

    public String getFormareelb() {
        return formareelb;
    }

    public void setFormareelb(String formareelb) {
        this.formareelb = formareelb;
    }

    public int getIdctto() {
        return idctto;
    }

    public void setIdctto(int idctto) {
        this.idctto = idctto;
    }

    public int getIdusuario() {
        return idusuario;
    }

    public void setIdusuario(int idusuario) {
        this.idusuario = idusuario;
    }

    public String getReelbolso() {
        return reelbolso;
    }

    public void setReelbolso(String reelbolso) {
        this.reelbolso = reelbolso;
    }

    public String getReembolsar() {
        return reembolsar;
    }

    public void setReembolsar(String reembolsar) {
        this.reembolsar = reembolsar;
    }

    public String getSaldo() {
        return saldo;
    }

    public void setSaldo(String saldo) {
        this.saldo = saldo;
    }

    public String getSaldoant() {
        return saldoant;
    }

    public void setSaldoant(String saldoant) {
        this.saldoant = saldoant;
    }

    public static Statement getStmt() {
        return stmt;
    }

    public static void setStmt(Statement stmt) {
        reldespesaGS.stmt = stmt;
    }

    public static boolean inserirReldesp (reldespesaGS produto){
        try{
            stm = Conexao.conectar().prepareStatement("insert into reldespesa(idreldespesa,"
                    + "idctto,datarel,reembolsar,formareelb,dtsaldoant,"
                    + "saldoant,debito,credito,saldo,reelbolso, "
                    + "ffixo,auditoria,filiais_idfiliais,idusuario) values ("
                    + "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            stm.setInt(1, produto.getChave());
            stm.setInt(2, produto.getIdctto());
            stm.setString(3, produto.getDatarel());
            stm.setString(4, produto.getReembolsar());
            stm.setString(5, produto.getFormareelb());
            stm.setString(6, produto.getDtsaldoant());
            stm.setString(7, produto.getSaldoant());
            stm.setString(8, produto.getDebito());
            stm.setString(9, produto.getCredito());
            stm.setString(10, produto.getSaldo());
            stm.setString(11, produto.getReelbolso());
            stm.setString(12, produto.getFfixo());
            stm.setString(13, produto.getAuditoria());
            stm.setInt(14, produto.getFiliais_idfiliais());
            stm.setInt(15, produto.getIdusuario());
            stm.executeUpdate();
            stm.close();
            return true;
        }catch(Exception e){
            System.out.println("Erro ao inserir");
            return false;
        }
    }

    public static boolean altRDidctto (reldespesaGS produto){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update reldespesa set "
                    + "idctto=? "
                    + "where idreldespesa=? "
                    + "and filiais_idfiliais=?");
            stm.setInt(1, produto.getIdctto());
            stm.setInt(2, produto.getChave());
            stm.setInt(3, produto.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 altRDdtrel (reldespesaGS produto){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update reldespesa set "
                    + "datarel=? "
                    + "where idreldespesa=? "
                    + "and filiais_idfiliais=?");
            stm.setString(1, produto.getDatarel());
            stm.setInt(2, produto.getChave());
            stm.setInt(3, produto.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 altRDreembsar (reldespesaGS produto){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update reldespesa set "
                    + "reembolsar=? "
                    + "where idreldespesa=? "
                    + "and filiais_idfiliais=?");
            stm.setString(1, produto.getReembolsar());
            stm.setInt(2, produto.getChave());
            stm.setInt(3, produto.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 altRDforeem (reldespesaGS produto){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update reldespesa set "
                    + "formareelb=? "
                    + "where idreldespesa=? "
                    + "and filiais_idfiliais=?");
            stm.setString(1, produto.getFormareelb());
            stm.setInt(2, produto.getChave());
            stm.setInt(3, produto.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 altRDdtsant (reldespesaGS produto){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update reldespesa set "
                    + "dtsaldoant=? "
                    + "where idreldespesa=? "
                    + "and filiais_idfiliais=?");
            stm.setString(1, produto.getDtsaldoant());
            stm.setInt(2, produto.getChave());
            stm.setInt(3, produto.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 altRDsalant (reldespesaGS produto){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update reldespesa set "
                    + "saldoant=? "
                    + "where idreldespesa=? "
                    + "and filiais_idfiliais=?");
            stm.setString(1, produto.getSaldoant());
            stm.setInt(2, produto.getChave());
            stm.setInt(3, produto.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 altRDdeb (reldespesaGS produto){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update reldespesa set "
                    + "debito=? "
                    + "where idreldespesa=? "
                    + "and filiais_idfiliais=?");
            stm.setString(1, produto.getDebito());
            stm.setInt(2, produto.getChave());
            stm.setInt(3, produto.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 altRDcre (reldespesaGS produto){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update reldespesa set "
                    + "credito=? "
                    + "where idreldespesa=? "
                    + "and filiais_idfiliais=?");
            stm.setString(1, produto.getCredito());
            stm.setInt(2, produto.getChave());
            stm.setInt(3, produto.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 altRDsal (reldespesaGS produto){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update reldespesa set "
                    + "saldo=? "
                    + "where idreldespesa=? "
                    + "and filiais_idfiliais=?");
            stm.setString(1, produto.getSaldo());
            stm.setInt(2, produto.getChave());
            stm.setInt(3, produto.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 altRDreem (reldespesaGS produto){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update reldespesa set "
                    + "reelbolso=? "
                    + "where idreldespesa=? "
                    + "and filiais_idfiliais=?");
            stm.setString(1, produto.getReelbolso());
            stm.setInt(2, produto.getChave());
            stm.setInt(3, produto.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 altRDffixo (reldespesaGS produto){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update reldespesa set "
                    + "ffixo=? "
                    + "where idreldespesa=? "
                    + "and filiais_idfiliais=?");
            stm.setString(1, produto.getFfixo());
            stm.setInt(2, produto.getChave());
            stm.setInt(3, produto.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 altRDaud (reldespesaGS produto){
        boolean testa = false;
        try{
            stm = Conexao.conectar().prepareStatement("update reldespesa set "
                    + "auditoria=? "
                    + "where idreldespesa=? "
                    + "and filiais_idfiliais=?");
            stm.setString(1, produto.getAuditoria());
            stm.setInt(2, produto.getChave());
            stm.setInt(3, produto.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 excluiReldesp (reldespesaGS produto){
        boolean testa = false;
        try {
            stm = Conexao.conectar().prepareStatement("delete from reldespesa "
                    + "where idreldespesa = ? "
                    + "and filiais_idfiliais = ?");
            stm.setInt(1, produto.getChave());
            stm.setInt(2, produto.getFiliais_idfiliais());
            int executeUpdate = stm.executeUpdate();

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

    public void selReldesp(String Id, String Idf) {
        try {
            reldespesaGS scid = null;
            stmt = Conexao.conectar().createStatement();
            rs = stmt.executeQuery("Select * From reldespesa "
                    + "Where idreldespesa ='"+Id+"' "
                    + "and filiais_idfiliais = '"+Idf+"'");
            if (rs.next()) {
                scid = new reldespesaGS();
                scid.setChave(rs.getInt("chave"));
                scid.setAuditoria(rs.getString("auditoria"));
                scid.setCredito(rs.getString("credito"));
                scid.setDatarel(rs.getString("datarel"));
                scid.setDebito(rs.getString("debito"));
                scid.setDtsaldoant(rs.getString("dtsaldoant"));
                scid.setFfixo(rs.getString("ffixo"));
                scid.setFiliais_idfiliais(rs.getInt("filiais_idfiliais"));
                scid.setFormareelb(rs.getString("formareelb"));
                scid.setIdctto(rs.getInt("idctto"));
                scid.setReelbolso(rs.getString("reelbolso"));
                scid.setReembolsar(rs.getString("reembolsar"));
                scid.setSaldo(rs.getString("saldo"));
                scid.setSaldoant(rs.getString("saldoant"));
                scid.setIdusuario(rs.getInt("idusuario"));
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

    public void listaReldesp(String Id, String Idu){
        try{
            stmt = Conexao.conectar().createStatement();
            rs = stmt.executeQuery("select r.*, u.nome from reldespesa r, usuarios u where "
                    + "idusuarios=idusuario and "
                    + "filiais_idfiliais = '"+Id+"' and "
                    + "idusuarios= '"+Idu+"'order by idreldespesa");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void impReldesp(String Id, String Idf){
        try{
            stmt = Conexao.conectar().createStatement();
            rs = stmt.executeQuery("select r.*, i.*, f.razao_social, c.cttonr, u.nomecompleto "
                    + "from reldespesa r, itensreldesp i, filiais f, cttos c, usuarios u "
                    + "where r.idreldespesa = i.reldespesa_idreldespesa "
                    + "and r.filiais_idfiliais=i.filiais_idfiliais "
                    + "and r.filiais_idfiliais=f.idfiliais "
                    + "and i.filiais_idfiliais=f.idfiliais "
                    + "and r.idctto = c.idcttos "
                    + "and r.filiais_idfiliais=c.filiais_idfiliais "
                    + "and r.idusuario = u.idusuarios "
                    + "and r.filiais_idfiliais='"+Idf+"' "
                    + "and r.idreldespesa='"+Id+"' order by i.iditensreldesp");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void exiReldesp(){
        try{
            stmt = Conexao.conectar().createStatement();
            rs = stmt.executeQuery("select * from relreldespesa "
                    + "order by dtdespesa");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public ResultSet getResultado(){
        return rs;
    }
    
}
