# `SefazNfe.Proc`
[🔗](https://github.com/devaction-labs/sefaz_nfe/blob/v0.5.0/lib/sefaz_nfe/proc.ex#L1)

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

| Function | Result | Parts |
|----------|--------|-------|
| `nfe_proc/2` | `nfeProc` | `NFe` + `protNFe` |
| `event_proc/2` | `procEventoNFe` | `evento` + `retEvento` |
| `void_proc/2` | `procInutNFe` | `inutNFe` + `retInutNFe` |
| `cancel_register/2` | the 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.

# `reason`

```elixir
@type reason() :: term()
```

# `cancel_register`

```elixir
@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
110112) 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`

```elixir
@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`

```elixir
@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`

```elixir
@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`

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

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

# `mdfe_proc`

```elixir
@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`

```elixir
@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`

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

`procInutNFe`: the signed `inutNFe` and SEFAZ's `retInutNFe`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
