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.faces.context.FacesContext; |
||
| 8 | import javax.inject.Inject; |
||
| 9 | import javax.inject.Named; |
||
| 10 | import javax.servlet.ServletContext; |
||
| 11 | |||
| 12 | import org.primefaces.model.StreamedContent; |
||
| 13 | import org.springframework.context.annotation.Scope; |
||
| 14 | |||
| 15 | import br.com.ec.domain.dto.EtiquetaDTO; |
||
| 16 | import br.com.ec.domain.model.Compra; |
||
| 17 | import br.com.ec.domain.model.CompraProduto; |
||
| 18 | import br.com.ec.domain.model.Produto; |
||
| 19 | import br.com.ec.domain.service.compraproduto.CompraProdutoService; |
||
| 20 | import br.com.ec.domain.service.etiqueta.EtiquetaService; |
||
| 21 | import br.com.ec.domain.service.produto.ProdutoService; |
||
| 22 | import br.com.ec.domain.shared.ConstantesSEC; |
||
| 23 | import br.com.ec.domain.shared.Ordenador; |
||
| 24 | import br.com.ec.domain.shared.OrdenadorImpl; |
||
| 25 | import br.edu.cesmac.core.consulta.ParametrosConsulta; |
||
| 26 | import br.edu.cesmac.core.generic.GenericService; |
||
| 27 | import br.edu.cesmac.core.util.VerificadorUtil; |
||
| 28 | import br.edu.cesmac.web.exception.VerificadorLancamentoException; |
||
| 29 | import br.edu.cesmac.web.exception.VerificadorLancamentoException.CommandBean; |
||
| 30 | import br.edu.cesmac.web.generic.AbstractBean; |
||
| 31 | import br.edu.cesmac.web.message.LancadorMensagem; |
||
| 32 | |||
| 33 | @Named |
||
| 34 | @Scope("view") |
||
| 35 | public class EtiquetaBean extends AbstractBean<EtiquetaDTO> implements Serializable { |
||
| 36 | |||
| 37 | private static final long serialVersionUID = 1L; |
||
| 38 | |||
| 39 | private CompraProdutoService compraProdutoService; |
||
| 40 | private EtiquetaService etiquetaService; |
||
| 41 | private ProdutoService produtoService; |
||
| 42 | |||
| 43 | private List<EtiquetaDTO> etiquetas; |
||
| 44 | private String codigoProduto; |
||
| 45 | private Ordenador ordenador; |
||
| 46 | |||
| 47 | @Inject |
||
| 48 | public EtiquetaBean(CompraProdutoService compraProdutoService, EtiquetaService etiquetaService, ProdutoService produtoService) { |
||
| 49 | this.compraProdutoService = compraProdutoService; |
||
| 50 | this.etiquetaService = etiquetaService; |
||
| 51 | this.produtoService = produtoService; |
||
| 52 | this.ordenador = new OrdenadorImpl(); |
||
| 53 | } |
||
| 54 | |||
| 55 | @Override |
||
| 56 | public void preCarregamento() { |
||
| 57 | entidade = new EtiquetaDTO(); |
||
| 58 | parametrosConsulta = new ParametrosConsulta<EtiquetaDTO>(); |
||
| 59 | parametrosConsulta.setEntidade(entidade); |
||
| 60 | } |
||
| 61 | |||
| 62 | @Override |
||
| 63 | public void limparEntidade() { |
||
| 64 | EtiquetaDTO etiqueta = new EtiquetaDTO(); |
||
| 65 | etiqueta.setQuantidade(0); |
||
| 66 | setEntidade(etiqueta); |
||
| 67 | setCodigoProduto(""); |
||
| 68 | } |
||
| 69 | |||
| 70 | @Override |
||
| 71 | public GenericService<EtiquetaDTO> getService() { |
||
| 72 | return null; |
||
| 73 | } |
||
| 74 | |||
| 75 | @Override |
||
| 76 | public EtiquetaDTO getEntidade() { |
||
| 77 | return entidade; |
||
| 78 | } |
||
| 79 | |||
| 80 | @Override |
||
| 81 | public EtiquetaDTO getId() { |
||
| 82 | return getEntidade(); |
||
| 83 | } |
||
| 84 | |||
| 85 | public List<EtiquetaDTO> getEtiquetas() { |
||
| 86 | return etiquetas; |
||
| 87 | } |
||
| 88 | public void setEtiquetas(List<EtiquetaDTO> etiquetas) { |
||
| 89 | this.etiquetas = etiquetas; |
||
| 90 | } |
||
| 91 | |||
| 92 | public String getCodigoProduto() { |
||
| 93 | return codigoProduto; |
||
| 94 | } |
||
| 95 | public void setCodigoProduto(String codigoProduto) { |
||
| 96 | this.codigoProduto = codigoProduto; |
||
| 97 | } |
||
| 98 | |||
| 99 | /***************************************************************/ |
||
| 100 | |||
| 101 | public void prepararProdutosParaEtiquetas() { |
||
| 102 | limparEntidade(); |
||
| 103 | setEtiquetas(new ArrayList<EtiquetaDTO>()); |
||
| 104 | preDetalhar(getEntidade()); |
||
| 105 | } |
||
| 106 | |||
| 107 | public void prepararProdutosParaEtiquetas(Compra compra) { |
||
| 108 | limparEntidade(); |
||
| 109 | preencherEtiquetas(compraProdutoService.consultarProdutosDaCompra(compra)); |
||
| 110 | setEtiquetas((List<EtiquetaDTO>) ordenador.ordenar(getEtiquetas())); |
||
| 111 | preDetalhar(getEntidade()); |
||
| 112 | } |
||
| 113 | |||
| 114 | public void preencherEtiquetas(List<CompraProduto> listaCompraProduto) { |
||
| 115 | setEtiquetas(new ArrayList<EtiquetaDTO>()); |
||
| 116 | for (CompraProduto compraProduto : listaCompraProduto) { |
||
| 117 | EtiquetaDTO etiqueta = new EtiquetaDTO(); |
||
| 118 | etiqueta.setProduto(compraProduto.getProduto()); |
||
| 119 | etiqueta.setQuantidade(compraProduto.getQuantidade()); |
||
| 120 | etiqueta.setSequencialCompra(compraProduto.getCompra().getSequencial()); |
||
| 121 | getEtiquetas().add(etiqueta); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | public void removerProduto(final EtiquetaDTO etiqueta) { |
||
| 126 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 127 | public void execute() { |
||
| 128 | getEtiquetas().remove(etiqueta); |
||
| 129 | LancadorMensagem.lancarSucesso("Etiqueta removida com sucesso!"); |
||
| 130 | } |
||
| 131 | }); |
||
| 132 | } |
||
| 133 | |||
| 134 | public void escolherProduto(Produto produto) { |
||
| 135 | limparEntidade(); |
||
| 136 | getEntidade().setProduto(produto); |
||
| 137 | } |
||
| 138 | |||
| 139 | public void consultarProduto() { |
||
| 140 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 141 | public void execute() { |
||
| 142 | Produto produtoSelecionado = produtoService.consultarProdutoPorCodigoOuCodigoEAN(getCodigoProduto()); |
||
| 143 | if (produtoSelecionado != null) { |
||
| 144 | getEntidade().setProduto(produtoSelecionado); |
||
| 145 | adicionandoParametroArgsConfirmandoAcao(true); |
||
| 146 | } else { |
||
| 147 | LancadorMensagem.lancarErro("Produto não encontrado"); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | }); |
||
| 151 | } |
||
| 152 | |||
| 153 | public void adicionarEtiqueta() { |
||
| 154 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 155 | public void execute() { |
||
| 156 | getEtiquetas().add(getEntidade()); |
||
| 157 | limparEntidade(); |
||
| 158 | LancadorMensagem.lancarSucesso("Etiqueta adicionada com sucesso!"); |
||
| 159 | } |
||
| 160 | }); |
||
| 161 | } |
||
| 162 | |||
| 163 | public StreamedContent emitirEtiquetasComBarras() throws Exception { |
||
| 164 | List<EtiquetaDTO> etiquetas = adicionarEtiquetas(); |
||
| 165 | return etiquetaService.gerarEtiquetas(etiquetas, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()), true); |
||
| 166 | } |
||
| 167 | |||
| 168 | public StreamedContent emitirEtiquetasSemBarras() throws Exception { |
||
| 169 | List<EtiquetaDTO> etiquetas = adicionarEtiquetas(); |
||
| 170 | return etiquetaService.gerarEtiquetas(etiquetas, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()), false); |
||
| 171 | } |
||
| 172 | |||
| 173 | private List<EtiquetaDTO> adicionarEtiquetas() { |
||
| 174 | List<EtiquetaDTO> etiquetas = new ArrayList<EtiquetaDTO>(); |
||
| 175 | for (EtiquetaDTO etiqueta : getEtiquetas()) { |
||
| 176 | Integer quantidadeEmitir = etiqueta.getQuantidade(); |
||
| 177 | for (int i = 1; i <= quantidadeEmitir; i++) { |
||
| 178 | etiquetas.add(etiqueta); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | return etiquetas; |
||
| 182 | } |
||
| 183 | |||
| 184 | public int getQuantidadeItens() { |
||
| 185 | if (VerificadorUtil.naoEstaNulo(getEtiquetas())) { |
||
| 186 | Integer quantidade = 0; |
||
| 187 | for (EtiquetaDTO etiqueta : getEtiquetas()) { |
||
| 188 | quantidade = quantidade + etiqueta.getQuantidade(); |
||
| 189 | } |
||
| 190 | return quantidade; |
||
| 191 | } |
||
| 192 | return 0; |
||
| 193 | } |
||
| 194 | |||
| 195 | public int getQuantidadePaginas() { |
||
| 196 | return VerificadorUtil.naoEstaNulo(getEtiquetas()) ? getQuantidadeItens() > 0 ? ((getQuantidadeItens() / ConstantesSEC.Etiqueta.QUANTIDADE_MAXIMA_POR_FOLHA) + 1) : 0 : 0; |
||
| 197 | } |
||
| 198 | |||
| 199 | public int getQuantidadeEtiquetasVazias() { |
||
| 200 | return VerificadorUtil.naoEstaNulo(getEtiquetas()) ? getQuantidadeItens() > 0 ? (ConstantesSEC.Etiqueta.QUANTIDADE_MAXIMA_POR_FOLHA - (getQuantidadeItens() % ConstantesSEC.Etiqueta.QUANTIDADE_MAXIMA_POR_FOLHA)) : 0 : 0; |
||
| 201 | } |
||
| 202 | |||
| 203 | } |