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