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.List;

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

import org.springframework.context.annotation.Scope;

import br.com.ec.core.generic.GenericService;
import br.com.ec.core.util.DataUtils;
import br.com.ec.domain.dto.ParametrosFecharCaixaDTO;
import br.com.ec.domain.dto.ParametrosReceberCaixaDTO;
import br.com.ec.domain.model.Conta;
import br.com.ec.domain.model.ContaBancaria;
import br.com.ec.domain.model.Loja;
import br.com.ec.domain.model.Pagamento;
import br.com.ec.domain.model.Parcela;
import br.com.ec.domain.model.Venda;
import br.com.ec.domain.model.tipos.TipoConta;
import br.com.ec.domain.model.tipos.TipoFrequencia;
import br.com.ec.domain.model.tipos.TipoSituacaoVenda;
import br.com.ec.domain.service.automatizador.AutomatizadorService;
import br.com.ec.domain.service.fecharcaixa.FecharCaixaService;
import br.com.ec.domain.service.pagamento.PagamentoService;
import br.com.ec.domain.service.parcela.ParcelaService;
import br.com.ec.domain.service.venda.VendaService;
import br.com.ec.domain.service.vigencia.VigenciaService;
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 ReceberCaixaBean extends AbstractBean<Object> implements Serializable {

        private static final long serialVersionUID = 1L;
       
        private AutomatizadorService automatizadorService;
        private FecharCaixaService fecharCaixaService;
        private VendaService vendaService;
        private ParcelaService parcelaService;
        private PagamentoService pagamentoService;
        private VigenciaService vigenciaService;
       
        private ParametrosReceberCaixaDTO parametrosReceberCaixa;
        private ParametrosFecharCaixaDTO parametrosFecharCaixa;
       
        @Inject
        public ReceberCaixaBean(AutomatizadorService automatizadorService, FecharCaixaService fecharCaixaService, VendaService vendaService,
                        ParcelaService parcelaService, PagamentoService pagamentoService, VigenciaService vigenciaService) {
                this.automatizadorService = automatizadorService;
                this.fecharCaixaService = fecharCaixaService;
                this.vendaService = vendaService;
                this.parcelaService = parcelaService;
                this.pagamentoService = pagamentoService;
                this.vigenciaService = vigenciaService;
        }
       
        @Override
        public void preCarregamento() {
                parametrosReceberCaixa = new ParametrosReceberCaixaDTO();
                parametrosFecharCaixa = new ParametrosFecharCaixaDTO();
        }
       
        @Override
        public void limparEntidade() {}

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

        @Override
        public Object getId() {
                return getEntidade();
        }
       
        public ParametrosReceberCaixaDTO getParametrosReceberCaixa() {
                return parametrosReceberCaixa;
        }
        public void setParametrosReceberCaixa(ParametrosReceberCaixaDTO parametrosReceberCaixa) {
                this.parametrosReceberCaixa = parametrosReceberCaixa;
        }
       
        public ParametrosFecharCaixaDTO getParametrosFecharCaixa() {
                return parametrosFecharCaixa;
        }
        public void setParametrosFecharCaixa(ParametrosFecharCaixaDTO parametrosFecharCaixa) {
                this.parametrosFecharCaixa = parametrosFecharCaixa;
        }
       
        /********************************************************************/
       
        public void prepararRecebimentoCaixa() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                getParametrosReceberCaixa().setValorRecebimentoEmDinheiro(automatizadorService.calcularReceberDia(getParametrosReceberCaixa().getLoja(), getParametrosReceberCaixa().getDataRecebimento()));
                                getParametrosReceberCaixa().setContaBancariaRecebimento(null);
                                setParametrosFecharCaixa(new ParametrosFecharCaixaDTO(getParametrosReceberCaixa().getLoja(), getParametrosReceberCaixa().getDataRecebimento()));
                                setParametrosFecharCaixa(fecharCaixaService.iniciarFechamentoCaixa(getParametrosFecharCaixa()));
                        }
                });
        }
       
        public void realizarRecebimentoCaixa() {
                new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() {
                        public void execute() {
                                automatizadorService.finalizarReceberDia(getParametrosReceberCaixa());
                                gerarPagamentos(getParametrosFecharCaixa().getListaPagamentos());
                                prepararRecebimentoCaixa();
                                LancadorMensagem.lancarSucesso("RECEBIMENTO REALIZADO COM SUCESSO");
                        }
                });
        }
       
        private void gerarPagamentos(final List<Pagamento> listaPagamentos) {
                for (Pagamento pagamento : listaPagamentos) {
                        if (pagamento.getGerarPagamento()) {
                                Conta conta = new Conta(TipoConta.CONTA_A_PAGAR.getValor());
                                conta.setCategoria(pagamento.getCategoriaGerarPagamento());
                                conta.setLoja(pagamento.getLoja());
                                conta.setTipoFrequencia(TipoFrequencia.UNICA.getValor());
                                conta.setVigencia(vigenciaService.consultarVigenciaDoPeriodo(pagamento.getData()));
                               
                                Parcela parcela = new Parcela();
                                parcela.setContaBancaria(new ContaBancaria(ConstantesSEC.ContaBancaria.CONTA_BANCARIA_CAIXA_1L));
                                parcela.setIndicadorOficial(false);
                                parcela.setIndicadorNegociacao(false);
                                parcela.setDataVencimento(pagamento.getData());
                                parcela.setDataPagamento(pagamento.getData());
                                parcela.setValor(pagamento.getValor());
                                parcela.setObservacao(pagamento.getDescricao());
                               
                                parcela.setConta(conta);
                                parcelaService.lancarParcela(parcela);
                               
                                pagamento.setLancado(true);
                                pagamentoService.alterar(pagamento);
                        }
                }
        }

        public Boolean temVendasConferidasOuNovas() {
                for (Venda venda : getParametrosFecharCaixa().getListaVendas()) {
                        if (venda.getTipoSituacao().equals(TipoSituacaoVenda.NOVO.getValor()) || venda.getTipoSituacao().equals(TipoSituacaoVenda.CONFERIDO.getValor())) {
                                return true;
                        }
                }
                return false;
        }
       
        public Boolean todasVendasConferidas(Loja loja) {
                Venda venda = new Venda();
                venda.setTipoSituacao(TipoSituacaoVenda.NOVO.getValor());
                if (vendaService.obterQuantidadeDeVendasPorLojaEePeriodo(venda, DataUtils.getDataAtual(), DataUtils.getDataAtual()) > 0) {
                        return false;
                }
                return true;
        }
       
}