Subversion Repositories Integrator Subversion

Rev

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

package br.com.ec.domain.service.orcamento.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import br.com.ec.core.generic.AbstractService;
import br.com.ec.core.generic.GenericRepository;
import br.com.ec.core.util.DataUtils;
import br.com.ec.core.util.VerificadorUtil;
import br.com.ec.core.validador.Validador;
import br.com.ec.domain.dto.FluxoDeCaixaDTO;
import br.com.ec.domain.model.Categoria;
import br.com.ec.domain.model.Orcamento;
import br.com.ec.domain.service.orcamento.OrcamentoService;
import br.com.ec.infrastructure.repository.OrcamentoRepository;

@Service
public class OrcamentoServiceImpl extends AbstractService<Orcamento> implements OrcamentoService {
       
        private OrcamentoRepository orcamentoRepository;

        @Autowired
        public OrcamentoServiceImpl(Validador validador, OrcamentoRepository orcamentoRepository) {
                super(validador);
                this.orcamentoRepository = orcamentoRepository;
        }

        @Override
        protected GenericRepository<Orcamento> getRepository() {
                return orcamentoRepository;
        }

        @Override
        public Orcamento consultarOrcamento(Date dataInicial, Date dataFinal, Categoria categoria, String tipoCategoria) {
                return orcamentoRepository.consultarOrcamento(dataInicial, dataFinal, categoria, tipoCategoria);
        }
       
        @Override
        public Orcamento prepararGerirOrcado(FluxoDeCaixaDTO fluxo, Categoria categoriaPai, Categoria categoria) {
                Orcamento orcamento = null;
                Categoria categoriaOrcado = null;
                String tipoCategoria = null;
                if (VerificadorUtil.estaNulo(categoria.getSequencial())) {
                        orcamento = consultarOrcamento(fluxo.getDataInicial(), fluxo.getDataFinal(), categoriaPai, categoriaPai.getTipo());
                        categoriaOrcado = categoriaPai;
                        tipoCategoria = categoriaPai.getTipo();
                } else {
                        orcamento = consultarOrcamento(fluxo.getDataInicial(), fluxo.getDataFinal(), categoria, categoria.getTipo());
                        categoriaOrcado = categoria;
                        tipoCategoria = categoria.getTipo();
                }
                if (VerificadorUtil.estaNulo(orcamento)) {
                        orcamento = new Orcamento();
                        orcamento.setCategoria(categoriaOrcado);
                        orcamento.setTipoCategoria(tipoCategoria);
                        if (DataUtils.getDataAtual().before(fluxo.getDataInicial())) {
                                orcamento.setDataOrcado(fluxo.getDataInicial());
                        } else {
                                orcamento.setDataOrcado(DataUtils.getDataAtual());
                        }
                }
                return orcamento;
        }
       
        @Override
        public void atualizarOrcamento(Orcamento orcamento) {
                if (VerificadorUtil.naoEstaNulo(orcamento)) {
                        if (VerificadorUtil.estaNulo(orcamento.getCategoria()) ||
                                VerificadorUtil.estaNulo(orcamento.getCategoria().getSequencial())) {
                                orcamento.setCategoria(null);
                        }
                        if (VerificadorUtil.naoEstaNulo(orcamento.getSequencial())) {
                                alterar(orcamento);
                        } else {
                                cadastrar(orcamento);
                        }
                }
        }
       
}