Subversion Repositories Integrator Subversion

Rev

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