diff --git a/src/java/com/rjconsultores/ventaboletos/relatorios/impl/RelatorioAgenciaFechamento.java b/src/java/com/rjconsultores/ventaboletos/relatorios/impl/RelatorioAgenciaFechamento.java index 0f9afa58f..051aa1490 100644 --- a/src/java/com/rjconsultores/ventaboletos/relatorios/impl/RelatorioAgenciaFechamento.java +++ b/src/java/com/rjconsultores/ventaboletos/relatorios/impl/RelatorioAgenciaFechamento.java @@ -20,7 +20,7 @@ import com.rjconsultores.ventaboletos.web.utilerias.NamedParameterStatement; public class RelatorioAgenciaFechamento extends Relatorio { private List lsDadosRelatorio; - + public RelatorioAgenciaFechamento(Map parametros, Connection conexao) throws Exception { super(parametros, conexao); @@ -37,54 +37,54 @@ public class RelatorioAgenciaFechamento extends Relatorio { String empresa = parametros.get("empresa") != null ? parametros.get("empresa").toString() : ""; String empresaNome = parametros.get("empresaNome") != null ? parametros.get("empresaNome").toString() : ""; String puntoVenta = parametros.get("NUMPUNTOVENTA") != null ? parametros.get("NUMPUNTOVENTA").toString() : null; - - RelatorioAgenciaFechamentoBean agenciaFechamento = new RelatorioAgenciaFechamentoBean(); + lsDadosRelatorio = new ArrayList(); - Boolean existeRegistro = Boolean.FALSE; - + String sql = carregarDadosTotaisdeVenda(puntoVenta); System.out.println(sql); - BigDecimal total = BigDecimal.ZERO; - BigDecimal vendido = BigDecimal.ZERO; NamedParameterStatement stmt = new NamedParameterStatement(conexao, sql); - ResultSet rset = null; + ResultSet rset1 = null; + ResultSet rset2 = null; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"); - stmt = new NamedParameterStatement(conexao, sql); stmt.setTimestamp("de", new java.sql.Timestamp(sdf.parse(fecInicio).getTime())); stmt.setTimestamp("ate", new java.sql.Timestamp(sdf.parse(fecFinal).getTime())); - + if (empresa != null && !empresa.equals("")) { stmt.setInt("empresaId", Integer.parseInt(empresa)); } - + if (puntoVenta != null && !puntoVenta.equals("-1")) { stmt.setString("puntoVentaId", puntoVenta); } - rset = stmt.executeQuery(); + rset1 = stmt.executeQuery(); - while (rset.next()) { + while (rset1.next()) { - existeRegistro = Boolean.TRUE; + RelatorioAgenciaFechamentoBean agenciaFechamento = new RelatorioAgenciaFechamentoBean(); + BigDecimal total = BigDecimal.ZERO; + BigDecimal vendido = BigDecimal.ZERO; + + String puntoVentaId = rset1.getString("codigo"); - Integer qtdevendido = rset.getInt("qtdevendido"); - Integer qtdedigitado = rset.getInt("qtdedigitado"); - Integer qtdeAnulado = rset.getInt("qtdeAnulado"); - Integer qtdeDevolvido = rset.getInt("qtdeDevolvido"); + Integer qtdevendido = rset1.getInt("qtdevendido"); + Integer qtdedigitado = rset1.getInt("qtdedigitado"); + Integer qtdeAnulado = rset1.getInt("qtdeAnulado"); + Integer qtdeDevolvido = rset1.getInt("qtdeDevolvido"); Integer qtdeTotal = null; - vendido = rset.getBigDecimal("vendido"); - BigDecimal digitado = rset.getBigDecimal("digitado"); - BigDecimal anulado = rset.getBigDecimal("anulado"); - BigDecimal devolvido = rset.getBigDecimal("devolvido"); + vendido = rset1.getBigDecimal("vendido"); + BigDecimal digitado = rset1.getBigDecimal("digitado"); + BigDecimal anulado = rset1.getBigDecimal("anulado"); + BigDecimal devolvido = rset1.getBigDecimal("devolvido"); agenciaFechamento.setEmpresa(empresaNome); - agenciaFechamento.setCodigo(rset.getString("codigo")); - agenciaFechamento.setDescricao(rset.getString("descricao")); + agenciaFechamento.setCodigo(rset1.getString("codigo")); + agenciaFechamento.setDescricao(rset1.getString("descricao")); agenciaFechamento.setQtdeVendido(qtdevendido != null ? qtdevendido : NumberUtils.INTEGER_ZERO); agenciaFechamento.setQtdeDigitado(qtdedigitado != null ? qtdedigitado : NumberUtils.INTEGER_ZERO); @@ -94,6 +94,10 @@ public class RelatorioAgenciaFechamento extends Relatorio { qtdeTotal = ((qtdevendido != null ? qtdevendido : NumberUtils.INTEGER_ZERO) + (qtdedigitado != null ? qtdedigitado : NumberUtils.INTEGER_ZERO)) - ((qtdeAnulado != null ? qtdeAnulado : NumberUtils.INTEGER_ZERO) + (qtdeDevolvido != null ? qtdeDevolvido : NumberUtils.INTEGER_ZERO)); + if (qtdeTotal < 0) { + qtdeTotal = NumberUtils.INTEGER_ZERO; + } + agenciaFechamento.setQtdeTotal(qtdeTotal); agenciaFechamento.setDigitado(digitado != null ? digitado : BigDecimal.ZERO); @@ -104,12 +108,11 @@ public class RelatorioAgenciaFechamento extends Relatorio { total = total.add(digitado != null ? digitado : BigDecimal.ZERO); total = total.subtract(anulado != null ? anulado : BigDecimal.ZERO); total = total.subtract(devolvido != null ? devolvido : BigDecimal.ZERO); + + stmt = new NamedParameterStatement(conexao, carregarDadosReceitaDespesa(puntoVentaId)); + + System.out.println(carregarDadosReceitaDespesa(puntoVentaId)); - } - - if (existeRegistro) { - - stmt = new NamedParameterStatement(conexao, carregarDadosReceitaDespesa(puntoVenta)); stmt.setTimestamp("de", new java.sql.Timestamp(sdf.parse(fecInicio).getTime())); stmt.setTimestamp("ate", new java.sql.Timestamp(sdf.parse(fecFinal).getTime())); @@ -117,17 +120,17 @@ public class RelatorioAgenciaFechamento extends Relatorio { stmt.setInt("empresaId", Integer.parseInt(empresa)); } - if (puntoVenta != null && !puntoVenta.equals("-1")) { - stmt.setString("puntoVentaId", puntoVenta); + if (puntoVentaId != null ) { + stmt.setString("puntoVentaId", puntoVentaId); } - - rset = stmt.executeQuery(); - while (rset.next()) { + rset2 = stmt.executeQuery(); - Integer qtdedespesa = rset.getInt("qtdedespesa"); - BigDecimal receita = rset.getBigDecimal("receita"); - BigDecimal despesa = rset.getBigDecimal("despesa"); + if (rset2.next()) { + + Integer qtdedespesa = rset2.getInt("qtdedespesa"); + BigDecimal receita = rset2.getBigDecimal("receita"); + BigDecimal despesa = rset2.getBigDecimal("despesa"); agenciaFechamento.setQtdeDespesa(qtdedespesa != null ? qtdedespesa : NumberUtils.INTEGER_ZERO); agenciaFechamento.setReceita(receita != null ? receita : BigDecimal.ZERO); @@ -140,8 +143,14 @@ public class RelatorioAgenciaFechamento extends Relatorio { total = total.subtract(despesa != null ? despesa : BigDecimal.ZERO); agenciaFechamento.setVendido(vendido != null ? vendido : BigDecimal.ZERO); - agenciaFechamento.setTotal(total); + agenciaFechamento.setTotal(total.abs()); + } else { + agenciaFechamento.setQtdeDespesa(NumberUtils.INTEGER_ZERO); + agenciaFechamento.setReceita(BigDecimal.ZERO); + agenciaFechamento.setDespesa(BigDecimal.ZERO); + agenciaFechamento.setVendido(vendido != null ? vendido : BigDecimal.ZERO); + agenciaFechamento.setTotal(total.abs()); } lsDadosRelatorio.add(agenciaFechamento); @@ -155,7 +164,7 @@ public class RelatorioAgenciaFechamento extends Relatorio { } }); } - + public void setLsDadosRelatorio(List lsDadosRelatorio) { this.setCollectionDataSource(new JRBeanCollectionDataSource(lsDadosRelatorio)); this.lsDadosRelatorio = lsDadosRelatorio; @@ -164,27 +173,27 @@ public class RelatorioAgenciaFechamento extends Relatorio { @Override protected void processaParametros() throws Exception { } - + private String carregarDadosTotaisdeVenda(String puntoVenta) { - + StringBuilder sql = new StringBuilder(); sql.append(" SELECT "); sql.append(" pv.PUNTOVENTA_ID AS codigo, "); sql.append(" pv.nombpuntoventa AS descricao, "); - sql.append(" COUNT(CASE WHEN c.MOTIVOCANCELACION_ID IS NULL THEN cfp.IMPORTE ELSE NULL END) AS qtdevendido, "); + sql.append(" COALESCE(COUNT(CASE WHEN c.MOTIVOCANCELACION_ID IS NULL THEN cfp.IMPORTE ELSE NULL END),0) AS qtdevendido, "); sql.append(" 0 AS qtdedigitado, "); - sql.append(" COUNT(CASE WHEN c.MOTIVOCANCELACION_ID = 31 THEN 1 ELSE NULL END) AS qtdeAnulado, "); - sql.append(" COUNT(CASE WHEN c.MOTIVOCANCELACION_ID = 32 THEN 1 ELSE NULL END) AS qtdeDevolvido, "); - sql.append(" SUM(CASE WHEN c.MOTIVOCANCELACION_ID IS NULL THEN cfp.IMPORTE ELSE 0 END) AS vendido, "); + sql.append(" COALESCE(COUNT(CASE WHEN c.MOTIVOCANCELACION_ID = 31 THEN 1 ELSE NULL END),0) AS qtdeAnulado, "); + sql.append(" COALESCE(COUNT(CASE WHEN c.MOTIVOCANCELACION_ID = 32 THEN 1 ELSE NULL END),0) AS qtdeDevolvido, "); + sql.append(" COALESCE(SUM(CASE WHEN c.MOTIVOCANCELACION_ID IS NULL THEN cfp.IMPORTE ELSE NULL END),0) AS vendido, "); sql.append(" 0 AS digitado, "); - sql.append(" SUM(CASE WHEN c.MOTIVOCANCELACION_ID = 31 THEN cfp.IMPORTE ELSE 0 END) AS anulado, "); - sql.append(" SUM(CASE WHEN c.MOTIVOCANCELACION_ID = 32 THEN cfp.IMPORTE ELSE 0 END) AS devolvido "); + sql.append(" COALESCE(SUM(CASE WHEN c.MOTIVOCANCELACION_ID = 31 THEN cfp.IMPORTE ELSE NULL END),0) AS anulado, "); + sql.append(" COALESCE(SUM(CASE WHEN c.MOTIVOCANCELACION_ID = 32 THEN cfp.IMPORTE ELSE NULL END),0) AS devolvido "); sql.append(" FROM CAJA c "); sql.append(" INNER JOIN CAJA_FORMAPAGO cfp ON c.CAJA_ID = cfp.CAJA_ID AND cfp.activo = 1 "); sql.append(" INNER JOIN PUNTO_VENTA pv ON c.PUNTOVENTA_ID = pv.PUNTOVENTA_ID "); sql.append(" WHERE c.TURNO_ID IS NOT NULL "); sql.append(" AND c.FECCORTE BETWEEN :de AND :ate "); - if(puntoVenta != null && !puntoVenta.equals("-1")){ + if (puntoVenta != null && !puntoVenta.equals("-1")) { sql.append(" AND c.PUNTOVENTA_ID =:puntoVentaId "); } sql.append(" AND c.TIPOVENTA_ID <> 6 "); @@ -192,29 +201,29 @@ public class RelatorioAgenciaFechamento extends Relatorio { sql.append(" GROUP BY pv.PUNTOVENTA_ID , pv.nombpuntoventa "); sql.append(" ORDER BY pv.PUNTOVENTA_ID , pv.nombpuntoventa "); - return sql.toString(); - + return sql.toString(); + } - + private String carregaAgenciasComVenda() { - + StringBuilder sql = new StringBuilder(); sql.append(" SELECT DISTINCT cc.PUNTOVENTA_ID "); sql.append(" FROM CONTA_CORRENTE_PTOVTA cc "); sql.append(" WHERE cc.FECHOROPERACION BETWEEN :de AND :ate "); sql.append(" and cc.EMPRESA_ID = :empresaId "); - + return sql.toString(); - + } - + private String carregarDadosReceitaDespesa(String puntoVenta) { - + StringBuilder sql = new StringBuilder(); sql.append(" SELECT "); - sql.append(" COUNT(CASE WHEN cdp.IMPORTE < 0 THEN 1 ELSE NULL END) as qtdedespesa, "); - sql.append(" SUM(CASE WHEN cdp.IMPORTE > 0 THEN cdp.IMPORTE ELSE 0 END) AS receita, "); - sql.append(" ABS(SUM(CASE WHEN cdp.IMPORTE < 0 THEN cdp.IMPORTE ELSE 0 END)) AS despesa "); + sql.append(" COALESCE(COUNT(CASE WHEN cdp.IMPORTE < 0 THEN 1 ELSE NULL END),0) as qtdedespesa, "); + sql.append(" COALESCE(SUM(CASE WHEN cdp.IMPORTE > 0 THEN cdp.IMPORTE ELSE NULL END),0) AS receita, "); + sql.append(" COALESCE(ABS(SUM(CASE WHEN cdp.IMPORTE < 0 THEN cdp.IMPORTE ELSE NULL END)),0) AS despesa "); sql.append(" FROM CAJA_DIVERSOS cd, "); sql.append(" CAJA_DIVERSOS_PAGO cdp, "); sql.append(" EVENTO_EXTRA ee "); @@ -224,24 +233,24 @@ public class RelatorioAgenciaFechamento extends Relatorio { sql.append(" AND ee.activo = 1 "); sql.append(" AND cd.TURNO_ID IS NOT NULL "); sql.append(" AND cd.FECCORTE BETWEEN :de AND :ate "); - if(puntoVenta != null && !puntoVenta.equals("-1")){ + if (puntoVenta != null && !puntoVenta.equals("-1")) { sql.append(" AND cd.PUNTOVENTA_ID = :puntoVentaId "); } sql.append(" AND ee.EMPRESA_ID=:empresaId "); - + return sql.toString(); - + } - + private String carregarNomeEmpresa() { - + StringBuilder sql = new StringBuilder(); sql.append(" SELECT "); sql.append(" NOMBEMPRESA "); sql.append(" FROM EMPRESA "); sql.append(" WHERE EMPRESA_ID=:empresaId "); - + return sql.toString(); - + } }