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

The processed CT-e documents that are archived and delivered, assembled the
way `SefazNfe.Proc` assembles the NF-e's (NFePHP `Complements`), and under the
same rules (AD-011): the signed document and SEFAZ's answer are copied byte
for byte out of what the caller passed and wrapped, never parsed and written
back. The wrapper declares the CT-e namespace, which the answer's elements
inherit from the SOAP body they were cut from.

| Function | Result | Parts |
|----------|--------|-------|
| `cte_proc/2` | `cteProc` (`cteOSProc` for a `CTeOS`) | `CTe` + `protCTe` |
| `cte_event_proc/2` | `procEventoCTe` | `eventoCTe` + `retEventoCTe` |
| `cte_cancel_register/2` | the same `cteProc` | + the cancellation's `retEventoCTe` |

Each returns `{:ok, xml}` or `{:error, reason}`, with the reasons
`SefazNfe.Proc` documents (`:no_protocol`, `:no_event_return`,
`:key_mismatch`, `:digest_mismatch`, `{:not_accepted, c_stat}`,
`{:not_a_cancellation, tp_evento}`, `{:xml, reason}`), plus
`:not_a_cte_proc` for `cte_cancel_register/2` of something else.

## What is checked

  * `cte_proc/2`: the `protCTe` whose `chCTe` is the document's key (`Id` of
    `infCte` without `CTe`) and, when both carry one, whose `digVal` is the
    document's `DigestValue`; `cStat` 100, 150, 110, 205 or 301 (authorized
    and denied, NFePHP's list; 301 only for a CT-e, never a CT-e OS). A CT-e
    OS (`CTeOS`) becomes a `cteOSProc`.
  * `cte_event_proc/2`: the `retEventoCTe` of the same `chCTe` and `tpEvento`;
    `cStat` 134, 135 or 136, and 155 for a cancellation (110111). 134 is not
    in NFePHP's list: the MOC homologates the event under it too (registered
    with a warning about the CT-e's situation), so it is archived.
  * `cte_cancel_register/2`: `cStat` 135, 136 or 155, `tpEvento` 110111 and
    the `chCTe` of the `cteProc`.

# `reason`

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

# `cte_cancel_register`

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

Marks an authorized `cteProc` as cancelled by appending the cancellation's
`retEventoCTe` after its `protCTe` (NFePHP's `cancelRegister`).

`event_xml` is the `procEventoCTe` from `cte_event_proc/2` or SEFAZ's answer
itself; the first `retEventoCTe` that is an accepted cancellation (110111) of
the `cteProc`'s key is the one appended, spliced as received. Registering the
same `retEventoCTe` again returns the `cteProc` unchanged. Unlike NFePHP, an
answer that does not qualify is an error that says why.

# `cte_event_proc`

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

`procEventoCTe`: the signed `eventoCTe` and the `retEventoCTe` SEFAZ answered
it with.

# `cte_proc`

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

`cteProc` (or `cteOSProc`): the signed `CTe` and its `protCTe`, from the
answer of the authorization or of a consultation.

---

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