Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 106 | espaco | 1 | package br.com.ec.controller.managedbean; |
| 2 | |||
| 3 | import java.awt.image.BufferedImage; |
||
| 4 | import java.io.ByteArrayOutputStream; |
||
| 5 | import java.io.IOException; |
||
| 6 | import java.io.Serializable; |
||
| 7 | import java.util.ArrayList; |
||
| 8 | import java.util.List; |
||
| 9 | |||
| 10 | import javax.faces.context.FacesContext; |
||
| 11 | import javax.imageio.ImageIO; |
||
| 12 | import javax.inject.Inject; |
||
| 13 | import javax.inject.Named; |
||
| 14 | import javax.servlet.http.HttpServletResponse; |
||
| 15 | |||
| 16 | import org.primefaces.model.StreamedContent; |
||
| 17 | import org.springframework.context.annotation.Scope; |
||
| 18 | |||
| 19 | import br.com.ec.domain.dto.FiltroCupom; |
||
| 20 | import br.com.ec.domain.dto.FiltroNfce; |
||
| 21 | import br.com.ec.domain.dto.nfce.NotaFiscalDTO; |
||
| 22 | import br.com.ec.domain.model.CompraProduto; |
||
| 23 | import br.com.ec.domain.model.Endereco; |
||
| 24 | import br.com.ec.domain.model.NotaFiscal; |
||
| 25 | import br.com.ec.domain.model.Venda; |
||
| 26 | import br.com.ec.domain.model.tipos.TipoRelatorioSistema; |
||
| 27 | import br.com.ec.domain.service.emissorrelatorio.EmissorRelatorioService; |
||
| 28 | import br.com.ec.domain.service.pessoa.PessoaService; |
||
| 29 | import br.edu.cesmac.relatorio.util.RelatorioUtils; |
||
| 30 | import br.edu.cesmac.web.exception.VerificadorLancamentoException; |
||
| 31 | import br.edu.cesmac.web.exception.VerificadorLancamentoException.CommandRelatorioStreamedContent; |
||
| 32 | import net.sf.jasperreports.engine.JRException; |
||
| 33 | import net.sf.jasperreports.engine.JasperExportManager; |
||
| 34 | import net.sf.jasperreports.engine.JasperPrint; |
||
| 35 | import net.sf.jasperreports.engine.JasperPrintManager; |
||
| 36 | |||
| 37 | @Named |
||
| 38 | @Scope("view") |
||
| 39 | public class RelatorioBean implements Serializable { |
||
| 40 | |||
| 41 | private static final long serialVersionUID = 1L; |
||
| 42 | |||
| 43 | private EmissorRelatorioService emissorRelatorioService; |
||
| 44 | private PessoaService pessoaService; |
||
| 45 | |||
| 46 | @Inject |
||
| 47 | public RelatorioBean(EmissorRelatorioService emissorRelatorioService, PessoaService pessoaService) { |
||
| 48 | this.emissorRelatorioService = emissorRelatorioService; |
||
| 49 | this.pessoaService = pessoaService; |
||
| 50 | } |
||
| 51 | |||
| 52 | public TipoRelatorioSistema[] getTiposRelatorioSistema() { |
||
| 53 | return TipoRelatorioSistema.values(); |
||
| 54 | } |
||
| 55 | |||
| 56 | public StreamedContent emitirNfce() { |
||
| 57 | return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() { |
||
| 58 | @Override |
||
| 59 | public StreamedContent execute() { |
||
| 60 | FiltroNfce filtro = new FiltroNfce(); |
||
| 61 | |||
| 62 | NotaFiscal nfce = new NotaFiscal(); |
||
| 63 | |||
| 64 | // Pessoa pessoa = new Pessoa(); |
||
| 65 | // pessoa.setNome("ESPAÇO CASE EPP"); |
||
| 66 | // nfce.setEmitente(pessoa); |
||
| 67 | |||
| 68 | List<CompraProduto> listaCompraProduto = new ArrayList<CompraProduto>(); |
||
| 69 | /* |
||
| 70 | CompraProduto item = new CompraProduto(); |
||
| 71 | Produto produto = new Produto(); |
||
| 72 | produto.setCodigo("12345"); |
||
| 73 | produto.setDescricao("TESTE"); |
||
| 74 | item.setProduto(produto); |
||
| 75 | listaCompraProduto.add(item); |
||
| 76 | nfce.setListaCompraProduto(listaCompraProduto); |
||
| 77 | |||
| 78 | filtro.setNotaFiscal(nfce); |
||
| 79 | */ |
||
| 80 | JasperPrint relatorio = emissorRelatorioService.emitirComprovante(filtro); |
||
| 81 | return RelatorioUtils.gerarPdf(relatorio, "NFCE"); |
||
| 82 | } |
||
| 83 | }); |
||
| 84 | } |
||
| 85 | |||
| 86 | public StreamedContent emitirNfceEmPdf(final Venda venda) { |
||
| 87 | return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() { |
||
| 88 | @Override |
||
| 89 | public StreamedContent execute() { |
||
| 90 | System.out.println("Emitir nota fiscal..."); |
||
| 91 | //NotaFiscal notaFiscalEmitida... |
||
| 92 | |||
| 93 | FiltroNfce filtro = new FiltroNfce(); |
||
| 94 | filtro.setNotaFiscal(preencherNotaFiscal(venda, true)); |
||
| 95 | JasperPrint relatorio = emissorRelatorioService.emitirComprovante(filtro); |
||
| 96 | return RelatorioUtils.gerarPdf(relatorio, "NFCE"); |
||
| 97 | } |
||
| 98 | }); |
||
| 99 | } |
||
| 100 | |||
| 101 | public StreamedContent emitirCupomEmPdf(final Venda venda) { |
||
| 102 | return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() { |
||
| 103 | @Override |
||
| 104 | public StreamedContent execute() { |
||
| 105 | FiltroCupom filtro = new FiltroCupom(); |
||
| 106 | filtro.setNotaFiscal(preencherNotaFiscal(venda, false)); |
||
| 107 | JasperPrint relatorio = emissorRelatorioService.emitirComprovante(filtro); |
||
| 108 | return RelatorioUtils.gerarPdf(relatorio, "CUPOM"); |
||
| 109 | } |
||
| 110 | }); |
||
| 111 | } |
||
| 112 | |||
| 113 | /************************************************************************/ |
||
| 114 | |||
| 115 | private NotaFiscalDTO preencherNotaFiscal(Venda venda, boolean notaFiscal) { |
||
| 116 | NotaFiscalDTO nfce = new NotaFiscalDTO(); |
||
| 117 | nfce.setPessoaJuridica(pessoaService.detalharPessoa(venda.getPessoaJuridicaDaLoja())); |
||
| 118 | nfce.setEnderecoEmitente(new ArrayList<Endereco>(nfce.getPessoaJuridica().getEnderecos()).get(0)); |
||
| 119 | nfce.preencherItens(venda.getLancamentosValidos()); |
||
| 120 | nfce.preencherPagamentos(venda.getVendaFormaPagamentos()); |
||
| 121 | |||
| 122 | if (notaFiscal) { |
||
| 123 | // PREENCHER COM DADOS DA NOTA FISCAL EMITIDA |
||
| 124 | // Número: 1 Série: 1 Emissão: 25/03/2018 16:28:40 |
||
| 125 | // Protocolo de Autorização: 327180000056573 25/03/2018 16:28:42 |
||
| 126 | // http://nfce.sefaz.al.gov.br/QRCode/consultarNFCe.jsp?chNFe=27180321494821000100650010000000011000000011&nVersao=100&tpAmb=2&cDest=06256954459&dhEmi=323031382d30332d32355431363a32383a34302d30333a3030&vNF=11.50&vICMS=0.00&digVal=4e35376b3876386a4d3931483269386843546f4a767443354d41453d&cIdToken=000002&cHashQRCode=A0FC7AB12F30E66649C3FFFB275323FCD2AA0AD7 |
||
| 127 | nfce.setNumero("1"); |
||
| 128 | nfce.setSerie("1"); |
||
| 129 | nfce.setDataHoraEmissao("25/03/2018 16:28:40"); |
||
| 130 | nfce.setChave("2718 0321 4948 2100 0100 6500 1000 0000 0110 0000 0011"); |
||
| 131 | nfce.setProtocoloAutorizacao("327180000056573 25/03/2018 16:28:42"); |
||
| 132 | nfce.setCaminhoQrCode("http://nfce.sefaz.al.gov.br/QRCode/consultarNFCe.jsp?chNFe=27180321494821000100650010000000011000000011&nVersao=100&tpAmb=2&cDest=06256954459&dhEmi=323031382d30332d32355431363a32383a34302d30333a3030&vNF=11.50&vICMS=0.00&digVal=4e35376b3876386a4d3931483269386843546f4a767443354d41453d&cIdToken=000002&cHashQRCode=A0FC7AB12F30E66649C3FFFB275323FCD2AA0AD7"); |
||
| 133 | nfce.setCpfCnpjConsumidor("062.569.544-59"); |
||
| 134 | } |
||
| 135 | return nfce; |
||
| 136 | } |
||
| 137 | |||
| 138 | public void visualizarPdf(Venda venda) { |
||
| 139 | FiltroCupom filtro = new FiltroCupom(); |
||
| 140 | filtro.setNotaFiscal(preencherNotaFiscal(venda, false)); |
||
| 141 | JasperPrint relatorio = emissorRelatorioService.emitirComprovante(filtro); |
||
| 142 | byte[] bytePdf = null; |
||
| 143 | try { |
||
| 144 | bytePdf = JasperExportManager.exportReportToPdf(relatorio); |
||
| 145 | } catch (JRException e1) { |
||
| 146 | e1.printStackTrace(); |
||
| 147 | } |
||
| 148 | |||
| 149 | FacesContext fc = FacesContext.getCurrentInstance(); |
||
| 150 | HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse(); |
||
| 151 | response.reset(); |
||
| 152 | response.setContentType("application/pdf"); |
||
| 153 | response.setContentLength(bytePdf.length); |
||
| 154 | response.setHeader("Content-disposition", "inline; filename=arquivo.pdf"); |
||
| 155 | try { |
||
| 156 | response.getOutputStream().write(bytePdf); |
||
| 157 | response.getOutputStream().flush(); |
||
| 158 | response.getOutputStream().close(); |
||
| 159 | fc.responseComplete(); |
||
| 160 | } catch (IOException e) { |
||
| 161 | e.printStackTrace(); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | public void visualizarCupomParaImpressao(Venda venda) { |
||
| 166 | FiltroCupom filtro = new FiltroCupom(); |
||
| 167 | filtro.setNotaFiscal(preencherNotaFiscal(venda, false)); |
||
| 168 | JasperPrint relatorio = emissorRelatorioService.emitirComprovante(filtro); |
||
| 169 | |||
| 170 | byte[] imageBytes = null; |
||
| 171 | try { |
||
| 172 | BufferedImage bufferedImage = (BufferedImage) JasperPrintManager.printPageToImage(relatorio, 0, 2.0f); |
||
| 173 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
||
| 174 | ImageIO.write(bufferedImage,"png", baos ); |
||
| 175 | imageBytes = baos.toByteArray(); |
||
| 176 | } catch (Exception e1) { |
||
| 177 | e1.printStackTrace(); |
||
| 178 | } |
||
| 179 | |||
| 180 | FacesContext fc = FacesContext.getCurrentInstance(); |
||
| 181 | HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse(); |
||
| 182 | response.reset(); |
||
| 183 | response.setContentType("image/png"); |
||
| 184 | response.setContentLength(imageBytes.length); |
||
| 185 | response.setHeader("Content-disposition", "inline; filename=cupom.png"); |
||
| 186 | try { |
||
| 187 | response.getOutputStream().write(imageBytes); |
||
| 188 | response.getOutputStream().flush(); |
||
| 189 | response.getOutputStream().close(); |
||
| 190 | fc.responseComplete(); |
||
| 191 | } catch (IOException e) { |
||
| 192 | e.printStackTrace(); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | } |