Subversion Repositories Integrator Subversion

Rev

Rev 106 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

package br.com.ec.controller.managedbean;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.imageio.ImageIO;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletResponse;

import org.primefaces.model.StreamedContent;
import org.springframework.context.annotation.Scope;

import br.com.ec.domain.dto.FiltroCupom;
import br.com.ec.domain.dto.FiltroNfce;
import br.com.ec.domain.dto.nfce.NotaFiscalDTO;
import br.com.ec.domain.model.CompraProduto;
import br.com.ec.domain.model.Endereco;
import br.com.ec.domain.model.NotaFiscal;
import br.com.ec.domain.model.Venda;
import br.com.ec.domain.model.tipos.TipoRelatorioSistema;
import br.com.ec.domain.service.emissorrelatorio.EmissorRelatorioService;
import br.com.ec.domain.service.pessoa.PessoaService;
import br.com.ec.relatorio.util.RelatorioUtils;
import br.com.ec.web.exception.VerificadorLancamentoException;
import br.com.ec.web.exception.VerificadorLancamentoException.CommandRelatorioStreamedContent;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;

@Named
@Scope("view")
public class RelatorioBean implements Serializable {

        private static final long serialVersionUID = 1L;

        private EmissorRelatorioService emissorRelatorioService;
        private PessoaService pessoaService;
       
        @Inject
        public RelatorioBean(EmissorRelatorioService emissorRelatorioService, PessoaService pessoaService) {
                this.emissorRelatorioService = emissorRelatorioService;
                this.pessoaService = pessoaService;
        }
       
        public TipoRelatorioSistema[] getTiposRelatorioSistema() {
                return TipoRelatorioSistema.values();
        }
       
        public StreamedContent emitirNfce() {
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
                        @Override
                        public StreamedContent execute() {
                                FiltroNfce filtro = new FiltroNfce();
                               
                                NotaFiscal nfce = new NotaFiscal();
                               
//                              Pessoa pessoa = new Pessoa();
//                              pessoa.setNome("ESPAÇO CASE EPP");
//                              nfce.setEmitente(pessoa);
                               
                                List<CompraProduto> listaCompraProduto = new ArrayList<CompraProduto>();
                                /*
                                CompraProduto item = new CompraProduto();
                                Produto produto = new Produto();
                                produto.setCodigo("12345");
                                produto.setDescricao("TESTE");
                                item.setProduto(produto);
                                listaCompraProduto.add(item);
                                nfce.setListaCompraProduto(listaCompraProduto);
                               
                                filtro.setNotaFiscal(nfce);
                                */

                                JasperPrint relatorio = emissorRelatorioService.emitirComprovante(filtro);
                                return RelatorioUtils.gerarPdf(relatorio, "NFCE");
                        }
                });
        }
       
        public StreamedContent emitirNfceEmPdf(final Venda venda) {
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
                        @Override
                        public StreamedContent execute() {
                                System.out.println("Emitir nota fiscal...");
                                //NotaFiscal notaFiscalEmitida...
                               
                                FiltroNfce filtro = new FiltroNfce();
                                filtro.setNotaFiscal(preencherNotaFiscal(venda, true));
                                JasperPrint relatorio = emissorRelatorioService.emitirComprovante(filtro);
                                return RelatorioUtils.gerarPdf(relatorio, "NFCE");
                        }
                });
        }
       
        public StreamedContent emitirCupomEmPdf(final Venda venda) {
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
                        @Override
                        public StreamedContent execute() {
                                FiltroCupom filtro = new FiltroCupom();
                                filtro.setNotaFiscal(preencherNotaFiscal(venda, false));
                                JasperPrint relatorio = emissorRelatorioService.emitirComprovante(filtro);
                                return RelatorioUtils.gerarPdf(relatorio, "CUPOM");
                        }
                });
        }
       
        /************************************************************************/
       
        private NotaFiscalDTO preencherNotaFiscal(Venda venda, boolean notaFiscal) {
                NotaFiscalDTO nfce = new NotaFiscalDTO();
                nfce.setPessoaJuridica(pessoaService.detalharPessoa(venda.getPessoaJuridicaDaLoja()));
                nfce.setEnderecoEmitente(new ArrayList<Endereco>(nfce.getPessoaJuridica().getEnderecos()).get(0));
                nfce.preencherItens(venda.getLancamentosValidos());
                nfce.preencherPagamentos(venda.getVendaFormaPagamentos());
               
                if (notaFiscal) {
                        // PREENCHER COM DADOS DA NOTA FISCAL EMITIDA
        //              Número: 1 Série: 1 Emissão: 25/03/2018 16:28:40
        //              Protocolo de Autorização: 327180000056573   25/03/2018 16:28:42
        //              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
                        nfce.setNumero("1");
                        nfce.setSerie("1");
                        nfce.setDataHoraEmissao("25/03/2018 16:28:40");
                        nfce.setChave("2718 0321 4948 2100 0100 6500 1000 0000 0110 0000 0011");
                        nfce.setProtocoloAutorizacao("327180000056573   25/03/2018 16:28:42");
                        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");
                        nfce.setCpfCnpjConsumidor("062.569.544-59");
                }
                return nfce;
        }
       
        public void visualizarPdf(Venda venda) {
                FiltroCupom filtro = new FiltroCupom();
                filtro.setNotaFiscal(preencherNotaFiscal(venda, false));
                JasperPrint relatorio = emissorRelatorioService.emitirComprovante(filtro);
                byte[] bytePdf = null;
                try {
                        bytePdf = JasperExportManager.exportReportToPdf(relatorio);
                } catch (JRException e1) {
                        e1.printStackTrace();
                }
               
                FacesContext fc = FacesContext.getCurrentInstance();
            HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
            response.reset();
            response.setContentType("application/pdf");
            response.setContentLength(bytePdf.length);
            response.setHeader("Content-disposition", "inline; filename=arquivo.pdf");
            try {
                response.getOutputStream().write(bytePdf);
                response.getOutputStream().flush();
                response.getOutputStream().close();
                fc.responseComplete();
             } catch (IOException e) {
                 e.printStackTrace();
             }
        }
       
        public void visualizarCupomParaImpressao(Venda venda) {
                FiltroCupom filtro = new FiltroCupom();
                filtro.setNotaFiscal(preencherNotaFiscal(venda, false));
                JasperPrint relatorio = emissorRelatorioService.emitirComprovante(filtro);
               
                byte[] imageBytes = null;
                try {
                        BufferedImage bufferedImage = (BufferedImage) JasperPrintManager.printPageToImage(relatorio, 0, 2.0f);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageIO.write(bufferedImage,"png", baos );
                imageBytes = baos.toByteArray();
                } catch (Exception e1) {
                        e1.printStackTrace();
                }
               
                FacesContext fc = FacesContext.getCurrentInstance();
            HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
            response.reset();
            response.setContentType("image/png");
            response.setContentLength(imageBytes.length);
            response.setHeader("Content-disposition", "inline; filename=cupom.png");
            try {
                response.getOutputStream().write(imageBytes);
                response.getOutputStream().flush();
                response.getOutputStream().close();
                fc.responseComplete();
             } catch (IOException e) {
                 e.printStackTrace();
             }
        }
       
}