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