Subversion Repositories Integrator Subversion

Rev

Rev 474 | Rev 478 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

package br.com.ec.controller;

import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

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

import br.com.ec.controller.consultademanda.NotaFiscalConsultaPorDemanda;
import br.com.ec.core.generic.GenericService;
import br.com.ec.core.util.VerificadorUtil;
import br.com.ec.domain.dto.CompraProdutoDTO;
import br.com.ec.domain.dto.NotaFiscalDTO;
import br.com.ec.domain.dto.NotaFiscalProdutoDTO;
import br.com.ec.domain.dto.ProdutoDTO;
import br.com.ec.domain.dto.consulta.ParametrosConsultaNotaFiscal;
import br.com.ec.domain.model.NotaFiscal;
import br.com.ec.domain.model.Pessoa;
import br.com.ec.domain.model.Transportadora;
import br.com.ec.domain.model.Transporte;
import br.com.ec.domain.model.Venda;
import br.com.ec.domain.model.nfe.TipoModeloNotaFiscal;
import br.com.ec.domain.model.nfe.TipoNotaFiscal;
import br.com.ec.domain.model.tipos.TipoModalidadeFrete;
import br.com.ec.domain.service.CompraProdutoService;
import br.com.ec.domain.service.EmissorRelatorioService;
import br.com.ec.domain.service.NotaFiscalProdutoService;
import br.com.ec.domain.service.NotaFiscalService;
import br.com.ec.domain.service.ProdutoService;
import br.com.ec.web.exception.VerificadorLancamentoException;
import br.com.ec.web.exception.VerificadorLancamentoException.CommandBean;
import br.com.ec.web.exception.VerificadorLancamentoException.CommandRelatorioStreamedContent;
import br.com.ec.web.generic.AbstractBean;
import br.com.ec.web.message.LancadorMensagem;
import br.com.ec.web.util.TipoOperacao;

@Named
@Scope("view")
public class NotaFiscalBean extends AbstractBean<NotaFiscal> implements Serializable {

        private static final long serialVersionUID = 1L;
       
        private String statusSefaz = "";
       
        private ParametrosConsultaNotaFiscal parametrosConsultaNotaFiscal;
        private NotaFiscalConsultaPorDemanda lazy;
       
        private Long sequencialVendaParaVinculo;
        private StreamedContent danfeNfePreVisualizacao;
        private StreamedContent danfeNfe;
       
        private String codigoProdutoParaVinculoNotaFiscal = "";
        private Integer quantidadeProdutoParaVinculoNotaFiscal = 0;
        private ProdutoDTO produtoDTOParaVinculoNotaFiscal;
        private CompraProdutoDTO compraProdutoDTOParaVinculoNotaFiscal;
        private List<CompraProdutoDTO> listaCompraProdutoDTOParaVinculoNotaFiscal;
       
        private String chaveNotaFiscalParaReferenciaNotaFiscal = "";
        private NotaFiscalDTO notaFiscalDTOParaReferenciaNotaFiscal;
       
        private NotaFiscalService notaFiscalService;
        private NotaFiscalProdutoService notaFiscalProdutoService;
        private ProdutoService produtoService;
        private CompraProdutoService compraProdutoService;
        private EmissorRelatorioService emissorRelatorioService;
       
        @Inject
        public NotaFiscalBean(NotaFiscalConsultaPorDemanda lazy, NotaFiscalService notaFiscalService, NotaFiscalProdutoService notaFiscalProdutoService,
                        ProdutoService produtoService, CompraProdutoService compraProdutoService,
                        EmissorRelatorioService emissorRelatorioService) {
                this.lazy = lazy;
                this.notaFiscalService = notaFiscalService;
                this.notaFiscalProdutoService = notaFiscalProdutoService;
                this.produtoService = produtoService;
                this.compraProdutoService = compraProdutoService;
                this.emissorRelatorioService = emissorRelatorioService;
        }
       
        @Override
        public void preCarregamento() {
                parametrosConsultaNotaFiscal = new ParametrosConsultaNotaFiscal();
                prepararConsultaDemanda();
                limparEntidade();
        }

        @Override
        public void limparEntidade() {
                setEntidade(new NotaFiscal());
        }

        @Override
        public GenericService<NotaFiscal> getService() {
                return notaFiscalService;
        }
       
        @Override
        public NotaFiscal getEntidade() {
                return entidade;
        }
       
        @Override
        public void preConsultar() {
                setTipoOperacao(TipoOperacao.CONSULTAR);
        }
       
        @Override
        public void preAlterar(NotaFiscal notaFiscal) {
                if (VerificadorUtil.estaNulo(notaFiscal.getTransporte())) {
                        notaFiscal.setTransporte(new Transporte());
                }
                super.preAlterar(notaFiscal);
        }
       
        /*
        @Override
        public void preAlterar(NotaFiscal notaFiscal) {
                NotaFiscal notaFiscalDetalhada = notaFiscalService.detalhar(notaFiscal.getSequencial());
                super.preAlterar(notaFiscalDetalhada);
        }
        */

       
        @Override
        public NotaFiscal getId() {
                return getEntidade();
        }
       
        public ParametrosConsultaNotaFiscal getParametrosConsultaNotaFiscal() {
                return parametrosConsultaNotaFiscal;
        }
        public void setParametrosConsultaNotaFiscal(ParametrosConsultaNotaFiscal parametrosConsultaNotaFiscal) {
                this.parametrosConsultaNotaFiscal = parametrosConsultaNotaFiscal;
        }
       
        public NotaFiscalConsultaPorDemanda getLazy() {
                return lazy;
        }
        public void setLazy(NotaFiscalConsultaPorDemanda lazy) {
                this.lazy = lazy;
        }
        public void prepararConsultaDemanda() {
                lazy.setarParametrosConsulta(getParametrosConsultaNotaFiscal());
        }
       
        public TipoNotaFiscal[] getTiposNotaFiscal() {
                return TipoNotaFiscal.values();
        }
       
        public TipoModalidadeFrete[] getTiposModalidadeFrete() {
                return TipoModalidadeFrete.values();
        }
       
        public Long getSequencialVendaParaVinculo() {
                return sequencialVendaParaVinculo;
        }
        public void setSequencialVendaParaVinculo(Long sequencialVendaParaVinculo) {
                this.sequencialVendaParaVinculo = sequencialVendaParaVinculo;
        }
       
        public StreamedContent getDanfeNfePreVisualizacao() {
                return danfeNfePreVisualizacao;
        }
        public void setDanfeNfePreVisualizacao(StreamedContent danfeNfePreVisualizacao) {
                this.danfeNfePreVisualizacao = danfeNfePreVisualizacao;
        }
       
        public StreamedContent getDanfeNfe() {
                return danfeNfe;
        }
        public void setDanfeNfe(StreamedContent danfeNfe) {
                this.danfeNfe = danfeNfe;
        }
       
        public String getCodigoProdutoParaVinculoNotaFiscal() {
                return codigoProdutoParaVinculoNotaFiscal;
        }
        public void setCodigoProdutoParaVinculoNotaFiscal(String codigoProdutoParaVinculoNotaFiscal) {
                this.codigoProdutoParaVinculoNotaFiscal = codigoProdutoParaVinculoNotaFiscal;
        }
       
        public Integer getQuantidadeProdutoParaVinculoNotaFiscal() {
                return quantidadeProdutoParaVinculoNotaFiscal;
        }
        public void setQuantidadeProdutoParaVinculoNotaFiscal(Integer quantidadeProdutoParaVinculoNotaFiscal) {
                this.quantidadeProdutoParaVinculoNotaFiscal = quantidadeProdutoParaVinculoNotaFiscal;
        }
       
        public ProdutoDTO getProdutoDTOParaVinculoNotaFiscal() {
                return produtoDTOParaVinculoNotaFiscal;
        }
        public void setProdutoDTOParaVinculoNotaFiscal(ProdutoDTO produtoDTOParaVinculoNotaFiscal) {
                this.produtoDTOParaVinculoNotaFiscal = produtoDTOParaVinculoNotaFiscal;
        }
       
        public CompraProdutoDTO getCompraProdutoDTOParaVinculoNotaFiscal() {
                return compraProdutoDTOParaVinculoNotaFiscal;
        }
        public void setCompraProdutoDTOParaVinculoNotaFiscal(CompraProdutoDTO compraProdutoDTOParaVinculoNotaFiscal) {
                this.compraProdutoDTOParaVinculoNotaFiscal = compraProdutoDTOParaVinculoNotaFiscal;
        }
       
        public List<CompraProdutoDTO> getListaCompraProdutoDTOParaVinculoNotaFiscal() {
                return listaCompraProdutoDTOParaVinculoNotaFiscal;
        }
        public void setListaCompraProdutoDTOParaVinculoNotaFiscal(List<CompraProdutoDTO> listaCompraProdutoDTOParaVinculoNotaFiscal) {
                this.listaCompraProdutoDTOParaVinculoNotaFiscal = listaCompraProdutoDTOParaVinculoNotaFiscal;
        }
       
        public String getChaveNotaFiscalParaReferenciaNotaFiscal() {
                return chaveNotaFiscalParaReferenciaNotaFiscal;
        }
        public void setChaveNotaFiscalParaReferenciaNotaFiscal(String chaveNotaFiscalParaReferenciaNotaFiscal) {
                this.chaveNotaFiscalParaReferenciaNotaFiscal = chaveNotaFiscalParaReferenciaNotaFiscal;
        }
       
        public NotaFiscalDTO getNotaFiscalDTOParaReferenciaNotaFiscal() {
                return notaFiscalDTOParaReferenciaNotaFiscal;
        }
        public void setNotaFiscalDTOParaReferenciaNotaFiscal(NotaFiscalDTO notaFiscalDTOParaReferenciaNotaFiscal) {
                this.notaFiscalDTOParaReferenciaNotaFiscal = notaFiscalDTOParaReferenciaNotaFiscal;
        }
       
        public String getStatusSefaz() {
                return statusSefaz;
        }
        public void setStatusSefaz(String statusSefaz) {
                this.statusSefaz = statusSefaz;
        }
       
        /**************************************************/
       
        public void atualizarStatusSefaz() {
                setStatusSefaz(notaFiscalService.retornarStatusSefaz());
        }
       
        public void desvincularVenda() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                notaFiscalService.desvincularVenda(getEntidade().getSequencial());
                                getEntidade().setVenda(null);
                                LancadorMensagem.lancarSucesso("VENDA DESVINCULADA COM SUCESSO");
                        }
                });
        }
       
        public void vincularVenda() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                Venda vendaVinculada = notaFiscalService.vincularVenda(getEntidade().getSequencial(), getSequencialVendaParaVinculo());
                                getEntidade().setVenda(vendaVinculada);
                                LancadorMensagem.lancarSucesso("VENDA VINCULADA COM SUCESSO");
                        }
                });
        }
       
        public void emitirDanfeNfe(Long sequencialNotaFiscal) {
                setDanfeNfePreVisualizacao(notaFiscalService.gerarDanfeNfePreVisualizacao(sequencialNotaFiscal));
                setDanfeNfe(notaFiscalService.gerarDanfeNfe(sequencialNotaFiscal));
        }
       
        public void emitirDanfeNfePreVisualizacao(Long sequencialNotaFiscal) {
                setDanfeNfePreVisualizacao(notaFiscalService.gerarDanfeNfePreVisualizacao(sequencialNotaFiscal));
                setDanfeNfe(notaFiscalService.gerarDanfeNfe(sequencialNotaFiscal));
                /*
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
                        @Override
                        public StreamedContent execute() {
                                return notaFiscalService.gerarDanfeNfe(getEntidade().getSequencial());
                        }
                });
                */

        }
       
        public StreamedContent downloadNotaFiscal() {
                return new VerificadorLancamentoException().verificarLancamentoException(new CommandRelatorioStreamedContent() {
                        @Override
                        public StreamedContent execute() {
                                return getDanfeNfe();
                        }
                });
        }
       
        public void emitirDanfeNfeTeste() {
                Long sequencialNotaFiscal = new Long(168722);
                setDanfeNfePreVisualizacao(notaFiscalService.gerarDanfeNfePreVisualizacao(sequencialNotaFiscal));
                setDanfeNfe(notaFiscalService.gerarDanfeNfe(sequencialNotaFiscal));
        }
       
        public void selecionarEmitente(final Pessoa pessoa) {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                getEntidade().setPessoaEmitente(pessoa);
                                adicionandoParametroArgsConfirmandoAcao(true);
                        }
                });
        }
       
        public void retirarEmitente() {
                getEntidade().setPessoaEmitente(null);
        }
       
        public void selecionarDestinatario(final Pessoa pessoa) {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                getEntidade().setPessoaDestinatario(pessoa);
                                adicionandoParametroArgsConfirmandoAcao(true);
                        }
                });
        }
       
        public void retirarDestinatario() {
                getEntidade().setPessoaDestinatario(null);
        }
       
        /************************************/
        /** REMESSA **/
        /************************************/
       
        public void prepararNotaFiscalRemessa() {
                preCadastrar();
                getEntidade().setTipoModeloNotaFiscal(TipoModeloNotaFiscal.MODELO_NFE_55.getValor());
                getEntidade().setTipoNotaFiscal(TipoNotaFiscal.NFE_REMESSA.getValor());
        }
       
        public void emitirNotaFiscalRemessa() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                notaFiscalService.emitirNotaFiscalRemessa(getEntidade());
                                limparEntidade();
                                LancadorMensagem.lancarSucesso("NOTA DE REMESSA EMITIDA COM SUCESSO");
                        }
                });
        }
       
        public void consultarProdutosVinculados() {
                getEntidade().setListaProdutosVinculadosDTO(notaFiscalService.consultarProdutosVinculados(getEntidade().getSequencial()));
        }
       
        public void selecionarCompraProdutoParaVinculoNotaFiscal(CompraProdutoDTO compraProdutoDTO) {
                setCompraProdutoDTOParaVinculoNotaFiscal(compraProdutoDTO);
        }
       
       
       
       
        public void excluirVinculoNotaFiscalProduto(NotaFiscalProdutoDTO notaFiscalProdutoDTO) {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                notaFiscalProdutoService.excluirNotaFiscalProduto(notaFiscalProdutoDTO);
                                consultarProdutosVinculados();
                                LancadorMensagem.lancarSucesso("VÍNCULO EXCLUÍDO COM SUCESSO");
                        }
                });
        }
       
        public void consultarProdutoParaVinculoNotaFiscal() {
                setProdutoDTOParaVinculoNotaFiscal(produtoService.consultarProdutoDTOPorCodigoOuCodigoEAN(getCodigoProdutoParaVinculoNotaFiscal()));
                setListaCompraProdutoDTOParaVinculoNotaFiscal(produtoService.consultarComprasDoProduto(getProdutoDTOParaVinculoNotaFiscal().getSequencial()));
        }
       
        public void vinculoProdutoNaNotaFiscal() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                notaFiscalProdutoService.vinculoProdutoNaNotaFiscal(getEntidade().getSequencial(),
                                                getProdutoDTOParaVinculoNotaFiscal().getSequencial(),
                                                getCompraProdutoDTOParaVinculoNotaFiscal().getSequencial(),
                                                getQuantidadeProdutoParaVinculoNotaFiscal());
                                setCodigoProdutoParaVinculoNotaFiscal("");
                                setProdutoDTOParaVinculoNotaFiscal(null);
                                setCompraProdutoDTOParaVinculoNotaFiscal(null);
                                setQuantidadeProdutoParaVinculoNotaFiscal(0);
                                consultarProdutosVinculados();
                                LancadorMensagem.lancarSucesso("PRODUTO VINCULADO COM SUCESSO");
                        }
                });
        }
       
        public void consultarNotaFiscalParaReferenciarNotaFiscal() {
                setNotaFiscalDTOParaReferenciaNotaFiscal(notaFiscalService.consultarPorChave(getChaveNotaFiscalParaReferenciaNotaFiscal()));
        }
       
        /*
         private StreamedContent chart;
         public StreamedContent getChart() {
                 return chart;
         }
         private PieDataset createDataset() {
                 DefaultPieDataset dataset = new DefaultPieDataset();
                 dataset.setValue("New York", new Double(45.0));
                 dataset.setValue("London", new Double(15.0));
                 dataset.setValue("Paris", new Double(25.2));
                 dataset.setValue("Berlin", new Double(14.8));
                 return dataset;
         }
       
         private StreamedContent escala;
         public StreamedContent getEscala() {
                 return escala;
         }
         
         private StreamedContent escalaPdf;
         public StreamedContent getEscalaPdf() {
                 return escalaPdf;
         }
         public String generateRandomIdForNotCaching() {
                 return java.util.UUID.randomUUID().toString();
         }
       
         private void criarImagens() {
                 try {
                         InputStream arquivoPdf = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/imagens/escala-202310.pdf");
                         byte[] bytePdf = ArquivoUtil.retornarBytesPassandoInputStream(arquivoPdf);
                         ByteArrayOutputStream osPdf = new ByteArrayOutputStream();
                         PdfReader reader = new PdfReader(bytePdf);
                         escalaPdf = DefaultStreamedContent.builder()
                            .contentType("application/pdf")
                            .name("escala-202310.pdf")
                            .stream(() -> {
                                                try {
                                                        return ArquivoUtil.gerarInputStreamDeArquivo(bytePdf);
                                                } catch (IOException e1) {
                                                        e1.printStackTrace();
                                                }
                                                return arquivoPdf;
                                        })
                            .build();
                         
                         
                         InputStream arquivo = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/imagens/escala-202310.png");
                         BufferedImage imagem = ImageIO.read(arquivo);
                         ByteArrayOutputStream os = new ByteArrayOutputStream();
             ImageIO.write(imagem, "png", os);
                         escala = DefaultStreamedContent.builder()
                            .contentType("image/png")
                            .stream(() -> new ByteArrayInputStream(os.toByteArray()))
                            .build();
                       
                         
                        chart = DefaultStreamedContent.builder()
                            .contentType("image/png")
                            .stream(() -> {
                                try {
                                    JFreeChart jfreechart = ChartFactory.createPieChart("Cities", createDataset(), true, true, false);
                                    File chartFile = new File("dynamichart");
                                    ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
                                    return new FileInputStream(chartFile);
                                }
                                catch (Exception e) {
                                    e.printStackTrace();
                                    return null;
                                }
                            })
                            .build();
                 } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                        }
        }
        */

}