## Overview

The **Contracts** module plugs into the platform's automation engines so you can **generate and send
contracts hands-free**, instead of clicking on every conversation. The same two actions are available
in **three places**:

- **Conversation Automations** — fire on an event/condition (e.g., the deal stage changed).
- **Macros** — you run the action manually, in one click, on a conversation.
- **Flow Builder** — the **Send contract** node inside a visual flow.

In all of them, the platform resolves the **template**, the **contact** and the **conversation** from
context, auto-fills the variables, **auto-signs the issuing company** (the contracted party) and
delivers the **signing link** to the contact whenever a conversation is linked.

## Prerequisites

- Contracts module **enabled** and a user with **permission** to manage contracts.
- At least one contract **template** already created.
- A **contact** (and, ideally, a **conversation**) on the trigger — that is where the signer comes
  from.
- An **issuing company** registered (used as the default contracted party and for auto-signing).
- To deliver the link through the channels, a WhatsApp or email **Inbox**.

## Step by step

### Conversation Automations

1. Go to **Settings → Automation** and create or edit a rule.
2. Define the **event** and the **conditions** that fire the rule.
3. Under **Actions**, pick one of the contract actions:
   - **Send contract from template** (`send_contract_from_template`): creates the contract from the
     template **and sends it for signature** right away.
   - **Create contract from template** (`create_contract_from_template`): only creates the contract as
     a **draft** for the agent to review before sending.
4. Set the **template** on the action. Save the rule.

### Macros

1. Go to **Settings → Macros** and create a macro.
2. Add the same contract action (**Send** or **Create** from template) and choose the template.
3. In the conversation, run the macro from the **Macros** menu to generate/send the contract manually.

### Flow Builder

1. In the flow editor, add the **Send contract** node.
2. Select the **template** (and, optionally, a **title**).
3. Connect the **success output** to the next step and the **failure output** to a fallback (e.g.,
   notify an agent).
4. Publish the flow. On execution, the node creates the contract (auto-fills variables, builds
   signers/items and picks the default issuer) and sends it.

## Settings & options

- **Action parameters**: `template_id` is required. Optionally, `company_id` (issuing company),
  `crm_item_id` (CRM deal) and `title` (contract title). When you omit the company, the platform uses
  the **default issuer**; contact and conversation come from the trigger.
- **Identical execution everywhere**: Automations and Macros share the exact same action set, so the
  behavior is identical — only the trigger differs (automatic, manual or by flow).
- **Contract-based conditions**: rules that respond to **contract events** can filter by
  `contract_status`, `contract_tier` (signature level), `contract_company` (issuing company) and
  `contract_total` (sum of items, in **whole currency units**). Available operators: equal to, not
  equal to, contains, does not contain, is present, is not present, is greater than, is less than. A
  rule with **no conditions** always runs; a misconfigured condition **fails safe** (it does not fire
  on everything).
- **Flow node outputs**: success moves forward (carrying the generated `contract_id`); failure exits
  through a separate path, identified by `contracts_not_enabled`, `contracts_template_missing`,
  `contracts_no_contact` or `send_contract_failed`.

## Use cases

- **Deal won in the CRM** → automatically send the service contract in the same conversation.
- **Stage change** (e.g., "Negotiating" → "Closing") → create the contract as a draft for the agent to
  review before sending.
- **Flow-driven service**: at the end of a qualification Flow Builder, the **Send contract** node
  issues the document and moves on to a billing step on success.

## Tips, limits & best practices

- Use **Create** when you want a human review before sending; use **Send** when the flow is already
  validated and can go straight to signature.
- In **Automations and Macros**, the actions **degrade silently**: if the module is disabled, the
  template is missing or there is no contact on the trigger, the action simply **does nothing** and
  writes a `[CONTRACTS_AUTOMATION]` log line — it **never breaks** the rest of the rule/macro.
- In **Flow Builder**, the same issue **does not interrupt** the flow: it leaves through the **failure
  output**, so always connect that path to a handler.
- Make sure the **source data** (contact, document, deal) is filled in so variable auto-fill works
  well.
- For the contracted party's auto-signature, keep the right **issuing company** (and a valid A1
  certificate, for qualified signatures).

## Troubleshooting

- **The rule ran but no contract appeared**: most likely a missing **template**, a missing **contact**
  on the conversation, or the **module is disabled**. Check the `[CONTRACTS_AUTOMATION]` log line.
- **The contract was created but not sent**: the action used was **Create from template** (it stays as
  a draft). Use **Send from template** to issue and send in one step.
- **The flow left through the failure output**: read the reason — `contracts_not_enabled` (module
  off), `contracts_template_missing` (template not set), `contracts_no_contact` (no contact) or
  `send_contract_failed` (delivery error).
- **The signer did not receive the link**: confirm there is a **linked conversation** and that the
  **channel** (WhatsApp/email) and the contact details are correct.

## See also

- [Contracts and Electronic Signature overview](/hc/ajuda/articles/contracts-esignature-overview-en)
- [Templates, variables and auto-fill](/hc/ajuda/articles/contracts-esignature-templates-variaveis-auto-fill-en)
- [Internal and external signing](/hc/ajuda/articles/contracts-esignature-assinatura-interna-externa-en)
- [Issuing companies and the A1 digital certificate](/hc/ajuda/articles/contracts-esignature-empresas-emissoras-certificado-a1-en)