Rev 127 | Go to most recent revision | Details | Compare with Previous | 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 | |||
| 195 | espaco | 15 | import br.com.ec.core.consulta.ParametrosConsulta; |
| 16 | import br.com.ec.core.exception.NegocioException; |
||
| 17 | import br.com.ec.core.generic.GenericService; |
||
| 18 | import br.com.ec.core.util.VerificadorUtil; |
||
| 106 | espaco | 19 | import br.com.ec.domain.dto.EtiquetaDTO; |
| 20 | import br.com.ec.domain.model.Compra; |
||
| 21 | import br.com.ec.domain.model.CompraProduto; |
||
| 22 | import br.com.ec.domain.model.Produto; |
||
| 23 | import br.com.ec.domain.service.compraproduto.CompraProdutoService; |
||
| 24 | import br.com.ec.domain.service.etiqueta.EtiquetaService; |
||
| 25 | import br.com.ec.domain.service.produto.ProdutoService; |
||
| 26 | import br.com.ec.domain.shared.ConstantesSEC; |
||
| 27 | import br.com.ec.domain.shared.Ordenador; |
||
| 28 | import br.com.ec.domain.shared.OrdenadorImpl; |
||
| 195 | espaco | 29 | import br.com.ec.web.exception.VerificadorLancamentoException; |
| 30 | import br.com.ec.web.exception.VerificadorLancamentoException.CommandBean; |
||
| 31 | import br.com.ec.web.generic.AbstractBean; |
||
| 32 | import br.com.ec.web.message.LancadorMensagem; |
||
| 106 | espaco | 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 | setEntidade(etiqueta); |
||
| 68 | setCodigoProduto(""); |
||
| 69 | } |
||
| 70 | |||
| 71 | @Override |
||
| 72 | public GenericService<EtiquetaDTO> getService() { |
||
| 73 | return null; |
||
| 74 | } |
||
| 75 | |||
| 76 | @Override |
||
| 77 | public EtiquetaDTO getEntidade() { |
||
| 78 | return entidade; |
||
| 79 | } |
||
| 80 | |||
| 81 | @Override |
||
| 82 | public EtiquetaDTO getId() { |
||
| 83 | return getEntidade(); |
||
| 84 | } |
||
| 85 | |||
| 86 | public List<EtiquetaDTO> getEtiquetas() { |
||
| 87 | return etiquetas; |
||
| 88 | } |
||
| 89 | public void setEtiquetas(List<EtiquetaDTO> etiquetas) { |
||
| 90 | this.etiquetas = etiquetas; |
||
| 91 | } |
||
| 92 | |||
| 93 | public Long getLoteCompra() { |
||
| 94 | return loteCompra; |
||
| 95 | } |
||
| 96 | public void setLoteCompra(Long loteCompra) { |
||
| 97 | this.loteCompra = loteCompra; |
||
| 98 | } |
||
| 99 | |||
| 100 | public String getCodigoProduto() { |
||
| 101 | return codigoProduto; |
||
| 102 | } |
||
| 103 | public void setCodigoProduto(String codigoProduto) { |
||
| 104 | this.codigoProduto = codigoProduto; |
||
| 105 | } |
||
| 106 | |||
| 107 | /***************************************************************/ |
||
| 108 | |||
| 109 | public void prepararProdutosParaEtiquetas() { |
||
| 110 | limparEntidade(); |
||
| 111 | setEtiquetas(new ArrayList<EtiquetaDTO>()); |
||
| 112 | preDetalhar(getEntidade()); |
||
| 113 | } |
||
| 114 | |||
| 115 | public void prepararProdutosParaEtiquetas(Compra compra) { |
||
| 116 | limparEntidade(); |
||
| 117 | preencherEtiquetas(compraProdutoService.consultarProdutosDaCompra(compra)); |
||
| 118 | setEtiquetas((List<EtiquetaDTO>) ordenador.ordenar(getEtiquetas())); |
||
| 119 | preDetalhar(getEntidade()); |
||
| 120 | } |
||
| 121 | |||
| 122 | public void preencherEtiquetas(List<CompraProduto> listaCompraProduto) { |
||
| 123 | setEtiquetas(new ArrayList<EtiquetaDTO>()); |
||
| 124 | for (CompraProduto compraProduto : listaCompraProduto) { |
||
| 125 | EtiquetaDTO etiqueta = new EtiquetaDTO(); |
||
| 126 | etiqueta.setProduto(compraProduto.getProduto()); |
||
| 127 | etiqueta.setQuantidade(compraProduto.getQuantidade()); |
||
| 127 | espaco | 128 | etiqueta.setLote(compraProduto.getCompra().getSequencial()); |
| 106 | espaco | 129 | getEtiquetas().add(etiqueta); |
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | public void removerProduto(final EtiquetaDTO etiqueta) { |
||
| 134 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 135 | public void execute() { |
||
| 136 | getEtiquetas().remove(etiqueta); |
||
| 137 | LancadorMensagem.lancarSucesso("Etiqueta removida com sucesso!"); |
||
| 138 | } |
||
| 139 | }); |
||
| 140 | } |
||
| 141 | |||
| 142 | public void escolherProduto(Produto produto) { |
||
| 143 | limparEntidade(); |
||
| 144 | getEntidade().setProduto(produto); |
||
| 145 | } |
||
| 146 | |||
| 147 | public void consultarProduto() { |
||
| 148 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 149 | public void execute() { |
||
| 150 | Produto produtoSelecionado = produtoService.consultarProdutoPorCodigoOuCodigoEAN(getCodigoProduto()); |
||
| 151 | if (produtoSelecionado != null) { |
||
| 152 | getEntidade().setProduto(produtoSelecionado); |
||
| 127 | espaco | 153 | getEntidade().setLote(getLoteCompra()); |
| 106 | espaco | 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 | etiquetas.add(etiqueta); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | return etiquetas; |
||
| 199 | } |
||
| 200 | |||
| 201 | public int getQuantidadeItens() { |
||
| 202 | if (VerificadorUtil.naoEstaNulo(getEtiquetas())) { |
||
| 203 | Integer quantidade = 0; |
||
| 204 | for (EtiquetaDTO etiqueta : getEtiquetas()) { |
||
| 205 | quantidade = quantidade + etiqueta.getQuantidade(); |
||
| 206 | } |
||
| 207 | return quantidade; |
||
| 208 | } |
||
| 209 | return 0; |
||
| 210 | } |
||
| 211 | |||
| 212 | public int getQuantidadePaginas() { |
||
| 213 | return VerificadorUtil.naoEstaNulo(getEtiquetas()) ? getQuantidadeItens() > 0 ? ((getQuantidadeItens() / ConstantesSEC.Etiqueta.QUANTIDADE_MAXIMA_POR_FOLHA) + 1) : 0 : 0; |
||
| 214 | } |
||
| 215 | |||
| 216 | public int getQuantidadeEtiquetasVazias() { |
||
| 217 | return VerificadorUtil.naoEstaNulo(getEtiquetas()) ? getQuantidadeItens() > 0 ? (ConstantesSEC.Etiqueta.QUANTIDADE_MAXIMA_POR_FOLHA - (getQuantidadeItens() % ConstantesSEC.Etiqueta.QUANTIDADE_MAXIMA_POR_FOLHA)) : 0 : 0; |
||
| 218 | } |
||
| 219 | |||
| 220 | } |