Subversion Repositories Integrator Subversion

Rev

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

package br.com.ec.controller.managedbean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

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

import org.apache.commons.lang.time.DateUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import org.springframework.context.annotation.Scope;

import br.com.ec.core.exception.NegocioException;
import br.com.ec.core.generic.GenericService;
import br.com.ec.core.util.DataUtils;
import br.com.ec.core.util.StringUtil;
import br.com.ec.core.util.VerificadorUtil;
import br.com.ec.domain.dto.CartaoBandeiraDTO;
import br.com.ec.domain.dto.CartaoDTO;
import br.com.ec.domain.dto.CartaoDataDTO;
import br.com.ec.domain.dto.CartaoResumoDTO;
import br.com.ec.domain.dto.CartaoResumoGeralDTO;
import br.com.ec.domain.model.FormaPagamento;
import br.com.ec.domain.model.Maquineta;
import br.com.ec.domain.model.VendaFormaPagamento;
import br.com.ec.domain.model.tipos.TipoCartao;
import br.com.ec.domain.service.bandeiracartao.BandeiraCartaoService;
import br.com.ec.domain.service.formapagamento.FormaPagamentoService;
import br.com.ec.domain.service.parcela.ParcelaService;
import br.com.ec.domain.service.vendaformapagamento.VendaFormaPagamentoService;
import br.com.ec.domain.shared.ConstantesSEC;
import br.com.ec.domain.shared.Ordenador;
import br.com.ec.domain.shared.OrdenadorImpl;
import br.com.ec.web.exception.VerificadorLancamentoException;
import br.com.ec.web.exception.VerificadorLancamentoException.CommandBean;
import br.com.ec.web.generic.AbstractBean;
import br.com.ec.web.message.LancadorMensagem;
import br.com.ec.web.util.DataUtil;

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

        private static final long serialVersionUID = 1L;
       
        private ParcelaService parcelaService;
        private VendaFormaPagamentoService vendaFormaPagamentoService;
        private BandeiraCartaoService bandeiraCartaoService;
        private FormaPagamentoService formaPagamentoService;
       
        private List<CartaoDTO> cartoes;
        private List<CartaoDTO> cartoesGerados;
        private CartaoResumoGeralDTO cartaoResumoGeral;
        private CartaoResumoDTO cartaoResumo;
       
        private Date dataVendas;
        private List<VendaFormaPagamento> listaVendaFormaPagamento;
        private List<CartaoDataDTO> listaDatas;
       
        private List<Maquineta> maquinetasSelecionadas;
        private Boolean indicadorVerificarMaquinetas = false;
        private String tipoArquivoSelecionado = ConstantesSEC.ArquivoBanco.ARQUIVO_BANCO_REDE_1;
       
        private List<String> linhas;
        private UploadedFile arquivoParaUpload;
        private Ordenador ordenador;
       
        @Inject
        public CartoesGerenciadorBean(ParcelaService parcelaService, VendaFormaPagamentoService vendaFormaPagamentoService,
                        BandeiraCartaoService bandeiraCartaoService, FormaPagamentoService formaPagamentoService) {
                this.parcelaService = parcelaService;
                this.vendaFormaPagamentoService = vendaFormaPagamentoService;
                this.bandeiraCartaoService = bandeiraCartaoService;
                this.formaPagamentoService = formaPagamentoService;
                this.ordenador = new OrdenadorImpl();
        }      
       
        @Override
        public void preCarregamento() {
                setDataVendas(DataUtils.getDataAtual());
                setCartaoResumoGeral(new CartaoResumoGeralDTO());
        }
       
        @Override
        public void limparEntidade() {
                setEntidade(new CartaoDTO());
                setCartoes(null);
                setCartoesGerados(null);
                setCartaoResumo(null);
        }

        @Override
        public GenericService<CartaoDTO> getService() {
                return null;
        }
       
        @Override
        public CartaoDTO getEntidade() {
                return entidade;
        }

        @Override
        public CartaoDTO getId() {
                return getEntidade();
        }
       
        public List<CartaoDTO> getCartoes() {
                return cartoes;
        }
        public void setCartoes(List<CartaoDTO> cartoes) {
                this.cartoes = cartoes;
        }
       
        public List<CartaoDTO> getCartoesGerados() {
                return cartoesGerados;
        }
        public void setCartoesGerados(List<CartaoDTO> cartoesGerados) {
                this.cartoesGerados = cartoesGerados;
        }
       
        public CartaoResumoGeralDTO getCartaoResumoGeral() {
                return cartaoResumoGeral;
        }
        public void setCartaoResumoGeral(CartaoResumoGeralDTO cartaoResumoGeral) {
                this.cartaoResumoGeral = cartaoResumoGeral;
        }
       
        public CartaoResumoDTO getCartaoResumo() {
                return cartaoResumo;
        }
        public void setCartaoResumo(CartaoResumoDTO cartaoResumo) {
                this.cartaoResumo = cartaoResumo;
        }
       
        public Date getDataVendas() {
                return dataVendas;
        }
        public void setDataVendas(Date dataVendas) {
                this.dataVendas = dataVendas;
        }
       
        public List<VendaFormaPagamento> getListaVendaFormaPagamento() {
                return listaVendaFormaPagamento;
        }
        public void setListaVendaFormaPagamento(List<VendaFormaPagamento> listaVendaFormaPagamento) {
                this.listaVendaFormaPagamento = listaVendaFormaPagamento;
        }
       
        public List<CartaoDataDTO> getListaDatas() {
                return listaDatas;
        }
        public void setListaDatas(List<CartaoDataDTO> listaDatas) {
                this.listaDatas = listaDatas;
        }
       
        public List<Maquineta> getMaquinetasSelecionadas() {
                return maquinetasSelecionadas;
        }
        public void setMaquinetasSelecionadas(List<Maquineta> maquinetasSelecionadas) {
                this.maquinetasSelecionadas = maquinetasSelecionadas;
        }
        public String maquinetasSelecionadasDescricao() {
                String descricao = "";
                for (Maquineta m : getMaquinetasSelecionadas()) {
                        descricao = descricao + m.getEmpresaAdquirente().getDescricao() + ": " + m.getDescricao() + "; ";
                }
                return descricao;
        }
       
        public Boolean getIndicadorVerificarMaquinetas() {
                return indicadorVerificarMaquinetas;
        }
        public void setIndicadorVerificarMaquinetas(Boolean indicadorVerificarMaquinetas) {
                this.indicadorVerificarMaquinetas = indicadorVerificarMaquinetas;
        }
       
        public String getTipoArquivoSelecionado() {
                return tipoArquivoSelecionado;
        }
        public void setTipoArquivoSelecionado(String tipoArquivoSelecionado) {
                this.tipoArquivoSelecionado = tipoArquivoSelecionado;
        }
       
        public List<String> getLinhas() {
                return linhas;
        }
        public void setLinhas(List<String> linhas) {
                this.linhas = linhas;
        }
       
        public UploadedFile getArquivoParaUpload() {
                return arquivoParaUpload;
        }
        public void setArquivoParaUpload(UploadedFile arquivoParaUpload) {
                this.arquivoParaUpload = arquivoParaUpload;
        }
       
        public void atualizarConsultaComArquivo() {
                limparEntidade();
                cartaoResumo = new CartaoResumoDTO();
                consultarComArquivo();
        }
       
        public void uploadArquivoCsv(final FileUploadEvent e) {
        new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                limparEntidade();
                                BufferedReader reader = null;
                                List<String> linhas = new ArrayList<String>();
                                try {
                                        reader = new BufferedReader(new InputStreamReader(e.getFile().getInputstream()));
                                        String linha = reader.readLine();
                                        cartaoResumo = new CartaoResumoDTO();
                                while ((linha = reader.readLine()) != null) {
                                        linhas.add(linha);
                                }
                                } catch (Exception ex) {
                                        ex.printStackTrace();
                                } finally {
                                        try {
                                                reader.close();
                                        } catch (IOException exc) {
                                                exc.printStackTrace();
                                        }
                                }
                                setLinhas(linhas);
                                consultarComArquivo();
                        }
                });
    }
       
        public void consultarComArquivo() {
                try {
                        if (getTipoArquivoSelecionado().equals(ConstantesSEC.ArquivoBanco.ARQUIVO_BANCO_REDE_1)) {
                                arquivoBancoRede(getCartaoResumoGeral().getDataInicialVendasSelecionada(), getCartaoResumoGeral().getDataFinalVendasSelecionada(), getMaquinetasSelecionadas(), getIndicadorVerificarMaquinetas());
                        }
                        if (getTipoArquivoSelecionado().equals(ConstantesSEC.ArquivoBanco.ARQUIVO_BANCO_SAFRA_2)) {
                                arquivoBancoSafra(getCartaoResumoGeral().getDataInicialVendasSelecionada(), getCartaoResumoGeral().getDataFinalVendasSelecionada());
                        }
                        if (getTipoArquivoSelecionado().equals(ConstantesSEC.ArquivoBanco.ARQUIVO_BANCO_PAGSEGURO_3)) {
                                arquivoBancoPagseguro(getCartaoResumoGeral().getDataInicialVendasSelecionada(), getCartaoResumoGeral().getDataFinalVendasSelecionada());
                        }
                setListaVendaFormaPagamento(vendaFormaPagamentoService.consultarLancamentosCartoesNoPeriodo(getCartaoResumoGeral().getDataInicialVendasSelecionada(), getCartaoResumoGeral().getDataFinalVendasSelecionada(), null, getMaquinetasSelecionadas()));
                for (VendaFormaPagamento vendaFormaPagamento : getListaVendaFormaPagamento()) {
                        if (VerificadorUtil.estaNulo(vendaFormaPagamento.getCupom())) {
                                preencherDados(vendaFormaPagamento);
                        }
                }
               
                List<CartaoResumoDTO> resumosPorDia = new ArrayList<CartaoResumoDTO>();
                        Date dataSelecionada = getCartaoResumoGeral().getDataInicialVendasSelecionada();
                        while (DataUtils.dataEstaIgualOuEntrePeriodos(dataSelecionada, DataUtils.getDataComHorarioMinimo(getCartaoResumoGeral().getDataInicialVendasSelecionada()), DataUtils.getDataComHorarioMinimo(getCartaoResumoGeral().getDataFinalVendasSelecionada()))) {
                                CartaoResumoDTO resumoPorDia = new CartaoResumoDTO();
                                resumoPorDia.setDataResumo(dataSelecionada);
                                for (CartaoBandeiraDTO cartaoPorBandeira : cartaoResumo.getCartoesPorBandeira()) {
                                        if (DataUtils.dataEstaIgualOuEntrePeriodos(cartaoPorBandeira.getData(), DataUtils.getDataComHorarioMinimo(dataSelecionada), DataUtils.getDataComHorarioMaximo(dataSelecionada))) {
                                                resumoPorDia.getCartoesPorBandeira().add(cartaoPorBandeira);
                                        }
                                }
                                dataSelecionada = DataUtils.acrescentarDias(dataSelecionada, 1);
                                resumoPorDia.somarValores();
                                resumosPorDia.add(resumoPorDia);
                        }
                        cartaoResumo.setCartoesPorBandeira((List<CartaoBandeiraDTO>) ordenador.ordenar(cartaoResumo.getCartoesPorBandeira()));
                        getCartaoResumoGeral().setCartoesResumo(resumosPorDia);
                } catch (Exception e) {
                        throw new NegocioException(e.getMessage());
                } finally {
                        cartaoResumo.setCartoesPorBandeira((List<CartaoBandeiraDTO>) ordenador.ordenar(cartaoResumo.getCartoesPorBandeira()));
                }
        }
       
        private void arquivoBancoRede(Date dataInicial, Date dataFinal, List<Maquineta> maquinetasSelecionadas, Boolean indicadorVerificarMaquinetas) {
                for (String linha : getLinhas()) {
                        Boolean adicionar = true;
                String[] dadosSeparados = linha.split(";");
                Date dataLinha = DataUtil.retornarDataApartirString("dd/MM/yyyy hh:mm:ss", dadosSeparados[0] + " " + dadosSeparados[1]);
                if (DataUtils.dataEstaIgualOuEntrePeriodos(DataUtil.retornarDataFormatada("dd/MM/yyyy", dataLinha), dataInicial, dataFinal)) {
                        if (indicadorVerificarMaquinetas) {
                                if (!maquinetasSelecionadas.isEmpty()) {
                                        for (Maquineta maquinetaSelecionada : maquinetasSelecionadas) {
                                                adicionar = false;
                                                String maquineta = dadosSeparados[19];
                                                if (maquinetaSelecionada.getDescricao().equals(maquineta)) {
                                                        adicionar = true;
                                                        break;
                                                }
                                        }
                                }
                        }
                        if (adicionar) {
                                        CartaoDTO cartao = new CartaoDTO();
                                        cartao.setBandeira(StringUtil.setarUpperCaseComTrim(dadosSeparados[9]));
                                FormaPagamento formaPagamento = null;
                                String bandeira = StringUtil.setarUpperCaseComTrim(dadosSeparados[5]);
                                String tipoCartao = null;
                                if (bandeira.equals("DÉBITO") || bandeira.equals("DéBITO")) {
                                        tipoCartao = TipoCartao.DEBITO.getValor();
                                        formaPagamento = new FormaPagamento(ConstantesSEC.FormasDePagamento.FORMAPAGAMENTO_DEBITO);
                                        if (cartao.getBandeira().equals("MASTERCARD")) {
                                                cartao.setBandeira("MAESTRO");
                                        }
                                } else {
                                        tipoCartao = TipoCartao.CREDITO.getValor();
                                        String parcelas = dadosSeparados[8];
                                        formaPagamento = new FormaPagamento(FormaPagamento.recuperarCodigoDaFormaDePagamento(parcelas));
                                }
                                cartao.setBrutoImportado(new Double(dadosSeparados[3].replace(".", "").replace(",", ".")));
                                preencherDadosArquivo(dataLinha, cartao, formaPagamento, tipoCartao);
                        }
                        }
                }
        }
       
        private void arquivoBancoSafra(Date dataInicial, Date dataFinal) {
                for (String linha : getLinhas()) {
                String[] dadosSeparados = linha.split(";");
                Date dataLinha = DataUtil.retornarDataApartirString("dd/MM/yyyy hh.mm.ss", dadosSeparados[4] + " " + dadosSeparados[5]);
                if (DataUtils.dataEstaIgualOuEntrePeriodos(DataUtil.retornarDataFormatada("dd/MM/yyyy", dataLinha), dataInicial, dataFinal)) {
                                CartaoDTO cartao = new CartaoDTO();
                                cartao.setBandeira(StringUtil.setarUpperCaseComTrim(dadosSeparados[7]));
                        FormaPagamento formaPagamento = null;
                        String bandeira = StringUtil.setarUpperCaseComTrim(dadosSeparados[9]);
                        String tipoCartao = null;
                        if (bandeira.equals("00")) {
                                tipoCartao = TipoCartao.DEBITO.getValor();
                                formaPagamento = new FormaPagamento(ConstantesSEC.FormasDePagamento.FORMAPAGAMENTO_DEBITO);
                                if (cartao.getBandeira().equals("MASTERCARD")) {
                                        cartao.setBandeira("MAESTRO");
                                }
                        } else {
                                String parcelas = "";
                                if (bandeira.substring(0, 1).equals("0")) {
                                        parcelas = dadosSeparados[9].substring(1);
                                } else {
                                        parcelas = dadosSeparados[9];
                                }
                                tipoCartao = TipoCartao.CREDITO.getValor();
                                formaPagamento = new FormaPagamento(FormaPagamento.recuperarCodigoDaFormaDePagamento(parcelas));
                        }
                        cartao.setBrutoImportado(new Double(dadosSeparados[11].replace(".", "").replace(",", ".")));
                        preencherDadosArquivo(dataLinha, cartao, formaPagamento, tipoCartao);
                        }
                }
        }
       
        private void arquivoBancoPagseguro(Date dataInicial, Date dataFinal) {
                for (String linha : getLinhas()) {
                        Boolean adicionar = true;
                String[] dadosSeparados = linha.split(";");
                Date dataLinha = DataUtil.retornarDataApartirString("dd/MM/yyyy hh:mm", dadosSeparados[2]);
                if (DataUtils.dataEstaIgualOuEntrePeriodos(DataUtil.retornarDataFormatada("dd/MM/yyyy", dataLinha), dataInicial, dataFinal)) {
                        if (indicadorVerificarMaquinetas) {
                                if (!maquinetasSelecionadas.isEmpty()) {
                                        for (Maquineta maquinetaSelecionada : maquinetasSelecionadas) {
                                                adicionar = false;
                                                String maquineta = dadosSeparados[3];
                                                if (maquinetaSelecionada.getDescricao().equals(maquineta)) {
                                                        adicionar = true;
                                                        break;
                                                }
                                        }
                                }
                        }
                        if (adicionar) {
                                        CartaoDTO cartao = new CartaoDTO();
                                        cartao.setBandeira(StringUtil.setarUpperCaseComTrim(dadosSeparados[5]));
                                FormaPagamento formaPagamento = null;
                                String bandeira = StringUtil.setarUpperCaseComTrim(dadosSeparados[4]);
                                String tipoCartao = null;
                                if (bandeira.equals("Débito") || bandeira.equals("D�BITO")) {
                                        tipoCartao = TipoCartao.DEBITO.getValor();
                                        formaPagamento = new FormaPagamento(ConstantesSEC.FormasDePagamento.FORMAPAGAMENTO_DEBITO);
                                        if (cartao.getBandeira().equals("MasterCard") || cartao.getBandeira().equals("MASTERCARD")) {
                                                cartao.setBandeira("MAESTRO");
                                        }
                                } else {
                                        tipoCartao = TipoCartao.CREDITO.getValor();
                                        String parcelas = dadosSeparados[6];
                                        if (parcelas.equals("À vista") || parcelas.equals("� vista")) {
                                                parcelas = "1";
                                        } else if (parcelas.equals("Parcelado")) {
                                                parcelas = "2";
                                        }
                                        formaPagamento = new FormaPagamento(FormaPagamento.recuperarCodigoDaFormaDePagamento(parcelas));
                                }
                                cartao.setBrutoImportado(new Double(dadosSeparados[7]));
                                preencherDadosArquivo(dataLinha, cartao, formaPagamento, tipoCartao);
                        }
                        }
                }
        }

        private void preencherDadosArquivo(Date data, CartaoDTO cartao, FormaPagamento formaPagamento, String tipoCartao) {
                cartao.setFormaDePagamento(formaPagamentoService.consultarPorId(formaPagamento));
                if (VerificadorUtil.naoEstaNulo(cartao.getBandeira())) {
                        if (cartao.getBandeira().equals("AMEX")) {
                                cartao.setBandeira("AMERICAN EXPRESS");
                        }
                        if (cartao.getBandeira().equals("HIPC")) {
                                cartao.setBandeira("HIPERCARD");
                        }
                }
               
                CartaoBandeiraDTO cartaoFormaDePagamento = new CartaoBandeiraDTO();
                cartaoFormaDePagamento.setBandeira(bandeiraCartaoService.consultarBandeiraCartoao(cartao.getBandeira(), tipoCartao));
                if (VerificadorUtil.estaNulo(cartaoFormaDePagamento.getBandeira())) {
                        cartaoFormaDePagamento.setBandeira(bandeiraCartaoService.consultarBandeiraCartoao("OUTROS", tipoCartao));
                }
                cartaoFormaDePagamento.setBrutoImportado(cartao.getBrutoImportado());
                cartaoFormaDePagamento.setFormaPagamento(cartao.getFormaDePagamento());
                cartaoFormaDePagamento.setData(DataUtil.retornarDataFormatada("dd/MM/yyyy", data));
                cartaoResumo.adicionarPagamentoImportado(cartaoFormaDePagamento);
        }
       
        private void preencherDados(VendaFormaPagamento vendaFormaPagamento) {
                CartaoBandeiraDTO cartaoFormaDePagamento = new CartaoBandeiraDTO();
                cartaoFormaDePagamento.setBandeira(vendaFormaPagamento.getBandeiraCartao());
                cartaoFormaDePagamento.setBruto(vendaFormaPagamento.getValorPagamento());
                cartaoFormaDePagamento.setFormaPagamento(vendaFormaPagamento.getFormaPagamento());
                cartaoFormaDePagamento.setData(DataUtil.retornarDataFormatada("dd/MM/yyyy", vendaFormaPagamento.getVenda().getDataVenda()));
                cartaoResumo.adicionarPagamento(cartaoFormaDePagamento);
        }
       
        public void verificarLancamentos() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                limparEntidade();
                                List<VendaFormaPagamento> preLancamentos = new ArrayList<VendaFormaPagamento>();
                                preLancamentos = vendaFormaPagamentoService.consultarLancamentosCartoesNoPeriodo(dataVendas, dataVendas, null, getMaquinetasSelecionadas());
                                setListaVendaFormaPagamento(preLancamentos);
                               
                                cartoes = new ArrayList<CartaoDTO>();
                                cartaoResumo = new CartaoResumoDTO();
                                for (VendaFormaPagamento vendaFormaPagamento : preLancamentos) {
                                        if (VerificadorUtil.estaNulo(vendaFormaPagamento.getCupom())) {
                                                CartaoDTO cartao = new CartaoDTO();
                                                cartao.setBandeira(vendaFormaPagamento.getBandeiraCartao().getDescricao());
                                                cartao.setFormaDePagamento(vendaFormaPagamento.getFormaPagamento());
                                                cartao.setBruto(vendaFormaPagamento.getValorPagamento());
                                                cartao.setDataVenda(vendaFormaPagamento.getVenda().getDataVenda());
                                       
                                                if (parcelaService.verificarPossuiParcelaAberta(vendaFormaPagamento.getVenda())) {
                                                        cartao.setParcelaGerada(true);
                                                        cartao.setGerarParcela(false);
                                                } else {
                                                        cartao.setParcelaGerada(false);
                                                        cartao.setGerarParcela(true);
                                                }
                                                cartao.setVendaFormaPagamento(vendaFormaPagamento);
                                                cartoes.add(cartao);
                                               
                                                CartaoBandeiraDTO cartaoFormaDePagamento = new CartaoBandeiraDTO();
                                                cartaoFormaDePagamento.setBandeira(vendaFormaPagamento.getBandeiraCartao());
                                                cartaoFormaDePagamento.setBruto(vendaFormaPagamento.getValorPagamento());
                                                cartaoFormaDePagamento.setFormaPagamento(vendaFormaPagamento.getFormaPagamento());
                                                cartaoResumo.adicionarPagamento(cartaoFormaDePagamento);
                                        }
                                }
                                cartoes = (List<CartaoDTO>) ordenador.ordenar(cartoes);
                                cartaoResumo.setCartoesPorBandeira((List<CartaoBandeiraDTO>) ordenador.ordenar(cartaoResumo.getCartoesPorBandeira()));
                        }
                });
        }
       
        public void consultarLancamentos() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                limparEntidade();
                                List<CartaoDTO> cartoesGerados = parcelaService.consultarContasDosCartoes(dataVendas, dataVendas);
                                adicionarDatasGeradas(cartoesGerados);
                                setCartoesGerados(cartoesGerados);
                                LancadorMensagem.lancarSucesso("PARCELAS GERADAS COM SUCESSO");
                        }
                });
        }
       
        public void gerarLancamentos() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                listaVendaFormaPagamento = new ArrayList<VendaFormaPagamento>();
                                for (CartaoDTO cartao : cartoes) {
                                        if (cartao.getGerarParcela()) {
                                                listaVendaFormaPagamento.add(cartao.getVendaFormaPagamento());
                                        }
                                }
                                limparEntidade();
                                List<CartaoDTO> cartoesGerados = parcelaService.gerarContasDosCartoes(listaVendaFormaPagamento);
                                adicionarDatasGeradas(cartoesGerados);
                                setCartoesGerados(cartoesGerados);
                                LancadorMensagem.lancarSucesso("PARCELAS GERADAS COM SUCESSO");
                        }

                });
        }
       
        private void adicionarDatasGeradas(List<CartaoDTO> cartoesGerados) {
                listaDatas = new ArrayList<CartaoDataDTO>();
                for (CartaoDTO cartaoGerado : cartoesGerados) {
                        CartaoDataDTO cartaoDataDTO = new CartaoDataDTO();
                        cartaoDataDTO.setDataVenda(DateUtils.truncate(cartaoGerado.getParcela().getDataVencimento(), Calendar.DATE));
                        cartaoDataDTO.setDataNovaVenda(DateUtils.truncate(cartaoGerado.getParcela().getDataVencimento(), Calendar.DATE));
                        boolean adicionar = true;
                        for (CartaoDataDTO dataAdicionada : listaDatas) {
                                if (dataAdicionada.getDataVenda().equals(cartaoDataDTO.getDataVenda())) {
                                        adicionar = false;
                                        break;
                                }
                        }
                        if (adicionar) {
                                listaDatas.add(cartaoDataDTO);
                        }
                }
        }
       
        public void atualizarDatasCartoes() {
                for (CartaoDTO cartaoGerado : getCartoesGerados()) {
                        for (CartaoDataDTO dataAdicionada : getListaDatas()) {
                                Date dataTruncada = DateUtils.truncate(cartaoGerado.getParcela().getDataVencimento(), Calendar.DATE);
                                if (dataAdicionada.getDataVenda().equals(dataTruncada)) {
                                        Date novaData = atualizarParaNovaData(cartaoGerado.getParcela().getDataVencimento(), dataAdicionada.getDataNovaVenda());
                                        cartaoGerado.getParcela().setDataVencimento(novaData);
                                }
                        }
                }
                adicionarDatasGeradas(getCartoesGerados());
                salvarAlteracoesCartoes();
        }
       
        private Date atualizarParaNovaData(Date data, Date novaData) {
            Calendar c = Calendar.getInstance();
            c.setTime(data);
           
            Calendar cal = Calendar.getInstance();
            cal.setTime(novaData);
            c.set(Calendar.DATE, cal.get(Calendar.DATE));
            c.set(Calendar.MONTH, cal.get(Calendar.MONTH));
            c.set(Calendar.YEAR, cal.get(Calendar.YEAR));
            return c.getTime();
        }
       
        public void salvarAlteracoesCartoes() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                for (CartaoDTO cartao : getCartoesGerados()) {
                                        parcelaService.alterar(cartao.getParcela());
                                }
                                LancadorMensagem.lancarSucesso("PARCELAS ATUALIZADAS COM SUCESSO");
                        }
                });
        }
       
        /*
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\arquivo.csv")));
        String linha = reader.readLine();
       
        List<CartaoDTO> cartoes = new ArrayList<CartaoDTO>();
                CartaoResumoDTO cartaoResumo = new CartaoResumoDTO();
                Double valorTotalBrutoCartoes = new Double(0.0);
        while ((linha = reader.readLine()) != null) {
                String[] dadosSeparados = linha.split(";");
                        CartaoDTO cartao = new CartaoDTO();
                        cartao.setBandeira(StringUtil.setarUpperCaseComTrim(dadosSeparados[5]));
                FormaPagamento formaPagamento = null;
                String bandeira = StringUtil.setarUpperCaseComTrim(dadosSeparados[4]);
                String tipoCartao = null;
                if (bandeira.equals("DÉBITO")) {
                        tipoCartao = TipoCartao.DEBITO.getValor();
                        formaPagamento = new FormaPagamento(ConstantesSEC.FormasDePagamento.FORMAPAGAMENTO_DEBITO);
                } else {
                        tipoCartao = TipoCartao.CREDITO.getValor();
                        String parcelas = dadosSeparados[3];
                        formaPagamento = new FormaPagamento(FormaPagamento.recuperarCodigoDaFormaDePagamento(parcelas));
                }
                cartao.setFormaDePagamento(formaPagamento);
                        cartao.setBruto(new Double(dadosSeparados[2]));
                        cartao.setDataVenda(DataUtil.retornarDataApartirString("dd/MM/yyyy", dadosSeparados[0]));
                       
                        cartoes.add(cartao);
//                              somarValoresBruto(vendaFormaPagamento.getValorPagamento(), vendaFormaPagamento.getFormaPagamento());
//                              somarValoresBruto(cartao.getBruto(), cartao.getFormaDePagamento());
                       
                        CartaoBandeiraDTO cartaoFormaDePagamento = new CartaoBandeiraDTO();
                       
                        BandeiraCartao bandeiraCartao = bandeiraCartaoService.consultarBandeiraCartoao(cartao.getBandeira(), tipoCartao);
                        cartaoFormaDePagamento.setBandeira(bandeiraCartao);
                        cartaoFormaDePagamento.setBruto(cartao.getBruto());
                        cartaoFormaDePagamento.setFormaPagamento(cartao.getFormaDePagamento());
                        cartaoResumo.adicionarPagamento(cartaoFormaDePagamento);
                }
        reader.close();
//            System.out.println(Arrays.toString(dadosSeparados));
//            System.out.println("Nome: " + dadosSeparados[0]);
//            System.out.println("País: " + dadosSeparados[1]);
//            System.out.println("Fórum: " + dadosSeparados[2]);
//            System.out.println("--------------------------");
    }
        */

        /***************************************************************/
       
}