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

Parsed SEFAZ business outcome. A SOAP envelope that we understood is
`{:ok, t}` even when `c_stat != 100` — that is a rejection, not a crash.

## The XML fields

`:signed_xml` is what was actually sent to SEFAZ, always present after an
authorization attempt, an event or a voiding. `:xml` is the processed
document, built by `SefazNfe.Proc`, and is only set once SEFAZ accepted it:
the `nfeProc` (signed document plus its protocol) for an authorization, the
`procEventoNFe` for an event (cancellation, CCe, EPEC, manifestação) and the
`procInutNFe` for a voiding. `authorization_result/1` and `consult_protocol/1`
also set both when given the optional `:signed_xml` of the document. The `nfeProc` is the
document that has to be archived and delivered to the recipient, so losing
either of them loses something legally required: without the protocol there
is nothing to prove, and without the signed bytes the protocol cannot be
attached to anything later.

## `:response_xml`

The SOAP answer exactly as received, envelope included, set by `parse/1` for
every operation that returns a `Result`. It is for audit and for storing what
SEFAZ said byte for byte (a rejection has no `:xml`); it is never altered, and
it is not part of the JSON encoding. It is `nil` only for a `Result` that was
not parsed from an answer. `SefazNfe.DistDFe` and `SefazNfe.TaxpayerLookup`
are other structs and do not carry it. Telemetry never sees it (no XML in
metadata).

A batch answer wraps the real outcome one level down — `protNFe/infProt` for a
document, `retEvento/infEvento` for an event — so `parse/1` reads the nested
`cStat` when there is one and the envelope's otherwise.

## Status

`:status` comes from `SefazNfe.CStat.classify/1`, which holds the table of
codes per class. The ones a host must not confuse: `:authorized`, `:denied`
(uso denegado, the number is consumed and the document must be stored),
`:cancelled`, `:retryable` (try again later, never a fix to the document) and
`:rejected` (definitive).

## `:dh_recbto`

SEFAZ's own `dhRecbto` text, untouched (ISO 8601 with offset, lossless), read
from the same element as the `cStat` and from the envelope when that has none.
An event answer carries `dhRegEvento` instead, which is used when `dhRecbto`
is absent. `nil` when the answer has neither.

# `status`

```elixir
@type status() :: SefazNfe.CStat.status()
```

# `t`

```elixir
@type t() :: %SefazNfe.Result{
  c_stat: pos_integer(),
  ch_nfe: String.t() | nil,
  dh_recbto: String.t() | nil,
  n_prot: String.t() | nil,
  n_rec: String.t() | nil,
  response_xml: String.t() | nil,
  signed_xml: String.t() | nil,
  status: status(),
  x_motivo: String.t(),
  xml: String.t() | nil
}
```

# `parse`

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

Maps a SEFAZ SOAP body to `t:t/0`.

A body we could read is `{:ok, t}` even when `cStat` is a rejection: 204
(duplicate) is a business outcome, not a transport failure (SEFAZ-03). Only an
unreadable body or a SOAP fault is `{:error, _}`.

The raw `cStat` is always preserved and classified by `SefazNfe.CStat`; this
library does not ship a `cStat` dictionary it would have to chase.

# `proc`

```elixir
@spec proc(String.t(), String.t()) :: String.t() | nil
```

The `nfeProc` of a signed document and the SEFAZ response, or `nil`.

`SefazNfe.Proc.nfe_proc/2` with the reason dropped: `nil` is the normal case
for a receipt, a rejection, or an answer about another document. Kept for
compatibility; prefer `SefazNfe.Proc`.

---

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