Subversion Repositories Integrator Subversion

Rev

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