Rev 182 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | package br.com.ec.controller.managedbean; |
| 2 | |||
| 3 | import java.io.Serializable; |
||
| 4 | import java.util.ArrayList; |
||
| 5 | import java.util.List; |
||
| 6 | |||
| 7 | import javax.inject.Inject; |
||
| 8 | import javax.inject.Named; |
||
| 9 | |||
| 10 | import org.primefaces.event.SelectEvent; |
||
| 11 | import org.springframework.context.annotation.Scope; |
||
| 12 | |||
| 13 | import br.com.ec.controller.managedbean.datamodel.ParcelaDataModel; |
||
| 14 | import br.com.ec.domain.dto.ContaGerenciadorDTO; |
||
| 15 | import br.com.ec.domain.model.Categoria; |
||
| 16 | import br.com.ec.domain.model.Compra; |
||
| 17 | import br.com.ec.domain.model.Conta; |
||
| 18 | import br.com.ec.domain.model.Parcela; |
||
| 19 | import br.com.ec.domain.model.Pessoa; |
||
| 20 | import br.com.ec.domain.model.Venda; |
||
| 21 | import br.com.ec.domain.model.tipos.TipoConta; |
||
| 22 | import br.com.ec.domain.service.categoria.CategoriaService; |
||
| 23 | import br.com.ec.domain.service.compra.CompraService; |
||
| 24 | import br.com.ec.domain.service.conta.ContaService; |
||
| 25 | import br.com.ec.domain.service.parcela.ParcelaService; |
||
| 26 | import br.com.ec.domain.shared.ConstantesSEC; |
||
| 27 | import br.edu.cesmac.core.generic.GenericService; |
||
| 28 | import br.edu.cesmac.core.util.DataUtils; |
||
| 29 | import br.edu.cesmac.core.util.StringUtil; |
||
| 30 | import br.edu.cesmac.core.util.VerificadorUtil; |
||
| 31 | import br.edu.cesmac.web.exception.VerificadorLancamentoException; |
||
| 32 | import br.edu.cesmac.web.exception.VerificadorLancamentoException.CommandBean; |
||
| 33 | import br.edu.cesmac.web.generic.AbstractBean; |
||
| 34 | import br.edu.cesmac.web.message.LancadorMensagem; |
||
| 35 | |||
| 36 | @Named |
||
| 37 | @Scope("view") |
||
| 38 | public class ContaGerenciadorBean extends AbstractBean<ContaGerenciadorDTO> implements Serializable { |
||
| 39 | |||
| 40 | private static final long serialVersionUID = 1L; |
||
| 41 | |||
| 42 | private ParcelaService parcelaService; |
||
| 43 | private ContaService contaService; |
||
| 44 | private CompraService compraService; |
||
| 45 | private CategoriaService categoriaService; |
||
| 46 | |||
| 47 | private String chavePagamento; |
||
| 48 | private ParcelaDataModel parcelas; |
||
| 49 | private Long vincularConta; |
||
| 50 | |||
| 51 | @Inject |
||
| 52 | public ContaGerenciadorBean(ParcelaService parcelaService, ContaService contaService, CompraService compraService, CategoriaService categoriaService) { |
||
| 53 | this.parcelaService = parcelaService; |
||
| 54 | this.contaService = contaService; |
||
| 55 | this.compraService = compraService; |
||
| 56 | this.categoriaService = categoriaService; |
||
| 57 | } |
||
| 58 | |||
| 59 | @Override |
||
| 60 | public void preCarregamento() { |
||
| 61 | setEntidade(new ContaGerenciadorDTO()); |
||
| 62 | } |
||
| 63 | |||
| 64 | @Override |
||
| 65 | public void limparEntidade() { |
||
| 66 | setEntidade(new ContaGerenciadorDTO()); |
||
| 67 | setParcelas(null); |
||
| 68 | } |
||
| 69 | |||
| 70 | @Override |
||
| 71 | public GenericService<ContaGerenciadorDTO> getService() { |
||
| 72 | return null; |
||
| 73 | } |
||
| 74 | |||
| 75 | @Override |
||
| 76 | public ContaGerenciadorDTO getEntidade() { |
||
| 77 | return entidade; |
||
| 78 | } |
||
| 79 | |||
| 80 | @Override |
||
| 81 | public ContaGerenciadorDTO getId() { |
||
| 82 | return getEntidade(); |
||
| 83 | } |
||
| 84 | |||
| 85 | public String getChavePagamento() { |
||
| 86 | return chavePagamento; |
||
| 87 | } |
||
| 88 | public void setChavePagamento(String chavePagamento) { |
||
| 89 | this.chavePagamento = chavePagamento; |
||
| 90 | } |
||
| 91 | public void formatarChavePagamento() { |
||
| 92 | if (VerificadorUtil.naoEstaNulo(getChavePagamento())) { |
||
| 93 | setChavePagamento(StringUtil.retornarApenasNumeros(getChavePagamento())); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | public ParcelaDataModel getParcelas() { |
||
| 98 | preencherParcelas(); |
||
| 99 | return parcelas; |
||
| 100 | } |
||
| 101 | public void setParcelas(ParcelaDataModel parcelas) { |
||
| 102 | this.parcelas = parcelas; |
||
| 103 | } |
||
| 104 | public void preencherParcelas() { |
||
| 105 | Parcela parcela = new Parcela(); |
||
| 106 | List<Parcela> listaParcelas = new ArrayList<Parcela>(); |
||
| 107 | parcela.setConta(getEntidade().getConta()); |
||
| 108 | if (VerificadorUtil.naoEstaNulo(getEntidade().getSequencialDaConta())) { |
||
| 109 | listaParcelas = parcelaService.consultarPassandoEntidade(parcela, 0, parcelaService.obterQuantidadeDeRegistrosPassandoEntidade(parcela)); |
||
| 110 | } |
||
| 111 | setParcelas(new ParcelaDataModel(listaParcelas)); |
||
| 112 | } |
||
| 113 | public Double getValorTotalParcelas() { |
||
| 114 | Double valorTotal = new Double(0); |
||
| 115 | if (VerificadorUtil.naoEstaNulo(parcelas)) { |
||
| 116 | for (Parcela parcela : parcelas) { |
||
| 117 | valorTotal = valorTotal + parcela.getValor(); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | return valorTotal; |
||
| 121 | } |
||
| 122 | |||
| 123 | public Long getVincularConta() { |
||
| 124 | return vincularConta; |
||
| 125 | } |
||
| 126 | public void setVincularConta(Long vincularConta) { |
||
| 127 | this.vincularConta = vincularConta; |
||
| 128 | } |
||
| 129 | |||
| 130 | public void onRowSelect(SelectEvent event) { |
||
| 131 | preencherConta(((Parcela) event.getObject())); |
||
| 132 | } |
||
| 133 | |||
| 134 | public void preencherConta(Parcela parcelaSelecionada) { |
||
| 135 | limparEntidade(); |
||
| 136 | if (VerificadorUtil.naoEstaNulo(parcelaSelecionada.getConta())) { |
||
| 137 | Conta conta = new Conta(); |
||
| 138 | conta.setSequencial(parcelaSelecionada.getConta().getSequencial()); |
||
| 139 | getEntidade().setConta(contaService.consultarPorId(conta)); |
||
| 140 | } |
||
| 141 | preencherParcelas(); |
||
| 142 | } |
||
| 143 | |||
| 144 | public void inicializarParcelaAReceber() { |
||
| 145 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 146 | public void execute() { |
||
| 147 | Parcela parcela = new Parcela(); |
||
| 148 | parcela.setIndicadorAtivo(true); |
||
| 149 | parcela.setConta(getEntidade().getConta()); |
||
| 150 | if (VerificadorUtil.naoEstaNulo(getEntidade().getConta().getVenda())) { |
||
| 151 | parcela.setDataEmissao(getEntidade().getConta().getVenda().getDataVenda()); |
||
| 152 | parcela.setObservacao("PARCELA DA VENDA: " + getEntidade().getConta().getVenda().getSequencial()); |
||
| 153 | } else { |
||
| 154 | parcela.setDataEmissao(DataUtils.getDataAtual()); |
||
| 155 | } |
||
| 156 | getEntidade().setParcela(parcela); |
||
| 157 | } |
||
| 158 | }); |
||
| 159 | } |
||
| 160 | |||
| 161 | public void inicializarParcelaAPagar() { |
||
| 162 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 163 | public void execute() { |
||
| 164 | Parcela parcela = new Parcela(); |
||
| 165 | parcela.setIndicadorAtivo(true); |
||
| 166 | if (VerificadorUtil.naoEstaNulo(getEntidade().getConta())) { |
||
| 167 | parcela.setDataEmissao(DataUtils.getDataAtual()); |
||
| 168 | parcela.setConta(getEntidade().getConta()); |
||
| 169 | parcela.setObservacao("PARCELA: " + getEntidade().getConta().getSequencial()); |
||
| 170 | } |
||
| 171 | getEntidade().setParcela(parcela); |
||
| 172 | } |
||
| 173 | }); |
||
| 174 | } |
||
| 175 | |||
| 176 | public void cadastrarParcela() { |
||
| 177 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 178 | public void execute() { |
||
| 179 | parcelaService.cadastrar(getEntidade().getParcela()); |
||
| 180 | LancadorMensagem.lancarSucesso("PARCELA CADASTRADA COM SUCESSO!"); |
||
| 181 | } |
||
| 182 | }); |
||
| 183 | preencherParcelas(); |
||
| 184 | } |
||
| 185 | |||
| 186 | public void receberParcela() { |
||
| 187 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 188 | public void execute() { |
||
| 189 | if (VerificadorUtil.estaNulo(getEntidade().getParcela().getDataPagamento())) { |
||
| 190 | getEntidade().getParcela().setDataPagamento(getEntidade().getParcela().getDataVencimento()); |
||
| 191 | } |
||
| 192 | parcelaService.alterar(getEntidade().getParcela()); |
||
| 193 | cadastrarNovaParcelaPelaFrequencia(getEntidade().getParcela()); |
||
| 194 | LancadorMensagem.lancarSucesso("PARCELA RECEBIDA COM SUCESSO!"); |
||
| 195 | } |
||
| 196 | }); |
||
| 197 | } |
||
| 198 | |||
| 199 | public void pagarParcela() { |
||
| 200 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 201 | public void execute() { |
||
| 202 | if (VerificadorUtil.estaNulo(getEntidade().getParcela().getDataPagamento())) { |
||
| 203 | getEntidade().getParcela().setDataPagamento(DataUtils.getDataAtual()); |
||
| 204 | } |
||
| 205 | parcelaService.alterar(getEntidade().getParcela()); |
||
| 206 | cadastrarNovaParcelaPelaFrequencia(getEntidade().getParcela()); |
||
| 207 | LancadorMensagem.lancarSucesso("PARCELA PAGA COM SUCESSO!"); |
||
| 208 | } |
||
| 209 | }); |
||
| 210 | } |
||
| 211 | |||
| 212 | public void alterarParcela() { |
||
| 213 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 214 | public void execute() { |
||
| 215 | parcelaService.alterar(getEntidade().getParcela()); |
||
| 216 | getEntidade().setParcela(null); |
||
| 217 | preencherParcelas(); |
||
| 218 | LancadorMensagem.lancarSucesso("PARCELA ALTERADA COM SUCESSO!"); |
||
| 219 | } |
||
| 220 | }); |
||
| 221 | } |
||
| 222 | |||
| 223 | public void excluirParcela() { |
||
| 224 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 225 | public void execute() { |
||
| 226 | parcelaService.excluir(getEntidade().getParcela()); |
||
| 227 | preencherParcelas(); |
||
| 228 | excluirContaSemParcela(getEntidade().getParcela()); |
||
| 229 | LancadorMensagem.lancarSucesso("PARCELA EXCLUÍDA COM SUCESSO"); |
||
| 230 | } |
||
| 231 | private void excluirContaSemParcela(Parcela parcela) { |
||
| 232 | if (parcelaService.obterQuantidadeDeRegistrosPassandoEntidade(parcela) == 0) { |
||
| 233 | if (VerificadorUtil.naoEstaNulo(parcela.getConta())) { |
||
| 234 | Compra compraDaConta = compraService.consultarCompraDaConta(parcela.getConta()); |
||
| 235 | if (VerificadorUtil.naoEstaNulo(compraDaConta)) { |
||
| 236 | compraDaConta.setConta(null); |
||
| 237 | compraService.alterar(compraDaConta); |
||
| 238 | } |
||
| 239 | contaService.excluir(parcela.getConta()); |
||
| 240 | prepararParaCadastrarConta(parcela.getConta()); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | }); |
||
| 245 | } |
||
| 246 | |||
| 247 | public void cancelarParcela() { |
||
| 248 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 249 | public void execute() { |
||
| 250 | parcelaService.cancelarParcela(getEntidade().getParcela()); |
||
| 251 | LancadorMensagem.lancarSucesso("PARCELA CANCELADA COM SUCESSO!"); |
||
| 252 | } |
||
| 253 | }); |
||
| 254 | } |
||
| 255 | |||
| 256 | private void cadastrarNovaParcelaPelaFrequencia(Parcela parcela) { |
||
| 257 | ContaGerenciadorDTO contaGerenciador = parcelaService.cadastrarNovaParcelaPelaFrequencia(parcela); |
||
| 258 | if (VerificadorUtil.naoEstaNulo(contaGerenciador)) { |
||
| 259 | setEntidade(contaGerenciador); |
||
| 260 | LancadorMensagem.lancarSucesso("NOVA PARCELA GERADA COM SUCESSO!"); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | public void alterarConta() { |
||
| 265 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 266 | public void execute() { |
||
| 267 | contaService.alterar(getEntidade().getConta()); |
||
| 268 | if (VerificadorUtil.naoEstaNulo(getEntidade().getConta().getCompra())) { |
||
| 269 | getEntidade().getConta().getCompra().setConta(getEntidade().getConta()); |
||
| 270 | compraService.alterar(getEntidade().getConta().getCompra()); |
||
| 271 | } |
||
| 272 | LancadorMensagem.lancarSucesso("CONTA ALTERADA COM SUCESSO!"); |
||
| 273 | } |
||
| 274 | }); |
||
| 275 | } |
||
| 276 | |||
| 277 | public void prepararParaCadastrarContaAReceber(Venda venda) { |
||
| 278 | Conta contaAReceber = new Conta(TipoConta.CONTA_A_RECEBER.getValor()); |
||
| 279 | contaAReceber.setVenda(venda); |
||
| 280 | prepararParaCadastrarConta(contaAReceber); |
||
| 281 | } |
||
| 282 | |||
| 283 | public void prepararParaCadastrarContaAPagar() { |
||
| 284 | prepararParaCadastrarConta(new Conta(TipoConta.CONTA_A_PAGAR.getValor())); |
||
| 285 | } |
||
| 286 | |||
| 287 | public void gerarContaAPagarDaCompra(Compra compra) { |
||
| 288 | prepararParaCadastrarConta(new Conta(TipoConta.CONTA_A_PAGAR.getValor())); |
||
| 289 | Categoria categoriaProduto = new Categoria(); |
||
| 290 | categoriaProduto.setSequencial(ConstantesSEC.Categoria.SEQUENCIAL_CATEGORIA_PRODUTO_7); |
||
| 291 | getEntidade().getConta().setCategoria(categoriaService.consultarPorId(categoriaProduto)); |
||
| 292 | getEntidade().getConta().setPessoa(compra.getFornecedor().getPessoa()); |
||
| 293 | getEntidade().getConta().setCompra(compra); |
||
| 294 | } |
||
| 295 | |||
| 296 | private void prepararParaCadastrarConta(Conta conta) { |
||
| 297 | limparEntidade(); |
||
| 298 | preencherParcelas(); |
||
| 299 | Conta contaParaCadastro = new Conta(conta.getTipoConta()); |
||
| 300 | if (VerificadorUtil.naoEstaNulo(conta.ehContaAReceber()) && VerificadorUtil.naoEstaNulo(conta.getSequencialDaVenda())) { |
||
| 301 | contaParaCadastro.setVenda(conta.getVenda()); |
||
| 302 | contaParaCadastro.setObservacao("VENDA: " + contaParaCadastro.getSequencialDaVenda()); |
||
| 303 | } |
||
| 304 | contaParaCadastro.setIndicadorAtivo(true); |
||
| 305 | getEntidade().setConta(contaParaCadastro); |
||
| 306 | } |
||
| 307 | |||
| 308 | public void consultarConta(Compra compra) { |
||
| 309 | Conta conta = contaService.consultarPorId(compra.getConta()); |
||
| 310 | getEntidade().setConta(conta); |
||
| 311 | } |
||
| 312 | |||
| 313 | public void selecionarCategoria(Categoria categoria) { |
||
| 314 | getEntidade().getConta().setCategoria(categoria); |
||
| 315 | } |
||
| 316 | |||
| 317 | public void retirarCategoria() { |
||
| 318 | getEntidade().getConta().setCategoria(null); |
||
| 319 | } |
||
| 320 | |||
| 321 | public void selecionarPessoa(Pessoa pessoa) { |
||
| 322 | getEntidade().getConta().setPessoa(pessoa); |
||
| 323 | } |
||
| 324 | |||
| 325 | public void retirarPessoa() { |
||
| 326 | getEntidade().getConta().setPessoa(null); |
||
| 327 | } |
||
| 328 | |||
| 329 | public void gerarParcelasDaVenda() { |
||
| 330 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 331 | public void execute() { |
||
| 332 | getEntidade().setConta(parcelaService.gerarParcelasDaVenda(getEntidade().getConta().getVenda(), getEntidade().getConta())); |
||
| 333 | preencherParcelas(); |
||
| 334 | LancadorMensagem.lancarSucesso("PARCELAS GERADAS COM SUCESSO!"); |
||
| 335 | } |
||
| 336 | }); |
||
| 337 | } |
||
| 338 | |||
| 339 | public void gerarParcelasDinheiroDaVenda() { |
||
| 340 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 341 | public void execute() { |
||
| 342 | getEntidade().setConta(parcelaService.gerarParcelasDinheiroDaVenda(getEntidade().getConta().getVenda(), getEntidade().getConta())); |
||
| 343 | preencherParcelas(); |
||
| 344 | LancadorMensagem.lancarSucesso("PARCELAS GERADAS COM SUCESSO!"); |
||
| 345 | } |
||
| 346 | }); |
||
| 347 | } |
||
| 348 | |||
| 349 | public void gerarParcelasCartaoDaVenda() { |
||
| 350 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 351 | public void execute() { |
||
| 352 | getEntidade().setConta(parcelaService.gerarParcelasCartaoDaVenda(getEntidade().getConta().getVenda(), getEntidade().getConta())); |
||
| 353 | preencherParcelas(); |
||
| 354 | LancadorMensagem.lancarSucesso("PARCELAS GERADAS COM SUCESSO!"); |
||
| 355 | } |
||
| 356 | }); |
||
| 357 | } |
||
| 358 | |||
| 359 | public void vincularConta(final Compra compra) { |
||
| 360 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 361 | public void execute() { |
||
| 362 | Conta conta = new Conta(); |
||
| 363 | conta.setSequencial(getVincularConta()); |
||
| 364 | conta = contaService.consultarPorId(conta); |
||
| 365 | compraService.vincularConta(compra, conta); |
||
| 366 | LancadorMensagem.lancarSucesso("CONTA [" + getVincularConta() + "] VINCULADA COM SUCESSO!"); |
||
| 367 | setVincularConta(null); |
||
| 368 | } |
||
| 369 | }); |
||
| 370 | } |
||
| 371 | |||
| 372 | public void desvincularConta(final Compra compra) { |
||
| 373 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 374 | public void execute() { |
||
| 375 | compraService.desvincularConta(compra); |
||
| 376 | LancadorMensagem.lancarSucesso("CONTA [" + compra.getConta().getSequencial() + "] DESVINCULADA COM SUCESSO!"); |
||
| 377 | setVincularConta(null); |
||
| 378 | } |
||
| 379 | }); |
||
| 380 | } |
||
| 381 | |||
| 382 | } |