# Using the rules editor

Some decisions in Breww are best described as a flowchart: "if the order is large, deliver it tomorrow, otherwise deliver it in 3 days". The **rules editor** is where you build these. You place questions and answers on a canvas, then draw lines between them to show Breww which way to go.

This guide uses delivery rules as the example, but the editor works in the same way wherever you find it:

| Rules                                | Where to find them                                                                           | Outcome block   | What the options mean                                                        |
| ------------------------------------ | -------------------------------------------------------------------------------------------- | --------------- | ---------------------------------------------------------------------------- |
| **Delivery rules**                   | **Settings** -> **Delivery settings** -> **Delivery rules**                                  | **Delivery**    | [Delivery rules](https://breww.com/docs/delivery-rules/)                     |
| **Best before rules from packaging** | **Settings** -> **Production settings** -> **Best before rules** -> **Rules from packaging** | **Best before** | [Best before/expiry dates](https://breww.com/docs/best-before_expiry-dates/) |
| **Best before rules from delivery**  | **Settings** -> **Production settings** -> **Best before rules** -> **Rules from delivery**  | **Best before** | [Best before/expiry dates](https://breww.com/docs/best-before_expiry-dates/) |
| **Purchase order approval rules**    | **Settings** -> **Stock items & purchasing** -> **Purchase order approval rules**            | **Approval**    | [Purchase order approval](https://breww.com/docs/purchase-order-approval/)   |

You'll need the **Settings** permission to open any of these editors.

> **Note:**
> The rules editor is much easier to use on a desktop computer or another device with a wide screen.

## How rules work

Every set of rules is made from three kinds of block:

- **Start** - where Breww begins. There is always exactly one, and it can't be deleted.
- **Condition** - a yes or no question, such as "Is this a large order?". It has one connector on top for the line coming in, and two connectors underneath for the lines going out: a **green check mark** for "yes" and a **red cross** for "no".
- **Outcome** - the answer at the end of a path. It's called **Delivery**, **Best before** or **Approval**, depending on which rules you're editing. It only has a connector on top, because nothing comes after it.

![A condition block called "By 10am?", with an input connector on top and a green check mark and red cross output underneath](https://storage.googleapis.com/breww-user-uploads/markdown-embedded/433a8f9b1fc5245128aa5c5591f36c9d9e87985588bd5a2e2c169f842f2f747c.png)

Whenever Breww uses your rules, it starts at **Start** and follows the line to the next block. At each condition it answers the question, then follows the green check mark line for "yes" or the red cross line for "no". It carries on until it reaches an outcome, and uses that outcome.

A few things follow from this:

- **Only one outcome is ever used.** Breww follows a single path, so it never combines two outcomes.
- **The order of your conditions matters.** Ask your most specific questions first, and let the last outcome on a path act as your catch-all.
- **Where blocks sit on the canvas makes no difference.** Only the lines between them matter, so arrange them however is easiest for you to read.
- **Changes save as you make them.** There's no save button for the canvas itself.

## A worked example

In this guide, we'll build delivery rules that do the following:

- If the rules run by 10am, deliver the next day.
- If it's after 10am, still deliver the next day for large orders (worth 500 or more).
- Deliver everything else in 3 days.

To follow along, go to **Settings** -> **Delivery settings** -> **Delivery rules**. The **Start** block is already on the canvas, and the panel in the top-left corner lists the blocks you can add.

## Adding a condition

1. Drag **Condition** from the panel in the top-left corner and drop it onto the canvas.
2. Enter a **Name**. This is shown on the block, so phrasing it as a question makes your rules easier to read, for example "By 10am?".
3. Under **Conditions**, choose what to check from the first dropdown (here, **Time of day**), then choose how to compare it (**less or equal**) and enter the value (`10:00`).
4. Press **Save**. The condition appears on the canvas where you dropped it.

![The add delivery condition form, checking whether the time of day is less than or equal to 10:00](https://storage.googleapis.com/breww-user-uploads/markdown-embedded/15542103bc332d127b8a905bb68e084d9ca46cbbcf52d5dbebace5d802fd76de.png)

A single condition can check more than one thing:

- **Add rule** adds another check. Choose **AND** if every check must be true, or **OR** if any one of them is enough.
- **Add group** adds a set of checks in brackets, so you can build questions such as "(customer tag is Priority OR total value is over 500) AND day of week is Friday".
- **Delete** removes a check.

Breww won't save a condition until every check in it is complete.

For our example, add a second condition called "Large order?" that checks **Total value** is **greater or equal** to `500`.

The options you can check depend on which rules you're editing. The guides linked in the table above explain each one.

## Adding an outcome

1. Drag the outcome block (for delivery rules, **Delivery**) from the panel and drop it onto the canvas.
2. Fill in the form to say what should happen. The optional **Label** is only there to help you recognise the block on the canvas, and has no effect on your rules.
3. Press **Save**.

![The add delivery action form, set to schedule an own vehicle delivery 1 day after the order date](https://storage.googleapis.com/breww-user-uploads/markdown-embedded/2fb9f52802b8ae90d1439d4cf5cd57f5cb6f997e21a0348648327eb12ee65b53.png)

For our example, add two **Delivery** outcomes: "Next day delivery", set to 1 day, and "Delivery in 3 days", set to 3 days.

## Drawing lines between blocks

Blocks don't do anything until they're joined up. To draw a line:

1. Press and hold on a connector underneath a block. This is the connector under **Start**, or the green check mark or red cross under a condition.
2. Drag to the round connector on top of the block that should come next.
3. Let go. The line stays in place and is saved straight away.

![A line being dragged from the red cross of the "Large order?" condition towards the "Delivery in 3 days" outcome](https://storage.googleapis.com/breww-user-uploads/markdown-embedded/205f73d5baf5b10d0803559423ef45a3fbf2a8a0ebdc3d65c7627c3c2c35b8e7.png)

Some things to know about lines:

- **Each output can only have one line.** If you draw a new line from an output that already has one, the new line replaces the old one. This is the quickest way to reroute a path.
- **A block can have many lines coming into it.** Several paths can finish at the same outcome, which keeps your rules tidy. In our example, both "yes" answers lead to "Next day delivery".
- **Never draw a line back to an earlier block.** Breww would go round in circles and never reach an outcome.

For our example, draw these lines:

1. **Start** to "By 10am?".
2. The green check mark of "By 10am?" to "Next day delivery".
3. The red cross of "By 10am?" to "Large order?".
4. The green check mark of "Large order?" to "Next day delivery".
5. The red cross of "Large order?" to "Delivery in 3 days".

![Complete delivery rules, where every output of every condition is connected and every path ends at a Delivery outcome](https://storage.googleapis.com/breww-user-uploads/markdown-embedded/7a6adbcdcd294ff3399acae143add1852d6557661f80f2e8873213d0cea3cbed.png)

## Changing your rules

- **Edit a block:** press the pencil button on it.
- **Move a block:** drag it by its title.
- **Delete a block:** press the bin button on it and confirm. Any lines going into or out of it are removed as well. The **Start** block can't be deleted.
- **Remove a line:** click the line to select it, then press `Delete` (or `Cmd` + `Backspace` on a Mac). If nothing happens, click an empty part of the canvas first and then select the line again.
- **Move around the canvas:** drag an empty part of the canvas. Hold `Ctrl` and scroll to zoom in and out.

## Don't leave dead ends

> **Important:**
> **Every path must finish at an outcome.** A connector with no line coming out of it is a dead end, and Breww won't guess what to do when it reaches one.

A dead end is any output with nothing connected to it: the connector under **Start**, or a green check mark or red cross under a condition.

In the example below, the red cross under "Large order?" has no line. Any order that arrives after 10am and is worth less than 500 has nowhere to go. The "Delivery in 3 days" block is on the canvas, but because nothing leads to it, it will never be used.

![Delivery rules with a dead end: the red cross under the "Large order?" condition isn't connected to anything](https://storage.googleapis.com/breww-user-uploads/markdown-embedded/89be250f483630824f8963ebedeb7d8556e0bc84a88f62439b279a0c10eab0fa.png)

### What happens when Breww reaches a dead end

When Breww follows a path and finds nothing connected, it can't produce a result at all. It doesn't fall back to another outcome, and it doesn't skip the condition. Instead, it treats your rules as invalid for that particular order, product or purchase order.

**Dead ends are easy to miss, because nothing goes wrong until something actually takes that path.** Your rules can work perfectly for weeks, then fail on the first order that answers "no" to a question you never finished.

What you'll see depends on the rules:

- **Delivery rules:** if your rules run when an order is confirmed, the order isn't confirmed and you'll see a message asking you to check your delivery rules. Elsewhere, such as when an order is paid in full or placed on your Trade Store, no delivery is scheduled. See [Delivery rules](https://breww.com/docs/delivery-rules/) for the full details.
- **Best before rules from packaging:** Breww can't work out a best before date, so you'll need to enter it yourself when packaging, and labels are printed without one.
- **Best before rules from delivery:** delivery labels show no best before date.
- **Purchase order approval rules:** the purchase order can't be finalised, and you'll see an "Invalid purchase order approval rules" error.

Blocks with nothing coming into them, like "Delivery in 3 days" above, don't cause errors because Breww never reaches them. They do make your rules harder to follow, though, so connect them or delete them.

### Checking your rules for dead ends

After every change, check your rules before you rely on them:

1. Make sure **Start** has a line coming out of it.
2. Make sure **every condition has two lines coming out of it**: one from the green check mark and one from the red cross.
3. Follow each path from **Start** and check it finishes at an outcome.

If you're editing best before rules from packaging, the **Best before date preview** button above the canvas also helps. Any product without a date is one your rules don't currently cover.
