# Execution Rules

Execution is a transaction that will perform, in a unique and atomic way, one or more financial entries (double-entries in debit and credit) and return the ID of the created transaction.

The rules for execution determine the accounting mapping of each entry involved and its order of execution, control of the balance of each part and which accounts will be received as an input parameter at the time of its creation, with up to two accounts permitted.

The rules for execution are controlled by transaction type, which are terms freely chosen by the user.

## Entries <a href="#entries" id="entries"></a>

The entries are the debits and credits that will be executed atomically in the transaction and must have their accounting functioning mapped.

### Entry types <a href="#entry-types" id="entry-types"></a>

The entry types are terms freely chosen by the user, except for the main\_amount type, which refers to the main value informed when creating the transaction. The main\_amount type is usually mapped as the first in a list of entries, but is not mandatory if the execution receives the value zero during its creation.

### Entry order <a href="#entry-order" id="entry-order"></a>

Each entry receives a number to order its sequential execution as determined by the user. Entries are carried out in this order, first debits and then credits.

### Source of accounts <a href="#source-of-accounts" id="source-of-accounts"></a>

For both debits and credits, the source of the account involved must be specified. This can be one of the accounts mapped and created as unique in the system (unique\_account) or originating from one of the accounts that were informed in the transaction input parameters (param\_account\_1 or param\_account\_2).

### Accounts descriptions <a href="#accounts-descriptions" id="accounts-descriptions"></a>

For both debits and credits, the description of the account involved must be specified. The description must exist in the account rules record.

### Balance type <a href="#balance-type" id="balance-type"></a>

For both debits and credits, you must specify the type of balance to be moved. The balance type must exist, for that account description, in the account rules record. The permitted types are available, pending and blocked.

### Balance validation <a href="#balance-validation" id="balance-validation"></a>

For both debits and credits, you must specify what type of balance validation will be carried out. The allowed validations are:

| Validation     | Description                                                      |
| -------------- | ---------------------------------------------------------------- |
| positive       | the system will not allow the desired balance to become negative |
| negative       | the system will not allow the desired balance to become positive |
| no\_validation | the system will not validate the desired balance                 |

## Create or Update an Execution Rule <a href="#create-or-update-an-execution-rule" id="create-or-update-an-execution-rule"></a>

The PUT /execution\_rules method is used to create or update an execution rule individually or in batch, based on the transaction\_type field.

Example of creating/updating rules:

```json
PUT /execution_rules

{
  "data": [
    {
      "transaction_type": "pix_in",
      "param_account_1": true,
      "param_account_2": false,
      "entries": [
        {
          "entry_type": "main_amount",
          "entry_order": 1,
          "debit_account_source": "unique_account",
          "debit_account_description": "spi",
          "debit_balance_type": "available",
          "debit_balance_validation": "negative",
          "credit_account_source": "param_account_1",
          "credit_account_description": "payment_account",
          "credit_balance_type": "available",
          "credit_balance_validation": "no_validation"
        }
      ]
    }  
  ]
}
```

## Delete of an Execution Rule <a href="#delete-of-an-execution-rule" id="delete-of-an-execution-rule"></a>

The DELETE /execution\_rules method is used do delete an execution rule individually or in batch.

Example of deleting rules:

```json
DELETE /execution_rules?transaction_types=[<transaction_types>]
```

## Execution Rules query <a href="#execution-rules-query" id="execution-rules-query"></a>

The GET /execution\_rules method is used to query the rules.

Rules query example:

```json
GET /execution_rules
```

Example response:

```json
{
  "data": [
    {
      "transaction_type": "pix_in",
      "param_account_1": true,
      "param_account_2": false,
      "entries": [
        {
          "entry_type": "main_amount",
          "entry_order": 1,
          "debit_account_source": "unique_account",
          "debit_account_description": "spi",
          "debit_balance_type": "available",
          "debit_balance_validation": "negative",
          "credit_account_source": "param_account_1",
          "credit_account_description": "payment_account",
          "credit_balance_type": "available",
          "credit_balance_validation": "no_validation"
        }
      ]
    }  
  ]
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ledgera.gitbook.io/api/setup/execution-rules.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
