fixes bug#14354

dev:julio
qua:

git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Web/trunk/ventaboletos@93640 d1611594-4594-4d17-8e1d-87c2c4800839
master
walace 2019-05-22 21:30:20 +00:00
parent 4d050fd238
commit ee88c1f2e9
1 changed files with 275 additions and 258 deletions

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
@ -31,6 +32,7 @@ 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.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Controller;
import org.zkoss.util.resource.Labels;
import org.zkoss.zhtml.Filedownload;
@ -111,10 +113,10 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
private void executarRelatorio() throws Exception {
empresa = cmbEmpresa.getSelectedItem() != null ? (Empresa)cmbEmpresa.getSelectedItem().getValue() : null;
ruta = cmbRuta.getSelectedItem() != null ? (Ruta)cmbRuta.getSelectedItem().getValue() : null;
orgao = cmbOrgaoConcedente.getSelectedItem() != null ? (OrgaoConcedente)cmbOrgaoConcedente.getSelectedItem().getValue() : null;
origem = cmbOrigem.getSelectedItem() != null ? (Parada)cmbOrigem.getSelectedItem().getValue() : null;
empresa = cmbEmpresa.getSelectedItem() != null ? (Empresa) cmbEmpresa.getSelectedItem().getValue() : null;
ruta = cmbRuta.getSelectedItem() != null ? (Ruta) cmbRuta.getSelectedItem().getValue() : null;
orgao = cmbOrgaoConcedente.getSelectedItem() != null ? (OrgaoConcedente) cmbOrgaoConcedente.getSelectedItem().getValue() : null;
origem = cmbOrigem.getSelectedItem() != null ? (Parada) cmbOrigem.getSelectedItem().getValue() : null;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat hf = new SimpleDateFormat("HH:mm:ss");
@ -123,16 +125,16 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
String horInicio = horInicial.getValue() != null ? hf.format(horInicial.getValue()) : "";
String horFim = horFinal.getValue() != null ? hf.format(horFinal.getValue()) : "";
Connection connection = null;
try {
connection = getConnectionRead();
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);
NamedParameterStatement stmt = new NamedParameterStatement(connection, sql);
stmt.setString(FILED_FEC_INICIO, fecInicio);
stmt.setString(FIELD_FEC_FINAL, fecFinal);
@ -140,7 +142,7 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
stmt.setInt(FIELD_EMPRESA_ID, empresa.getEmpresaId());
}
if (ruta != null && !ruta.equals("-1")) {
if (ruta != null && !"-1".equals(ruta.getRutaId().toString())) {
stmt.setInt(FIELD_RUTA_ID, ruta.getRutaId());
}
@ -152,23 +154,38 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
stmt.setInt(FIELD_ORIGEM_ID, origem.getParadaId());
}
if (!horInicio.isEmpty() && !horFim.isEmpty()){
if (!horInicio.isEmpty() && !horFim.isEmpty()) {
stmt.setString(FIELD_HOR_INICIO, horInicio);
stmt.setString(FIELD_HOR_FINAL, horFim);
}
rset = stmt.executeQuery();
Map<String, Map<String, List<RelatorioEmbarqueLocalidadeBean>>> datas = ResultSetToList(rset);
ResultSet rset = stmt.executeQuery();
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();
stmt.close();
} catch (Exception e) {
log.error("", e);
} finally {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
log.error("error fechar conexão", e);
throw new RuntimeException(e);
}
}
}
private Connection getConnectionRead() {
return DataSourceUtils.getConnection(dataSourceRead);
}
private void exportaExcel(Map<String, Map<String, List<RelatorioEmbarqueLocalidadeBean>>> datas) {
try {
HSSFWorkbook wb = new HSSFWorkbook();
@ -179,7 +196,7 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
HSSFFont font = wb.createFont();
HSSFCellStyle style = wb.createCellStyle();
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short)10);
font.setFontHeightInPoints((short) 10);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
style.setFont(font);
@ -195,18 +212,18 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
int totalGeral = 0;
int linhaTotalizador = 6;
HSSFCell cellTotal = null ;
HSSFCell cellTotal = null;
for (RelatorioEmbarqueLocalidadeBean bean : ls) {
int indexColun = mapAux.get(bean.getHorario());
if(!linhas.containsKey(bean.getLinha())) {
if (!linhas.containsKey(bean.getLinha())) {
linhas.put(bean.getLinha(), linha);
row = sheet1.createRow(linha);
linhaTotalizador = linha;
linha++;
}else {
} else {
row = sheet1.getRow(linhas.get(bean.getLinha()));
linhaTotalizador = sheet1.getRow(linhas.get(bean.getLinha())).getRowNum();
}
@ -222,10 +239,10 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
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);
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);
@ -233,32 +250,31 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
}
row = sheet1.createRow(linha);
int qtdLinhas = linhas.size()+5;
int qtdLinhas = linhas.size() + 5;
int somatorioColuna = 3;
for (int i = 3; i < mapAux.size()+3; i++) {
for (int i = 3; i < mapAux.size() + 3; i++) {
String colluna = CellReference.convertNumToColString(i);
cellTotal = row.createCell(somatorioColuna);
cellTotal.setCellFormula("SUM("+colluna+6+":"+colluna+(qtdLinhas)+")");
cellTotal.setCellFormula("SUM(" + colluna + 6 + ":" + colluna + (qtdLinhas) + ")");
cellTotal.setCellStyle(style);
somatorioColuna++;
}
HSSFCell cellTotalValue = row.createCell(total);
cellTotalValue = row.createCell(mapAux.size()+3);
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()){
if (mapAux.isEmpty()) {
Messagebox.show(
Labels.getLabel("relatorio.parametro.msgNoData"),
Labels.getLabel("relatorioEmbarqueLocalidadeController.window.title"),
@ -266,36 +282,39 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
return;
}
String nomeArquivo =
UsuarioLogado.getUsuarioLogado().getNombusuario()
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) {
stream.flush();
stream.close();
//is.close();
} catch (Exception e) {
e.printStackTrace();
}
}finally {
}
}
private void geraCabecalhoTitulo(HSSFSheet sheet1, HSSFWorkbook wb){
private void geraCabecalhoTitulo(HSSFSheet sheet1, HSSFWorkbook wb) {
HSSFRow titulo = sheet1.createRow(NUM_LINHA_TITULO);
sheet1.addMergedRegion(new CellRangeAddress(
NUM_LINHA_TITULO, //first row (0-based)
NUM_LINHA_TITULO, //last row (0-based)
0, //first column (0-based)
7 //last column (0-based)
NUM_LINHA_TITULO, // first row (0-based)
NUM_LINHA_TITULO, // last row (0-based)
0, // first column (0-based)
7 // last column (0-based)
));
HSSFFont fonte = wb.createFont();
@ -307,56 +326,53 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
titulo.setRowStyle(estilo);
}
private void geraCabecalhoPeriodo(HSSFSheet sheet1){
private void geraCabecalhoPeriodo(HSSFSheet sheet1) {
HSSFRow periodo = sheet1.createRow(NUM_LINHA_PERIODO);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sheet1.addMergedRegion(new CellRangeAddress(
NUM_LINHA_PERIODO, //first row (0-based)
NUM_LINHA_PERIODO, //last row (0-based)
0, //first column (0-based)
7 //last column (0-based)
NUM_LINHA_PERIODO, // first row (0-based)
NUM_LINHA_PERIODO, // last row (0-based)
0, // first column (0-based)
7 // last column (0-based)
));
periodo.createCell(0).setCellValue("Período: " + sdf.format(this.datInicial.getValue()) + " a " + sdf.format(this.datFinal.getValue()) );
periodo.createCell(0).setCellValue("Período: " + sdf.format(this.datInicial.getValue()) + " a " + sdf.format(this.datFinal.getValue()));
}
private void geraCabecalho(HSSFSheet sheet1, String dataAtual, HSSFWorkbook wb){
private void geraCabecalho(HSSFSheet sheet1, String dataAtual, HSSFWorkbook wb) {
geraCabecalhoTitulo(sheet1, wb);
geraCabecalhoPeriodo(sheet1);
sheet1.addMergedRegion(new CellRangeAddress(
NUM_LINHA_DATA_EMPRESA, //first row (0-based)
NUM_LINHA_DATA_EMPRESA, //last row (0-based)
1, //first column (0-based)
7 //last column (0-based)
NUM_LINHA_DATA_EMPRESA, // first row (0-based)
NUM_LINHA_DATA_EMPRESA, // last row (0-based)
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.setFontHeightInPoints((short) 10);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
style.setFont(font);
//Montando o Cabeçalho
// Montando o Cabeçalho
HSSFRow r0 = sheet1.createRow(NUM_LINHA_DATA_EMPRESA);
if(empresa != null) {
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 {
} else {
}
HSSFRow r1 = sheet1.createRow(NUM_LINHA_DATA_EMPRESA+1);
HSSFRow r1 = sheet1.createRow(NUM_LINHA_DATA_EMPRESA + 1);
List<RelatorioEmbarqueLocalidadeBean> lsAux = new ArrayList<RelatorioEmbarqueLocalidadeBean>();
mapAux = new HashMap<String, Integer>();
@ -365,12 +381,12 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
int i = 3;
for (RelatorioEmbarqueLocalidadeBean bean : ls) {
if(map.containsKey(bean.getHorario())) {
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);
} else {
map.put(bean.getHorario(), i);
bean.setIndexColun(i);
i++;
}
@ -384,13 +400,12 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
fontHorario.setBoldweight(Font.BOLDWEIGHT_BOLD);
styleHorario.setFont(fontHorario);
for (Entry<String,Integer> bean : mapAux.entrySet()) {
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;
@ -402,11 +417,12 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
lsEmpresas = empresaService.obtenerTodos();
lsOrgaosConcedentes = orgaoConcedenteService.obtenerTodos();
lsOrigens = paradaService.obtenerTodos();
lsRutas = rutaService.obtenerTodos();
super.doAfterCompose(comp);
// lsRutas = rutaService.obtenerTodos();
}
public void onChange$cmbOrgaoConcedente(Event ev) throws Exception {
@ -435,20 +451,20 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
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') ");
if (!horInicio.isEmpty() && !horFim.isEmpty()){
if (!horInicio.isEmpty() && !horFim.isEmpty()) {
sql.append(" and TO_CHAR(b.fechorviaje, 'HH24:MI:SS') between :horInicio AND :horFinal ");
}
sql.append(" and b.motivocancelacion_id is null and b.numasiento is not null and b.indstatusoperacion = 'F' ");
if (orgao != null && !orgao.isEmpty()){
if (orgao != null && !orgao.isEmpty()) {
sql.append(" and r.orgaoconcedente_id = :orgao ");
}
if (empresa != null){
if (empresa != null) {
sql.append(" and m.empresa_id = :empresa ");
}
if (ruta != null && !ruta.isEmpty() && !ruta.equals("-1")){
if (ruta != null && !ruta.isEmpty() && !ruta.equals("-1")) {
sql.append(" and b.ruta_id = :ruta ");
}
if (origem != null && !origem.isEmpty()){
if (origem != null && !origem.isEmpty()) {
sql.append(" and b.origen_id = :origem ");
}
@ -461,7 +477,7 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
return sql.toString();
}
protected Map<String, Map<String, List<RelatorioEmbarqueLocalidadeBean>>> ResultSetToList(ResultSet rset) throws SQLException {
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>();
@ -472,14 +488,16 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
bean.setLinha(rset.getString(2));
bean.setClasse(rset.getString(3));
bean.setEmbarques(rset.getLong(4));
ls.add(bean);;
ls.add(bean);
}
return datas;
}
public void checarLinhasDuplicadas() {
}
public Datebox getDatInicial() {
return datInicial;
}
@ -577,4 +595,3 @@ public class RelatorioEmbarqueLocalidadeController extends MyGenericForwardComp
}
}