Subversion Repositories Integrator Subversion

Rev

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