package com.rjconsultores.integracaoreceitadespesa.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Properties; import org.apache.log4j.Logger; import com.rjconsultores.integracaoreceitadespesa.Application; import com.rjconsultores.integracaoreceitadespesa.entidades.DepositoBean; import com.rjconsultores.integracaoreceitadespesa.entidades.Empresa; import com.rjconsultores.integracaoreceitadespesa.entidades.PuntoVenta; public class Totalbus { private static final Logger log = Logger.getLogger(Totalbus.class); private static Totalbus instance = null; private Connection conn; private List pontosVenda = new ArrayList(); private List empresas = new ArrayList(); private Totalbus(){ try { Properties props = Application.getInstance().getApplicationProperties(); String DRIVER = "oracle.jdbc.driver.OracleDriver"; Class.forName(DRIVER); this.conn = DriverManager.getConnection( props.getProperty("url"), props.getProperty("username").trim(), props.getProperty("password").trim()); loadEmpresas(); loadPuntosVenta(); } catch (Exception e){ log.error("", e); } } private void loadPuntosVenta(){ PreparedStatement pstmt = null; ResultSet rs = null; try{ pstmt = getConnection().prepareStatement("Select puntoventa_id, nombpuntoventa from punto_venta order by nombpuntoventa"); rs = pstmt.executeQuery(); while (rs.next()){ PuntoVenta puntoVenta = new PuntoVenta(); puntoVenta.codigo = rs.getInt(1); puntoVenta.nombpuntoventa = rs.getString(2); pontosVenda.add(puntoVenta); } } catch (Exception e){ log.error("", e); } finally { try { rs.close(); } catch (Exception ignore) { log.error("", ignore); } try { pstmt.close(); } catch (Exception ignore) { log.error("", ignore); } } } private void loadEmpresas(){ PreparedStatement pstmt = null; ResultSet rs = null; try{ pstmt = getConnection().prepareStatement("Select empresa_id, nombempresa from empresa order by nombempresa"); rs = pstmt.executeQuery(); while (rs.next()){ Empresa empresa = new Empresa(); empresa.codigo = rs.getInt(1); empresa.nombempresa = rs.getString(2); empresas.add(empresa); } } catch (Exception e){ log.error("", e); } finally { try { rs.close(); } catch (Exception ignore) { log.error("", ignore); } try { pstmt.close(); } catch (Exception ignore) { log.error("", ignore); } } } public static Totalbus getInstance(){ if (instance == null){ instance = new Totalbus(); } return instance; } public Connection getConnection(){ return this.conn; } public List getDepositos(Integer puntoventaId, Integer empresaId){ List depositos = new ArrayList(); StringBuilder sb = new StringBuilder(); sb.append("Select fc.fecfechamento, fc.empresa_id, fc.puntoventa_id, fd.valor_pago, "); sb.append("(select valorconstante from constante where nombconstante = 'CODIGO_RECEITA_DESPESA_GLOBUS') as codigoReceitaDespesa "); sb.append("from fechamento_cntcorrente fc "); sb.append("inner join fechamento_cct_deposito fd on fd.fechamentocntcorrente_id = fc.fechamentocntcorrente_id "); sb.append("where fc.fecfechamento = :fecha and fd.activo <> 0 and fc.activo <> 0 "); if (puntoventaId != null){ sb.append("and fc.puntoventa_id = :puntoventaId "); } if (empresaId != null){ sb.append("and fc.empresa_id = :empresaId "); } Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.DAY_OF_MONTH, -3); Date fecha = cal.getTime(); PreparedStatement pstmt = null; ResultSet rs = null; try{ pstmt = getConnection().prepareStatement(sb.toString()); pstmt.setDate(1, new java.sql.Date(fecha.getTime())); if (puntoventaId != null){ pstmt.setInt(2, puntoventaId); } if (empresaId != null){ pstmt.setInt(3, empresaId); } rs = pstmt.executeQuery(); while (rs.next()){ DepositoBean deposito = new DepositoBean(); deposito.setCodigoEmpresa(rs.getInt(2)); deposito.setCodigoReceitaDespesa(rs.getInt(5)); deposito.setDataLancamento(rs.getDate(1)); deposito.setDataMovimento(rs.getDate(1)); deposito.setLocalArrecadação(rs.getInt(3)); deposito.setValorLançamento(rs.getBigDecimal(4).toString()); depositos.add(deposito); } } catch (Exception e){ log.error("", e); } finally { try { rs.close(); } catch (Exception ignore) { log.error("", ignore); } try { pstmt.close(); } catch (Exception ignore) { log.error("", ignore); } } return depositos; } public Integer getCodigoReceitaDespesaGlobus(){ StringBuilder sb = new StringBuilder(); sb.append("Select valorconstante from constante where nombconstante = 'CODIGO_RECEITA_DESPESA_GLOBUS' "); PreparedStatement pstmt = null; ResultSet rs = null; Integer result = null; try{ pstmt = getConnection().prepareStatement(sb.toString()); rs = pstmt.executeQuery(); if (rs.next()){ result = rs.getInt(1); } } catch (Exception e){ log.error("", e); } finally { try { rs.close(); } catch (Exception ignore) { log.error("", ignore); } try { pstmt.close(); } catch (Exception ignore) { log.error("", ignore); } } return result; } public List getPontosVenda() { return pontosVenda; } public void setPontosVenda(List pontosVenda) { this.pontosVenda = pontosVenda; } public List getEmpresas() { return empresas; } public void setEmpresas(List empresas) { this.empresas = empresas; } }