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

MDF-e (modelo 58) transport, layout 3.00.

SVRS authorizes MDF-e for every UF, so `:uf` selects nothing but the circuit
breaker and the telemetry tag. Every call goes through
`SefazNfe.SOAP.isolated_call/4`; every answer SEFAZ gave is `{:ok, result}`,
a rejection included, and only transport failures are `{:error, _}`.

| Function | Service | Event |
|----------|---------|-------|
| `authorize/1` | `MDFeRecepcaoSinc` | |
| `consult/1` | `MDFeConsulta` | |
| `service_status/1` | `MDFeStatusServico` | |
| `pending/1` | `MDFeConsNaoEnc` | |
| `cancel/1` | `MDFeRecepcaoEvento` | 110111 |
| `close/1` | `MDFeRecepcaoEvento` | 110112 |
| `include_driver/1` | `MDFeRecepcaoEvento` | 110114 |
| `include_dfe/1` | `MDFeRecepcaoEvento` | 110115 |

Events carry `:signed_xml` (the `eventoMDFe` sent) and, once SEFAZ registered
them (`cStat` 135 or 136), `:xml` holding the `procEventoMDFe`; see
`SefazNfe.MDFe.Proc` and `SefazNfe.Proc`. `Result.ch_nfe` holds the `chMDFe`:
the field is the access key of the document, whatever the model.

Contingency is the caller's (AD-005) and MDF-e has no SVC: it is the off-line
`tpEmis` 2, which `authorize/1` accepts and signs the QR code for.

# `authorize`

```elixir
@spec authorize(map()) :: {:ok, SefazNfe.Result.t()} | {:error, term()}
```

Signs and sends an MDF-e to `MDFeRecepcaoSinc`, which answers in the same call.

The document is `:xml` (the MDF-e, signed or not) or `:mdfe`, a map for
`SefazNfe.Builder.build_mdfe/2` (exactly one of them); a map that does not
build is `{:error, {:invalid_mdfe, errors}}`.

The pipeline is sign `infMDFe`, add `infMDFeSupl` with the QR code
(see `SefazNfe.MDFe.QRCode`; a document that has one keeps it), validate,
send `base64(gzip(MDFe))` and parse `retMDFe`. The validation step is
`SefazNfe.Schema` against the bundled `mdfe_v3.00.xsd`, on the signed
document (the XSD makes `Signature` mandatory, and `:xmerl_xsd` refuses the
signed one, so the bundled schemas are checked with `xmllint`). It is off
unless `config :sefaz_nfe, :schemas, ...` is set; a violation is
`{:error, {:schema, [line, ...]}}`, before the network.

`tpAmb` against `:environment` is checked before anything is sent, and `<mod>`
must be 58. `tpEmis` 1 is normal and 2 is off-line contingency (the QR code is
then signed with the certificate); anything else is
`{:error, {:unsupported, {:tp_emis, n}}}`.

The result carries `:signed_xml` (what was sent) and, on `cStat` 100, `:xml`
holding the `mdfeProc` from `SefazNfe.Proc.mdfe_proc/2`.

# `cancel`

```elixir
@spec cancel(map()) :: {:ok, SefazNfe.Result.t()} | {:error, term()}
```

Event 110111, cancelling an authorized MDF-e.

Needs `:ch_mdfe`, `:n_prot` (from the authorization), `:justification` (15 to
255 characters) and `:tax_id`, the author (the emitter's CNPJ or CPF).

# `close`

```elixir
@spec close(map()) :: {:ok, SefazNfe.Result.t()} | {:error, term()}
```

Event 110112, the encerramento: the MDF-e reached where it was going.

Needs `:ch_mdfe`, `:n_prot`, `:tax_id`, `:dt_enc` (a `Date` or `YYYY-MM-DD`),
and `:c_uf` and `:c_mun` (IBGE codes of the place of the encerramento).
`:ind_enc_por_terceiro` (`true`) marks an encerramento by a third party.

# `consult`

```elixir
@spec consult(map()) :: {:ok, SefazNfe.Result.t()} | {:error, term()}
```

`MDFeConsulta` by access key (`:ch_mdfe`).

The idempotent way back after a crash between a send and its answer: ask SEFAZ
what it did with the MDF-e instead of sending it again. `cStat` is the
document's situation: 100 `:authorized`, 101 `:cancelled`, 132 `:closed`.

# `include_dfe`

```elixir
@spec include_dfe(map()) :: {:ok, SefazNfe.Result.t()} | {:error, term()}
```

Event 110115, adding DF-e (NF-e) to an MDF-e in transit.

Needs `:ch_mdfe`, `:n_prot`, `:tax_id`, `:c_mun_carrega`, `:x_mun_carrega` and
`:documents`, a non-empty list of
`%{c_mun_descarga: ..., x_mun_descarga: ..., ch_nfe: ...}`.

The event carries NF-e only. In `evInclusaoDFeMDFe_v3.00.xsd` (PL_MDFe_300b,
NT 2025.001) each `infDoc` is `cMunDescarga`, `xMunDescarga` and `chNFe`: there
is no `chCTe`, so a CT-e cannot be added to a MDF-e in transit by this event.
A CT-e belongs in `infMunDescarga/infCTe` of the MDF-e itself, which the
builder supports. Do not put a CT-e key in `:ch_nfe`: the field is the NF-e's.

# `include_driver`

```elixir
@spec include_driver(map()) :: {:ok, SefazNfe.Result.t()} | {:error, term()}
```

Event 110114, adding a driver to an MDF-e in transit.

Needs `:ch_mdfe`, `:tax_id`, `:x_nome` (2 to 60 characters) and `:cpf`.
`:sequence` numbers the event (1 by default); each driver added to the same
MDF-e takes the next one. The layout has no protocol field here, so `:n_prot`
is accepted and not sent.

# `pending`

```elixir
@spec pending(map()) :: {:ok, SefazNfe.MDFe.Pending.t()} | {:error, term()}
```

`MDFeConsNaoEnc`: the MDF-e of `:tax_id` (CNPJ or CPF of the emitter) that were
authorized and not closed.

`{:ok, %SefazNfe.MDFe.Pending{}}` with `:documents` a list of
`%{ch_mdfe: key, n_prot: protocol}`: empty, one or many.

# `service_status`

```elixir
@spec service_status(map()) :: {:ok, SefazNfe.Result.t()} | {:error, term()}
```

`MDFeStatusServico`.

Options: `:cert`, `:uf` and `:environment`. `cStat` 107 is `:in_operation`;
108 and 109 are `:retryable`. See `SefazNfe.CStat`.

---

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