# `Discount`

An amount off a price, or a percentage off it.

Use it for promotions and vouchers. `money.apply_discount` takes one off a [`Price`](/docs/types/type-price.md).

## Shape

One of these, each with its own fields:

| Variant | Field | Type | Required? |
|---|---|---|---|
| `Discount.amount_off` | `amount` | [`Money`](/docs/types/type-money.md) | Yes |
| `Discount.percentage_off` | `percentage` | [`Percentage`](/docs/types/type-percentage.md) | Yes |

## Example

```jadpo
Discount.percentage_off { percentage: 20% }
```

## Good to know

- The other variant is `Discount.amount_off { amount: ... }`.
- An amount off can't be below zero, and a percentage off runs from 0% to 100%.
- `==` tells you whether two are the same. `<` and `list.sort_by` don't take it.

## In JSON

An object of its variant's name, as `tag`, and the variant's fields, in a request, a response or `json.write`:

```json
{"tag":"amount_off","amount":{"amount":500,"currency":"GBP"}}
{"tag":"percentage_off","percentage":"20%"}
{"tag":"percentage_off","percentage":"12.5%"}
```

## Details

<details>
<summary>Text form, storage, how it is shown, and operations</summary>

`Discount` is in the group Money and banking, and has been in Jadpo since 0.1.0.

### Text form

It has no single text form, so `text.from` and `text.parse` don't take it.

### How it is stored

It is stored in 4 columns, each named for the field, `_` and its suffix. This is `{"tag":"percentage_off","percentage":"20%"}`:

| Column | Type | Holds |
|---|---|---|
| `<field>_kind` | `text` | `'percentage_off'` |
| `<field>_amount` | `bigint` | `NULL::bigint` |
| `<field>_currency` | `char(3)` | `NULL::text` |
| `<field>_percentage` | `numeric` | `0.20` |

### How it is shown to people

`text.format(value, locale:)` shows it to people in their locale.

With `locale: Locale.en_gb`:

| Value | Shown |
|---|---|
| `{"tag":"amount_off","amount":{"amount":500,"currency":"GBP"}}` | `£5.00` |
| `{"tag":"percentage_off","percentage":"20%"}` | `20%` |
| `{"tag":"percentage_off","percentage":"12.5%"}` | `12.5%` |

### Operations

- `money.apply_discount(price: Price, discount: Discount) -> Price`: `price` with `discount` taken off its amount, keeping whether it includes tax: an amount off in the price's currency, or that percentage of the amount, rounded half to even to a minor unit, so 15% off £10.05 is £1.51 off. More off than the price leaves nothing. It fails with `currency_mismatch` for an amount off in another currency.

It also takes `text.format`, `json.read`, `json.write` and `==`. [How every standard type behaves](/docs/every-standard-type-meets-one-contract.md) says how they work.

</details>
