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

Builders for the CT-e (model 57 and 67) events, layout 4.00.

Like `SefazNfe.Events`, these carry no tax arithmetic: a key, a protocol and
a few facts. The shape differs from the NF-e's in four ways, all from
`eventoCTe_v4.00.xsd` (and confirmed against NFePHP `sped-cte`):

  * the root is `eventoCTe` (`versao` 4.00) in the CT-e namespace, sent alone:
    there is no `envEvento` and no `idLote`;
  * the key is `chCTe`, and `detEvento` carries `versaoEvento`, not `versao`;
  * `detEvento` holds one element per event (`evCancCTe`, `evCCeCTe`,
    `evCECTe`, `evCancCECTe`, `evIECTe`, `evCancIECTe`), where an NF-e event
    puts its fields in `detEvento` directly;
  * `nSeqEvento` and the last part of `Id` have three digits.

| Event | Code | Element |
|-------|------|---------|
| cancel | 110111 | `evCancCTe` |
| CC-e | 110110 | `evCCeCTe` |
| delivery proof | 110180 | `evCECTe` |
| cancel of the delivery proof | 110181 | `evCancCECTe` |
| delivery failure | 110190 | `evIECTe` |
| cancel of the delivery failure | 110191 | `evCancIECTe` |

## The delivery hash

`hashEntrega` and `hashTentativaEntrega` are, per the MOC and the XSD,
"Hash (SHA1) no formato Base64 resultante da concatenacao: Chave de acesso
do CT-e + Base64 da imagem capturada". `delivery_hash/2` is exactly that:
`Base.encode64(sha1(ch_cte <> Base.encode64(image)))`, where the
concatenation is of the two ASCII texts, not of the key and the raw image
bytes. The result is 28 characters (the XSD's 20-byte `base64Binary`). The MOC
adds that SEFAZ has no control over the field: it is the author's own index to
the proof it keeps. NFePHP only forwards a hash the caller computed.

A caller that keeps the image elsewhere may compute the hash itself and pass it
as `:hash` instead of `:image`: the 28 characters of the Base64 of the 20-byte
SHA-1. `valid_hash?/1` is the check the XSD makes (`base64Binary` of length 20).

# `cancel`

```elixir
@spec cancel(map()) :: binary()
```

Event 110111, cancelling an authorized CT-e.

# `cancel_delivery_failure`

```elixir
@spec cancel_delivery_failure(map()) :: binary()
```

Event 110191, cancelling the delivery failure whose protocol is `:n_prot_ie`.

# `cancel_delivery_proof`

```elixir
@spec cancel_delivery_proof(map()) :: binary()
```

Event 110181, cancelling the delivery proof whose protocol is `:n_prot_ce`.

# `correction`

```elixir
@spec correction(map()) :: binary()
```

Event 110110, the structured Carta de Correcao.

`opts.corrections` is a list of maps with `:grupo_alterado`, `:campo_alterado`
and `:valor_alterado`, and optionally `:nro_item_alterado` (the 1-based index
when the corrected field is in a list). `xCondUso` is the fixed legal text.

# `delivery_failure`

```elixir
@spec delivery_failure(map()) :: binary()
```

Event 110190, the insucesso na entrega.

`opts`: `:n_prot`, `:dh_tentativa_entrega`, `:tp_motivo` (1 recebedor nao
encontrado, 2 recusa do recebedor, 3 endereco inexistente, 4 outros) and
optionally `:n_tentativa` (three digits), `:x_just_motivo` (required with
motivo 4), `:latitude`, `:longitude`, `:image` (hashed here) or `:hash` (the
finished hash); with neither, the hash group is left out, as the XSD allows, `:dh_hash` and `:ch_nfes`.

# `delivery_hash`

```elixir
@spec delivery_hash(String.t(), binary()) :: String.t()
```

The delivery hash: `Base.encode64(sha1(ch_cte <> Base.encode64(image)))`. See
the moduledoc.

# `delivery_proof`

```elixir
@spec delivery_proof(map()) :: binary()
```

Event 110180, the comprovante de entrega.

`opts`: `:n_prot` (of the CT-e), `:dh_entrega`, `:n_doc`, `:x_nome`, `:image`
(the proof's bytes, hashed here, see the moduledoc; or `:hash`, the finished
hash, instead of it) and optionally
`:latitude`, `:longitude`, `:ch_nfes` (the NF-e keys delivered, for a normal
service) and `:dh_hash` (when the hash was generated; `dhEntrega` by default).

# `valid_hash?`

```elixir
@spec valid_hash?(term()) :: boolean()
```

Whether `hash` is what `hashEntrega` and `hashTentativaEntrega` accept: 28
characters of Base64 that decode to 20 bytes (a SHA-1).

---

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