Subversion Repositories Integrator Subversion

Rev

Rev 274 | Rev 296 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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