Subversion Repositories Integrator Subversion

Rev

Rev 414 | Rev 446 | 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;
13
import br.com.ec.domain.dto.consulta.ParametrosConsultaNotaFiscal;
14
import br.com.ec.domain.model.NotaFiscal;
413 espaco 15
import br.com.ec.domain.model.Venda;
353 espaco 16
import br.com.ec.domain.model.nfe.TipoNotaFiscal;
414 espaco 17
import br.com.ec.domain.service.EmissorRelatorioService;
353 espaco 18
import br.com.ec.domain.service.NotaFiscalService;
19
import br.com.ec.web.exception.VerificadorLancamentoException;
20
import br.com.ec.web.exception.VerificadorLancamentoException.CommandBean;
21
import br.com.ec.web.generic.AbstractBean;
22
import br.com.ec.web.message.LancadorMensagem;
23
import br.com.ec.web.util.TipoOperacao;
24
 
25
@Named
26
@Scope("view")
27
public class NotaFiscalBean extends AbstractBean<NotaFiscal> implements Serializable {
28
 
29
        private static final long serialVersionUID = 1L;
30
 
31
        private NotaFiscalService notaFiscalService;
414 espaco 32
        private EmissorRelatorioService emissorRelatorioService;
353 espaco 33
 
34
        private ParametrosConsultaNotaFiscal parametrosConsultaNotaFiscal;
35
        private NotaFiscalConsultaPorDemanda lazy;
36
 
37
        private Long sequencialVendaParaVinculo;
414 espaco 38
        private StreamedContent danfeNfe;
353 espaco 39
 
40
        @Inject
414 espaco 41
        public NotaFiscalBean(NotaFiscalConsultaPorDemanda lazy, NotaFiscalService notaFiscalService, EmissorRelatorioService emissorRelatorioService) {
353 espaco 42
                this.lazy = lazy;
43
                this.notaFiscalService = notaFiscalService;
414 espaco 44
                this.emissorRelatorioService = emissorRelatorioService;
353 espaco 45
        }
46
 
47
        @Override
48
        public void preCarregamento() {
49
                parametrosConsultaNotaFiscal = new ParametrosConsultaNotaFiscal();
50
                prepararConsultaDemanda();
51
                limparEntidade();
52
        }
53
 
54
        @Override
55
        public void limparEntidade() {
56
                setEntidade(new NotaFiscal());
57
        }
58
 
59
        @Override
60
        public GenericService<NotaFiscal> getService() {
61
                return notaFiscalService;
62
        }
63
 
64
        @Override
65
        public NotaFiscal getEntidade() {
66
                return entidade;
67
        }
68
 
69
        @Override
70
        public void preConsultar() {
71
                setTipoOperacao(TipoOperacao.CONSULTAR);
72
        }
73
 
74
        @Override
75
        public NotaFiscal getId() {
76
                return getEntidade();
77
        }
78
 
79
        public ParametrosConsultaNotaFiscal getParametrosConsultaNotaFiscal() {
80
                return parametrosConsultaNotaFiscal;
81
        }
82
        public void setParametrosConsultaNotaFiscal(ParametrosConsultaNotaFiscal parametrosConsultaNotaFiscal) {
83
                this.parametrosConsultaNotaFiscal = parametrosConsultaNotaFiscal;
84
        }
85
 
86
        public NotaFiscalConsultaPorDemanda getLazy() {
87
                return lazy;
88
        }
89
        public void setLazy(NotaFiscalConsultaPorDemanda lazy) {
90
                this.lazy = lazy;
91
        }
92
        public void prepararConsultaDemanda() {
93
                lazy.setarParametrosConsulta(getParametrosConsultaNotaFiscal());
94
        }
95
 
96
        public TipoNotaFiscal[] getTiposNotaFiscal() {
97
                return TipoNotaFiscal.values();
98
        }
99
 
100
        public Long getSequencialVendaParaVinculo() {
101
                return sequencialVendaParaVinculo;
102
        }
103
        public void setSequencialVendaParaVinculo(Long sequencialVendaParaVinculo) {
104
                this.sequencialVendaParaVinculo = sequencialVendaParaVinculo;
105
        }
106
 
414 espaco 107
        public StreamedContent getDanfeNfe() {
108
                return danfeNfe;
109
        }
110
        public void setDanfeNfe(StreamedContent danfeNfe) {
111
                this.danfeNfe = danfeNfe;
112
        }
113
 
353 espaco 114
        /**************************************************/
115
 
116
        public void desvincularVenda() {
117
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
118
                        public void execute() {
413 espaco 119
                                notaFiscalService.desvincularVenda(getEntidade().getSequencial());
120
                                getEntidade().setVenda(null);
353 espaco 121
                                LancadorMensagem.lancarSucesso("VENDA DESVINCULADA COM SUCESSO");
122
                        }
123
                });
124
        }
125
 
126
        public void vincularVenda() {
127
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
128
                        public void execute() {
413 espaco 129
                                Venda vendaVinculada = notaFiscalService.vincularVenda(getEntidade().getSequencial(), getSequencialVendaParaVinculo());
130
                                getEntidade().setVenda(vendaVinculada);
353 espaco 131
                                LancadorMensagem.lancarSucesso("VENDA VINCULADA COM SUCESSO");
132
                        }
133
                });
134
        }
135
 
414 espaco 136
        public void emitirDanfeNfe(Long sequencialNotaFiscal) {
137
                setDanfeNfe(notaFiscalService.gerarDanfeNfe(sequencialNotaFiscal));
138
                /*
139
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
140
                        @Override
141
                        public StreamedContent execute() {
142
                                return notaFiscalService.gerarDanfeNfe(getEntidade().getSequencial());
143
                        }
144
                });
145
                */
146
        }
147
 
425 espaco 148
        public void emitirDanfeNfeTeste() {
149
                Long sequencialNotaFiscal = new Long(168722);
150
                setDanfeNfe(notaFiscalService.gerarDanfeNfe(sequencialNotaFiscal));
151
        }
152
 
413 espaco 153
        /*
154
         private StreamedContent chart;
155
         public StreamedContent getChart() {
156
                 return chart;
157
         }
158
         private PieDataset createDataset() {
159
                 DefaultPieDataset dataset = new DefaultPieDataset();
160
                 dataset.setValue("New York", new Double(45.0));
161
                 dataset.setValue("London", new Double(15.0));
162
                 dataset.setValue("Paris", new Double(25.2));
163
                 dataset.setValue("Berlin", new Double(14.8));
164
                 return dataset;
165
         }
166
 
167
         private StreamedContent escala;
168
         public StreamedContent getEscala() {
169
                 return escala;
170
         }
171
 
172
         private StreamedContent escalaPdf;
173
         public StreamedContent getEscalaPdf() {
174
                 return escalaPdf;
175
         }
176
         public String generateRandomIdForNotCaching() {
177
                 return java.util.UUID.randomUUID().toString();
178
         }
179
 
180
         private void criarImagens() {
181
                 try {
182
                         InputStream arquivoPdf = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/imagens/escala-202310.pdf");
183
                         byte[] bytePdf = ArquivoUtil.retornarBytesPassandoInputStream(arquivoPdf);
184
                         ByteArrayOutputStream osPdf = new ByteArrayOutputStream();
185
                         PdfReader reader = new PdfReader(bytePdf);
186
                         escalaPdf = DefaultStreamedContent.builder()
187
                            .contentType("application/pdf")
188
                            .name("escala-202310.pdf")
189
                            .stream(() -> {
190
                                                try {
191
                                                        return ArquivoUtil.gerarInputStreamDeArquivo(bytePdf);
192
                                                } catch (IOException e1) {
193
                                                        e1.printStackTrace();
194
                                                }
195
                                                return arquivoPdf;
196
                                        })
197
                            .build();
198
 
199
 
200
                         InputStream arquivo = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/imagens/escala-202310.png");
201
                         BufferedImage imagem = ImageIO.read(arquivo);
202
                         ByteArrayOutputStream os = new ByteArrayOutputStream();
203
             ImageIO.write(imagem, "png", os);
204
                         escala = DefaultStreamedContent.builder()
205
                            .contentType("image/png")
206
                            .stream(() -> new ByteArrayInputStream(os.toByteArray()))
207
                            .build();
208
 
209
 
210
                        chart = DefaultStreamedContent.builder()
211
                            .contentType("image/png")
212
                            .stream(() -> {
213
                                try {
214
                                    JFreeChart jfreechart = ChartFactory.createPieChart("Cities", createDataset(), true, true, false);
215
                                    File chartFile = new File("dynamichart");
216
                                    ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
217
                                    return new FileInputStream(chartFile);
218
                                }
219
                                catch (Exception e) {
220
                                    e.printStackTrace();
221
                                    return null;
222
                                }
223
                            })
224
                            .build();
225
                 } catch (IOException e1) {
226
                                // TODO Auto-generated catch block
227
                                e1.printStackTrace();
228
                        }
229
        }
230
        */
353 espaco 231
}