Subversion Repositories Integrator Subversion

Rev

Rev 413 | Rev 425 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
353 espaco 1
package br.com.ec.controller;
2
 
3
import java.io.Serializable;
4
 
5
import javax.inject.Inject;
6
import javax.inject.Named;
7
 
414 espaco 8
import org.primefaces.model.StreamedContent;
353 espaco 9
import org.springframework.context.annotation.Scope;
10
 
11
import br.com.ec.controller.consultademanda.NotaFiscalConsultaPorDemanda;
12
import br.com.ec.core.generic.GenericService;
13
import br.com.ec.domain.dto.consulta.ParametrosConsultaNotaFiscal;
14
import br.com.ec.domain.model.NotaFiscal;
413 espaco 15
import br.com.ec.domain.model.Venda;
353 espaco 16
import br.com.ec.domain.model.nfe.TipoNotaFiscal;
414 espaco 17
import br.com.ec.domain.service.EmissorRelatorioService;
353 espaco 18
import br.com.ec.domain.service.NotaFiscalService;
19
import br.com.ec.web.exception.VerificadorLancamentoException;
20
import br.com.ec.web.exception.VerificadorLancamentoException.CommandBean;
21
import br.com.ec.web.generic.AbstractBean;
22
import br.com.ec.web.message.LancadorMensagem;
23
import br.com.ec.web.util.TipoOperacao;
24
 
25
@Named
26
@Scope("view")
27
public class NotaFiscalBean extends AbstractBean<NotaFiscal> implements Serializable {
28
 
29
        private static final long serialVersionUID = 1L;
30
 
31
        private NotaFiscalService notaFiscalService;
414 espaco 32
        private EmissorRelatorioService emissorRelatorioService;
353 espaco 33
 
34
        private ParametrosConsultaNotaFiscal parametrosConsultaNotaFiscal;
35
        private NotaFiscalConsultaPorDemanda lazy;
36
 
37
        private Long sequencialVendaParaVinculo;
414 espaco 38
        private StreamedContent danfeNfe;
353 espaco 39
 
40
        @Inject
414 espaco 41
        public NotaFiscalBean(NotaFiscalConsultaPorDemanda lazy, NotaFiscalService notaFiscalService, EmissorRelatorioService emissorRelatorioService) {
353 espaco 42
                this.lazy = lazy;
43
                this.notaFiscalService = notaFiscalService;
414 espaco 44
                this.emissorRelatorioService = emissorRelatorioService;
353 espaco 45
        }
46
 
47
        @Override
48
        public void preCarregamento() {
49
                parametrosConsultaNotaFiscal = new ParametrosConsultaNotaFiscal();
50
                prepararConsultaDemanda();
51
                limparEntidade();
52
        }
53
 
54
        @Override
55
        public void limparEntidade() {
56
                setEntidade(new NotaFiscal());
57
        }
58
 
59
        @Override
60
        public GenericService<NotaFiscal> getService() {
61
                return notaFiscalService;
62
        }
63
 
64
        @Override
65
        public NotaFiscal getEntidade() {
66
                return entidade;
67
        }
68
 
69
        @Override
70
        public void preConsultar() {
71
                setTipoOperacao(TipoOperacao.CONSULTAR);
72
        }
73
 
74
        @Override
75
        public NotaFiscal getId() {
76
                return getEntidade();
77
        }
78
 
79
        public ParametrosConsultaNotaFiscal getParametrosConsultaNotaFiscal() {
80
                return parametrosConsultaNotaFiscal;
81
        }
82
        public void setParametrosConsultaNotaFiscal(ParametrosConsultaNotaFiscal parametrosConsultaNotaFiscal) {
83
                this.parametrosConsultaNotaFiscal = parametrosConsultaNotaFiscal;
84
        }
85
 
86
        public NotaFiscalConsultaPorDemanda getLazy() {
87
                return lazy;
88
        }
89
        public void setLazy(NotaFiscalConsultaPorDemanda lazy) {
90
                this.lazy = lazy;
91
        }
92
        public void prepararConsultaDemanda() {
93
                lazy.setarParametrosConsulta(getParametrosConsultaNotaFiscal());
94
        }
95
 
96
        public TipoNotaFiscal[] getTiposNotaFiscal() {
97
                return TipoNotaFiscal.values();
98
        }
99
 
100
        public Long getSequencialVendaParaVinculo() {
101
                return sequencialVendaParaVinculo;
102
        }
103
        public void setSequencialVendaParaVinculo(Long sequencialVendaParaVinculo) {
104
                this.sequencialVendaParaVinculo = sequencialVendaParaVinculo;
105
        }
106
 
414 espaco 107
        public StreamedContent getDanfeNfe() {
108
                return danfeNfe;
109
        }
110
        public void setDanfeNfe(StreamedContent danfeNfe) {
111
                this.danfeNfe = danfeNfe;
112
        }
113
 
353 espaco 114
        /**************************************************/
115
 
116
        public void desvincularVenda() {
117
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
118
                        public void execute() {
413 espaco 119
                                notaFiscalService.desvincularVenda(getEntidade().getSequencial());
120
                                getEntidade().setVenda(null);
353 espaco 121
                                LancadorMensagem.lancarSucesso("VENDA DESVINCULADA COM SUCESSO");
122
                        }
123
                });
124
        }
125
 
126
        public void vincularVenda() {
127
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
128
                        public void execute() {
413 espaco 129
                                Venda vendaVinculada = notaFiscalService.vincularVenda(getEntidade().getSequencial(), getSequencialVendaParaVinculo());
130
                                getEntidade().setVenda(vendaVinculada);
353 espaco 131
                                LancadorMensagem.lancarSucesso("VENDA VINCULADA COM SUCESSO");
132
                        }
133
                });
134
        }
135
 
414 espaco 136
        public void emitirDanfeNfe(Long sequencialNotaFiscal) {
137
                setDanfeNfe(notaFiscalService.gerarDanfeNfe(sequencialNotaFiscal));
138
                /*
139
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
140
                        @Override
141
                        public StreamedContent execute() {
142
                                return notaFiscalService.gerarDanfeNfe(getEntidade().getSequencial());
143
                        }
144
                });
145
                */
146
        }
147
 
413 espaco 148
        /*
149
         private StreamedContent chart;
150
         public StreamedContent getChart() {
151
                 return chart;
152
         }
153
         private PieDataset createDataset() {
154
                 DefaultPieDataset dataset = new DefaultPieDataset();
155
                 dataset.setValue("New York", new Double(45.0));
156
                 dataset.setValue("London", new Double(15.0));
157
                 dataset.setValue("Paris", new Double(25.2));
158
                 dataset.setValue("Berlin", new Double(14.8));
159
                 return dataset;
160
         }
161
 
162
         private StreamedContent escala;
163
         public StreamedContent getEscala() {
164
                 return escala;
165
         }
166
 
167
         private StreamedContent escalaPdf;
168
         public StreamedContent getEscalaPdf() {
169
                 return escalaPdf;
170
         }
171
         public String generateRandomIdForNotCaching() {
172
                 return java.util.UUID.randomUUID().toString();
173
         }
174
 
175
         private void criarImagens() {
176
                 try {
177
                         InputStream arquivoPdf = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/imagens/escala-202310.pdf");
178
                         byte[] bytePdf = ArquivoUtil.retornarBytesPassandoInputStream(arquivoPdf);
179
                         ByteArrayOutputStream osPdf = new ByteArrayOutputStream();
180
                         PdfReader reader = new PdfReader(bytePdf);
181
                         escalaPdf = DefaultStreamedContent.builder()
182
                            .contentType("application/pdf")
183
                            .name("escala-202310.pdf")
184
                            .stream(() -> {
185
                                                try {
186
                                                        return ArquivoUtil.gerarInputStreamDeArquivo(bytePdf);
187
                                                } catch (IOException e1) {
188
                                                        e1.printStackTrace();
189
                                                }
190
                                                return arquivoPdf;
191
                                        })
192
                            .build();
193
 
194
 
195
                         InputStream arquivo = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/imagens/escala-202310.png");
196
                         BufferedImage imagem = ImageIO.read(arquivo);
197
                         ByteArrayOutputStream os = new ByteArrayOutputStream();
198
             ImageIO.write(imagem, "png", os);
199
                         escala = DefaultStreamedContent.builder()
200
                            .contentType("image/png")
201
                            .stream(() -> new ByteArrayInputStream(os.toByteArray()))
202
                            .build();
203
 
204
 
205
                        chart = DefaultStreamedContent.builder()
206
                            .contentType("image/png")
207
                            .stream(() -> {
208
                                try {
209
                                    JFreeChart jfreechart = ChartFactory.createPieChart("Cities", createDataset(), true, true, false);
210
                                    File chartFile = new File("dynamichart");
211
                                    ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
212
                                    return new FileInputStream(chartFile);
213
                                }
214
                                catch (Exception e) {
215
                                    e.printStackTrace();
216
                                    return null;
217
                                }
218
                            })
219
                            .build();
220
                 } catch (IOException e1) {
221
                                // TODO Auto-generated catch block
222
                                e1.printStackTrace();
223
                        }
224
        }
225
        */
353 espaco 226
}