Subversion Repositories Integrator Subversion

Rev

Rev 693 | Details | Compare with Previous | Last modification | View Log | RSS feed

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