Subversion Repositories Integrator Subversion

Rev

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