Rev 195 | 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; |
||
| 545 | blopes | 5 | import java.util.Date; |
| 106 | espaco | 6 | import java.util.List; |
| 7 | |||
| 8 | import javax.faces.context.FacesContext; |
||
| 9 | import javax.inject.Inject; |
||
| 10 | import javax.inject.Named; |
||
| 11 | import javax.servlet.ServletContext; |
||
| 12 | |||
| 13 | import org.primefaces.model.StreamedContent; |
||
| 14 | import org.springframework.context.annotation.Scope; |
||
| 15 | |||
| 195 | espaco | 16 | import br.com.ec.core.consulta.ParametrosConsulta; |
| 17 | import br.com.ec.core.exception.NegocioException; |
||
| 18 | import br.com.ec.core.generic.GenericService; |
||
| 545 | blopes | 19 | import br.com.ec.core.util.DataUtils; |
| 195 | espaco | 20 | import br.com.ec.core.util.VerificadorUtil; |
| 106 | espaco | 21 | import br.com.ec.domain.dto.EtiquetaDTO; |
| 22 | import br.com.ec.domain.model.Compra; |
||
| 23 | import br.com.ec.domain.model.CompraProduto; |
||
| 545 | blopes | 24 | import br.com.ec.domain.model.EstoqueAuditoria; |
| 106 | espaco | 25 | import br.com.ec.domain.model.Produto; |
| 26 | import br.com.ec.domain.service.compraproduto.CompraProdutoService; |
||
| 545 | blopes | 27 | import br.com.ec.domain.service.estoqueauditoria.EstoqueAuditoriaService; |
| 106 | espaco | 28 | import br.com.ec.domain.service.etiqueta.EtiquetaService; |
| 29 | import br.com.ec.domain.service.produto.ProdutoService; |
||
| 30 | import br.com.ec.domain.shared.ConstantesSEC; |
||
| 31 | import br.com.ec.domain.shared.Ordenador; |
||
| 32 | import br.com.ec.domain.shared.OrdenadorImpl; |
||
| 195 | espaco | 33 | import br.com.ec.web.exception.VerificadorLancamentoException; |
| 34 | import br.com.ec.web.exception.VerificadorLancamentoException.CommandBean; |
||
| 35 | import br.com.ec.web.generic.AbstractBean; |
||
| 36 | import br.com.ec.web.message.LancadorMensagem; |
||
| 106 | espaco | 37 | |
| 38 | @Named |
||
| 39 | @Scope("view") |
||
| 40 | public class EtiquetaBean extends AbstractBean<EtiquetaDTO> implements Serializable { |
||
| 41 | |||
| 42 | private static final long serialVersionUID = 1L; |
||
| 43 | |||
| 44 | private CompraProdutoService compraProdutoService; |
||
| 45 | private EtiquetaService etiquetaService; |
||
| 46 | private ProdutoService produtoService; |
||
| 545 | blopes | 47 | private EstoqueAuditoriaService estoqueAuditoriaService; |
| 106 | espaco | 48 | |
| 49 | private List<EtiquetaDTO> etiquetas; |
||
| 50 | private Long loteCompra; |
||
| 51 | private String codigoProduto; |
||
| 52 | private Ordenador ordenador; |
||
| 53 | |||
| 545 | blopes | 54 | private Date dataProducao; |
| 55 | private List<EstoqueAuditoria> ultimasProducoes; |
||
| 56 | |||
| 106 | espaco | 57 | @Inject |
| 545 | blopes | 58 | public EtiquetaBean(CompraProdutoService compraProdutoService, EtiquetaService etiquetaService, ProdutoService produtoService, |
| 59 | EstoqueAuditoriaService estoqueAuditoriaService) { |
||
| 106 | espaco | 60 | this.compraProdutoService = compraProdutoService; |
| 61 | this.etiquetaService = etiquetaService; |
||
| 62 | this.produtoService = produtoService; |
||
| 63 | this.ordenador = new OrdenadorImpl(); |
||
| 545 | blopes | 64 | this.estoqueAuditoriaService = estoqueAuditoriaService; |
| 106 | espaco | 65 | } |
| 66 | |||
| 67 | @Override |
||
| 68 | public void preCarregamento() { |
||
| 69 | entidade = new EtiquetaDTO(); |
||
| 70 | parametrosConsulta = new ParametrosConsulta<EtiquetaDTO>(); |
||
| 71 | parametrosConsulta.setEntidade(entidade); |
||
| 72 | } |
||
| 73 | |||
| 74 | @Override |
||
| 75 | public void limparEntidade() { |
||
| 76 | EtiquetaDTO etiqueta = new EtiquetaDTO(); |
||
| 77 | setEntidade(etiqueta); |
||
| 78 | setCodigoProduto(""); |
||
| 545 | blopes | 79 | setDataProducao(DataUtils.getDataAtual()); |
| 106 | espaco | 80 | } |
| 81 | |||
| 82 | @Override |
||
| 83 | public GenericService<EtiquetaDTO> getService() { |
||
| 84 | return null; |
||
| 85 | } |
||
| 86 | |||
| 87 | @Override |
||
| 88 | public EtiquetaDTO getEntidade() { |
||
| 89 | return entidade; |
||
| 90 | } |
||
| 91 | |||
| 92 | @Override |
||
| 93 | public EtiquetaDTO getId() { |
||
| 94 | return getEntidade(); |
||
| 95 | } |
||
| 96 | |||
| 97 | public List<EtiquetaDTO> getEtiquetas() { |
||
| 98 | return etiquetas; |
||
| 99 | } |
||
| 100 | public void setEtiquetas(List<EtiquetaDTO> etiquetas) { |
||
| 101 | this.etiquetas = etiquetas; |
||
| 102 | } |
||
| 103 | |||
| 104 | public Long getLoteCompra() { |
||
| 105 | return loteCompra; |
||
| 106 | } |
||
| 107 | public void setLoteCompra(Long loteCompra) { |
||
| 108 | this.loteCompra = loteCompra; |
||
| 109 | } |
||
| 110 | |||
| 111 | public String getCodigoProduto() { |
||
| 112 | return codigoProduto; |
||
| 113 | } |
||
| 114 | public void setCodigoProduto(String codigoProduto) { |
||
| 115 | this.codigoProduto = codigoProduto; |
||
| 116 | } |
||
| 117 | |||
| 545 | blopes | 118 | public Date getDataProducao() { |
| 119 | return dataProducao; |
||
| 120 | } |
||
| 121 | public void setDataProducao(Date dataProducao) { |
||
| 122 | this.dataProducao = dataProducao; |
||
| 123 | } |
||
| 124 | |||
| 125 | public List<EstoqueAuditoria> getUltimasProducoes() { |
||
| 126 | return ultimasProducoes; |
||
| 127 | } |
||
| 128 | public void setUltimasProducoes(List<EstoqueAuditoria> ultimasProducoes) { |
||
| 129 | this.ultimasProducoes = ultimasProducoes; |
||
| 130 | } |
||
| 131 | |||
| 106 | espaco | 132 | /***************************************************************/ |
| 133 | |||
| 134 | public void prepararProdutosParaEtiquetas() { |
||
| 135 | limparEntidade(); |
||
| 136 | setEtiquetas(new ArrayList<EtiquetaDTO>()); |
||
| 137 | preDetalhar(getEntidade()); |
||
| 138 | } |
||
| 139 | |||
| 140 | public void prepararProdutosParaEtiquetas(Compra compra) { |
||
| 141 | limparEntidade(); |
||
| 142 | preencherEtiquetas(compraProdutoService.consultarProdutosDaCompra(compra)); |
||
| 143 | setEtiquetas((List<EtiquetaDTO>) ordenador.ordenar(getEtiquetas())); |
||
| 144 | preDetalhar(getEntidade()); |
||
| 145 | } |
||
| 146 | |||
| 147 | public void preencherEtiquetas(List<CompraProduto> listaCompraProduto) { |
||
| 148 | setEtiquetas(new ArrayList<EtiquetaDTO>()); |
||
| 149 | for (CompraProduto compraProduto : listaCompraProduto) { |
||
| 150 | EtiquetaDTO etiqueta = new EtiquetaDTO(); |
||
| 151 | etiqueta.setProduto(compraProduto.getProduto()); |
||
| 152 | etiqueta.setQuantidade(compraProduto.getQuantidade()); |
||
| 127 | espaco | 153 | etiqueta.setLote(compraProduto.getCompra().getSequencial()); |
| 106 | espaco | 154 | getEtiquetas().add(etiqueta); |
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | public void removerProduto(final EtiquetaDTO etiqueta) { |
||
| 159 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 160 | public void execute() { |
||
| 161 | getEtiquetas().remove(etiqueta); |
||
| 162 | LancadorMensagem.lancarSucesso("Etiqueta removida com sucesso!"); |
||
| 163 | } |
||
| 164 | }); |
||
| 165 | } |
||
| 166 | |||
| 167 | public void escolherProduto(Produto produto) { |
||
| 168 | limparEntidade(); |
||
| 169 | getEntidade().setProduto(produto); |
||
| 170 | } |
||
| 171 | |||
| 172 | public void consultarProduto() { |
||
| 173 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 174 | public void execute() { |
||
| 175 | Produto produtoSelecionado = produtoService.consultarProdutoPorCodigoOuCodigoEAN(getCodigoProduto()); |
||
| 176 | if (produtoSelecionado != null) { |
||
| 177 | getEntidade().setProduto(produtoSelecionado); |
||
| 127 | espaco | 178 | getEntidade().setLote(getLoteCompra()); |
| 106 | espaco | 179 | adicionandoParametroArgsConfirmandoAcao(true); |
| 180 | } else { |
||
| 181 | LancadorMensagem.lancarErro("Produto não encontrado"); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | }); |
||
| 185 | } |
||
| 186 | |||
| 187 | public void adicionarEtiqueta() { |
||
| 188 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 189 | public void execute() { |
||
| 190 | if (VerificadorUtil.estaNuloOuVazio(getEntidade().getProduto().getValorVarejo())) { |
||
| 191 | throw new NegocioException("Não é possível gerar etiqueta sem preço"); |
||
| 192 | } |
||
| 193 | getEtiquetas().add(getEntidade()); |
||
| 194 | limparEntidade(); |
||
| 195 | LancadorMensagem.lancarSucesso("Etiqueta adicionada com sucesso!"); |
||
| 196 | } |
||
| 197 | }); |
||
| 198 | } |
||
| 199 | |||
| 200 | public StreamedContent emitirEtiquetasComBarras() { |
||
| 201 | List<EtiquetaDTO> etiquetas = adicionarEtiquetas(); |
||
| 202 | return etiquetaService.gerarEtiquetas(etiquetas, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()), true, false); |
||
| 203 | } |
||
| 204 | |||
| 205 | public StreamedContent emitirEtiquetasComBarrasAtacado() { |
||
| 206 | List<EtiquetaDTO> etiquetas = adicionarEtiquetas(); |
||
| 207 | return etiquetaService.gerarEtiquetas(etiquetas, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()), true, true); |
||
| 208 | } |
||
| 209 | |||
| 210 | public StreamedContent emitirEtiquetasSemBarras() throws Exception { |
||
| 211 | List<EtiquetaDTO> etiquetas = adicionarEtiquetas(); |
||
| 212 | return etiquetaService.gerarEtiquetas(etiquetas, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()), false, false); |
||
| 213 | } |
||
| 214 | |||
| 215 | private List<EtiquetaDTO> adicionarEtiquetas() { |
||
| 216 | List<EtiquetaDTO> etiquetas = new ArrayList<EtiquetaDTO>(); |
||
| 217 | for (EtiquetaDTO etiqueta : getEtiquetas()) { |
||
| 218 | Integer quantidadeEmitir = etiqueta.getQuantidade(); |
||
| 219 | for (int i = 1; i <= quantidadeEmitir; i++) { |
||
| 220 | etiquetas.add(etiqueta); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | return etiquetas; |
||
| 224 | } |
||
| 225 | |||
| 226 | public int getQuantidadeItens() { |
||
| 227 | if (VerificadorUtil.naoEstaNulo(getEtiquetas())) { |
||
| 228 | Integer quantidade = 0; |
||
| 229 | for (EtiquetaDTO etiqueta : getEtiquetas()) { |
||
| 230 | quantidade = quantidade + etiqueta.getQuantidade(); |
||
| 231 | } |
||
| 232 | return quantidade; |
||
| 233 | } |
||
| 234 | return 0; |
||
| 235 | } |
||
| 236 | |||
| 237 | public int getQuantidadePaginas() { |
||
| 238 | return VerificadorUtil.naoEstaNulo(getEtiquetas()) ? getQuantidadeItens() > 0 ? ((getQuantidadeItens() / ConstantesSEC.Etiqueta.QUANTIDADE_MAXIMA_POR_FOLHA) + 1) : 0 : 0; |
||
| 239 | } |
||
| 240 | |||
| 241 | public int getQuantidadeEtiquetasVazias() { |
||
| 242 | return VerificadorUtil.naoEstaNulo(getEtiquetas()) ? getQuantidadeItens() > 0 ? (ConstantesSEC.Etiqueta.QUANTIDADE_MAXIMA_POR_FOLHA - (getQuantidadeItens() % ConstantesSEC.Etiqueta.QUANTIDADE_MAXIMA_POR_FOLHA)) : 0 : 0; |
||
| 243 | } |
||
| 244 | |||
| 545 | blopes | 245 | public void consultarUltimasProducoes() { |
| 246 | List<EstoqueAuditoria> ultimasProducoes = estoqueAuditoriaService.consultarUltimasProducoes(getDataProducao()); |
||
| 247 | setUltimasProducoes(ultimasProducoes); |
||
| 248 | } |
||
| 249 | |||
| 250 | public void adicionarEtiquetasUltimasProducoes() { |
||
| 251 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 252 | public void execute() { |
||
| 253 | for (EstoqueAuditoria producao : getUltimasProducoes()) { |
||
| 254 | EtiquetaDTO etiqueta = new EtiquetaDTO(); |
||
| 255 | etiqueta.setProduto(producao.getProduto()); |
||
| 256 | etiqueta.setQuantidade(producao.getQuantidadeEstoqueNovo() - producao.getQuantidadeEstoqueAnterior()); |
||
| 257 | getEtiquetas().add(etiqueta); |
||
| 258 | } |
||
| 259 | limparEntidade(); |
||
| 260 | LancadorMensagem.lancarSucesso("Etiqueta(s) adicionada(s) com sucesso!"); |
||
| 261 | } |
||
| 262 | }); |
||
| 263 | } |
||
| 264 | |||
| 106 | espaco | 265 | } |