Subversion Repositories Integrator Subversion

Rev

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