# `SefazDanfe.ContingencyCode`
[🔗](https://github.com/devaction-labs/sefaz_danfe/blob/v0.1.0/lib/sefaz_danfe/contingency_code.ex#L1)

The additional barcode of the documents printed in contingency on the security form (FS-DA, and
FS-IA of the NF-e): "Dados da NF-e" (MOC 7.0, Anexo II, item 2.9.2) and "Dados do CT-e" (MOC
3.00a of the DACTE, item 2.18.2). It is a Code 128 subset C of 36 digits, and the same 36 digits
in nine blocks of four go printed in the box next to it.

The layout, every field right aligned, zero padded and without formatting:

| Field    | Size | Content                                                                     |
| -------- | ---- | --------------------------------------------------------------------------- |
| `cUF`    | 2    | IBGE code of the UF of the recipient (or sender); `99` for a foreign one     |
| `tpEmis` | 1    | the emission type                                                           |
| `CNPJ`   | 14   | CNPJ of the recipient (or sender), its CPF zero padded, zeros when foreign   |
| `vNF`    | 14   | the total value in cents (`vNF` of the NF-e, `vTPrest` of the CT-e)         |
| `ICMSp`  | 1    | `1` when the own ICMS is shown on the document, else `2`                    |
| `ICMSs`  | 1    | `1` when the ICMS of the tax substitution is shown, else `2`                |
| `DD`     | 2    | the day of the emission                                                     |
| `DV`     | 1    | the check digit, modulo 11 as the one of the access key                     |

Pure: `build/1` takes what the data modules read from the XML and answers the digits and their
presentation.

# `input`

```elixir
@type input() :: %{
  optional(:uf) =&gt; String.t() | nil,
  optional(:emission_type) =&gt; integer() | nil,
  optional(:tax_id) =&gt; String.t() | nil,
  optional(:amount) =&gt; String.t() | nil,
  optional(:own_icms?) =&gt; boolean(),
  optional(:st_icms?) =&gt; boolean(),
  optional(:issued_at) =&gt; String.t() | nil
}
```

# `build`

```elixir
@spec build(input()) :: %{required(String.t()) =&gt; String.t()}
```

The code of a document: `%{"digits" => "...36 digits...", "formatted" => "9999 9999 ..."}`.

  * `:uf` - the UF of the recipient or sender (`"EX"` or none for a foreign one);
  * `:emission_type` - the `tpEmis`;
  * `:tax_id` - the CNPJ (14 digits) or CPF (11 digits, zero padded) of the same party, `nil`
    when foreign; punctuation is ignored;
  * `:amount` - the total value as the XML writes it (`"252.00"`);
  * `:own_icms?` and `:st_icms?` - the ICMS flags;
  * `:issued_at` - the `dhEmi` (ISO 8601), of which the day is taken.

# `check_digit`

```elixir
@spec check_digit(String.t()) :: 0..9
```

The check digit of `digits`: modulo 11 with weights 2 to 9 from the right, as the access key's. It
is `11 - rem(sum, 11)`, and 0 when the remainder is 0 or 1.

# `format`

```elixir
@spec format(String.t()) :: String.t()
```

The digits in blocks of four, separated by a space.

# `positive?`

```elixir
@spec positive?(String.t() | nil) :: boolean()
```

Whether a decimal as the XML writes it (`28.80`) is greater than zero.

# `uf_code`

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

The IBGE code of a UF; 99 for `EX`, an unknown UF or none.

---

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