Subversion Repositories Integrator Subversion

Rev

Rev 263 | Rev 267 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 263 Rev 264
Line 1... Line 1...
1
package br.com.ec.domain.service.impl;
1
package br.com.ec.domain.service.impl;
2
2
3
import java.util.ArrayList;
-
 
-
 
3
import java.util.Arrays;
4
import java.util.Calendar;
4
import java.util.Calendar;
5
import java.util.List;
-
 
-
 
5
import java.util.Date;
6
6
7
import org.primefaces.model.charts.ChartData;
7
import org.primefaces.model.charts.ChartData;
8
import org.primefaces.model.charts.axes.cartesian.CartesianScales;
8
import org.primefaces.model.charts.axes.cartesian.CartesianScales;
9
import org.primefaces.model.charts.axes.cartesian.linear.CartesianLinearAxes;
9
import org.primefaces.model.charts.axes.cartesian.linear.CartesianLinearAxes;
10
import org.primefaces.model.charts.axes.cartesian.linear.CartesianLinearTicks;
10
import org.primefaces.model.charts.axes.cartesian.linear.CartesianLinearTicks;
11
import org.primefaces.model.charts.bar.BarChartDataSet;
11
import org.primefaces.model.charts.bar.BarChartDataSet;
12
import org.primefaces.model.charts.bar.BarChartModel;
12
import org.primefaces.model.charts.bar.BarChartModel;
13
import org.primefaces.model.charts.bar.BarChartOptions;
13
import org.primefaces.model.charts.bar.BarChartOptions;
14
import org.primefaces.model.charts.optionconfig.animation.Animation;
-
 
15
import org.primefaces.model.charts.optionconfig.legend.Legend;
14
import org.primefaces.model.charts.optionconfig.legend.Legend;
16
import org.primefaces.model.charts.optionconfig.legend.LegendLabel;
15
import org.primefaces.model.charts.optionconfig.legend.LegendLabel;
17
import org.primefaces.model.charts.optionconfig.title.Title;
16
import org.primefaces.model.charts.optionconfig.title.Title;
18
import org.primefaces.model.charts.optionconfig.tooltip.Tooltip;
-
 
19
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.stereotype.Service;
18
import org.springframework.stereotype.Service;
21
19
22
import br.com.ec.core.generic.AbstractService;
20
import br.com.ec.core.generic.AbstractService;
23
import br.com.ec.core.generic.GenericRepository;
21
import br.com.ec.core.generic.GenericRepository;
24
import br.com.ec.core.util.DataUtils;
22
import br.com.ec.core.util.DataUtils;
-
 
23
import br.com.ec.core.util.VerificadorUtil;
25
import br.com.ec.core.validador.Validador;
24
import br.com.ec.core.validador.Validador;
26
import br.com.ec.domain.dto.consulta.ParametrosConsultaVendasDTO;
25
import br.com.ec.domain.dto.consulta.ParametrosConsultaVendasDTO;
27
import br.com.ec.domain.dto.grafico.GraficoBarraDTO;
26
import br.com.ec.domain.dto.grafico.GraficoBarraDTO;
28
import br.com.ec.domain.dto.grafico.GraficoDTO;
27
import br.com.ec.domain.dto.grafico.GraficoDTO;
-
 
28
import br.com.ec.domain.dto.grafico.GraficoDadoDTO;
29
import br.com.ec.domain.service.GraficoService;
29
import br.com.ec.domain.service.GraficoService;
30
import br.com.ec.domain.service.VendaService;
30
import br.com.ec.domain.service.VendaService;
31
31
32
@Service
32
@Service
33
public class GraficoServiceImpl extends AbstractService<GraficoDTO> implements GraficoService {
33
public class GraficoServiceImpl extends AbstractService<GraficoDTO> implements GraficoService {
34
       
-
 
35
//      private DashboardRepository dashboardRepository;
-
 
36
       
34
       
37
        private VendaService vendaService;
35
        private VendaService vendaService;
38
36
39
        @Autowired
37
        @Autowired
40
        public GraficoServiceImpl(Validador validador, VendaService vendaService/*, DashboardRepository dashboardRepository*/) {
-
 
-
 
38
        public GraficoServiceImpl(Validador validador, VendaService vendaService) {
41
                super(validador);
39
                super(validador);
42
                this.vendaService = vendaService;
40
                this.vendaService = vendaService;
43
//              this.dashboardRepository = dashboardRepository;
-
 
44
        }
41
        }
45
42
46
        @Override
43
        @Override
47
        protected GenericRepository<GraficoDTO> getRepository() {
44
        protected GenericRepository<GraficoDTO> getRepository() {
48
//              return dashboardRepository;
-
 
49
                return null;
45
                return null;
50
        }
46
        }
51
       
47
       
52
        @Override
48
        @Override
53
        public GraficoBarraDTO gerarGraficoVendas(ParametrosConsultaVendasDTO parametrosConsulta) {
49
        public GraficoBarraDTO gerarGraficoVendas(ParametrosConsultaVendasDTO parametrosConsulta) {
54
                GraficoBarraDTO graficoLinhaDTO = new GraficoBarraDTO();
-
 
55
                BarChartModel graficoBarra = new BarChartModel();
-
 
56
        ChartData data = new ChartData();
-
 
-
 
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
               
57
        BarChartDataSet barDataSet = new BarChartDataSet();
74
        BarChartDataSet barDataSet = new BarChartDataSet();
58
        barDataSet.setLabel("");
75
        barDataSet.setLabel("");
59
       
-
 
60
        List<Number> valores = new ArrayList<>();
-
 
61
        List<String> cores = new ArrayList<>();
-
 
62
        List<String> bordas = new ArrayList<>();
-
 
63
        List<String> marcadores = new ArrayList<>();
-
 
64
        for (int mes = 1; mes <= 12; mes++) {
-
 
65
                        Double valorAcumulado = new Double(0.0);
-
 
66
                        Calendar dataInicial = DataUtils.obterCalendario(DataUtils.obterData(1, new Integer(mes)-1, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno())));
-
 
67
                        int qntMaximaDias = dataInicial.getActualMaximum(Calendar.DAY_OF_MONTH);
-
 
68
                        Calendar dataFinal = DataUtils.obterCalendario(DataUtils.obterData(qntMaximaDias, new Integer(mes)-1, new Integer(parametrosConsulta.getParametrosPeriodoDTO().getAno())));
-
 
69
                        parametrosConsulta.getParametrosPeriodoDTO().setDataInicial(dataInicial.getTime());
-
 
70
                        parametrosConsulta.getParametrosPeriodoDTO().setDataFinal(dataFinal.getTime());
-
 
71
                        valorAcumulado = vendaService.obterValorTotalBrutoDeVendasPorLojaEePeriodoEeFormasDePagamento(
-
 
72
                                        parametrosConsulta, null/* Arrays.asList(parametrosConsulta.getFormasDePagamentoDTO()) */);
-
 
73
                valores.add(valorAcumulado);
-
 
74
                cores.add("rgba(75, 192, 192, 0.2)");
-
 
75
                bordas.add("rgb(75, 192, 192)");
-
 
76
                marcadores.add("" + mes);
-
 
77
                }
-
 
78
        barDataSet.setData(valores);
-
 
79
        barDataSet.setBackgroundColor(cores);
-
 
80
        barDataSet.setBorderColor(bordas);
-
 
-
 
76
        barDataSet.setData(graficoDTO.getDados().getValores());
-
 
77
        barDataSet.setBackgroundColor(graficoDTO.getDados().getCores());
-
 
78
        barDataSet.setBorderColor(graficoDTO.getDados().getBordas());
81
        barDataSet.setBorderWidth(1);
79
        barDataSet.setBorderWidth(1);
82
        data.addChartDataSet(barDataSet);
-
 
83
        data.setLabels(marcadores);
-
 
84
       
-
 
85
        graficoBarra.setData(data);
-
 
86
-
 
-
 
80
        ChartData dados = new ChartData();
-
 
81
        dados.addChartDataSet(barDataSet);
-
 
82
        dados.setLabels(graficoDTO.getDados().getMarcadores());
-
 
83
               
87
        BarChartOptions options = new BarChartOptions();
84
        BarChartOptions options = new BarChartOptions();
88
        CartesianScales cScales = new CartesianScales();
85
        CartesianScales cScales = new CartesianScales();
89
        CartesianLinearAxes linearAxes = new CartesianLinearAxes();
86
        CartesianLinearAxes linearAxes = new CartesianLinearAxes();
90
        linearAxes.setOffset(true);
87
        linearAxes.setOffset(true);
91
        CartesianLinearTicks ticks = new CartesianLinearTicks();
88
        CartesianLinearTicks ticks = new CartesianLinearTicks();
Line 93... Line 90...
93
        linearAxes.setTicks(ticks);
90
        linearAxes.setTicks(ticks);
94
        cScales.setDisplay(true);
91
        cScales.setDisplay(true);
95
        cScales.addYAxesData(linearAxes);
92
        cScales.addYAxesData(linearAxes);
96
        options.setScales(cScales);
93
        options.setScales(cScales);
97
       
94
       
98
        Title title = new Title();
-
 
99
        title.setText("VENDAS");
-
 
100
        options.setTitle(title);
-
 
-
 
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
        }
101
101
102
        Legend legend = new Legend();
102
        Legend legend = new Legend();
103
        legend.setDisplay(true);
103
        legend.setDisplay(true);
104
        legend.setPosition("top");
104
        legend.setPosition("top");
105
       
105
       
106
        LegendLabel legendLabels = new LegendLabel();
106
        LegendLabel legendLabels = new LegendLabel();
107
        legendLabels.setFontStyle("bold");
107
        legendLabels.setFontStyle("bold");
108
        legendLabels.setFontColor("#2980B9");
108
        legendLabels.setFontColor("#2980B9");
109
        legendLabels.setFontSize(24);
109
        legendLabels.setFontSize(24);
110
        legend.setLabels(legendLabels);
110
        legend.setLabels(legendLabels);
111
        legend.setPosition("ne");//extra
-
 
112
        options.setLegend(legend);
111
        options.setLegend(legend);
113
112
114
//        Animation animation = new Animation();
-
 
115
//        animation.setDuration(0);
-
 
116
//        options.setAnimation(animation);
-
 
117
-
 
-
 
113
        BarChartModel graficoBarra = new BarChartModel();
-
 
114
        graficoBarra.setData(dados);
118
        graficoBarra.setOptions(options);
115
        graficoBarra.setOptions(options);
119
        graficoBarra.setExtender("barra");
116
        graficoBarra.setExtender("barra");
120
        graficoLinhaDTO.setGraficoBarra(graficoBarra);
-
 
121
                return graficoLinhaDTO;
-
 
-
 
117
       
-
 
118
        graficoDTO.setGraficoBarra(graficoBarra);
-
 
119
                return graficoDTO;
-
 
120
        }
-
 
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
        }
-
 
152
       
-
 
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;
122
        }
201
        }
123
       
202
       
124
}
203
}