Subversion Repositories Integrator Subversion

Rev

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