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