Subversion Repositories Integrator Subversion

Rev

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

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