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