Details | 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 | limparEntidade(); |
||
| 132 | Parcela parcelaSelecionada = ((Parcela) event.getObject()); |
||
| 133 | if (VerificadorUtil.naoEstaNulo(parcelaSelecionada.getConta())) { |
||
| 134 | Conta conta = new Conta(); |
||
| 135 | conta.setSequencial(parcelaSelecionada.getConta().getSequencial()); |
||
| 136 | getEntidade().setConta(contaService.consultarPorId(conta)); |
||
| 137 | } |
||
| 138 | preencherParcelas(); |
||
| 139 | } |
||
| 140 | |||
| 141 | public void inicializarParcelaAReceber() { |
||
| 142 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 143 | public void execute() { |
||
| 144 | Parcela parcela = new Parcela(); |
||
| 145 | parcela.setIndicadorAtivo(true); |
||
| 146 | parcela.setConta(getEntidade().getConta()); |
||
| 147 | if (VerificadorUtil.naoEstaNulo(getEntidade().getConta().getVenda())) { |
||
| 148 | parcela.setDataEmissao(getEntidade().getConta().getVenda().getDataVenda()); |
||
| 149 | parcela.setObservacao("PARCELA DA VENDA: " + getEntidade().getConta().getVenda().getSequencial()); |
||
| 150 | } else { |
||
| 151 | parcela.setDataEmissao(DataUtils.getDataAtual()); |
||
| 152 | } |
||
| 153 | getEntidade().setParcela(parcela); |
||
| 154 | } |
||
| 155 | }); |
||
| 156 | } |
||
| 157 | |||
| 158 | public void inicializarParcelaAPagar() { |
||
| 159 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 160 | public void execute() { |
||
| 161 | Parcela parcela = new Parcela(); |
||
| 162 | parcela.setIndicadorAtivo(true); |
||
| 163 | if (VerificadorUtil.naoEstaNulo(getEntidade().getConta())) { |
||
| 164 | parcela.setDataEmissao(DataUtils.getDataAtual()); |
||
| 165 | parcela.setConta(getEntidade().getConta()); |
||
| 166 | parcela.setObservacao("PARCELA: " + getEntidade().getConta().getSequencial()); |
||
| 167 | } |
||
| 168 | getEntidade().setParcela(parcela); |
||
| 169 | } |
||
| 170 | }); |
||
| 171 | } |
||
| 172 | |||
| 173 | public void cadastrarParcela() { |
||
| 174 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 175 | public void execute() { |
||
| 176 | parcelaService.cadastrar(getEntidade().getParcela()); |
||
| 177 | LancadorMensagem.lancarSucesso("PARCELA CADASTRADA COM SUCESSO!"); |
||
| 178 | } |
||
| 179 | }); |
||
| 180 | preencherParcelas(); |
||
| 181 | } |
||
| 182 | |||
| 183 | public void receberParcela() { |
||
| 184 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 185 | public void execute() { |
||
| 186 | if (VerificadorUtil.estaNulo(getEntidade().getParcela().getDataPagamento())) { |
||
| 187 | getEntidade().getParcela().setDataPagamento(getEntidade().getParcela().getDataVencimento()); |
||
| 188 | } |
||
| 189 | parcelaService.alterar(getEntidade().getParcela()); |
||
| 190 | cadastrarNovaParcelaPelaFrequencia(getEntidade().getParcela()); |
||
| 191 | LancadorMensagem.lancarSucesso("PARCELA RECEBIDA COM SUCESSO!"); |
||
| 192 | } |
||
| 193 | }); |
||
| 194 | } |
||
| 195 | |||
| 196 | public void pagarParcela() { |
||
| 197 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 198 | public void execute() { |
||
| 199 | if (VerificadorUtil.estaNulo(getEntidade().getParcela().getDataPagamento())) { |
||
| 200 | getEntidade().getParcela().setDataPagamento(DataUtils.getDataAtual()); |
||
| 201 | } |
||
| 202 | parcelaService.alterar(getEntidade().getParcela()); |
||
| 203 | cadastrarNovaParcelaPelaFrequencia(getEntidade().getParcela()); |
||
| 204 | LancadorMensagem.lancarSucesso("PARCELA PAGA COM SUCESSO!"); |
||
| 205 | } |
||
| 206 | }); |
||
| 207 | } |
||
| 208 | |||
| 209 | public void alterarParcela() { |
||
| 210 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 211 | public void execute() { |
||
| 212 | parcelaService.alterar(getEntidade().getParcela()); |
||
| 213 | getEntidade().setParcela(null); |
||
| 214 | preencherParcelas(); |
||
| 215 | LancadorMensagem.lancarSucesso("PARCELA ALTERADA COM SUCESSO!"); |
||
| 216 | } |
||
| 217 | }); |
||
| 218 | } |
||
| 219 | |||
| 220 | public void excluirParcela() { |
||
| 221 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 222 | public void execute() { |
||
| 223 | parcelaService.excluir(getEntidade().getParcela()); |
||
| 224 | preencherParcelas(); |
||
| 225 | excluirContaSemParcela(getEntidade().getParcela()); |
||
| 226 | LancadorMensagem.lancarSucesso("PARCELA EXCLUÍDA COM SUCESSO"); |
||
| 227 | } |
||
| 228 | private void excluirContaSemParcela(Parcela parcela) { |
||
| 229 | if (parcelaService.obterQuantidadeDeRegistrosPassandoEntidade(parcela) == 0) { |
||
| 230 | if (VerificadorUtil.naoEstaNulo(parcela.getConta())) { |
||
| 231 | Compra compraDaConta = compraService.consultarCompraDaConta(parcela.getConta()); |
||
| 232 | if (VerificadorUtil.naoEstaNulo(compraDaConta)) { |
||
| 233 | compraDaConta.setConta(null); |
||
| 234 | compraService.alterar(compraDaConta); |
||
| 235 | } |
||
| 236 | contaService.excluir(parcela.getConta()); |
||
| 237 | prepararParaCadastrarConta(parcela.getConta()); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | }); |
||
| 242 | } |
||
| 243 | |||
| 244 | public void cancelarParcela() { |
||
| 245 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 246 | public void execute() { |
||
| 247 | parcelaService.cancelarParcela(getEntidade().getParcela()); |
||
| 248 | LancadorMensagem.lancarSucesso("PARCELA CANCELADA COM SUCESSO!"); |
||
| 249 | } |
||
| 250 | }); |
||
| 251 | } |
||
| 252 | |||
| 253 | private void cadastrarNovaParcelaPelaFrequencia(Parcela parcela) { |
||
| 254 | ContaGerenciadorDTO contaGerenciador = parcelaService.cadastrarNovaParcelaPelaFrequencia(parcela); |
||
| 255 | if (VerificadorUtil.naoEstaNulo(contaGerenciador)) { |
||
| 256 | setEntidade(contaGerenciador); |
||
| 257 | LancadorMensagem.lancarSucesso("NOVA PARCELA GERADA COM SUCESSO!"); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | public void alterarConta() { |
||
| 262 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 263 | public void execute() { |
||
| 264 | contaService.alterar(getEntidade().getConta()); |
||
| 265 | if (VerificadorUtil.naoEstaNulo(getEntidade().getConta().getCompra())) { |
||
| 266 | getEntidade().getConta().getCompra().setConta(getEntidade().getConta()); |
||
| 267 | compraService.alterar(getEntidade().getConta().getCompra()); |
||
| 268 | } |
||
| 269 | LancadorMensagem.lancarSucesso("CONTA ALTERADA COM SUCESSO!"); |
||
| 270 | } |
||
| 271 | }); |
||
| 272 | } |
||
| 273 | |||
| 274 | public void prepararParaCadastrarContaAReceber(Venda venda) { |
||
| 275 | Conta contaAReceber = new Conta(TipoConta.CONTA_A_RECEBER.getValor()); |
||
| 276 | contaAReceber.setVenda(venda); |
||
| 277 | prepararParaCadastrarConta(contaAReceber); |
||
| 278 | } |
||
| 279 | |||
| 280 | public void prepararParaCadastrarContaAPagar() { |
||
| 281 | prepararParaCadastrarConta(new Conta(TipoConta.CONTA_A_PAGAR.getValor())); |
||
| 282 | } |
||
| 283 | |||
| 284 | public void gerarContaAPagarDaCompra(Compra compra) { |
||
| 285 | prepararParaCadastrarConta(new Conta(TipoConta.CONTA_A_PAGAR.getValor())); |
||
| 286 | Categoria categoriaProduto = new Categoria(); |
||
| 287 | categoriaProduto.setSequencial(ConstantesSEC.Categoria.SEQUENCIAL_CATEGORIA_PRODUTO_7); |
||
| 288 | getEntidade().getConta().setCategoria(categoriaService.consultarPorId(categoriaProduto)); |
||
| 289 | getEntidade().getConta().setPessoa(compra.getFornecedor().getPessoa()); |
||
| 290 | getEntidade().getConta().setCompra(compra); |
||
| 291 | } |
||
| 292 | |||
| 293 | private void prepararParaCadastrarConta(Conta conta) { |
||
| 294 | limparEntidade(); |
||
| 295 | preencherParcelas(); |
||
| 296 | Conta contaParaCadastro = new Conta(conta.getTipoConta()); |
||
| 297 | if (VerificadorUtil.naoEstaNulo(conta.ehContaAReceber()) && VerificadorUtil.naoEstaNulo(conta.getSequencialDaVenda())) { |
||
| 298 | contaParaCadastro.setVenda(conta.getVenda()); |
||
| 299 | contaParaCadastro.setObservacao("VENDA: " + contaParaCadastro.getSequencialDaVenda()); |
||
| 300 | } |
||
| 301 | contaParaCadastro.setIndicadorAtivo(true); |
||
| 302 | getEntidade().setConta(contaParaCadastro); |
||
| 303 | } |
||
| 304 | |||
| 305 | public void consultarConta(Compra compra) { |
||
| 306 | Conta conta = contaService.consultarPorId(compra.getConta()); |
||
| 307 | getEntidade().setConta(conta); |
||
| 308 | } |
||
| 309 | |||
| 310 | public void selecionarCategoria(Categoria categoria) { |
||
| 311 | getEntidade().getConta().setCategoria(categoria); |
||
| 312 | } |
||
| 313 | |||
| 314 | public void retirarCategoria() { |
||
| 315 | getEntidade().getConta().setCategoria(null); |
||
| 316 | } |
||
| 317 | |||
| 318 | public void selecionarPessoa(Pessoa pessoa) { |
||
| 319 | getEntidade().getConta().setPessoa(pessoa); |
||
| 320 | } |
||
| 321 | |||
| 322 | public void retirarPessoa() { |
||
| 323 | getEntidade().getConta().setPessoa(null); |
||
| 324 | } |
||
| 325 | |||
| 326 | public void gerarParcelasDaVenda() { |
||
| 327 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 328 | public void execute() { |
||
| 329 | getEntidade().setConta(parcelaService.gerarParcelasDaVenda(getEntidade().getConta().getVenda(), getEntidade().getConta())); |
||
| 330 | preencherParcelas(); |
||
| 331 | LancadorMensagem.lancarSucesso("PARCELAS GERADAS COM SUCESSO!"); |
||
| 332 | } |
||
| 333 | }); |
||
| 334 | } |
||
| 335 | |||
| 336 | public void gerarParcelasDinheiroDaVenda() { |
||
| 337 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 338 | public void execute() { |
||
| 339 | getEntidade().setConta(parcelaService.gerarParcelasDinheiroDaVenda(getEntidade().getConta().getVenda(), getEntidade().getConta())); |
||
| 340 | preencherParcelas(); |
||
| 341 | LancadorMensagem.lancarSucesso("PARCELAS GERADAS COM SUCESSO!"); |
||
| 342 | } |
||
| 343 | }); |
||
| 344 | } |
||
| 345 | |||
| 346 | public void gerarParcelasCartaoDaVenda() { |
||
| 347 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 348 | public void execute() { |
||
| 349 | getEntidade().setConta(parcelaService.gerarParcelasCartaoDaVenda(getEntidade().getConta().getVenda(), getEntidade().getConta())); |
||
| 350 | preencherParcelas(); |
||
| 351 | LancadorMensagem.lancarSucesso("PARCELAS GERADAS COM SUCESSO!"); |
||
| 352 | } |
||
| 353 | }); |
||
| 354 | } |
||
| 355 | |||
| 356 | public void vincularConta(final Compra compra) { |
||
| 357 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 358 | public void execute() { |
||
| 359 | Conta conta = new Conta(); |
||
| 360 | conta.setSequencial(getVincularConta()); |
||
| 361 | conta = contaService.consultarPorId(conta); |
||
| 362 | compraService.vincularConta(compra, conta); |
||
| 363 | LancadorMensagem.lancarSucesso("CONTA [" + getVincularConta() + "] VINCULADA COM SUCESSO!"); |
||
| 364 | setVincularConta(null); |
||
| 365 | } |
||
| 366 | }); |
||
| 367 | } |
||
| 368 | |||
| 369 | public void desvincularConta(final Compra compra) { |
||
| 370 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 371 | public void execute() { |
||
| 372 | compraService.desvincularConta(compra); |
||
| 373 | LancadorMensagem.lancarSucesso("CONTA [" + compra.getConta().getSequencial() + "] DESVINCULADA COM SUCESSO!"); |
||
| 374 | setVincularConta(null); |
||
| 375 | } |
||
| 376 | }); |
||
| 377 | } |
||
| 378 | |||
| 379 | } |