Rev 264 | Rev 268 | 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 | |||
| 267 | espaco | 3 | import java.util.ArrayList; |
| 264 | espaco | 4 | import java.util.Arrays; |
| 261 | espaco | 5 | import java.util.Calendar; |
| 264 | espaco | 6 | import java.util.Date; |
| 267 | espaco | 7 | import java.util.List; |
| 261 | espaco | 8 | |
| 9 | import org.primefaces.model.charts.ChartData; |
||
| 10 | import org.primefaces.model.charts.axes.cartesian.CartesianScales; |
||
| 11 | import org.primefaces.model.charts.axes.cartesian.linear.CartesianLinearAxes; |
||
| 12 | import org.primefaces.model.charts.axes.cartesian.linear.CartesianLinearTicks; |
||
| 13 | import org.primefaces.model.charts.bar.BarChartDataSet; |
||
| 14 | import org.primefaces.model.charts.bar.BarChartModel; |
||
| 15 | import org.primefaces.model.charts.bar.BarChartOptions; |
||
| 16 | import org.primefaces.model.charts.optionconfig.legend.Legend; |
||
| 17 | import org.primefaces.model.charts.optionconfig.legend.LegendLabel; |
||
| 18 | import org.primefaces.model.charts.optionconfig.title.Title; |
||
| 267 | espaco | 19 | import org.primefaces.model.charts.pie.PieChartDataSet; |
| 20 | import org.primefaces.model.charts.pie.PieChartModel; |
||
| 261 | espaco | 21 | import org.springframework.beans.factory.annotation.Autowired; |
| 22 | import org.springframework.stereotype.Service; |
||
| 23 | |||
| 24 | import br.com.ec.core.generic.AbstractService; |
||
| 25 | import br.com.ec.core.generic.GenericRepository; |
||
| 26 | import br.com.ec.core.util.DataUtils; |
||
| 264 | espaco | 27 | import br.com.ec.core.util.VerificadorUtil; |
| 261 | espaco | 28 | import br.com.ec.core.validador.Validador; |
| 29 | import br.com.ec.domain.dto.consulta.ParametrosConsultaVendasDTO; |
||
| 30 | import br.com.ec.domain.dto.grafico.GraficoBarraDTO; |
||
| 31 | import br.com.ec.domain.dto.grafico.GraficoDTO; |
||
| 264 | espaco | 32 | import br.com.ec.domain.dto.grafico.GraficoDadoDTO; |
| 267 | espaco | 33 | import br.com.ec.domain.dto.grafico.GraficoPizzaDTO; |
| 261 | espaco | 34 | import br.com.ec.domain.service.GraficoService; |
| 35 | import br.com.ec.domain.service.VendaService; |
||
| 36 | |||
| 37 | @Service |
||
| 38 | public class GraficoServiceImpl extends AbstractService<GraficoDTO> implements GraficoService { |
||
| 39 | |||
| 40 | private VendaService vendaService; |
||
| 41 | |||
| 42 | @Autowired |
||
| 264 | espaco | 43 | public GraficoServiceImpl(Validador validador, VendaService vendaService) { |
| 261 | espaco | 44 | super(validador); |
| 45 | this.vendaService = vendaService; |
||
| 46 | } |
||
| 47 | |||
| 48 | @Override |
||
| 49 | protected GenericRepository<GraficoDTO> getRepository() { |
||
| 50 | return null; |
||
| 51 | } |
||
| 52 | |||
| 53 | @Override |
||
| 267 | espaco | 54 | public GraficoBarraDTO gerarGraficoVendas(ParametrosConsultaVendasDTO parametrosConsulta, ParametrosConsultaVendasDTO parametrosConsultaComparativo) { |
| 264 | espaco | 55 | GraficoBarraDTO graficoDTO = new GraficoBarraDTO(); |
| 56 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhPorDia()) { |
||
| 267 | espaco | 57 | graficoDTO.setDados(preencherDadosComPeriodoEhPorDia(parametrosConsulta, "rgba(75, 192, 192, 0.2)", "rgb(75, 192, 192)")); |
| 264 | espaco | 58 | graficoDTO.setTitulo("VENDAS POR DIA"); |
| 59 | } |
||
| 60 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhPorMes()) { |
||
| 267 | espaco | 61 | graficoDTO.setDados(preencherDadosComPeriodoEhPorMes(parametrosConsulta, "rgba(75, 192, 192, 0.2)", "rgb(75, 192, 192)")); |
| 264 | espaco | 62 | graficoDTO.setTitulo("VENDAS POR MÊS"); |
| 63 | } |
||
| 64 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhPorAno()) { |
||
| 267 | espaco | 65 | graficoDTO.setDados(preencherDadosComPeriodoEhPorAno(parametrosConsulta, "rgba(75, 192, 192, 0.2)", "rgb(75, 192, 192)")); |
| 264 | espaco | 66 | graficoDTO.setTitulo("VENDAS POR ANO"); |
| 67 | } |
||
| 68 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhUltimosSeisMeses() || |
||
| 69 | parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhUltimosDozeMeses()) { |
||
| 267 | espaco | 70 | graficoDTO.setDados(preencherDadosComPeriodoEhUltimosSeisOuDozeMeses(parametrosConsulta, "rgba(75, 192, 192, 0.2)", "rgb(75, 192, 192)")); |
| 264 | espaco | 71 | graficoDTO.setTitulo("VENDAS PELOS ÚLTIMOS MESES"); |
| 72 | /* |
||
| 73 | if (VerificadorUtil.naoEstaNulo(parametrosRelatorioVendas.getVendedor())) { |
||
| 74 | consultarHistoricoPercentual(parametrosRelatorioVendas); |
||
| 75 | } |
||
| 76 | */ |
||
| 77 | } |
||
| 78 | |||
| 261 | espaco | 79 | BarChartDataSet barDataSet = new BarChartDataSet(); |
| 80 | barDataSet.setLabel(""); |
||
| 264 | espaco | 81 | barDataSet.setData(graficoDTO.getDados().getValores()); |
| 82 | barDataSet.setBackgroundColor(graficoDTO.getDados().getCores()); |
||
| 83 | barDataSet.setBorderColor(graficoDTO.getDados().getBordas()); |
||
| 261 | espaco | 84 | barDataSet.setBorderWidth(1); |
| 267 | espaco | 85 | |
| 264 | espaco | 86 | ChartData dados = new ChartData(); |
| 267 | espaco | 87 | dados.setLabels(graficoDTO.getDados().getMarcadores()); |
| 88 | |||
| 89 | if (VerificadorUtil.naoEstaNulo(parametrosConsultaComparativo)) { |
||
| 90 | parametrosConsultaComparativo.setFormasDePagamentoDTO(parametrosConsulta.getFormasDePagamentoDTO()); |
||
| 91 | parametrosConsultaComparativo.setLojasSelecionadasDTO(parametrosConsulta.getLojasSelecionadasDTO()); |
||
| 92 | parametrosConsultaComparativo.getParametrosPeriodoDTO().setTipoPeriodo(parametrosConsulta.getParametrosPeriodoDTO().getTipoPeriodo()); |
||
| 93 | GraficoBarraDTO graficoDTOComparativo = new GraficoBarraDTO(); |
||
| 94 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhPorDia()) { |
||
| 95 | graficoDTOComparativo.setDados(preencherDadosComPeriodoEhPorDia(parametrosConsultaComparativo, "rgba(255, 205, 86, 0.2)", "rgb(255, 205, 86)")); |
||
| 96 | graficoDTOComparativo.setTitulo("VENDAS POR DIA"); |
||
| 97 | } |
||
| 98 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhPorMes()) { |
||
| 99 | graficoDTOComparativo.setDados(preencherDadosComPeriodoEhPorMes(parametrosConsultaComparativo, "rgba(255, 205, 86, 0.2)", "rgb(255, 205, 86)")); |
||
| 100 | graficoDTOComparativo.setTitulo("VENDAS POR MÊS"); |
||
| 101 | } |
||
| 102 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhPorAno()) { |
||
| 103 | graficoDTOComparativo.setDados(preencherDadosComPeriodoEhPorAno(parametrosConsultaComparativo, "rgba(255, 205, 86, 0.2)", "rgb(255, 205, 86)")); |
||
| 104 | graficoDTOComparativo.setTitulo("VENDAS POR ANO"); |
||
| 105 | } |
||
| 106 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhUltimosSeisMeses() || |
||
| 107 | parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhUltimosDozeMeses()) { |
||
| 108 | graficoDTOComparativo.setDados(preencherDadosComPeriodoEhUltimosSeisOuDozeMeses(parametrosConsultaComparativo, "rgba(255, 205, 86, 0.2)", "rgb(255, 205, 86)")); |
||
| 109 | graficoDTOComparativo.setTitulo("VENDAS PELOS ÚLTIMOS MESES"); |
||
| 110 | } |
||
| 111 | BarChartDataSet barDataSetComparativo = new BarChartDataSet(); |
||
| 112 | barDataSetComparativo.setLabel("COMPARATIVO"); |
||
| 113 | barDataSetComparativo.setData(graficoDTOComparativo.getDados().getValores()); |
||
| 114 | barDataSetComparativo.setBackgroundColor(graficoDTOComparativo.getDados().getCores()); |
||
| 115 | barDataSetComparativo.setBorderColor(graficoDTOComparativo.getDados().getBordas()); |
||
| 116 | barDataSetComparativo.setBorderWidth(1); |
||
| 117 | |||
| 118 | dados.addChartDataSet(barDataSetComparativo); |
||
| 119 | } |
||
| 120 | |||
| 264 | espaco | 121 | dados.addChartDataSet(barDataSet); |
| 122 | |||
| 261 | espaco | 123 | BarChartOptions options = new BarChartOptions(); |
| 124 | CartesianScales cScales = new CartesianScales(); |
||
| 125 | CartesianLinearAxes linearAxes = new CartesianLinearAxes(); |
||
| 126 | linearAxes.setOffset(true); |
||
| 127 | CartesianLinearTicks ticks = new CartesianLinearTicks(); |
||
| 128 | ticks.setBeginAtZero(true); |
||
| 129 | linearAxes.setTicks(ticks); |
||
| 130 | cScales.setDisplay(true); |
||
| 131 | cScales.addYAxesData(linearAxes); |
||
| 132 | options.setScales(cScales); |
||
| 263 | espaco | 133 | |
| 264 | espaco | 134 | if (VerificadorUtil.naoEstaNuloOuVazio(graficoDTO.getTitulo())) { |
| 135 | Title title = new Title(); |
||
| 136 | title.setDisplay(true); |
||
| 137 | title.setText(graficoDTO.getTitulo()); |
||
| 138 | options.setTitle(title); |
||
| 139 | } |
||
| 261 | espaco | 140 | |
| 141 | Legend legend = new Legend(); |
||
| 142 | legend.setDisplay(true); |
||
| 143 | legend.setPosition("top"); |
||
| 263 | espaco | 144 | |
| 261 | espaco | 145 | LegendLabel legendLabels = new LegendLabel(); |
| 146 | legendLabels.setFontStyle("bold"); |
||
| 147 | legendLabels.setFontColor("#2980B9"); |
||
| 148 | legendLabels.setFontSize(24); |
||
| 149 | legend.setLabels(legendLabels); |
||
| 150 | options.setLegend(legend); |
||
| 151 | |||
| 264 | espaco | 152 | BarChartModel graficoBarra = new BarChartModel(); |
| 153 | graficoBarra.setData(dados); |
||
| 261 | espaco | 154 | graficoBarra.setOptions(options); |
| 263 | espaco | 155 | graficoBarra.setExtender("barra"); |
| 264 | espaco | 156 | |
| 157 | graficoDTO.setGraficoBarra(graficoBarra); |
||
| 158 | return graficoDTO; |
||
| 261 | espaco | 159 | } |
| 264 | espaco | 160 | |
| 267 | espaco | 161 | private GraficoDadoDTO preencherDadosComPeriodoEhPorDia(ParametrosConsultaVendasDTO parametrosConsulta, String cor, String corBorda) { |
| 264 | espaco | 162 | GraficoDadoDTO dados = new GraficoDadoDTO(); |
| 163 | Calendar dataInicial = DataUtils.obterCalendario(DataUtils.obterData(1, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getMes())-1, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno()))); |
||
| 164 | int qntMaximaDias = dataInicial.getActualMaximum(Calendar.DAY_OF_MONTH); |
||
| 165 | for (int dia = 1; dia <= qntMaximaDias; dia++) { |
||
| 166 | Double valorAcumulado = new Double(0.0); |
||
| 167 | Date data = DataUtils.obterData(dia, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getMes())-1, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno())); |
||
| 168 | parametrosConsulta.getParametrosPeriodoDTO().setDataInicial(data); |
||
| 169 | parametrosConsulta.getParametrosPeriodoDTO().setDataFinal(data); |
||
| 170 | valorAcumulado = vendaService.obterValorTotalBrutoDeVendasPorLojaEePeriodoEeFormasDePagamento(parametrosConsulta, Arrays.asList(parametrosConsulta.getFormasDePagamentoDTO())); |
||
| 267 | espaco | 171 | dados.adicionarDados(valorAcumulado, cor, corBorda, "" + dia); |
| 264 | espaco | 172 | } |
| 173 | return dados; |
||
| 174 | } |
||
| 175 | |||
| 267 | espaco | 176 | private GraficoDadoDTO preencherDadosComPeriodoEhPorMes(ParametrosConsultaVendasDTO parametrosConsulta, String cor, String corBorda) { |
| 264 | espaco | 177 | GraficoDadoDTO dados = new GraficoDadoDTO(); |
| 178 | for (int mes = 1; mes <= 12; mes++) { |
||
| 179 | Double valorAcumulado = new Double(0.0); |
||
| 180 | Calendar dataInicial = DataUtils.obterCalendario(DataUtils.obterData(1, new Integer(mes)-1, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno()))); |
||
| 181 | int qntMaximaDias = dataInicial.getActualMaximum(Calendar.DAY_OF_MONTH); |
||
| 182 | Calendar dataFinal = DataUtils.obterCalendario(DataUtils.obterData(qntMaximaDias, new Integer(mes)-1, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno()))); |
||
| 183 | parametrosConsulta.getParametrosPeriodoDTO().setDataInicial(dataInicial.getTime()); |
||
| 184 | parametrosConsulta.getParametrosPeriodoDTO().setDataFinal(dataFinal.getTime()); |
||
| 185 | valorAcumulado = vendaService.obterValorTotalBrutoDeVendasPorLojaEePeriodoEeFormasDePagamento( |
||
| 186 | parametrosConsulta, Arrays.asList(parametrosConsulta.getFormasDePagamentoDTO())); |
||
| 267 | espaco | 187 | dados.adicionarDados(valorAcumulado, cor, corBorda, "" + mes); |
| 264 | espaco | 188 | } |
| 189 | return dados; |
||
| 190 | } |
||
| 261 | espaco | 191 | |
| 267 | espaco | 192 | private GraficoDadoDTO preencherDadosComPeriodoEhPorAno(ParametrosConsultaVendasDTO parametrosConsulta, String cor, String corBorda) { |
| 264 | espaco | 193 | GraficoDadoDTO dados = new GraficoDadoDTO(); |
| 194 | Integer anoInicial = new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno()) - 10; |
||
| 195 | for (int ano = anoInicial; ano <= new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno()); ano++) { |
||
| 196 | Double valorAcumulado = new Double(0.0); |
||
| 197 | Calendar dataInicial = DataUtils.obterCalendario(DataUtils.obterData(1, 0, new Integer(ano))); |
||
| 198 | Calendar dataFinal = DataUtils.obterCalendario(DataUtils.obterData(31, 13, new Integer(ano))); |
||
| 199 | parametrosConsulta.getParametrosPeriodoDTO().setDataInicial(dataInicial.getTime()); |
||
| 200 | parametrosConsulta.getParametrosPeriodoDTO().setDataFinal(dataFinal.getTime()); |
||
| 201 | valorAcumulado = vendaService.obterValorTotalBrutoDeVendasPorLojaEePeriodoEeFormasDePagamento(parametrosConsulta, Arrays.asList(parametrosConsulta.getFormasDePagamentoDTO())); |
||
| 267 | espaco | 202 | dados.adicionarDados(valorAcumulado, cor, corBorda, "" + ano); |
| 264 | espaco | 203 | } |
| 204 | return dados; |
||
| 205 | } |
||
| 206 | |||
| 267 | espaco | 207 | private GraficoDadoDTO preencherDadosComPeriodoEhUltimosSeisOuDozeMeses(ParametrosConsultaVendasDTO parametrosConsulta, String cor, String corBorda) { |
| 264 | espaco | 208 | GraficoDadoDTO dados = new GraficoDadoDTO(); |
| 209 | Integer mesAtual = new Integer(parametrosConsulta.getParametrosPeriodoDTO().getMes()); |
||
| 210 | Integer anoAtual = new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno()); |
||
| 211 | Integer quantidadeMesesAnteriores = 0; |
||
| 212 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhUltimosSeisMeses()) { |
||
| 213 | quantidadeMesesAnteriores = 7; |
||
| 214 | } |
||
| 215 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhUltimosDozeMeses()) { |
||
| 216 | quantidadeMesesAnteriores = 13; |
||
| 217 | } |
||
| 218 | |||
| 219 | mesAtual = mesAtual - quantidadeMesesAnteriores + 1; |
||
| 220 | if (mesAtual <= 0) { |
||
| 221 | mesAtual = mesAtual + 12; |
||
| 222 | anoAtual--; |
||
| 223 | } |
||
| 224 | for (int mes = 1; mes <= quantidadeMesesAnteriores; quantidadeMesesAnteriores--) { |
||
| 225 | if (mesAtual == 13) { |
||
| 226 | mesAtual = 1; |
||
| 227 | anoAtual++; |
||
| 228 | } |
||
| 229 | Double valorAcumulado = new Double(0.0); |
||
| 230 | Calendar dataInicial = DataUtils.obterCalendario(DataUtils.obterData(1, mesAtual - 1, anoAtual)); |
||
| 231 | parametrosConsulta.getParametrosPeriodoDTO().setDataInicial(dataInicial.getTime()); |
||
| 232 | int qntMaximaDias = dataInicial.getActualMaximum(Calendar.DAY_OF_MONTH); |
||
| 233 | parametrosConsulta.getParametrosPeriodoDTO().setDataFinal(DataUtils.obterCalendario(DataUtils.obterData(qntMaximaDias, mesAtual - 1, anoAtual)).getTime()); |
||
| 234 | valorAcumulado = vendaService.obterValorTotalBrutoDeVendasPorLojaEePeriodoEeFormasDePagamento(parametrosConsulta, Arrays.asList(parametrosConsulta.getFormasDePagamentoDTO())); |
||
| 235 | String descricaoLinha = mesAtual.toString() + "/" + anoAtual; |
||
| 236 | mesAtual++; |
||
| 267 | espaco | 237 | dados.adicionarDados(valorAcumulado, cor, corBorda, "" + descricaoLinha); |
| 264 | espaco | 238 | } |
| 239 | return dados; |
||
| 240 | } |
||
| 241 | |||
| 267 | espaco | 242 | @Override |
| 243 | public GraficoPizzaDTO gerarGraficoVendasPorFormasDePagamento(ParametrosConsultaVendasDTO parametrosConsulta) { |
||
| 244 | GraficoBarraDTO graficoDTO = new GraficoBarraDTO(); |
||
| 245 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhPorDia()) { |
||
| 246 | graficoDTO.setDados(preencherDadosComPeriodoEhPorDia(parametrosConsulta, "rgba(75, 192, 192, 0.2)", "rgb(75, 192, 192)")); |
||
| 247 | graficoDTO.setTitulo("VENDAS POR DIA"); |
||
| 248 | } |
||
| 249 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhPorMes()) { |
||
| 250 | graficoDTO.setDados(preencherDadosComPeriodoEhPorMes(parametrosConsulta, "rgba(75, 192, 192, 0.2)", "rgb(75, 192, 192)")); |
||
| 251 | graficoDTO.setTitulo("VENDAS POR MÊS"); |
||
| 252 | } |
||
| 253 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhPorAno()) { |
||
| 254 | graficoDTO.setDados(preencherDadosComPeriodoEhPorAno(parametrosConsulta, "rgba(75, 192, 192, 0.2)", "rgb(75, 192, 192)")); |
||
| 255 | graficoDTO.setTitulo("VENDAS POR ANO"); |
||
| 256 | } |
||
| 257 | if (parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhUltimosSeisMeses() || |
||
| 258 | parametrosConsulta.getParametrosPeriodoDTO().tipoPeriodoEhUltimosDozeMeses()) { |
||
| 259 | graficoDTO.setDados(preencherDadosComPeriodoEhUltimosSeisOuDozeMeses(parametrosConsulta, "rgba(75, 192, 192, 0.2)", "rgb(75, 192, 192)")); |
||
| 260 | graficoDTO.setTitulo("VENDAS PELOS ÚLTIMOS MESES"); |
||
| 261 | } |
||
| 262 | |||
| 263 | PieChartModel graficoFormasPagamento = new PieChartModel(); |
||
| 264 | ChartData dataFP = new ChartData(); |
||
| 265 | PieChartDataSet dataSetFP = new PieChartDataSet(); |
||
| 266 | List<Number> valuesFP = new ArrayList<>(); |
||
| 267 | valuesFP.add(300); |
||
| 268 | valuesFP.add(50); |
||
| 269 | valuesFP.add(100); |
||
| 270 | dataSetFP.setData(valuesFP); |
||
| 271 | List<String> bgColorsFP = new ArrayList<>(); |
||
| 272 | bgColorsFP.add("rgb(255, 99, 132)"); |
||
| 273 | bgColorsFP.add("rgb(54, 162, 235)"); |
||
| 274 | bgColorsFP.add("rgb(255, 205, 86)"); |
||
| 275 | dataSetFP.setBackgroundColor(bgColorsFP); |
||
| 276 | dataFP.addChartDataSet(dataSetFP); |
||
| 277 | List<String> labelsFP = new ArrayList<>(); |
||
| 278 | labelsFP.add("DINHEIRO"); |
||
| 279 | labelsFP.add("DÉBITO"); |
||
| 280 | labelsFP.add("CARTÃO À VISTA"); |
||
| 281 | labelsFP.add("PARCELADO"); |
||
| 282 | dataFP.setLabels(labelsFP); |
||
| 283 | graficoFormasPagamento.setData(dataFP); |
||
| 284 | |||
| 285 | GraficoPizzaDTO grafico = new GraficoPizzaDTO(); |
||
| 286 | grafico.setGraficoPizza(graficoFormasPagamento); |
||
| 287 | |||
| 288 | return grafico; |
||
| 289 | } |
||
| 261 | espaco | 290 | } |