Subversion Repositories Integrator Subversion

Rev

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

package br.com.ec.controller.managedbean;

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

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

import org.primefaces.event.SelectEvent;
import org.springframework.context.annotation.Scope;

import br.com.ec.controller.managedbean.datamodel.ParcelaDataModel;
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.ContaGerenciadorDTO;
import br.com.ec.domain.model.Categoria;
import br.com.ec.domain.model.Compra;
import br.com.ec.domain.model.Conta;
import br.com.ec.domain.model.FormaPagamento;
import br.com.ec.domain.model.Parcela;
import br.com.ec.domain.model.Pessoa;
import br.com.ec.domain.model.Venda;
import br.com.ec.domain.model.tipos.TipoConta;
import br.com.ec.domain.service.categoria.CategoriaService;
import br.com.ec.domain.service.compra.CompraService;
import br.com.ec.domain.service.conta.ContaService;
import br.com.ec.domain.service.formapagamento.FormaPagamentoService;
import br.com.ec.domain.service.parcela.ParcelaService;
import br.com.ec.domain.shared.ConstantesSEC;
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;

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

        private static final long serialVersionUID = 1L;
       
        private ParcelaService parcelaService;
        private ContaService contaService;
        private CompraService compraService;
        private CategoriaService categoriaService;
        private FormaPagamentoService formaPagamentoService;
       
        private String chavePagamento;
        private ParcelaDataModel parcelas;
        private Long vincularConta;
        private List<FormaPagamento> formasDePagamento;

        @Inject
        public ContaGerenciadorBean(ParcelaService parcelaService, ContaService contaService, CompraService compraService, CategoriaService categoriaService,
                        FormaPagamentoService formaPagamentoService) {
                this.parcelaService = parcelaService;
                this.contaService = contaService;
                this.compraService = compraService;
                this.categoriaService = categoriaService;
                this.formaPagamentoService = formaPagamentoService;
        }
       
        @Override
        public void preCarregamento() {
                setEntidade(new ContaGerenciadorDTO());
        }
       
        @Override
        public void limparEntidade() {
                setEntidade(new ContaGerenciadorDTO());
                setParcelas(null);
        }

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

        @Override
        public ContaGerenciadorDTO getId() {
                return getEntidade();
        }
       
        public String getChavePagamento() {
                return chavePagamento;
        }
        public void setChavePagamento(String chavePagamento) {
                this.chavePagamento = chavePagamento;
        }
        public void formatarChavePagamento() {
                if (VerificadorUtil.naoEstaNulo(getChavePagamento())) {
                        setChavePagamento(StringUtil.retornarApenasNumeros(getChavePagamento()));
                }
        }
       
        public ParcelaDataModel getParcelas() {
                preencherParcelas();
        return parcelas;
    }
    public void setParcelas(ParcelaDataModel parcelas) {
        this.parcelas = parcelas;
    }
    public void preencherParcelas() {
        Parcela parcela = new Parcela();
        List<Parcela> listaParcelas = new ArrayList<Parcela>();
        parcela.setConta(getEntidade().getConta());
        if (VerificadorUtil.naoEstaNulo(getEntidade().getSequencialDaConta())) {
                        listaParcelas = parcelaService.consultarPassandoEntidade(parcela, 0, parcelaService.obterQuantidadeDeRegistrosPassandoEntidade(parcela));
                }
        setParcelas(new ParcelaDataModel(listaParcelas));
    }
    public Double getValorTotalParcelas() {
        Double valorTotal = new Double(0);
        if (VerificadorUtil.naoEstaNulo(parcelas)) {
                for (Parcela parcela : parcelas) {
                        valorTotal = valorTotal + parcela.getValor();
                }
        }
        return valorTotal;
    }
   
    public Long getVincularConta() {
                return vincularConta;
        }
        public void setVincularConta(Long vincularConta) {
                this.vincularConta = vincularConta;
        }
       
        public List<FormaPagamento> getFormasDePagamento() {
                if (VerificadorUtil.estaNulo(formasDePagamento)) {
                        setFormasDePagamento(formaPagamentoService.listarFormasPagamentoAtivas());
                }
                return formasDePagamento;
        }
        public void setFormasDePagamento(List<FormaPagamento> formasDePagamento) {
                this.formasDePagamento = formasDePagamento;
        }
   
        public void onRowSelect(SelectEvent event) {
                preencherConta(((Parcela) event.getObject()));
    }

        public void preencherConta(Parcela parcelaSelecionada) {
                limparEntidade();
        if (VerificadorUtil.naoEstaNulo(parcelaSelecionada.getConta())) {
                Conta conta = new Conta();
                conta.setSequencial(parcelaSelecionada.getConta().getSequencial());
                getEntidade().setConta(contaService.consultarPorId(conta));
        }
        preencherParcelas();
        }
       
        public void inicializarParcelaAReceber() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                Parcela parcela = new Parcela();
                                parcela.setIndicadorAtivo(true);
                                parcela.setConta(getEntidade().getConta());
                                if (VerificadorUtil.naoEstaNulo(getEntidade().getConta().getVenda())) {
                                        parcela.setDataEmissao(getEntidade().getConta().getVenda().getDataVenda());
                                        parcela.setObservacao("PARCELA DA VENDA: " + getEntidade().getConta().getVenda().getSequencial());
                                } else {
                                        parcela.setDataEmissao(DataUtils.getDataAtual());
                                }
                                getEntidade().setParcela(parcela);
                        }
                });
        }
       
        public void inicializarParcelaAPagar() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                Parcela parcela = new Parcela();
                                parcela.setIndicadorAtivo(true);
                                if (VerificadorUtil.naoEstaNulo(getEntidade().getConta())) {
                                        parcela.setDataEmissao(DataUtils.getDataAtual());
                                        parcela.setConta(getEntidade().getConta());
                                        parcela.setObservacao("PARCELA: " + getEntidade().getConta().getSequencial());
                                }
                                getEntidade().setParcela(parcela);
                        }
                });
        }
       
        public void cadastrarParcela() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                parcelaService.cadastrar(getEntidade().getParcela());
                                LancadorMensagem.lancarSucesso("PARCELA CADASTRADA COM SUCESSO!");
                        }
                });
                preencherParcelas();
        }
   
    public void receberParcela() {
        new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                if (VerificadorUtil.estaNulo(getEntidade().getParcela().getDataPagamento())) {
                                        getEntidade().getParcela().setDataPagamento(getEntidade().getParcela().getDataVencimento());
                                }
                                parcelaService.alterar(getEntidade().getParcela());
                                cadastrarNovaParcelaPelaFrequencia(getEntidade().getParcela());
                                LancadorMensagem.lancarSucesso("PARCELA RECEBIDA COM SUCESSO!");
                        }
                });
    }
       
        public void pagarParcela() {
        new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                if (VerificadorUtil.estaNulo(getEntidade().getParcela().getDataPagamento())) {
                                        getEntidade().getParcela().setDataPagamento(DataUtils.getDataAtual());
                                }
                                parcelaService.alterar(getEntidade().getParcela());
                                cadastrarNovaParcelaPelaFrequencia(getEntidade().getParcela());
                                LancadorMensagem.lancarSucesso("PARCELA PAGA COM SUCESSO!");
                        }
                });
    }
       
        public void alterarParcela() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                parcelaService.alterar(getEntidade().getParcela());
                                getEntidade().setParcela(null);
                                preencherParcelas();
                                LancadorMensagem.lancarSucesso("PARCELA ALTERADA COM SUCESSO!");
                        }
                });
        }
       
        public void excluirParcela() {
        new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                parcelaService.excluir(getEntidade().getParcela());
                                preencherParcelas();
                                excluirContaSemParcela(getEntidade().getParcela());
                                LancadorMensagem.lancarSucesso("PARCELA EXCLUÍDA COM SUCESSO");
                        }
                        private void excluirContaSemParcela(Parcela parcela) {
                                if (parcelaService.obterQuantidadeDeRegistrosPassandoEntidade(parcela) == 0) {
                                        if (VerificadorUtil.naoEstaNulo(parcela.getConta())) {
                                                Compra compraDaConta = compraService.consultarCompraDaConta(parcela.getConta());
                                                if (VerificadorUtil.naoEstaNulo(compraDaConta)) {
                                                        compraDaConta.setConta(null);
                                                        compraService.alterar(compraDaConta);
                                                }
                                                contaService.excluir(parcela.getConta());
                                                prepararParaCadastrarConta(parcela.getConta());
                                        }
                                }
                        }
                });
    }
       
        public void cancelarParcela() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                parcelaService.cancelarParcela(getEntidade().getParcela());
                                LancadorMensagem.lancarSucesso("PARCELA CANCELADA COM SUCESSO!");
                        }
                });
    }
       
        private void cadastrarNovaParcelaPelaFrequencia(Parcela parcela) {
                ContaGerenciadorDTO contaGerenciador = parcelaService.cadastrarNovaParcelaPelaFrequencia(parcela);
                if (VerificadorUtil.naoEstaNulo(contaGerenciador)) {
                        setEntidade(contaGerenciador);
                        LancadorMensagem.lancarSucesso("NOVA PARCELA GERADA COM SUCESSO!");
                }
        }
   
        public void alterarConta() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                contaService.alterar(getEntidade().getConta());
                                if (VerificadorUtil.naoEstaNulo(getEntidade().getConta().getCompra())) {
                                        getEntidade().getConta().getCompra().setConta(getEntidade().getConta());
                                        compraService.alterar(getEntidade().getConta().getCompra());
                                }
                                LancadorMensagem.lancarSucesso("CONTA ALTERADA COM SUCESSO!");
                        }
                });
        }
       
        public void prepararParaCadastrarContaAReceber(Venda venda) {
                Conta contaAReceber = new Conta(TipoConta.CONTA_A_RECEBER.getValor());
                contaAReceber.setVenda(venda);
                prepararParaCadastrarConta(contaAReceber);
        }
       
        public void prepararParaCadastrarContaAPagar() {
                prepararParaCadastrarConta(new Conta(TipoConta.CONTA_A_PAGAR.getValor()));
        }
       
        public void gerarContaAPagarDaCompra(Compra compra) {
                prepararParaCadastrarConta(new Conta(TipoConta.CONTA_A_PAGAR.getValor()));
                Categoria categoriaProduto = new Categoria();
                categoriaProduto.setSequencial(ConstantesSEC.Categoria.SEQUENCIAL_CATEGORIA_PRODUTO_7);
                getEntidade().getConta().setCategoria(categoriaService.consultarPorId(categoriaProduto));
                getEntidade().getConta().setPessoa(compra.getFornecedor().getPessoa());
                getEntidade().getConta().setCompra(compra);
        }
       
        private void prepararParaCadastrarConta(Conta conta) {
                limparEntidade();
                preencherParcelas();
                Conta contaParaCadastro = new Conta(conta.getTipoConta());
                if (VerificadorUtil.naoEstaNulo(conta.ehContaAReceber()) && VerificadorUtil.naoEstaNulo(conta.getSequencialDaVenda())) {
                        contaParaCadastro.setVenda(conta.getVenda());
                        contaParaCadastro.setObservacao("VENDA: " + contaParaCadastro.getSequencialDaVenda());
                }
                contaParaCadastro.setIndicadorAtivo(true);
                getEntidade().setConta(contaParaCadastro);
        }
       
        public void consultarConta(Compra compra) {
                Conta conta = contaService.consultarPorId(compra.getConta());
                getEntidade().setConta(conta);
        }
       
        public void selecionarCategoria(Categoria categoria) {
                getEntidade().getConta().setCategoria(categoria);
        }

        public void retirarCategoria() {
                getEntidade().getConta().setCategoria(null);
        }
       
        public void selecionarPessoa(Pessoa pessoa) {
                getEntidade().getConta().setPessoa(pessoa);
        }

        public void retirarPessoa() {
                getEntidade().getConta().setPessoa(null);
        }
       
        public void gerarParcelasDaVenda() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                getEntidade().setConta(parcelaService.gerarParcelasDaVenda(getEntidade().getConta().getVenda(), getEntidade().getConta()));
                                preencherParcelas();
                                LancadorMensagem.lancarSucesso("PARCELAS GERADAS COM SUCESSO!");
                        }
                });
    }
   
    public void gerarParcelasDinheiroDaVenda() {
        new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                getEntidade().setConta(parcelaService.gerarParcelasDinheiroDaVenda(getEntidade().getConta().getVenda(), getEntidade().getConta()));
                                preencherParcelas();
                                LancadorMensagem.lancarSucesso("PARCELAS GERADAS COM SUCESSO!");
                        }
                });
    }

    public void gerarParcelasCartaoDaVenda() {
        new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                getEntidade().setConta(parcelaService.gerarParcelasCartaoDaVenda(getEntidade().getConta().getVenda(), getEntidade().getConta()));
                                preencherParcelas();
                                LancadorMensagem.lancarSucesso("PARCELAS GERADAS COM SUCESSO!");
                        }
                });
    }
   
    public void vincularConta(final Compra compra) {
        new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                Conta conta = new Conta();
                                conta.setSequencial(getVincularConta());
                                conta = contaService.consultarPorId(conta);
                                compraService.vincularConta(compra, conta);
                                LancadorMensagem.lancarSucesso("CONTA [" + getVincularConta() + "] VINCULADA COM SUCESSO!");
                                setVincularConta(null);
                        }
                });
    }
   
    public void desvincularConta(final Compra compra) {
        new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                compraService.desvincularConta(compra);
                                LancadorMensagem.lancarSucesso("CONTA [" + compra.getConta().getSequencial() + "] DESVINCULADA COM SUCESSO!");
                                setVincularConta(null);
                        }
                });
    }
   
}