Rev 721 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 259 | espaco | 1 | package br.com.ec.domain.service.impl; |
| 2 | |||
| 282 | espaco | 3 | import java.util.Date; |
| 4 | import java.util.List; |
||
| 5 | |||
| 259 | espaco | 6 | import org.springframework.beans.factory.annotation.Autowired; |
| 7 | import org.springframework.stereotype.Service; |
||
| 8 | |||
| 296 | espaco | 9 | import br.com.ec.core.consulta.ParametrosConsulta; |
| 10 | import br.com.ec.core.exception.NegocioException; |
||
| 259 | espaco | 11 | import br.com.ec.core.generic.AbstractService; |
| 12 | import br.com.ec.core.generic.GenericRepository; |
||
| 439 | espaco | 13 | import br.com.ec.core.util.DataUtils; |
| 296 | espaco | 14 | import br.com.ec.core.util.VerificadorUtil; |
| 259 | espaco | 15 | import br.com.ec.core.validador.Validador; |
| 296 | espaco | 16 | import br.com.ec.domain.dto.FormaPagamentoDTO; |
| 282 | espaco | 17 | import br.com.ec.domain.dto.ParcelaDTO; |
| 439 | espaco | 18 | import br.com.ec.domain.dto.VigenciaDTO; |
| 282 | espaco | 19 | import br.com.ec.domain.dto.consulta.ParametrosConsultaParcelasDTO; |
| 439 | espaco | 20 | import br.com.ec.domain.model.Conta; |
| 259 | espaco | 21 | import br.com.ec.domain.model.Parcela; |
| 439 | espaco | 22 | import br.com.ec.domain.model.Vigencia; |
| 23 | import br.com.ec.domain.model.tipos.TipoFrequencia; |
||
| 24 | import br.com.ec.domain.service.ContaService; |
||
| 259 | espaco | 25 | import br.com.ec.domain.service.ParcelaService; |
| 439 | espaco | 26 | import br.com.ec.domain.service.VigenciaService; |
| 259 | espaco | 27 | import br.com.ec.repository.ParcelaRepository; |
| 28 | |||
| 29 | @Service |
||
| 30 | public class ParcelaServiceImpl extends AbstractService<Parcela> implements ParcelaService { |
||
| 274 | espaco | 31 | |
| 259 | espaco | 32 | private ParcelaRepository parcelaRepository; |
| 439 | espaco | 33 | private ContaService contaService; |
| 34 | private VigenciaService vigenciaService; |
||
| 35 | |||
| 274 | espaco | 36 | /* |
| 259 | espaco | 37 | private TaxaService taxaService; |
| 38 | private CategoriaService categoriaService; |
||
| 39 | private ContaFormaPagamentoService contaFormaPagamentoService; |
||
| 40 | private VendaFormaPagamentoService vendaFormaPagamentoService; |
||
| 439 | espaco | 41 | */ |
| 274 | espaco | 42 | |
| 259 | espaco | 43 | @Autowired |
| 439 | espaco | 44 | public ParcelaServiceImpl(Validador validador, ParcelaRepository parcelaRepository, ContaService contaService, |
| 45 | VigenciaService vigenciaService/*, TaxaService taxaService, |
||
| 46 | CategoriaService categoriaService, ContaFormaPagamentoService contaFormaPagamentoService, |
||
| 47 | VendaFormaPagamentoService vendaFormaPagamentoService*/) { |
||
| 259 | espaco | 48 | super(validador); |
| 49 | this.parcelaRepository = parcelaRepository; |
||
| 439 | espaco | 50 | this.contaService = contaService; |
| 51 | this.vigenciaService = vigenciaService; |
||
| 259 | espaco | 52 | /* |
| 53 | this.taxaService = taxaService; |
||
| 54 | this.contaFormaPagamentoService = contaFormaPagamentoService; |
||
| 55 | this.vendaFormaPagamentoService = vendaFormaPagamentoService; |
||
| 56 | this.categoriaService = categoriaService; |
||
| 57 | */ |
||
| 58 | } |
||
| 59 | |||
| 60 | @Override |
||
| 61 | protected GenericRepository<Parcela> getRepository() { |
||
| 62 | return this.parcelaRepository; |
||
| 63 | } |
||
| 282 | espaco | 64 | |
| 65 | @Override |
||
| 439 | espaco | 66 | protected void regrasNegocioCadastrar(Parcela parcela) { |
| 67 | verificarParcela(parcela); |
||
| 575 | blopes | 68 | parcela.setIndicadorNegociacao(false); |
| 439 | espaco | 69 | } |
| 70 | |||
| 71 | @Override |
||
| 72 | protected void regrasNegocioAlterar(Parcela parcela) { |
||
| 73 | verificarParcela(parcela); |
||
| 74 | } |
||
| 75 | |||
| 76 | private void verificarParcela(Parcela parcela) { |
||
| 77 | if (VerificadorUtil.estaNulo(parcela.getDataVencimento()) && VerificadorUtil.estaNulo(parcela.getDataPagamento())) { |
||
| 78 | throw new NegocioException("INFORME A DATA DE VENCIMENTO OU A DATA DE PAGAMENTO"); |
||
| 79 | } |
||
| 80 | cadastrarContaCasoNaoTenhaSidoCadastrada(parcela.getConta()); |
||
| 81 | } |
||
| 82 | |||
| 83 | @Override |
||
| 282 | espaco | 84 | public List<ParcelaDTO> consultarParcelas(ParametrosConsultaParcelasDTO parametrosConsultaParcelasDTO) { |
| 85 | return parcelaRepository.consultarParcelas(parametrosConsultaParcelasDTO); |
||
| 86 | } |
||
| 87 | |||
| 88 | @Override |
||
| 382 | espaco | 89 | public List<ParcelaDTO> consultarParcelas(Long sequencialContaBancaria, Date dataRecebimento, List<FormaPagamentoDTO> formasDePagamento) { |
| 90 | return parcelaRepository.consultarParcelas(sequencialContaBancaria, dataRecebimento, formasDePagamento); |
||
| 91 | } |
||
| 92 | |||
| 93 | @Override |
||
| 669 | blopes | 94 | public List<ParcelaDTO> consultarParcelas(String sqlConsulta) { |
| 95 | return parcelaRepository.consultarParcelas(sqlConsulta); |
||
| 96 | } |
||
| 97 | |||
| 98 | @Override |
||
| 296 | espaco | 99 | public List<ParcelaDTO> consultarParcelasAReceber(Long sequencialContaBancaria, Date dataRecebimento, List<FormaPagamentoDTO> formasDePagamento) { |
| 100 | return parcelaRepository.consultarParcelasAReceber(sequencialContaBancaria, dataRecebimento, formasDePagamento); |
||
| 282 | espaco | 101 | } |
| 102 | |||
| 103 | @Override |
||
| 296 | espaco | 104 | public List<ParcelaDTO> consultarParcelasRecebidas(Long sequencialContaBancaria, Date dataRecebimento, List<FormaPagamentoDTO> formasDePagamento) { |
| 105 | return parcelaRepository.consultarParcelasRecebidas(sequencialContaBancaria, dataRecebimento, formasDePagamento); |
||
| 282 | espaco | 106 | } |
| 107 | |||
| 296 | espaco | 108 | @Override |
| 610 | blopes | 109 | public List<ParcelaDTO> consultarParcelasDaPessoaEeCategoriaEeVigencia(Long sequencialPessoa, Long sequencialCategoria, Long sequencialVigencia) { |
| 110 | return parcelaRepository.consultarParcelasDaPessoaEeCategoriaEeVigencia(sequencialPessoa, sequencialCategoria, sequencialVigencia); |
||
| 111 | } |
||
| 112 | |||
| 113 | @Override |
||
| 296 | espaco | 114 | public void receberParcelas(Date dataConciliacao, List<ParcelaDTO> parcelasSelecionadas) { |
| 115 | if (VerificadorUtil.estaNulo(dataConciliacao)) { |
||
| 116 | throw new NegocioException("INFORME A DATA DA CONCILIAÇÃO"); |
||
| 117 | } |
||
| 383 | espaco | 118 | parcelaRepository.conciliarParcelas(dataConciliacao, parcelasSelecionadas); |
| 296 | espaco | 119 | } |
| 120 | |||
| 121 | @Override |
||
| 122 | public Double obterValorPassandoParametrosConsulta(ParametrosConsulta<Parcela> parametrosConsulta) { |
||
| 123 | return parcelaRepository.obterValorPassandoParametrosConsulta(parametrosConsulta); |
||
| 124 | } |
||
| 125 | |||
| 297 | espaco | 126 | @Override |
| 127 | public List<ParcelaDTO> consultarParcelasPassandoParametrosConsulta(ParametrosConsulta<Parcela> parametrosConsulta) { |
||
| 128 | return parcelaRepository.consultarParcelasPassandoParametrosConsulta(parametrosConsulta); |
||
| 129 | } |
||
| 130 | |||
| 402 | espaco | 131 | @Override |
| 132 | public void atualizarObservacaoPagamentos(List<ParcelaDTO> parcelas) { |
||
| 133 | for (ParcelaDTO parcelaDTO : parcelas) { |
||
| 134 | parcelaRepository.alterarObservacao(parcelaDTO.getSequencial(), parcelaDTO.getObservacao()); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 259 | espaco | 138 | @Override |
| 439 | espaco | 139 | public void efetuarPagamentoRecebimento(ParcelaDTO parcelaDTO, Date dataEfetuar) { |
| 140 | Parcela parcela = this.consultarPorId(new Parcela(parcelaDTO.getSequencial())); |
||
| 740 | blopes | 141 | |
| 142 | Vigencia proximaVigencia = vigenciaService.consultarProximaVigencia(parcela.getConta().getVigencia().getSequencial()); |
||
| 143 | if (VerificadorUtil.estaNulo(proximaVigencia)) { |
||
| 144 | throw new NegocioException("NECESSÁRIO O CADASTRO DA PRÓXIMA VIGÊNCIA"); |
||
| 145 | } |
||
| 146 | |||
| 439 | espaco | 147 | if (VerificadorUtil.estaNulo(parcela.getDataPagamento())) { |
| 148 | parcela.setDataPagamento(dataEfetuar); |
||
| 149 | this.alterar(parcela); |
||
| 740 | blopes | 150 | cadastrarNovaParcelaPelaFrequencia(parcela, proximaVigencia); |
| 439 | espaco | 151 | } |
| 259 | espaco | 152 | } |
| 153 | |||
| 439 | espaco | 154 | private Conta cadastrarContaCasoNaoTenhaSidoCadastrada(Conta conta) { |
| 155 | if (VerificadorUtil.naoEstaNulo(conta)) { |
||
| 156 | if (VerificadorUtil.estaNulo(conta.getSequencial())) { |
||
| 157 | contaService.cadastrar(conta); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | return conta; |
||
| 259 | espaco | 161 | } |
| 274 | espaco | 162 | |
| 439 | espaco | 163 | @Override |
| 740 | blopes | 164 | public void cadastrarNovaParcelaPelaFrequencia(Parcela parcela, Vigencia proximaVigencia) { |
| 439 | espaco | 165 | if (VerificadorUtil.naoEstaNulo(parcela.getConta())) { |
| 166 | if (!this.verificarPossuiParcelaEmAberta(parcela.getConta().getSequencial(), parcela.getConta().getSequencialDaVenda())) { |
||
| 167 | if (!parcela.getConta().getTipoFrequencia().equals(TipoFrequencia.UNICA.getValor())) { |
||
| 168 | Conta contaNova = new Conta(parcela.getConta()); |
||
| 169 | contaNova.setSequencial(null); |
||
| 170 | if (parcela.getConta().getTipoFrequencia().equals(TipoFrequencia.MENSAL.getValor())) { |
||
| 171 | contaNova.setVigencia(proximaVigencia); |
||
| 172 | } |
||
| 173 | contaService.cadastrar(contaNova); |
||
| 174 | |||
| 175 | List<Parcela> parcelas = parcelaRepository.consultarParcelasDaConta(parcela.getConta().getSequencial()); |
||
| 176 | for (Parcela parcelaAnterior : parcelas) { |
||
| 177 | Parcela parcelaNova = new Parcela(parcelaAnterior); |
||
| 178 | parcelaNova.setConta(contaNova); |
||
| 179 | parcelaNova.setDataEmissao(DataUtils.getDataAtual()); |
||
| 180 | parcelaNova.provisionarNovaDataVencimentoPelaFrequencia(); |
||
| 181 | parcelaNova.setDataPagamento(null); |
||
| 182 | this.cadastrar(parcelaNova); |
||
| 183 | } |
||
| 184 | // ContaGerenciadorDTO contaGerenciador = new ContaGerenciadorDTO(); |
||
| 185 | // contaGerenciador.setConta(contaAPagar); |
||
| 186 | // return contaGerenciador; |
||
| 187 | } |
||
| 188 | } |
||
| 259 | espaco | 189 | } |
| 439 | espaco | 190 | //return null; |
| 259 | espaco | 191 | } |
| 192 | |||
| 193 | @Override |
||
| 439 | espaco | 194 | public Boolean verificarPossuiParcelaEmAberta(Long sequencialConta, Long sequencialVenda) { |
| 195 | return parcelaRepository.verificarPossuiParcelaEmAberta(sequencialConta, sequencialVenda); |
||
| 196 | } |
||
| 197 | |||
| 610 | blopes | 198 | @Override |
| 199 | public void atualizarValorParcela(ParcelaDTO parcelaDTO) { |
||
| 200 | parcelaRepository.atualizarValorParcela(parcelaDTO); |
||
| 201 | } |
||
| 202 | |||
| 679 | blopes | 203 | @Override |
| 721 | blopes | 204 | public void atualizarCentroDeCustos(Long sequencialConta, Long sequencialCentroDeCustos) { |
| 205 | parcelaRepository.atualizarCentroDeCustos(sequencialConta, sequencialCentroDeCustos); |
||
| 206 | } |
||
| 207 | |||
| 208 | @Override |
||
| 679 | blopes | 209 | public void efetivarConciliacaoAntecipacao(String sqlAtualizacaoParcelas) { |
| 210 | parcelaRepository.efetivarConciliacaoAntecipacao(sqlAtualizacaoParcelas); |
||
| 211 | } |
||
| 212 | |||
| 439 | espaco | 213 | /* |
| 214 | @Override |
||
| 259 | espaco | 215 | public void cancelarParcela(Parcela parcela) { |
| 216 | parcela.setIndicadorAtivo(false); |
||
| 217 | this.alterar(parcela); |
||
| 218 | cancelarContaSemParcelaAtiva(parcela); |
||
| 219 | } |
||
| 220 | |||
| 221 | private void cancelarContaSemParcelaAtiva(Parcela parcela) { |
||
| 222 | Parcela parcelaAtiva = new Parcela(); |
||
| 223 | parcelaAtiva.setIndicadorAtivo(true); |
||
| 224 | parcelaAtiva.setConta(parcela.getConta()); |
||
| 225 | if (this.obterQuantidadeDeRegistrosPassandoEntidade(parcelaAtiva) == 0) { |
||
| 226 | if (VerificadorUtil.naoEstaNulo(parcela.getConta())) { |
||
| 227 | parcela.getConta().setIndicadorAtivo(false); |
||
| 228 | contaService.alterar(parcela.getConta()); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | @Override |
||
| 234 | public void receberParcelas(Date dataRecebimento, List<Parcela> parcelasSelecionadas) { |
||
| 235 | if (VerificadorUtil.estaNulo(dataRecebimento)) { |
||
| 236 | throw new NegocioException("INFORME A DATA DO RECEBIMENTO"); |
||
| 237 | } |
||
| 238 | for (Parcela parcela : parcelasSelecionadas) { |
||
| 239 | parcela.setDataPagamento(dataRecebimento); |
||
| 240 | this.alterar(parcela); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | @Override |
||
| 245 | public void lancarParcela(Parcela parcela) { |
||
| 246 | verificarParametros(parcela); |
||
| 247 | parcela.getConta().setObservacao(parcela.getObservacao()); |
||
| 248 | contaService.cadastrar(parcela.getConta()); |
||
| 249 | |||
| 250 | parcela.setDataEmissao(DataUtils.getDataAtual()); |
||
| 251 | parcela.setIndicadorAtivo(true); |
||
| 252 | this.cadastrar(parcela); |
||
| 253 | } |
||
| 254 | |||
| 255 | private void verificarParametros(Parcela parcela) { |
||
| 256 | if (VerificadorUtil.estaNulo(parcela.getContaBancaria())) { |
||
| 257 | throw new NegocioException("INFORME A CONTA"); |
||
| 258 | } |
||
| 259 | if (VerificadorUtil.estaNulo(parcela.getDataPagamento())) { |
||
| 260 | throw new NegocioException("INFORME A DATA DO PAGAMENTO"); |
||
| 261 | } |
||
| 262 | if (VerificadorUtil.estaNulo(parcela.getValor())) { |
||
| 263 | throw new NegocioException("INFORME O VALOR DO PAGAMETNO"); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | @Override |
||
| 268 | public List<Parcela> consultarParcelas(ParametrosConsultaLancamentosDTO parcelaConsulta) { |
||
| 269 | return parcelaRepository.consultarParcelas(parcelaConsulta); |
||
| 270 | } |
||
| 271 | |||
| 272 | @Override |
||
| 273 | public List<Parcela> consultarParcelasAVencer(Conta conta) { |
||
| 274 | return parcelaRepository.consultarParcelasAVencer(conta); |
||
| 275 | } |
||
| 276 | |||
| 277 | @Override |
||
| 278 | public List<Parcela> consultarParcelasRecebidas(Date dataRecebimento, TipoCartao tipoCartao) { |
||
| 279 | return parcelaRepository.consultarParcelasRecebidas(dataRecebimento, tipoCartao); |
||
| 280 | } |
||
| 281 | |||
| 282 | @Override |
||
| 283 | public List<Parcela> consultarParcelasDaVenda(Long sequencialVenda) { |
||
| 284 | return parcelaRepository.consultarParcelasDaVenda(sequencialVenda); |
||
| 285 | } |
||
| 286 | |||
| 287 | @Override |
||
| 288 | public Conta gerarParcelas(VendaFormaPagamento vendaFormaPagamento, Conta conta) { |
||
| 289 | Taxa taxa = consultarTaxa(vendaFormaPagamento); |
||
| 290 | Double valorParcelaCalculado = calcularValorParcela(taxa, vendaFormaPagamento.getValorPagamento()); |
||
| 291 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhDebito()) {return criarParcelaDebito(valorParcelaCalculado, taxa.getContaBancaria(), vendaFormaPagamento, conta);} |
||
| 292 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhCredito()) {return criarParcelaCredito(valorParcelaCalculado, taxa.getContaBancaria(), vendaFormaPagamento, conta);} |
||
| 293 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhPagseguro()) {return criarParcelaPagseguro(valorParcelaCalculado, taxa.getContaBancaria(), vendaFormaPagamento, conta);} |
||
| 294 | return conta; |
||
| 295 | } |
||
| 296 | |||
| 297 | private Conta criarParcelaDebito(Double valorParcelaCalculado, ContaBancaria contaBancaria, VendaFormaPagamento vendaFormaPagamento, Conta conta) { |
||
| 298 | Date dataDebito = DataUtils.acrescentarDiasResultadoDiaUtil(vendaFormaPagamento.getVenda().getDataVenda(), 1); |
||
| 299 | cadastrarParcelas(vendaFormaPagamento, contaBancaria, dataDebito, valorParcelaCalculado, conta, "DÉBITO"); |
||
| 300 | return conta; |
||
| 301 | } |
||
| 302 | |||
| 303 | private Conta criarParcelaCredito(Double valorParcelaCalculado, ContaBancaria contaBancaria, VendaFormaPagamento vendaFormaPagamento, Conta contaAReceber) { |
||
| 304 | Integer quantidadeVezes = 0; |
||
| 305 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh1x()) {quantidadeVezes = 1;} |
||
| 306 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh2x()) {quantidadeVezes = 2;} |
||
| 307 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh3x()) {quantidadeVezes = 3;} |
||
| 308 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh4x()) {quantidadeVezes = 4;} |
||
| 309 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh5x()) {quantidadeVezes = 5;} |
||
| 310 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh6x()) {quantidadeVezes = 6;} |
||
| 311 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh7x()) {quantidadeVezes = 7;} |
||
| 312 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh8x()) {quantidadeVezes = 8;} |
||
| 313 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh9x()) {quantidadeVezes = 9;} |
||
| 314 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh10x()) {quantidadeVezes = 10;} |
||
| 315 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh11x()) {quantidadeVezes = 11;} |
||
| 316 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh12x()) {quantidadeVezes = 12;} |
||
| 274 | espaco | 317 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh13x()) {quantidadeVezes = 13;} |
| 318 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh14x()) {quantidadeVezes = 14;} |
||
| 319 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh15x()) {quantidadeVezes = 15;} |
||
| 259 | espaco | 320 | Date dataParcela = vendaFormaPagamento.getVenda().getDataVenda(); |
| 321 | Date dataInicial = vendaFormaPagamento.getVenda().getDataVenda(); |
||
| 322 | for (int i = 1; i <= quantidadeVezes; i++) { |
||
| 323 | if (i == 1) { |
||
| 324 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEh1x()) { |
||
| 325 | dataParcela = DataUtils.acrescentarDias(dataParcela, 2); |
||
| 326 | } else { |
||
| 327 | dataParcela = DataUtils.acrescentarDias(dataParcela, 31); |
||
| 328 | } |
||
| 329 | } else { |
||
| 330 | dataParcela = DataUtils.acrescentarDias(dataInicial, i * 30); |
||
| 331 | } |
||
| 332 | dataParcela = DataUtils.acrescentarDiasResultadoDiaUtil(dataParcela, 0); |
||
| 333 | cadastrarParcelas(vendaFormaPagamento, contaBancaria, dataParcela, valorParcelaCalculado / quantidadeVezes, contaAReceber, "CRÉDITO " + i + "/" + quantidadeVezes); |
||
| 334 | } |
||
| 335 | return contaAReceber; |
||
| 336 | } |
||
| 337 | |||
| 338 | @Override |
||
| 339 | public Conta gerarParcelasDaVenda(Venda venda, Conta conta) { |
||
| 340 | if (VerificadorUtil.naoEstaNulo(conta) && VerificadorUtil.naoEstaNulo(venda)) { |
||
| 341 | conta = cadastrarContaCasoNaoTenhaSidoCadastrada(conta); |
||
| 342 | List<VendaFormaPagamento> listaVendaFormaPagamento = new ArrayList<VendaFormaPagamento>(); |
||
| 343 | listaVendaFormaPagamento.addAll(vendaFormaPagamentoService.consultarLancamentosDaVenda(venda)); |
||
| 344 | for (VendaFormaPagamento vendaFormaPagamento : listaVendaFormaPagamento) { |
||
| 345 | Taxa taxa = consultarTaxa(vendaFormaPagamento); |
||
| 346 | Double valorParcelaCalculado = calcularValorParcela(taxa, vendaFormaPagamento.getValorPagamento()); |
||
| 347 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhDinheiro()) {criarParcelaDinheiro(vendaFormaPagamento, conta);} |
||
| 348 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhDebito()) {criarParcelaDebito(valorParcelaCalculado, taxa.getContaBancaria(), vendaFormaPagamento, conta);} |
||
| 349 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhCredito()) {criarParcelaCredito(valorParcelaCalculado, taxa.getContaBancaria(), vendaFormaPagamento, conta);} |
||
| 350 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhPagseguro()) {criarParcelaPagseguro(valorParcelaCalculado, taxa.getContaBancaria(), vendaFormaPagamento, conta);} |
||
| 351 | } |
||
| 352 | return conta; |
||
| 353 | } |
||
| 354 | throw new NegocioException("NENHUMA PARCELA FOI CRIADA"); |
||
| 355 | } |
||
| 356 | |||
| 357 | @Override |
||
| 358 | public Conta gerarParcelasDinheiroDaVenda(Venda venda, Conta contaAReceber) { |
||
| 359 | if (VerificadorUtil.naoEstaNulo(contaAReceber) && VerificadorUtil.naoEstaNulo(venda)) { |
||
| 360 | contaAReceber = cadastrarContaCasoNaoTenhaSidoCadastrada(contaAReceber); |
||
| 361 | List<VendaFormaPagamento> listaVendaFormaPagamento = new ArrayList<VendaFormaPagamento>(); |
||
| 362 | listaVendaFormaPagamento.addAll(vendaFormaPagamentoService.consultarLancamentosDaVenda(venda)); |
||
| 363 | for (VendaFormaPagamento vendaFormaPagamento : listaVendaFormaPagamento) { |
||
| 364 | Taxa taxa = consultarTaxa(vendaFormaPagamento); |
||
| 365 | Double valorParcelaCalculado = calcularValorParcela(taxa, vendaFormaPagamento.getValorPagamento()); |
||
| 366 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhDinheiro()) {criarParcelaDinheiro(vendaFormaPagamento, contaAReceber);} |
||
| 367 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhPagseguro()) {criarParcelaPagseguro(valorParcelaCalculado, taxa.getContaBancaria(), vendaFormaPagamento, contaAReceber);} |
||
| 368 | } |
||
| 369 | return contaAReceber; |
||
| 370 | } |
||
| 371 | throw new NegocioException("NENHUMA PARCELA FOI CRIADA"); |
||
| 372 | } |
||
| 373 | |||
| 374 | @Override |
||
| 375 | public Conta gerarParcelasCartaoDaVenda(Venda venda, Conta conta) { |
||
| 376 | if (VerificadorUtil.naoEstaNulo(conta) && VerificadorUtil.naoEstaNulo(venda)) { |
||
| 377 | conta = cadastrarContaCasoNaoTenhaSidoCadastrada(conta); |
||
| 378 | List<VendaFormaPagamento> listaVendaFormaPagamento = new ArrayList<VendaFormaPagamento>(); |
||
| 379 | listaVendaFormaPagamento.addAll(vendaFormaPagamentoService.consultarLancamentosDaVenda(venda)); |
||
| 380 | for (VendaFormaPagamento vendaFormaPagamento : listaVendaFormaPagamento) { |
||
| 381 | Taxa taxa = consultarTaxa(vendaFormaPagamento); |
||
| 382 | Double valorParcelaCalculado = calcularValorParcela(taxa, vendaFormaPagamento.getValorPagamento()); |
||
| 383 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhDebito()) {criarParcelaDebito(valorParcelaCalculado, taxa.getContaBancaria(), vendaFormaPagamento, conta);} |
||
| 384 | if (vendaFormaPagamento.getFormaPagamento().formaPagamentoEhCredito()) {criarParcelaCredito(valorParcelaCalculado, taxa.getContaBancaria(), vendaFormaPagamento, conta);} |
||
| 385 | } |
||
| 386 | return conta; |
||
| 387 | } |
||
| 388 | throw new NegocioException("NENHUMA PARCELA FOI CRIADA"); |
||
| 389 | } |
||
| 390 | |||
| 391 | private Conta criarParcelaDinheiro(VendaFormaPagamento vendaFormaPagamento, Conta contaAReceber) { |
||
| 392 | cadastrarParcelas(vendaFormaPagamento, new ContaBancaria(ContaBancaria.getContaCaixa()), vendaFormaPagamento.getVenda().getDataVenda(), vendaFormaPagamento.getValorPagamento(), contaAReceber, "VALOR"); |
||
| 393 | return contaAReceber; |
||
| 394 | } |
||
| 395 | |||
| 396 | private Conta criarParcelaPagseguro(Double valorParcelaCalculado, ContaBancaria contaBancaria, VendaFormaPagamento vendaFormaPagamento, Conta contaAReceber) { |
||
| 397 | cadastrarParcelas(vendaFormaPagamento, contaBancaria, vendaFormaPagamento.getVenda().getDataVenda(), valorParcelaCalculado, contaAReceber, "PAGSEGURO"); |
||
| 398 | return contaAReceber; |
||
| 399 | } |
||
| 400 | |||
| 401 | private Taxa consultarTaxa(VendaFormaPagamento vendaFormaPagamento) { |
||
| 402 | Taxa taxa = new Taxa(); |
||
| 403 | taxa.setEmpresaAdquirente(vendaFormaPagamento.getEmpresaAdquirenteDaMaquinetaDaVenda()); |
||
| 404 | taxa.setFormaPagamento(vendaFormaPagamento.getFormaPagamento()); |
||
| 405 | taxa.setBandeiraCartao(vendaFormaPagamento.getBandeiraCartao()); |
||
| 406 | taxa = taxaService.consultarTaxa(taxa); |
||
| 407 | if (VerificadorUtil.estaNulo(taxa)) { |
||
| 408 | taxa = new Taxa(); |
||
| 409 | taxa.setEmpresaAdquirente(vendaFormaPagamento.getEmpresaAdquirenteDaMaquinetaDaVenda()); |
||
| 410 | taxa.setFormaPagamento(vendaFormaPagamento.getFormaPagamento()); |
||
| 411 | taxa = taxaService.consultarTaxa(taxa); |
||
| 412 | } |
||
| 413 | if (VerificadorUtil.estaNulo(taxa)) { |
||
| 414 | taxa = new Taxa(); |
||
| 415 | taxa.setContaBancaria(new ContaBancaria(new Long(1))); |
||
| 416 | } |
||
| 417 | return taxa; |
||
| 418 | } |
||
| 419 | |||
| 420 | private Double calcularValorParcela(Taxa taxa, Double valorPagamento) { |
||
| 421 | Double valorParcelaCalculado = new Double(0.0); |
||
| 422 | if (VerificadorUtil.naoEstaNulo(taxa)) { |
||
| 423 | if (VerificadorUtil.naoEstaNulo(taxa.getValor())) { |
||
| 424 | valorParcelaCalculado = descontarTaxa(valorPagamento, taxa.getValor()); |
||
| 425 | } else { |
||
| 426 | return valorPagamento; |
||
| 427 | } |
||
| 428 | } |
||
| 429 | return valorParcelaCalculado; |
||
| 430 | } |
||
| 431 | |||
| 432 | private Double descontarTaxa(Double valorPagamento, Double taxa) { |
||
| 433 | return new Double (new DecimalFormat("#.##").format(valorPagamento - (valorPagamento * taxa / 100)).replace(",", ".")); |
||
| 434 | } |
||
| 435 | |||
| 436 | private void cadastrarParcelas(VendaFormaPagamento vendaFormaPagamento, ContaBancaria contaBancaria, Date dataVencimento, Double valorPagamento, Conta conta, String observacao) { |
||
| 437 | Parcela parcela = new Parcela(conta, contaBancaria, vendaFormaPagamento.getVenda().getDataVenda(), dataVencimento, null, valorPagamento, observacao + " DA VENDA: " + vendaFormaPagamento.getVenda().getSequencial(), true); |
||
| 438 | this.cadastrar(parcela); |
||
| 439 | } |
||
| 440 | |||
| 441 | @Override |
||
| 442 | public Boolean verificarPossuiParcelaAberta(Venda venda) { |
||
| 443 | return parcelaRepository.verificarPossuiParcelaAberta(venda); |
||
| 444 | } |
||
| 445 | |||
| 446 | @Override |
||
| 447 | public Boolean verificarPossuiParcelaAbertaParaPagamento(Conta conta) { |
||
| 448 | return parcelaRepository.verificarPossuiParcelaAbertaParaPagamento(conta); |
||
| 449 | } |
||
| 450 | |||
| 451 | @Override |
||
| 452 | public Boolean verificarPossuiParcela(Venda venda) { |
||
| 453 | return parcelaRepository.verificarPossuiParcela(venda); |
||
| 454 | } |
||
| 455 | |||
| 456 | @Override |
||
| 457 | public List<CartaoDTO> gerarContasDosCartoes(List<VendaFormaPagamento> listaVendaFormaPagamento) { |
||
| 458 | List<CartaoDTO> contas = new ArrayList<CartaoDTO>(); |
||
| 459 | for (VendaFormaPagamento vendaFormaPagamento : listaVendaFormaPagamento) { |
||
| 460 | Conta contaAReceber = new Conta(); |
||
| 461 | contaAReceber.setLoja(vendaFormaPagamento.getVenda().getLoja()); |
||
| 462 | contaAReceber.setIndicadorAtivo(true); |
||
| 463 | contaAReceber.setTipoFrequencia(TipoFrequencia.UNICA.getValor()); |
||
| 464 | contaAReceber.setTipoConta(TipoConta.CONTA_A_RECEBER.getValor()); |
||
| 465 | contaAReceber.setObservacao("VENDA: " + vendaFormaPagamento.getSequencialDaVenda()); |
||
| 466 | contaAReceber.setVenda(vendaFormaPagamento.getVenda()); |
||
| 467 | |||
| 468 | ContaFormaPagamento contaFormaPagamento = new ContaFormaPagamento(vendaFormaPagamento); |
||
| 469 | contaFormaPagamentoService.cadastrar(contaFormaPagamento); |
||
| 470 | contaAReceber.setFormaDePagamento(contaFormaPagamento); |
||
| 471 | |||
| 472 | contaService.cadastrar(contaAReceber); |
||
| 473 | |||
| 474 | List<Parcela> parcelas = new ArrayList<Parcela>(); |
||
| 475 | this.gerarParcelas(vendaFormaPagamento, contaAReceber); |
||
| 476 | parcelas = this.consultarParcelasAVencer(contaAReceber); |
||
| 477 | |||
| 478 | for (Parcela parcela : parcelas) { |
||
| 479 | CartaoDTO cartao = new CartaoDTO(); |
||
| 480 | cartao.setParcela(parcela); |
||
| 481 | cartao.setBandeira(vendaFormaPagamento.getDescricaoDaBandeiraCartao()); |
||
| 482 | cartao.setFormaDePagamento(vendaFormaPagamento.getFormaPagamento()); |
||
| 483 | cartao.setDataVenda(vendaFormaPagamento.getVenda().getDataVenda()); |
||
| 484 | cartao.setBruto(vendaFormaPagamento.getValorPagamento()); |
||
| 485 | contas.add(cartao); |
||
| 486 | } |
||
| 487 | } |
||
| 488 | return contas; |
||
| 489 | } |
||
| 490 | |||
| 491 | @Override |
||
| 492 | public List<CartaoDTO> consultarContasDosCartoes(Date dataInicial, Date dataFinal) { |
||
| 493 | List<CartaoDTO> contas = new ArrayList<CartaoDTO>(); |
||
| 494 | List<Conta> contasAReceber = consultarContasAReceber(dataInicial, dataFinal); |
||
| 495 | if (VerificadorUtil.naoEstaNuloOuVazio(contasAReceber)) { |
||
| 496 | for (Conta contaAReceber : contasAReceber) { |
||
| 497 | List<Parcela> parcelas = this.consultarParcelasAVencer(contaAReceber); |
||
| 498 | for (Parcela parcela : parcelas) { |
||
| 499 | CartaoDTO cartao = new CartaoDTO(); |
||
| 500 | cartao.setParcela(parcela); |
||
| 501 | if (VerificadorUtil.naoEstaNulo(contaAReceber.getFormaDePagamento())) { |
||
| 502 | cartao.setBandeira(contaAReceber.getFormaDePagamento().getBandeiraCartao().getDescricao()); |
||
| 503 | cartao.setFormaDePagamento(contaAReceber.getFormaDePagamento().getFormaPagamento()); |
||
| 504 | cartao.setBruto(contaAReceber.getFormaDePagamento().getValorPagamento()); |
||
| 505 | } |
||
| 506 | cartao.setDataVenda(contaAReceber.getVenda().getDataVenda()); |
||
| 507 | contas.add(cartao); |
||
| 508 | } |
||
| 509 | } |
||
| 510 | } |
||
| 511 | return contas; |
||
| 512 | } |
||
| 513 | |||
| 514 | private List<Conta> consultarContasAReceber(Date dataInicial, Date dataFinal) { |
||
| 515 | return contaService.consultarContasAReceberPorPeriodo(dataInicial, dataFinal); |
||
| 516 | } |
||
| 517 | |||
| 518 | @Override |
||
| 519 | public void cadastrarContaComParcelasNaCompra(Compra compra, List<Parcela> listaParcelas) { |
||
| 520 | if (VerificadorUtil.naoEstaNuloOuVazio(listaParcelas)) { |
||
| 521 | Conta contaAPagar = new Conta(TipoConta.CONTA_A_PAGAR.getValor()); |
||
| 522 | Categoria categoriaProduto = new Categoria(); |
||
| 523 | categoriaProduto.setSequencial(ConstantesSEC.Categoria.SEQUENCIAL_CATEGORIA_PRODUTO_7); |
||
| 524 | contaAPagar.setCategoria(categoriaService.consultarPorId(categoriaProduto)); |
||
| 525 | contaAPagar.setPessoa(compra.getFornecedor().getPessoa()); |
||
| 526 | contaAPagar.setIndicadorAtivo(true); |
||
| 527 | contaAPagar.setTipoFrequencia(TipoFrequencia.UNICA.getValor()); |
||
| 528 | contaService.cadastrar(contaAPagar); |
||
| 529 | |||
| 530 | for (Parcela parcela : listaParcelas) { |
||
| 531 | parcela.setObservacao(compra.getFornecedor().getNomeAbreviadoDaPessoa() + " " + parcela.getObservacao()); |
||
| 532 | parcela.setConta(contaAPagar); |
||
| 533 | this.cadastrar(parcela); |
||
| 534 | } |
||
| 535 | compra.setConta(contaAPagar); |
||
| 536 | } |
||
| 537 | } |
||
| 538 | |||
| 539 | public static void main(String[] args) { |
||
| 540 | String numero = "GBMAX NF 032393 001/003"; |
||
| 541 | System.out.println(removeZeros(numero)); |
||
| 542 | } |
||
| 543 | |||
| 544 | public static String removeZeros(String linha) { return linha.replaceFirst("0*", ""); } |
||
| 545 | */ |
||
| 546 | /* |
||
| 547 | @Override |
||
| 548 | public void parcelamento(Parcela parcela, Integer quantidadeParcelas) { |
||
| 549 | Double valorTotal = parcela.getValor(); |
||
| 550 | Double valorDaParcela = valorTotal/quantidadeParcelas; |
||
| 551 | Date dataVencimento = new Date(parcela.getDataVencimento().getTime()); |
||
| 552 | for (int i = 1; i <= quantidadeParcelas; i++) { |
||
| 553 | Parcela parcelado = new Parcela(null, parcela.getContaAPagar(), parcela.getContaBancaria(), DataUtils.getDataAtual(), dataVencimento, null, valorDaParcela, i + "ª PARCELA DA CONTA: " + parcela.getContaAPagar().getSequencial(), true); |
||
| 554 | parcelaRepository.cadastrar(parcelado); |
||
| 555 | dataVencimento = new Date(DataUtils.acrescentarDiasResultadoDiaUtil(dataVencimento, 30).getTime()); |
||
| 556 | } |
||
| 557 | } |
||
| 558 | |||
| 559 | private void cadastrarParcelas(VendaFormaPagamento vendaFormaPagamento, ContaBancaria contaBancaria, Date dataVencimento, Double valorPagamento, ContaAReceber contaAReceber, String observacao) { |
||
| 560 | Parcela parcela = new Parcela(contaAReceber, null, contaBancaria, vendaFormaPagamento.getVenda().getDataVenda(), dataVencimento, null, valorPagamento, observacao + " DA VENDA: " + vendaFormaPagamento.getVenda().getSequencial(), true); |
||
| 561 | parcelaRepository.cadastrar(parcela); |
||
| 562 | } |
||
| 563 | |||
| 564 | @Override |
||
| 565 | public List<Parcela> consultarParcelasPorPeriodo(Parcela parcela, Boolean pagamentoRealizado, Boolean ehContaAReceber, Date dataInicial, Date dataFinal, String ordenacao, int first, int pageSize) { |
||
| 566 | return parcelaRepository.consultarParcelasPorPeriodo(parcela, pagamentoRealizado, ehContaAReceber, dataInicial, dataFinal, ordenacao, first, pageSize); |
||
| 567 | } |
||
| 568 | |||
| 569 | @Override |
||
| 570 | public Integer obterQuantidadeRegistrosConsultarParcelasPorPeriodo(Parcela parcela, Boolean pagamentoRealizado, Boolean ehContaAReceber, Date dataInicial, Date dataFinal) { |
||
| 571 | return parcelaRepository.obterQuantidadeRegistrosConsultarParcelasPorPeriodo(parcela, pagamentoRealizado, ehContaAReceber, dataInicial, dataFinal); |
||
| 572 | } |
||
| 573 | */ |
||
| 574 | } |