Subversion Repositories Integrator Subversion

Rev

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