Rev 170 | Rev 470 | 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.domain.model; |
| 2 | |||
| 3 | import java.io.ByteArrayInputStream; |
||
| 4 | import java.io.ByteArrayOutputStream; |
||
| 5 | import java.io.IOException; |
||
| 6 | import java.io.Serializable; |
||
| 7 | import java.io.StringReader; |
||
| 8 | import java.util.Date; |
||
| 9 | import java.util.List; |
||
| 10 | |||
| 11 | import javax.persistence.Column; |
||
| 12 | import javax.persistence.Entity; |
||
| 13 | import javax.persistence.GeneratedValue; |
||
| 14 | import javax.persistence.GenerationType; |
||
| 15 | import javax.persistence.Id; |
||
| 16 | import javax.persistence.JoinColumn; |
||
| 17 | import javax.persistence.ManyToOne; |
||
| 18 | import javax.persistence.SequenceGenerator; |
||
| 19 | import javax.persistence.Table; |
||
| 20 | import javax.persistence.Transient; |
||
| 21 | import javax.validation.constraints.NotNull; |
||
| 22 | import javax.validation.constraints.Size; |
||
| 23 | import javax.xml.bind.JAXBContext; |
||
| 24 | import javax.xml.bind.JAXBException; |
||
| 25 | import javax.xml.bind.Unmarshaller; |
||
| 26 | import javax.xml.parsers.DocumentBuilder; |
||
| 27 | import javax.xml.parsers.DocumentBuilderFactory; |
||
| 28 | import javax.xml.transform.Transformer; |
||
| 29 | import javax.xml.transform.TransformerFactory; |
||
| 30 | import javax.xml.transform.dom.DOMSource; |
||
| 31 | import javax.xml.transform.stream.StreamResult; |
||
| 32 | import javax.xml.transform.stream.StreamSource; |
||
| 33 | |||
| 34 | import org.apache.commons.io.IOUtils; |
||
| 35 | import org.hibernate.annotations.ForeignKey; |
||
| 36 | import org.primefaces.model.DefaultStreamedContent; |
||
| 37 | import org.w3c.dom.Document; |
||
| 38 | |||
| 195 | espaco | 39 | import br.com.ec.core.exception.NegocioException; |
| 40 | import br.com.ec.core.generic.identidade.Identidade; |
||
| 41 | import br.com.ec.core.interfaces.Alterar; |
||
| 42 | import br.com.ec.core.interfaces.Cadastrar; |
||
| 43 | import br.com.ec.core.util.StringUtil; |
||
| 44 | import br.com.ec.core.util.VerificadorUtil; |
||
| 170 | espaco | 45 | import br.com.ec.domain.model.tipos.nfe.TipoModeloNotaFiscal; |
| 46 | import br.com.ec.domain.model.tipos.nfe.TipoNotaFiscal; |
||
| 47 | import br.com.ec.domain.model.tipos.nfe.TipoNotaFiscalNaturezaOperacao; |
||
| 48 | import br.com.ec.domain.model.tipos.nfe.TipoNotaFiscalPresencaComprador; |
||
| 49 | import br.com.ec.domain.model.tipos.nfe.TipoNotaFiscalStatusRetorno; |
||
| 195 | espaco | 50 | import br.com.ec.relatorio.util.RelatorioUtils; |
| 51 | import br.com.ec.web.util.DataUtil; |
||
| 106 | espaco | 52 | import nfce.java.TNfeProc; |
| 53 | |||
| 54 | @Entity |
||
| 55 | @Table(name="sec_nota_fiscal", schema="sc_sec") |
||
| 56 | public class NotaFiscal implements Serializable, Identidade { |
||
| 57 | |||
| 58 | private static final long serialVersionUID = 1L; |
||
| 59 | |||
| 60 | private Long sequencial; |
||
| 170 | espaco | 61 | private String tipoModeloNotaFiscal; |
| 62 | private String serie; |
||
| 106 | espaco | 63 | private Long numeroNotaFiscal; |
| 170 | espaco | 64 | private Date dataHoraEmissao; |
| 65 | |||
| 106 | espaco | 66 | private String chave; |
| 170 | espaco | 67 | |
| 68 | private String tipoNotaFiscalNaturezaOperacao; // NOVO |
||
| 69 | private String tipoNotaFiscalPresencaComprador; //NOVO |
||
| 70 | private Boolean indicadorOperacaoConsumidorFinal; |
||
| 71 | |||
| 106 | espaco | 72 | private Venda venda; |
| 170 | espaco | 73 | private Pessoa pessoaEmitente; |
| 74 | private Pessoa pessoaDestinatario; |
||
| 106 | espaco | 75 | private String observacaoVenda; |
| 76 | // private Transporte transporte; |
||
| 77 | private String descricaoComplementares; |
||
| 170 | espaco | 78 | |
| 106 | espaco | 79 | private String textoXml; |
| 80 | private String tipoNotaFiscal; |
||
| 170 | espaco | 81 | |
| 106 | espaco | 82 | private String protocoloAutorizacao; |
| 83 | private String caminhoQrcode; |
||
| 84 | |||
| 85 | private String statusRetorno; |
||
| 86 | private String motivoRetorno; |
||
| 87 | private Date dataHoraEvento; |
||
| 88 | private String textoXmlEvento; |
||
| 89 | |||
| 90 | private List<NotaFiscalProduto> listaNFRemessaProdutos; |
||
| 91 | |||
| 92 | private byte[] arquivoXml; |
||
| 93 | |||
| 94 | private List<Parcela> listaParcelas; |
||
| 95 | private List<CompraProduto> listaCompraProduto; |
||
| 96 | private List<Lancamento> listaLancamento; |
||
| 97 | private List<VendaFormaPagamento> listaFormaPagamento; |
||
| 98 | |||
| 99 | private Boolean notaFiscalPendente; |
||
| 100 | |||
| 101 | @Override |
||
| 102 | @Transient |
||
| 103 | public Object getId() { |
||
| 104 | return this.getSequencial(); |
||
| 105 | } |
||
| 106 | @Override |
||
| 107 | public void setId(Object id) { |
||
| 108 | this.sequencial = (Long) id; |
||
| 109 | } |
||
| 110 | |||
| 111 | @Id |
||
| 112 | @SequenceGenerator(name = "sq_notafiscal") |
||
| 113 | @GeneratedValue(strategy = GenerationType.IDENTITY) |
||
| 114 | @Column(name="seq_nota_fiscal", nullable=false) |
||
| 115 | public Long getSequencial() { |
||
| 116 | return sequencial; |
||
| 117 | } |
||
| 118 | public void setSequencial(Long sequencial) { |
||
| 119 | this.sequencial = sequencial; |
||
| 120 | } |
||
| 121 | |||
| 122 | @Column(name="dsc_serie") |
||
| 123 | public String getSerie() { |
||
| 124 | return serie; |
||
| 125 | } |
||
| 126 | public void setSerie(String serie) { |
||
| 127 | this.serie = serie; |
||
| 128 | } |
||
| 129 | |||
| 130 | @Column(name="num_nota_fiscal", nullable=false) |
||
| 131 | public Long getNumeroNotaFiscal() { |
||
| 132 | return numeroNotaFiscal; |
||
| 133 | } |
||
| 134 | public void setNumeroNotaFiscal(Long numeroNotaFiscal) { |
||
| 135 | this.numeroNotaFiscal = numeroNotaFiscal; |
||
| 136 | } |
||
| 137 | |||
| 170 | espaco | 138 | @Column(name="cod_nota_fiscal") |
| 139 | public String getChave() { |
||
| 140 | return chave; |
||
| 141 | } |
||
| 142 | public void setChave(String chave) { |
||
| 143 | this.chave = chave; |
||
| 144 | } |
||
| 145 | |||
| 146 | @Transient |
||
| 147 | public String getTipoNotaFiscalNaturezaOperacao() { |
||
| 148 | return tipoNotaFiscalNaturezaOperacao; |
||
| 149 | } |
||
| 150 | public void setTipoNotaFiscalNaturezaOperacao(String tipoNotaFiscalNaturezaOperacao) { |
||
| 151 | this.tipoNotaFiscalNaturezaOperacao = tipoNotaFiscalNaturezaOperacao; |
||
| 152 | } |
||
| 153 | @Transient |
||
| 154 | public String descricaoTipoNotaFiscalNaturezaOperacao() { |
||
| 155 | return VerificadorUtil.naoEstaNuloOuVazio(getTipoNotaFiscalNaturezaOperacao())? TipoNotaFiscalNaturezaOperacao.parse(getTipoNotaFiscalNaturezaOperacao()).getDescricao() : null; |
||
| 156 | } |
||
| 157 | |||
| 158 | @Transient |
||
| 159 | public String getTipoNotaFiscalPresencaComprador() { |
||
| 160 | return tipoNotaFiscalPresencaComprador; |
||
| 161 | } |
||
| 162 | public void setTipoNotaFiscalPresencaComprador(String tipoNotaFiscalPresencaComprador) { |
||
| 163 | this.tipoNotaFiscalPresencaComprador = tipoNotaFiscalPresencaComprador; |
||
| 164 | } |
||
| 165 | @Transient |
||
| 166 | public String descricaoTipoNotaFiscalPresencaComprador() { |
||
| 167 | return VerificadorUtil.naoEstaNuloOuVazio(getTipoNotaFiscalPresencaComprador())? TipoNotaFiscalPresencaComprador.parse(getTipoNotaFiscalPresencaComprador()).getDescricao() : null; |
||
| 168 | } |
||
| 169 | |||
| 106 | espaco | 170 | @ManyToOne |
| 171 | @ForeignKey(name = "fk_notafiscal_venda") |
||
| 172 | @JoinColumn(name="seq_venda", referencedColumnName="seq_venda", insertable=true, updatable=false) |
||
| 173 | public Venda getVenda() { |
||
| 174 | return venda; |
||
| 175 | } |
||
| 176 | public void setVenda(Venda venda) { |
||
| 177 | this.venda = venda; |
||
| 178 | } |
||
| 179 | |||
| 180 | @ManyToOne |
||
| 181 | @ForeignKey(name = "fk_notafiscal_emitente") |
||
| 182 | @JoinColumn(name="seq_pessoa_emitente", referencedColumnName="seq_pessoa", insertable=true, updatable=true) |
||
| 170 | espaco | 183 | public Pessoa getPessoaEmitente() { |
| 184 | return pessoaEmitente; |
||
| 106 | espaco | 185 | } |
| 170 | espaco | 186 | public void setPessoaEmitente(Pessoa pessoaEmitente) { |
| 187 | this.pessoaEmitente = pessoaEmitente; |
||
| 106 | espaco | 188 | } |
| 189 | |||
| 190 | @ManyToOne |
||
| 191 | @ForeignKey(name = "fk_notafiscal_destinatario") |
||
| 192 | @JoinColumn(name="seq_pessoa_destinatario", referencedColumnName="seq_pessoa", insertable=true, updatable=true) |
||
| 170 | espaco | 193 | public Pessoa getPessoaDestinatario() { |
| 194 | return pessoaDestinatario; |
||
| 106 | espaco | 195 | } |
| 170 | espaco | 196 | public void setPessoaDestinatario(Pessoa pessoaDestinatario) { |
| 197 | this.pessoaDestinatario = pessoaDestinatario; |
||
| 106 | espaco | 198 | } |
| 199 | |||
| 200 | @Column(name="dsc_obs_venda") |
||
| 201 | @Size(max = 100, message = "Limite de caracteres ultrapassado: Observação da Venda") |
||
| 202 | public String getObservacaoVenda() { |
||
| 203 | return observacaoVenda; |
||
| 204 | } |
||
| 205 | public void setObservacaoVenda(String observacaoVenda) { |
||
| 206 | this.observacaoVenda = observacaoVenda; |
||
| 207 | } |
||
| 208 | |||
| 209 | @Column(name="dsc_complementares") |
||
| 210 | @Size(max = 1500, message = "Limite de caracteres ultrapassado: Descrição Complementares") |
||
| 211 | public String getDescricaoComplementares() { |
||
| 212 | return descricaoComplementares; |
||
| 213 | } |
||
| 214 | public void setDescricaoComplementares(String descricaoComplementares) { |
||
| 215 | this.descricaoComplementares = descricaoComplementares; |
||
| 216 | } |
||
| 217 | |||
| 218 | @Column(name="dth_emissao", nullable=false) |
||
| 219 | public Date getDataHoraEmissao() { |
||
| 220 | return dataHoraEmissao; |
||
| 221 | } |
||
| 222 | public void setDataHoraEmissao(Date dataHoraEmissao) { |
||
| 223 | this.dataHoraEmissao = dataHoraEmissao; |
||
| 224 | } |
||
| 225 | |||
| 226 | @Column(name="txt_xml") |
||
| 227 | public String getTextoXml() { |
||
| 228 | return textoXml; |
||
| 229 | } |
||
| 230 | public void setTextoXml(String textoXml) { |
||
| 231 | this.textoXml = textoXml; |
||
| 232 | } |
||
| 233 | |||
| 234 | @Column(name="tip_nota_fiscal", nullable=false) |
||
| 235 | @NotNull(message="Obrigatório informar o tipo da nota fiscal", groups={Cadastrar.class, Alterar.class}) |
||
| 236 | public String getTipoNotaFiscal() { |
||
| 237 | return tipoNotaFiscal; |
||
| 238 | } |
||
| 239 | public void setTipoNotaFiscal(String tipoNotaFiscal) { |
||
| 240 | this.tipoNotaFiscal = tipoNotaFiscal; |
||
| 241 | } |
||
| 242 | @Transient |
||
| 243 | public String getDescricaoDoTipoNotaFiscal() { |
||
| 244 | return VerificadorUtil.naoEstaNuloOuVazio(getTipoNotaFiscal())? TipoNotaFiscal.parse(getTipoNotaFiscal()).getDescricao() : null; |
||
| 245 | } |
||
| 246 | |||
| 247 | @Column(name="tip_modelo_notafiscal", nullable=false) |
||
| 248 | @NotNull(message="Obrigatório informar o tipo do modelo da nota fiscal", groups={Cadastrar.class, Alterar.class}) |
||
| 249 | public String getTipoModeloNotaFiscal() { |
||
| 250 | return tipoModeloNotaFiscal; |
||
| 251 | } |
||
| 252 | public void setTipoModeloNotaFiscal(String tipoModeloNotaFiscal) { |
||
| 253 | this.tipoModeloNotaFiscal = tipoModeloNotaFiscal; |
||
| 254 | } |
||
| 255 | @Transient |
||
| 256 | public String getDescricaoDoTipoModeloNotaFiscal() { |
||
| 257 | return VerificadorUtil.naoEstaNuloOuVazio(getTipoModeloNotaFiscal())? TipoModeloNotaFiscal.parse(getTipoModeloNotaFiscal()).getDescricao() : null; |
||
| 258 | } |
||
| 259 | |||
| 260 | @Column(name="dsc_protocolo_autorizacao") |
||
| 261 | @Size(max = 100, message = "Limite de caracteres ultrapassado: Protocolo da Autorização") |
||
| 262 | public String getProtocoloAutorizacao() { |
||
| 263 | return protocoloAutorizacao; |
||
| 264 | } |
||
| 265 | public void setProtocoloAutorizacao(String protocoloAutorizacao) { |
||
| 266 | this.protocoloAutorizacao = protocoloAutorizacao; |
||
| 267 | } |
||
| 268 | |||
| 269 | @Column(name="dsc_caminho_qrcode") |
||
| 270 | @Size(max = 500, message = "Limite de caracteres ultrapassado: Caminho QRCode") |
||
| 271 | public String getCaminhoQrcode() { |
||
| 272 | return caminhoQrcode; |
||
| 273 | } |
||
| 274 | public void setCaminhoQrcode(String caminhoQrcode) { |
||
| 275 | this.caminhoQrcode = caminhoQrcode; |
||
| 276 | } |
||
| 277 | |||
| 278 | @Column(name="dsc_status_retorno") |
||
| 279 | @Size(max = 100, message = "Limite de caracteres ultrapassado: Status do Retorno") |
||
| 280 | public String getStatusRetorno() { |
||
| 281 | return statusRetorno; |
||
| 282 | } |
||
| 283 | public void setStatusRetorno(String statusRetorno) { |
||
| 284 | this.statusRetorno = statusRetorno; |
||
| 285 | } |
||
| 170 | espaco | 286 | @Transient |
| 287 | public String resultadoStatusRetorno() { |
||
| 288 | TipoNotaFiscalStatusRetorno tipoStatus = TipoNotaFiscalStatusRetorno.parse(new Long(getStatusRetorno())); |
||
| 289 | return VerificadorUtil.naoEstaNulo(tipoStatus)? tipoStatus.getDescricao() : getStatusRetorno(); |
||
| 290 | } |
||
| 106 | espaco | 291 | |
| 292 | @Column(name="dsc_motivo_retorno") |
||
| 293 | @Size(max = 100, message = "Limite de caracteres ultrapassado: Motivo do Retorno") |
||
| 294 | public String getMotivoRetorno() { |
||
| 295 | return motivoRetorno; |
||
| 296 | } |
||
| 297 | public void setMotivoRetorno(String motivoRetorno) { |
||
| 298 | this.motivoRetorno = motivoRetorno; |
||
| 299 | } |
||
| 300 | |||
| 301 | @Column(name="dth_evento") |
||
| 302 | public Date getDataHoraEvento() { |
||
| 303 | return dataHoraEvento; |
||
| 304 | } |
||
| 305 | public void setDataHoraEvento(Date dataHoraEvento) { |
||
| 306 | this.dataHoraEvento = dataHoraEvento; |
||
| 307 | } |
||
| 308 | |||
| 309 | @Column(name="txt_xml_evento", nullable=false) |
||
| 310 | public String getTextoXmlEvento() { |
||
| 311 | return textoXmlEvento; |
||
| 312 | } |
||
| 313 | public void setTextoXmlEvento(String textoXmlEvento) { |
||
| 314 | this.textoXmlEvento = textoXmlEvento; |
||
| 315 | } |
||
| 316 | |||
| 317 | // @OneToMany(mappedBy="notaFiscal", cascade=CascadeType.ALL, orphanRemoval=true) |
||
| 318 | @Transient |
||
| 319 | public List<NotaFiscalProduto> getListaNFRemessaProdutos() { |
||
| 320 | return listaNFRemessaProdutos; |
||
| 321 | } |
||
| 322 | public void setListaNFRemessaProdutos(List<NotaFiscalProduto> listaNFRemessaProdutos) { |
||
| 323 | this.listaNFRemessaProdutos = listaNFRemessaProdutos; |
||
| 324 | } |
||
| 325 | |||
| 326 | @Transient |
||
| 327 | public Boolean temPessoaJuridica() { |
||
| 328 | if (VerificadorUtil.estaNulo(getVenda())) return false; |
||
| 329 | if (VerificadorUtil.estaNulo(getVenda().getLoja())) return false; |
||
| 330 | return VerificadorUtil.naoEstaNulo(getVenda().getLoja().getPessoaJuridica()); |
||
| 331 | } |
||
| 332 | |||
| 333 | @Transient |
||
| 334 | public String getRazaoSocialDaLoja() { |
||
| 335 | return temPessoaJuridica()? getVenda().getLoja().getPessoaJuridica().getRazaoSocial() : ""; |
||
| 336 | } |
||
| 337 | |||
| 338 | @Transient |
||
| 339 | public String getCnpjDaLoja() { |
||
| 340 | return temPessoaJuridica()? StringUtil.formatarCnpj(getVenda().getLoja().getPessoaJuridica().getCpfCnpj()) : ""; |
||
| 341 | } |
||
| 342 | |||
| 343 | @Transient |
||
| 344 | public byte[] getArquivoXml() { |
||
| 345 | return arquivoXml; |
||
| 346 | } |
||
| 347 | public void setArquivoXml(byte[] arquivoXml) { |
||
| 348 | this.arquivoXml = arquivoXml; |
||
| 349 | } |
||
| 350 | |||
| 351 | @Transient |
||
| 352 | public List<Parcela> getListaParcelas() { |
||
| 353 | return listaParcelas; |
||
| 354 | } |
||
| 355 | public void setListaParcelas(List<Parcela> listaParcelas) { |
||
| 356 | this.listaParcelas = listaParcelas; |
||
| 357 | } |
||
| 358 | |||
| 359 | @Transient |
||
| 360 | public List<CompraProduto> getListaCompraProduto() { |
||
| 361 | return listaCompraProduto; |
||
| 362 | } |
||
| 363 | public void setListaCompraProduto(List<CompraProduto> listaCompraProduto) { |
||
| 364 | this.listaCompraProduto = listaCompraProduto; |
||
| 365 | } |
||
| 366 | |||
| 367 | @Transient |
||
| 368 | public List<Lancamento> getListaLancamento() { |
||
| 369 | return listaLancamento; |
||
| 370 | } |
||
| 371 | public void setListaLancamento(List<Lancamento> listaLancamento) { |
||
| 372 | this.listaLancamento = listaLancamento; |
||
| 373 | } |
||
| 374 | |||
| 375 | @Transient |
||
| 376 | public List<VendaFormaPagamento> getListaFormaPagamento() { |
||
| 377 | return listaFormaPagamento; |
||
| 378 | } |
||
| 379 | public void setListaFormaPagamento(List<VendaFormaPagamento> listaFormaPagamento) { |
||
| 380 | this.listaFormaPagamento = listaFormaPagamento; |
||
| 381 | } |
||
| 382 | |||
| 383 | @Transient |
||
| 384 | public Boolean getNotaFiscalPendente() { |
||
| 385 | return notaFiscalPendente; |
||
| 386 | } |
||
| 387 | public void setNotaFiscalPendente(Boolean notaFiscalPendente) { |
||
| 388 | this.notaFiscalPendente = notaFiscalPendente; |
||
| 389 | } |
||
| 390 | |||
| 391 | @Override |
||
| 392 | public int hashCode() { |
||
| 393 | final int prime = 31; |
||
| 394 | int result = 1; |
||
| 395 | result = prime * result + ((chave == null) ? 0 : chave.hashCode()); |
||
| 396 | return result; |
||
| 397 | } |
||
| 398 | |||
| 399 | @Override |
||
| 400 | public boolean equals(Object obj) { |
||
| 401 | if (this == obj) |
||
| 402 | return true; |
||
| 403 | if (obj == null) |
||
| 404 | return false; |
||
| 405 | if (getClass() != obj.getClass()) |
||
| 406 | return false; |
||
| 407 | NotaFiscal other = (NotaFiscal) obj; |
||
| 408 | if (chave == null) { |
||
| 409 | if (other.chave != null) |
||
| 410 | return false; |
||
| 411 | } else if (!chave.equals(other.chave)) |
||
| 412 | return false; |
||
| 413 | return true; |
||
| 414 | } |
||
| 415 | |||
| 416 | @Transient |
||
| 417 | public Double getTotalLancamentos() { |
||
| 418 | Double total = 0.0; |
||
| 419 | if (!VerificadorUtil.isListaNulaOuVazia(getListaLancamento())) { |
||
| 420 | for (Lancamento lancamento : getListaLancamento()) { |
||
| 421 | total = total + lancamento.getValorVenda(); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | return total; |
||
| 425 | } |
||
| 426 | |||
| 427 | @Transient |
||
| 428 | public Double getTotalPago() { |
||
| 429 | Double total = 0.0; |
||
| 430 | if (!VerificadorUtil.isListaNulaOuVazia(getListaFormaPagamento())) { |
||
| 431 | for (VendaFormaPagamento vendaFormaPagamento : getListaFormaPagamento()) { |
||
| 432 | total = total + vendaFormaPagamento.getValorPagamento(); |
||
| 433 | } |
||
| 434 | } |
||
| 435 | return total; |
||
| 436 | } |
||
| 437 | |||
| 438 | @Transient |
||
| 439 | public Double getValorNotaFiscal() { |
||
| 440 | if (VerificadorUtil.naoEstaNuloOuVazio(getTextoXml())) { |
||
| 441 | if (getTextoXml().indexOf("<vNF>") > 0) { |
||
| 442 | String valorLocalizado = getTextoXml().substring(getTextoXml().indexOf("<vNF>"), getTextoXml().indexOf("</vNF>")) |
||
| 443 | .replace("<vNF>", "").replace("</vNF>", ""); |
||
| 444 | return new Double(valorLocalizado); |
||
| 445 | } |
||
| 446 | } |
||
| 447 | return 0.0; |
||
| 448 | } |
||
| 449 | |||
| 450 | @Transient |
||
| 451 | public Double getTotalValorProdutosRemessa() { |
||
| 452 | Double total = new Double(0.0); |
||
| 453 | if (VerificadorUtil.naoEstaNuloOuVazio(getListaNFRemessaProdutos())) { |
||
| 454 | for (NotaFiscalProduto notaFiscalProduto : getListaNFRemessaProdutos()) { |
||
| 455 | total = total + notaFiscalProduto.getProduto().getValorCompra() * notaFiscalProduto.getQuantidade(); |
||
| 456 | } |
||
| 457 | } |
||
| 458 | return total; |
||
| 459 | } |
||
| 460 | |||
| 461 | @Transient |
||
| 462 | public Boolean ehNotaFiscalDeRemessa() { |
||
| 463 | return VerificadorUtil.naoEstaNulo(getTipoNotaFiscal()) ? getTipoNotaFiscal().equals(TipoNotaFiscal.NFE_REMESSA.getValor()) : false; |
||
| 464 | } |
||
| 465 | |||
| 466 | @Transient |
||
| 467 | public byte[] criarArquivoXml() { |
||
| 468 | ByteArrayInputStream in = null; |
||
| 469 | ByteArrayOutputStream bos = null; |
||
| 470 | try { |
||
| 471 | in = new ByteArrayInputStream(getTextoXml().getBytes()); |
||
| 472 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
||
| 473 | DocumentBuilder builder = dbf.newDocumentBuilder(); |
||
| 474 | Document xml = builder.parse(in); |
||
| 475 | |||
| 476 | bos = new ByteArrayOutputStream(); |
||
| 477 | TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
||
| 478 | Transformer transformer = transformerFactory.newTransformer(); |
||
| 479 | DOMSource source = new DOMSource(xml); |
||
| 480 | StreamResult result = new StreamResult(bos); |
||
| 481 | transformer.transform(source, result); |
||
| 482 | |||
| 483 | DefaultStreamedContent arquivo = (DefaultStreamedContent) RelatorioUtils.gerarArquivo(bos.toByteArray(), "ArquivoXML", "xml"); |
||
| 484 | return IOUtils.toByteArray(arquivo.getStream()); |
||
| 485 | } catch (Exception e) { |
||
| 486 | e.printStackTrace(); |
||
| 487 | } finally { |
||
| 488 | try { |
||
| 489 | in.close(); |
||
| 490 | bos.close(); |
||
| 491 | } catch (IOException e) { |
||
| 492 | e.printStackTrace(); |
||
| 493 | } |
||
| 494 | } |
||
| 495 | return null; |
||
| 496 | } |
||
| 497 | |||
| 498 | @Transient |
||
| 499 | public byte[] getRetornarArquivoXml() { |
||
| 500 | setArquivoXml(criarArquivoXml()); |
||
| 501 | return getArquivoXml(); |
||
| 502 | } |
||
| 503 | |||
| 504 | @Transient |
||
| 505 | public void gerarChaveAcessoNfe(String codigoUFEmitente, String dataAAMM, String cnpjCpfEmitente, String modeloNf, |
||
| 506 | String serieNF, String tpEmissao, String numeroNF) { |
||
| 507 | try { |
||
| 508 | StringBuilder chave = new StringBuilder(); |
||
| 509 | chave.append(StringUtil.lpadTo(codigoUFEmitente, 2, '0')); |
||
| 510 | chave.append(StringUtil.lpadTo(dataAAMM, 4, '0')); |
||
| 511 | chave.append(StringUtil.lpadTo(cnpjCpfEmitente.replaceAll("\\D",""), 14, '0')); |
||
| 512 | chave.append(StringUtil.lpadTo(modeloNf, 2, '0')); |
||
| 513 | chave.append(StringUtil.lpadTo(serieNF, 3, '0')); |
||
| 514 | chave.append(StringUtil.lpadTo(String.valueOf(numeroNF), 9, '0')); |
||
| 515 | chave.append(StringUtil.lpadTo(tpEmissao, 1, '0')); |
||
| 516 | chave.append(StringUtil.lpadTo(numeroNF, 8, '0')); |
||
| 517 | chave.append(StringUtil.modulo11(chave.toString())); |
||
| 518 | chave.insert(0, "NFe"); |
||
| 519 | setChave(chave.toString()); |
||
| 520 | } catch (Exception e) { |
||
| 521 | System.out.println(e.toString()); |
||
| 522 | throw new NegocioException(e.getMessage()); |
||
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | @Transient |
||
| 527 | public Long getSequencialDaVenda() { |
||
| 528 | return VerificadorUtil.naoEstaNulo(getVenda())? getVenda().getSequencial() : null; |
||
| 529 | } |
||
| 530 | |||
| 531 | @Transient |
||
| 532 | public void preencherNumeroPelaChave() { |
||
| 533 | if (VerificadorUtil.naoEstaNuloOuVazio(getChave())) { |
||
| 534 | setNumeroNotaFiscal(new Long(getChave().substring(25, 34))); |
||
| 535 | } |
||
| 536 | } |
||
| 537 | |||
| 538 | @Transient |
||
| 539 | public void preencherMotivoPadrao() { |
||
| 540 | setMotivoRetorno("Autorizado o uso da NF-e"); |
||
| 541 | } |
||
| 542 | |||
| 543 | @Transient |
||
| 544 | public void preencherSeriePadrao() { |
||
| 545 | setSerie("1"); |
||
| 546 | } |
||
| 547 | |||
| 548 | @Transient |
||
| 549 | public void preencherStatusPadrao() { |
||
| 550 | setStatusRetorno("100"); |
||
| 551 | } |
||
| 552 | |||
| 553 | @Transient |
||
| 554 | public void preencherQRCodePadrao() { |
||
| 555 | setCaminhoQrcode("http://nfce.sefaz.al.gov.br/QRCode/consultarNFCe.jsp?p=" + getChave() + "|2|1|1|437A52AD9DE9BDD04D6BFF55ED997C5199ECC64D"); |
||
| 556 | } |
||
| 557 | |||
| 558 | @Transient |
||
| 559 | public String retornarCodigoNumerico() { |
||
| 560 | if (VerificadorUtil.naoEstaNuloOuVazio(getChave())) { |
||
| 561 | if (getChave().length() > 42) { |
||
| 562 | return getChave().substring(35, 43); |
||
| 563 | } |
||
| 564 | } |
||
| 565 | return ""; |
||
| 566 | } |
||
| 567 | |||
| 568 | @Transient |
||
| 569 | public void preencherComXML() { |
||
| 570 | if (VerificadorUtil.naoEstaNuloOuVazio(this.getTextoXml())) { |
||
| 571 | TNfeProc tNfeProc = unmarshal(this.getTextoXml()); |
||
| 572 | preencher(tNfeProc); |
||
| 573 | } |
||
| 574 | |||
| 575 | } |
||
| 576 | |||
| 577 | @Transient |
||
| 578 | public void preencher(TNfeProc nfeProc) { |
||
| 579 | if (VerificadorUtil.naoEstaNulo(nfeProc)) { |
||
| 580 | if (VerificadorUtil.naoEstaNulo(nfeProc.getNFe())) { |
||
| 581 | if (VerificadorUtil.naoEstaNulo(nfeProc.getNFe().getInfNFe())) { |
||
| 582 | if (VerificadorUtil.naoEstaNulo(nfeProc.getNFe().getInfNFe().getIde())) { |
||
| 583 | //this.setCodigoNotaFiscal(new Long(nfeProc.getNFe().getInfNFe().getIde().getCNF())); |
||
| 584 | this.setNumeroNotaFiscal(new Long(nfeProc.getNFe().getInfNFe().getIde().getNNF())); |
||
| 585 | this.setSerie(nfeProc.getNFe().getInfNFe().getIde().getSerie()); |
||
| 586 | this.setDataHoraEmissao(DataUtil.retornarDataApartirString("yyyy-MM-dd'T'HH:mm:ss", nfeProc.getNFe().getInfNFe().getIde().getDhEmi())); |
||
| 587 | } |
||
| 588 | if (VerificadorUtil.naoEstaNulo(nfeProc.getNFe().getInfNFe().getInfAdic())) { |
||
| 589 | this.setDescricaoComplementares(nfeProc.getNFe().getInfNFe().getInfAdic().getInfCpl()); |
||
| 590 | } |
||
| 591 | } |
||
| 592 | if (VerificadorUtil.naoEstaNulo(nfeProc.getNFe().getInfNFeSupl())) {} |
||
| 593 | if (VerificadorUtil.naoEstaNulo(nfeProc.getNFe().getSignature())) {} |
||
| 594 | } |
||
| 595 | if (VerificadorUtil.naoEstaNulo(nfeProc.getProtNFe())) { |
||
| 596 | if (VerificadorUtil.naoEstaNulo(nfeProc.getProtNFe().getInfProt())) { |
||
| 597 | this.setChave(nfeProc.getProtNFe().getInfProt().getChNFe()); |
||
| 598 | this.setDataHoraEvento(DataUtil.retornarDataApartirString("yyyy-MM-dd'T'HH:mm:ss", nfeProc.getProtNFe().getInfProt().getDhRecbto())); |
||
| 599 | this.setProtocoloAutorizacao(nfeProc.getProtNFe().getInfProt().getNProt()); |
||
| 600 | this.setStatusRetorno(nfeProc.getProtNFe().getInfProt().getCStat()); |
||
| 601 | this.setMotivoRetorno(nfeProc.getProtNFe().getInfProt().getXMotivo()); |
||
| 602 | } |
||
| 603 | } |
||
| 604 | } |
||
| 605 | } |
||
| 606 | |||
| 607 | @Transient |
||
| 608 | public TNfeProc unmarshal(String stringXml) { |
||
| 609 | JAXBContext context = null; |
||
| 610 | try { |
||
| 611 | context = JAXBContext.newInstance(nfce.java.TNfeProc.class); |
||
| 612 | Unmarshaller unmarshaller = context.createUnmarshaller(); |
||
| 613 | return (TNfeProc) unmarshaller.unmarshal(new StreamSource(new StringReader(stringXml))); |
||
| 614 | } catch (JAXBException e) { |
||
| 615 | e.printStackTrace(); |
||
| 616 | } |
||
| 617 | return null; |
||
| 618 | } |
||
| 619 | |||
| 620 | } |