SefazNfe.Proc (sefaz_nfe v0.5.0)

Copy Markdown View Source

Assembles the processed documents that are archived and delivered: the signed document plus SEFAZ's answer (NFePHP's Complements).

FunctionResultParts
nfe_proc/2nfeProcNFe + protNFe
event_proc/2procEventoNFeevento + retEvento
void_proc/2procInutNFeinutNFe + retInutNFe
cancel_register/2the same nfeProc+ the cancellation's retEvento

Bytes are spliced, never re-serialised (AD-011)

The signature covers the document exactly as it was sent, and the protocol is SEFAZ's own. Both are copied byte for byte out of what the caller passed (the signed document, the SOAP body of the answer) and wrapped; nothing is parsed and written back. The only thing removed is the XML declaration of the inner document, which a wrapper cannot carry twice. The wrapper declares the NF-e namespace, which is what lets the answer's elements, that inherit it from the SOAP body they were cut from, stay in it.

The answer may be the whole SOAP envelope, the retEnviNFe / retEnvEvento / retInutNFe or the element itself. Its elements are found unprefixed, the way SEFAZ sends them.

What is checked

Like NFePHP, an answer is only joined to the document it is about, and only when SEFAZ accepted it:

  • nfe_proc/2: the protNFe whose chNFe is the document's key (Id without NFe) and, when both carry one, whose digVal is the document's DigestValue; cStat 100, 150, 120, 110, 205, 301, 302 or 303 (the authorized and the denied ones: a denial is archived too).
  • event_proc/2: the retEvento of the same chNFe and tpEvento; cStat 135 or 136, and 155 for a cancellation (110111).
  • void_proc/2: cStat 102 and the same versao, tpAmb, cUF, ano, CNPJ/CPF, mod, serie, nNFIni and nNFFin as the request.
  • cancel_register/2: cStat 135, 136 or 155, tpEvento 110111 or 110112 (cancelamento por substituição) and the chNFe of the nfeProc.

Each returns {:ok, xml} or {:error, reason}:

  • :no_protocol, :no_event_return, :no_void_return - the answer has no such element (a receipt, a rejection of the batch);
  • :key_mismatch, :digest_mismatch, {:mismatch, field} - it is about another document;
  • {:not_accepted, c_stat} - SEFAZ did not accept it;
  • {:not_a_cancellation, tp_evento} - cancel_register/2 of another event;
  • {:xml, reason} - a part is not XML (SefazNfe.XML).

SefazNfe.Result.xml is what these return, for the authorization, events and voiding of the facade.

Summary

Functions

Marks an authorized nfeProc as cancelled by appending the cancellation's retEvento after its protNFe (NFePHP's cancelRegister).

procEventoNFe: the signed evento and the retEvento SEFAZ answered it with, for a cancellation, a CCe, an EPEC or a manifestação.

Marks an mdfeProc closed (NFePHP closeRegister). See SefazNfe.MDFe.Proc.mdfe_close_register/2.

nfeProc: the signed NFe and its protNFe, from the answer of the authorization or of a protocol query.

procInutNFe: the signed inutNFe and SEFAZ's retInutNFe.

Types

reason()

@type reason() :: term()

Functions

cancel_register(nfe_proc_xml, event_xml)

@spec cancel_register(String.t(), String.t()) ::
  {:ok, String.t()} | {:error, reason()}

Marks an authorized nfeProc as cancelled by appending the cancellation's retEvento after its protNFe (NFePHP's cancelRegister).

event_xml is the procEventoNFe from event_proc/2 or SEFAZ's answer itself; the first retEvento that is an accepted cancellation (110111 or

  1. of the nfeProc's key is the one appended. It is spliced as received. Registering the same retEvento again returns the nfeProc unchanged.

Unlike NFePHP, which returns the nfeProc untouched for anything else, an answer that does not qualify is an error that says why: :no_protocol (the nfeProc carries no protNFe), :no_event_return, :key_mismatch, {:not_a_cancellation, tp_evento} or {:not_accepted, c_stat}.

event_proc(signed_event_xml, response)

@spec event_proc(String.t(), String.t()) :: {:ok, String.t()} | {:error, reason()}

procEventoNFe: the signed evento and the retEvento SEFAZ answered it with, for a cancellation, a CCe, an EPEC or a manifestação.

signed_event_xml is the signed evento (or an envEvento holding it).

mdfe_cancel_register(mdfe_proc_xml, event_xml)

@spec mdfe_cancel_register(String.t(), String.t()) ::
  {:ok, String.t()} | {:error, reason()}

Marks an mdfeProc cancelled. See SefazNfe.MDFe.Proc.mdfe_cancel_register/2.

mdfe_close_register(mdfe_proc_xml, event_xml)

@spec mdfe_close_register(String.t(), String.t()) ::
  {:ok, String.t()} | {:error, reason()}

Marks an mdfeProc closed (NFePHP closeRegister). See SefazNfe.MDFe.Proc.mdfe_close_register/2.

mdfe_event_proc(signed_event_xml, response)

@spec mdfe_event_proc(String.t(), String.t()) ::
  {:ok, String.t()} | {:error, reason()}

procEventoMDFe. See SefazNfe.MDFe.Proc.mdfe_event_proc/2.

mdfe_proc(signed_mdfe_xml, response)

@spec mdfe_proc(String.t(), String.t()) :: {:ok, String.t()} | {:error, reason()}

mdfeProc (MDF-e 58). See SefazNfe.MDFe.Proc.mdfe_proc/2.

nfe_proc(signed_nfe_xml, response)

@spec nfe_proc(String.t(), String.t()) :: {:ok, String.t()} | {:error, reason()}

nfeProc: the signed NFe and its protNFe, from the answer of the authorization or of a protocol query.

void_proc(signed_inut_xml, response)

@spec void_proc(String.t(), String.t()) :: {:ok, String.t()} | {:error, reason()}

procInutNFe: the signed inutNFe and SEFAZ's retInutNFe.