Subversion Repositories Integrator Subversion

Rev

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