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