Subversion Repositories Integrator Subversion

Rev

Rev 449 | Rev 465 | 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;
462 blopes 13
import br.com.ec.domain.dto.NotaFiscalDTO;
14
import br.com.ec.domain.dto.NotaFiscalProdutoDTO;
15
import br.com.ec.domain.dto.NotaFiscalReferenciadaDTO;
16
import br.com.ec.domain.dto.ProdutoDTO;
353 espaco 17
import br.com.ec.domain.dto.consulta.ParametrosConsultaNotaFiscal;
18
import br.com.ec.domain.model.NotaFiscal;
449 blopes 19
import br.com.ec.domain.model.Pessoa;
413 espaco 20
import br.com.ec.domain.model.Venda;
353 espaco 21
import br.com.ec.domain.model.nfe.TipoNotaFiscal;
414 espaco 22
import br.com.ec.domain.service.EmissorRelatorioService;
462 blopes 23
import br.com.ec.domain.service.NotaFiscalProdutoService;
24
import br.com.ec.domain.service.NotaFiscalReferenciadaService;
353 espaco 25
import br.com.ec.domain.service.NotaFiscalService;
462 blopes 26
import br.com.ec.domain.service.ProdutoService;
353 espaco 27
import br.com.ec.web.exception.VerificadorLancamentoException;
28
import br.com.ec.web.exception.VerificadorLancamentoException.CommandBean;
446 blopes 29
import br.com.ec.web.exception.VerificadorLancamentoException.CommandRelatorioStreamedContent;
353 espaco 30
import br.com.ec.web.generic.AbstractBean;
31
import br.com.ec.web.message.LancadorMensagem;
32
import br.com.ec.web.util.TipoOperacao;
33
 
34
@Named
35
@Scope("view")
36
public class NotaFiscalBean extends AbstractBean<NotaFiscal> implements Serializable {
37
 
38
        private static final long serialVersionUID = 1L;
39
 
40
        private NotaFiscalService notaFiscalService;
462 blopes 41
        private NotaFiscalProdutoService notaFiscalProdutoService;
42
        private NotaFiscalReferenciadaService notaFiscalReferenciadaService;
43
        private ProdutoService produtoService;
414 espaco 44
        private EmissorRelatorioService emissorRelatorioService;
353 espaco 45
 
46
        private ParametrosConsultaNotaFiscal parametrosConsultaNotaFiscal;
47
        private NotaFiscalConsultaPorDemanda lazy;
48
 
49
        private Long sequencialVendaParaVinculo;
446 blopes 50
        private StreamedContent danfeNfePreVisualizacao;
414 espaco 51
        private StreamedContent danfeNfe;
449 blopes 52
 
462 blopes 53
        private String codigoProdutoParaVinculoNotaFiscal = "";
54
        private Integer quantidadeProdutoParaVinculoNotaFiscal = 0;
55
        private ProdutoDTO produtoDTOParaVinculoNotaFiscal;
56
 
57
        private String chaveNotaFiscalParaReferenciaNotaFiscal = "";
58
        private NotaFiscalDTO notaFiscalDTOParaReferenciaNotaFiscal;
59
 
353 espaco 60
        @Inject
462 blopes 61
        public NotaFiscalBean(NotaFiscalConsultaPorDemanda lazy, NotaFiscalService notaFiscalService, NotaFiscalProdutoService notaFiscalProdutoService,
62
                        NotaFiscalReferenciadaService notaFiscalReferenciadaService, ProdutoService produtoService, EmissorRelatorioService emissorRelatorioService) {
353 espaco 63
                this.lazy = lazy;
64
                this.notaFiscalService = notaFiscalService;
462 blopes 65
                this.notaFiscalProdutoService = notaFiscalProdutoService;
66
                this.notaFiscalReferenciadaService = notaFiscalReferenciadaService;
67
                this.produtoService = produtoService;
414 espaco 68
                this.emissorRelatorioService = emissorRelatorioService;
353 espaco 69
        }
70
 
71
        @Override
72
        public void preCarregamento() {
73
                parametrosConsultaNotaFiscal = new ParametrosConsultaNotaFiscal();
74
                prepararConsultaDemanda();
75
                limparEntidade();
76
        }
77
 
78
        @Override
79
        public void limparEntidade() {
80
                setEntidade(new NotaFiscal());
81
        }
82
 
83
        @Override
84
        public GenericService<NotaFiscal> getService() {
85
                return notaFiscalService;
86
        }
87
 
88
        @Override
89
        public NotaFiscal getEntidade() {
90
                return entidade;
91
        }
92
 
93
        @Override
94
        public void preConsultar() {
95
                setTipoOperacao(TipoOperacao.CONSULTAR);
96
        }
449 blopes 97
 
98
        /*
353 espaco 99
        @Override
449 blopes 100
        public void preAlterar(NotaFiscal notaFiscal) {
101
                NotaFiscal notaFiscalDetalhada = notaFiscalService.detalhar(notaFiscal.getSequencial());
102
                super.preAlterar(notaFiscalDetalhada);
103
        }
104
        */
105
 
106
        @Override
353 espaco 107
        public NotaFiscal getId() {
108
                return getEntidade();
109
        }
110
 
111
        public ParametrosConsultaNotaFiscal getParametrosConsultaNotaFiscal() {
112
                return parametrosConsultaNotaFiscal;
113
        }
114
        public void setParametrosConsultaNotaFiscal(ParametrosConsultaNotaFiscal parametrosConsultaNotaFiscal) {
115
                this.parametrosConsultaNotaFiscal = parametrosConsultaNotaFiscal;
116
        }
117
 
118
        public NotaFiscalConsultaPorDemanda getLazy() {
119
                return lazy;
120
        }
121
        public void setLazy(NotaFiscalConsultaPorDemanda lazy) {
122
                this.lazy = lazy;
123
        }
124
        public void prepararConsultaDemanda() {
125
                lazy.setarParametrosConsulta(getParametrosConsultaNotaFiscal());
126
        }
127
 
128
        public TipoNotaFiscal[] getTiposNotaFiscal() {
129
                return TipoNotaFiscal.values();
130
        }
131
 
132
        public Long getSequencialVendaParaVinculo() {
133
                return sequencialVendaParaVinculo;
134
        }
135
        public void setSequencialVendaParaVinculo(Long sequencialVendaParaVinculo) {
136
                this.sequencialVendaParaVinculo = sequencialVendaParaVinculo;
137
        }
138
 
446 blopes 139
        public StreamedContent getDanfeNfePreVisualizacao() {
140
                return danfeNfePreVisualizacao;
141
        }
142
        public void setDanfeNfePreVisualizacao(StreamedContent danfeNfePreVisualizacao) {
143
                this.danfeNfePreVisualizacao = danfeNfePreVisualizacao;
144
        }
145
 
414 espaco 146
        public StreamedContent getDanfeNfe() {
147
                return danfeNfe;
148
        }
149
        public void setDanfeNfe(StreamedContent danfeNfe) {
150
                this.danfeNfe = danfeNfe;
151
        }
152
 
462 blopes 153
        public String getCodigoProdutoParaVinculoNotaFiscal() {
154
                return codigoProdutoParaVinculoNotaFiscal;
155
        }
156
        public void setCodigoProdutoParaVinculoNotaFiscal(String codigoProdutoParaVinculoNotaFiscal) {
157
                this.codigoProdutoParaVinculoNotaFiscal = codigoProdutoParaVinculoNotaFiscal;
158
        }
159
 
160
        public Integer getQuantidadeProdutoParaVinculoNotaFiscal() {
161
                return quantidadeProdutoParaVinculoNotaFiscal;
162
        }
163
        public void setQuantidadeProdutoParaVinculoNotaFiscal(Integer quantidadeProdutoParaVinculoNotaFiscal) {
164
                this.quantidadeProdutoParaVinculoNotaFiscal = quantidadeProdutoParaVinculoNotaFiscal;
165
        }
166
 
167
        public ProdutoDTO getProdutoDTOParaVinculoNotaFiscal() {
168
                return produtoDTOParaVinculoNotaFiscal;
169
        }
170
        public void setProdutoDTOParaVinculoNotaFiscal(ProdutoDTO produtoDTOParaVinculoNotaFiscal) {
171
                this.produtoDTOParaVinculoNotaFiscal = produtoDTOParaVinculoNotaFiscal;
172
        }
173
 
174
        public String getChaveNotaFiscalParaReferenciaNotaFiscal() {
175
                return chaveNotaFiscalParaReferenciaNotaFiscal;
176
        }
177
        public void setChaveNotaFiscalParaReferenciaNotaFiscal(String chaveNotaFiscalParaReferenciaNotaFiscal) {
178
                this.chaveNotaFiscalParaReferenciaNotaFiscal = chaveNotaFiscalParaReferenciaNotaFiscal;
179
        }
180
 
181
        public NotaFiscalDTO getNotaFiscalDTOParaReferenciaNotaFiscal() {
182
                return notaFiscalDTOParaReferenciaNotaFiscal;
183
        }
184
        public void setNotaFiscalDTOParaReferenciaNotaFiscal(NotaFiscalDTO notaFiscalDTOParaReferenciaNotaFiscal) {
185
                this.notaFiscalDTOParaReferenciaNotaFiscal = notaFiscalDTOParaReferenciaNotaFiscal;
186
        }
187
 
353 espaco 188
        /**************************************************/
189
 
190
        public void desvincularVenda() {
191
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
192
                        public void execute() {
413 espaco 193
                                notaFiscalService.desvincularVenda(getEntidade().getSequencial());
194
                                getEntidade().setVenda(null);
353 espaco 195
                                LancadorMensagem.lancarSucesso("VENDA DESVINCULADA COM SUCESSO");
196
                        }
197
                });
198
        }
199
 
200
        public void vincularVenda() {
201
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
202
                        public void execute() {
413 espaco 203
                                Venda vendaVinculada = notaFiscalService.vincularVenda(getEntidade().getSequencial(), getSequencialVendaParaVinculo());
204
                                getEntidade().setVenda(vendaVinculada);
353 espaco 205
                                LancadorMensagem.lancarSucesso("VENDA VINCULADA COM SUCESSO");
206
                        }
207
                });
208
        }
209
 
414 espaco 210
        public void emitirDanfeNfe(Long sequencialNotaFiscal) {
447 blopes 211
                setDanfeNfePreVisualizacao(notaFiscalService.gerarDanfeNfePreVisualizacao(sequencialNotaFiscal));
414 espaco 212
                setDanfeNfe(notaFiscalService.gerarDanfeNfe(sequencialNotaFiscal));
446 blopes 213
        }
214
 
215
        public void emitirDanfeNfePreVisualizacao(Long sequencialNotaFiscal) {
216
                setDanfeNfePreVisualizacao(notaFiscalService.gerarDanfeNfePreVisualizacao(sequencialNotaFiscal));
217
                setDanfeNfe(notaFiscalService.gerarDanfeNfe(sequencialNotaFiscal));
414 espaco 218
                /*
219
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
220
                        @Override
221
                        public StreamedContent execute() {
222
                                return notaFiscalService.gerarDanfeNfe(getEntidade().getSequencial());
223
                        }
224
                });
225
                */
226
        }
227
 
446 blopes 228
        public StreamedContent downloadNotaFiscal() {
229
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
230
                        @Override
231
                        public StreamedContent execute() {
232
                                return getDanfeNfe();
233
                        }
234
                });
235
        }
236
 
425 espaco 237
        public void emitirDanfeNfeTeste() {
238
                Long sequencialNotaFiscal = new Long(168722);
446 blopes 239
                setDanfeNfePreVisualizacao(notaFiscalService.gerarDanfeNfePreVisualizacao(sequencialNotaFiscal));
425 espaco 240
                setDanfeNfe(notaFiscalService.gerarDanfeNfe(sequencialNotaFiscal));
241
        }
242
 
449 blopes 243
        public void selecionarEmitente(final Pessoa pessoa) {
244
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
245
                        public void execute() {
246
                                getEntidade().setPessoaEmitente(pessoa);
247
                                adicionandoParametroArgsConfirmandoAcao(true);
248
                        }
249
                });
250
        }
251
 
252
        public void retirarEmitente() {
253
                getEntidade().setPessoaEmitente(null);
254
        }
255
 
256
        public void selecionarDestinatario(final Pessoa pessoa) {
257
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
258
                        public void execute() {
259
                                getEntidade().setPessoaDestinatario(pessoa);
260
                                adicionandoParametroArgsConfirmandoAcao(true);
261
                        }
262
                });
263
        }
264
 
265
        public void retirarDestinatario() {
266
                getEntidade().setPessoaDestinatario(null);
267
        }
268
 
269
        public void consultarProdutosVinculados() {
270
                getEntidade().setListaProdutosVinculadosDTO(notaFiscalService.consultarProdutosVinculados(getEntidade().getSequencial()));
271
        }
272
 
462 blopes 273
        public void consultarNfReferenciadas() {
274
                getEntidade().setListaNfeReferenciadasDTO(notaFiscalService.consultarNfReferenciadas(getEntidade().getSequencial()));
275
        }
276
 
277
        public void excluirVinculoNotaFiscalProduto(NotaFiscalProdutoDTO notaFiscalProdutoDTO) {
278
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
279
                        public void execute() {
280
                                notaFiscalProdutoService.excluirNotaFiscalProduto(notaFiscalProdutoDTO);
281
                                consultarProdutosVinculados();
282
                                LancadorMensagem.lancarSucesso("VÍNCULO EXCLUÍDO COM SUCESSO");
283
                        }
284
                });
285
        }
286
 
287
        public void consultarProdutoParaVinculoNotaFiscal() {
288
                setProdutoDTOParaVinculoNotaFiscal(produtoService.consultarProdutoDTOPorCodigoOuCodigoEAN(getCodigoProdutoParaVinculoNotaFiscal()));
289
        }
290
 
291
        public void vinculoProdutoNaNotaFiscal() {
292
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
293
                        public void execute() {
294
                                notaFiscalProdutoService.vinculoProdutoNaNotaFiscal(getEntidade().getSequencial(),
295
                                                getProdutoDTOParaVinculoNotaFiscal().getSequencial(),
296
                                                getQuantidadeProdutoParaVinculoNotaFiscal());
297
                                setCodigoProdutoParaVinculoNotaFiscal("");
298
                                setProdutoDTOParaVinculoNotaFiscal(null);
299
                                setQuantidadeProdutoParaVinculoNotaFiscal(0);
300
                                consultarProdutosVinculados();
301
                                LancadorMensagem.lancarSucesso("PRODUTO VINCULADO COM SUCESSO");
302
                        }
303
                });
304
        }
305
 
306
        public void consultarNotaFiscalParaReferenciarNotaFiscal() {
307
                setNotaFiscalDTOParaReferenciaNotaFiscal(notaFiscalService.consultarPorChave(getChaveNotaFiscalParaReferenciaNotaFiscal()));
308
        }
309
 
310
        public void referenciarNotaFiscalNaNotaFiscal() {
311
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
312
                        public void execute() {
313
                                notaFiscalReferenciadaService.referenciarNotaFiscal(getEntidade().getSequencial(), getNotaFiscalDTOParaReferenciaNotaFiscal().getSequencialNotaFiscal());
314
                                setChaveNotaFiscalParaReferenciaNotaFiscal("");
315
                                setNotaFiscalDTOParaReferenciaNotaFiscal(null);
316
                                consultarNfReferenciadas();
317
                                LancadorMensagem.lancarSucesso("NOTA FISCAL REFERENCIADA COM SUCESSO");
318
                        }
319
                });
320
        }
321
 
322
        public void excluirNotaFiscalReferenciada(NotaFiscalReferenciadaDTO notaFiscalReferenciadaDTO) {
323
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
324
                        public void execute() {
325
                                notaFiscalReferenciadaService.excluirNotaFiscalReferenciada(notaFiscalReferenciadaDTO);
326
                                consultarNfReferenciadas();
327
                                LancadorMensagem.lancarSucesso("REFERÊNCIA EXCLUÍDA COM SUCESSO");
328
                        }
329
                });
330
        }
331
 
413 espaco 332
        /*
333
         private StreamedContent chart;
334
         public StreamedContent getChart() {
335
                 return chart;
336
         }
337
         private PieDataset createDataset() {
338
                 DefaultPieDataset dataset = new DefaultPieDataset();
339
                 dataset.setValue("New York", new Double(45.0));
340
                 dataset.setValue("London", new Double(15.0));
341
                 dataset.setValue("Paris", new Double(25.2));
342
                 dataset.setValue("Berlin", new Double(14.8));
343
                 return dataset;
344
         }
345
 
346
         private StreamedContent escala;
347
         public StreamedContent getEscala() {
348
                 return escala;
349
         }
350
 
351
         private StreamedContent escalaPdf;
352
         public StreamedContent getEscalaPdf() {
353
                 return escalaPdf;
354
         }
355
         public String generateRandomIdForNotCaching() {
356
                 return java.util.UUID.randomUUID().toString();
357
         }
358
 
359
         private void criarImagens() {
360
                 try {
361
                         InputStream arquivoPdf = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/imagens/escala-202310.pdf");
362
                         byte[] bytePdf = ArquivoUtil.retornarBytesPassandoInputStream(arquivoPdf);
363
                         ByteArrayOutputStream osPdf = new ByteArrayOutputStream();
364
                         PdfReader reader = new PdfReader(bytePdf);
365
                         escalaPdf = DefaultStreamedContent.builder()
366
                            .contentType("application/pdf")
367
                            .name("escala-202310.pdf")
368
                            .stream(() -> {
369
                                                try {
370
                                                        return ArquivoUtil.gerarInputStreamDeArquivo(bytePdf);
371
                                                } catch (IOException e1) {
372
                                                        e1.printStackTrace();
373
                                                }
374
                                                return arquivoPdf;
375
                                        })
376
                            .build();
377
 
378
 
379
                         InputStream arquivo = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/imagens/escala-202310.png");
380
                         BufferedImage imagem = ImageIO.read(arquivo);
381
                         ByteArrayOutputStream os = new ByteArrayOutputStream();
382
             ImageIO.write(imagem, "png", os);
383
                         escala = DefaultStreamedContent.builder()
384
                            .contentType("image/png")
385
                            .stream(() -> new ByteArrayInputStream(os.toByteArray()))
386
                            .build();
387
 
388
 
389
                        chart = DefaultStreamedContent.builder()
390
                            .contentType("image/png")
391
                            .stream(() -> {
392
                                try {
393
                                    JFreeChart jfreechart = ChartFactory.createPieChart("Cities", createDataset(), true, true, false);
394
                                    File chartFile = new File("dynamichart");
395
                                    ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
396
                                    return new FileInputStream(chartFile);
397
                                }
398
                                catch (Exception e) {
399
                                    e.printStackTrace();
400
                                    return null;
401
                                }
402
                            })
403
                            .build();
404
                 } catch (IOException e1) {
405
                                // TODO Auto-generated catch block
406
                                e1.printStackTrace();
407
                        }
408
        }
409
        */
353 espaco 410
}