Subversion Repositories Integrator Subversion

Rev

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