Subversion Repositories Integrator Subversion

Rev

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