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