Subversion Repositories Integrator Subversion

Rev

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