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

The processed MDF-e documents (NFePHP's `Complements` for sped-mdfe), reached
through `SefazNfe.Proc` as `mdfe_proc/2`, `mdfe_event_proc/2`,
`mdfe_cancel_register/2` and `mdfe_close_register/2`.

| Function | Result | Parts |
|----------|--------|-------|
| `mdfe_proc/2` | `mdfeProc` | `MDFe` + `protMDFe` |
| `mdfe_event_proc/2` | `procEventoMDFe` | `eventoMDFe` + `retEventoMDFe` |
| `mdfe_cancel_register/2` | the same `mdfeProc` | + the cancellation's `retEventoMDFe` |
| `mdfe_close_register/2` | the same `mdfeProc` | + the encerramento's `retEventoMDFe` |

Bytes are spliced, never re-serialised, exactly as in `SefazNfe.Proc` (AD-011):
the signed `MDFe` (with its `infMDFeSupl`) and SEFAZ's own `protMDFe` or
`retEventoMDFe` are copied verbatim and wrapped in the MDF-e namespace.

## What is checked

  * `mdfe_proc/2`: the `protMDFe` whose `chMDFe` is the document's key (`Id`
    without `MDFe`) and, when both carry one, whose `digVal` is the document's
    `DigestValue`; `cStat` 100 only (MDF-e has no denial).
  * `mdfe_event_proc/2`: the `retEventoMDFe` of the same `chMDFe` and
    `tpEvento`; `cStat` 135 or 136 (and 132 for an encerramento).
  * the registers: `cStat` 135, 136 or 155 (and 132 for an encerramento), the
    right `tpEvento` (110111, or 110112) and the `chMDFe` of the `mdfeProc`.

Errors are those of `SefazNfe.Proc` with MDF-e names: `:no_protocol`,
`:no_event`, `:no_event_return`, `:key_mismatch`, `:digest_mismatch`,
`:no_inf_mdfe`, `{:not_accepted, c_stat}`, `{:not_a_cancellation, tp_evento}`,
`{:not_a_closure, tp_evento}`, `:not_an_mdfe_proc` and `{:xml, reason}`.

# `reason`

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

# `mdfe_cancel_register`

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

Marks an authorized `mdfeProc` as cancelled by appending the cancellation's
`retEventoMDFe` after its `protMDFe` (NFePHP's `cancelRegister`).

`event_xml` is the `procEventoMDFe` from `mdfe_event_proc/2` or SEFAZ's answer
itself. Registering the same `retEventoMDFe` again returns the `mdfeProc`
unchanged. Unlike NFePHP, which returns the document untouched for anything
else, an answer that does not qualify is an error that says why.

# `mdfe_close_register`

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

Marks an authorized `mdfeProc` as closed (encerrado) by appending the
encerramento's `retEventoMDFe` (NFePHP's `closeRegister`). Same rules as
`mdfe_cancel_register/2` for event 110112, with `{:not_a_closure, tp_evento}`.

# `mdfe_event_proc`

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

`procEventoMDFe`: the signed `eventoMDFe` and the `retEventoMDFe` SEFAZ
answered it with.

# `mdfe_proc`

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

`mdfeProc`: the signed `MDFe` and its `protMDFe`, from the answer of the
authorization or of a consultation.

---

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