Subversion Repositories Integrator Subversion

Rev

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