Subversion Repositories Integrator Subversion

Rev

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