Rev 127 | Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 106 | 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.exception.NegocioException; |
||
| 27 | import br.edu.cesmac.core.generic.GenericService; |
||
| 28 | import br.edu.cesmac.core.util.VerificadorUtil; |
||
| 29 | import br.edu.cesmac.web.exception.VerificadorLancamentoException; |
||
| 30 | import br.edu.cesmac.web.exception.VerificadorLancamentoException.CommandBean; |
||
| 31 | import br.edu.cesmac.web.generic.AbstractBean; |
||
| 32 | import br.edu.cesmac.web.message.LancadorMensagem; |
||
| 33 | |||
| 34 | @Named |
||
| 35 | @Scope("view") |
||
| 36 | public class EtiquetaBean extends AbstractBean<EtiquetaDTO> implements Serializable { |
||
| 37 | |||
| 38 | private static final long serialVersionUID = 1L; |
||
| 39 | |||
| 40 | private CompraProdutoService compraProdutoService; |
||
| 41 | private EtiquetaService etiquetaService; |
||
| 42 | private ProdutoService produtoService; |
||
| 43 | |||
| 44 | private List<EtiquetaDTO> etiquetas; |
||
| 45 | private Long loteCompra; |
||
| 46 | private String codigoProduto; |
||
| 47 | private Ordenador ordenador; |
||
| 48 | |||
| 49 | @Inject |
||
| 50 | public EtiquetaBean(CompraProdutoService compraProdutoService, EtiquetaService etiquetaService, ProdutoService produtoService) { |
||
| 51 | this.compraProdutoService = compraProdutoService; |
||
| 52 | this.etiquetaService = etiquetaService; |
||
| 53 | this.produtoService = produtoService; |
||
| 54 | this.ordenador = new OrdenadorImpl(); |
||
| 55 | } |
||
| 56 | |||
| 57 | @Override |
||
| 58 | public void preCarregamento() { |
||
| 59 | entidade = new EtiquetaDTO(); |
||
| 60 | parametrosConsulta = new ParametrosConsulta<EtiquetaDTO>(); |
||
| 61 | parametrosConsulta.setEntidade(entidade); |
||
| 62 | } |
||
| 63 | |||
| 64 | @Override |
||
| 65 | public void limparEntidade() { |
||
| 66 | EtiquetaDTO etiqueta = new EtiquetaDTO(); |
||
| 67 | etiqueta.setQuantidade(0); |
||
| 68 | setEntidade(etiqueta); |
||
| 69 | setCodigoProduto(""); |
||
| 70 | } |
||
| 71 | |||
| 72 | @Override |
||
| 73 | public GenericService<EtiquetaDTO> getService() { |
||
| 74 | return null; |
||
| 75 | } |
||
| 76 | |||
| 77 | @Override |
||
| 78 | public EtiquetaDTO getEntidade() { |
||
| 79 | return entidade; |
||
| 80 | } |
||
| 81 | |||
| 82 | @Override |
||
| 83 | public EtiquetaDTO getId() { |
||
| 84 | return getEntidade(); |
||
| 85 | } |
||
| 86 | |||
| 87 | public List<EtiquetaDTO> getEtiquetas() { |
||
| 88 | return etiquetas; |
||
| 89 | } |
||
| 90 | public void setEtiquetas(List<EtiquetaDTO> etiquetas) { |
||
| 91 | this.etiquetas = etiquetas; |
||
| 92 | } |
||
| 93 | |||
| 94 | public Long getLoteCompra() { |
||
| 95 | return loteCompra; |
||
| 96 | } |
||
| 97 | public void setLoteCompra(Long loteCompra) { |
||
| 98 | this.loteCompra = loteCompra; |
||
| 99 | } |
||
| 100 | |||
| 101 | public String getCodigoProduto() { |
||
| 102 | return codigoProduto; |
||
| 103 | } |
||
| 104 | public void setCodigoProduto(String codigoProduto) { |
||
| 105 | this.codigoProduto = codigoProduto; |
||
| 106 | } |
||
| 107 | |||
| 108 | /***************************************************************/ |
||
| 109 | |||
| 110 | public void prepararProdutosParaEtiquetas() { |
||
| 111 | limparEntidade(); |
||
| 112 | setEtiquetas(new ArrayList<EtiquetaDTO>()); |
||
| 113 | preDetalhar(getEntidade()); |
||
| 114 | } |
||
| 115 | |||
| 116 | public void prepararProdutosParaEtiquetas(Compra compra) { |
||
| 117 | limparEntidade(); |
||
| 118 | preencherEtiquetas(compraProdutoService.consultarProdutosDaCompra(compra)); |
||
| 119 | setEtiquetas((List<EtiquetaDTO>) ordenador.ordenar(getEtiquetas())); |
||
| 120 | preDetalhar(getEntidade()); |
||
| 121 | } |
||
| 122 | |||
| 123 | public void preencherEtiquetas(List<CompraProduto> listaCompraProduto) { |
||
| 124 | setEtiquetas(new ArrayList<EtiquetaDTO>()); |
||
| 125 | for (CompraProduto compraProduto : listaCompraProduto) { |
||
| 126 | EtiquetaDTO etiqueta = new EtiquetaDTO(); |
||
| 127 | etiqueta.setProduto(compraProduto.getProduto()); |
||
| 128 | etiqueta.setQuantidade(compraProduto.getQuantidade()); |
||
| 129 | etiqueta.setSequencialCompra(compraProduto.getCompra().getSequencial()); |
||
| 130 | getEtiquetas().add(etiqueta); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | public void removerProduto(final EtiquetaDTO etiqueta) { |
||
| 135 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 136 | public void execute() { |
||
| 137 | getEtiquetas().remove(etiqueta); |
||
| 138 | LancadorMensagem.lancarSucesso("Etiqueta removida com sucesso!"); |
||
| 139 | } |
||
| 140 | }); |
||
| 141 | } |
||
| 142 | |||
| 143 | public void escolherProduto(Produto produto) { |
||
| 144 | limparEntidade(); |
||
| 145 | getEntidade().setProduto(produto); |
||
| 146 | } |
||
| 147 | |||
| 148 | public void consultarProduto() { |
||
| 149 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 150 | public void execute() { |
||
| 151 | Produto produtoSelecionado = produtoService.consultarProdutoPorCodigoOuCodigoEAN(getCodigoProduto()); |
||
| 152 | if (produtoSelecionado != null) { |
||
| 153 | getEntidade().setProduto(produtoSelecionado); |
||
| 154 | adicionandoParametroArgsConfirmandoAcao(true); |
||
| 155 | } else { |
||
| 156 | LancadorMensagem.lancarErro("Produto não encontrado"); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | }); |
||
| 160 | } |
||
| 161 | |||
| 162 | public void adicionarEtiqueta() { |
||
| 163 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 164 | public void execute() { |
||
| 165 | if (VerificadorUtil.estaNuloOuVazio(getEntidade().getProduto().getValorVarejo())) { |
||
| 166 | throw new NegocioException("Não é possível gerar etiqueta sem preço"); |
||
| 167 | } |
||
| 168 | getEtiquetas().add(getEntidade()); |
||
| 169 | limparEntidade(); |
||
| 170 | LancadorMensagem.lancarSucesso("Etiqueta adicionada com sucesso!"); |
||
| 171 | } |
||
| 172 | }); |
||
| 173 | } |
||
| 174 | |||
| 175 | public StreamedContent emitirEtiquetasComBarras() { |
||
| 176 | List<EtiquetaDTO> etiquetas = adicionarEtiquetas(); |
||
| 177 | return etiquetaService.gerarEtiquetas(etiquetas, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()), true, false); |
||
| 178 | } |
||
| 179 | |||
| 180 | public StreamedContent emitirEtiquetasComBarrasAtacado() { |
||
| 181 | List<EtiquetaDTO> etiquetas = adicionarEtiquetas(); |
||
| 182 | return etiquetaService.gerarEtiquetas(etiquetas, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()), true, true); |
||
| 183 | } |
||
| 184 | |||
| 185 | public StreamedContent emitirEtiquetasSemBarras() throws Exception { |
||
| 186 | List<EtiquetaDTO> etiquetas = adicionarEtiquetas(); |
||
| 187 | return etiquetaService.gerarEtiquetas(etiquetas, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()), false, false); |
||
| 188 | } |
||
| 189 | |||
| 190 | private List<EtiquetaDTO> adicionarEtiquetas() { |
||
| 191 | List<EtiquetaDTO> etiquetas = new ArrayList<EtiquetaDTO>(); |
||
| 192 | for (EtiquetaDTO etiqueta : getEtiquetas()) { |
||
| 193 | Integer quantidadeEmitir = etiqueta.getQuantidade(); |
||
| 194 | for (int i = 1; i <= quantidadeEmitir; i++) { |
||
| 195 | if (VerificadorUtil.naoEstaNuloOuVazio(getLoteCompra())) { |
||
| 196 | etiqueta.setSequencialCompra(getLoteCompra()); |
||
| 197 | } |
||
| 198 | etiquetas.add(etiqueta); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | return etiquetas; |
||
| 202 | } |
||
| 203 | |||
| 204 | public int getQuantidadeItens() { |
||
| 205 | if (VerificadorUtil.naoEstaNulo(getEtiquetas())) { |
||
| 206 | Integer quantidade = 0; |
||
| 207 | for (EtiquetaDTO etiqueta : getEtiquetas()) { |
||
| 208 | quantidade = quantidade + etiqueta.getQuantidade(); |
||
| 209 | } |
||
| 210 | return quantidade; |
||
| 211 | } |
||
| 212 | return 0; |
||
| 213 | } |
||
| 214 | |||
| 215 | public int getQuantidadePaginas() { |
||
| 216 | return VerificadorUtil.naoEstaNulo(getEtiquetas()) ? getQuantidadeItens() > 0 ? ((getQuantidadeItens() / ConstantesSEC.Etiqueta.QUANTIDADE_MAXIMA_POR_FOLHA) + 1) : 0 : 0; |
||
| 217 | } |
||
| 218 | |||
| 219 | public int getQuantidadeEtiquetasVazias() { |
||
| 220 | return VerificadorUtil.naoEstaNulo(getEtiquetas()) ? getQuantidadeItens() > 0 ? (ConstantesSEC.Etiqueta.QUANTIDADE_MAXIMA_POR_FOLHA - (getQuantidadeItens() % ConstantesSEC.Etiqueta.QUANTIDADE_MAXIMA_POR_FOLHA)) : 0 : 0; |
||
| 221 | } |
||
| 222 | |||
| 223 | } |