fixes bug#14052

qua:renato
dev:

git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Web/trunk/ventaboletos@93150 d1611594-4594-4d17-8e1d-87c2c4800839
master
walace 2019-05-13 14:42:39 +00:00
parent 0da7cceb6a
commit b5ebed25f9
2 changed files with 282 additions and 166 deletions

View File

@ -1,33 +1,94 @@
package com.rjconsultores.ventaboletos.relatorios.utilitarios;
public class RelatorioEmbarqueLocalidadeBean {
protected String data;
protected String linha;
protected Long embarques;
private String data;
private String horario;
private String linha;
private String classe;
private Long embarques;
private Integer indexColun;
private Integer indexLinha;
public String getHorario() {
return horario;
}
public void setHorario(String horario) {
this.horario = horario;
}
public String getData() {
return data.substring(0, 10);
}
public String getHorario(){
return data.substring(11, data.length());
}
public void setData(String data) {
this.data = data;
}
public String getLinha() {
return linha;
}
public void setLinha(String linha) {
this.linha = linha;
}
public Long getEmbarques() {
return embarques;
}
public void setEmbarques(Long embarques) {
this.embarques = embarques;
}
public Integer getIndexColun() {
return indexColun;
}
public void setIndexColun(Integer indexColun) {
this.indexColun = indexColun;
}
public Integer getIndexLinha() {
return indexLinha;
}
public void setIndexLinha(Integer indexLinha) {
this.indexLinha = indexLinha;
}
public String getClasse() {
return classe;
}
public void setClasse(String classe) {
this.classe = classe;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((linha == null) ? 0 : linha.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RelatorioEmbarqueLocalidadeBean other = (RelatorioEmbarqueLocalidadeBean) obj;
if (linha == null) {
if (other.linha != null)
return false;
} else if (!linha.equals(other.linha))
return false;
return true;
}
}

View File

@ -9,10 +9,12 @@ import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import javax.sql.DataSource;
@ -23,7 +25,10 @@ import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -99,9 +104,10 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
private Ruta ruta;
private OrgaoConcedente orgao;
private Parada origem;
private int total;
private Map<String, Integer> mapAux = new HashMap<String, Integer>();
private List<RelatorioEmbarqueLocalidadeBean> lsDadosRelatorio;
private List<RelatorioEmbarqueLocalidadeBean> ls;
private void executarRelatorio() throws Exception {
@ -119,14 +125,14 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
String sql = getSql(empresa.getEmpresaId().toString(),
ruta != null ? ruta.getRutaId().toString() : null,
String sql = getSql(empresa, ruta != null ? ruta.getRutaId().toString() : null,
orgao != null ? orgao.getOrgaoConcedenteId().toString() : null,
origem != null ? origem.getParadaId().toString() : null, horInicio, horFim);
ResultSet rset = null;
try{
NamedParameterStatement stmt = new NamedParameterStatement(dataSourceRead.getConnection(), sql);
ResultSet rset = null;
stmt.setString(FILED_FEC_INICIO, fecInicio);
stmt.setString(FIELD_FEC_FINAL, fecFinal);
@ -152,129 +158,135 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
}
rset = stmt.executeQuery();
lsDadosRelatorio = new ArrayList<RelatorioEmbarqueLocalidadeBean>();
Map<String, Map<String, List<RelatorioEmbarqueLocalidadeBean>>> datas = ResultSetToList(rset);
exportaExcel(datas);
} catch (Exception e){
log.error("", e);
}finally {
if(rset != null && !rset.isClosed()) {
rset.close();
}
}
}
private void exportaExcel(Map<String, Map<String, List<RelatorioEmbarqueLocalidadeBean>>> datas){
try{
HSSFWorkbook wb = new HSSFWorkbook();
private void exportaExcel(Map<String, Map<String, List<RelatorioEmbarqueLocalidadeBean>>> datas) {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCellStyle estiloVerticalMerge = wb.createCellStyle();
estiloVerticalMerge.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
HSSFCellStyle estiloVerticalMerge = wb.createCellStyle();
estiloVerticalMerge.setVerticalAlignment(CellStyle.ALIGN_CENTER);
HSSFSheet sheet1 = null;
HSSFFont font = wb.createFont();
HSSFCellStyle style = wb.createCellStyle();
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short)10);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
style.setFont(font);
int linha = NUM_LINHA_INICIO;
int coluna = 1;
HSSFRow row = null;
HSSFSheet sheet1 = null;
for (Entry<String, Map<String, List<RelatorioEmbarqueLocalidadeBean>>> data : datas.entrySet()){
// entry.getKey() => Data
sheet1 = wb.createSheet(data.getKey().replaceAll("/", "-"));
geraCabecalho(sheet1, data.getKey(), wb);
int totalGeral = 0;
for (Entry<String, List<RelatorioEmbarqueLocalidadeBean>> horario : data.getValue().entrySet()){
row = sheet1.createRow(linha);
HSSFCell cellHorario = row.createCell(0);
cellHorario.setCellValue(horario.getKey());
cellHorario.setCellStyle(estiloVerticalMerge);
int linha = NUM_LINHA_INICIO;
HSSFRow row = null;
HSSFCell cellTotal = row.createCell(7);
sheet1 = wb.createSheet("07-08-19");
geraCabecalho(sheet1, "07-08-19", wb);
List<RelatorioEmbarqueLocalidadeBean> rows = horario.getValue();
Map<String, Integer> linhas = new HashMap<String, Integer>();
int totalGeral = 0;
int rowcount = (int) Math.ceil((double)rows.size() / 3) -1;
int linhaTotalizador = 6;
HSSFCell cellTotal = null ;
for (RelatorioEmbarqueLocalidadeBean bean : ls) {
int indexColun = mapAux.get(bean.getHorario());
sheet1.addMergedRegion(new CellRangeAddress(
linha, //first row (0-based)
linha+rowcount, //last row (0-based)
0, //first column (0-based)
0 //last column (0-based)
));
if(!linhas.containsKey(bean.getLinha())) {
linhas.put(bean.getLinha(), linha);
row = sheet1.createRow(linha);
linhaTotalizador = linha;
linha++;
sheet1.addMergedRegion(new CellRangeAddress(
linha, //first row (0-based)
linha+rowcount, //last row (0-based)
7, //first column (0-based)
7 //last column (0-based)
));
int beancount = 1;
int embarquesTotais = 0;
for (RelatorioEmbarqueLocalidadeBean bean : rows){
if (beancount == 4){
linha++;
row = sheet1.createRow(linha);
coluna = 1;
}
row.createCell(coluna++).setCellValue(bean.getLinha());
row.createCell(coluna++).setCellValue(bean.getEmbarques());
embarquesTotais += bean.getEmbarques();
beancount++;
}
cellTotal.setCellValue(embarquesTotais);
cellTotal.setCellStyle(estiloVerticalMerge);
linha++;
coluna = 1;
totalGeral += embarquesTotais;
}else {
row = sheet1.getRow(linhas.get(bean.getLinha()));
linhaTotalizador = sheet1.getRow(linhas.get(bean.getLinha())).getRowNum();
}
row = sheet1.createRow(linha);
HSSFCell cellTotal = row.createCell(0);
cellTotal.setCellValue("TOTAL");
HSSFCell cellTotalValue = row.createCell(7);
cellTotalValue.setCellValue(totalGeral);
HSSFCell cellHorario = row.createCell(0);
cellHorario.setCellStyle(estiloVerticalMerge);
linha = NUM_LINHA_INICIO;
cellTotal = row.createCell(mapAux.size() + 3);
sheet1.autoSizeColumn(0);
sheet1.autoSizeColumn(1);
sheet1.autoSizeColumn(2);
sheet1.autoSizeColumn(3);
sheet1.autoSizeColumn(4);
sheet1.autoSizeColumn(5);
sheet1.autoSizeColumn(6);
sheet1.autoSizeColumn(7);
row.createCell(indexColun).setCellValue(bean.getEmbarques());
row.createCell(0).setCellValue(bean.getLinha());
row.createCell(1).setCellValue(bean.getClasse());
}
if(datas.isEmpty()){
Messagebox.show(
Labels.getLabel("relatorio.parametro.msgNoData"),
Labels.getLabel("relatorioEmbarqueLocalidadeController.window.title"),
Messagebox.OK, Messagebox.INFORMATION);
return;
}
cellTotal.setCellStyle(estiloVerticalMerge);
String colluna;
String celTotal ;
colluna = CellReference.convertNumToColString(mapAux.size()+2);
celTotal = "SUM(D"+(linhaTotalizador+1)+":"+colluna+(linhaTotalizador+1)+")";
row.createCell(mapAux.size()+3).setCellValue(celTotal);
cellTotal.setCellFormula(celTotal);
cellTotal.setCellStyle(style);
String nomeArquivo =
UsuarioLogado.getUsuarioLogado().getNombusuario()
+ "_" + Calendar.getInstance().getTime().getTime()
+ "_" + "EmbarqueLocalidade";
totalGeral += bean.getEmbarques();
}
File fNomeArquivo = File.createTempFile(nomeArquivo, ".tmp");
row = sheet1.createRow(linha);
int qtdLinhas = linhas.size()+5;
FileOutputStream stream = new FileOutputStream(fNomeArquivo);
stream.flush();
wb.write(stream);
stream.close();
int somatorioColuna = 3;
for (int i = 3; i < mapAux.size()+3; i++) {
InputStream is = null;
is = new FileInputStream(fNomeArquivo);
Filedownload.save(is, "application/xls", "EmbarqueLocalidade.xls");
String colluna = CellReference.convertNumToColString(i);
cellTotal = row.createCell(somatorioColuna);
cellTotal.setCellFormula("SUM("+colluna+6+":"+colluna+(qtdLinhas)+")");
cellTotal.setCellStyle(style);
somatorioColuna++;
fNomeArquivo.delete();
} catch (Exception e){
log.error("", e);
}
HSSFCell cellTotalValue = row.createCell(total);
cellTotalValue = row.createCell(mapAux.size()+3);
cellTotalValue.setCellValue(totalGeral);
cellTotalValue.setCellStyle(style);
linha = NUM_LINHA_INICIO;
for (int i = 0; i <= total; i++) {
sheet1.autoSizeColumn(i);
}
if(mapAux.isEmpty()){
Messagebox.show(
Labels.getLabel("relatorio.parametro.msgNoData"),
Labels.getLabel("relatorioEmbarqueLocalidadeController.window.title"),
Messagebox.OK, Messagebox.INFORMATION);
return;
}
String nomeArquivo =
UsuarioLogado.getUsuarioLogado().getNombusuario()
+ "_" + Calendar.getInstance().getTime().getTime()
+ "_" + "EmbarqueLocalidade";
File fNomeArquivo = File.createTempFile(nomeArquivo, ".tmp");
FileOutputStream stream = new FileOutputStream(fNomeArquivo);
stream.flush();
wb.write(stream);
stream.close();
InputStream is = null;
is = new FileInputStream(fNomeArquivo);
Filedownload.save(is, "application/xls", "EmbarqueLocalidade.xls");
fNomeArquivo.delete();
}catch (Exception e) {
e.printStackTrace();
}
}
private void geraCabecalhoTitulo(HSSFSheet sheet1, HSSFWorkbook wb){
@ -322,21 +334,66 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
1, //first column (0-based)
7 //last column (0-based)
));
HSSFFont font = wb.createFont();
HSSFCellStyle style = wb.createCellStyle();
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short)10);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
style.setFont(font);
//Montando o Cabeçalho
HSSFRow r0 = sheet1.createRow(NUM_LINHA_DATA_EMPRESA);
r0.createCell(0).setCellValue("DATA: " + dataAtual);
r0.createCell(1).setCellValue("EMPRESA: " + empresa.getNombempresa());
if(empresa != null) {
r0.createCell(1).setCellValue("EMPRESA: " + empresa.getNombempresa());
r0.getCell(1).setCellStyle(style);
r0.createCell(1).setCellValue("EMPRESA: " + empresa.getNombempresa());
r0.getCell(1).setCellStyle(style);
font.setColor(HSSFColor.RED.index);
style.setFont(font);
}else {
}
HSSFRow r1 = sheet1.createRow(NUM_LINHA_DATA_EMPRESA+1);
r1.createCell(0).setCellValue("FAIXA HORÁRIA");
r1.createCell(1).setCellValue("Prefixo / Nome Linha");
r1.createCell(2).setCellValue("Embarques");
r1.createCell(3).setCellValue("Prefixo / Nome Linha");
r1.createCell(4).setCellValue("Embarques");
r1.createCell(5).setCellValue("Prefixo / Nome Linha");
r1.createCell(6).setCellValue("Embarques");
r1.createCell(7).setCellValue("TOTAL");
List<RelatorioEmbarqueLocalidadeBean> lsAux = new ArrayList<RelatorioEmbarqueLocalidadeBean>();
mapAux = new HashMap<String, Integer>();
Map<String, Integer> map = new HashMap<String, Integer>();
int i = 3;
for (RelatorioEmbarqueLocalidadeBean bean : ls) {
if(map.containsKey(bean.getHorario())) {
Integer indice = map.get(bean.getHorario());
map.put(bean.getHorario(), indice);
bean.setIndexColun(indice);
}else {
map.put(bean.getHorario(),i);
bean.setIndexColun(i);
i++;
}
lsAux.add(bean);
}
i = 3;
mapAux = new TreeMap<String, Integer>(map);
HSSFFont fontHorario = wb.createFont();
HSSFCellStyle styleHorario = wb.createCellStyle();
fontHorario.setColor(HSSFColor.BLACK.index);
fontHorario.setBoldweight(Font.BOLDWEIGHT_BOLD);
styleHorario.setFont(fontHorario);
for (Entry<String,Integer> bean : mapAux.entrySet()) {
r1.createCell(bean.getValue()).setCellValue(bean.getKey());
r1.getCell(bean.getValue()).setCellStyle(styleHorario);
i++;
}
r1.createCell(i++).setCellValue("TOTAL");
r1.setRowStyle(styleHorario);
total = i;
}
public void onClick$btnExecutarRelatorio(Event ev) throws Exception {
@ -364,16 +421,17 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
cmbOrigem.setModel(new ListModelList(lsOrigens));
}
protected String getSql(String empresa, String ruta, String orgao, String origem, String horInicio, String horFim) {
protected String getSql(Empresa empresa, String ruta, String orgao, String origem, String horInicio, String horFim) {
StringBuilder sql = new StringBuilder();
sql.append("select ");
sql.append(" concat(concat(concat(TO_CHAR(b.fechorviaje,'DD/MM/YYYY HH24'), ':00'), ' a '),concat(TO_CHAR(b.fechorviaje,'HH24'), ':59')), ");
sql.append(" concat(concat(r.prefixo, ' '), r.descruta), ");
sql.append(" concat(concat(concat(TO_CHAR(b.fechorviaje,'HH24'), ':00'), ' a '),concat(TO_CHAR(b.fechorviaje,'HH24'), ':59')), ");
sql.append(" concat(concat(r.prefixo, ' '), r.descruta), cla.descclase, ");
sql.append(" count(b.boleto_id) ");
sql.append("from ");
sql.append(" boleto b ");
sql.append(" inner join ruta r on r.ruta_id = b.ruta_id ");
sql.append(" inner join clase_servicio cla on cla.claseservicio_id = r.claseservicio_id ");
sql.append(" inner join marca m on m.marca_id = b.marca_id ");
sql.append("where ");
sql.append(" b.fechorviaje between TO_DATE(:fecInicio, 'DD/MM/YYYY HH24:MI:SS') AND TO_DATE(:fecFinal, 'DD/MM/YYYY HH24:MI:SS') ");
@ -384,7 +442,7 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
if (orgao != null && !orgao.isEmpty()){
sql.append(" and r.orgaoconcedente_id = :orgao ");
}
if (!empresa.isEmpty()){
if (empresa != null){
sql.append(" and m.empresa_id = :empresa ");
}
if (ruta != null && !ruta.isEmpty() && !ruta.equals("-1")){
@ -395,8 +453,8 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
}
sql.append("group by ");
sql.append(" concat(concat(concat(TO_CHAR(b.fechorviaje,'DD/MM/YYYY HH24'), ':00'), ' a '),concat(TO_CHAR(b.fechorviaje,'HH24'), ':59')), ");
sql.append(" concat(concat(r.prefixo, ' '), r.descruta) ");
sql.append(" concat(concat(concat(TO_CHAR(b.fechorviaje,'HH24'), ':00'), ' a '),concat(TO_CHAR(b.fechorviaje,'HH24'), ':59')), ");
sql.append(" concat(concat(r.prefixo, ' '), r.descruta), cla.descclase ");
sql.append("order by ");
sql.append(" 1 ");
@ -406,26 +464,22 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
protected Map<String, Map<String, List<RelatorioEmbarqueLocalidadeBean>>> ResultSetToList(ResultSet rset) throws SQLException {
Map<String, Map<String, List<RelatorioEmbarqueLocalidadeBean>>> datas = new LinkedHashMap<String, Map<String, List<RelatorioEmbarqueLocalidadeBean>>>();
ls = new ArrayList<RelatorioEmbarqueLocalidadeBean>();
while (rset.next()) {
RelatorioEmbarqueLocalidadeBean bean = new RelatorioEmbarqueLocalidadeBean();
bean.setData(rset.getString(1));
bean.setHorario(rset.getString(1));
bean.setLinha(rset.getString(2));
bean.setEmbarques(rset.getLong(3));
if (!datas.containsKey(bean.getData())){
datas.put(bean.getData(), new LinkedHashMap<String, List<RelatorioEmbarqueLocalidadeBean>>());
}
if (!datas.get(bean.getData()).containsKey(bean.getHorario())){
datas.get(bean.getData()).put(bean.getHorario(), new ArrayList<RelatorioEmbarqueLocalidadeBean>());
}
datas.get(bean.getData()).get(bean.getHorario()).add(bean);
bean.setClasse(rset.getString(3));
bean.setEmbarques(rset.getLong(4));
ls.add(bean);;
}
return datas;
}
public void checarLinhasDuplicadas() {
}
public Datebox getDatInicial() {
return datInicial;
}
@ -523,3 +577,4 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
}
}