Rev 263 | Rev 267 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 261 | espaco | 1 | package br.com.ec.domain.service.impl; |
| 2 | |||
| 264 | espaco | 3 | import java.util.Arrays; |
| 261 | espaco | 4 | import java.util.Calendar; |
| 264 | espaco | 5 | import java.util.Date; |
| 261 | espaco | 6 | |
| 7 | import org.primefaces.model.charts.ChartData; |
||
| 8 | import org.primefaces.model.charts.axes.cartesian.CartesianScales; |
||
| 9 | import org.primefaces.model.charts.axes.cartesian.linear.CartesianLinearAxes; |
||
| 10 | import org.primefaces.model.charts.axes.cartesian.linear.CartesianLinearTicks; |
||
| 11 | import org.primefaces.model.charts.bar.BarChartDataSet; |
||
| 12 | import org.primefaces.model.charts.bar.BarChartModel; |
||
| 13 | import org.primefaces.model.charts.bar.BarChartOptions; |
||
| 14 | import org.primefaces.model.charts.optionconfig.legend.Legend; |
||
| 15 | import org.primefaces.model.charts.optionconfig.legend.LegendLabel; |
||
| 16 | import org.primefaces.model.charts.optionconfig.title.Title; |
||
| 17 | import org.springframework.beans.factory.annotation.Autowired; |
||
| 18 | import org.springframework.stereotype.Service; |
||
| 19 | |||
| 20 | import br.com.ec.core.generic.AbstractService; |
||
| 21 | import br.com.ec.core.generic.GenericRepository; |
||
| 22 | import br.com.ec.core.util.DataUtils; |
||
| 264 | espaco | 23 | import br.com.ec.core.util.VerificadorUtil; |
| 261 | espaco | 24 | import br.com.ec.core.validador.Validador; |
| 25 | import br.com.ec.domain.dto.consulta.ParametrosConsultaVendasDTO; |
||
| 26 | import br.com.ec.domain.dto.grafico.GraficoBarraDTO; |
||
| 27 | import br.com.ec.domain.dto.grafico.GraficoDTO; |
||
| 264 | espaco | 28 | import br.com.ec.domain.dto.grafico.GraficoDadoDTO; |
| 261 | espaco | 29 | import br.com.ec.domain.service.GraficoService; |
| 30 | import br.com.ec.domain.service.VendaService; |
||
| 31 | |||
| 32 | @Service |
||
| 33 | public class GraficoServiceImpl extends AbstractService<GraficoDTO> implements GraficoService { |
||
| 34 | |||
| 35 | private VendaService vendaService; |
||
| 36 | |||
| 37 | @Autowired |
||
| 264 | espaco | 38 | public GraficoServiceImpl(Validador validador, VendaService vendaService) { |
| 261 | espaco | 39 | super(validador); |
| 40 | this.vendaService = vendaService; |
||
| 41 | } |
||
| 42 | |||
| 43 | @Override |
||
| 44 | protected GenericRepository<GraficoDTO> getRepository() { |
||
| 45 | return null; |
||
| 46 | } |
||
| 47 | |||
| 48 | @Override |
||
| 49 | public GraficoBarraDTO gerarGraficoVendas(ParametrosConsultaVendasDTO parametrosConsulta) { |
||
| 264 | espaco | 50 | GraficoBarraDTO graficoDTO = new GraficoBarraDTO(); |
| 51 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhPorDia()) { |
||
| 52 | graficoDTO.setDados(preencherDadosComPeriodoEhPorDia(parametrosConsulta)); |
||
| 53 | graficoDTO.setTitulo("VENDAS POR DIA"); |
||
| 54 | } |
||
| 55 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhPorMes()) { |
||
| 56 | graficoDTO.setDados(preencherDadosComPeriodoEhPorMes(parametrosConsulta)); |
||
| 57 | graficoDTO.setTitulo("VENDAS POR MÊS"); |
||
| 58 | } |
||
| 59 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhPorAno()) { |
||
| 60 | graficoDTO.setDados(preencherDadosComPeriodoEhPorAno(parametrosConsulta)); |
||
| 61 | graficoDTO.setTitulo("VENDAS POR ANO"); |
||
| 62 | } |
||
| 63 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhUltimosSeisMeses() || |
||
| 64 | parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhUltimosDozeMeses()) { |
||
| 65 | graficoDTO.setDados(preencherDadosComPeriodoEhUltimosSeisOuDozeMeses(parametrosConsulta)); |
||
| 66 | graficoDTO.setTitulo("VENDAS PELOS ÚLTIMOS MESES"); |
||
| 67 | /* |
||
| 68 | if (VerificadorUtil.naoEstaNulo(parametrosRelatorioVendas.getVendedor())) { |
||
| 69 | consultarHistoricoPercentual(parametrosRelatorioVendas); |
||
| 70 | } |
||
| 71 | */ |
||
| 72 | } |
||
| 73 | |||
| 261 | espaco | 74 | BarChartDataSet barDataSet = new BarChartDataSet(); |
| 75 | barDataSet.setLabel(""); |
||
| 264 | espaco | 76 | barDataSet.setData(graficoDTO.getDados().getValores()); |
| 77 | barDataSet.setBackgroundColor(graficoDTO.getDados().getCores()); |
||
| 78 | barDataSet.setBorderColor(graficoDTO.getDados().getBordas()); |
||
| 261 | espaco | 79 | barDataSet.setBorderWidth(1); |
| 264 | espaco | 80 | ChartData dados = new ChartData(); |
| 81 | dados.addChartDataSet(barDataSet); |
||
| 82 | dados.setLabels(graficoDTO.getDados().getMarcadores()); |
||
| 83 | |||
| 261 | espaco | 84 | BarChartOptions options = new BarChartOptions(); |
| 85 | CartesianScales cScales = new CartesianScales(); |
||
| 86 | CartesianLinearAxes linearAxes = new CartesianLinearAxes(); |
||
| 87 | linearAxes.setOffset(true); |
||
| 88 | CartesianLinearTicks ticks = new CartesianLinearTicks(); |
||
| 89 | ticks.setBeginAtZero(true); |
||
| 90 | linearAxes.setTicks(ticks); |
||
| 91 | cScales.setDisplay(true); |
||
| 92 | cScales.addYAxesData(linearAxes); |
||
| 93 | options.setScales(cScales); |
||
| 263 | espaco | 94 | |
| 264 | espaco | 95 | if (VerificadorUtil.naoEstaNuloOuVazio(graficoDTO.getTitulo())) { |
| 96 | Title title = new Title(); |
||
| 97 | title.setDisplay(true); |
||
| 98 | title.setText(graficoDTO.getTitulo()); |
||
| 99 | options.setTitle(title); |
||
| 100 | } |
||
| 261 | espaco | 101 | |
| 102 | Legend legend = new Legend(); |
||
| 103 | legend.setDisplay(true); |
||
| 104 | legend.setPosition("top"); |
||
| 263 | espaco | 105 | |
| 261 | espaco | 106 | LegendLabel legendLabels = new LegendLabel(); |
| 107 | legendLabels.setFontStyle("bold"); |
||
| 108 | legendLabels.setFontColor("#2980B9"); |
||
| 109 | legendLabels.setFontSize(24); |
||
| 110 | legend.setLabels(legendLabels); |
||
| 111 | options.setLegend(legend); |
||
| 112 | |||
| 264 | espaco | 113 | BarChartModel graficoBarra = new BarChartModel(); |
| 114 | graficoBarra.setData(dados); |
||
| 261 | espaco | 115 | graficoBarra.setOptions(options); |
| 263 | espaco | 116 | graficoBarra.setExtender("barra"); |
| 264 | espaco | 117 | |
| 118 | graficoDTO.setGraficoBarra(graficoBarra); |
||
| 119 | return graficoDTO; |
||
| 261 | espaco | 120 | } |
| 264 | espaco | 121 | |
| 122 | private GraficoDadoDTO preencherDadosComPeriodoEhPorDia(ParametrosConsultaVendasDTO parametrosConsulta) { |
||
| 123 | GraficoDadoDTO dados = new GraficoDadoDTO(); |
||
| 124 | Calendar dataInicial = DataUtils.obterCalendario(DataUtils.obterData(1, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getMes())-1, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno()))); |
||
| 125 | int qntMaximaDias = dataInicial.getActualMaximum(Calendar.DAY_OF_MONTH); |
||
| 126 | for (int dia = 1; dia <= qntMaximaDias; dia++) { |
||
| 127 | Double valorAcumulado = new Double(0.0); |
||
| 128 | Date data = DataUtils.obterData(dia, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getMes())-1, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno())); |
||
| 129 | parametrosConsulta.getParametrosPeriodoDTO().setDataInicial(data); |
||
| 130 | parametrosConsulta.getParametrosPeriodoDTO().setDataFinal(data); |
||
| 131 | valorAcumulado = vendaService.obterValorTotalBrutoDeVendasPorLojaEePeriodoEeFormasDePagamento(parametrosConsulta, Arrays.asList(parametrosConsulta.getFormasDePagamentoDTO())); |
||
| 132 | dados.adicionarDados(valorAcumulado, "rgba(75, 192, 192, 0.2)", "rgb(75, 192, 192)", "" + dia); |
||
| 133 | } |
||
| 134 | return dados; |
||
| 135 | } |
||
| 136 | |||
| 137 | private GraficoDadoDTO preencherDadosComPeriodoEhPorMes(ParametrosConsultaVendasDTO parametrosConsulta) { |
||
| 138 | GraficoDadoDTO dados = new GraficoDadoDTO(); |
||
| 139 | for (int mes = 1; mes <= 12; mes++) { |
||
| 140 | Double valorAcumulado = new Double(0.0); |
||
| 141 | Calendar dataInicial = DataUtils.obterCalendario(DataUtils.obterData(1, new Integer(mes)-1, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno()))); |
||
| 142 | int qntMaximaDias = dataInicial.getActualMaximum(Calendar.DAY_OF_MONTH); |
||
| 143 | Calendar dataFinal = DataUtils.obterCalendario(DataUtils.obterData(qntMaximaDias, new Integer(mes)-1, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno()))); |
||
| 144 | parametrosConsulta.getParametrosPeriodoDTO().setDataInicial(dataInicial.getTime()); |
||
| 145 | parametrosConsulta.getParametrosPeriodoDTO().setDataFinal(dataFinal.getTime()); |
||
| 146 | valorAcumulado = vendaService.obterValorTotalBrutoDeVendasPorLojaEePeriodoEeFormasDePagamento( |
||
| 147 | parametrosConsulta, Arrays.asList(parametrosConsulta.getFormasDePagamentoDTO())); |
||
| 148 | dados.adicionarDados(valorAcumulado, "rgba(75, 192, 192, 0.2)", "rgb(75, 192, 192)", "" + mes); |
||
| 149 | } |
||
| 150 | return dados; |
||
| 151 | } |
||
| 261 | espaco | 152 | |
| 264 | espaco | 153 | private GraficoDadoDTO preencherDadosComPeriodoEhPorAno(ParametrosConsultaVendasDTO parametrosConsulta) { |
| 154 | GraficoDadoDTO dados = new GraficoDadoDTO(); |
||
| 155 | Integer anoInicial = new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno()) - 10; |
||
| 156 | for (int ano = anoInicial; ano <= new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno()); ano++) { |
||
| 157 | Double valorAcumulado = new Double(0.0); |
||
| 158 | Calendar dataInicial = DataUtils.obterCalendario(DataUtils.obterData(1, 0, new Integer(ano))); |
||
| 159 | Calendar dataFinal = DataUtils.obterCalendario(DataUtils.obterData(31, 13, new Integer(ano))); |
||
| 160 | parametrosConsulta.getParametrosPeriodoDTO().setDataInicial(dataInicial.getTime()); |
||
| 161 | parametrosConsulta.getParametrosPeriodoDTO().setDataFinal(dataFinal.getTime()); |
||
| 162 | valorAcumulado = vendaService.obterValorTotalBrutoDeVendasPorLojaEePeriodoEeFormasDePagamento(parametrosConsulta, Arrays.asList(parametrosConsulta.getFormasDePagamentoDTO())); |
||
| 163 | dados.adicionarDados(valorAcumulado, "rgba(75, 192, 192, 0.2)", "rgb(75, 192, 192)", "" + ano); |
||
| 164 | } |
||
| 165 | return dados; |
||
| 166 | } |
||
| 167 | |||
| 168 | private GraficoDadoDTO preencherDadosComPeriodoEhUltimosSeisOuDozeMeses(ParametrosConsultaVendasDTO parametrosConsulta) { |
||
| 169 | GraficoDadoDTO dados = new GraficoDadoDTO(); |
||
| 170 | Integer mesAtual = new Integer(parametrosConsulta.getParametrosPeriodoDTO().getMes()); |
||
| 171 | Integer anoAtual = new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno()); |
||
| 172 | Integer quantidadeMesesAnteriores = 0; |
||
| 173 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhUltimosSeisMeses()) { |
||
| 174 | quantidadeMesesAnteriores = 7; |
||
| 175 | } |
||
| 176 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhUltimosDozeMeses()) { |
||
| 177 | quantidadeMesesAnteriores = 13; |
||
| 178 | } |
||
| 179 | |||
| 180 | mesAtual = mesAtual - quantidadeMesesAnteriores + 1; |
||
| 181 | if (mesAtual <= 0) { |
||
| 182 | mesAtual = mesAtual + 12; |
||
| 183 | anoAtual--; |
||
| 184 | } |
||
| 185 | for (int mes = 1; mes <= quantidadeMesesAnteriores; quantidadeMesesAnteriores--) { |
||
| 186 | if (mesAtual == 13) { |
||
| 187 | mesAtual = 1; |
||
| 188 | anoAtual++; |
||
| 189 | } |
||
| 190 | Double valorAcumulado = new Double(0.0); |
||
| 191 | Calendar dataInicial = DataUtils.obterCalendario(DataUtils.obterData(1, mesAtual - 1, anoAtual)); |
||
| 192 | parametrosConsulta.getParametrosPeriodoDTO().setDataInicial(dataInicial.getTime()); |
||
| 193 | int qntMaximaDias = dataInicial.getActualMaximum(Calendar.DAY_OF_MONTH); |
||
| 194 | parametrosConsulta.getParametrosPeriodoDTO().setDataFinal(DataUtils.obterCalendario(DataUtils.obterData(qntMaximaDias, mesAtual - 1, anoAtual)).getTime()); |
||
| 195 | valorAcumulado = vendaService.obterValorTotalBrutoDeVendasPorLojaEePeriodoEeFormasDePagamento(parametrosConsulta, Arrays.asList(parametrosConsulta.getFormasDePagamentoDTO())); |
||
| 196 | String descricaoLinha = mesAtual.toString() + "/" + anoAtual; |
||
| 197 | mesAtual++; |
||
| 198 | dados.adicionarDados(valorAcumulado, "rgba(75, 192, 192, 0.2)", "rgb(75, 192, 192)", "" + descricaoLinha); |
||
| 199 | } |
||
| 200 | return dados; |
||
| 201 | } |
||
| 202 | |||
| 261 | espaco | 203 | } |